summaryrefslogtreecommitdiff
path: root/qmake/tools/qcstring.cpp
Unidiff
Diffstat (limited to 'qmake/tools/qcstring.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--qmake/tools/qcstring.cpp2474
1 files changed, 2474 insertions, 0 deletions
diff --git a/qmake/tools/qcstring.cpp b/qmake/tools/qcstring.cpp
new file mode 100644
index 0000000..cf1b853
--- a/dev/null
+++ b/qmake/tools/qcstring.cpp
@@ -0,0 +1,2474 @@
1/****************************************************************************
2** $Id$
3**
4** Implementation of extended char array operations, and QByteArray and
5** QCString classes
6**
7** Created : 920722
8**
9** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
10**
11** This file is part of the tools module of the Qt GUI Toolkit.
12**
13** This file may be distributed under the terms of the Q Public License
14** as defined by Trolltech AS of Norway and appearing in the file
15** LICENSE.QPL included in the packaging of this file.
16**
17** This file may be distributed and/or modified under the terms of the
18** GNU General Public License version 2 as published by the Free Software
19** Foundation and appearing in the file LICENSE.GPL included in the
20** packaging of this file.
21**
22** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
23** licenses may use this file in accordance with the Qt Commercial License
24** Agreement provided with the Software.
25**
26** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
27** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
28**
29** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
30** information about Qt Commercial License Agreements.
31** See http://www.trolltech.com/qpl/ for QPL licensing information.
32** See http://www.trolltech.com/gpl/ for GPL licensing information.
33**
34** Contact info@trolltech.com if any conditions of this licensing are
35** not clear to you.
36**
37**********************************************************************/
38
39#include "qstring.h"
40#include "qregexp.h"
41#include "qdatastream.h"
42
43#ifdef QT_THREAD_SUPPORT
44# include <private/qmutexpool_p.h>
45#endif // QT_THREAD_SUPPORT
46
47#include <stdio.h>
48#include <stdarg.h>
49#include <stdlib.h>
50#include <ctype.h>
51#include <limits.h>
52#ifndef QT_NO_COMPRESS
53#include "../3rdparty/zlib/zlib.h"
54#endif
55
56/*****************************************************************************
57 Safe and portable C string functions; extensions to standard string.h
58 *****************************************************************************/
59
60/*!
61 \relates QCString
62
63 This function is normally part of the C library. Qt implements
64 memmove() for platforms that do not provide it.
65
66 memmove() copies \a len bytes from \a src into \a dst. The data
67 is copied correctly even if \a src and \a dst overlap.
68*/
69
70void *qmemmove( void *dst, const void *src, uint len )
71{
72 register char *d;
73 register char *s;
74 if ( dst > src ) {
75 d = (char *)dst + len - 1;
76 s = (char *)src + len - 1;
77 while ( len-- )
78 *d-- = *s--;
79 } else if ( dst < src ) {
80 d = (char *)dst;
81 s = (char *)src;
82 while ( len-- )
83 *d++ = *s++;
84 }
85 return dst;
86}
87
88
89/*!
90 \relates QCString
91
92 Returns a duplicate string.
93
94 Allocates space for a copy of \a src, copies it, and returns a
95 pointer to the copy. If \a src is 0, it immediately returns 0.
96
97 The returned string must be deleted using \c delete[].
98*/
99
100char *qstrdup( const char *src )
101{
102 if ( !src )
103 return 0;
104 char *dst = new char[strlen(src)+1];
105 Q_CHECK_PTR( dst );
106 return strcpy( dst, src );
107}
108
109/*!
110 \fn char *qstrcpy( char *dst, const char *src )
111
112 \relates QCString
113
114 A safe strcpy() function.
115
116 Copies all characters up to and including the '\0' from \a src
117 into \a dst and returns a pointer to \a dst.
118*/
119
120/*!
121 \relates QCString
122
123 A safe strncpy() function.
124
125 Copies at most \a len bytes from \a src (stopping at \a len or the
126 terminating '\0' whichever comes first) into \a dst and returns a
127 pointer to \a dst. Guarantees that \a dst is '\0'-terminated. If
128 \a src or \a dst is 0, returns 0 immediately.
129
130 \sa qstrcpy()
131*/
132
133char *qstrncpy( char *dst, const char *src, uint len )
134{
135 if ( !src || !dst )
136 return 0;
137 strncpy( dst, src, len );
138 if ( len > 0 )
139 dst[len-1] = '\0';
140 return dst;
141}
142
143/*!
144 \fn int qstrcmp( const char *str1, const char *str2 );
145
146 \relates QCString
147
148 A safe strcmp() function.
149
150 Compares \a str1 and \a str2. Returns a negative value if \a str1
151 is less than \a str2, 0 if \a str1 is equal to \a str2 or a
152 positive value if \a str1 is greater than \a str2.
153
154 Special case I: Returns 0 if \a str1 and \a str2 are both 0.
155
156 Special case II: Returns a random nonzero value if \a str1 is 0
157 or \a str2 is 0 (but not both).
158
159 \sa qstrncmp() qstricmp() qstrnicmp()
160 \link #asciinotion Note on character comparisons \endlink
161*/
162
163/*!
164 \fn int qstrncmp( const char *str1, const char *str2, uint len );
165
166 \relates QCString
167
168 A safe strncmp() function.
169
170 Compares at most \a len bytes of \a str1 and \a str2.
171
172 Returns a negative value if \a str1 is less than \a str2, 0 if \a
173 str1 is equal to \a str2 or a positive value if \a str1 is greater
174 than \a str2.
175
176 Special case I: Returns 0 if \a str1 and \a str2 are both 0.
177
178 Special case II: Returns a random nonzero value if \a str1 is 0
179 or \a str2 is 0 (but not both).
180
181 \sa qstrcmp(), qstricmp(), qstrnicmp()
182 \link #asciinotion Note on character comparisons \endlink
183*/
184
185/*!
186 \relates QCString
187
188 A safe stricmp() function.
189
190 Compares \a str1 and \a str2 ignoring the case.
191
192 Returns a negative value if \a str1 is less than \a str2, 0 if \a
193 str1 is equal to \a str2 or a positive value if \a str1 is greater
194 than \a str2.
195
196 Special case I: Returns 0 if \a str1 and \a str2 are both 0.
197
198 Special case II: Returns a random nonzero value if \a str1 is 0
199 or \a str2 is 0 (but not both).
200
201 \sa qstrcmp(), qstrncmp(), qstrnicmp()
202 \link #asciinotion Note on character comparisons \endlink
203*/
204
205int qstricmp( const char *str1, const char *str2 )
206{
207 register const uchar *s1 = (const uchar *)str1;
208 register const uchar *s2 = (const uchar *)str2;
209 int res;
210 uchar c;
211 if ( !s1 || !s2 )
212 return s1 ? 1 : ( s2 ? -1 : 0 );
213 for ( ; !(res = (c=tolower(*s1)) - tolower(*s2)); s1++, s2++ )
214 if ( !c ) // strings are equal
215 break;
216 return res;
217}
218
219/*!
220 \relates QCString
221
222 A safe strnicmp() function.
223
224 Compares at most \a len bytes of \a str1 and \a str2 ignoring the case.
225
226 Returns a negative value if \a str1 is less than \a str2, 0 if \a str1
227 is equal to \a str2 or a positive value if \a str1 is greater than \a
228 str2.
229
230 Special case I: Returns 0 if \a str1 and \a str2 are both 0.
231
232 Special case II: Returns a random nonzero value if \a str1 is 0
233 or \a str2 is 0 (but not both).
234
235 \sa qstrcmp(), qstrncmp() qstricmp()
236 \link #asciinotion Note on character comparisons \endlink
237*/
238
239int qstrnicmp( const char *str1, const char *str2, uint len )
240{
241 register const uchar *s1 = (const uchar *)str1;
242 register const uchar *s2 = (const uchar *)str2;
243 int res;
244 uchar c;
245 if ( !s1 || !s2 )
246 return s1 ? 1 : ( s2 ? -1 : 0 );
247 for ( ; len--; s1++, s2++ ) {
248 if ( (res = (c=tolower(*s1)) - tolower(*s2)) )
249 return res;
250 if ( !c ) // strings are equal
251 break;
252 }
253 return 0;
254}
255
256
257static Q_UINT16 crc_tbl[16];
258static bool crc_tbl_init = FALSE;
259
260 static void createCRC16Table() // build CRC16 lookup table
261{
262 register uint i;
263 register uint j;
264 uint v0, v1, v2, v3;
265 for ( i = 0; i < 16; i++ ) {
266 v0 = i & 1;
267 v1 = ( i >> 1 ) & 1;
268 v2 = ( i >> 2 ) & 1;
269 v3 = ( i >> 3 ) & 1;
270 j = 0;
271#undef SET_BIT
272#define SET_BIT(x, b, v) (x) |= (v) << (b)
273 SET_BIT( j, 0, v0 );
274 SET_BIT( j, 7, v0 );
275 SET_BIT( j, 12, v0 );
276 SET_BIT( j, 1, v1 );
277 SET_BIT( j, 8, v1 );
278 SET_BIT( j, 13, v1 );
279 SET_BIT( j, 2, v2 );
280 SET_BIT( j, 9, v2 );
281 SET_BIT( j, 14, v2 );
282 SET_BIT( j, 3, v3 );
283 SET_BIT( j, 10, v3 );
284 SET_BIT( j, 15, v3 );
285 crc_tbl[i] = j;
286 }
287}
288
289/*!
290 \relates QMemArray
291
292 Returns the CRC-16 checksum of \a len bytes starting at \a data.
293
294 The checksum is independent of the byte order (endianness).
295*/
296
297Q_UINT16 qChecksum( const char *data, uint len )
298{
299 if ( !crc_tbl_init ) { // create lookup table
300
301#ifdef QT_THREAD_SUPPORT
302 QMutexLocker locker( qt_global_mutexpool->get( &crc_tbl_init ) );
303#endif // QT_THREAD_SUPPORT
304
305 if ( !crc_tbl_init ) {
306 createCRC16Table();
307 crc_tbl_init = TRUE;
308 }
309 }
310 register Q_UINT16 crc = 0xffff;
311 uchar c;
312 uchar *p = (uchar *)data;
313 while ( len-- ) {
314 c = *p++;
315 crc = ( (crc >> 4) & 0x0fff ) ^ crc_tbl[((crc ^ c) & 15)];
316 c >>= 4;
317 crc = ( (crc >> 4) & 0x0fff ) ^ crc_tbl[((crc ^ c) & 15)];
318 }
319 return ~crc & 0xffff;
320}
321
322/*! \fn QByteArray qCompress( const QByteArray& data)
323 \relates QByteArray
324 \overload
325*/
326
327/*!
328 \relates QByteArray
329
330 Compresses the array \a data which is \a nbytes long and returns the
331 compressed byte array.
332
333 \sa qUncompress()
334*/
335
336#ifndef QT_NO_COMPRESS
337QByteArray qCompress( const uchar* data, int nbytes )
338{
339 if ( nbytes == 0 ) {
340 QByteArray tmp( 4 );
341 tmp.fill( 0 );
342 return tmp;
343 }
344 if ( !data ) {
345#if defined(QT_CHECK_RANGE)
346 qWarning( "qCompress: data is NULL." );
347#endif
348 return QByteArray();
349 }
350
351 ulong len = nbytes * 2;
352 QByteArray bazip;
353 int res;
354 do {
355 bazip.resize( len + 4 );
356 res = ::compress( (uchar*)bazip.data()+4, &len, (uchar*)data, nbytes );
357
358 switch ( res ) {
359 case Z_OK:
360 bazip.resize( len + 4 );
361 bazip[0] = ( nbytes & 0xff000000 ) >> 24;
362 bazip[1] = ( nbytes & 0x00ff0000 ) >> 16;
363 bazip[2] = ( nbytes & 0x0000ff00 ) >> 8;
364 bazip[3] = ( nbytes & 0x000000ff );
365 break;
366 case Z_MEM_ERROR:
367#if defined(QT_CHECK_RANGE)
368 qWarning( "qCompress: Z_MEM_ERROR: Not enough memory." );
369#endif
370 bazip.resize( 0 );
371 break;
372 case Z_BUF_ERROR:
373 len *= 2;
374 break;
375 }
376 } while ( res == Z_BUF_ERROR );
377
378 return bazip;
379}
380#endif
381
382/*! \fn QByteArray qUncompress( const QByteArray& data )
383 \relates QByteArray
384 \overload
385*/
386
387/*!
388 \relates QByteArray
389
390 Uncompresses the array \a data which is \a nbytes long and returns
391 the uncompressed byte array.
392
393 Returns an empty QByteArray if the input data was corrupt.
394
395 \sa qCompress()
396*/
397
398#ifndef QT_NO_COMPRESS
399QByteArray qUncompress( const uchar* data, int nbytes )
400{
401 if ( !data ) {
402#if defined(QT_CHECK_RANGE)
403 qWarning( "qUncompress: data is NULL." );
404#endif
405 return QByteArray();
406 }
407 if ( nbytes <= 4 ) {
408#if defined(QT_CHECK_RANGE)
409 if ( nbytes < 4 || ( data[0]!=0 || data[1]!=0 || data[2]!=0 || data[3]!=0 ) )
410 qWarning( "qUncompress: Input data is corrupted." );
411#endif
412 return QByteArray();
413 }
414 ulong expectedSize = ( data[0] << 24 ) | ( data[1] << 16 ) | ( data[2] << 8 ) | data[3];
415 ulong len = QMAX( expectedSize, 1 );
416 QByteArray baunzip;
417 int res;
418 do {
419 baunzip.resize( len );
420 res = ::uncompress( (uchar*)baunzip.data(), &len,
421 (uchar*)data+4, nbytes-4 );
422
423 switch ( res ) {
424 case Z_OK:
425 if ( len != baunzip.size() )
426 baunzip.resize( len );
427 break;
428 case Z_MEM_ERROR:
429#if defined(QT_CHECK_RANGE)
430 qWarning( "qUncompress: Z_MEM_ERROR: Not enough memory." );
431#endif
432 break;
433 case Z_BUF_ERROR:
434 len *= 2;
435 break;
436 case Z_DATA_ERROR:
437#if defined(QT_CHECK_RANGE)
438 qWarning( "qUncompress: Z_DATA_ERROR: Input data is corrupted." );
439#endif
440 break;
441 }
442 } while ( res == Z_BUF_ERROR );
443
444 if ( res != Z_OK )
445 baunzip = QByteArray();
446
447 return baunzip;
448}
449#endif
450
451/*****************************************************************************
452 QByteArray documentation
453 *****************************************************************************/
454
455/*!
456 \class QByteArray
457 \reentrant
458 \brief The QByteArray class provides an array of bytes.
459
460 \ingroup collection
461 \ingroup tools
462
463 The QByteArray class provides an explicitly shared array of bytes.
464 It is useful for manipulating memory areas with custom data.
465 QByteArray is implemented as a QMemArray\<char\>. See the \l
466 QMemArray documentation for further information.
467*/
468
469/*!
470 \fn QByteArray::QByteArray()
471
472 Constructs an empty QByteArray.
473*/
474
475/*!
476 \fn QByteArray::QByteArray( int size )
477
478 Constructs a QByteArray of size \a size.
479*/
480
481/*****************************************************************************
482 QByteArray stream functions
483 *****************************************************************************/
484
485/*!
486 \relates QMemArray
487
488 Writes byte array \a a to the stream \a s and returns a reference
489 to the stream.
490
491 \sa \link datastreamformat.html Format of the QDataStream operators \endlink
492*/
493#ifndef QT_NO_DATASTREAM
494
495QDataStream &operator<<( QDataStream &s, const QByteArray &a )
496{
497 return s.writeBytes( a.data(), a.size() );
498}
499
500/*!
501 \relates QMemArray
502
503 Reads a byte array into \a a from the stream \a s and returns a
504 reference to the stream.
505
506 \sa \link datastreamformat.html Format of the QDataStream operators \endlink
507*/
508
509QDataStream &operator>>( QDataStream &s, QByteArray &a )
510{
511 Q_UINT32 len;
512 s >> len; // read size of array
513 if ( len == 0 || s.eof() ) { // end of file reached
514 a.resize( 0 );
515 return s;
516 }
517 if ( !a.resize( (uint)len ) ) { // resize array
518#if defined(QT_CHECK_NULL)
519 qWarning( "QDataStream: Not enough memory to read QByteArray" );
520#endif
521 len = 0;
522 }
523 if ( len > 0 ) // not null array
524 s.readRawBytes( a.data(), (uint)len );
525 return s;
526}
527
528#endif //QT_NO_DATASTREAM
529
530/*****************************************************************************
531 QCString member functions
532 *****************************************************************************/
533
534/*!
535 \class QCString qcstring.h
536 \reentrant
537 \brief The QCString class provides an abstraction of the classic C
538 zero-terminated char array (char *).
539
540 \ingroup text
541 \ingroup collection
542 \ingroup tools
543 \ingroup shared
544
545 QCString inherits QByteArray, which is defined as
546 QMemArray\<char\>. Since QCString is a QMemArray, it uses \link
547 shclass.html explicit sharing\endlink with a reference count.
548
549 QCString tries to behave like a more convenient \c{const char *}.
550 The price of doing this is that some algorithms will perform
551 badly. For example, append() is O(length()) since it scans for a
552 null terminator. Although you might use QCString for text that is
553 never exposed to the user, for most purposes, and especially for
554 user-visible text, you should use QString. QString provides
555 implicit sharing, Unicode and other internationalization support,
556 and is well optimized.
557
558 Note that for the QCString methods that take a \c{const char *}
559 parameter the \c{const char *} must either be 0 (null) or not-null
560 and '\0' (NUL byte) terminated; otherwise the results are
561 undefined.
562
563 A QCString that has not been assigned to anything is \e null, i.e.
564 both the length and the data pointer is 0. A QCString that
565 references the empty string ("", a single '\0' char) is \e empty.
566 Both null and empty QCStrings are legal parameters to the methods.
567 Assigning \c{const char *} 0 to QCString produces a null QCString.
568
569 The length() function returns the length of the string; resize()
570 resizes the string and truncate() truncates the string. A string
571 can be filled with a character using fill(). Strings can be left
572 or right padded with characters using leftJustify() and
573 rightJustify(). Characters, strings and regular expressions can be
574 searched for using find() and findRev(), and counted using
575 contains().
576
577 Strings and characters can be inserted with insert() and appended
578 with append(). A string can be prepended with prepend().
579 Characters can be removed from the string with remove() and
580 replaced with replace().
581
582 Portions of a string can be extracted using left(), right() and
583 mid(). Whitespace can be removed using stripWhiteSpace() and
584 simplifyWhiteSpace(). Strings can be converted to uppercase or
585 lowercase with upper() and lower() respectively.
586
587 Strings that contain numbers can be converted to numbers with
588 toShort(), toInt(), toLong(), toULong(), toFloat() and toDouble().
589 Numbers can be converted to strings with setNum().
590
591 Many operators are overloaded to work with QCStrings. QCString
592 also supports some more obscure functions, e.g. sprintf(),
593 setStr() and setExpand().
594
595 \target asciinotion
596 \sidebar Note on Character Comparisons
597
598 In QCString the notion of uppercase and lowercase and of which
599 character is greater than or less than another character is locale
600 dependent. This affects functions which support a case insensitive
601 option or which compare or lowercase or uppercase their arguments.
602 Case insensitive operations and comparisons will be accurate if
603 both strings contain only ASCII characters. (If \c $LC_CTYPE is
604 set, most Unix systems do "the right thing".) Functions that this
605 affects include contains(), find(), findRev(), \l operator<(), \l
606 operator<=(), \l operator>(), \l operator>=(), lower() and
607 upper().
608
609 This issue does not apply to \l{QString}s since they represent
610 characters using Unicode.
611 \endsidebar
612
613 Performance note: The QCString methods for QRegExp searching are
614 implemented by converting the QCString to a QString and performing
615 the search on that. This implies a deep copy of the QCString data.
616 If you are going to perform many QRegExp searches on a large
617 QCString, you will get better performance by converting the
618 QCString to a QString yourself, and then searching in the QString.
619*/
620
621/*!
622 \fn QCString::QCString()
623
624 Constructs a null string.
625
626 \sa isNull()
627*/
628
629/*!
630 \fn QCString::QCString( const QCString &s )
631
632 Constructs a shallow copy \a s.
633
634 \sa assign()
635*/
636
637/*!
638 Constructs a string with room for \a size characters, including
639 the '\0'-terminator. Makes a null string if \a size == 0.
640
641 If \a size \> 0, then the first and last characters in the string
642 are initialized to '\0'. All other characters are uninitialized.
643
644 \sa resize(), isNull()
645*/
646
647QCString::QCString( int size )
648 : QByteArray( size )
649{
650 if ( size > 0 ) {
651 *data() = '\0'; // set terminator
652 *(data()+(size-1)) = '\0';
653 }
654}
655
656/*!
657 Constructs a string that is a deep copy of \a str.
658
659 If \a str is 0 a null string is created.
660
661 \sa isNull()
662*/
663
664QCString::QCString( const char *str )
665{
666 duplicate( str, qstrlen(str) + 1 );
667}
668
669
670/*!
671 Constructs a string that is a deep copy of \a str. The copy will
672 be at most \a maxsize bytes long including the '\0'-terminator.
673
674 Example:
675 \code
676 QCString str( "helloworld", 6 ); // assigns "hello" to str
677 \endcode
678
679 If \a str contains a 0 byte within the first \a maxsize bytes, the
680 resulting QCString will be terminated by this 0. If \a str is 0 a
681 null string is created.
682
683 \sa isNull()
684*/
685
686QCString::QCString( const char *str, uint maxsize )
687{
688 if ( str == 0 )
689 return;
690 uint len; // index of first '\0'
691 for ( len = 0; len < maxsize - 1; len++ ) {
692 if ( str[len] == '\0' )
693 break;
694 }
695 QByteArray::resize( len + 1 );
696 memcpy( data(), str, len );
697 data()[len] = 0;
698}
699
700/*!
701 \reimp
702*/
703
704QCString::~QCString()
705{
706}
707
708/*!
709 \fn QCString &QCString::operator=( const QCString &s )
710
711 Assigns a shallow copy of \a s to this string and returns a
712 reference to this string.
713*/
714
715/*!
716 \overload QCString &QCString::operator=( const char *str )
717
718 Assigns a deep copy of \a str to this string and returns a
719 reference to this string.
720
721 If \a str is 0 a null string is created.
722
723 \sa isNull()
724*/
725
726/*!
727 \fn bool QCString::isNull() const
728
729 Returns TRUE if the string is null, i.e. if data() == 0; otherwise
730 returns FALSE. A null string is also an empty string.
731
732 Example:
733 \code
734 QCString a; // a.data() == 0, a.size() == 0, a.length() == 0
735 QCString b == "";// b.data() == "", b.size() == 1, b.length() == 0
736 a.isNull(); // TRUE because a.data() == 0
737 a.isEmpty();// TRUE because a.length() == 0
738 b.isNull(); // FALSE because b.data() == ""
739 b.isEmpty();// TRUE because b.length() == 0
740 \endcode
741
742 \sa isEmpty(), length(), size()
743*/
744
745/*!
746 \fn bool QCString::isEmpty() const
747
748 Returns TRUE if the string is empty, i.e. if length() == 0;
749 otherwise returns FALSE. An empty string is not always a null
750 string.
751
752 See example in isNull().
753
754 \sa isNull(), length(), size()
755*/
756
757/*!
758 \fn uint QCString::length() const
759
760 Returns the length of the string, excluding the '\0'-terminator.
761 Equivalent to calling \c strlen(data()).
762
763 Null strings and empty strings have zero length.
764
765 \sa size(), isNull(), isEmpty()
766*/
767
768/*!
769 \fn bool QCString::truncate( uint pos )
770
771 Truncates the string at position \a pos.
772
773 Equivalent to calling \c resize(pos+1).
774
775 Example:
776 \code
777 QCString s = "truncate this string";
778 s.truncate( 5 ); // s == "trunc"
779 \endcode
780
781 \sa resize()
782*/
783
784/*!
785 Extends or shrinks the string to \a len bytes, including the
786 '\0'-terminator.
787
788 A '\0'-terminator is set at position \c{len - 1} unless
789 \c{len == 0}.
790
791 Example:
792 \code
793 QCString s = "resize this string";
794 s.resize( 7 ); // s == "resize"
795 \endcode
796
797 \sa truncate()
798*/
799
800bool QCString::resize( uint len )
801{
802 detach();
803 uint wasNull = isNull();
804 if ( !QByteArray::resize(len) )
805 return FALSE;
806 if ( len )
807 data()[len - 1] = '\0';
808 if ( len > 0 && wasNull )
809 data()[0] = '\0';
810 return TRUE;
811}
812
813
814/*!
815 Implemented as a call to the native vsprintf() (see the manual for
816 your C library).
817
818 If the string is shorter than 256 characters, this sprintf() calls
819 resize(256) to decrease the chance of memory corruption. The
820 string is resized back to its actual length before sprintf()
821 returns.
822
823 Example:
824 \code
825 QCString s;
826 s.sprintf( "%d - %s", 1, "first" ); // result < 256 chars
827
828 QCString big( 25000 ); // very long string
829 big.sprintf( "%d - %s", 2, longString );// result < 25000 chars
830 \endcode
831
832 \warning All vsprintf() implementations will write past the end of
833 the target string (*this) if the \a format specification and
834 arguments happen to be longer than the target string, and some
835 will also fail if the target string is longer than some arbitrary
836 implementation limit.
837
838 Giving user-supplied arguments to sprintf() is risky: Sooner or
839 later someone will paste a huge line into your application.
840*/
841
842QCString &QCString::sprintf( const char *format, ... )
843{
844 detach();
845 va_list ap;
846 va_start( ap, format );
847 if ( size() < 256 )
848 QByteArray::resize( 256 ); // make string big enough
849 vsprintf( data(), format, ap );
850 resize( qstrlen(data()) + 1 ); // truncate
851 va_end( ap );
852 return *this;
853}
854
855
856/*!
857 Fills the string with \a len bytes of character \a c, followed by
858 a '\0'-terminator.
859
860 If \a len is negative, then the current string length is used.
861
862 Returns FALSE is \a len is nonnegative and there is not enough
863 memory to resize the string; otherwise returns TRUE.
864*/
865
866bool QCString::fill( char c, int len )
867{
868 detach();
869 if ( len < 0 )
870 len = length();
871 if ( !QByteArray::fill(c,len+1) )
872 return FALSE;
873 *(data()+len) = '\0';
874 return TRUE;
875}
876
877
878/*!
879 \fn QCString QCString::copy() const
880
881 Returns a deep copy of this string.
882
883 \sa detach()
884*/
885
886
887/*!
888 Finds the first occurrence of the character \a c, starting at
889 position \a index.
890
891 The search is case sensitive if \a cs is TRUE, or case insensitive
892 if \a cs is FALSE.
893
894 Returns the position of \a c, or -1 if \a c could not be found.
895
896 \sa \link #asciinotion Note on character comparisons \endlink
897*/
898
899int QCString::find( char c, int index, bool cs ) const
900{
901 if ( (uint)index >= size() ) // index outside string
902 return -1;
903 register const char *d;
904 if ( cs ) { // case sensitive
905 d = strchr( data()+index, c );
906 } else {
907 d = data()+index;
908 c = tolower( (uchar) c );
909 while ( *d && tolower((uchar) *d) != c )
910 d++;
911 if ( !*d && c ) // not found
912 d = 0;
913 }
914 return d ? (int)(d - data()) : -1;
915}
916
917#define REHASH( a ) \
918 if ( sl_minus_1 < sizeof(uint) * CHAR_BIT ) \
919 hashHaystack -= (a) << sl_minus_1; \
920 hashHaystack <<= 1
921
922/*!
923 \overload
924
925 Finds the first occurrence of the string \a str, starting at
926 position \a index.
927
928 The search is case sensitive if \a cs is TRUE, or case insensitive
929 if \a cs is FALSE.
930
931 Returns the position of \a str, or -1 if \a str could not be
932 found.
933
934 \sa \link #asciinotion Note on character comparisons \endlink
935*/
936
937int QCString::find( const char *str, int index, bool cs ) const
938{
939 if ( (uint)index >= size() )
940 return -1;
941 if ( !str )
942 return -1;
943 if ( !*str )
944 return index;
945 const uint l = length();
946 const uint sl = qstrlen( str );
947 if ( sl + index > l )
948 return -1;
949
950 if ( sl == 1 )
951 return find( *str, index, cs );
952
953 /*
954 See QString::find() for details.
955 */
956 const char* needle = str;
957 const char* haystack = data() + index;
958 const char* end = data() + (l-sl);
959 const uint sl_minus_1 = sl-1;
960 uint hashNeedle = 0, hashHaystack = 0,i;
961
962 if ( cs ) {
963 for ( i = 0; i < sl; ++i ) {
964 hashNeedle = ((hashNeedle<<1) + needle[i] );
965 hashHaystack = ((hashHaystack<<1) + haystack[i] );
966 }
967 hashHaystack -= *(haystack+sl_minus_1);
968
969 while ( haystack <= end ) {
970 hashHaystack += *(haystack+sl_minus_1);
971 if ( hashHaystack == hashNeedle && *needle == *haystack
972 && qstrncmp( needle, haystack, sl ) == 0 )
973 return haystack - data();
974
975 REHASH( *haystack );
976 ++haystack;
977 }
978 } else {
979 for ( i = 0; i < sl; ++i ) {
980 hashNeedle = ((hashNeedle<<1) +
981 tolower( needle[i] ) );
982 hashHaystack = ((hashHaystack<<1) +
983 tolower( haystack[i] ) );
984 }
985 hashHaystack -= tolower(*(haystack+sl_minus_1));
986
987 while ( haystack <= end ) {
988 hashHaystack += tolower(*(haystack+sl_minus_1));
989 if ( hashHaystack == hashNeedle
990 && qstrnicmp( needle, haystack, sl ) == 0 )
991 return haystack - data();
992
993 REHASH( tolower(*haystack) );
994 ++haystack;
995 }
996 }
997 return -1;
998}
999
1000
1001/*!
1002 Finds the first occurrence of the character \a c, starting at
1003 position \a index and searching backwards.
1004
1005 The search is case sensitive if \a cs is TRUE, or case insensitive
1006 if \a cs is FALSE.
1007
1008 Returns the position of \a c, or -1 if \a c could not be found.
1009
1010 \sa \link #asciinotion Note on character comparisons \endlink
1011*/
1012
1013int QCString::findRev( char c, int index, bool cs ) const
1014{
1015 register const char *b = data();
1016 register const char *d;
1017 if ( index < 0 )
1018 index = length();
1019 if ( (uint)index >= size() )
1020 return -1;
1021 d = b + index;
1022 if ( cs ) {
1023 while ( d >= b && *d != c )
1024 d--;
1025 } else {
1026 c = tolower( (uchar) c );
1027 while ( d >= b && tolower((uchar) *d) != c )
1028 d--;
1029 }
1030 return d >= b ? (int)(d - b) : -1;
1031}
1032
1033/*!
1034 \overload
1035
1036 Finds the first occurrence of the string \a str, starting at
1037 position \a index and searching backwards.
1038
1039 The search is case sensitive if \a cs is TRUE, or case insensitive
1040 if \a cs is FALSE.
1041
1042 Returns the position of \a str, or -1 if \a str could not be
1043 found.
1044
1045 \sa \link #asciinotion Note on character comparisons \endlink
1046*/
1047
1048int QCString::findRev( const char *str, int index, bool cs ) const
1049{
1050 /*
1051 See QString::find() for explanations.
1052 */
1053 const uint sl = qstrlen( str );
1054 const uint l = length();
1055 int delta = l-sl;
1056 if ( index < 0 )
1057 index = delta;
1058 if ( index < 0 || index > (int)l )
1059 return -1;
1060 if ( index > delta )
1061 index = delta;
1062
1063 if ( sl == 1 )
1064 return findRev( *str, index, cs );
1065
1066 const char* needle = str;
1067 const char* haystack = data() + index;
1068 const char* end = data();
1069 const uint sl_minus_1 = sl-1;
1070 const char* n = needle+sl_minus_1;
1071 const char* h = haystack+sl_minus_1;
1072 uint hashNeedle = 0, hashHaystack = 0, i;
1073
1074 if ( cs ) {
1075 for ( i = 0; i < sl; ++i ) {
1076 hashNeedle = ((hashNeedle<<1) + *(n-i) );
1077 hashHaystack = ((hashHaystack<<1) + *(h-i) );
1078 }
1079 hashHaystack -= *haystack;
1080 while ( haystack >= end ) {
1081 hashHaystack += *haystack;
1082 if ( hashHaystack == hashNeedle && qstrncmp( needle, haystack, sl ) == 0 )
1083 return haystack-data();
1084 --haystack;
1085 REHASH( *(haystack+sl) );
1086 }
1087 } else {
1088 for ( i = 0; i < sl; ++i ) {
1089 hashNeedle = ((hashNeedle<<1) + tolower( *(n-i) ) );
1090 hashHaystack = ((hashHaystack<<1) + tolower( *(h-i) ) );
1091 }
1092 hashHaystack -= tolower(*haystack);
1093 while ( haystack >= end ) {
1094 hashHaystack += tolower(*haystack);
1095 if ( hashHaystack == hashNeedle && qstrnicmp( needle, haystack, sl ) == 0 )
1096 return haystack-data();
1097 --haystack;
1098 REHASH( tolower(*(haystack+sl)) );
1099 }
1100 }
1101 return -1;
1102}
1103
1104
1105/*!
1106 Returns the number of times the character \a c occurs in the
1107 string.
1108
1109 The match is case sensitive if \a cs is TRUE, or case insensitive
1110 if \a cs if FALSE.
1111
1112 \sa \link #asciinotion Note on character comparisons \endlink
1113*/
1114
1115int QCString::contains( char c, bool cs ) const
1116{
1117 int count = 0;
1118 char *d = data();
1119 if ( !d )
1120 return 0;
1121 if ( cs ) { // case sensitive
1122 while ( *d )
1123 if ( *d++ == c )
1124 count++;
1125 } else { // case insensitive
1126 c = tolower( (uchar) c );
1127 while ( *d ) {
1128 if ( tolower((uchar) *d) == c )
1129 count++;
1130 d++;
1131 }
1132 }
1133 return count;
1134}
1135
1136/*!
1137 \overload
1138
1139 Returns the number of times \a str occurs in the string.
1140
1141 The match is case sensitive if \a cs is TRUE, or case insensitive
1142 if \a cs if FALSE.
1143
1144 This function counts overlapping substrings, for example, "banana"
1145 contains two occurrences of "ana".
1146
1147 \sa findRev()
1148 \link #asciinotion Note on character comparisons \endlink
1149*/
1150
1151int QCString::contains( const char *str, bool cs ) const
1152{
1153 int count = 0;
1154 int i = -1;
1155 // use find for the faster hashing algorithm
1156 while ( ( i = find ( str, i+1, cs ) ) != -1 )
1157 count++;
1158 return count;
1159}
1160
1161/*!
1162 Returns a substring that contains the \a len leftmost characters
1163 of the string.
1164
1165 The whole string is returned if \a len exceeds the length of the
1166 string.
1167
1168 Example:
1169 \code
1170 QCString s = "Pineapple";
1171 QCString t = s.left( 4 ); // t == "Pine"
1172 \endcode
1173
1174 \sa right(), mid()
1175*/
1176
1177QCString QCString::left( uint len ) const
1178{
1179 if ( isEmpty() ) {
1180 QCString empty;
1181 return empty;
1182 } else if ( len >= size() ) {
1183 QCString same( data() );
1184 return same;
1185 } else {
1186 QCString s( len+1 );
1187 strncpy( s.data(), data(), len );
1188 *(s.data()+len) = '\0';
1189 return s;
1190 }
1191}
1192
1193/*!
1194 Returns a substring that contains the \a len rightmost characters
1195 of the string.
1196
1197 The whole string is returned if \a len exceeds the length of the
1198 string.
1199
1200 Example:
1201 \code
1202 QCString s = "Pineapple";
1203 QCString t = s.right( 5 ); // t == "apple"
1204 \endcode
1205
1206 \sa left(), mid()
1207*/
1208
1209QCString QCString::right( uint len ) const
1210{
1211 if ( isEmpty() ) {
1212 QCString empty;
1213 return empty;
1214 } else {
1215 uint l = length();
1216 if ( len > l )
1217 len = l;
1218 char *p = data() + (l - len);
1219 return QCString( p );
1220 }
1221}
1222
1223/*!
1224 Returns a substring that contains at most \a len characters from
1225 this string, starting at position \a index.
1226
1227 Returns a null string if the string is empty or if \a index is out
1228 of range. Returns the whole string from \a index if \a index+len
1229 exceeds the length of the string.
1230
1231 Example:
1232 \code
1233 QCString s = "Two pineapples";
1234 QCString t = s.mid( 4, 3 ); // t == "pin"
1235 \endcode
1236
1237 \sa left(), right()
1238*/
1239
1240QCString QCString::mid( uint index, uint len ) const
1241{
1242 uint slen = qstrlen( data() );
1243 if ( isEmpty() || index >= slen ) {
1244 QCString empty;
1245 return empty;
1246 } else {
1247 if ( len > slen-index )
1248 len = slen - index;
1249 register char *p = data()+index;
1250 QCString s( len+1 );
1251 strncpy( s.data(), p, len );
1252 *(s.data()+len) = '\0';
1253 return s;
1254 }
1255}
1256
1257/*!
1258 Returns a string of length \a width (plus one for the terminating
1259 '\0') that contains this string padded with the \a fill character.
1260
1261 If the length of the string exceeds \a width and \a truncate is
1262 FALSE (the default), then the returned string is a copy of the
1263 string. If the length of the string exceeds \a width and \a
1264 truncate is TRUE, then the returned string is a left(\a width).
1265
1266 Example:
1267 \code
1268 QCString s("apple");
1269 QCString t = s.leftJustify(8, '.'); // t == "apple..."
1270 \endcode
1271
1272 \sa rightJustify()
1273*/
1274
1275QCString QCString::leftJustify( uint width, char fill, bool truncate ) const
1276{
1277 QCString result;
1278 int len = qstrlen(data());
1279 int padlen = width - len;
1280 if ( padlen > 0 ) {
1281 result.QByteArray::resize( len+padlen+1 );
1282 memcpy( result.data(), data(), len );
1283 memset( result.data()+len, fill, padlen );
1284 result[len+padlen] = '\0';
1285 } else {
1286 if ( truncate )
1287 result = left( width );
1288 else
1289 result = copy();
1290 }
1291 return result;
1292}
1293
1294/*!
1295 Returns a string of length \a width (plus one for the terminating
1296 '\0') that contains zero or more of the \a fill character followed
1297 by this string.
1298
1299 If the length of the string exceeds \a width and \a truncate is
1300 FALSE (the default), then the returned string is a copy of the
1301 string. If the length of the string exceeds \a width and \a
1302 truncate is TRUE, then the returned string is a left(\a width).
1303
1304 Example:
1305 \code
1306 QCString s("pie");
1307 QCString t = s.rightJustify(8, '.'); // t == ".....pie"
1308 \endcode
1309
1310 \sa leftJustify()
1311*/
1312
1313QCString QCString::rightJustify( uint width, char fill, bool truncate ) const
1314{
1315 QCString result;
1316 int len = qstrlen(data());
1317 int padlen = width - len;
1318 if ( padlen > 0 ) {
1319 result.QByteArray::resize( len+padlen+1 );
1320 memset( result.data(), fill, padlen );
1321 memcpy( result.data()+padlen, data(), len );
1322 result[len+padlen] = '\0';
1323 } else {
1324 if ( truncate )
1325 result = left( width );
1326 else
1327 result = copy();
1328 }
1329 return result;
1330}
1331
1332/*!
1333 Returns a new string that is a copy of this string converted to lower
1334 case.
1335
1336 Example:
1337 \code
1338 QCString s("Credit");
1339 QCString t = s.lower(); // t == "credit"
1340 \endcode
1341
1342 \sa upper()
1343 \link #asciinotion Note on character comparisons \endlink
1344*/
1345
1346QCString QCString::lower() const
1347{
1348 QCString s( data() );
1349 register char *p = s.data();
1350 if ( p ) {
1351 while ( *p ) {
1352 *p = tolower( (uchar) *p );
1353 p++;
1354 }
1355 }
1356 return s;
1357}
1358
1359/*!
1360 Returns a new string that is a copy of this string converted to upper case.
1361
1362 Example:
1363 \code
1364 QCString s( "Debit" );
1365 QCString t = s.upper(); // t == "DEBIT"
1366 \endcode
1367
1368 \sa lower()
1369 \link #asciinotion Note on character comparisons \endlink
1370*/
1371
1372QCString QCString::upper() const
1373{
1374 QCString s( data() );
1375 register char *p = s.data();
1376 if ( p ) {
1377 while ( *p ) {
1378 *p = toupper(*p);
1379 p++;
1380 }
1381 }
1382 return s;
1383}
1384
1385
1386/*!
1387 Returns a new string that has white space removed from the start
1388 and the end.
1389
1390 White space means the decimal ASCII codes 9, 10, 11, 12, 13 and
1391 32.
1392
1393 Example:
1394 \code
1395 QCString s = " space ";
1396 QCString t = s.stripWhiteSpace(); // t == "space"
1397 \endcode
1398
1399 \sa simplifyWhiteSpace()
1400*/
1401
1402QCString QCString::stripWhiteSpace() const
1403{
1404 if ( isEmpty() ) // nothing to do
1405 return copy();
1406
1407 register char *s = data();
1408 QCString result = s;
1409 int reslen = result.length();
1410 if ( !isspace((uchar) s[0]) && !isspace((uchar) s[reslen-1]) )
1411 return result; // returns a copy
1412
1413 s = result.data();
1414 int start = 0;
1415 int end = reslen - 1;
1416 while ( isspace((uchar) s[start]) ) // skip white space from start
1417 start++;
1418 if ( s[start] == '\0' ) { // only white space
1419 result.resize( 1 );
1420 return result;
1421 }
1422 while ( end && isspace((uchar) s[end]) )// skip white space from end
1423 end--;
1424 end -= start - 1;
1425 memmove( result.data(), &s[start], end );
1426 result.resize( end + 1 );
1427 return result;
1428}
1429
1430
1431/*!
1432 Returns a new string that has white space removed from the start
1433 and the end, plus any sequence of internal white space replaced
1434 with a single space (ASCII 32).
1435
1436 White space means the decimal ASCII codes 9, 10, 11, 12, 13 and
1437 32.
1438
1439 \code
1440 QCString s = " lots\t of\nwhite space ";
1441 QCString t = s.simplifyWhiteSpace(); // t == "lots of white space"
1442 \endcode
1443
1444 \sa stripWhiteSpace()
1445*/
1446
1447QCString QCString::simplifyWhiteSpace() const
1448{
1449 if ( isEmpty() ) // nothing to do
1450 return copy();
1451 QCString result( size() );
1452 char *from= data();
1453 char *to= result.data();
1454 char *first = to;
1455 for ( ;; ) {
1456 while ( isspace((uchar) *from) )
1457 from++;
1458 while ( *from && !isspace((uchar) *from) )
1459 *to++ = *from++;
1460 if ( *from )
1461 *to++ = 0x20; // ' '
1462 else
1463 break;
1464 }
1465 if ( to > first && *(to-1) == 0x20 )
1466 to--;
1467 *to = '\0';
1468 result.resize( (int)(to - result.data()) + 1 );
1469 return result;
1470}
1471
1472
1473/*!
1474 \overload
1475
1476 Inserts string \a s into the string at position \a index.
1477
1478 If \a index is beyond the end of the string, the string is
1479 padded with spaces (ASCII 32) to length \a index and then \a s
1480 is appended.
1481
1482 \code
1483 QCString s = "I like fish";
1484 s.insert( 2, "don't "); // s == "I don't like fish"
1485
1486 s = "x"; // index 01234
1487 s.insert( 3, "yz" ); // s == "x yz"
1488 \endcode
1489*/
1490
1491QCString &QCString::insert( uint index, const char *s )
1492{
1493 int len = qstrlen(s);
1494 if ( len == 0 )
1495 return *this;
1496 uint olen = length();
1497 int nlen = olen + len;
1498 if ( index >= olen ) { // insert after end of string
1499 detach();
1500 if ( QByteArray::resize(nlen+index-olen+1) ) {
1501 memset( data()+olen, ' ', index-olen );
1502 memcpy( data()+index, s, len+1 );
1503 }
1504 } else if ( QByteArray::resize(nlen+1) ) {// normal insert
1505 detach();
1506 memmove( data()+index+len, data()+index, olen-index+1 );
1507 memcpy( data()+index, s, len );
1508 }
1509 return *this;
1510}
1511
1512/*!
1513 Inserts character \a c into the string at position \a index and
1514 returns a reference to the string.
1515
1516 If \a index is beyond the end of the string, the string is
1517 padded with spaces (ASCII 32) to length \a index and then \a c
1518 is appended.
1519
1520 Example:
1521 \code
1522 QCString s = "Yes";
1523 s.insert( 3, '!'); // s == "Yes!"
1524 \endcode
1525
1526 \sa remove(), replace()
1527*/
1528
1529 QCString &QCString::insert( uint index, char c )// insert char
1530{
1531 char buf[2];
1532 buf[0] = c;
1533 buf[1] = '\0';
1534 return insert( index, buf );
1535}
1536
1537/*!
1538 \fn QCString &QCString::prepend( const char *s )
1539
1540 Prepend \a s to the string. Equivalent to insert(0, s).
1541
1542 \sa insert()
1543*/
1544
1545/*!
1546 Removes \a len characters from the string, starting at position \a
1547 index, and returns a reference to the string.
1548
1549 If \a index is out of range, nothing happens. If \a index is
1550 valid, but \a index + \a len is larger than the length of the
1551 string, the string is truncated at position \a index.
1552
1553 \code
1554 QCString s = "Montreal";
1555 s.remove( 1, 4 ); // s == "Meal"
1556 \endcode
1557
1558 \sa insert(), replace()
1559*/
1560
1561QCString &QCString::remove( uint index, uint len )
1562{
1563 uint olen = length();
1564 if ( index + len >= olen ) { // range problems
1565 if ( index < olen ) { // index ok
1566 detach();
1567 resize( index+1 );
1568 }
1569 } else if ( len != 0 ) {
1570 detach();
1571 memmove( data()+index, data()+index+len, olen-index-len+1 );
1572 QByteArray::resize(olen-len+1);
1573 }
1574 return *this;
1575}
1576
1577/*!
1578 Replaces \a len characters from the string, starting at position
1579 \a index, with \a str, and returns a reference to the string.
1580
1581 If \a index is out of range, nothing is removed and \a str is
1582 appended at the end of the string. If \a index is valid, but \a
1583 index + \a len is larger than the length of the string, \a str
1584 replaces the rest of the string from position \a index.
1585
1586 \code
1587 QCString s = "Say yes!";
1588 s.replace( 4, 3, "NO" ); // s == "Say NO!"
1589 \endcode
1590
1591 \sa insert(), remove()
1592*/
1593
1594QCString &QCString::replace( uint index, uint len, const char *str )
1595{
1596 remove( index, len );
1597 insert( index, str );
1598 return *this;
1599}
1600
1601
1602/*! \overload
1603
1604 Replaces every occurrence of the character \a c in the string
1605 with \a after. Returns a reference to the string.
1606
1607 Example:
1608 \code
1609 QCString s = "a,b,c";
1610 s.replace( ',', " or " );
1611 // s == "a or b or c"
1612 \endcode
1613*/
1614QCString &QCString::replace( char c, const char *after )
1615{
1616 char str[2];
1617 str[0] = c;
1618 str[1] = '\0';
1619 return replace( str, after );
1620}
1621
1622/*! \overload
1623
1624 Replaces every occurrence of the string \a before in the string
1625 with the string \a after. Returns a reference to the string.
1626
1627 Example:
1628 \code
1629 QCString s = "Greek is Greek";
1630 s.replace( "Greek", "English" );
1631 // s == "English is English"
1632 \endcode
1633*/
1634QCString &QCString::replace( const char *before, const char *after )
1635{
1636 if ( before == after || isNull() )
1637 return *this;
1638
1639 detach();
1640
1641 int index = 0;
1642 const int bl = before ? strlen( before ) : 0;
1643 const int al = after ? strlen( after ) : 0;
1644 char *d = data();
1645 uint len = length();
1646
1647 if ( bl == al ) {
1648 if ( bl ) {
1649 while( (index = find( before, index ) ) != -1 ) {
1650 memcpy( d+index, after, al );
1651 index += bl;
1652 }
1653 }
1654 } else if ( al < bl ) {
1655 uint to = 0;
1656 uint movestart = 0;
1657 uint num = 0;
1658 while( (index = find( before, index ) ) != -1 ) {
1659 if ( num ) {
1660 int msize = index - movestart;
1661 if ( msize > 0 ) {
1662 memmove( d + to, d + movestart, msize );
1663 to += msize;
1664 }
1665 } else {
1666 to = index;
1667 }
1668 if ( al ) {
1669 memcpy( d + to, after, al );
1670 to += al;
1671 }
1672 index += bl;
1673 movestart = index;
1674 num++;
1675 }
1676 if ( num ) {
1677 int msize = len - movestart;
1678 if ( msize > 0 )
1679 memmove( d + to, d + movestart, msize );
1680 resize( len - num*(bl-al) + 1 );
1681 }
1682 } else {
1683 // the most complex case. We don't want to loose performance by doing repeated
1684 // copies and reallocs of the string.
1685 while( index != -1 ) {
1686 uint indices[4096];
1687 uint pos = 0;
1688 while( pos < 4095 ) {
1689 index = find(before, index);
1690 if ( index == -1 )
1691 break;
1692 indices[pos++] = index;
1693 index += bl;
1694 // avoid infinite loop
1695 if ( !bl )
1696 index++;
1697 }
1698 if ( !pos )
1699 break;
1700
1701 // we have a table of replacement positions, use them for fast replacing
1702 int adjust = pos*(al-bl);
1703 // index has to be adjusted in case we get back into the loop above.
1704 if ( index != -1 )
1705 index += adjust;
1706 uint newlen = len + adjust;
1707 int moveend = len;
1708 if ( newlen > len ) {
1709 resize( newlen + 1 );
1710 len = newlen;
1711 }
1712 d = data();
1713
1714 while( pos ) {
1715 pos--;
1716 int movestart = indices[pos] + bl;
1717 int insertstart = indices[pos] + pos*(al-bl);
1718 int moveto = insertstart + al;
1719 memmove( d + moveto, d + movestart, (moveend - movestart) );
1720 if ( after )
1721 memcpy( d + insertstart, after, al );
1722 moveend = movestart - bl;
1723 }
1724 }
1725 }
1726 return *this;
1727}
1728
1729/*! \overload
1730
1731 Replaces every occurrence of \a c1 with the char \a c2.
1732 Returns a reference to the string.
1733*/
1734QCString &QCString::replace( char c1, char c2 )
1735{
1736 detach();
1737 uint i = 0;
1738 char *d = data();
1739 uint len = length();
1740 while ( i < len ) {
1741 if ( d[i] == c1 )
1742 d[i] = c2;
1743 i++;
1744 }
1745 return *this;
1746}
1747
1748
1749#ifndef QT_NO_REGEXP_CAPTURE
1750/*!
1751 \overload
1752
1753 Finds the first occurrence of the regular expression \a rx,
1754 starting at position \a index.
1755
1756 Returns the position of the next match, or -1 if \a rx was not
1757 found.
1758
1759 \warning If you want to apply this function repeatedly to the same
1760 string it is more efficient to convert the string to a QString and
1761 apply the function to that.
1762*/
1763
1764int QCString::find( const QRegExp& rx, int index ) const
1765{
1766 QString d = QString::fromLatin1( data() );
1767 return d.find( rx, index );
1768}
1769
1770/*!
1771 \overload
1772
1773 Finds the first occurrence of the regular expression \a rx,
1774 starting at position \a index and searching backwards.
1775
1776 Returns the position of the next match (backwards), or -1 if \a rx
1777 was not found.
1778
1779 \warning If you want to apply this function repeatedly to the same
1780 string it is more efficient to convert the string to a QString and
1781 apply the function to that.
1782*/
1783
1784int QCString::findRev( const QRegExp& rx, int index ) const
1785{
1786 QString d = QString::fromLatin1( data() );
1787 return d.findRev( rx, index );
1788}
1789
1790/*!
1791 \overload
1792
1793 Counts the number of overlapping occurrences of \a rx in the string.
1794
1795 Example:
1796 \code
1797 QString s = "banana and panama";
1798 QRegExp r = QRegExp( "a[nm]a", TRUE, FALSE );
1799 s.contains( r ); // 4 matches
1800 \endcode
1801
1802 \sa find(), findRev()
1803
1804 \warning If you want to apply this function repeatedly to the same
1805 string it is more efficient to convert the string to a QString and
1806 apply the function to that.
1807*/
1808
1809int QCString::contains( const QRegExp &rx ) const
1810{
1811 QString d = QString::fromLatin1( data() );
1812 return d.contains( rx );
1813}
1814
1815
1816/*!
1817 \overload
1818
1819 Replaces every occurrence of \a rx in the string with \a str.
1820 Returns a reference to the string.
1821
1822 Example:
1823 \code
1824 QString s = "banana";
1825 s.replace( QRegExp("a.*a"), "" ); // becomes "b"
1826
1827 s = "banana";
1828 s.replace( QRegExp("^[bn]a"), "X" ); // becomes "Xnana"
1829
1830 s = "banana";
1831 s.replace( QRegExp("^[bn]a"), "" ); // becomes "nana"
1832 \endcode
1833
1834 \warning If you want to apply this function repeatedly to the same
1835 string it is more efficient to convert the string to a QString and
1836 apply the function to that.
1837*/
1838
1839QCString &QCString::replace( const QRegExp &rx, const char *str )
1840{
1841 QString d = QString::fromLatin1( data() );
1842 QString r = QString::fromLatin1( str );
1843 d.replace( rx, r );
1844 setStr( d.ascii() );
1845 return *this;
1846}
1847#endif //QT_NO_REGEXP
1848
1849/*!
1850 Returns the string converted to a \c long value.
1851
1852 If \a ok is not 0: \a *ok is set to FALSE if the string is not a
1853 number, or if it has trailing garbage; otherwise \a *ok is set to
1854 TRUE.
1855*/
1856
1857long QCString::toLong( bool *ok ) const
1858{
1859 char *p = data();
1860 long val=0;
1861 const long max_mult = 214748364;
1862 bool is_ok = FALSE;
1863 int neg = 0;
1864 if ( !p )
1865 goto bye;
1866 while ( isspace((uchar) *p) ) // skip leading space
1867 p++;
1868 if ( *p == '-' ) {
1869 p++;
1870 neg = 1;
1871 } else if ( *p == '+' ) {
1872 p++;
1873 }
1874 if ( !isdigit((uchar) *p) )
1875 goto bye;
1876 while ( isdigit((uchar) *p) ) {
1877 if ( val > max_mult || (val == max_mult && (*p-'0') > 7+neg) )
1878 goto bye;
1879 val = 10*val + (*p++ - '0');
1880 }
1881 if ( neg )
1882 val = -val;
1883 while ( isspace((uchar) *p) ) // skip trailing space
1884 p++;
1885 if ( *p == '\0' )
1886 is_ok = TRUE;
1887bye:
1888 if ( ok )
1889 *ok = is_ok;
1890 return is_ok ? val : 0;
1891}
1892
1893/*!
1894 Returns the string converted to an \c{unsigned long} value.
1895
1896 If \a ok is not 0: \a *ok is set to FALSE if the string is not a
1897 number, or if it has trailing garbage; otherwise \a *ok is set to
1898 TRUE.
1899*/
1900
1901ulong QCString::toULong( bool *ok ) const
1902{
1903 char *p = data();
1904 ulong val=0;
1905 const ulong max_mult = 429496729;
1906 bool is_ok = FALSE;
1907 if ( !p )
1908 goto bye;
1909 while ( isspace((uchar) *p) ) // skip leading space
1910 p++;
1911 if ( *p == '+' )
1912 p++;
1913 if ( !isdigit((uchar) *p) )
1914 goto bye;
1915 while ( isdigit((uchar) *p) ) {
1916 if ( val > max_mult || (val == max_mult && (*p-'0') > 5) )
1917 goto bye;
1918 val = 10*val + (*p++ - '0');
1919 }
1920 while ( isspace((uchar) *p) ) // skip trailing space
1921 p++;
1922 if ( *p == '\0' )
1923 is_ok = TRUE;
1924bye:
1925 if ( ok )
1926 *ok = is_ok;
1927 return is_ok ? val : 0;
1928}
1929
1930/*!
1931 Returns the string converted to a \c{short} value.
1932
1933 If \a ok is not 0: \a *ok is set to FALSE if the string is not a
1934 number, is out of range, or if it has trailing garbage; otherwise
1935 \a *ok is set to TRUE.
1936*/
1937
1938short QCString::toShort( bool *ok ) const
1939{
1940 long v = toLong( ok );
1941 if ( ok && *ok && (v < -32768 || v > 32767) )
1942 *ok = FALSE;
1943 return (short)v;
1944}
1945
1946/*!
1947 Returns the string converted to an \c{unsigned short} value.
1948
1949 If \a ok is not 0: \a *ok is set to FALSE if the string is not a
1950 number, is out of range, or if it has trailing garbage; otherwise
1951 \a *ok is set to TRUE.
1952*/
1953
1954ushort QCString::toUShort( bool *ok ) const
1955{
1956 ulong v = toULong( ok );
1957 if ( ok && *ok && (v > 65535) )
1958 *ok = FALSE;
1959 return (ushort)v;
1960}
1961
1962
1963/*!
1964 Returns the string converted to a \c{int} value.
1965
1966 If \a ok is not 0: \a *ok is set to FALSE if the string is not a
1967 number, or if it has trailing garbage; otherwise \a *ok is set to
1968 TRUE.
1969*/
1970
1971int QCString::toInt( bool *ok ) const
1972{
1973 return (int)toLong( ok );
1974}
1975
1976/*!
1977 Returns the string converted to an \c{unsigned int} value.
1978
1979 If \a ok is not 0: \a *ok is set to FALSE if the string is not a
1980 number, or if it has trailing garbage; otherwise \a *ok is set to
1981 TRUE.
1982*/
1983
1984uint QCString::toUInt( bool *ok ) const
1985{
1986 return (uint)toULong( ok );
1987}
1988
1989/*!
1990 Returns the string converted to a \c{double} value.
1991
1992 If \a ok is not 0: \a *ok is set to FALSE if the string is not a
1993 number, or if it has trailing garbage; otherwise \a *ok is set to
1994 TRUE.
1995*/
1996
1997double QCString::toDouble( bool *ok ) const
1998{
1999 char *end;
2000 double val = strtod( data() ? data() : "", &end );
2001 if ( ok )
2002 *ok = ( data() && *data() && ( end == 0 || *end == '\0' ) );
2003 return val;
2004}
2005
2006/*!
2007 Returns the string converted to a \c{float} value.
2008
2009 If \a ok is not 0: \a *ok is set to FALSE if the string is not a
2010 number, or if it has trailing garbage; otherwise \a *ok is set to
2011 TRUE.
2012*/
2013
2014float QCString::toFloat( bool *ok ) const
2015{
2016 return (float)toDouble( ok );
2017}
2018
2019
2020/*!
2021 Makes a deep copy of \a str. Returns a reference to the string.
2022*/
2023
2024QCString &QCString::setStr( const char *str )
2025{
2026 detach();
2027 if ( str ) // valid string
2028 store( str, qstrlen(str)+1 );
2029 else // empty
2030 resize( 0 );
2031 return *this;
2032}
2033
2034/*!
2035 \overload
2036
2037 Sets the string to the string representation of the number \a n
2038 and returns a reference to the string.
2039*/
2040
2041QCString &QCString::setNum( long n )
2042{
2043 detach();
2044 char buf[20];
2045 register char *p = &buf[19];
2046 bool neg;
2047 if ( n < 0 ) {
2048 neg = TRUE;
2049 n = -n;
2050 } else {
2051 neg = FALSE;
2052 }
2053 *p = '\0';
2054 do {
2055 *--p = ((int)(n%10)) + '0';
2056 n /= 10;
2057 } while ( n );
2058 if ( neg )
2059 *--p = '-';
2060 store( p, qstrlen(p)+1 );
2061 return *this;
2062}
2063
2064/*!
2065 \overload
2066
2067 Sets the string to the string representation of the number \a n
2068 and returns a reference to the string.
2069*/
2070
2071QCString &QCString::setNum( ulong n )
2072{
2073 detach();
2074 char buf[20];
2075 register char *p = &buf[19];
2076 *p = '\0';
2077 do {
2078 *--p = ((int)(n%10)) + '0';
2079 n /= 10;
2080 } while ( n );
2081 store( p, qstrlen(p)+1 );
2082 return *this;
2083}
2084
2085/*!
2086 \overload QCString &QCString::setNum( int n )
2087
2088 Sets the string to the string representation of the number \a n
2089 and returns a reference to the string.
2090*/
2091
2092/*!
2093 \overload QCString &QCString::setNum( uint n )
2094
2095 Sets the string to the string representation of the number \a n
2096 and returns a reference to the string.
2097*/
2098
2099/*!
2100 \overload QCString &QCString::setNum( short n )
2101
2102 Sets the string to the string representation of the number \a n
2103 and returns a reference to the string.
2104*/
2105
2106/*!
2107 \overload QCString &QCString::setNum( ushort n )
2108
2109 Sets the string to the string representation of the number \a n
2110 and returns a reference to the string.
2111*/
2112
2113/*!
2114 Sets the string to the string representation of the number \a n
2115 and returns a reference to the string.
2116
2117 The format of the string representation is specified by the format
2118 character \a f, and the precision (number of digits after the
2119 decimal point) is specified with \a prec.
2120
2121 The valid formats for \a f are 'e', 'E', 'f', 'g' and 'G'. The
2122 formats are the same as for sprintf(); they are explained in \l
2123 QString::arg().
2124*/
2125
2126QCString &QCString::setNum( double n, char f, int prec )
2127{
2128#if defined(QT_CHECK_RANGE)
2129 if ( !(f=='f' || f=='F' || f=='e' || f=='E' || f=='g' || f=='G') )
2130 qWarning( "QCString::setNum: Invalid format char '%c'", f );
2131#endif
2132 char format[20];
2133 register char *fs = format; // generate format string
2134 *fs++ = '%'; // "%.<prec>l<f>"
2135 if ( prec > 99 )
2136 prec = 99;
2137 *fs++ = '.';
2138 if ( prec >= 10 ) {
2139 *fs++ = prec / 10 + '0';
2140 *fs++ = prec % 10 + '0';
2141 } else {
2142 *fs++ = prec + '0';
2143 }
2144 *fs++ = 'l';
2145 *fs++ = f;
2146 *fs = '\0';
2147 return sprintf( format, n );
2148}
2149
2150/*! \overload QCString &QCString::setNum( float n, char f, int prec ) */
2151
2152
2153/*!
2154 Sets the character at position \a index to \a c and expands the
2155 string if necessary, padding with spaces.
2156
2157 Returns FALSE if \a index was out of range and the string could
2158 not be expanded; otherwise returns TRUE.
2159*/
2160
2161bool QCString::setExpand( uint index, char c )
2162{
2163 detach();
2164 uint oldlen = length();
2165 if ( index >= oldlen ) {
2166 if ( !QByteArray::resize( index+2 ) )// no memory
2167 return FALSE;
2168 if ( index > oldlen )
2169 memset( data() + oldlen, ' ', index - oldlen );
2170 *(data() + index+1) = '\0'; // terminate padded string
2171 }
2172 *(data() + index) = c;
2173 return TRUE;
2174}
2175
2176
2177/*!
2178 \fn QCString::operator const char *() const
2179
2180 Returns the string data.
2181*/
2182
2183
2184/*!
2185 \fn QCString& QCString::append( const char *str )
2186
2187 Appends string \a str to the string and returns a reference to the
2188 string. Equivalent to operator+=().
2189*/
2190
2191/*!
2192 Appends string \a str to the string and returns a reference to the string.
2193*/
2194
2195QCString& QCString::operator+=( const char *str )
2196{
2197 if ( !str )
2198 return *this; // nothing to append
2199 detach();
2200 uint len1 = length();
2201 uint len2 = qstrlen(str);
2202 if ( !QByteArray::resize( len1 + len2 + 1 ) )
2203 return *this; // no memory
2204 memcpy( data() + len1, str, len2 + 1 );
2205 return *this;
2206}
2207
2208/*!
2209 \overload
2210
2211 Appends character \a c to the string and returns a reference to the string.
2212*/
2213
2214QCString &QCString::operator+=( char c )
2215{
2216 detach();
2217 uint len = length();
2218 if ( !QByteArray::resize( len + 2 ) )
2219 return *this; // no memory
2220 *(data() + len) = c;
2221 *(data() + len+1) = '\0';
2222 return *this;
2223}
2224
2225
2226/*****************************************************************************
2227 QCString stream functions
2228 *****************************************************************************/
2229#ifndef QT_NO_DATASTREAM
2230/*!
2231 \relates QCString
2232
2233 Writes string \a str to the stream \a s.
2234
2235 \sa \link datastreamformat.html Format of the QDataStream operators \endlink
2236*/
2237QDataStream &operator<<( QDataStream &s, const QCString &str )
2238{
2239 return s.writeBytes( str.data(), str.size() );
2240}
2241
2242/*!
2243 \relates QCString
2244
2245 Reads a string into \a str from the stream \a s.
2246
2247 \sa \link datastreamformat.html Format of the QDataStream operators \endlink
2248*/
2249
2250QDataStream &operator>>( QDataStream &s, QCString &str )
2251{
2252 str.detach();
2253 Q_UINT32 len;
2254 s >> len; // read size of string
2255 if ( len == 0 || s.eof() ) { // end of file reached
2256 str.resize( 0 );
2257 return s;
2258 }
2259 if ( !str.QByteArray::resize( (uint)len )) {// resize string
2260#if defined(QT_CHECK_NULL)
2261 qWarning( "QDataStream: Not enough memory to read QCString" );
2262#endif
2263 len = 0;
2264 }
2265 if ( len > 0 ) // not null array
2266 s.readRawBytes( str.data(), (uint)len );
2267 return s;
2268}
2269#endif //QT_NO_DATASTREAM
2270
2271/*****************************************************************************
2272 Documentation for related functions
2273 *****************************************************************************/
2274
2275/*!
2276 \fn bool operator==( const QCString &s1, const QCString &s2 )
2277
2278 \relates QCString
2279
2280 Returns TRUE if \a s1 and \a s2 are equal; otherwise returns FALSE.
2281
2282 Equivalent to qstrcmp(\a s1, \a s2) == 0.
2283*/
2284
2285/*!
2286 \overload bool operator==( const QCString &s1, const char *s2 )
2287
2288 \relates QCString
2289
2290 Returns TRUE if \a s1 and \a s2 are equal; otherwise returns FALSE.
2291
2292 Equivalent to qstrcmp(\a s1, \a s2) == 0.
2293*/
2294
2295/*!
2296 \overload bool operator==( const char *s1, const QCString &s2 )
2297
2298 \relates QCString
2299
2300 Returns TRUE if \a s1 and \a s2 are equal; otherwise returns FALSE.
2301
2302 Equivalent to qstrcmp(\a s1, \a s2) == 0.
2303*/
2304
2305/*!
2306 \fn bool operator!=( const QCString &s1, const QCString &s2 )
2307
2308 \relates QCString
2309
2310 Returns TRUE if \a s1 and \a s2 are different; otherwise returns FALSE.
2311
2312 Equivalent to qstrcmp(\a s1, \a s2) != 0.
2313*/
2314
2315/*!
2316 \overload bool operator!=( const QCString &s1, const char *s2 )
2317
2318 \relates QCString
2319
2320 Returns TRUE if \a s1 and \a s2 are different; otherwise returns FALSE.
2321
2322 Equivalent to qstrcmp(\a s1, \a s2) != 0.
2323*/
2324
2325/*!
2326 \overload bool operator!=( const char *s1, const QCString &s2 )
2327
2328 \relates QCString
2329
2330 Returns TRUE if \a s1 and \a s2 are different; otherwise returns FALSE.
2331
2332 Equivalent to qstrcmp(\a s1, \a s2) != 0.
2333*/
2334
2335/*!
2336 \fn bool operator<( const QCString &s1, const char *s2 )
2337
2338 \relates QCString
2339
2340 Returns TRUE if \a s1 is less than \a s2; otherwise returns FALSE.
2341
2342 Equivalent to qstrcmp(\a s1, \a s2) \< 0.
2343
2344 \sa \link #asciinotion Note on character comparisons \endlink
2345*/
2346
2347/*!
2348 \overload bool operator<( const char *s1, const QCString &s2 )
2349
2350 \relates QCString
2351
2352 Returns TRUE if \a s1 is less than \a s2; otherwise returns FALSE.
2353
2354 Equivalent to qstrcmp(\a s1, \a s2) \< 0.
2355
2356 \sa \link #asciinotion Note on character comparisons \endlink
2357*/
2358
2359/*!
2360 \fn bool operator<=( const QCString &s1, const char *s2 )
2361
2362 \relates QCString
2363
2364 Returns TRUE if \a s1 is less than or equal to \a s2; otherwise
2365 returns FALSE.
2366
2367 Equivalent to qstrcmp(\a s1, \a s2) \<= 0.
2368
2369 \sa \link #asciinotion Note on character comparisons \endlink
2370*/
2371
2372/*!
2373 \overload bool operator<=( const char *s1, const QCString &s2 )
2374
2375 \relates QCString
2376
2377 Returns TRUE if \a s1 is less than or equal to \a s2; otherwise
2378 returns FALSE.
2379
2380 Equivalent to qstrcmp(\a s1, \a s2) \<= 0.
2381
2382 \sa \link #asciinotion Note on character comparisons \endlink
2383*/
2384
2385/*!
2386 \fn bool operator>( const QCString &s1, const char *s2 )
2387
2388 \relates QCString
2389
2390 Returns TRUE if \a s1 is greater than \a s2; otherwise returns FALSE.
2391
2392 Equivalent to qstrcmp(\a s1, \a s2) \> 0.
2393
2394 \sa \link #asciinotion Note on character comparisons \endlink
2395*/
2396
2397/*!
2398 \overload bool operator>( const char *s1, const QCString &s2 )
2399
2400 \relates QCString
2401
2402 Returns TRUE if \a s1 is greater than \a s2; otherwise returns FALSE.
2403
2404 Equivalent to qstrcmp(\a s1, \a s2) \> 0.
2405
2406 \sa \link #asciinotion Note on character comparisons \endlink
2407*/
2408
2409/*!
2410 \fn bool operator>=( const QCString &s1, const char *s2 )
2411
2412 \relates QCString
2413
2414 Returns TRUE if \a s1 is greater than or equal to \a s2; otherwise
2415 returns FALSE.
2416
2417 Equivalent to qstrcmp(\a s1, \a s2) \>= 0.
2418
2419 \sa \link #asciinotion Note on character comparisons \endlink
2420*/
2421
2422/*!
2423 \overload bool operator>=( const char *s1, const QCString &s2 )
2424
2425 \relates QCString
2426
2427 Returns TRUE if \a s1 is greater than or equal to \a s2; otherwise
2428 returns FALSE.
2429
2430 Equivalent to qstrcmp(\a s1, \a s2) \>= 0.
2431
2432 \sa \link #asciinotion Note on character comparisons \endlink
2433*/
2434
2435/*!
2436 \fn const QCString operator+( const QCString &s1, const QCString &s2 )
2437
2438 \relates QCString
2439
2440 Returns a string which consists of the concatenation of \a s1 and
2441 \a s2.
2442*/
2443
2444/*!
2445 \overload const QCString operator+( const QCString &s1, const char *s2 )
2446
2447 \relates QCString
2448
2449 Returns a string which consists of the concatenation of \a s1 and \a s2.
2450*/
2451
2452/*!
2453 \overload const QCString operator+( const char *s1, const QCString &s2 )
2454
2455 \relates QCString
2456
2457 Returns a string which consists of the concatenation of \a s1 and \a s2.
2458*/
2459
2460/*!
2461 \overload const QCString operator+( const QCString &s, char c )
2462
2463 \relates QCString
2464
2465 Returns a string which consists of the concatenation of \a s and \a c.
2466*/
2467
2468/*!
2469 \overload const QCString operator+( char c, const QCString &s )
2470
2471 \relates QCString
2472
2473 Returns a string which consists of the concatenation of \a c and \a s.
2474*/