| 1 |
/****************************************************************************** |
| 2 |
* Copyright (C) 2008 Nicos Gollan <gtdev@spearhead.de> * |
| 3 |
* 2008 Teo Mrnjavac <teo.mrnjavac@gmail.com> * |
| 4 |
* * |
| 5 |
* This program is free software; you can redistribute it and/or * |
| 6 |
* modify it under the terms of the GNU General Public License as * |
| 7 |
* published by the Free Software Foundation; either version 2 of * |
| 8 |
* the License, or (at your option) any later version. * |
| 9 |
* * |
| 10 |
* This program is distributed in the hope that it will be useful, * |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 13 |
* GNU General Public License for more details. * |
| 14 |
* * |
| 15 |
* You should have received a copy of the GNU General Public License * |
| 16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. * |
| 17 |
******************************************************************************/ |
| 18 |
#define DEBUG_PREFIX "CaseConverter" |
| 19 |
|
| 20 |
#include "CaseConverter.h" |
| 21 |
|
| 22 |
#include "Debug.h" |
| 23 |
|
| 24 |
#include <QObject> |
| 25 |
#include <QString> |
| 26 |
#include <QRegExp> |
| 27 |
|
| 28 |
namespace Amarok |
| 29 |
{ |
| 30 |
const QString CaseConverter::s_MATCH_A_WORD( "\\b([\\w']+)\\b" ); |
| 31 |
const QString CaseConverter::s_LITTLE_WORDS( "\\b(a|an|as|at|by|for|if|and|of|or|to|the|in)\\b" ); |
| 32 |
|
| 33 |
QString |
| 34 |
CaseConverter::toTitleCase( const QString &s ) |
| 35 |
{ |
| 36 |
QString result = s; |
| 37 |
debug() << "Original string: " << s; |
| 38 |
|
| 39 |
QRegExp wordRegExp( CaseConverter::s_MATCH_A_WORD ); |
| 40 |
int i = wordRegExp.indexIn( result ); |
| 41 |
QString match = wordRegExp.cap( 1 ); |
| 42 |
bool first = true; |
| 43 |
|
| 44 |
QRegExp littleWordRegExp( CaseConverter::s_LITTLE_WORDS ); |
| 45 |
while ( i > -1 ) |
| 46 |
{ |
| 47 |
debug() << " Title case i=" << i << "; remaining: \"" << result.mid( i ) << "\""; |
| 48 |
|
| 49 |
// uppercase if: |
| 50 |
// * no uppercase letters in partial AND first partial |
| 51 |
// OR |
| 52 |
// * no uppercase letters in partial AND not a "little" word |
| 53 |
if ( match == match.toLower() && ( first || !littleWordRegExp.exactMatch( match ) ) ) |
| 54 |
{ |
| 55 |
result[i] = result[i].toUpper(); |
| 56 |
} |
| 57 |
else |
| 58 |
{ |
| 59 |
debug() << " partial will not be capitalized: \"" << match << "\""; |
| 60 |
} |
| 61 |
|
| 62 |
i = wordRegExp.indexIn( result, i + match.length() ); |
| 63 |
match = wordRegExp.cap( 1 ); |
| 64 |
first = false; |
| 65 |
} |
| 66 |
|
| 67 |
debug() << " Title case of \"" << s << "\" = \"" << result << "\""; |
| 68 |
return result; |
| 69 |
} |
| 70 |
|
| 71 |
QString |
| 72 |
CaseConverter::toCapitalizedCase( const QString &s ) |
| 73 |
{ |
| 74 |
QString result = s; |
| 75 |
QRegExp wordRegExp( CaseConverter::s_MATCH_A_WORD ); |
| 76 |
int i = wordRegExp.indexIn( result ); |
| 77 |
int ml = wordRegExp.cap( 1 ).length(); |
| 78 |
while ( i > -1 ) |
| 79 |
{ |
| 80 |
result[i] = result[i].toUpper(); |
| 81 |
i = wordRegExp.indexIn( result, i + ml ); |
| 82 |
ml = wordRegExp.cap( 1 ).length(); |
| 83 |
} |
| 84 |
return result; |
| 85 |
} |
| 86 |
} |