| 1 |
/**************************************************************************************** |
| 2 |
* Copyright (c) 2008 Nicos Gollan <gtdev@spearhead.de> * |
| 3 |
* Copyright (c) 2008 Téo Mrnjavac <teo@kde.org> * |
| 4 |
* * |
| 5 |
* This program is free software; you can redistribute it and/or modify it under * |
| 6 |
* the terms of the GNU General Public License as published by the Free Software * |
| 7 |
* Foundation; either version 2 of the License, or (at your option) any later * |
| 8 |
* version. * |
| 9 |
* * |
| 10 |
* This program is distributed in the hope that it will be useful, but WITHOUT ANY * |
| 11 |
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * |
| 12 |
* PARTICULAR PURPOSE. See the GNU General Public License for more details. * |
| 13 |
* * |
| 14 |
* You should have received a copy of the GNU General Public License along with * |
| 15 |
* this program. If not, see <http://www.gnu.org/licenses/>. * |
| 16 |
****************************************************************************************/ |
| 17 |
|
| 18 |
#define DEBUG_PREFIX "CaseConverter" |
| 19 |
|
| 20 |
#include "CaseConverter.h" |
| 21 |
|
| 22 |
#include "core/support/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|and|as|at|by|for|if|in|of|on|or|to|the)\\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 |
} |