Qtopia library API Documentation

stringutil.cpp

00001 /**********************************************************************
00002 ** Copyright (C) 2000-2002 Trolltech AS.  All rights reserved.
00003 **
00004 ** This file is part of the Qtopia Environment.
00005 **
00006 ** This file may be distributed and/or modified under the terms of the
00007 ** GNU General Public License version 2 as published by the Free Software
00008 ** Foundation and appearing in the file LICENSE.GPL included in the
00009 ** packaging of this file.
00010 **
00011 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00012 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00013 **
00014 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
00015 **
00016 ** Contact info@trolltech.com if any conditions of this licensing are
00017 ** not clear to you.
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   Very, very simple Latin-1 only collation guaranteed to displease anyone
00032   who actually uses the non-ASCII characters.
00033  */
00034 
00035 static const char  collationHack[] = {
00036 0x00, //C-@
00037 0x01, //C-A
00038 0x02, //C-B
00039 0x03, //C-C
00040 0x04, //C-D
00041 0x05, //C-E
00042 0x06, //C-F
00043 0x07, //C-G
00044 0x08, //C-H
00045 0x09, //C-I
00046 0x0a, //C-J
00047 0x0b, //C-K
00048 0x0c, //C-L
00049 0x0d, //C-M
00050 0x0e, //C-N
00051 0x0f, //C-O
00052 0x10, //C-P
00053 0x11, //C-Q
00054 0x12, //C-R
00055 0x13, //C-S
00056 0x14, //C-T
00057 0x15, //C-U
00058 0x16, //C-V
00059 0x17, //C-W
00060 0x18, //C-X
00061 0x19, //C-Y
00062 0x1a, //C-Z
00063 0x1b, //C-[
00064 0x1c, //C-\ 
00065 0x1d, //C-]
00066 0x1e, //C-^
00067 0x1f, //C-_
00068 ' ', // 
00069 '!', 
00070 '"', //"
00071 '#', //#
00072 '$', //$
00073 '%', //%
00074 '&', //&
00075 '\'', //'
00076 '(', //(
00077 ')', //)
00078 '*', //*
00079 '+', //+
00080 ',', //,
00081 '-', //-
00082 '.', //.
00083 '/', 
00084 0x80, //0
00085 0x81, //1
00086 0x82, //2
00087 0x83, //3
00088 0x84, //4
00089 0x85, //5
00090 0x86, //6
00091 0x87, //7
00092 0x88, //8
00093 0x89, //9
00094 ':', //:
00095 ';', //;
00096 '<', //<
00097 '=', //=
00098 '>', //>
00099 '?', //?
00100 '@', //@
00101 'A', //A
00102 'B', //B
00103 'C', //C
00104 'D', //D
00105 'E', //E
00106 'F', //F
00107 'G', //G
00108 'H', //H
00109 'I', //I
00110 'J', //J
00111 'K', //K
00112 'L', //L
00113 'M', //M
00114 'N', //N
00115 'O', //O
00116 'P', //P
00117 'Q', //Q
00118 'R', //R
00119 'S', //S
00120 'T', //T
00121 'U', //U
00122 'V', //V
00123 'W', //W
00124 'X', //X
00125 'Y', //Y
00126 'Z', //Z
00127 '[', //[
00128 '\\', //\ 
00129 ']', //]
00130 '^', //^
00131 '_', //_
00132 '`', //`
00133 'A', //a
00134 'B', //b
00135 'C', //c
00136 'D', //d
00137 'E', //e
00138 'F', //f
00139 'G', //g
00140 'H', //h
00141 'I', //i
00142 'J', //j
00143 'K', //k
00144 'L', //l
00145 'M', //m
00146 'N', //n
00147 'O', //o
00148 'P', //p
00149 'Q', //q
00150 'R', //r
00151 'S', //s
00152 'T', //t
00153 'U', //u
00154 'V', //v
00155 'W', //w
00156 'X', //x
00157 'Y', //y
00158 'Z', //z
00159 '{', //{
00160 '|', //|
00161 '}', //}
00162 '~', //~
00163 '', //
00164 0x80, //C-M-@
00165 0x81, //C-M-A
00166 0x82, //C-M-B
00167 0x83, //C-M-C
00168 0x84, //C-M-D
00169 0x85, //C-M-E
00170 0x86, //C-M-F
00171 0x87, //C-M-G
00172 0x88, //C-M-H
00173 0x89, //C-M-I
00174 0x8a, //C-M-J
00175 0x8b, //C-M-K
00176 0x8c, //C-M-L
00177 0x8d, //C-M-M
00178 0x8e, //C-M-N
00179 0x8f, //C-M-O
00180 0x90, //C-M-P
00181 0x91, //C-M-Q
00182 0x92, //C-M-R
00183 0x93, //C-M-S
00184 0x94, //C-M-T
00185 0x95, //C-M-U
00186 0x96, //C-M-V
00187 0x97, //C-M-W
00188 0x98, //C-M-X
00189 0x99, //C-M-Y
00190 0x9a, //C-M-Z
00191 0x9b, //C-M-[
00192 0x9c, //C-M-\ 
00193 0x9d, //C-M-]
00194 0x9e, //C-M-^
00195 0x9f, //C-M-_
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, "&amp;" );
00376     else if ( ch == '<' )
00377         tmp.replace( pos, 1, "&lt;" );
00378     else if ( ch == '>' )
00379         tmp.replace( pos, 1, "&gt;" );
00380     else if ( ch == '\"' )
00381         tmp.replace( pos, 1, "&quot;" );
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     // We first have to pass it through a ::fromUtf8()
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( "&amp;", pos ) == pos )
00403         tmp.replace( pos, 5, "&" );
00404     else if ( tmp.find( "&lt;", pos ) == pos )
00405         tmp.replace( pos, 4, "<" );
00406     else if( tmp.find( "&gt;", pos ) == pos )
00407         tmp.replace( pos, 4, ">" );
00408     else if ( tmp.find( "&quot;", pos ) == pos )
00409         tmp.replace( pos, 6, "\"" );
00410     }
00411     return tmp;
00412 }
00413 
00414 } // namespace QPC
KDE Logo
This file is part of the documentation for OPIE Version 1.5.5.
Documentation copyright © 1997-2003 the KDE developers. 2003 OPIE developers
Generated on Tue Feb 10 20:24:08 2004 by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2001