# kdecore Library API Documentation

Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

# kstringhandler.cpp

    00001 /* This file is part of the KDE libraries
    00002    Copyright (C) 1999 Ian Zepp (icszepp@islc.net)
    00003
    00004    This library is free software; you can redistribute it and/or
    00005    modify it under the terms of the GNU Library General Public
    00006    License as published by the Free Software Foundation; either
    00007    version 2 of the License, or (at your option) any later version.
    00008
    00009    This library is distributed in the hope that it will be useful,
    00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
    00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00012    Library General Public License for more details.
    00013
    00014    You should have received a copy of the GNU Library General Public License
    00015    along with this library; see the file COPYING.LIB.  If not, write to
    00016    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00017    Boston, MA 02111-1307, USA.
    00018 */
    00019
    00020 /* AIX needs strings.h for str*casecmp(), and our config.h loads it on AIX
    00021    So we don't need to include strings.h explicitly */
    00022 #include "config.h"
    00023
    00024 #include "kstringhandler.h"
    00025
    00026 QString KStringHandler::word( const QString &text , uint pos )
    00027 {
    00028     QStringList list = QStringList::split( " ", text , true );
    00029
    00030     if ( pos < list.count() )
    00031         return list[ pos ];
    00032
    00033     return "";
    00034 }
    00035
    00036 QString KStringHandler::word( const QString &text , const char *range )
    00037 {
    00038     // Format in: START:END
    00039     // Note index starts a 0 (zero)
    00040     //
    00041     // 0:        first word to end
    00042     // 1:3        second to fourth words
    00043     QStringList list = QStringList::split( " ", text , true );
    00044     QString tmp = "";
    00045     QString r = range;
    00046
    00047     if ( text.isEmpty() )
    00048         return tmp;
    00049
    00050     // do stuff here
    00051     QRegExp reg;
    00052
    00053     int at = 0;
    00054     int pos = 0;
    00055     int cnt = 0;
    00056
    00057     if ( r.find(QRegExp("[0-9]+:[0-9]+")) != -1 )
    00058     {
    00059         at  = r.find(":");
    00060         pos = atoi( r.left(at).ascii() );
    00061         cnt = atoi( r.remove(0,at+1).ascii() );
    00062     }
    00063     else if ( r.find(QRegExp(":+[0-9]+")) != -1 )
    00064     {
    00065         at  = r.find(":");
    00066         pos = 0;
    00067         cnt = atoi( r.remove(0,at+1).ascii() );
    00068     }
    00069     else if ( r.find(QRegExp("[0-9]+:+")) != -1 )
    00070     {
    00071         at  = r.find(":");
    00072         pos = atoi( r.left(at).ascii() );
    00073         cnt = list.count(); // zero index
    00074     }
    00075     else if ( r.find(QRegExp("[0-9]+")) != -1 )
    00076     {
    00077         pos = atoi( r.ascii() );
    00078         cnt = pos;
    00079     }
    00080     else
    00081     {
    00082         return tmp; // not found/implemented
    00083     }
    00084
    00085     //
    00086     // Extract words
    00087     //
    00088     int wordsToExtract = cnt-pos+1;
    00089     QStringList::Iterator it = list.at( pos);
    00090
    00091     while ( (it != list.end()) && (wordsToExtract-- > 0))
    00092     {
    00093        tmp += *it;
    00094        tmp += " ";
    00095        it++;
    00096     }
    00097
    00098     return tmp.stripWhiteSpace();
    00099 }
    00100
    00101 //
    00102 // Insertion and removal routines
    00103 //
    00104 QString KStringHandler::insword( const QString &text , const QString &word , uint pos )
    00105 {
    00106     if ( text.isEmpty() )
    00107         return word;
    00108
    00109     if ( word.isEmpty() )
    00110         return text;
    00111
    00112     // Split words and add into list
    00113     QStringList list = QStringList::split( " ", text, true );
    00114
    00115     if ( pos >= list.count() )
    00116         list.append( word );
    00117     else
    00118         list.insert( list.at(pos) , word );
    00119
    00120     // Rejoin
    00121     return list.join( " " );
    00122 }
    00123
    00124 QString KStringHandler::setword( const QString &text , const QString &word , uint pos )
    00125 {
    00126     if ( text.isEmpty() )
    00127         return word;
    00128
    00129     if ( word.isEmpty() )
    00130         return text;
    00131
    00132     // Split words and add into list
    00133     QStringList list = QStringList::split( " ", text, true );
    00134
    00135     if ( pos >= list.count() )
    00136         list.append( word );
    00137     else
    00138     {
    00139         list.insert( list.remove( list.at(pos) ) , word );
    00140     }
    00141
    00142     // Rejoin
    00143     return list.join( " " );
    00144 }
    00145
    00146 QString KStringHandler::remrange( const QString &text , const char *range )
    00147 {
    00148     // Format in: START:END
    00149     // Note index starts a 0 (zero)
    00150     //
    00151     // 0:        first word to end
    00152     // 1:3        second to fourth words
    00153     QStringList list = QStringList::split( " ", text , true );
    00154     QString tmp = "";
    00155     QString r = range;
    00156
    00157     if ( text.isEmpty() )
    00158         return tmp;
    00159
    00160     // do stuff here
    00161     QRegExp reg;
    00162
    00163     int at = 0;
    00164     int pos = 0;
    00165     int cnt = 0;
    00166
    00167     if ( r.find(QRegExp("[0-9]+:[0-9]+")) != -1 )
    00168     {
    00169         at  = r.find(':');
    00170         pos = atoi( r.left(at).ascii() );
    00171         cnt = atoi( r.remove(0,at+1).ascii() );
    00172     }
    00173     else if ( r.find(QRegExp(":+[0-9]+")) != -1 )
    00174     {
    00175         at  = r.find(':');
    00176         pos = 0;
    00177         cnt = atoi( r.remove(0,at+1).ascii() );
    00178     }
    00179     else if ( r.find(QRegExp("[0-9]+:+")) != -1 )
    00180     {
    00181         at  = r.find(':');
    00182         pos = atoi( r.left(at).ascii() );
    00183         cnt = list.count(); // zero index
    00184     }
    00185     else if ( r.find(QRegExp("[0-9]+")) != -1 )
    00186     {
    00187         pos = atoi( r.ascii() );
    00188         cnt = pos;
    00189     }
    00190     else
    00191     {
    00192         return text; // not found/implemented
    00193     }
    00194
    00195     //
    00196     // Remove that range of words
    00197     //
    00198     int wordsToDelete = cnt-pos+1;
    00199     QStringList::Iterator it = list.at( pos);
    00200
    00201     while ( (it != list.end()) && (wordsToDelete-- > 0))
    00202        it = list.remove( it );
    00203
    00204     return list.join( " " );
    00205 }
    00206
    00207 QString KStringHandler::remword( const QString &text , uint pos )
    00208 {
    00209     QString tmp = "";
    00210
    00211     if ( text.isEmpty() )
    00212         return tmp;
    00213
    00214     // Split words and add into list
    00215     QStringList list = QStringList::split( " ", text, true );
    00216
    00217     if ( pos < list.count() )
    00218         list.remove( list.at( pos ) );
    00219
    00220     // Rejoin
    00221     return list.join( " " );
    00222 }
    00223
    00224 QString KStringHandler::remword( const QString &text , const QString &word )
    00225 {
    00226     QString tmp = "";
    00227
    00228     if ( text.isEmpty() )
    00229         return tmp;
    00230
    00231     if ( word.isEmpty() )
    00232         return text;
    00233
    00234     // Split words and add into list
    00235     QStringList list = QStringList::split( " ", text, true );
    00236
    00237     QStringList::Iterator it = list.find(word);
    00238
    00239     if (it != list.end())
    00240        list.remove( it );
    00241
    00242     // Rejoin
    00243     return list.join( " " );
    00244 }
    00245
    00246 //
    00247 // Capitalization routines
    00248 //
    00249 QString KStringHandler::capwords( const QString &text )
    00250 {
    00251     QString tmp = "";
    00252
    00253     if ( text.isEmpty() )
    00254         return tmp;
    00255
    00256     QStringList list = QStringList::split( " ", text, true );
    00257
    00258     return capwords( QStringList::split( " ", text, true )).join( " " );
    00259 }
    00260
    00261 QStringList KStringHandler::capwords( const QStringList &list )
    00262 {
    00263     QStringList tmp;
    00264     QString word;
    00265
    00266     if ( list.count() == 0 )
    00267         return tmp;
    00268
    00269     for ( QStringList::ConstIterator it= list.begin();
    00270           it != list.end();
    00271           it++)
    00272     {
    00273         word = *it;
    00274         word = word.left(1).upper() + word.remove(0,1);
    00275
    00276         tmp.append( word ); // blank list to start with
    00277     }
    00278
    00279     return tmp;
    00280 }
    00281
    00282 //
    00283 // Reverse routines
    00284 //
    00285 QString KStringHandler::reverse( const QString &text )
    00286 {
    00287     QString tmp;
    00288
    00289     if ( text.isEmpty() )
    00290         return tmp;
    00291
    00292     QStringList list;
    00293     list = QStringList::split( " ", text, true );
    00294     list = reverse( list );
    00295
    00296     return list.join( " " );
    00297 }
    00298
    00299 QStringList KStringHandler::reverse( const QStringList &list )
    00300 {
    00301     QStringList tmp;
    00302
    00303     if ( list.count() == 0 )
    00304         return tmp;
    00305
    00306     for ( QStringList::ConstIterator it= list.begin();
    00307           it != list.end();
    00308           it++)
    00309         tmp.prepend( *it );
    00310
    00311     return tmp;
    00312 }
    00313
    00314 //
    00315 // Left, Right, Center justification
    00316 //
    00317 QString KStringHandler::ljust( const QString &text , uint width )
    00318 {
    00319     QString tmp = text;
    00320     tmp = tmp.stripWhiteSpace(); // remove leading/trailing spaces
    00321
    00322     if ( tmp.length() >= width )
    00323         return tmp;
    00324
    00325     for ( uint pos = tmp.length() ; pos < width ; pos++ )
    00326         tmp.append(" ");
    00327
    00328     return tmp;
    00329 }
    00330
    00331 QString KStringHandler::rjust( const QString &text , uint width )
    00332 {
    00333     QString tmp = text;
    00334     tmp = tmp.stripWhiteSpace(); // remove leading/trailing spaces
    00335
    00336     if ( tmp.length() >= width )
    00337         return tmp;
    00338
    00339     for ( uint pos = tmp.length() ; pos < width ; pos++ )
    00340         tmp.prepend(" ");
    00341
    00342     return tmp;
    00343 }
    00344
    00345 QString KStringHandler::center( const QString &text , uint width )
    00346 {
    00347     // Center is slightly different, in that it will add
    00348     // spaces to the RIGHT side (left-justified) before
    00349     // it adds a space to the LEFT side.
    00350
    00351     QString tmp = text;
    00352     tmp = tmp.stripWhiteSpace(); // remove leading/trailing spaces
    00353
    00354     if ( tmp.length() >= width )
    00355         return tmp;
    00356
    00357     bool left = false; // start at right side.
    00358
    00359     for ( uint pos = tmp.length() ; pos < width ; pos++ )
    00360     {
    00361         if ( left )
    00362             tmp.prepend(" ");
    00363         else
    00364             tmp.append(" ");
    00365
    00366         // Reverse bool
    00367         left = !left;
    00368     }
    00369
    00370     return tmp;
    00371 }
    00372
    00373 QString KStringHandler::lsqueeze( const QString & str, uint maxlen )
    00374 {
    00375   if (str.length() > maxlen) {
    00376     int part = maxlen-3;
    00377     return QString("..." + str.right(part));
    00378   }
    00379   else return str;
    00380 }
    00381
    00382 QString KStringHandler::csqueeze( const QString & str, uint maxlen )
    00383 {
    00384   if (str.length() > maxlen && maxlen > 3) {
    00385     int part = (maxlen-3)/2;
    00386     return QString(str.left(part) + "..." + str.right(part));
    00387   }
    00388   else return str;
    00389 }
    00390
    00391 QString KStringHandler::rsqueeze( const QString & str, uint maxlen )
    00392 {
    00393   if (str.length() > maxlen) {
    00394     int part = maxlen-3;
    00395     return QString(str.left(part) + "...");
    00396   }
    00397   else return str;
    00398 }
    00399
    00401
    00402 bool KStringHandler::matchFileName( const QString& filename, const QString& pattern  )
    00403 {
    00404    int len = filename.length();
    00405    int pattern_len = pattern.length();
    00406
    00407    if (!pattern_len)
    00408       return false;
    00409
    00410    // Patterns like "Makefile*"
    00411    if ( pattern[ pattern_len - 1 ] == '*' && len + 1 >= pattern_len ) {
    00412       const QChar *c1 = pattern.unicode();
    00413       const QChar *c2 = filename.unicode();
    00414       int cnt = 1;
    00415       while ( cnt < pattern_len && *c1++ == *c2++ )
    00416          ++cnt;
    00417       return cnt == pattern_len;
    00418    }
    00419
    00420    // Patterns like "*~", "*.extension"
    00421    if ( pattern[ 0 ] == '*' && len + 1 >= pattern_len )
    00422    {
    00423      const QChar *c1 = pattern.unicode() + pattern_len - 1;
    00424      const QChar *c2 = filename.unicode() + len - 1;
    00425      int cnt = 1;
    00426      while ( cnt < pattern_len && *c1-- == *c2-- )
    00427         ++cnt;
    00428      return cnt == pattern_len;
    00429   }
    00430
    00431    // Patterns like "Makefile"
    00432    return ( filename == pattern );
    00433 }
    00434
    00435   QStringList
    00436 KStringHandler::perlSplit(const QString & sep, const QString & s, uint max)
    00437 {
    00438   bool ignoreMax = 0 == max;
    00439
    00440   QStringList l;
    00441
    00442   int searchStart = 0;
    00443
    00444   int tokenStart = s.find(sep, searchStart);
    00445
    00446   while (-1 != tokenStart && (ignoreMax || l.count() < max - 1))
    00447   {
    00448     if (!s.mid(searchStart, tokenStart - searchStart).isEmpty())
    00449       l << s.mid(searchStart, tokenStart - searchStart);
    00450
    00451     searchStart = tokenStart + sep.length();
    00452     tokenStart = s.find(sep, searchStart);
    00453   }
    00454
    00455   if (!s.mid(searchStart, s.length() - searchStart).isEmpty())
    00456     l << s.mid(searchStart, s.length() - searchStart);
    00457
    00458   return l;
    00459 }
    00460
    00461   QStringList
    00462 KStringHandler::perlSplit(const QChar & sep, const QString & s, uint max)
    00463 {
    00464   bool ignoreMax = 0 == max;
    00465
    00466   QStringList l;
    00467
    00468   int searchStart = 0;
    00469
    00470   int tokenStart = s.find(sep, searchStart);
    00471
    00472   while (-1 != tokenStart && (ignoreMax || l.count() < max - 1))
    00473   {
    00474     if (!s.mid(searchStart, tokenStart - searchStart).isEmpty())
    00475       l << s.mid(searchStart, tokenStart - searchStart);
    00476
    00477     searchStart = tokenStart + 1;
    00478     tokenStart = s.find(sep, searchStart);
    00479   }
    00480
    00481   if (!s.mid(searchStart, s.length() - searchStart).isEmpty())
    00482     l << s.mid(searchStart, s.length() - searchStart);
    00483
    00484   return l;
    00485 }
    00486
    00487   QStringList
    00488 KStringHandler::perlSplit(const QRegExp & sep, const QString & s, uint max)
    00489 {
    00490   bool ignoreMax = 0 == max;
    00491
    00492   QStringList l;
    00493
    00494   int searchStart = 0;
    00495   int tokenStart = sep.search(s, searchStart);
    00496   int len = sep.matchedLength();
    00497
    00498   while (-1 != tokenStart && (ignoreMax || l.count() < max - 1))
    00499   {
    00500     if (!s.mid(searchStart, tokenStart - searchStart).isEmpty())
    00501       l << s.mid(searchStart, tokenStart - searchStart);
    00502
    00503     searchStart = tokenStart + len;
    00504     tokenStart = sep.search(s, searchStart);
    00505     len = sep.matchedLength();
    00506   }
    00507
    00508   if (!s.mid(searchStart, s.length() - searchStart).isEmpty())
    00509     l << s.mid(searchStart, s.length() - searchStart);
    00510
    00511   return l;
    00512 }
    00513
    00514  QString
    00515 KStringHandler::tagURLs( const QString& text )
    00516 {
    00517     /*static*/ QRegExp urlEx("(www\\.(?!\\.)|(f|ht)tp(|s)://)[\\d\\w\\./,:_~\\?=&;#@\\-\\+\\%]+[\\d\\w/]");
    00518
    00519     QString richText( text );
    00520     int urlPos = 0, urlLen;
    00521     while ((urlPos = urlEx.search(richText, urlPos)) >= 0)
    00522     {
    00523         urlLen = urlEx.matchedLength();
    00524         QString href = richText.mid( urlPos, urlLen );
    00525         // Qt doesn't support (?<=pattern) so we do it here
    00526         if((urlPos > 0) && richText[urlPos-1].isLetterOrNumber()){
    00527             urlPos++;
    00528             continue;
    00529         }
    00530         // Don't use QString::arg since %01, %20, etc could be in the string
    00531         QString anchor = "<a href=\"" + href + "\">" + href + "</a>";
    00532         richText.replace( urlPos, urlLen, anchor );
    00533
    00534
    00535         urlPos += anchor.length();
    00536     }
    00537     return richText;
    00538 }

This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 12:46:57 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001