summaryrefslogtreecommitdiffabout
path: root/microkde/kdecore/kstringhandler.cpp
Unidiff
Diffstat (limited to 'microkde/kdecore/kstringhandler.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--microkde/kdecore/kstringhandler.cpp650
1 files changed, 650 insertions, 0 deletions
diff --git a/microkde/kdecore/kstringhandler.cpp b/microkde/kdecore/kstringhandler.cpp
new file mode 100644
index 0000000..b737e97
--- a/dev/null
+++ b/microkde/kdecore/kstringhandler.cpp
@@ -0,0 +1,650 @@
1/* This file is part of the KDE libraries
2 Copyright (C) 1999 Ian Zepp (icszepp@islc.net)
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA.
18*/
19
20/* AIX needs strings.h for str*casecmp(), and our config.h loads it on AIX
21 So we don't need to include strings.h explicitly */
22
23//US #include "config.h"
24
25#include "kstringhandler.h"
26/*US
27QString KStringHandler::word( const QString &text , uint pos )
28{
29 QStringList list = QStringList::split( " ", text , true );
30
31 if ( pos < list.count() )
32 return list[ pos ];
33
34 return "";
35}
36
37QString KStringHandler::word( const QString &text , const char *range )
38{
39 // Format in: START:END
40 // Note index starts a 0 (zero)
41 //
42 // 0: first word to end
43 // 1:3 second to fourth words
44 QStringList list = QStringList::split( " ", text , true );
45 QString tmp = "";
46 QString r = range;
47
48 if ( text.isEmpty() )
49 return tmp;
50
51 // do stuff here
52 QRegExp reg;
53
54 int at = 0;
55 int pos = 0;
56 int cnt = 0;
57
58 if ( r.find(QRegExp("[0-9]+:[0-9]+")) != -1 )
59 {
60 at = r.find(":");
61 pos = atoi( r.left(at).ascii() );
62 cnt = atoi( r.remove(0,at+1).ascii() );
63 }
64 else if ( r.find(QRegExp(":+[0-9]+")) != -1 )
65 {
66 at = r.find(":");
67 pos = 0;
68 cnt = atoi( r.remove(0,at+1).ascii() );
69 }
70 else if ( r.find(QRegExp("[0-9]+:+")) != -1 )
71 {
72 at = r.find(":");
73 pos = atoi( r.left(at).ascii() );
74 cnt = list.count(); // zero index
75 }
76 else if ( r.find(QRegExp("[0-9]+")) != -1 )
77 {
78 pos = atoi( r.ascii() );
79 cnt = pos;
80 }
81 else
82 {
83 return tmp; // not found/implemented
84 }
85
86 //
87 // Extract words
88 //
89 int wordsToExtract = cnt-pos+1;
90 QStringList::Iterator it = list.at( pos);
91
92 while ( (it != list.end()) && (wordsToExtract-- > 0))
93 {
94 tmp += *it;
95 tmp += " ";
96 it++;
97 }
98
99 return tmp.stripWhiteSpace();
100}
101
102//
103// Insertion and removal routines
104//
105QString KStringHandler::insword( const QString &text , const QString &word , uint pos )
106{
107 if ( text.isEmpty() )
108 return word;
109
110 if ( word.isEmpty() )
111 return text;
112
113 // Split words and add into list
114 QStringList list = QStringList::split( " ", text, true );
115
116 if ( pos >= list.count() )
117 list.append( word );
118 else
119 list.insert( list.at(pos) , word );
120
121 // Rejoin
122 return list.join( " " );
123}
124
125QString KStringHandler::setword( const QString &text , const QString &word , uint pos )
126{
127 if ( text.isEmpty() )
128 return word;
129
130 if ( word.isEmpty() )
131 return text;
132
133 // Split words and add into list
134 QStringList list = QStringList::split( " ", text, true );
135
136 if ( pos >= list.count() )
137 list.append( word );
138 else
139 {
140 list.insert( list.remove( list.at(pos) ) , word );
141 }
142
143 // Rejoin
144 return list.join( " " );
145}
146
147QString KStringHandler::remrange( const QString &text , const char *range )
148{
149 // Format in: START:END
150 // Note index starts a 0 (zero)
151 //
152 // 0: first word to end
153 // 1:3 second to fourth words
154 QStringList list = QStringList::split( " ", text , true );
155 QString tmp = "";
156 QString r = range;
157
158 if ( text.isEmpty() )
159 return tmp;
160
161 // do stuff here
162 QRegExp reg;
163
164 int at = 0;
165 int pos = 0;
166 int cnt = 0;
167
168 if ( r.find(QRegExp("[0-9]+:[0-9]+")) != -1 )
169 {
170 at = r.find(':');
171 pos = atoi( r.left(at).ascii() );
172 cnt = atoi( r.remove(0,at+1).ascii() );
173 }
174 else if ( r.find(QRegExp(":+[0-9]+")) != -1 )
175 {
176 at = r.find(':');
177 pos = 0;
178 cnt = atoi( r.remove(0,at+1).ascii() );
179 }
180 else if ( r.find(QRegExp("[0-9]+:+")) != -1 )
181 {
182 at = r.find(':');
183 pos = atoi( r.left(at).ascii() );
184 cnt = list.count(); // zero index
185 }
186 else if ( r.find(QRegExp("[0-9]+")) != -1 )
187 {
188 pos = atoi( r.ascii() );
189 cnt = pos;
190 }
191 else
192 {
193 return text; // not found/implemented
194 }
195
196 //
197 // Remove that range of words
198 //
199 int wordsToDelete = cnt-pos+1;
200 QStringList::Iterator it = list.at( pos);
201
202 while ( (it != list.end()) && (wordsToDelete-- > 0))
203 it = list.remove( it );
204
205 return list.join( " " );
206}
207
208QString KStringHandler::remword( const QString &text , uint pos )
209{
210 QString tmp = "";
211
212 if ( text.isEmpty() )
213 return tmp;
214
215 // Split words and add into list
216 QStringList list = QStringList::split( " ", text, true );
217
218 if ( pos < list.count() )
219 list.remove( list.at( pos ) );
220
221 // Rejoin
222 return list.join( " " );
223}
224
225QString KStringHandler::remword( const QString &text , const QString &word )
226{
227 QString tmp = "";
228
229 if ( text.isEmpty() )
230 return tmp;
231
232 if ( word.isEmpty() )
233 return text;
234
235 // Split words and add into list
236 QStringList list = QStringList::split( " ", text, true );
237
238 QStringList::Iterator it = list.find(word);
239
240 if (it != list.end())
241 list.remove( it );
242
243 // Rejoin
244 return list.join( " " );
245}
246
247//
248// Capitalization routines
249//
250QString KStringHandler::capwords( const QString &text )
251{
252 QString tmp = "";
253
254 if ( text.isEmpty() )
255 return tmp;
256
257 QStringList list = QStringList::split( " ", text, true );
258
259 return capwords( QStringList::split( " ", text, true )).join( " " );
260}
261
262QStringList KStringHandler::capwords( const QStringList &list )
263{
264 QStringList tmp;
265 QString word;
266
267 if ( list.count() == 0 )
268 return tmp;
269
270 for ( QStringList::ConstIterator it= list.begin();
271 it != list.end();
272 it++)
273 {
274 word = *it;
275 word = word.left(1).upper() + word.remove(0,1);
276
277 tmp.append( word ); // blank list to start with
278 }
279
280 return tmp;
281}
282
283//
284// Reverse routines
285//
286QString KStringHandler::reverse( const QString &text )
287{
288 QString tmp;
289
290 if ( text.isEmpty() )
291 return tmp;
292
293 QStringList list;
294 list = QStringList::split( " ", text, true );
295 list = reverse( list );
296
297 return list.join( " " );
298}
299
300QStringList KStringHandler::reverse( const QStringList &list )
301{
302 QStringList tmp;
303
304 if ( list.count() == 0 )
305 return tmp;
306
307 for ( QStringList::ConstIterator it= list.begin();
308 it != list.end();
309 it++)
310 tmp.prepend( *it );
311
312 return tmp;
313}
314
315//
316// Left, Right, Center justification
317//
318QString KStringHandler::ljust( const QString &text , uint width )
319{
320 QString tmp = text;
321 tmp = tmp.stripWhiteSpace(); // remove leading/trailing spaces
322
323 if ( tmp.length() >= width )
324 return tmp;
325
326 for ( uint pos = tmp.length() ; pos < width ; pos++ )
327 tmp.append(" ");
328
329 return tmp;
330}
331
332QString KStringHandler::rjust( const QString &text , uint width )
333{
334 QString tmp = text;
335 tmp = tmp.stripWhiteSpace(); // remove leading/trailing spaces
336
337 if ( tmp.length() >= width )
338 return tmp;
339
340 for ( uint pos = tmp.length() ; pos < width ; pos++ )
341 tmp.prepend(" ");
342
343 return tmp;
344}
345
346QString KStringHandler::center( const QString &text , uint width )
347{
348 // Center is slightly different, in that it will add
349 // spaces to the RIGHT side (left-justified) before
350 // it adds a space to the LEFT side.
351
352 QString tmp = text;
353 tmp = tmp.stripWhiteSpace(); // remove leading/trailing spaces
354
355 if ( tmp.length() >= width )
356 return tmp;
357
358 bool left = false; // start at right side.
359
360 for ( uint pos = tmp.length() ; pos < width ; pos++ )
361 {
362 if ( left )
363 tmp.prepend(" ");
364 else
365 tmp.append(" ");
366
367 // Reverse bool
368 left = !left;
369 }
370
371 return tmp;
372}
373
374QString KStringHandler::lsqueeze( const QString & str, uint maxlen )
375{
376 if (str.length() > maxlen) {
377 int part = maxlen-3;
378 return QString("..." + str.right(part));
379 }
380 else return str;
381}
382
383QString KStringHandler::csqueeze( const QString & str, uint maxlen )
384{
385 if (str.length() > maxlen && maxlen > 3) {
386 int part = (maxlen-3)/2;
387 return QString(str.left(part) + "..." + str.right(part));
388 }
389 else return str;
390}
391
392QString KStringHandler::rsqueeze( const QString & str, uint maxlen )
393{
394 if (str.length() > maxlen) {
395 int part = maxlen-3;
396 return QString(str.left(part) + "...");
397 }
398 else return str;
399}
400
401QString KStringHandler::lEmSqueeze(const QString &name, const QFontMetrics& fontMetrics, uint maxlen)
402{
403 return lPixelSqueeze(name, fontMetrics, fontMetrics.maxWidth() * maxlen);
404}
405
406static inline int emSqueezeLimit(int delta, int min, int max)
407{
408 if (delta < min) return min;
409 if (delta > max) return max;
410 return delta;
411}
412
413QString KStringHandler::lPixelSqueeze(const QString& name, const QFontMetrics& fontMetrics, uint maxPixels)
414{
415 uint nameWidth = fontMetrics.width(name);
416
417 if (maxPixels < nameWidth)
418 {
419 QString tmp = name;
420 const uint em = fontMetrics.maxWidth();
421 maxPixels -= fontMetrics.width("...");
422
423 while (maxPixels < nameWidth && !tmp.isEmpty())
424 {
425 int delta = (nameWidth - maxPixels) / em;
426 delta = emSqueezeLimit(delta, 1, delta); // no max
427
428 tmp.remove(0, delta);
429 nameWidth = fontMetrics.width(tmp);
430 }
431
432 return ("..." + tmp);
433 }
434
435 return name;
436}
437
438QString KStringHandler::cEmSqueeze(const QString& name, const QFontMetrics& fontMetrics, uint maxlen)
439{
440 return cPixelSqueeze(name, fontMetrics, fontMetrics.maxWidth() * maxlen);
441}
442
443QString KStringHandler::cPixelSqueeze(const QString& name, const QFontMetrics& fontMetrics, uint maxPixels)
444{
445 uint nameWidth = fontMetrics.width(name);
446
447 if (maxPixels < nameWidth)
448 {
449 QString tmp = name;
450 const uint em = fontMetrics.maxWidth();
451 maxPixels -= fontMetrics.width("...");
452
453 while (maxPixels < nameWidth && !tmp.isEmpty())
454 {
455 int length = tmp.length();
456 int delta = (nameWidth - maxPixels) / em;
457 delta = emSqueezeLimit(delta, 1, length) ;
458
459 tmp.remove((length / 2) - (delta / 2), delta);
460 nameWidth = fontMetrics.width(tmp);
461 }
462
463 return tmp.insert((tmp.length() + 1) / 2, "...");
464 }
465
466 return name;
467}
468
469QString KStringHandler::rEmSqueeze(const QString& name, const QFontMetrics& fontMetrics, uint maxlen)
470{
471 return rPixelSqueeze(name, fontMetrics, fontMetrics.maxWidth() * maxlen);
472}
473
474QString KStringHandler::rPixelSqueeze(const QString& name, const QFontMetrics& fontMetrics, uint maxPixels)
475{
476 uint nameWidth = fontMetrics.width(name);
477
478 if (maxPixels < nameWidth)
479 {
480 QString tmp = name;
481 const uint em = fontMetrics.maxWidth();
482 maxPixels -= fontMetrics.width("...");
483
484 while (maxPixels < nameWidth && !tmp.isEmpty())
485 {
486 int length = tmp.length();
487 int delta = (nameWidth - maxPixels) / em;
488 delta = emSqueezeLimit(delta, 1, length) ;
489
490 tmp.remove(length - delta, delta);
491 nameWidth = fontMetrics.width(tmp);
492 }
493
494 return (tmp + "...");
495 }
496
497 return name;
498}
499
500///// File name patterns (like *.txt)
501
502bool KStringHandler::matchFileName( const QString& filename, const QString& pattern )
503{
504 int len = filename.length();
505 int pattern_len = pattern.length();
506
507 if (!pattern_len)
508 return false;
509
510 // Patterns like "Makefile*"
511 if ( pattern[ pattern_len - 1 ] == '*' && len + 1 >= pattern_len ) {
512 const QChar *c1 = pattern.unicode();
513 const QChar *c2 = filename.unicode();
514 int cnt = 1;
515 while ( cnt < pattern_len && *c1++ == *c2++ )
516 ++cnt;
517 return cnt == pattern_len;
518 }
519
520 // Patterns like "*~", "*.extension"
521 if ( pattern[ 0 ] == '*' && len + 1 >= pattern_len )
522 {
523 const QChar *c1 = pattern.unicode() + pattern_len - 1;
524 const QChar *c2 = filename.unicode() + len - 1;
525 int cnt = 1;
526 while ( cnt < pattern_len && *c1-- == *c2-- )
527 ++cnt;
528 return cnt == pattern_len;
529 }
530
531 // Patterns like "Makefile"
532 return ( filename == pattern );
533}
534
535 QStringList
536KStringHandler::perlSplit(const QString & sep, const QString & s, uint max)
537{
538 bool ignoreMax = 0 == max;
539
540 QStringList l;
541
542 int searchStart = 0;
543
544 int tokenStart = s.find(sep, searchStart);
545
546 while (-1 != tokenStart && (ignoreMax || l.count() < max - 1))
547 {
548 if (!s.mid(searchStart, tokenStart - searchStart).isEmpty())
549 l << s.mid(searchStart, tokenStart - searchStart);
550
551 searchStart = tokenStart + sep.length();
552 tokenStart = s.find(sep, searchStart);
553 }
554
555 if (!s.mid(searchStart, s.length() - searchStart).isEmpty())
556 l << s.mid(searchStart, s.length() - searchStart);
557
558 return l;
559}
560
561 QStringList
562KStringHandler::perlSplit(const QChar & sep, const QString & s, uint max)
563{
564 bool ignoreMax = 0 == max;
565
566 QStringList l;
567
568 int searchStart = 0;
569
570 int tokenStart = s.find(sep, searchStart);
571
572 while (-1 != tokenStart && (ignoreMax || l.count() < max - 1))
573 {
574 if (!s.mid(searchStart, tokenStart - searchStart).isEmpty())
575 l << s.mid(searchStart, tokenStart - searchStart);
576
577 searchStart = tokenStart + 1;
578 tokenStart = s.find(sep, searchStart);
579 }
580
581 if (!s.mid(searchStart, s.length() - searchStart).isEmpty())
582 l << s.mid(searchStart, s.length() - searchStart);
583
584 return l;
585}
586
587 QStringList
588KStringHandler::perlSplit(const QRegExp & sep, const QString & s, uint max)
589{
590 bool ignoreMax = 0 == max;
591
592 QStringList l;
593
594 int searchStart = 0;
595 int tokenStart = sep.search(s, searchStart);
596 int len = sep.matchedLength();
597
598 while (-1 != tokenStart && (ignoreMax || l.count() < max - 1))
599 {
600 if (!s.mid(searchStart, tokenStart - searchStart).isEmpty())
601 l << s.mid(searchStart, tokenStart - searchStart);
602
603 searchStart = tokenStart + len;
604 tokenStart = sep.search(s, searchStart);
605 len = sep.matchedLength();
606 }
607
608 if (!s.mid(searchStart, s.length() - searchStart).isEmpty())
609 l << s.mid(searchStart, s.length() - searchStart);
610
611 return l;
612}
613US end */
614
615/*US
616 QString
617KStringHandler::tagURLs( const QString& text )
618{
619 QRegExp urlEx("(www\\.(?!\\.)|(f|ht)tp(|s)://)[\\d\\w\\./,:_~\\?=&;#@\\-\\+\\%]+[\\d\\w/]");
620
621 QString richText( text );
622 int urlPos = 0, urlLen;
623 while ((urlPos = urlEx.search(richText, urlPos)) >= 0)
624 {
625 urlLen = urlEx.matchedLength();
626 QString href = richText.mid( urlPos, urlLen );
627 // Qt doesn't support (?<=pattern) so we do it here
628 if((urlPos > 0) && richText[urlPos-1].isLetterOrNumber()){
629 urlPos++;
630 continue;
631 }
632 // Don't use QString::arg since %01, %20, etc could be in the string
633 QString anchor = "<a href=\"" + href + "\">" + href + "</a>";
634 richText.replace( urlPos, urlLen, anchor );
635
636
637 urlPos += anchor.length();
638 }
639 return richText;
640}
641*/
642QString KStringHandler::obscure( const QString &str )
643{
644 QString result;
645 for ( uint i = 0; i < str.length(); ++i )
646 result += ( str.at( i ).unicode() < 0x20 ) ? str.at( i ) :
647 QChar( 0x1001F - str.at( i ).unicode() );
648
649 return result;
650}