File indexing completed on 2024-04-21 04:47:49
0001 /**************************************************************************************** 0002 * Copyright (c) 2008 Nicos Gollan <gtdev@spearhead.de> * 0003 * Copyright (c) 2008 Téo Mrnjavac <teo@kde.org> * 0004 * * 0005 * This program is free software; you can redistribute it and/or modify it under * 0006 * the terms of the GNU General Public License as published by the Free Software * 0007 * Foundation; either version 2 of the License, or (at your option) any later * 0008 * version. * 0009 * * 0010 * This program is distributed in the hope that it will be useful, but WITHOUT ANY * 0011 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * 0012 * PARTICULAR PURPOSE. See the GNU General Public License for more details. * 0013 * * 0014 * You should have received a copy of the GNU General Public License along with * 0015 * this program. If not, see <http://www.gnu.org/licenses/>. * 0016 ****************************************************************************************/ 0017 0018 #define DEBUG_PREFIX "CaseConverter" 0019 0020 #include "CaseConverter.h" 0021 0022 #include "core/support/Debug.h" 0023 0024 #include <QObject> 0025 #include <QString> 0026 #include <QRegExp> 0027 0028 namespace Amarok 0029 { 0030 const QString CaseConverter::s_MATCH_A_WORD( "\\b([\\w']+)\\b" ); 0031 const QString CaseConverter::s_LITTLE_WORDS( "\\b(a|an|and|as|at|by|for|if|in|of|on|or|to|the)\\b" ); 0032 0033 QString 0034 CaseConverter::toTitleCase( const QString &s ) 0035 { 0036 QString result = s; 0037 debug() << "Original string: " << s; 0038 0039 QRegExp wordRegExp( CaseConverter::s_MATCH_A_WORD ); 0040 int i = wordRegExp.indexIn( result ); 0041 QString match = wordRegExp.cap( 1 ); 0042 bool first = true; 0043 0044 QRegExp littleWordRegExp( CaseConverter::s_LITTLE_WORDS ); 0045 while ( i > -1 ) 0046 { 0047 debug() << " Title case i=" << i << "; remaining: \"" << result.mid( i ) << "\""; 0048 0049 // uppercase if: 0050 // * no uppercase letters in partial AND first partial 0051 // OR 0052 // * no uppercase letters in partial AND not a "little" word 0053 if ( match == match.toLower() && ( first || !littleWordRegExp.exactMatch( match ) ) ) 0054 { 0055 result[i] = result[i].toUpper(); 0056 } 0057 else 0058 { 0059 debug() << " partial will not be capitalized: \"" << match << "\""; 0060 } 0061 0062 i = wordRegExp.indexIn( result, i + match.length() ); 0063 match = wordRegExp.cap( 1 ); 0064 first = false; 0065 } 0066 0067 debug() << " Title case of \"" << s << "\" = \"" << result << "\""; 0068 return result; 0069 } 0070 0071 QString 0072 CaseConverter::toCapitalizedCase( const QString &s ) 0073 { 0074 QString result = s; 0075 QRegExp wordRegExp( CaseConverter::s_MATCH_A_WORD ); 0076 int i = wordRegExp.indexIn( result ); 0077 int ml = wordRegExp.cap( 1 ).length(); 0078 while ( i > -1 ) 0079 { 0080 result[i] = result[i].toUpper(); 0081 i = wordRegExp.indexIn( result, i + ml ); 0082 ml = wordRegExp.cap( 1 ).length(); 0083 } 0084 return result; 0085 } 0086 }