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