00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <qtopia/stringutil.h>
00022 #include <qregexp.h>
00023 #include <qstringlist.h>
00024
00025 namespace Qtopia
00026 {
00027
00028
00029
00030
00031
00032
00033
00034
00035 static const char collationHack[] = {
00036 0x00,
00037 0x01,
00038 0x02,
00039 0x03,
00040 0x04,
00041 0x05,
00042 0x06,
00043 0x07,
00044 0x08,
00045 0x09,
00046 0x0a,
00047 0x0b,
00048 0x0c,
00049 0x0d,
00050 0x0e,
00051 0x0f,
00052 0x10,
00053 0x11,
00054 0x12,
00055 0x13,
00056 0x14,
00057 0x15,
00058 0x16,
00059 0x17,
00060 0x18,
00061 0x19,
00062 0x1a,
00063 0x1b,
00064 0x1c,
00065 0x1d,
00066 0x1e,
00067 0x1f,
00068 ' ',
00069 '!',
00070 '"',
00071 '#',
00072 '$',
00073 '%',
00074 '&',
00075 '\'',
00076 '(',
00077 ')',
00078 '*',
00079 '+',
00080 ',',
00081 '-',
00082 '.',
00083 '/',
00084 0x80,
00085 0x81,
00086 0x82,
00087 0x83,
00088 0x84,
00089 0x85,
00090 0x86,
00091 0x87,
00092 0x88,
00093 0x89,
00094 ':',
00095 ';',
00096 '<',
00097 '=',
00098 '>',
00099 '?',
00100 '@',
00101 'A',
00102 'B',
00103 'C',
00104 'D',
00105 'E',
00106 'F',
00107 'G',
00108 'H',
00109 'I',
00110 'J',
00111 'K',
00112 'L',
00113 'M',
00114 'N',
00115 'O',
00116 'P',
00117 'Q',
00118 'R',
00119 'S',
00120 'T',
00121 'U',
00122 'V',
00123 'W',
00124 'X',
00125 'Y',
00126 'Z',
00127 '[',
00128 '\\',
00129 ']',
00130 '^',
00131 '_',
00132 '`',
00133 'A',
00134 'B',
00135 'C',
00136 'D',
00137 'E',
00138 'F',
00139 'G',
00140 'H',
00141 'I',
00142 'J',
00143 'K',
00144 'L',
00145 'M',
00146 'N',
00147 'O',
00148 'P',
00149 'Q',
00150 'R',
00151 'S',
00152 'T',
00153 'U',
00154 'V',
00155 'W',
00156 'X',
00157 'Y',
00158 'Z',
00159 '{',
00160 '|',
00161 '}',
00162 '~',
00163 '',
00164 0x80,
00165 0x81,
00166 0x82,
00167 0x83,
00168 0x84,
00169 0x85,
00170 0x86,
00171 0x87,
00172 0x88,
00173 0x89,
00174 0x8a,
00175 0x8b,
00176 0x8c,
00177 0x8d,
00178 0x8e,
00179 0x8f,
00180 0x90,
00181 0x91,
00182 0x92,
00183 0x93,
00184 0x94,
00185 0x95,
00186 0x96,
00187 0x97,
00188 0x98,
00189 0x99,
00190 0x9a,
00191 0x9b,
00192 0x9c,
00193 0x9d,
00194 0x9e,
00195 0x9f,
00196 ' ',
00197 '¡',
00198 '¢',
00199 '£',
00200 '¤',
00201 '¥',
00202 '¦',
00203 '§',
00204 '¨',
00205 '©',
00206 'A',
00207 '«',
00208 '¬',
00209 '',
00210 '®',
00211 '¯',
00212 'O',
00213 '±',
00214 '²',
00215 '³',
00216 '´',
00217 'µ',
00218 'P',
00219 '·',
00220 '¸',
00221 '¹',
00222 'O',
00223 '»',
00224 '¼',
00225 '½',
00226 '¾',
00227 '¿',
00228 'A',
00229 'A',
00230 'A',
00231 'A',
00232 'A',
00233 'A',
00234 'A',
00235 'C',
00236 'E',
00237 'E',
00238 'E',
00239 'E',
00240 'I',
00241 'I',
00242 'I',
00243 'I',
00244 'D',
00245 'N',
00246 'O',
00247 'O',
00248 'O',
00249 'O',
00250 'O',
00251 '×',
00252 'O',
00253 'U',
00254 'U',
00255 'U',
00256 'U',
00257 'Y',
00258 'T',
00259 'S',
00260 'A',
00261 'A',
00262 'A',
00263 'A',
00264 'A',
00265 'A',
00266 'A',
00267 'C',
00268 'E',
00269 'E',
00270 'E',
00271 'E',
00272 'I',
00273 'I',
00274 'I',
00275 'I',
00276 'D',
00277 'N',
00278 'O',
00279 'O',
00280 'O',
00281 'O',
00282 'O',
00283 '÷',
00284 'O',
00285 'U',
00286 'U',
00287 'U',
00288 'U',
00289 'Y',
00290 'T',
00291 'Y',
00292 };
00293
00294
00295
00296
00297
00298 static void hackString ( QString &s )
00299 {
00300 int len = s.length();
00301 const QChar* uc = s.unicode();
00302 for ( int i = 0; i < len; i++ ) {
00303 if ( !uc++->row() )
00304 s[i] = collationHack[s[i].cell()];
00305 }
00306 }
00307
00308 QString buildSortKey( const QString & s )
00309 {
00310 QString res = s;
00311 hackString( res );
00312 return res;
00313 }
00314
00315 QString buildSortKey( const QString & s1, const QString & s2 )
00316 {
00317 QString res = s1 + QChar( '\0' ) + s2;
00318 hackString( res );
00319 return res;
00320 }
00321
00322 QString buildSortKey( const QString & s1, const QString & s2,
00323 const QString & s3 )
00324 {
00325 QString res = s1 + QChar( '\0' ) + s2 + QChar( '\0' ) + s3;
00326 hackString( res );
00327 return res;
00328 }
00329
00330 static inline QChar coll( QChar u )
00331 {
00332 return u.row() ? u : QChar(collationHack[ u.cell() ]);
00333 }
00334
00335
00336 int compare( const QString & s1, const QString & s2 )
00337 {
00338 const QChar* u1 = s1.unicode();
00339 const QChar* u2 = s2.unicode();
00340
00341 if ( u1 == u2 )
00342 return 0;
00343 if ( u1 == 0 )
00344 return 1;
00345 if ( u2 == 0 )
00346 return -1;
00347 int l=QMIN(s1.length(),s2.length());
00348 while ( l-- && coll(*u1) == coll(*u2) )
00349 u1++,u2++;
00350 if ( l==-1 )
00351 return ( s1.length()-s2.length() );
00352 return u1->unicode() - u2->unicode();
00353 }
00354
00355 QString simplifyMultiLineSpace( const QString &multiLine )
00356 {
00357 QString result;
00358 QStringList lines = QStringList::split("\n", multiLine);
00359 for ( QStringList::Iterator it = lines.begin(); it != lines.end(); ++it ) {
00360 if ( it != lines.begin() )
00361 result += "\n";
00362 result += (*it).simplifyWhiteSpace();
00363 }
00364 return result;
00365 }
00366
00367 QString escapeString( const QString& plain )
00368 {
00369 QString tmp(plain);
00370 int pos = tmp.length();
00371 const QChar *uc = plain.unicode();
00372 while ( pos-- ) {
00373 unsigned char ch = uc[pos].latin1();
00374 if ( ch == '&' )
00375 tmp.replace( pos, 1, "&" );
00376 else if ( ch == '<' )
00377 tmp.replace( pos, 1, "<" );
00378 else if ( ch == '>' )
00379 tmp.replace( pos, 1, ">" );
00380 else if ( ch == '\"' )
00381 tmp.replace( pos, 1, """ );
00382 }
00383 return tmp;
00384 }
00385
00386 QString plainString( const char* escaped, unsigned int length )
00387 {
00388 return plainString( QString::fromUtf8( escaped, length ) );
00389 }
00390
00391 QString plainString( const QCString& string )
00392 {
00393
00394 return plainString( string.data(), string.length() );
00395 }
00396
00397 QString plainString( const QString& string )
00398 {
00399 QString tmp( string );
00400 int pos = -1;
00401 while ( (pos = tmp.find( "&", pos +1 ) ) != -1 ) {
00402 if ( tmp.find( "&", pos ) == pos )
00403 tmp.replace( pos, 5, "&" );
00404 else if ( tmp.find( "<", pos ) == pos )
00405 tmp.replace( pos, 4, "<" );
00406 else if( tmp.find( ">", pos ) == pos )
00407 tmp.replace( pos, 4, ">" );
00408 else if ( tmp.find( """, pos ) == pos )
00409 tmp.replace( pos, 6, "\"" );
00410 }
00411 return tmp;
00412 }
00413
00414 }