source: trunk/src/tools/qstring.cpp@ 123

Last change on this file since 123 was 119, checked in by dmik, 19 years ago

Tools: Fixed SYS3184 (divide-by-zero) in qt_os2QString2MB() (used by QString::local8Bit()) happened when the target codepage required more than one byte per some Unicode character, e.g. in Japanese locales [thanks to achain].

  • Property svn:keywords set to Id
File size: 179.0 KB
Line 
1/****************************************************************************
2** $Id: qstring.cpp 119 2006-08-25 22:51:01Z dmik $
3**
4** Implementation of the QString class and related Unicode functions
5**
6** Created : 920722
7**
8** Copyright (C) 1992-2002 Trolltech AS. All rights reserved.
9**
10** This file is part of the tools module of the Qt GUI Toolkit.
11**
12** This file may be distributed under the terms of the Q Public License
13** as defined by Trolltech AS of Norway and appearing in the file
14** LICENSE.QPL included in the packaging of this file.
15**
16** This file may be distributed and/or modified under the terms of the
17** GNU General Public License version 2 as published by the Free Software
18** Foundation and appearing in the file LICENSE.GPL included in the
19** packaging of this file.
20**
21** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
22** licenses may use this file in accordance with the Qt Commercial License
23** Agreement provided with the Software.
24**
25** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
26** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27**
28** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
29** information about Qt Commercial License Agreements.
30** See http://www.trolltech.com/qpl/ for QPL licensing information.
31** See http://www.trolltech.com/gpl/ for GPL licensing information.
32**
33** Contact info@trolltech.com if any conditions of this licensing are
34** not clear to you.
35**
36**********************************************************************/
37
38// Don't define it while compiling this module, or USERS of Qt will
39// not be able to link.
40#ifdef QT_NO_CAST_ASCII
41#undef QT_NO_CAST_ASCII
42#endif
43
44#include "qstring.h"
45#include "qregexp.h"
46#include "qdatastream.h"
47#ifndef QT_NO_TEXTCODEC
48#include "qtextcodec.h"
49#endif
50#include "qlocale.h"
51#include "qlocale_p.h"
52
53#include "qunicodetables_p.h"
54#include <limits.h>
55#include <stdarg.h>
56#include <stdio.h>
57#include <stdlib.h>
58#include <string.h>
59#ifndef Q_OS_TEMP
60#include <locale.h>
61#endif
62#if defined(Q_WS_WIN)
63#include "qt_windows.h"
64#endif
65#if defined(Q_OS_OS2)
66#include <unidef.h>
67#include <uconv.h>
68#endif
69#if !defined( QT_NO_COMPONENT ) && !defined( QT_LITE_COMPONENT )
70#include "qcleanuphandler.h"
71#endif
72
73#ifndef LLONG_MAX
74#define LLONG_MAX Q_INT64_C(9223372036854775807)
75#endif
76#ifndef LLONG_MIN
77#define LLONG_MIN (-LLONG_MAX - Q_INT64_C(1))
78#endif
79#ifndef ULLONG_MAX
80#define ULLONG_MAX Q_UINT64_C(18446744073709551615)
81#endif
82
83static int ucstrcmp( const QString &as, const QString &bs )
84{
85 const QChar *a = as.unicode();
86 const QChar *b = bs.unicode();
87 if ( a == b )
88 return 0;
89 if ( a == 0 )
90 return 1;
91 if ( b == 0 )
92 return -1;
93 int l=QMIN(as.length(),bs.length());
94 while ( l-- && *a == *b )
95 a++,b++;
96 if ( l==-1 )
97 return ( as.length()-bs.length() );
98 return a->unicode() - b->unicode();
99}
100
101static int ucstrncmp( const QChar *a, const QChar *b, int l )
102{
103 while ( l-- && *a == *b )
104 a++,b++;
105 if ( l==-1 )
106 return 0;
107 return a->unicode() - b->unicode();
108}
109
110static int ucstrnicmp( const QChar *a, const QChar *b, int l )
111{
112 while ( l-- && ::lower( *a ) == ::lower( *b ) )
113 a++,b++;
114 if ( l==-1 )
115 return 0;
116 return ::lower( *a ).unicode() - ::lower( *b ).unicode();
117}
118
119static uint computeNewMax( uint len )
120{
121 uint newMax = 4;
122 while ( newMax < len )
123 newMax *= 2;
124 // try to save some memory
125 if ( newMax >= 1024 * 1024 && len <= newMax - (newMax >> 2) )
126 newMax -= newMax >> 2;
127 return newMax;
128}
129
130static bool qIsUpper(char c)
131{
132 return c >= 'A' && c <= 'Z';
133}
134
135static bool qIsDigit(char c)
136{
137 return c >= '0' && c <= '9';
138}
139
140static char qToLower(char c)
141{
142 if (c >= 'A' && c <= 'Z')
143 return c - 'A' + 'a';
144 else
145 return c;
146}
147
148/*!
149 \class QCharRef qstring.h
150 \reentrant
151 \brief The QCharRef class is a helper class for QString.
152
153 \ingroup text
154
155 When you get an object of type QCharRef, if you can assign to it,
156 the assignment will apply to the character in the string from
157 which you got the reference. That is its whole purpose in life.
158 The QCharRef becomes invalid once modifications are made to the
159 string: if you want to keep the character, copy it into a QChar.
160
161 Most of the QChar member functions also exist in QCharRef.
162 However, they are not explicitly documented here.
163
164 \sa QString::operator[]() QString::at() QChar
165*/
166
167/*!
168 \class QChar qstring.h
169 \reentrant
170 \brief The QChar class provides a lightweight Unicode character.
171
172 \ingroup text
173
174 Unicode characters are (so far) 16-bit entities without any markup
175 or structure. This class represents such an entity. It is
176 lightweight, so it can be used everywhere. Most compilers treat it
177 like a "short int". (In a few years it may be necessary to make
178 QChar 32-bit when more than 65536 Unicode code points have been
179 defined and come into use.)
180
181 QChar provides a full complement of testing/classification
182 functions, converting to and from other formats, converting from
183 composed to decomposed Unicode, and trying to compare and
184 case-convert if you ask it to.
185
186 The classification functions include functions like those in
187 ctype.h, but operating on the full range of Unicode characters.
188 They all return TRUE if the character is a certain type of
189 character; otherwise they return FALSE. These classification
190 functions are isNull() (returns TRUE if the character is U+0000),
191 isPrint() (TRUE if the character is any sort of printable
192 character, including whitespace), isPunct() (any sort of
193 punctation), isMark() (Unicode Mark), isLetter (a letter),
194 isNumber() (any sort of numeric character), isLetterOrNumber(),
195 and isDigit() (decimal digits). All of these are wrappers around
196 category() which return the Unicode-defined category of each
197 character.
198
199 QChar further provides direction(), which indicates the "natural"
200 writing direction of this character. The joining() function
201 indicates how the character joins with its neighbors (needed
202 mostly for Arabic) and finally mirrored(), which indicates whether
203 the character needs to be mirrored when it is printed in its
204 "unnatural" writing direction.
205
206 Composed Unicode characters (like &aring;) can be converted to
207 decomposed Unicode ("a" followed by "ring above") by using
208 decomposition().
209
210 In Unicode, comparison is not necessarily possible and case
211 conversion is very difficult at best. Unicode, covering the
212 "entire" world, also includes most of the world's case and sorting
213 problems. Qt tries, but not very hard: operator==() and friends
214 will do comparison based purely on the numeric Unicode value (code
215 point) of the characters, and upper() and lower() will do case
216 changes when the character has a well-defined upper/lower-case
217 equivalent. There is no provision for locale-dependent case
218 folding rules or comparison; these functions are meant to be fast
219 so they can be used unambiguously in data structures. (See
220 QString::localeAwareCompare() though.)
221
222 The conversion functions include unicode() (to a scalar), latin1()
223 (to scalar, but converts all non-Latin-1 characters to 0), row()
224 (gives the Unicode row), cell() (gives the Unicode cell),
225 digitValue() (gives the integer value of any of the numerous digit
226 characters), and a host of constructors.
227
228 More information can be found in the document \link unicode.html
229 About Unicode. \endlink
230
231 \sa QString QCharRef
232*/
233
234/*!
235 \enum QChar::Category
236
237 This enum maps the Unicode character categories.
238
239 The following characters are normative in Unicode:
240
241 \value Mark_NonSpacing Unicode class name Mn
242
243 \value Mark_SpacingCombining Unicode class name Mc
244
245 \value Mark_Enclosing Unicode class name Me
246
247 \value Number_DecimalDigit Unicode class name Nd
248
249 \value Number_Letter Unicode class name Nl
250
251 \value Number_Other Unicode class name No
252
253 \value Separator_Space Unicode class name Zs
254
255 \value Separator_Line Unicode class name Zl
256
257 \value Separator_Paragraph Unicode class name Zp
258
259 \value Other_Control Unicode class name Cc
260
261 \value Other_Format Unicode class name Cf
262
263 \value Other_Surrogate Unicode class name Cs
264
265 \value Other_PrivateUse Unicode class name Co
266
267 \value Other_NotAssigned Unicode class name Cn
268
269
270 The following categories are informative in Unicode:
271
272 \value Letter_Uppercase Unicode class name Lu
273
274 \value Letter_Lowercase Unicode class name Ll
275
276 \value Letter_Titlecase Unicode class name Lt
277
278 \value Letter_Modifier Unicode class name Lm
279
280 \value Letter_Other Unicode class name Lo
281
282 \value Punctuation_Connector Unicode class name Pc
283
284 \value Punctuation_Dash Unicode class name Pd
285
286 \value Punctuation_Open Unicode class name Ps
287
288 \value Punctuation_Close Unicode class name Pe
289
290 \value Punctuation_InitialQuote Unicode class name Pi
291
292 \value Punctuation_FinalQuote Unicode class name Pf
293
294 \value Punctuation_Other Unicode class name Po
295
296 \value Symbol_Math Unicode class name Sm
297
298 \value Symbol_Currency Unicode class name Sc
299
300 \value Symbol_Modifier Unicode class name Sk
301
302 \value Symbol_Other Unicode class name So
303
304
305 There are two categories that are specific to Qt:
306
307 \value NoCategory used when Qt is dazed and confused and cannot
308 make sense of anything.
309
310 \value Punctuation_Dask old typo alias for Punctuation_Dash
311
312*/
313
314/*!
315 \enum QChar::Direction
316
317 This enum type defines the Unicode direction attributes. See \link
318 http://www.unicode.org/ the Unicode Standard\endlink for a
319 description of the values.
320
321 In order to conform to C/C++ naming conventions "Dir" is prepended
322 to the codes used in the Unicode Standard.
323*/
324
325/*!
326 \enum QChar::Decomposition
327
328 This enum type defines the Unicode decomposition attributes. See
329 \link http://www.unicode.org/ the Unicode Standard\endlink for a
330 description of the values.
331*/
332
333/*!
334 \enum QChar::Joining
335
336 This enum type defines the Unicode joining attributes. See \link
337 http://www.unicode.org/ the Unicode Standard\endlink for a
338 description of the values.
339*/
340
341/*!
342 \enum QChar::CombiningClass
343
344 This enum type defines names for some of the Unicode combining
345 classes. See \link http://www.unicode.org/ the Unicode
346 Standard\endlink for a description of the values.
347*/
348
349/*!
350 \fn void QChar::setCell( uchar cell )
351 \internal
352*/
353
354/*!
355 \fn void QChar::setRow( uchar row )
356 \internal
357*/
358
359
360/*!
361 \fn QChar::QChar()
362
363 Constructs a null QChar (one that isNull()).
364*/
365
366
367/*!
368 \fn QChar::QChar( char c )
369
370 Constructs a QChar corresponding to ASCII/Latin-1 character \a c.
371*/
372
373
374/*!
375 \fn QChar::QChar( uchar c )
376
377 Constructs a QChar corresponding to ASCII/Latin-1 character \a c.
378*/
379
380
381/*!
382 \fn QChar::QChar( uchar c, uchar r )
383
384 Constructs a QChar for Unicode cell \a c in row \a r.
385*/
386
387
388/*!
389 \fn QChar::QChar( const QChar& c )
390
391 Constructs a copy of \a c. This is a deep copy, if such a
392 lightweight object can be said to have deep copies.
393*/
394
395
396/*!
397 \fn QChar::QChar( ushort rc )
398
399 Constructs a QChar for the character with Unicode code point \a rc.
400*/
401
402
403/*!
404 \fn QChar::QChar( short rc )
405
406 Constructs a QChar for the character with Unicode code point \a rc.
407*/
408
409
410/*!
411 \fn QChar::QChar( uint rc )
412
413 Constructs a QChar for the character with Unicode code point \a rc.
414*/
415
416
417/*!
418 \fn QChar::QChar( int rc )
419
420 Constructs a QChar for the character with Unicode code point \a rc.
421*/
422
423
424/*!
425 \fn bool QChar::networkOrdered ()
426
427 \obsolete
428
429 Returns TRUE if this character is in network byte order (MSB
430 first); otherwise returns FALSE. This is platform dependent.
431*/
432
433
434/*!
435 \fn bool QChar::isNull() const
436
437 Returns TRUE if the character is the Unicode character 0x0000
438 (ASCII NUL); otherwise returns FALSE.
439*/
440
441/*!
442 \fn uchar QChar::cell () const
443
444 Returns the cell (least significant byte) of the Unicode
445 character.
446*/
447
448/*!
449 \fn uchar QChar::row () const
450
451 Returns the row (most significant byte) of the Unicode character.
452*/
453
454/*!
455 Returns TRUE if the character is a printable character; otherwise
456 returns FALSE. This is any character not of category Cc or Cn.
457
458 Note that this gives no indication of whether the character is
459 available in a particular \link QFont font\endlink.
460*/
461bool QChar::isPrint() const
462{
463 Category c = ::category( *this );
464 return !(c == Other_Control || c == Other_NotAssigned);
465}
466
467/*!
468 Returns TRUE if the character is a separator character
469 (Separator_* categories); otherwise returns FALSE.
470*/
471bool QChar::isSpace() const
472{
473 return ::isSpace( *this );
474}
475
476/*!
477 Returns TRUE if the character is a mark (Mark_* categories);
478 otherwise returns FALSE.
479*/
480bool QChar::isMark() const
481{
482 Category c = ::category( *this );
483 return c >= Mark_NonSpacing && c <= Mark_Enclosing;
484}
485
486/*!
487 Returns TRUE if the character is a punctuation mark (Punctuation_*
488 categories); otherwise returns FALSE.
489*/
490bool QChar::isPunct() const
491{
492 Category c = ::category( *this );
493 return (c >= Punctuation_Connector && c <= Punctuation_Other);
494}
495
496/*!
497 Returns TRUE if the character is a letter (Letter_* categories);
498 otherwise returns FALSE.
499*/
500bool QChar::isLetter() const
501{
502 Category c = ::category( *this );
503 return (c >= Letter_Uppercase && c <= Letter_Other);
504}
505
506/*!
507 Returns TRUE if the character is a number (of any sort - Number_*
508 categories); otherwise returns FALSE.
509
510 \sa isDigit()
511*/
512bool QChar::isNumber() const
513{
514 Category c = ::category( *this );
515 return c >= Number_DecimalDigit && c <= Number_Other;
516}
517
518/*!
519 Returns TRUE if the character is a letter or number (Letter_* or
520 Number_* categories); otherwise returns FALSE.
521*/
522bool QChar::isLetterOrNumber() const
523{
524 Category c = ::category( *this );
525 return (c >= Letter_Uppercase && c <= Letter_Other)
526 || (c >= Number_DecimalDigit && c <= Number_Other);
527}
528
529
530/*!
531 Returns TRUE if the character is a decimal digit
532 (Number_DecimalDigit); otherwise returns FALSE.
533*/
534bool QChar::isDigit() const
535{
536 return (::category( *this ) == Number_DecimalDigit);
537}
538
539
540/*!
541 Returns TRUE if the character is a symbol (Symbol_* categories);
542 otherwise returns FALSE.
543*/
544bool QChar::isSymbol() const
545{
546 Category c = ::category( *this );
547 return c >= Symbol_Math && c <= Symbol_Other;
548}
549
550/*!
551 Returns the numeric value of the digit, or -1 if the character is
552 not a digit.
553*/
554int QChar::digitValue() const
555{
556#ifndef QT_NO_UNICODETABLES
557 register int pos = QUnicodeTables::decimal_info[row()];
558 if( !pos )
559 return -1;
560 return QUnicodeTables::decimal_info[(pos<<8) + cell()];
561#else
562 // ##### just latin1
563 if ( ucs < '0' || ucs > '9' )
564 return -1;
565 else
566 return ucs - '0';
567#endif
568}
569
570/*!
571 Returns the character category.
572
573 \sa Category
574*/
575QChar::Category QChar::category() const
576{
577 return ::category( *this );
578}
579
580/*!
581 Returns the character's direction.
582
583 \sa Direction
584*/
585QChar::Direction QChar::direction() const
586{
587 return ::direction( *this );
588}
589
590/*!
591 \warning This function is not supported (it may change to use
592 Unicode character classes).
593
594 Returns information about the joining properties of the character
595 (needed for example, for Arabic).
596*/
597QChar::Joining QChar::joining() const
598{
599 return ::joining( *this );
600}
601
602
603/*!
604 Returns TRUE if the character is a mirrored character (one that
605 should be reversed if the text direction is reversed); otherwise
606 returns FALSE.
607*/
608bool QChar::mirrored() const
609{
610 return ::mirrored( *this );
611}
612
613/*!
614 Returns the mirrored character if this character is a mirrored
615 character, otherwise returns the character itself.
616*/
617QChar QChar::mirroredChar() const
618{
619 return ::mirroredChar( *this );
620}
621
622#ifndef QT_NO_UNICODETABLES
623// ### REMOVE ME 4.0
624static QString shared_decomp;
625#endif
626/*!
627 \nonreentrant
628
629 Decomposes a character into its parts. Returns QString::null if no
630 decomposition exists.
631*/
632const QString &QChar::decomposition() const
633{
634#ifndef QT_NO_UNICODETABLES
635 register int pos = QUnicodeTables::decomposition_info[row()];
636 if(!pos) return QString::null;
637
638 pos = QUnicodeTables::decomposition_info[(pos<<8)+cell()];
639 if(!pos) return QString::null;
640 pos+=2;
641
642 QString s;
643 Q_UINT16 c;
644 while ( (c = QUnicodeTables::decomposition_map[pos++]) != 0 )
645 s += QChar( c );
646 // ### In 4.0, return s, and not shared_decomp. shared_decomp
647 // prevents this function from being reentrant.
648 shared_decomp = s;
649 return shared_decomp;
650#else
651 return QString::null;
652#endif
653}
654
655/*!
656 Returns the tag defining the composition of the character. Returns
657 QChar::Single if no decomposition exists.
658*/
659QChar::Decomposition QChar::decompositionTag() const
660{
661#ifndef QT_NO_UNICODETABLES
662 register int pos = QUnicodeTables::decomposition_info[row()];
663 if(!pos) return QChar::Single;
664
665 pos = QUnicodeTables::decomposition_info[(pos<<8)+cell()];
666 if(!pos) return QChar::Single;
667
668 return (QChar::Decomposition) QUnicodeTables::decomposition_map[pos];
669#else
670 return Single; // ########### FIX eg. just latin1
671#endif
672}
673
674/*!
675 Returns the combining class for the character as defined in the
676 Unicode standard. This is mainly useful as a positioning hint for
677 marks attached to a base character.
678
679 The Qt text rendering engine uses this information to correctly
680 position non spacing marks around a base character.
681*/
682unsigned char QChar::combiningClass() const
683{
684 return ::combiningClass( *this );
685}
686
687
688/*!
689 Returns the lowercase equivalent if the character is uppercase;
690 otherwise returns the character itself.
691*/
692QChar QChar::lower() const
693{
694 return ::lower( *this );
695}
696
697/*!
698 Returns the uppercase equivalent if the character is lowercase;
699 otherwise returns the character itself.
700*/
701QChar QChar::upper() const
702{
703 return ::upper( *this );
704}
705
706/*!
707 \fn QChar::operator char() const
708
709 Returns the Latin-1 character equivalent to the QChar, or 0. This
710 is mainly useful for non-internationalized software.
711
712 \sa unicode()
713*/
714
715/*!
716 \fn ushort QChar::unicode() const
717
718 Returns the numeric Unicode value equal to the QChar. Normally,
719 you should use QChar objects as they are equivalent, but for some
720 low-level tasks (e.g. indexing into an array of Unicode
721 information), this function is useful.
722*/
723
724/*!
725 \fn ushort & QChar::unicode()
726
727 \overload
728
729 Returns a reference to the numeric Unicode value equal to the
730 QChar.
731*/
732
733/*****************************************************************************
734 Documentation of QChar related functions
735 *****************************************************************************/
736
737/*!
738 \fn bool operator==( QChar c1, QChar c2 )
739
740 \relates QChar
741
742 Returns TRUE if \a c1 and \a c2 are the same Unicode character;
743 otherwise returns FALSE.
744*/
745
746/*!
747 \fn bool operator==( char ch, QChar c )
748
749 \overload
750 \relates QChar
751
752 Returns TRUE if \a c is the ASCII/Latin-1 character \a ch;
753 otherwise returns FALSE.
754*/
755
756/*!
757 \fn bool operator==( QChar c, char ch )
758
759 \overload
760 \relates QChar
761
762 Returns TRUE if \a c is the ASCII/Latin-1 character \a ch;
763 otherwise returns FALSE.
764*/
765
766/*!
767 \fn int operator!=( QChar c1, QChar c2 )
768
769 \relates QChar
770
771 Returns TRUE if \a c1 and \a c2 are not the same Unicode
772 character; otherwise returns FALSE.
773*/
774
775/*!
776 \fn int operator!=( char ch, QChar c )
777
778 \overload
779 \relates QChar
780
781 Returns TRUE if \a c is not the ASCII/Latin-1 character \a ch;
782 otherwise returns FALSE.
783*/
784
785/*!
786 \fn int operator!=( QChar c, char ch )
787
788 \overload
789 \relates QChar
790
791 Returns TRUE if \a c is not the ASCII/Latin-1 character \a ch;
792 otherwise returns FALSE.
793*/
794
795/*!
796 \fn int operator<=( QChar c1, QChar c2 )
797
798 \relates QChar
799
800 Returns TRUE if the numeric Unicode value of \a c1 is less than
801 that of \a c2, or they are the same Unicode character; otherwise
802 returns FALSE.
803*/
804
805/*!
806 \fn int operator<=( QChar c, char ch )
807
808 \overload
809 \relates QChar
810
811 Returns TRUE if the numeric Unicode value of \a c is less than or
812 equal to that of the ASCII/Latin-1 character \a ch; otherwise
813 returns FALSE.
814*/
815
816/*!
817 \fn int operator<=( char ch, QChar c )
818
819 \overload
820 \relates QChar
821
822 Returns TRUE if the numeric Unicode value of the ASCII/Latin-1
823 character \a ch is less than or equal to that of \a c; otherwise
824 returns FALSE.
825*/
826
827/*!
828 \fn int operator>=( QChar c1, QChar c2 )
829
830 \relates QChar
831
832 Returns TRUE if the numeric Unicode value of \a c1 is greater than
833 that of \a c2, or they are the same Unicode character; otherwise
834 returns FALSE.
835*/
836
837/*!
838 \fn int operator>=( QChar c, char ch )
839
840 \overload
841 \relates QChar
842
843 Returns TRUE if the numeric Unicode value of \a c is greater than
844 or equal to that of the ASCII/Latin-1 character \a ch; otherwise
845 returns FALSE.
846*/
847
848/*!
849 \fn int operator>=( char ch, QChar c )
850
851 \overload
852 \relates QChar
853
854 Returns TRUE if the numeric Unicode value of the ASCII/Latin-1
855 character \a ch is greater than or equal to that of \a c;
856 otherwise returns FALSE.
857*/
858
859/*!
860 \fn int operator<( QChar c1, QChar c2 )
861
862 \relates QChar
863
864 Returns TRUE if the numeric Unicode value of \a c1 is less than
865 that of \a c2; otherwise returns FALSE.
866*/
867
868/*!
869 \fn int operator<( QChar c, char ch )
870
871 \overload
872 \relates QChar
873
874 Returns TRUE if the numeric Unicode value of \a c is less than that
875 of the ASCII/Latin-1 character \a ch; otherwise returns FALSE.
876*/
877
878/*!
879 \fn int operator<( char ch, QChar c )
880
881 \overload
882 \relates QChar
883
884 Returns TRUE if the numeric Unicode value of the ASCII/Latin-1
885 character \a ch is less than that of \a c; otherwise returns
886 FALSE.
887*/
888
889/*!
890 \fn int operator>( QChar c1, QChar c2 )
891
892 \relates QChar
893
894 Returns TRUE if the numeric Unicode value of \a c1 is greater than
895 that of \a c2; otherwise returns FALSE.
896*/
897
898/*!
899 \fn int operator>( QChar c, char ch )
900
901 \overload
902 \relates QChar
903
904 Returns TRUE if the numeric Unicode value of \a c is greater than
905 that of the ASCII/Latin-1 character \a ch; otherwise returns FALSE.
906*/
907
908/*!
909 \fn int operator>( char ch, QChar c )
910
911 \overload
912 \relates QChar
913
914 Returns TRUE if the numeric Unicode value of the ASCII/Latin-1
915 character \a ch is greater than that of \a c; otherwise returns
916 FALSE.
917*/
918
919#ifndef QT_NO_UNICODETABLES
920
921// small class used internally in QString::Compose()
922class QLigature
923{
924public:
925 QLigature( QChar c );
926
927 Q_UINT16 first() { cur = ligatures; return cur ? *cur : 0; }
928 Q_UINT16 next() { return cur && *cur ? *(cur++) : 0; }
929 Q_UINT16 current() { return cur ? *cur : 0; }
930
931 int match(QString & str, unsigned int index);
932 QChar head();
933 QChar::Decomposition tag();
934
935private:
936 Q_UINT16 *ligatures;
937 Q_UINT16 *cur;
938};
939
940QLigature::QLigature( QChar c )
941{
942 register int pos = QUnicodeTables::ligature_info[c.row()];
943 if( !pos )
944 ligatures = 0;
945 else
946 {
947 pos = QUnicodeTables::ligature_info[(pos<<8)+c.cell()];
948 ligatures = (Q_UINT16 *)&(QUnicodeTables::ligature_map[pos]);
949 }
950 cur = ligatures;
951}
952
953QChar QLigature::head()
954{
955 if(current())
956 return QChar(QUnicodeTables::decomposition_map[current()+1]);
957
958 return QChar::null;
959}
960
961QChar::Decomposition QLigature::tag()
962{
963 if(current())
964 return (QChar::Decomposition) QUnicodeTables::decomposition_map[current()];
965
966 return QChar::Canonical;
967}
968
969int QLigature::match(QString & str, unsigned int index)
970{
971 unsigned int i=index;
972
973 if(!current()) return 0;
974
975 Q_UINT16 lig = current() + 2;
976 Q_UINT16 ch;
977
978 while ((i < str.length()) && (ch = QUnicodeTables::decomposition_map[lig])) {
979 if (str[(int)i] != QChar(ch))
980 return 0;
981 i++;
982 lig++;
983 }
984
985 if (!QUnicodeTables::decomposition_map[lig])
986 {
987 return i-index;
988 }
989 return 0;
990}
991
992
993// this function is just used in QString::compose()
994static inline bool format(QChar::Decomposition tag, QString & str,
995 int index, int len)
996{
997 unsigned int l = index + len;
998 unsigned int r = index;
999
1000 bool left = FALSE, right = FALSE;
1001
1002 left = ((l < str.length()) &&
1003 ((str[(int)l].joining() == QChar::Dual) ||
1004 (str[(int)l].joining() == QChar::Right)));
1005 if (r > 0) {
1006 r--;
1007 //printf("joining(right) = %d\n", str[(int)r].joining());
1008 right = (str[(int)r].joining() == QChar::Dual);
1009 }
1010
1011
1012 switch (tag) {
1013 case QChar::Medial:
1014 return (left & right);
1015 case QChar::Initial:
1016 return (left && !right);
1017 case QChar::Final:
1018 return (right);// && !left);
1019 case QChar::Isolated:
1020 default:
1021 return (!right && !left);
1022 }
1023} // format()
1024#endif
1025
1026/*
1027 QString::compose() and visual() were developed by Gordon Tisher
1028 <tisher@uniserve.ca>, with input from Lars Knoll <knoll@mpi-hd.mpg.de>,
1029 who developed the unicode data tables.
1030*/
1031/*!
1032 \warning This function is not supported in Qt 3.x. It is provided
1033 for experimental and illustrative purposes only. It is mainly of
1034 interest to those experimenting with Arabic and other
1035 composition-rich texts.
1036
1037 Applies possible ligatures to a QString. Useful when
1038 composition-rich text requires rendering with glyph-poor fonts,
1039 but it also makes compositions such as QChar(0x0041) ('A') and
1040 QChar(0x0308) (Unicode accent diaresis), giving QChar(0x00c4)
1041 (German A Umlaut).
1042*/
1043void QString::compose()
1044{
1045#ifndef QT_NO_UNICODETABLES
1046 unsigned int index=0, len;
1047 unsigned int cindex = 0;
1048
1049 QChar code, head;
1050
1051 QMemArray<QChar> dia;
1052
1053 QString composed = *this;
1054
1055 while (index < length()) {
1056 code = at(index);
1057 //printf("\n\nligature for 0x%x:\n", code.unicode());
1058 QLigature ligature(code);
1059 ligature.first();
1060 while ( ligature.current() ) {
1061 if ((len = ligature.match(*this, index)) != 0) {
1062 head = ligature.head();
1063 unsigned short code = head.unicode();
1064 // we exclude Arabic presentation forms A and a few
1065 // other ligatures, which are undefined in most fonts
1066 if(!(code > 0xfb50 && code < 0xfe80) &&
1067 !(code > 0xfb00 && code < 0xfb2a)) {
1068 // joining info is only needed for Arabic
1069 if (format(ligature.tag(), *this, index, len)) {
1070 //printf("using ligature 0x%x, len=%d\n",code,len);
1071 // replace letter
1072 composed.replace(cindex, len, QChar(head));
1073 index += len-1;
1074 // we continue searching in case we have a final
1075 // form because medial ones are preferred.
1076 if ( len != 1 || ligature.tag() !=QChar::Final )
1077 break;
1078 }
1079 }
1080 }
1081 ligature.next();
1082 }
1083 cindex++;
1084 index++;
1085 }
1086 *this = composed;
1087#endif
1088}
1089
1090
1091// These macros are used for efficient allocation of QChar strings.
1092// IMPORTANT! If you change these, make sure you also change the
1093// "delete unicode" statement in ~QStringData() in qstring.h correspondingly!
1094
1095#define QT_ALLOC_QCHAR_VEC( N ) (QChar*) new char[ sizeof(QChar)*( N ) ]
1096#define QT_DELETE_QCHAR_VEC( P ) delete[] ((char*)( P ))
1097
1098
1099/*!
1100 This utility function converts the 8-bit string \a ba to Unicode,
1101 returning the result.
1102
1103 The caller is responsible for deleting the return value with
1104 delete[].
1105*/
1106
1107QChar* QString::latin1ToUnicode( const QByteArray& ba, uint* len )
1108{
1109 if ( ba.isNull() ) {
1110 *len = 0;
1111 return 0;
1112 }
1113 int l = 0;
1114 while ( l < (int)ba.size() && ba[l] )
1115 l++;
1116 char* str = ba.data();
1117 QChar *uc = new QChar[ l ]; // Can't use macro, since function is public
1118 QChar *result = uc;
1119 if ( len )
1120 *len = l;
1121 while (l--)
1122 *uc++ = *str++;
1123 return result;
1124}
1125
1126static QChar* internalLatin1ToUnicode( const QByteArray& ba, uint* len )
1127{
1128 if ( ba.isNull() ) {
1129 *len = 0;
1130 return 0;
1131 }
1132 int l = 0;
1133 while ( l < (int)ba.size() && ba[l] )
1134 l++;
1135 char* str = ba.data();
1136 QChar *uc = QT_ALLOC_QCHAR_VEC( l );
1137 QChar *result = uc;
1138 if ( len )
1139 *len = l;
1140 while (l--)
1141 *uc++ = *str++;
1142 return result;
1143}
1144
1145/*!
1146 \overload
1147
1148 This utility function converts the '\0'-terminated 8-bit string \a
1149 str to Unicode, returning the result and setting \a *len to the
1150 length of the Unicode string.
1151
1152 The caller is responsible for deleting the return value with
1153 delete[].
1154*/
1155
1156QChar* QString::latin1ToUnicode( const char *str, uint* len, uint maxlen )
1157{
1158 QChar* result = 0;
1159 uint l = 0;
1160 if ( str ) {
1161 if ( maxlen != (uint)-1 ) {
1162 while ( l < maxlen && str[l] )
1163 l++;
1164 } else {
1165 // Faster?
1166 l = strlen( str );
1167 }
1168 QChar *uc = new QChar[ l ]; // Can't use macro since function is public
1169 result = uc;
1170 uint i = l;
1171 while ( i-- )
1172 *uc++ = *str++;
1173 }
1174 if ( len )
1175 *len = l;
1176 return result;
1177}
1178
1179static QChar* internalLatin1ToUnicode( const char *str, uint* len,
1180 uint maxlen = (uint)-1 )
1181{
1182 QChar* result = 0;
1183 uint l = 0;
1184 if ( str ) {
1185 if ( maxlen != (uint)-1 ) {
1186 while ( l < maxlen && str[l] )
1187 l++;
1188 } else {
1189 // Faster?
1190 l = strlen( str );
1191 }
1192 QChar *uc = QT_ALLOC_QCHAR_VEC( l );
1193 result = uc;
1194 uint i = l;
1195 while ( i-- )
1196 *uc++ = *str++;
1197 }
1198 if ( len )
1199 *len = l;
1200 return result;
1201}
1202
1203/*!
1204 This utility function converts \a l 16-bit characters from \a uc
1205 to ASCII, returning a '\0'-terminated string.
1206
1207 The caller is responsible for deleting the resultant string with
1208 delete[].
1209*/
1210char* QString::unicodeToLatin1(const QChar *uc, uint l)
1211{
1212 if (!uc) {
1213 return 0;
1214 }
1215 char *a = new char[l+1];
1216 char *result = a;
1217 while (l--) {
1218 *a++ = (uc->unicode() > 0xff) ? '?' : (char)uc->unicode();
1219 uc++;
1220 }
1221 *a = '\0';
1222 return result;
1223}
1224
1225/*****************************************************************************
1226 QString member functions
1227 *****************************************************************************/
1228
1229/*!
1230 \class QString qstring.h
1231 \reentrant
1232
1233 \brief The QString class provides an abstraction of Unicode text
1234 and the classic C '\0'-terminated char array.
1235
1236 \ingroup tools
1237 \ingroup shared
1238 \ingroup text
1239 \mainclass
1240
1241 QString uses \link shclass.html implicit sharing\endlink, which
1242 makes it very efficient and easy to use.
1243
1244 In all of the QString methods that take \c {const char *}
1245 parameters, the \c {const char *} is interpreted as a classic
1246 C-style '\0'-terminated ASCII string. It is legal for the \c
1247 {const char *} parameter to be 0. If the \c {const char *} is not
1248 '\0'-terminated, the results are undefined. Functions that copy
1249 classic C strings into a QString will not copy the terminating
1250 '\0' character. The QChar array of the QString (as returned by
1251 unicode()) is generally not terminated by a '\0'. If you need to
1252 pass a QString to a function that requires a C '\0'-terminated
1253 string use latin1().
1254
1255 \keyword QString::null
1256 A QString that has not been assigned to anything is \e null, i.e.
1257 both the length and data pointer is 0. A QString that references
1258 the empty string ("", a single '\0' char) is \e empty. Both null
1259 and empty QStrings are legal parameters to the methods. Assigning
1260 \c{(const char *) 0} to QString gives a null QString. For
1261 convenience, \c QString::null is a null QString. When sorting,
1262 empty strings come first, followed by non-empty strings, followed
1263 by null strings. We recommend using \c{if ( !str.isNull() )} to
1264 check for a non-null string rather than \c{if ( !str )}; see \l
1265 operator!() for an explanation.
1266
1267 Note that if you find that you are mixing usage of \l QCString,
1268 QString, and \l QByteArray, this causes lots of unnecessary
1269 copying and might indicate that the true nature of the data you
1270 are dealing with is uncertain. If the data is '\0'-terminated 8-bit
1271 data, use \l QCString; if it is unterminated (i.e. contains '\0's)
1272 8-bit data, use \l QByteArray; if it is text, use QString.
1273
1274 Lists of strings are handled by the QStringList class. You can
1275 split a string into a list of strings using QStringList::split(),
1276 and join a list of strings into a single string with an optional
1277 separator using QStringList::join(). You can obtain a list of
1278 strings from a string list that contain a particular substring or
1279 that match a particular \link qregexp.html regex\endlink using
1280 QStringList::grep().
1281
1282 <b>Note for C programmers</b>
1283
1284 Due to C++'s type system and the fact that QString is implicitly
1285 shared, QStrings can be treated like ints or other simple base
1286 types. For example:
1287
1288 \code
1289 QString boolToString( bool b )
1290 {
1291 QString result;
1292 if ( b )
1293 result = "True";
1294 else
1295 result = "False";
1296 return result;
1297 }
1298 \endcode
1299
1300 The variable, result, is an auto variable allocated on the stack.
1301 When return is called, because we're returning by value, The copy
1302 constructor is called and a copy of the string is returned. (No
1303 actual copying takes place thanks to the implicit sharing, see
1304 below.)
1305
1306 Throughout Qt's source code you will encounter QString usages like
1307 this:
1308 \code
1309 QString func( const QString& input )
1310 {
1311 QString output = input;
1312 // process output
1313 return output;
1314 }
1315 \endcode
1316
1317 The 'copying' of input to output is almost as fast as copying a
1318 pointer because behind the scenes copying is achieved by
1319 incrementing a reference count. QString (like all Qt's implicitly
1320 shared classes) operates on a copy-on-write basis, only copying if
1321 an instance is actually changed.
1322
1323 If you wish to create a deep copy of a QString without losing any
1324 Unicode information then you should use QDeepCopy.
1325
1326 \sa QChar QCString QByteArray QConstString
1327*/
1328
1329/*! \enum Qt::ComparisonFlags
1330\internal
1331*/
1332/*!
1333 \enum Qt::StringComparisonMode
1334
1335 This enum type is used to set the string comparison mode when
1336 searching for an item. It is used by QListBox, QListView and
1337 QIconView, for example. We'll refer to the string being searched
1338 as the 'target' string.
1339
1340 \value CaseSensitive The strings must match case sensitively.
1341 \value ExactMatch The target and search strings must match exactly.
1342 \value BeginsWith The target string begins with the search string.
1343 \value EndsWith The target string ends with the search string.
1344 \value Contains The target string contains the search string.
1345
1346 If you OR these flags together (excluding \c CaseSensitive), the
1347 search criteria be applied in the following order: \c ExactMatch,
1348 \c BeginsWith, \c EndsWith, \c Contains.
1349
1350 Matching is case-insensitive unless \c CaseSensitive is set. \c
1351 CaseSensitive can be OR-ed with any combination of the other
1352 flags.
1353
1354*/
1355Q_EXPORT QStringData *QString::shared_null = 0;
1356QT_STATIC_CONST_IMPL QString QString::null;
1357QT_STATIC_CONST_IMPL QChar QChar::null;
1358QT_STATIC_CONST_IMPL QChar QChar::replacement((ushort)0xfffd);
1359QT_STATIC_CONST_IMPL QChar QChar::byteOrderMark((ushort)0xfeff);
1360QT_STATIC_CONST_IMPL QChar QChar::byteOrderSwapped((ushort)0xfffe);
1361QT_STATIC_CONST_IMPL QChar QChar::nbsp((ushort)0x00a0);
1362
1363QStringData* QString::makeSharedNull()
1364{
1365 QString::shared_null = new QStringData;
1366#if defined( Q_OS_MAC ) || defined(Q_OS_SOLARIS) || defined(Q_OS_HPUX) || defined(Q_OS_AIX)
1367 QString *that = const_cast<QString *>(&QString::null);
1368 that->d = QString::shared_null;
1369#endif
1370 return QString::shared_null;
1371}
1372
1373/*!
1374 \fn QString::QString()
1375
1376 Constructs a null string, i.e. both the length and data pointer
1377 are 0.
1378
1379 \sa isNull()
1380*/
1381
1382/*!
1383 Constructs a string of length one, containing the character \a ch.
1384*/
1385QString::QString( QChar ch )
1386{
1387 d = new QStringData( QT_ALLOC_QCHAR_VEC( 1 ), 1, 1 );
1388 d->unicode[0] = ch;
1389}
1390
1391/*!
1392 Constructs an implicitly shared copy of \a s. This is very fast
1393 since it only involves incrementing a reference count.
1394*/
1395QString::QString( const QString &s ) :
1396 d(s.d)
1397{
1398 d->ref();
1399}
1400
1401/*!
1402 \internal
1403
1404 Private function.
1405
1406 Constructs a string with preallocated space for \a size characters.
1407
1408 The string is empty.
1409
1410 \sa isNull()
1411*/
1412
1413QString::QString( int size, bool /*dummy*/ )
1414{
1415 if ( size ) {
1416 int l = size;
1417 QChar* uc = QT_ALLOC_QCHAR_VEC( l );
1418 d = new QStringData( uc, 0, l );
1419 } else {
1420 d = shared_null ? shared_null : (shared_null=new QStringData);
1421 d->ref();
1422 }
1423}
1424
1425/*!
1426 Constructs a string that is a deep copy of \a ba interpreted as a
1427 classic C string.
1428*/
1429
1430QString::QString( const QByteArray& ba )
1431{
1432#ifndef QT_NO_TEXTCODEC
1433 if ( QTextCodec::codecForCStrings() ) {
1434 d = 0;
1435 *this = fromAscii( ba.data(), ba.size() );
1436 return;
1437 }
1438#endif
1439 uint l;
1440 QChar *uc = internalLatin1ToUnicode(ba,&l);
1441 d = new QStringData(uc,l,l);
1442}
1443
1444/*!
1445 Constructs a string that is a deep copy of the first \a length
1446 characters in the QChar array.
1447
1448 If \a unicode and \a length are 0, then a null string is created.
1449
1450 If only \a unicode is 0, the string is empty but has \a length
1451 characters of space preallocated: QString expands automatically
1452 anyway, but this may speed up some cases a little. We recommend
1453 using the plain constructor and setLength() for this purpose since
1454 it will result in more readable code.
1455
1456 \sa isNull() setLength()
1457*/
1458
1459QString::QString( const QChar* unicode, uint length )
1460{
1461 if ( !unicode && !length ) {
1462 d = shared_null ? shared_null : makeSharedNull();
1463 d->ref();
1464 } else {
1465 QChar* uc = QT_ALLOC_QCHAR_VEC( length );
1466 if ( unicode )
1467 memcpy(uc, unicode, length*sizeof(QChar));
1468 d = new QStringData(uc,unicode ? length : 0,length);
1469 }
1470}
1471
1472/*!
1473 Constructs a string that is a deep copy of \a str, interpreted as
1474 a classic C string.
1475
1476 If \a str is 0, then a null string is created.
1477
1478 This is a cast constructor, but it is perfectly safe: converting a
1479 Latin-1 \c{const char *} to QString preserves all the information. You
1480 can disable this constructor by defining \c QT_NO_CAST_ASCII when
1481 you compile your applications. You can also make QString objects
1482 by using setLatin1(), fromLatin1(), fromLocal8Bit(), and
1483 fromUtf8(). Or whatever encoding is appropriate for the 8-bit data
1484 you have.
1485
1486 \sa isNull()
1487*/
1488
1489QString::QString( const char *str )
1490{
1491#ifndef QT_NO_TEXTCODEC
1492 if ( QTextCodec::codecForCStrings() ) {
1493 d = 0;
1494 *this = fromAscii( str );
1495 return;
1496 }
1497#endif
1498 uint l;
1499 QChar *uc = internalLatin1ToUnicode(str,&l);
1500 d = new QStringData(uc,l,l);
1501}
1502
1503#ifndef QT_NO_STL
1504/*!
1505 Constructs a string that is a deep copy of \a str.
1506
1507 This is the same as fromAscii(\a str).
1508*/
1509
1510QString::QString( const std::string &str )
1511{
1512#ifndef QT_NO_TEXTCODEC
1513 if ( QTextCodec::codecForCStrings() ) {
1514 d = 0;
1515 *this = fromAscii( str.c_str() );
1516 return;
1517 }
1518#endif
1519 uint l;
1520 QChar *uc = internalLatin1ToUnicode(str.c_str(),&l);
1521 d = new QStringData(uc,l,l);
1522}
1523#endif
1524
1525/*!
1526 \fn QString::~QString()
1527
1528 Destroys the string and frees the string's data if this is the
1529 last reference to the string.
1530*/
1531
1532
1533/*!
1534 Deallocates any space reserved solely by this QString.
1535
1536 If the string does not share its data with another QString
1537 instance, nothing happens; otherwise the function creates a new,
1538 unique copy of this string. This function is called whenever the
1539 string is modified.
1540*/
1541
1542void QString::real_detach()
1543{
1544 setLength( length() );
1545}
1546
1547void QString::deref()
1548{
1549 if ( d && d->deref() ) {
1550 if ( d != shared_null )
1551 delete d;
1552 d = 0;
1553 }
1554}
1555
1556void QStringData::deleteSelf()
1557{
1558 delete this;
1559}
1560
1561/*!
1562 \fn QString& QString::operator=( QChar c )
1563
1564 Sets the string to contain just the single character \a c.
1565*/
1566
1567/*!
1568 \fn QString& QString::operator=( const std::string& s )
1569
1570 \overload
1571
1572 Makes a deep copy of \a s and returns a reference to the deep
1573 copy.
1574*/
1575
1576/*!
1577 \fn QString& QString::operator=( char c )
1578
1579 \overload
1580
1581 Sets the string to contain just the single character \a c.
1582*/
1583
1584/*!
1585 \overload
1586
1587 Assigns a shallow copy of \a s to this string and returns a
1588 reference to this string. This is very fast because the string
1589 isn't actually copied.
1590*/
1591QString &QString::operator=( const QString &s )
1592{
1593 s.d->ref();
1594 deref();
1595 d = s.d;
1596 return *this;
1597}
1598
1599/*!
1600 \overload
1601
1602 Assigns a deep copy of \a cstr, interpreted as a classic C
1603 string, to this string. Returns a reference to this string.
1604*/
1605QString &QString::operator=( const QCString& cstr )
1606{
1607 return setAscii( cstr );
1608}
1609
1610
1611/*!
1612 \overload
1613
1614 Assigns a deep copy of \a str, interpreted as a classic C string
1615 to this string and returns a reference to this string.
1616
1617 If \a str is 0, then a null string is created.
1618
1619 \sa isNull()
1620*/
1621QString &QString::operator=( const char *str )
1622{
1623 return setAscii(str);
1624}
1625
1626
1627/*!
1628 \fn bool QString::isNull() const
1629
1630 Returns TRUE if the string is null; otherwise returns FALSE. A
1631 null string is always empty.
1632
1633 \code
1634 QString a; // a.unicode() == 0, a.length() == 0
1635 a.isNull(); // TRUE, because a.unicode() == 0
1636 a.isEmpty(); // TRUE, because a.length() == 0
1637 \endcode
1638
1639 \sa isEmpty(), length()
1640*/
1641
1642/*!
1643 \fn bool QString::isEmpty() const
1644
1645 Returns TRUE if the string is empty, i.e. if length() == 0;
1646 otherwise returns FALSE. Null strings are also empty.
1647
1648 \code
1649 QString a( "" );
1650 a.isEmpty(); // TRUE
1651 a.isNull(); // FALSE
1652
1653 QString b;
1654 b.isEmpty(); // TRUE
1655 b.isNull(); // TRUE
1656 \endcode
1657
1658 \sa isNull(), length()
1659*/
1660
1661/*!
1662 \fn uint QString::length() const
1663
1664 Returns the length of the string.
1665
1666 Null strings and empty strings have zero length.
1667
1668 \sa isNull(), isEmpty()
1669*/
1670
1671/*!
1672 If \a newLen is less than the length of the string, then the
1673 string is truncated at position \a newLen. Otherwise nothing
1674 happens.
1675
1676 \code
1677 QString s = "truncate me";
1678 s.truncate( 5 ); // s == "trunc"
1679 \endcode
1680
1681 \sa setLength()
1682*/
1683
1684void QString::truncate( uint newLen )
1685{
1686 if ( newLen < d->len )
1687 setLength( newLen );
1688}
1689
1690/*!
1691 Ensures that at least \a newLen characters are allocated to the
1692 string, and sets the length of the string to \a newLen. Any new
1693 space allocated contains arbitrary data.
1694
1695 \sa reserve(), truncate()
1696*/
1697void QString::setLength( uint newLen )
1698{
1699 if ( d->count != 1 || newLen > d->maxl ||
1700 ( newLen * 4 < d->maxl && d->maxl > 4 ) ) {
1701 // detach, grow or shrink
1702 uint newMax = computeNewMax( newLen );
1703 QChar* nd = QT_ALLOC_QCHAR_VEC( newMax );
1704 if ( nd ) {
1705 uint len = QMIN( d->len, newLen );
1706 memcpy( nd, d->unicode, sizeof(QChar) * len );
1707 deref();
1708 d = new QStringData( nd, newLen, newMax );
1709 }
1710 } else {
1711 d->len = newLen;
1712 d->setDirty();
1713 }
1714}
1715
1716/*!
1717 \fn uint QString::capacity() const
1718
1719 Returns the number of characters this string can hold
1720 in the allocated memory.
1721
1722 \sa reserve(), squeeze()
1723*/
1724
1725/*!
1726 Ensures that at least \a minCapacity characters are allocated to
1727 the string.
1728
1729 This function is useful for code that needs to build up a long
1730 string and wants to avoid repeated reallocation. In this example,
1731 we want to add to the string until some condition is true, and
1732 we're fairly sure that size is big enough:
1733 \code
1734 QString result;
1735 int len = 0;
1736 result.reserve(maxLen);
1737 while (...) {
1738 result[len++] = ... // fill part of the space
1739 }
1740 result.squeeze();
1741 \endcode
1742
1743 If \e maxLen is an underestimate, the worst that will happen is
1744 that the loop will slow down.
1745
1746 If it is not possible to allocate enough memory, the string
1747 remains unchanged.
1748
1749 \sa capacity(), squeeze(), setLength()
1750*/
1751
1752void QString::reserve( uint minCapacity )
1753{
1754 if ( d->maxl < minCapacity ) {
1755 QChar *nd = QT_ALLOC_QCHAR_VEC( minCapacity );
1756 if ( nd ) {
1757 uint len = d->len;
1758 if ( len )
1759 memcpy( nd, d->unicode, sizeof(QChar) * len );
1760 deref();
1761 d = new QStringData( nd, len, minCapacity );
1762 }
1763 }
1764}
1765
1766
1767/*!
1768 Squeezes the string's capacity to the current content.
1769
1770 \sa capacity(), reserve()
1771*/
1772void QString::squeeze()
1773{
1774 if ( d->maxl > d->len ) {
1775 QChar *nd = QT_ALLOC_QCHAR_VEC( d->len );
1776 if ( nd ) {
1777 uint len = d->len;
1778 if ( len )
1779 memcpy( nd, d->unicode, sizeof(QChar) * len );
1780 deref();
1781 d = new QStringData( nd, len, len );
1782 }
1783 }
1784}
1785
1786/*!
1787 \internal
1788
1789 Like setLength, but doesn't shrink the allocated memory.
1790*/
1791void QString::grow( uint newLen )
1792{
1793 if ( d->count != 1 || newLen > d->maxl ) {
1794 setLength( newLen );
1795 } else {
1796 d->len = newLen;
1797 d->setDirty();
1798 }
1799}
1800
1801struct ArgEscapeData
1802{
1803 uint min_escape; // lowest escape sequence number
1804 uint occurrences; // number of occurences of the lowest escape
1805 // sequence number
1806 uint locale_occurrences; // number of occurences of the lowest escape
1807 // sequence number which contain 'L'
1808 uint escape_len; // total length of escape sequences which will
1809 // be replaced
1810};
1811
1812static ArgEscapeData findArgEscapes(const QString &s)
1813{
1814 const QChar *uc_begin = s.unicode();
1815 const QChar *uc_end = uc_begin + s.length();
1816
1817 ArgEscapeData d;
1818
1819 d.min_escape = 10;
1820 d.occurrences = 0;
1821 d.escape_len = 0;
1822 d.locale_occurrences = 0;
1823
1824 const QChar *c = uc_begin;
1825 while (c != uc_end) {
1826 while (c != uc_end && c->unicode() != '%')
1827 ++c;
1828
1829 if (c == uc_end || ++c == uc_end)
1830 break;
1831
1832 bool locale_arg = false;
1833 if (c->unicode() == 'L') {
1834 locale_arg = true;
1835 if (++c == uc_end)
1836 break;
1837 }
1838
1839 if (c->unicode() < '0' || c->unicode() > '9')
1840 continue;
1841
1842 uint escape = c->unicode() - '0';
1843 ++c;
1844
1845 if (escape > d.min_escape)
1846 continue;
1847
1848 if (escape < d.min_escape) {
1849 d.min_escape = escape;
1850 d.occurrences = 0;
1851 d.escape_len = 0;
1852 d.locale_occurrences = 0;
1853 }
1854
1855#if QT_VERSION < 0x040000
1856 // ### remove preprocessor in Qt 4.0
1857 /* Since in Qt < 4.0 only the first instance is replaced,
1858 escape_len should hold the length of only the first escape
1859 sequence */
1860 if (d.occurrences == 0)
1861#endif
1862 {
1863 ++d.occurrences;
1864 if (locale_arg) {
1865 ++d.locale_occurrences;
1866 d.escape_len += 3;
1867 }
1868 else
1869 d.escape_len += 2;
1870 }
1871 }
1872
1873 return d;
1874}
1875
1876static QString replaceArgEscapes(const QString &s, const ArgEscapeData &d, int field_width,
1877 const QString &arg, const QString &larg)
1878{
1879 const QChar *uc_begin = s.unicode();
1880 const QChar *uc_end = uc_begin + s.length();
1881
1882 uint abs_field_width = QABS(field_width);
1883 uint result_len = s.length()
1884 - d.escape_len
1885 + (d.occurrences - d.locale_occurrences)
1886 *QMAX(abs_field_width, arg.length())
1887 + d.locale_occurrences
1888 *QMAX(abs_field_width, larg.length());
1889
1890 QString result;
1891 result.setLength(result_len);
1892 QChar *result_buff = (QChar*) result.unicode();
1893
1894 QChar *rc = result_buff;
1895 const QChar *c = uc_begin;
1896 uint repl_cnt = 0;
1897 while (c != uc_end) {
1898 /* We don't have to check if we run off the end of the string with c,
1899 because as long as d.occurrences > 0 we KNOW there are valid escape
1900 sequences. */
1901
1902 const QChar *text_start = c;
1903
1904 while (c->unicode() != '%')
1905 ++c;
1906
1907 const QChar *escape_start = c++;
1908
1909 bool locale_arg = false;
1910 if (c->unicode() == 'L') {
1911 locale_arg = true;
1912 ++c;
1913 }
1914
1915 if (c->unicode() != '0' + d.min_escape) {
1916 memcpy(rc, text_start, (c - text_start)*sizeof(QChar));
1917 rc += c - text_start;
1918 }
1919 else {
1920 ++c;
1921
1922 memcpy(rc, text_start, (escape_start - text_start)*sizeof(QChar));
1923 rc += escape_start - text_start;
1924
1925 uint pad_chars;
1926 if (locale_arg)
1927 pad_chars = QMAX(abs_field_width, larg.length()) - larg.length();
1928 else
1929 pad_chars = QMAX(abs_field_width, arg.length()) - arg.length();
1930
1931 if (field_width > 0) { // left padded
1932 for (uint i = 0; i < pad_chars; ++i)
1933 (rc++)->unicode() = ' ';
1934 }
1935
1936 if (locale_arg) {
1937 memcpy(rc, larg.unicode(), larg.length()*sizeof(QChar));
1938 rc += larg.length();
1939 }
1940 else {
1941 memcpy(rc, arg.unicode(), arg.length()*sizeof(QChar));
1942 rc += arg.length();
1943 }
1944
1945 if (field_width < 0) { // right padded
1946 for (uint i = 0; i < pad_chars; ++i)
1947 (rc++)->unicode() = ' ';
1948 }
1949
1950 if (++repl_cnt == d.occurrences) {
1951 memcpy(rc, c, (uc_end - c)*sizeof(QChar));
1952 rc += uc_end - c;
1953 Q_ASSERT(rc - result_buff == (int)result_len);
1954 c = uc_end;
1955 }
1956 }
1957 }
1958
1959 return result;
1960}
1961
1962/*!
1963 This function will return a string that replaces the lowest
1964 numbered occurrence of \c %1, \c %2, ..., \c %9 with \a a.
1965
1966 The \a fieldWidth value specifies the minimum amount of space that
1967 \a a is padded to. A positive value will produce right-aligned
1968 text, whereas a negative value will produce left-aligned text.
1969
1970 The following example shows how we could create a 'status' string
1971 when processing a list of files:
1972 \code
1973 QString status = QString( "Processing file %1 of %2: %3" )
1974 .arg( i ) // current file's number
1975 .arg( total ) // number of files to process
1976 .arg( fileName ); // current file's name
1977 \endcode
1978
1979 It is generally fine to use filenames and numbers as we have done
1980 in the example above. But note that using arg() to construct
1981 natural language sentences does not usually translate well into
1982 other languages because sentence structure and word order often
1983 differ between languages.
1984
1985 If there is no place marker (\c %1, \c %2, etc.), a warning
1986 message (qWarning()) is output and the result is undefined.
1987*/
1988QString QString::arg( const QString& a, int fieldWidth ) const
1989{
1990 ArgEscapeData d = findArgEscapes(*this);
1991
1992 if (d.occurrences == 0) {
1993 qWarning( "QString::arg(): Argument missing: %s, %s", latin1(),
1994 a.latin1() );
1995 return *this;
1996 }
1997
1998 return replaceArgEscapes(*this, d, fieldWidth, a, a);
1999}
2000
2001/*!
2002 \fn QString QString::arg( const QString& a1, const QString& a2 ) const
2003
2004 \overload
2005
2006 This is the same as str.arg(\a a1).arg(\a a2), except that
2007 the strings are replaced in one pass. This can make a difference
2008 if \a a1 contains e.g. \c{%1}:
2009
2010 \code
2011 QString str( "%1 %2" );
2012 str.arg( "Hello", "world" ); // returns "Hello world"
2013 str.arg( "Hello" ).arg( "world" ); // returns "Hello world"
2014
2015 str.arg( "(%1)", "Hello" ); // returns "(%1) Hello"
2016 str.arg( "(%1)" ).arg( "Hello" ); // returns "(Hello) %2"
2017 \endcode
2018*/
2019
2020/*!
2021 \fn QString QString::arg( const QString& a1, const QString& a2,
2022 const QString& a3 ) const
2023 \overload
2024
2025 This is the same as calling str.arg(\a a1).arg(\a a2).arg(\a a3),
2026 except that the strings are replaced in one pass.
2027*/
2028
2029/*!
2030 \fn QString QString::arg( const QString& a1, const QString& a2,
2031 const QString& a3, const QString& a4 ) const
2032 \overload
2033
2034 This is the same as calling
2035 str.arg(\a a1).arg(\a a2).arg(\a a3).arg(\a a4),
2036 except that the strings are replaced in one pass.
2037*/
2038
2039/*!
2040 \overload
2041
2042 The \a fieldWidth value specifies the minimum amount of space that
2043 \a a is padded to. A positive value will produce a right-aligned
2044 number, whereas a negative value will produce a left-aligned
2045 number.
2046
2047 \a a is expressed in base \a base, which is 10 by default and must
2048 be between 2 and 36.
2049
2050 The '%' can be followed by an 'L', in which case the sequence is
2051 replaced with a localized representation of \a a. The conversion uses
2052 the default locale, set by QLocale::setDefaultLocale(). If no default
2053 locale was specified, the "C" locale is used. The 'L' flag is ignored
2054 if \a base is not 10.
2055
2056 \code
2057 QString str;
2058 str = QString( "Decimal 63 is %1 in hexadecimal" )
2059 .arg( 63, 0, 16 );
2060 // str == "Decimal 63 is 3f in hexadecimal"
2061
2062 QLocale::setDefaultLocale(QLocale::English, QLocale::UnitedStates);
2063 str = QString( "%1 %L2 %L3" )
2064 .arg( 12345 )
2065 .arg( 12345 )
2066 .arg( 12345, 0, 16 );
2067 // str == "12345 12,345 3039"
2068 \endcode
2069*/
2070QString QString::arg( long a, int fieldWidth, int base ) const
2071{
2072 return arg((Q_LLONG)a, fieldWidth, base);
2073}
2074
2075/*!
2076 \overload
2077
2078 \a a is expressed in base \a base, which is 10 by default and must
2079 be between 2 and 36. If \a base is 10, the '%l' syntax can be used
2080 to produce localized strings.
2081*/
2082QString QString::arg( ulong a, int fieldWidth, int base ) const
2083{
2084 return arg((Q_ULLONG)a, fieldWidth, base);
2085}
2086
2087/*!
2088 \overload
2089
2090 \a a is expressed in base \a base, which is 10 by default and must
2091 be between 2 and 36. If \a base is 10, the '%l' syntax can be used
2092 to produce localized strings.
2093*/
2094QString QString::arg( Q_LLONG a, int fieldWidth, int base ) const
2095{
2096 ArgEscapeData d = findArgEscapes(*this);
2097
2098 if (d.occurrences == 0) {
2099 qWarning( "QString::arg(): Argument missing: %s, %lld", latin1(),
2100 a );
2101 return *this;
2102 }
2103
2104 QString arg;
2105 if (d.occurrences > d.locale_occurrences)
2106 arg = number(a, base);
2107
2108 QString locale_arg;
2109 if (d.locale_occurrences > 0) {
2110 QLocale locale;
2111 locale_arg = locale.d->longLongToString(a, -1, base, -1, QLocalePrivate::ThousandsGroup);
2112 }
2113
2114 return replaceArgEscapes(*this, d, fieldWidth, arg, locale_arg);
2115}
2116
2117/*!
2118 \overload
2119
2120 \a a is expressed in base \a base, which is 10 by default and must
2121 be between 2 and 36. If \a base is 10, the '%l' syntax can be used
2122 to produce localized strings.
2123*/
2124QString QString::arg( Q_ULLONG a, int fieldWidth, int base ) const
2125{
2126 ArgEscapeData d = findArgEscapes(*this);
2127
2128 if (d.occurrences == 0) {
2129 qWarning( "QString::arg(): Argument missing: %s, %llu", latin1(),
2130 a );
2131 return *this;
2132 }
2133
2134 QString arg;
2135 if (d.occurrences > d.locale_occurrences)
2136 arg = number(a, base);
2137
2138 QString locale_arg;
2139 if (d.locale_occurrences > 0) {
2140 QLocale locale;
2141 locale_arg = locale.d->unsLongLongToString(a, -1, base, -1, QLocalePrivate::ThousandsGroup);
2142 }
2143
2144 return replaceArgEscapes(*this, d, fieldWidth, arg, locale_arg);
2145}
2146
2147/*!
2148 \fn QString QString::arg( int a, int fieldWidth, int base ) const
2149
2150 \overload
2151
2152 \a a is expressed in base \a base, which is 10 by default and must
2153 be between 2 and 36. If \a base is 10, the '%l' syntax can be used
2154 to produce localized strings.
2155*/
2156
2157/*!
2158 \fn QString QString::arg( uint a, int fieldWidth, int base ) const
2159
2160 \overload
2161
2162 \a a is expressed in base \a base, which is 10 by default and must
2163 be between 2 and 36. If \a base is 10, the '%l' syntax can be used
2164 to produce localized strings.
2165*/
2166
2167/*!
2168 \fn QString QString::arg( short a, int fieldWidth, int base ) const
2169
2170 \overload
2171
2172 \a a is expressed in base \a base, which is 10 by default and must
2173 be between 2 and 36. If \a base is 10, the '%l' syntax can be used
2174 to produce localized strings.
2175*/
2176
2177/*!
2178 \fn QString QString::arg( ushort a, int fieldWidth, int base ) const
2179
2180 \overload
2181
2182 \a a is expressed in base \a base, which is 10 by default and must
2183 be between 2 and 36. If \a base is 10, the '%l' syntax can be used
2184 to produce localized strings.
2185*/
2186
2187
2188/*!
2189 \overload
2190
2191 \a a is assumed to be in the Latin-1 character set.
2192*/
2193QString QString::arg( char a, int fieldWidth ) const
2194{
2195 QString c;
2196 c += a;
2197 return arg( c, fieldWidth );
2198}
2199
2200/*!
2201 \overload
2202*/
2203QString QString::arg( QChar a, int fieldWidth ) const
2204{
2205 QString c;
2206 c += a;
2207 return arg( c, fieldWidth );
2208}
2209
2210/*!
2211 \overload
2212
2213 \target arg-formats
2214
2215 Argument \a a is formatted according to the \a fmt format specified,
2216 which is 'g' by default and can be any of the following:
2217
2218 \table
2219 \header \i Format \i Meaning
2220 \row \i \c e \i format as [-]9.9e[+|-]999
2221 \row \i \c E \i format as [-]9.9E[+|-]999
2222 \row \i \c f \i format as [-]9.9
2223 \row \i \c g \i use \c e or \c f format, whichever is the most concise
2224 \row \i \c G \i use \c E or \c f format, whichever is the most concise
2225 \endtable
2226
2227 With 'e', 'E', and 'f', \a prec is the number of digits after the
2228 decimal point. With 'g' and 'G', \a prec is the maximum number of
2229 significant digits (trailing zeroes are omitted).
2230
2231 \code
2232 double d = 12.34;
2233 QString ds = QString( "'E' format, precision 3, gives %1" )
2234 .arg( d, 0, 'E', 3 );
2235 // ds == "1.234E+001"
2236 \endcode
2237
2238 The '%l' syntax can be used to produce localized strings.
2239*/
2240QString QString::arg( double a, int fieldWidth, char fmt, int prec ) const
2241{
2242 ArgEscapeData d = findArgEscapes(*this);
2243
2244 if (d.occurrences == 0) {
2245 qWarning( "QString::arg(): Argument missing: %s, %g", latin1(),
2246 a );
2247 return *this;
2248 }
2249
2250 QString arg;
2251 if (d.occurrences > d.locale_occurrences)
2252 arg = number(a, fmt, prec);
2253
2254 QString locale_arg;
2255 if (d.locale_occurrences > 0) {
2256 QLocale locale;
2257
2258 QLocalePrivate::DoubleForm form = QLocalePrivate::DFDecimal;
2259 uint flags = 0;
2260
2261 if (qIsUpper(fmt))
2262 flags = QLocalePrivate::CapitalEorX;
2263 fmt = qToLower(fmt);
2264
2265 switch (fmt) {
2266 case 'f':
2267 form = QLocalePrivate::DFDecimal;
2268 break;
2269 case 'e':
2270 form = QLocalePrivate::DFExponent;
2271 break;
2272 case 'g':
2273 form = QLocalePrivate::DFSignificantDigits;
2274 break;
2275 default:
2276#if defined(QT_CHECK_RANGE)
2277 qWarning( "QString::setNum: Invalid format char '%c'", fmt );
2278#endif
2279 break;
2280 }
2281
2282 flags |= QLocalePrivate::ThousandsGroup;
2283
2284 locale_arg = locale.d->doubleToString(a, prec, form, -1, flags);
2285 }
2286
2287 return replaceArgEscapes(*this, d, fieldWidth, arg, locale_arg);
2288}
2289
2290QString QString::multiArg( int numArgs, const QString& a1, const QString& a2,
2291 const QString& a3, const QString& a4 ) const
2292{
2293 QString result;
2294 union {
2295 int digitUsed[10];
2296 int argForDigit[10];
2297 };
2298 register const QChar *uc = d->unicode;
2299 const QString *args[4];
2300 const int len = (int) length();
2301 const int end = len - 1;
2302 int lastDigit = -1;
2303 int i;
2304
2305 memset( digitUsed, 0, sizeof(digitUsed) );
2306 args[0] = &a1;
2307 args[1] = &a2;
2308 args[2] = &a3;
2309 args[3] = &a4;
2310
2311 for ( i = 0; i < end; i++ ) {
2312 if ( uc[i] == '%' ) {
2313 int digit = uc[i + 1].unicode() - '0';
2314 if ( digit >= 0 && digit <= 9 )
2315 digitUsed[digit]++;
2316 }
2317 }
2318
2319 for ( i = 0; i < numArgs; i++ ) {
2320 do {
2321 ++lastDigit;
2322 } while ( lastDigit < 10 && digitUsed[lastDigit] == 0 );
2323
2324 if ( lastDigit == 10 ) {
2325 qWarning( "QString::arg(): Argument missing: %s, %s",
2326 latin1(), args[i]->latin1() );
2327 numArgs = i;
2328 lastDigit = 9;
2329 break;
2330 }
2331 argForDigit[lastDigit] = i;
2332 }
2333
2334 i = 0;
2335 while ( i < len ) {
2336 if ( uc[i] == '%' && i != end ) {
2337 int digit = uc[i + 1].unicode() - '0';
2338 if ( digit >= 0 && digit <= lastDigit ) {
2339 result += *args[argForDigit[digit]];
2340 i += 2;
2341 continue;
2342 }
2343 }
2344 result += uc[i++];
2345 }
2346 return result;
2347}
2348
2349
2350/*!
2351 Safely builds a formatted string from the format string \a cformat
2352 and an arbitrary list of arguments. The format string supports all
2353 the escape sequences of printf() in the standard C library.
2354
2355 The %s escape sequence expects a utf8() encoded string. The format
2356 string \e cformat is expected to be in latin1. If you need a
2357 Unicode format string, use arg() instead. For typesafe string
2358 building, with full Unicode support, you can use QTextOStream like
2359 this:
2360
2361 \code
2362 QString str;
2363 QString s = ...;
2364 int x = ...;
2365 QTextOStream( &str ) << s << " : " << x;
2366 \endcode
2367
2368 For \link QObject::tr() translations,\endlink especially if the
2369 strings contains more than one escape sequence, you should
2370 consider using the arg() function instead. This allows the order
2371 of the replacements to be controlled by the translator, and has
2372 Unicode support.
2373
2374 The %lc escape sequence expects a unicode character of type ushort
2375 (as returned by QChar::unicode()).
2376 The %ls escape sequence expects a pointer to a zero-terminated
2377 array of unicode characters of type ushort (as returned by
2378 QString::ucs2()).
2379
2380 \sa arg()
2381*/
2382
2383#ifndef QT_NO_SPRINTF
2384QString &QString::sprintf( const char* cformat, ... )
2385{
2386 QLocale locale(QLocale::C);
2387
2388 va_list ap;
2389 va_start( ap, cformat );
2390
2391 if ( !cformat || !*cformat ) {
2392 // Qt 1.x compat
2393 *this = fromLatin1( "" );
2394 return *this;
2395 }
2396
2397 // Parse cformat
2398
2399 QString result;
2400 const char *c = cformat;
2401 for (;;) {
2402 // Copy non-escape chars to result
2403 while (*c != '\0' && *c != '%')
2404 result.append(*c++);
2405
2406 if (*c == '\0')
2407 break;
2408
2409 // Found '%'
2410 const char *escape_start = c;
2411 ++c;
2412
2413 if (*c == '\0') {
2414 result.append('%'); // a % at the end of the string - treat as non-escape text
2415 break;
2416 }
2417 if (*c == '%') {
2418 result.append('%'); // %%
2419 ++c;
2420 continue;
2421 }
2422
2423 // Parse flag characters
2424 unsigned flags = 0;
2425 bool no_more_flags = false;
2426 do {
2427 switch (*c) {
2428 case '#': flags |= QLocalePrivate::Alternate; break;
2429 case '0': flags |= QLocalePrivate::ZeroPadded; break;
2430 case '-': flags |= QLocalePrivate::LeftAdjusted; break;
2431 case ' ': flags |= QLocalePrivate::BlankBeforePositive; break;
2432 case '+': flags |= QLocalePrivate::AlwaysShowSign; break;
2433 case '\'': flags |= QLocalePrivate::ThousandsGroup; break;
2434 default: no_more_flags = true; break;
2435 }
2436
2437 if (!no_more_flags)
2438 ++c;
2439 } while (!no_more_flags);
2440
2441 if (*c == '\0') {
2442 result.append(escape_start); // incomplete escape, treat as non-escape text
2443 break;
2444 }
2445
2446 // Parse field width
2447 int width = -1; // -1 means unspecified
2448 if (qIsDigit(*c)) {
2449 QString width_str;
2450 while (*c != '\0' && qIsDigit(*c))
2451 width_str.append(*c++);
2452
2453 // can't be negative - started with a digit
2454 // contains at least one digit
2455 width = width_str.toInt();
2456 }
2457 else if (*c == '*') {
2458 width = va_arg(ap, int);
2459 if (width < 0)
2460 width = -1; // treat all negative numbers as unspecified
2461 ++c;
2462 }
2463
2464 if (*c == '\0') {
2465 result.append(escape_start); // incomplete escape, treat as non-escape text
2466 break;
2467 }
2468
2469 // Parse precision
2470 int precision = -1; // -1 means unspecified
2471 if (*c == '.') {
2472 ++c;
2473 if (qIsDigit(*c)) {
2474 QString precision_str;
2475 while (*c != '\0' && qIsDigit(*c))
2476 precision_str.append(*c++);
2477
2478 // can't be negative - started with a digit
2479 // contains at least one digit
2480 precision = precision_str.toInt();
2481 }
2482 else if (*c == '*') {
2483 precision = va_arg(ap, int);
2484 if (precision < 0)
2485 precision = -1; // treat all negative numbers as unspecified
2486 ++c;
2487 }
2488 }
2489
2490 if (*c == '\0') {
2491 result.append(escape_start); // incomplete escape, treat as non-escape text
2492 break;
2493 }
2494
2495 // Parse the length modifier
2496 enum LengthMod { lm_none, lm_hh, lm_h, lm_l, lm_ll, lm_L, lm_j, lm_z, lm_t };
2497 LengthMod length_mod = lm_none;
2498 switch (*c) {
2499 case 'h':
2500 ++c;
2501 if (*c == 'h') {
2502 length_mod = lm_hh;
2503 ++c;
2504 }
2505 else
2506 length_mod = lm_h;
2507 break;
2508
2509 case 'l':
2510 ++c;
2511 if (*c == 'l') {
2512 length_mod = lm_ll;
2513 ++c;
2514 }
2515 else
2516 length_mod = lm_l;
2517 break;
2518
2519 case 'L':
2520 ++c;
2521 length_mod = lm_L;
2522 break;
2523
2524 case 'j':
2525 ++c;
2526 length_mod = lm_j;
2527 break;
2528
2529 case 'z':
2530 case 'Z':
2531 ++c;
2532 length_mod = lm_z;
2533 break;
2534
2535 case 't':
2536 ++c;
2537 length_mod = lm_t;
2538 break;
2539
2540 default: break;
2541 }
2542
2543 if (*c == '\0') {
2544 result.append(escape_start); // incomplete escape, treat as non-escape text
2545 break;
2546 }
2547
2548 // Parse the conversion specifier and do the conversion
2549 QString subst;
2550 switch (*c) {
2551 case 'd':
2552 case 'i': {
2553 Q_LLONG i;
2554 switch (length_mod) {
2555 case lm_none: i = va_arg(ap, int); break;
2556 case lm_hh: i = va_arg(ap, int); break;
2557 case lm_h: i = va_arg(ap, int); break;
2558 case lm_l: i = va_arg(ap, long int); break;
2559 case lm_ll: i = va_arg(ap, Q_LLONG); break;
2560 case lm_j: i = va_arg(ap, long int); break;
2561 case lm_z: i = va_arg(ap, size_t); break;
2562 case lm_t: i = va_arg(ap, int); break;
2563 default: i = 0; break;
2564 }
2565 subst = locale.d->longLongToString(i, precision, 10, width, flags);
2566 ++c;
2567 break;
2568 }
2569 case 'o':
2570 case 'u':
2571 case 'x':
2572 case 'X': {
2573 Q_ULLONG u;
2574 switch (length_mod) {
2575 case lm_none: u = va_arg(ap, unsigned int); break;
2576 case lm_hh: u = va_arg(ap, unsigned int); break;
2577 case lm_h: u = va_arg(ap, unsigned int); break;
2578 case lm_l: u = va_arg(ap, unsigned long int); break;
2579 case lm_ll: u = va_arg(ap, Q_ULLONG); break;
2580 default: u = 0; break;
2581 }
2582
2583 if (qIsUpper(*c))
2584 flags |= QLocalePrivate::CapitalEorX;
2585
2586 int base = 10;
2587 switch (qToLower(*c)) {
2588 case 'o':
2589 base = 8; break;
2590 case 'u':
2591 base = 10; break;
2592 case 'x':
2593 base = 16; break;
2594 default: break;
2595 }
2596 subst = locale.d->unsLongLongToString(u, precision, base, width, flags);
2597 ++c;
2598 break;
2599 }
2600 case 'E':
2601 case 'e':
2602 case 'F':
2603 case 'f':
2604 case 'G':
2605 case 'g':
2606 case 'A':
2607 case 'a': {
2608 double d;
2609 if (length_mod == lm_L)
2610 d = va_arg(ap, long double); // not supported - converted to a double
2611 else
2612 d = va_arg(ap, double);
2613
2614 if (qIsUpper(*c))
2615 flags |= QLocalePrivate::CapitalEorX;
2616
2617 QLocalePrivate::DoubleForm form = QLocalePrivate::DFDecimal;
2618 switch (qToLower(*c)) {
2619 case 'e': form = QLocalePrivate::DFExponent; break;
2620 case 'a': // not supported - decimal form used instead
2621 case 'f': form = QLocalePrivate::DFDecimal; break;
2622 case 'g': form = QLocalePrivate::DFSignificantDigits; break;
2623 default: break;
2624 }
2625 subst = locale.d->doubleToString(d, precision, form, width, flags);
2626 ++c;
2627 break;
2628 }
2629 case 'c': {
2630 if (length_mod == lm_l)
2631 subst = QChar((ushort) va_arg(ap, int));
2632 else
2633 subst = (uchar) va_arg(ap, int);
2634 ++c;
2635 break;
2636 }
2637 case 's': {
2638 if (length_mod == lm_l) {
2639 const ushort *buff = va_arg(ap, const ushort*);
2640 const ushort *ch = buff;
2641 while (*ch != 0)
2642 ++ch;
2643 subst.setUnicodeCodes(buff, ch - buff);
2644 } else
2645 subst = QString::fromUtf8(va_arg(ap, const char*));
2646 if (precision != -1)
2647 subst.truncate(precision);
2648 ++c;
2649 break;
2650 }
2651 case 'p': {
2652 Q_ULLONG i;
2653#ifdef Q_OS_WIN64
2654 i = (Q_ULLONG) va_arg(ap, void*);
2655#else
2656 i = (Q_ULONG) va_arg(ap, void*);
2657#endif
2658 flags |= QLocalePrivate::Alternate;
2659 subst = locale.d->unsLongLongToString(i, precision, 16, width, flags);
2660 ++c;
2661 break;
2662 }
2663 case 'n':
2664 switch (length_mod) {
2665 case lm_hh: {
2666 signed char *n = va_arg(ap, signed char*);
2667 *n = result.length();
2668 break;
2669 }
2670 case lm_h: {
2671 short int *n = va_arg(ap, short int*);
2672 *n = result.length();
2673 break;
2674 }
2675 case lm_l: {
2676 long int *n = va_arg(ap, long int*);
2677 *n = result.length();
2678 break;
2679 }
2680 case lm_ll: {
2681 Q_LLONG *n = va_arg(ap, Q_LLONG*);
2682 volatile uint tmp = result.length(); // egcs-2.91.66 gets internal
2683 *n = tmp; // compiler error without volatile
2684 break;
2685 }
2686 default: {
2687 int *n = va_arg(ap, int*);
2688 *n = result.length();
2689 break;
2690 }
2691 }
2692 ++c;
2693 break;
2694
2695 default: // bad escape, treat as non-escape text
2696 for (const char *cc = escape_start; cc != c; ++cc)
2697 result.append(*cc);
2698 continue;
2699 }
2700
2701 if (flags & QLocalePrivate::LeftAdjusted)
2702 result.append(subst.leftJustify(width));
2703 else
2704 result.append(subst.rightJustify(width));
2705 }
2706
2707 va_end(ap);
2708 *this = result;
2709
2710 return *this;
2711}
2712#endif
2713
2714/*!
2715 Fills the string with \a len characters of value \a c, and returns
2716 a reference to the string.
2717
2718 If \a len is negative (the default), the current string length is
2719 used.
2720
2721 \code
2722 QString str;
2723 str.fill( 'g', 5 ); // string == "ggggg"
2724 \endcode
2725*/
2726
2727QString& QString::fill( QChar c, int len )
2728{
2729 if ( len < 0 )
2730 len = length();
2731 if ( len == 0 ) {
2732 *this = "";
2733 } else {
2734 deref();
2735 QChar * nd = QT_ALLOC_QCHAR_VEC( len );
2736 d = new QStringData(nd,len,len);
2737 while (len--) *nd++ = c;
2738 }
2739 return *this;
2740}
2741
2742
2743/*!
2744 \fn QString QString::copy() const
2745
2746 \obsolete
2747
2748 In Qt 2.0 and later, all calls to this function are needless. Just
2749 remove them.
2750*/
2751
2752/*!
2753 \overload
2754
2755 Finds the first occurrence of the character \a c, starting at
2756 position \a index. If \a index is -1, the search starts at the
2757 last character; if -2, at the next to last character and so on.
2758 (See findRev() for searching backwards.)
2759
2760 If \a cs is TRUE (the default), the search is case sensitive;
2761 otherwise the search is case insensitive.
2762
2763 Returns the position of \a c or -1 if \a c could not be found.
2764*/
2765
2766int QString::find( QChar c, int index, bool cs ) const
2767{
2768 const uint l = length();
2769 if ( index < 0 )
2770 index += l;
2771 if ( (uint)index >= l )
2772 return -1;
2773 register const QChar *uc = unicode()+index;
2774 const QChar *end = unicode() + l;
2775 if ( cs ) {
2776 while ( uc < end && *uc != c )
2777 uc++;
2778 } else {
2779 c = ::lower( c );
2780 while ( uc < end && ::lower( *uc ) != c )
2781 uc++;
2782 }
2783 if ( uint(uc - unicode()) >= l )
2784 return -1;
2785 return (int)(uc - unicode());
2786}
2787
2788/* an implementation of the Boyer-Moore search algorithm
2789*/
2790
2791/* initializes the skiptable to know haw far ahead we can skip on a wrong match
2792*/
2793static void bm_init_skiptable( const QString &pattern, uint *skiptable, bool cs )
2794{
2795 int i = 0;
2796 register uint *st = skiptable;
2797 int l = pattern.length();
2798 while ( i++ < 0x100/8 ) {
2799 *(st++) = l;
2800 *(st++) = l;
2801 *(st++) = l;
2802 *(st++) = l;
2803 *(st++) = l;
2804 *(st++) = l;
2805 *(st++) = l;
2806 *(st++) = l;
2807 }
2808 const QChar *uc = pattern.unicode();
2809 if ( cs ) {
2810 while ( l-- ) {
2811 skiptable[ uc->cell() ] = l;
2812 uc++;
2813 }
2814 } else {
2815 while ( l-- ) {
2816 skiptable[ ::lower( *uc ).cell() ] = l;
2817 uc++;
2818 }
2819 }
2820}
2821
2822static int bm_find( const QString &str, int index, const QString &pattern, uint *skiptable, bool cs )
2823{
2824 const uint l = str.length();
2825 if ( pattern.isEmpty() )
2826 return index > (int)l ? -1 : index;
2827
2828 const QChar *uc = str.unicode();
2829 const QChar *puc = pattern.unicode();
2830 const uint pl = pattern.length();
2831 const uint pl_minus_one = pl - 1;
2832
2833 register const QChar *current = uc + index + pl_minus_one;
2834 const QChar *end = uc + l;
2835 if ( cs ) {
2836 while ( current < end ) {
2837 uint skip = skiptable[ current->cell() ];
2838 if ( !skip ) {
2839 // possible match
2840 while ( skip < pl ) {
2841 if ( *(current - skip ) != puc[pl_minus_one-skip] )
2842 break;
2843 skip++;
2844 }
2845 if ( skip > pl_minus_one ) { // we have a match
2846 return (current - uc) - skip + 1;
2847 }
2848 // in case we don't have a match we are a bit inefficient as we only skip by one
2849 // when we have the non matching char in the string.
2850 if ( skiptable[ (current-skip)->cell() ] == pl )
2851 skip = pl - skip;
2852 else
2853 skip = 1;
2854 }
2855 current += skip;
2856 }
2857 } else {
2858 while ( current < end ) {
2859 uint skip = skiptable[ ::lower( *current ).cell() ];
2860 if ( !skip ) {
2861 // possible match
2862 while ( skip < pl ) {
2863 if ( ::lower( *(current - skip) ) != ::lower( puc[pl_minus_one-skip] ) )
2864 break;
2865 skip++;
2866 }
2867 if ( skip > pl_minus_one ) // we have a match
2868 return (current - uc) - skip + 1;
2869 // in case we don't have a match we are a bit inefficient as we only skip by one
2870 // when we have the non matching char in the string.
2871 if ( skiptable[ ::lower(*(current - skip)).cell() ] == pl )
2872 skip = pl - skip;
2873 else
2874 skip = 1;
2875 }
2876 current += skip;
2877 }
2878 }
2879 // not found
2880 return -1;
2881}
2882
2883
2884#define REHASH( a ) \
2885 if ( sl_minus_1 < sizeof(uint) * CHAR_BIT ) \
2886 hashHaystack -= (a) << sl_minus_1; \
2887 hashHaystack <<= 1
2888
2889/*!
2890 \overload
2891
2892 Finds the first occurrence of the string \a str, starting at
2893 position \a index. If \a index is -1, the search starts at the
2894 last character, if it is -2, at the next to last character and so
2895 on. (See findRev() for searching backwards.)
2896
2897 If \a cs is TRUE (the default), the search is case sensitive;
2898 otherwise the search is case insensitive.
2899
2900 Returns the position of \a str or -1 if \a str could not be found.
2901*/
2902
2903int QString::find( const QString& str, int index, bool cs ) const
2904{
2905 const uint l = length();
2906 const uint sl = str.length();
2907 if ( index < 0 )
2908 index += l;
2909 if ( sl + index > l )
2910 return -1;
2911 if ( !sl )
2912 return index;
2913 if (!l)
2914 return -1;
2915
2916#if defined(Q_OS_MACX) && defined(QT_MACOSX_VERSION) && QT_MACOSX_VERSION >= 0x1020
2917 if ( sl == 1 )
2918 return find( *str.unicode(), index, cs );
2919#endif
2920
2921 // we use the Boyer-Moore algorithm in cases where the overhead
2922 // for the hash table should pay off, otherwise we use a simple
2923 // hash function
2924 if ( l > 500 && sl > 5 ) {
2925 uint skiptable[0x100];
2926 bm_init_skiptable( str, skiptable, cs );
2927 return bm_find( *this, index, str, skiptable, cs );
2928 }
2929
2930 /*
2931 We use some hashing for efficiency's sake. Instead of
2932 comparing strings, we compare the hash value of str with that of
2933 a part of this QString. Only if that matches, we call ucstrncmp
2934 or ucstrnicmp.
2935 */
2936 const QChar* needle = str.unicode();
2937 const QChar* haystack = unicode() + index;
2938 const QChar* end = unicode() + (l-sl);
2939 const uint sl_minus_1 = sl-1;
2940 uint hashNeedle = 0, hashHaystack = 0, i;
2941
2942 if ( cs ) {
2943 for ( i = 0; i < sl; ++i ) {
2944 hashNeedle = ((hashNeedle<<1) + needle[i].unicode() );
2945 hashHaystack = ((hashHaystack<<1) + haystack[i].unicode() );
2946 }
2947 hashHaystack -= (haystack+sl_minus_1)->unicode();
2948
2949 while ( haystack <= end ) {
2950 hashHaystack += (haystack+sl_minus_1)->unicode();
2951 if ( hashHaystack == hashNeedle
2952 && ucstrncmp( needle, haystack, sl ) == 0 )
2953 return haystack-unicode();
2954
2955 REHASH( haystack->unicode() );
2956 ++haystack;
2957 }
2958 } else {
2959 for ( i = 0; i < sl; ++i ) {
2960 hashNeedle = ((hashNeedle<<1) +
2961 ::lower( needle[i].unicode() ).unicode() );
2962 hashHaystack = ((hashHaystack<<1) +
2963 ::lower( haystack[i].unicode() ).unicode() );
2964 }
2965
2966 hashHaystack -= ::lower(*(haystack+sl_minus_1)).unicode();
2967 while ( haystack <= end ) {
2968 hashHaystack += ::lower(*(haystack+sl_minus_1)).unicode();
2969 if ( hashHaystack == hashNeedle
2970 && ucstrnicmp( needle, haystack, sl ) == 0 )
2971 return haystack-unicode();
2972
2973 REHASH( ::lower(*haystack).unicode() );
2974 ++haystack;
2975 }
2976 }
2977 return -1;
2978}
2979
2980/*!
2981 \fn int QString::findRev( const char* str, int index ) const
2982
2983 Equivalent to findRev(QString(\a str), \a index).
2984*/
2985
2986/*!
2987 \fn int QString::find( const char* str, int index ) const
2988
2989 \overload
2990
2991 Equivalent to find(QString(\a str), \a index).
2992*/
2993
2994/*!
2995 \overload
2996
2997 Finds the first occurrence of the character \a c, starting at
2998 position \a index and searching backwards. If the index is -1, the
2999 search starts at the last character, if it is -2, at the next to
3000 last character and so on.
3001
3002 Returns the position of \a c or -1 if \a c could not be found.
3003
3004 If \a cs is TRUE (the default), the search is case sensitive;
3005 otherwise the search is case insensitive.
3006
3007 \code
3008 QString string( "bananas" );
3009 int i = string.findRev( 'a' ); // i == 5
3010 \endcode
3011*/
3012
3013int QString::findRev( QChar c, int index, bool cs ) const
3014{
3015#if defined(Q_OS_MACX) && defined(QT_MACOSX_VERSION) && QT_MACOSX_VERSION < 0x1020
3016 return findRev( QString( c ), index, cs );
3017#else
3018 const uint l = length();
3019 if ( index < 0 )
3020 index += l;
3021 if ( (uint)index >= l )
3022 return -1;
3023 const QChar *end = unicode();
3024 register const QChar *uc = end + index;
3025 if ( cs ) {
3026 while ( uc >= end && *uc != c )
3027 uc--;
3028 } else {
3029 c = ::lower( c );
3030 while ( uc >= end && ::lower( *uc ) != c )
3031 uc--;
3032 }
3033 return uc - end;
3034#endif
3035}
3036
3037/*!
3038 \overload
3039
3040 Finds the first occurrence of the string \a str, starting at
3041 position \a index and searching backwards. If the index is -1, the
3042 search starts at the last character, if it is -2, at the next to
3043 last character and so on.
3044
3045 Returns the position of \a str or -1 if \a str could not be found.
3046
3047 If \a cs is TRUE (the default), the search is case sensitive;
3048 otherwise the search is case insensitive.
3049
3050 \code
3051 QString string("bananas");
3052 int i = string.findRev( "ana" ); // i == 3
3053 \endcode
3054*/
3055
3056int QString::findRev( const QString& str, int index, bool cs ) const
3057{
3058 /*
3059 See QString::find() for explanations.
3060 */
3061 const uint l = length();
3062 if ( index < 0 )
3063 index += l;
3064 const uint sl = str.length();
3065 int delta = l-sl;
3066 if ( index < 0 || index > (int)l || delta < 0 )
3067 return -1;
3068 if ( index > delta )
3069 index = delta;
3070
3071#if defined(Q_OS_MACX) && defined(QT_MACOSX_VERSION) && QT_MACOSX_VERSION >= 0x1020
3072 if ( sl == 1 )
3073 return findRev( *str.unicode(), index, cs );
3074#endif
3075
3076 const QChar* needle = str.unicode();
3077 const QChar* haystack = unicode() + index;
3078 const QChar* end = unicode();
3079 const uint sl_minus_1 = sl-1;
3080 const QChar* n = needle+sl_minus_1;
3081 const QChar* h = haystack+sl_minus_1;
3082 uint hashNeedle = 0, hashHaystack = 0, i;
3083
3084 if ( cs ) {
3085 for ( i = 0; i < sl; ++i ) {
3086 hashNeedle = ((hashNeedle<<1) + (n-i)->unicode() );
3087 hashHaystack = ((hashHaystack<<1) + (h-i)->unicode() );
3088 }
3089 hashHaystack -= haystack->unicode();
3090
3091 while ( haystack >= end ) {
3092 hashHaystack += haystack->unicode();
3093 if ( hashHaystack == hashNeedle
3094 && ucstrncmp( needle, haystack, sl ) == 0 )
3095 return haystack-unicode();
3096 --haystack;
3097 REHASH( (haystack+sl)->unicode() );
3098 }
3099 } else {
3100 for ( i = 0; i < sl; ++i ) {
3101 hashNeedle = ((hashNeedle<<1)
3102 + ::lower( (n-i)->unicode() ).unicode() );
3103 hashHaystack = ((hashHaystack<<1)
3104 + ::lower( (h-i)->unicode() ).unicode() );
3105 }
3106 hashHaystack -= ::lower(*haystack).unicode();
3107
3108 while ( haystack >= end ) {
3109 hashHaystack += ::lower(*haystack).unicode();
3110 if ( hashHaystack == hashNeedle
3111 && ucstrnicmp( needle, haystack, sl ) == 0 )
3112 return haystack-unicode();
3113 --haystack;
3114 REHASH( ::lower(*(haystack+sl)).unicode() );
3115 }
3116 }
3117 return -1;
3118}
3119
3120#undef REHASH
3121
3122/*!
3123 \enum QString::SectionFlags
3124
3125 \value SectionDefault Empty fields are counted, leading and
3126 trailing separators are not included, and the separator is
3127 compared case sensitively.
3128
3129 \value SectionSkipEmpty Treat empty fields as if they don't exist,
3130 i.e. they are not considered as far as \e start and \e end are
3131 concerned.
3132
3133 \value SectionIncludeLeadingSep Include the leading separator (if
3134 any) in the result string.
3135
3136 \value SectionIncludeTrailingSep Include the trailing separator
3137 (if any) in the result string.
3138
3139 \value SectionCaseInsensitiveSeps Compare the separator
3140 case-insensitively.
3141
3142 Any of the last four values can be OR-ed together to form a flag.
3143
3144 \sa section()
3145*/
3146
3147/*!
3148 \fn QString QString::section( QChar sep, int start, int end = 0xffffffff, int flags = SectionDefault ) const
3149
3150 This function returns a section of the string.
3151
3152 This string is treated as a sequence of fields separated by the
3153 character, \a sep. The returned string consists of the fields from
3154 position \a start to position \a end inclusive. If \a end is not
3155 specified, all fields from position \a start to the end of the
3156 string are included. Fields are numbered 0, 1, 2, etc., counting
3157 from the left, and -1, -2, etc., counting from right to left.
3158
3159 The \a flags argument can be used to affect some aspects of the
3160 function's behaviour, e.g. whether to be case sensitive, whether
3161 to skip empty fields and how to deal with leading and trailing
3162 separators; see \l{SectionFlags}.
3163
3164 \code
3165 QString csv( "forename,middlename,surname,phone" );
3166 QString s = csv.section( ',', 2, 2 ); // s == "surname"
3167
3168 QString path( "/usr/local/bin/myapp" ); // First field is empty
3169 QString s = path.section( '/', 3, 4 ); // s == "bin/myapp"
3170 QString s = path.section( '/', 3, 3, SectionSkipEmpty ); // s == "myapp"
3171 \endcode
3172
3173 If \a start or \a end is negative, we count fields from the right
3174 of the string, the right-most field being -1, the one from
3175 right-most field being -2, and so on.
3176
3177 \code
3178 QString csv( "forename,middlename,surname,phone" );
3179 QString s = csv.section( ',', -3, -2 ); // s == "middlename,surname"
3180
3181 QString path( "/usr/local/bin/myapp" ); // First field is empty
3182 QString s = path.section( '/', -1 ); // s == "myapp"
3183 \endcode
3184
3185 \sa QStringList::split()
3186*/
3187
3188/*!
3189 \overload
3190
3191 This function returns a section of the string.
3192
3193 This string is treated as a sequence of fields separated by the
3194 string, \a sep. The returned string consists of the fields from
3195 position \a start to position \a end inclusive. If \a end is not
3196 specified, all fields from position \a start to the end of the
3197 string are included. Fields are numbered 0, 1, 2, etc., counting
3198 from the left, and -1, -2, etc., counting from right to left.
3199
3200 The \a flags argument can be used to affect some aspects of the
3201 function's behaviour, e.g. whether to be case sensitive, whether
3202 to skip empty fields and how to deal with leading and trailing
3203 separators; see \l{SectionFlags}.
3204
3205 \code
3206 QString data( "forename**middlename**surname**phone" );
3207 QString s = data.section( "**", 2, 2 ); // s == "surname"
3208 \endcode
3209
3210 If \a start or \a end is negative, we count fields from the right
3211 of the string, the right-most field being -1, the one from
3212 right-most field being -2, and so on.
3213
3214 \code
3215 QString data( "forename**middlename**surname**phone" );
3216 QString s = data.section( "**", -3, -2 ); // s == "middlename**surname"
3217 \endcode
3218
3219 \sa QStringList::split()
3220*/
3221
3222QString QString::section( const QString &sep, int start, int end, int flags ) const
3223{
3224 QStringList sections = QStringList::split(sep, *this, TRUE);
3225 if(sections.isEmpty())
3226 return QString();
3227 if(!(flags & SectionSkipEmpty)) {
3228 if(start < 0)
3229 start += sections.count();
3230 if(end < 0)
3231 end += sections.count();
3232 } else {
3233 int skip = 0;
3234 for(QStringList::Iterator it = sections.begin(); it != sections.end(); ++it) {
3235 if((*it).isEmpty())
3236 skip++;
3237 }
3238 if(start < 0)
3239 start += sections.count() - skip;
3240 if(end < 0)
3241 end += sections.count() - skip;
3242 }
3243 int x = 0, run = 0;
3244 QString ret;
3245 for(QStringList::Iterator it = sections.begin(); x <= end && it != sections.end(); ++it) {
3246 if(x >= start) {
3247 if((*it).isEmpty()) {
3248 run++;
3249 } else {
3250 if(!ret.isEmpty() || !(flags & SectionSkipEmpty)) {
3251 int i_end = run;
3252 if(!ret.isEmpty() && !(flags & SectionIncludeTrailingSep))
3253 i_end++;
3254 if((flags & SectionIncludeLeadingSep) && it != sections.begin() && x == start)
3255 i_end++;
3256 for(int i = 0; i < i_end; i++)
3257 ret += sep;
3258 } else if((flags & SectionIncludeLeadingSep) && it != sections.begin()) {
3259 ret += sep;
3260 }
3261 run = 0;
3262 ret += (*it);
3263 if((flags & SectionIncludeTrailingSep) && it != sections.end())
3264 ret += sep;
3265 }
3266 }
3267 if(!(*it).isEmpty() || !(flags & SectionSkipEmpty))
3268 x++;
3269 }
3270 return ret;
3271}
3272
3273#ifndef QT_NO_REGEXP
3274class section_chunk {
3275public:
3276 section_chunk(int l, QString s) { length = l; string = s; }
3277 int length;
3278 QString string;
3279};
3280/*!
3281 \overload
3282
3283 This function returns a section of the string.
3284
3285 This string is treated as a sequence of fields separated by the
3286 regular expression, \a reg. The returned string consists of the
3287 fields from position \a start to position \a end inclusive. If \a
3288 end is not specified, all fields from position \a start to the end
3289 of the string are included. Fields are numbered 0, 1, 2, etc., counting
3290 from the left, and -1, -2, etc., counting from right to left.
3291
3292 The \a flags argument can be used to affect some aspects of the
3293 function's behaviour, e.g. whether to be case sensitive, whether
3294 to skip empty fields and how to deal with leading and trailing
3295 separators; see \l{SectionFlags}.
3296
3297 \code
3298 QString line( "forename\tmiddlename surname \t \t phone" );
3299 QRegExp sep( "\s+" );
3300 QString s = line.section( sep, 2, 2 ); // s == "surname"
3301 \endcode
3302
3303 If \a start or \a end is negative, we count fields from the right
3304 of the string, the right-most field being -1, the one from
3305 right-most field being -2, and so on.
3306
3307 \code
3308 QString line( "forename\tmiddlename surname \t \t phone" );
3309 QRegExp sep( "\\s+" );
3310 QString s = line.section( sep, -3, -2 ); // s == "middlename surname"
3311 \endcode
3312
3313 \warning Using this QRegExp version is much more expensive than
3314 the overloaded string and character versions.
3315
3316 \sa QStringList::split() simplifyWhiteSpace()
3317*/
3318
3319QString QString::section( const QRegExp &reg, int start, int end, int flags ) const
3320{
3321 const QChar *uc = unicode();
3322 if(!uc)
3323 return QString();
3324
3325 QRegExp sep(reg);
3326 sep.setCaseSensitive(!(flags & SectionCaseInsensitiveSeps));
3327
3328 QPtrList<section_chunk> l;
3329 l.setAutoDelete(TRUE);
3330 int n = length(), m = 0, last_m = 0, last = 0, last_len = 0;
3331
3332 while ( ( m = sep.search( *this, m ) ) != -1 ) {
3333 l.append(new section_chunk(last_len, QString(uc + last_m, m - last_m)));
3334 last_m = m;
3335 last_len = sep.matchedLength();
3336 if((m += sep.matchedLength()) >= n) {
3337 last = 1;
3338 break;
3339 }
3340 }
3341 if(!last)
3342 l.append(new section_chunk(last_len, QString(uc + last_m, n - last_m)));
3343
3344 if(start < 0)
3345 start = l.count() + start;
3346 if(end == -1)
3347 end = l.count();
3348 else if(end < 0)
3349 end = l.count() + end;
3350
3351 int i = 0;
3352 QString ret;
3353 for ( section_chunk *chk=l.first(); chk; chk=l.next(), i++ ) {
3354 if((flags & SectionSkipEmpty) && chk->length == (int)chk->string.length()) {
3355 if(i <= start)
3356 start++;
3357 end++;
3358 }
3359 if(i == start) {
3360 ret = (flags & SectionIncludeLeadingSep) ? chk->string : chk->string.mid(chk->length);
3361 } else if(i > start) {
3362 ret += chk->string;
3363 }
3364 if(i == end) {
3365 if((chk=l.next()) && flags & SectionIncludeTrailingSep)
3366 ret += chk->string.left(chk->length);
3367 break;
3368 }
3369 }
3370 return ret;
3371}
3372#endif
3373
3374/*!
3375 \fn QString QString::section( char sep, int start, int end = 0xffffffff, int flags = SectionDefault ) const
3376
3377 \overload
3378*/
3379
3380/*!
3381 \fn QString QString::section( const char *sep, int start, int end = 0xffffffff, int flags = SectionDefault ) const
3382
3383 \overload
3384*/
3385
3386
3387/*!
3388 Returns the number of times the character \a c occurs in the
3389 string.
3390
3391 If \a cs is TRUE (the default), the search is case sensitive;
3392 otherwise the search is case insensitive.
3393
3394 \code
3395 QString string( "Trolltech and Qt" );
3396 int n = string.contains( 't', FALSE );
3397 // n == 3
3398 \endcode
3399*/
3400
3401int QString::contains( QChar c, bool cs ) const
3402{
3403 int count = 0;
3404 const QChar *uc = unicode();
3405 if ( !uc )
3406 return 0;
3407 int n = length();
3408 if ( cs ) {
3409 while ( n-- )
3410 if ( *uc++ == c )
3411 count++;
3412 } else {
3413 c = ::lower( c );
3414 while ( n-- ) {
3415 if ( ::lower( *uc ) == c )
3416 count++;
3417 uc++;
3418 }
3419 }
3420 return count;
3421}
3422
3423/*!
3424 \overload
3425
3426 Returns the number of times the string \a str occurs in the string.
3427
3428 If \a cs is TRUE (the default), the search is case sensitive;
3429 otherwise the search is case insensitive.
3430*/
3431int QString::contains( const char* str, bool cs ) const
3432{
3433 return contains( QString(str), cs );
3434}
3435
3436/*!
3437 \fn int QString::contains( char c, bool cs ) const
3438
3439 \overload
3440*/
3441
3442/*!
3443 \fn int QString::find( char c, int index, bool cs ) const
3444
3445 \overload
3446
3447 Find character \a c starting from position \a index.
3448
3449 If \a cs is TRUE (the default), the search is case sensitive;
3450 otherwise the search is case insensitive.
3451*/
3452
3453/*!
3454 \fn int QString::findRev( char c, int index, bool cs ) const
3455
3456 \overload
3457
3458 Find character \a c starting from position \a index and working
3459 backwards.
3460
3461 If \a cs is TRUE (the default), the search is case sensitive;
3462 otherwise the search is case insensitive.
3463*/
3464
3465/*!
3466 \overload
3467
3468 Returns the number of times \a str occurs in the string.
3469
3470 If \a cs is TRUE (the default), the search is case sensitive;
3471 otherwise the search is case insensitive.
3472
3473 This function counts overlapping strings, so in the example below,
3474 there are two instances of "ana" in "bananas".
3475
3476 \code
3477 QString str( "bananas" );
3478 int i = str.contains( "ana" ); // i == 2
3479 \endcode
3480
3481 \sa findRev()
3482*/
3483
3484int QString::contains( const QString &str, bool cs ) const
3485{
3486 if ( isNull() )
3487 return 0;
3488 int count = 0;
3489 uint skiptable[0x100];
3490 bm_init_skiptable( str, skiptable, cs );
3491 int i = -1;
3492 // use boyer-moore for the ultimate speed experience
3493 while ( ( i = bm_find( *this, i + 1, str, skiptable, cs ) ) != -1 )
3494 count++;
3495 return count;
3496}
3497
3498/*!
3499 Returns a substring that contains the \a len leftmost characters
3500 of the string.
3501
3502 The whole string is returned if \a len exceeds the length of the
3503 string.
3504
3505 \code
3506 QString s = "Pineapple";
3507 QString t = s.left( 4 ); // t == "Pine"
3508 \endcode
3509
3510 \sa right(), mid(), isEmpty()
3511*/
3512
3513QString QString::left( uint len ) const
3514{
3515 if ( isEmpty() ) {
3516 return QString();
3517 } else if ( len == 0 ) { // ## just for 1.x compat:
3518 return fromLatin1( "" );
3519 } else if ( len >= length() ) {
3520 return *this;
3521 } else {
3522 QString s( len, TRUE );
3523 memcpy( s.d->unicode, d->unicode, len * sizeof(QChar) );
3524 s.d->len = len;
3525 return s;
3526 }
3527}
3528
3529/*!
3530 Returns a string that contains the \a len rightmost characters of
3531 the string.
3532
3533 If \a len is greater than the length of the string then the whole
3534 string is returned.
3535
3536 \code
3537 QString string( "Pineapple" );
3538 QString t = string.right( 5 ); // t == "apple"
3539 \endcode
3540
3541 \sa left(), mid(), isEmpty()
3542*/
3543
3544QString QString::right( uint len ) const
3545{
3546 if ( isEmpty() ) {
3547 return QString();
3548 } else if ( len == 0 ) { // ## just for 1.x compat:
3549 return fromLatin1( "" );
3550 } else {
3551 uint l = length();
3552 if ( len >= l )
3553 return *this;
3554 QString s( len, TRUE );
3555 memcpy( s.d->unicode, d->unicode+(l-len), len*sizeof(QChar) );
3556 s.d->len = len;
3557 return s;
3558 }
3559}
3560
3561/*!
3562 Returns a string that contains the \a len characters of this
3563 string, starting at position \a index.
3564
3565 Returns a null string if the string is empty or \a index is out of
3566 range. Returns the whole string from \a index if \a index + \a len
3567 exceeds the length of the string.
3568
3569 \code
3570 QString s( "Five pineapples" );
3571 QString t = s.mid( 5, 4 ); // t == "pine"
3572 \endcode
3573
3574 \sa left(), right()
3575*/
3576
3577QString QString::mid( uint index, uint len ) const
3578{
3579 uint slen = length();
3580 if ( isEmpty() || index >= slen ) {
3581 return QString();
3582 } else if ( len == 0 ) { // ## just for 1.x compat:
3583 return fromLatin1( "" );
3584 } else {
3585 if ( len > slen-index )
3586 len = slen - index;
3587 if ( index == 0 && len == slen )
3588 return *this;
3589 register const QChar *p = unicode()+index;
3590 QString s( len, TRUE );
3591 memcpy( s.d->unicode, p, len * sizeof(QChar) );
3592 s.d->len = len;
3593 return s;
3594 }
3595}
3596
3597/*!
3598 Returns a string of length \a width that contains this string
3599 padded by the \a fill character.
3600
3601 If \a truncate is FALSE and the length of the string is more than
3602 \a width, then the returned string is a copy of the string.
3603
3604 If \a truncate is TRUE and the length of the string is more than
3605 \a width, then any characters in a copy of the string after length
3606 \a width are removed, and the copy is returned.
3607
3608 \code
3609 QString s( "apple" );
3610 QString t = s.leftJustify( 8, '.' ); // t == "apple..."
3611 \endcode
3612
3613 \sa rightJustify()
3614*/
3615
3616QString QString::leftJustify( uint width, QChar fill, bool truncate ) const
3617{
3618 QString result;
3619 int len = length();
3620 int padlen = width - len;
3621 if ( padlen > 0 ) {
3622 result.setLength(len+padlen);
3623 if ( len )
3624 memcpy( result.d->unicode, unicode(), sizeof(QChar)*len );
3625 QChar* uc = result.d->unicode + len;
3626 while (padlen--)
3627 *uc++ = fill;
3628 } else {
3629 if ( truncate )
3630 result = left( width );
3631 else
3632 result = *this;
3633 }
3634 return result;
3635}
3636
3637/*!
3638 Returns a string of length \a width that contains the \a fill
3639 character followed by the string.
3640
3641 If \a truncate is FALSE and the length of the string is more than
3642 \a width, then the returned string is a copy of the string.
3643
3644 If \a truncate is TRUE and the length of the string is more than
3645 \a width, then the resulting string is truncated at position \a
3646 width.
3647
3648 \code
3649 QString string( "apple" );
3650 QString t = string.rightJustify( 8, '.' ); // t == "...apple"
3651 \endcode
3652
3653 \sa leftJustify()
3654*/
3655
3656QString QString::rightJustify( uint width, QChar fill, bool truncate ) const
3657{
3658 QString result;
3659 int len = length();
3660 int padlen = width - len;
3661 if ( padlen > 0 ) {
3662 result.setLength( len+padlen );
3663 QChar* uc = result.d->unicode;
3664 while (padlen--)
3665 *uc++ = fill;
3666 if ( len )
3667 memcpy( uc, unicode(), sizeof(QChar)*len );
3668 } else {
3669 if ( truncate )
3670 result = left( width );
3671 else
3672 result = *this;
3673 }
3674 return result;
3675}
3676
3677/*!
3678 Returns a lowercase copy of the string.
3679
3680 \code
3681 QString string( "TROlltECH" );
3682 str = string.lower(); // str == "trolltech"
3683 \endcode
3684
3685 \sa upper()
3686*/
3687
3688QString QString::lower() const
3689{
3690 int l = length();
3691 register QChar *p = d->unicode;
3692 while ( l ) {
3693 if ( *p != ::lower(*p) ) {
3694 QString s( *this );
3695 s.real_detach();
3696 p = s.d->unicode + ( p - d->unicode );
3697 while ( l ) {
3698 *p = ::lower( *p );
3699 l--;
3700 p++;
3701 }
3702 return s;
3703 }
3704 l--;
3705 p++;
3706 }
3707 return *this;
3708}
3709
3710/*!
3711 Returns an uppercase copy of the string.
3712
3713 \code
3714 QString string( "TeXt" );
3715 str = string.upper(); // t == "TEXT"
3716 \endcode
3717
3718 \sa lower()
3719*/
3720
3721QString QString::upper() const
3722{
3723 int l = length();
3724 register QChar *p = d->unicode;
3725 while ( l ) {
3726 if ( *p != ::upper(*p) ) {
3727 QString s( *this );
3728 s.real_detach();
3729 p = s.d->unicode + ( p - d->unicode );
3730 while ( l ) {
3731 *p = ::upper( *p );
3732 l--;
3733 p++;
3734 }
3735 return s;
3736 }
3737 l--;
3738 p++;
3739 }
3740 return *this;
3741}
3742
3743
3744/*!
3745 Returns a string that has whitespace removed from the start and
3746 the end.
3747
3748 Whitespace means any character for which QChar::isSpace() returns
3749 TRUE. This includes Unicode characters with decimal values 9
3750 (TAB), 10 (LF), 11 (VT), 12 (FF), 13 (CR) and 32 (Space), and may
3751 also include other Unicode characters.
3752
3753 \code
3754 QString string = " white space ";
3755 QString s = string.stripWhiteSpace(); // s == "white space"
3756 \endcode
3757
3758 \sa simplifyWhiteSpace()
3759*/
3760
3761QString QString::stripWhiteSpace() const
3762{
3763 if ( isEmpty() ) // nothing to do
3764 return *this;
3765 register const QChar *s = unicode();
3766 if ( !s->isSpace() && !s[length()-1].isSpace() )
3767 return *this;
3768
3769 int start = 0;
3770 int end = length() - 1;
3771 while ( start<=end && s[start].isSpace() ) // skip white space from start
3772 start++;
3773 if ( start <= end ) { // only white space
3774 while ( end && s[end].isSpace() ) // skip white space from end
3775 end--;
3776 }
3777 int l = end - start + 1;
3778 if ( l <= 0 )
3779 return QString::fromLatin1("");
3780
3781 QString result( l, TRUE );
3782 memcpy( result.d->unicode, &s[start], sizeof(QChar)*l );
3783 result.d->len = l;
3784 return result;
3785}
3786
3787
3788/*!
3789 Returns a string that has whitespace removed from the start and
3790 the end, and which has each sequence of internal whitespace
3791 replaced with a single space.
3792
3793 Whitespace means any character for which QChar::isSpace() returns
3794 TRUE. This includes Unicode characters with decimal values 9
3795 (TAB), 10 (LF), 11 (VT), 12 (FF), 13 (CR), and 32 (Space).
3796
3797 \code
3798 QString string = " lots\t of\nwhite space ";
3799 QString t = string.simplifyWhiteSpace();
3800 // t == "lots of white space"
3801 \endcode
3802
3803 \sa stripWhiteSpace()
3804*/
3805
3806QString QString::simplifyWhiteSpace() const
3807{
3808 if ( isEmpty() )
3809 return *this;
3810 QString result;
3811 result.setLength( length() );
3812 const QChar *from = unicode();
3813 const QChar *fromend = from+length();
3814 int outc=0;
3815 QChar *to = result.d->unicode;
3816 for (;;) {
3817 while ( from!=fromend && from->isSpace() )
3818 from++;
3819 while ( from!=fromend && !from->isSpace() )
3820 to[outc++] = *from++;
3821 if ( from!=fromend )
3822 to[outc++] = ' ';
3823 else
3824 break;
3825 }
3826 if ( outc > 0 && to[outc-1] == ' ' )
3827 outc--;
3828 result.truncate( outc );
3829 return result;
3830}
3831
3832
3833/*!
3834 Inserts \a s into the string at position \a index.
3835
3836 If \a index is beyond the end of the string, the string is
3837 extended with spaces to length \a index and \a s is then appended
3838 and returns a reference to the string.
3839
3840 \code
3841 QString string( "I like fish" );
3842 str = string.insert( 2, "don't " );
3843 // str == "I don't like fish"
3844 \endcode
3845
3846 \sa remove(), replace()
3847*/
3848
3849QString &QString::insert( uint index, const QString &s )
3850{
3851 // the sub function takes care of &s == this case.
3852 return insert( index, s.unicode(), s.length() );
3853}
3854
3855/*! \fn QString &QString::insert( uint index, const QByteArray &s )
3856 \overload
3857
3858 Inserts \a s into the string at position \a index and returns
3859 a reference to the string.
3860*/
3861
3862/*! \fn QString &QString::insert( uint index, const char *s )
3863 \overload
3864
3865 Inserts \a s into the string at position \a index and returns
3866 a reference to the string.
3867*/
3868
3869#ifndef QT_NO_CAST_ASCII
3870QString &QString::insertHelper( uint index, const char *s, uint len )
3871{
3872 if ( s ) {
3873#ifndef QT_NO_TEXTCODEC
3874 if ( QTextCodec::codecForCStrings() )
3875 return insert( index, fromAscii( s, len ) );
3876#endif
3877 if ( len == UINT_MAX )
3878 len = strlen( s );
3879 if ( len == 0 )
3880 return *this;
3881
3882 uint olen = length();
3883 int nlen = olen + len;
3884
3885 if ( index >= olen ) { // insert after end of string
3886 grow( len + index );
3887 int n = index - olen;
3888 QChar* uc = d->unicode + olen;
3889 while ( n-- )
3890 *uc++ = ' ';
3891
3892 uc = d->unicode + index;
3893 while ( len-- )
3894 *uc++ = *s++;
3895 } else { // normal insert
3896 grow( nlen );
3897 memmove( d->unicode + index + len, unicode() + index,
3898 sizeof(QChar) * (olen - index) );
3899
3900 QChar* uc = d->unicode + index;
3901 while ( len-- )
3902 *uc++ = *s++;
3903 }
3904 }
3905 return *this;
3906}
3907#endif
3908
3909/*!
3910 \overload
3911
3912 Inserts the first \a len characters in \a s into the string at
3913 position \a index and returns a reference to the string.
3914*/
3915
3916QString &QString::insert( uint index, const QChar* s, uint len )
3917{
3918 if ( len == 0 )
3919 return *this;
3920 uint olen = length();
3921 int nlen = olen + len;
3922
3923 if ( s >= d->unicode && (uint)(s - d->unicode) < d->maxl ) {
3924 // Part of me - take a copy.
3925 QChar *tmp = QT_ALLOC_QCHAR_VEC( len );
3926 memcpy(tmp,s,len*sizeof(QChar));
3927 insert(index,tmp,len);
3928 QT_DELETE_QCHAR_VEC( tmp );
3929 return *this;
3930 }
3931
3932 if ( index >= olen ) { // insert after end of string
3933 grow( len + index );
3934 int n = index - olen;
3935 QChar* uc = d->unicode+olen;
3936 while (n--)
3937 *uc++ = ' ';
3938 memcpy( d->unicode+index, s, sizeof(QChar)*len );
3939 } else { // normal insert
3940 grow( nlen );
3941 memmove( d->unicode + index + len, unicode() + index,
3942 sizeof(QChar) * (olen - index) );
3943 memcpy( d->unicode + index, s, sizeof(QChar) * len );
3944 }
3945 return *this;
3946}
3947
3948/*!
3949 \overload
3950
3951 Insert \a c into the string at position \a index and returns a
3952 reference to the string.
3953
3954 If \a index is beyond the end of the string, the string is
3955 extended with spaces (ASCII 32) to length \a index and \a c is
3956 then appended.
3957*/
3958
3959QString &QString::insert( uint index, QChar c ) // insert char
3960{
3961 QString s( c );
3962 return insert( index, s );
3963}
3964
3965/*!
3966 \fn QString& QString::insert( uint index, char c )
3967
3968 \overload
3969
3970 Insert character \a c at position \a index.
3971*/
3972
3973/*!
3974 \fn QString &QString::prepend( const QString &s )
3975
3976 Inserts \a s at the beginning of the string and returns a
3977 reference to the string.
3978
3979 Equivalent to insert(0, \a s).
3980
3981 \code
3982 QString string = "42";
3983 string.prepend( "The answer is " );
3984 // string == "The answer is 42"
3985 \endcode
3986
3987 \sa insert()
3988*/
3989
3990/*!
3991 \fn QString& QString::prepend( char ch )
3992
3993 \overload
3994
3995 Inserts \a ch at the beginning of the string and returns a
3996 reference to the string.
3997
3998 Equivalent to insert(0, \a ch).
3999
4000 \sa insert()
4001*/
4002
4003/*!
4004 \fn QString& QString::prepend( QChar ch )
4005
4006 \overload
4007
4008 Inserts \a ch at the beginning of the string and returns a
4009 reference to the string.
4010
4011 Equivalent to insert(0, \a ch).
4012
4013 \sa insert()
4014*/
4015
4016/*! \fn QString& QString::prepend( const QByteArray &s )
4017 \overload
4018
4019 Inserts \a s at the beginning of the string and returns a reference to the string.
4020
4021 Equivalent to insert(0, \a s).
4022
4023 \sa insert()
4024 */
4025
4026/*! \fn QString& QString::prepend( const std::string &s )
4027 \overload
4028
4029 Inserts \a s at the beginning of the string and returns a reference to the string.
4030
4031 Equivalent to insert(0, \a s).
4032
4033 \sa insert()
4034*/
4035
4036/*!
4037 \overload
4038
4039 Inserts \a s at the beginning of the string and returns a reference to the string.
4040
4041 Equivalent to insert(0, \a s).
4042
4043 \sa insert()
4044 */
4045QString &QString::prepend( const char *s )
4046{
4047 return insert( 0, QString(s) );
4048}
4049
4050/*!
4051 Removes \a len characters from the string starting at position \a
4052 index, and returns a reference to the string.
4053
4054 If \a index is beyond the length of the string, nothing happens.
4055 If \a index is within the string, but \a index + \a len is beyond
4056 the end of the string, the string is truncated at position \a
4057 index.
4058
4059 \code
4060 QString string( "Montreal" );
4061 string.remove( 1, 4 ); // string == "Meal"
4062 \endcode
4063
4064 \sa insert(), replace()
4065*/
4066
4067QString &QString::remove( uint index, uint len )
4068{
4069 uint olen = length();
4070 if ( index >= olen ) {
4071 // range problems
4072 } else if ( index + len >= olen ) { // index ok
4073 setLength( index );
4074 } else if ( len != 0 ) {
4075 real_detach();
4076 memmove( d->unicode+index, d->unicode+index+len,
4077 sizeof(QChar)*(olen-index-len) );
4078 setLength( olen-len );
4079 }
4080 return *this;
4081}
4082
4083/*! \overload
4084
4085 Removes every occurrence of the character \a c in the string.
4086 Returns a reference to the string.
4087
4088 This is the same as replace(\a c, "").
4089*/
4090QString &QString::remove( QChar c )
4091{
4092 int i = 0;
4093 while ( i < (int) length() ) {
4094 if ( constref(i) == c ) {
4095 remove( i, 1 );
4096 } else {
4097 i++;
4098 }
4099 }
4100 return *this;
4101}
4102
4103/*! \overload
4104
4105 \fn QString &QString::remove( char c )
4106
4107 Removes every occurrence of the character \a c in the string.
4108 Returns a reference to the string.
4109
4110 This is the same as replace(\a c, "").
4111*/
4112
4113/*! \overload
4114
4115 Removes every occurrence of \a str in the string. Returns a
4116 reference to the string.
4117
4118 If \a cs is TRUE (the default), the search is case sensitive;
4119 otherwise the search is case insensitive.
4120
4121 This is the same as replace(\a str, "", \a cs).
4122*/
4123QString &QString::remove( const QString & str, bool cs )
4124{
4125 if ( str.isEmpty() ) {
4126 if ( isNull() )
4127 real_detach();
4128 } else {
4129 int index = 0;
4130 while ( (index = find(str, index, cs)) != -1 )
4131 remove( index, str.length() );
4132 }
4133 return *this;
4134}
4135
4136QString &QString::remove( const QString & str )
4137{
4138 return remove( str, TRUE );
4139}
4140
4141/*! \overload
4142
4143 Replaces every occurrence of \a c1 with the char \a c2. Returns a
4144 reference to the string.
4145*/
4146QString &QString::replace( QChar c1, QChar c2 )
4147{
4148 if ( isEmpty() )
4149 return *this;
4150
4151 real_detach();
4152 uint i = 0;
4153 while ( i < d->len ) {
4154 if ( d->unicode[i] == c1 )
4155 d->unicode[i] = c2;
4156 i++;
4157 }
4158 return *this;
4159}
4160
4161#ifndef QT_NO_REGEXP_CAPTURE
4162
4163/*! \overload
4164
4165 Removes every occurrence of the regular expression \a rx in the
4166 string. Returns a reference to the string.
4167
4168 This is the same as replace(\a rx, "").
4169*/
4170
4171QString &QString::remove( const QRegExp & rx )
4172{
4173 return replace( rx, QString::null );
4174}
4175
4176#endif
4177
4178/*!
4179 \overload
4180
4181 Removes every occurrence of \a str in the string. Returns a
4182 reference to the string.
4183*/
4184QString &QString::remove( const char *str )
4185{
4186 return remove( QString::fromAscii(str), TRUE );
4187}
4188
4189/*!
4190 Replaces \a len characters from the string with \a s, starting at
4191 position \a index, and returns a reference to the string.
4192
4193 If \a index is beyond the length of the string, nothing is deleted
4194 and \a s is appended at the end of the string. If \a index is
4195 valid, but \a index + \a len is beyond the end of the string,
4196 the string is truncated at position \a index, then \a s is
4197 appended at the end.
4198
4199 \code
4200 QString string( "Say yes!" );
4201 string = string.replace( 4, 3, "NO" );
4202 // string == "Say NO!"
4203 \endcode
4204
4205 \sa insert(), remove()
4206*/
4207
4208QString &QString::replace( uint index, uint len, const QString &s )
4209{
4210 return replace( index, len, s.unicode(), s.length() );
4211}
4212
4213/*! \overload
4214
4215 This is the same as replace(\a index, \a len, QString(\a c)).
4216*/
4217QString &QString::replace( uint index, uint len, QChar c )
4218{
4219 return replace( index, len, &c, 1 );
4220}
4221
4222/*! \overload
4223 \fn QString &QString::replace( uint index, uint len, char c )
4224
4225 This is the same as replace(\a index, \a len, QChar(\a c)).
4226*/
4227
4228/*!
4229 \overload
4230
4231 Replaces \a len characters with \a slen characters of QChar data
4232 from \a s, starting at position \a index, and returns a reference
4233 to the string.
4234
4235 \sa insert(), remove()
4236*/
4237
4238QString &QString::replace( uint index, uint len, const QChar* s, uint slen )
4239{
4240 real_detach();
4241 if ( len == slen && index + len <= length() ) {
4242 // Optimized common case: replace without size change
4243 memcpy( d->unicode+index, s, len * sizeof(QChar) );
4244 } else if ( s >= d->unicode && (uint)(s - d->unicode) < d->maxl ) {
4245 // Part of me - take a copy.
4246 QChar *tmp = QT_ALLOC_QCHAR_VEC( slen );
4247 memcpy( tmp, s, slen * sizeof(QChar) );
4248 replace( index, len, tmp, slen );
4249 QT_DELETE_QCHAR_VEC( tmp );
4250 } else {
4251 remove( index, len );
4252 insert( index, s, slen );
4253 }
4254 return *this;
4255}
4256
4257/*! \overload
4258
4259 Replaces every occurrence of the character \a c in the string
4260 with \a after. Returns a reference to the string.
4261
4262 If \a cs is TRUE (the default), the search is case sensitive;
4263 otherwise the search is case insensitive.
4264
4265 Example:
4266 \code
4267 QString s = "a,b,c";
4268 s.replace( QChar(','), " or " );
4269 // s == "a or b or c"
4270 \endcode
4271*/
4272QString &QString::replace( QChar c, const QString & after, bool cs )
4273{
4274 return replace( QString( c ), after, cs );
4275}
4276
4277QString &QString::replace( QChar c, const QString & after )
4278{
4279 return replace( QString( c ), after, TRUE );
4280}
4281
4282/*! \overload
4283 \fn QString &QString::replace( char c, const QString & after, bool cs )
4284
4285 Replaces every occurrence of the character \a c in the string
4286 with \a after. Returns a reference to the string.
4287
4288 If \a cs is TRUE (the default), the search is case sensitive;
4289 otherwise the search is case insensitive.
4290*/
4291
4292/*! \overload
4293
4294 Replaces every occurrence of the string \a before in the string
4295 with the string \a after. Returns a reference to the string.
4296
4297 If \a cs is TRUE (the default), the search is case sensitive;
4298 otherwise the search is case insensitive.
4299
4300 Example:
4301 \code
4302 QString s = "Greek is Greek";
4303 s.replace( "Greek", "English" );
4304 // s == "English is English"
4305 \endcode
4306*/
4307QString &QString::replace( const QString & before, const QString & after,
4308 bool cs )
4309{
4310 if ( isEmpty() ) {
4311 if ( !before.isEmpty() )
4312 return *this;
4313 } else {
4314 if ( cs && before == after )
4315 return *this;
4316 }
4317
4318 real_detach();
4319
4320 int index = 0;
4321 uint skiptable[256];
4322 bm_init_skiptable( before, skiptable, cs );
4323 const int bl = before.length();
4324 const int al = after.length();
4325
4326 if ( bl == al ) {
4327 if ( bl ) {
4328 const QChar *auc = after.unicode();
4329 while ( (index = bm_find(*this, index, before, skiptable, cs) ) != -1 ) {
4330 memcpy( d->unicode + index, auc, al * sizeof(QChar) );
4331 index += bl;
4332 }
4333 }
4334 } else if ( al < bl ) {
4335 const QChar *auc = after.unicode();
4336 uint to = 0;
4337 uint movestart = 0;
4338 uint num = 0;
4339 while ( (index = bm_find(*this, index, before, skiptable, cs)) != -1 ) {
4340 if ( num ) {
4341 int msize = index - movestart;
4342 if ( msize > 0 ) {
4343 memmove( d->unicode + to, d->unicode + movestart, msize*sizeof(QChar) );
4344 to += msize;
4345 }
4346 } else {
4347 to = index;
4348 }
4349 if ( al ) {
4350 memcpy( d->unicode+to, auc, al*sizeof(QChar) );
4351 to += al;
4352 }
4353 index += bl;
4354 movestart = index;
4355 num++;
4356 }
4357 if ( num ) {
4358 int msize = d->len - movestart;
4359 if ( msize > 0 )
4360 memmove( d->unicode + to, d->unicode + movestart, msize*sizeof(QChar) );
4361 setLength( d->len - num*(bl-al) );
4362 }
4363 } else {
4364 // the most complex case. We don't want to loose performance by doing repeated
4365 // copies and reallocs of the string.
4366 while ( index != -1 ) {
4367 uint indices[4096];
4368 uint pos = 0;
4369 while ( pos < 4095 ) {
4370 index = bm_find( *this, index, before, skiptable, cs );
4371 if ( index == -1 )
4372 break;
4373 indices[pos++] = index;
4374 index += bl;
4375 // avoid infinite loop
4376 if ( !bl )
4377 index++;
4378 }
4379 if ( !pos )
4380 break;
4381
4382 // we have a table of replacement positions, use them for fast replacing
4383 int adjust = pos*(al-bl);
4384 // index has to be adjusted in case we get back into the loop above.
4385 if ( index != -1 )
4386 index += adjust;
4387 uint newlen = d->len + adjust;
4388 int moveend = d->len;
4389 if ( newlen > d->len )
4390 setLength( newlen );
4391
4392 while ( pos ) {
4393 pos--;
4394 int movestart = indices[pos] + bl;
4395 int insertstart = indices[pos] + pos*(al-bl);
4396 int moveto = insertstart + al;
4397 memmove( d->unicode + moveto, d->unicode + movestart, (moveend - movestart)*sizeof(QChar) );
4398 memcpy( d->unicode + insertstart, after.unicode(), al*sizeof(QChar) );
4399 moveend = movestart-bl;
4400 }
4401 }
4402 }
4403 return *this;
4404}
4405
4406QString &QString::replace( const QString & before, const QString & after )
4407{
4408 return replace( before, after, TRUE );
4409}
4410
4411#ifndef QT_NO_REGEXP_CAPTURE
4412/*! \overload
4413
4414 Replaces every occurrence of the regexp \a rx in the string with
4415 \a after. Returns a reference to the string. For example:
4416 \code
4417 QString s = "banana";
4418 s.replace( QRegExp("an"), "" );
4419 // s == "ba"
4420 \endcode
4421
4422 For regexps containing \link qregexp.html#capturing-text capturing
4423 parentheses \endlink, occurrences of <b>\\1</b>, <b>\\2</b>, ...,
4424 in \a after are replaced with \a{rx}.cap(1), cap(2), ...
4425
4426 \code
4427 QString t = "A <i>bon mot</i>.";
4428 t.replace( QRegExp("<i>([^<]*)</i>"), "\\emph{\\1}" );
4429 // t == "A \\emph{bon mot}."
4430 \endcode
4431
4432 \sa find(), findRev(), QRegExp::cap()
4433*/
4434
4435QString &QString::replace( const QRegExp &rx, const QString &after )
4436{
4437 QRegExp rx2 = rx;
4438
4439 if ( isEmpty() && rx2.search(*this) == -1 )
4440 return *this;
4441
4442 real_detach();
4443
4444 int index = 0;
4445 int numCaptures = rx2.numCaptures();
4446 int al = after.length();
4447 QRegExp::CaretMode caretMode = QRegExp::CaretAtZero;
4448
4449 if ( numCaptures > 0 ) {
4450 if ( numCaptures > 9 )
4451 numCaptures = 9;
4452
4453 const QChar *uc = after.unicode();
4454 int numBackRefs = 0;
4455
4456 for ( int i = 0; i < al - 1; i++ ) {
4457 if ( uc[i] == '\\' ) {
4458 int no = uc[i + 1].digitValue();
4459 if ( no > 0 && no <= numCaptures )
4460 numBackRefs++;
4461 }
4462 }
4463
4464 /*
4465 This is the harder case where we have back-references.
4466 We don't try to optimize it.
4467 */
4468 if ( numBackRefs > 0 ) {
4469 int *capturePositions = new int[numBackRefs];
4470 int *captureNumbers = new int[numBackRefs];
4471 int j = 0;
4472
4473 for ( int i = 0; i < al - 1; i++ ) {
4474 if ( uc[i] == '\\' ) {
4475 int no = uc[i + 1].digitValue();
4476 if ( no > 0 && no <= numCaptures ) {
4477 capturePositions[j] = i;
4478 captureNumbers[j] = no;
4479 j++;
4480 }
4481 }
4482 }
4483
4484 while ( index <= (int)length() ) {
4485 index = rx2.search( *this, index, caretMode );
4486 if ( index == -1 )
4487 break;
4488
4489 QString after2 = after;
4490 for ( j = numBackRefs - 1; j >= 0; j-- )
4491 after2.replace( capturePositions[j], 2,
4492 rx2.cap(captureNumbers[j]) );
4493
4494 replace( index, rx2.matchedLength(), after2 );
4495 index += after2.length();
4496
4497 if ( rx2.matchedLength() == 0 ) {
4498 // avoid infinite loop on 0-length matches (e.g., [a-z]*)
4499 index++;
4500 }
4501 caretMode = QRegExp::CaretWontMatch;
4502 }
4503 delete[] capturePositions;
4504 delete[] captureNumbers;
4505 return *this;
4506 }
4507 }
4508
4509 /*
4510 This is the simple and optimized case where we don't have
4511 back-references.
4512 */
4513 while ( index != -1 ) {
4514 struct {
4515 int pos;
4516 int length;
4517 } replacements[2048];
4518
4519 uint pos = 0;
4520 int adjust = 0;
4521 while ( pos < 2047 ) {
4522 index = rx2.search( *this, index, caretMode );
4523 if ( index == -1 )
4524 break;
4525 int ml = rx2.matchedLength();
4526 replacements[pos].pos = index;
4527 replacements[pos++].length = ml;
4528 index += ml;
4529 adjust += al - ml;
4530 // avoid infinite loop
4531 if ( !ml )
4532 index++;
4533 }
4534 if ( !pos )
4535 break;
4536 replacements[pos].pos = d->len;
4537 uint newlen = d->len + adjust;
4538
4539 // to continue searching at the right position after we did
4540 // the first round of replacements
4541 if ( index != -1 )
4542 index += adjust;
4543 QChar *newuc = QT_ALLOC_QCHAR_VEC( newlen + 1 );
4544 QChar *uc = newuc;
4545 int copystart = 0;
4546 uint i = 0;
4547 while ( i < pos ) {
4548 int copyend = replacements[i].pos;
4549 int size = copyend - copystart;
4550 memcpy( uc, d->unicode + copystart, size * sizeof(QChar) );
4551 uc += size;
4552 memcpy( uc, after.unicode(), al * sizeof(QChar) );
4553 uc += al;
4554 copystart = copyend + replacements[i].length;
4555 i++;
4556 }
4557 memcpy( uc, d->unicode + copystart,
4558 (d->len - copystart) * sizeof(QChar) );
4559 QT_DELETE_QCHAR_VEC( d->unicode );
4560 d->unicode = newuc;
4561 d->len = newlen;
4562 d->maxl = newlen + 1;
4563 d->setDirty();
4564 caretMode = QRegExp::CaretWontMatch;
4565 }
4566 return *this;
4567}
4568#endif
4569
4570#ifndef QT_NO_REGEXP
4571/*!
4572 Finds the first match of the regular expression \a rx, starting
4573 from position \a index. If \a index is -1, the search starts at
4574 the last character; if -2, at the next to last character and so
4575 on. (See findRev() for searching backwards.)
4576
4577 Returns the position of the first match of \a rx or -1 if no match
4578 was found.
4579
4580 \code
4581 QString string( "bananas" );
4582 int i = string.find( QRegExp("an"), 0 ); // i == 1
4583 \endcode
4584
4585 \sa findRev() replace() contains()
4586*/
4587
4588int QString::find( const QRegExp &rx, int index ) const
4589{
4590 return rx.search( *this, index );
4591}
4592
4593/*!
4594 \overload
4595
4596 Finds the first match of the regexp \a rx, starting at position \a
4597 index and searching backwards. If the index is -1, the search
4598 starts at the last character, if it is -2, at the next to last
4599 character and so on. (See findRev() for searching backwards.)
4600
4601 Returns the position of the match or -1 if no match was found.
4602
4603 \code
4604 QString string( "bananas" );
4605 int i = string.findRev( QRegExp("an") ); // i == 3
4606 \endcode
4607
4608 \sa find()
4609*/
4610
4611int QString::findRev( const QRegExp &rx, int index ) const
4612{
4613 return rx.searchRev( *this, index );
4614}
4615
4616/*!
4617 \overload
4618
4619 Returns the number of times the regexp, \a rx, matches in the
4620 string.
4621
4622 This function counts overlapping matches, so in the example below,
4623 there are four instances of "ana" or "ama".
4624
4625 \code
4626 QString str = "banana and panama";
4627 QRegExp rxp = QRegExp( "a[nm]a", TRUE, FALSE );
4628 int i = str.contains( rxp ); // i == 4
4629 \endcode
4630
4631 \sa find() findRev()
4632*/
4633
4634int QString::contains( const QRegExp &rx ) const
4635{
4636 int count = 0;
4637 int index = -1;
4638 int len = length();
4639 while ( index < len - 1 ) { // count overlapping matches
4640 index = rx.search( *this, index + 1 );
4641 if ( index == -1 )
4642 break;
4643 count++;
4644 }
4645 return count;
4646}
4647
4648#endif //QT_NO_REGEXP
4649
4650/*!
4651 Returns the string converted to a \c long using base \a
4652 base, which is 10 by default and must be between 2 and 36 or 0. If
4653 \a base is 0, the base is determined automatically using the
4654 following rules: If the string begins with "0x", it is assumed to
4655 be hexadecimal; if it begins with "0", it is assumed to be octal;
4656 otherwise it is assumed to be decimal.
4657
4658 Returns 0 if the conversion fails.
4659
4660 If \a ok is not 0: if a conversion error occurs, \a *ok is set to
4661 FALSE; otherwise \a *ok is set to TRUE.
4662
4663 \sa number()
4664*/
4665
4666long QString::toLong( bool *ok, int base ) const
4667{
4668 Q_LLONG v = toLongLong( ok, base );
4669 if ( v < LONG_MIN || v > LONG_MAX ) {
4670 if ( ok )
4671 *ok = FALSE;
4672 v = 0;
4673 }
4674 return long(v);
4675}
4676
4677/*!
4678 Returns the string converted to a \c {long long} using base \a
4679 base, which is 10 by default and must be between 2 and 36 or 0. If
4680 \a base is 0, the base is determined automatically using the
4681 following rules: If the string begins with "0x", it is assumed to
4682 be hexadecimal; if it begins with "0", it is assumed to be octal;
4683 otherwise it is assumed to be decimal.
4684
4685 Returns 0 if the conversion fails.
4686
4687 If \a ok is not 0: if a conversion error occurs, \a *ok is set to
4688 FALSE; otherwise \a *ok is set to TRUE.
4689
4690 \sa number()
4691*/
4692
4693Q_LLONG QString::toLongLong( bool *ok, int base ) const
4694{
4695#if defined(QT_CHECK_RANGE)
4696 if ( base != 0 && (base < 2 || base > 36) ) {
4697 qWarning( "QString::toLongLong: Invalid base (%d)", base );
4698 base = 10;
4699 }
4700#endif
4701
4702 QLocale locale(QLocale::C);
4703 return locale.d->stringToLongLong(*this, base, ok);
4704}
4705
4706/*!
4707 Returns the string converted to an \c {unsigned long} using base \a
4708 base, which is 10 by default and must be between 2 and 36 or 0. If
4709 \a base is 0, the base is determined automatically using the
4710 following rules: If the string begins with "0x", it is assumed to
4711 be hexadecimal; if it begins with "0", it is assumed to be octal;
4712 otherwise it is assumed to be decimal.
4713
4714 Returns 0 if the conversion fails.
4715
4716 If \a ok is not 0: if a conversion error occurs, \a *ok is set to
4717 FALSE; otherwise \a *ok is set to TRUE.
4718
4719 \sa number()
4720*/
4721
4722ulong QString::toULong( bool *ok, int base ) const
4723{
4724 Q_ULLONG v = toULongLong( ok, base );
4725 if ( v > ULONG_MAX ) {
4726 if ( ok )
4727 *ok = FALSE;
4728 v = 0;
4729 }
4730 return ulong(v);
4731}
4732
4733/*!
4734 Returns the string converted to an \c {unsigned long long} using base \a
4735 base, which is 10 by default and must be between 2 and 36 or 0. If
4736 \a base is 0, the base is determined automatically using the
4737 following rules: If the string begins with "0x", it is assumed to
4738 be hexadecimal; if it begins with "0", it is assumed to be octal;
4739 otherwise it is assumed to be decimal.
4740
4741 Returns 0 if the conversion fails.
4742
4743 If \a ok is not 0: if a conversion error occurs, \a *ok is set to
4744 FALSE; otherwise \a *ok is set to TRUE.
4745
4746 \sa number()
4747*/
4748
4749Q_ULLONG QString::toULongLong( bool *ok, int base ) const
4750{
4751#if defined(QT_CHECK_RANGE)
4752 if ( base != 0 && (base < 2 || base > 36) ) {
4753 qWarning( "QString::toULongLong: Invalid base %d", base );
4754 base = 10;
4755 }
4756#endif
4757
4758 QLocale locale(QLocale::C);
4759 return locale.d->stringToUnsLongLong(*this, base, ok);
4760}
4761
4762/*!
4763 Returns the string converted to a \c short using base \a
4764 base, which is 10 by default and must be between 2 and 36 or 0. If
4765 \a base is 0, the base is determined automatically using the
4766 following rules: If the string begins with "0x", it is assumed to
4767 be hexadecimal; if it begins with "0", it is assumed to be octal;
4768 otherwise it is assumed to be decimal.
4769
4770 Returns 0 if the conversion fails.
4771
4772 If \a ok is not 0: if a conversion error occurs, \a *ok is set to
4773 FALSE; otherwise \a *ok is set to TRUE.
4774
4775 \sa number()
4776*/
4777
4778
4779short QString::toShort( bool *ok, int base ) const
4780{
4781 Q_LLONG v = toLongLong( ok, base );
4782 if ( v < SHRT_MIN || v > SHRT_MAX ) {
4783 if ( ok )
4784 *ok = FALSE;
4785 v = 0;
4786 }
4787 return (short)v;
4788}
4789
4790/*!
4791 Returns the string converted to an \c {unsigned short} using base \a
4792 base, which is 10 by default and must be between 2 and 36 or 0. If
4793 \a base is 0, the base is determined automatically using the
4794 following rules: If the string begins with "0x", it is assumed to
4795 be hexadecimal; if it begins with "0", it is assumed to be octal;
4796 otherwise it is assumed to be decimal.
4797
4798 Returns 0 if the conversion fails.
4799
4800 If \a ok is not 0: if a conversion error occurs, \a *ok is set to
4801 FALSE; otherwise \a *ok is set to TRUE.
4802
4803 \sa number()
4804*/
4805
4806ushort QString::toUShort( bool *ok, int base ) const
4807{
4808 Q_ULLONG v = toULongLong( ok, base );
4809 if ( v > USHRT_MAX ) {
4810 if ( ok )
4811 *ok = FALSE;
4812 v = 0;
4813 }
4814 return (ushort)v;
4815}
4816
4817
4818/*!
4819 Returns the string converted to an \c int using base \a
4820 base, which is 10 by default and must be between 2 and 36 or 0. If
4821 \a base is 0, the base is determined automatically using the
4822 following rules: If the string begins with "0x", it is assumed to
4823 be hexadecimal; if it begins with "0", it is assumed to be octal;
4824 otherwise it is assumed to be decimal.
4825
4826 Returns 0 if the conversion fails.
4827
4828 If \a ok is not 0: if a conversion error occurs, \a *ok is set to
4829 FALSE; otherwise \a *ok is set to TRUE.
4830
4831 \code
4832 QString str( "FF" );
4833 bool ok;
4834 int hex = str.toInt( &ok, 16 ); // hex == 255, ok == TRUE
4835 int dec = str.toInt( &ok, 10 ); // dec == 0, ok == FALSE
4836 \endcode
4837
4838 \sa number()
4839*/
4840
4841int QString::toInt( bool *ok, int base ) const
4842{
4843 Q_LLONG v = toLongLong( ok, base );
4844 if ( v < INT_MIN || v > INT_MAX ) {
4845 if ( ok )
4846 *ok = FALSE;
4847 v = 0;
4848 }
4849 return (int)v;
4850}
4851
4852/*!
4853 Returns the string converted to an \c {unsigned int} using base \a
4854 base, which is 10 by default and must be between 2 and 36 or 0. If
4855 \a base is 0, the base is determined automatically using the
4856 following rules: If the string begins with "0x", it is assumed to
4857 be hexadecimal; if it begins with "0", it is assumed to be octal;
4858 otherwise it is assumed to be decimal.
4859
4860 Returns 0 if the conversion fails.
4861
4862 If \a ok is not 0: if a conversion error occurs, \a *ok is set to
4863 FALSE; otherwise \a *ok is set to TRUE.
4864
4865 \sa number()
4866*/
4867
4868uint QString::toUInt( bool *ok, int base ) const
4869{
4870 Q_ULLONG v = toULongLong( ok, base );
4871 if ( v > UINT_MAX ) {
4872 if ( ok )
4873 *ok = FALSE;
4874 v = 0;
4875 }
4876 return (uint)v;
4877}
4878
4879/*!
4880 Returns the string converted to a \c double value.
4881
4882 Returns 0.0 if the conversion fails.
4883
4884 If \a ok is not 0: if a conversion error occurs, \a *ok is set to
4885 FALSE; otherwise \a *ok is set to TRUE.
4886
4887 \code
4888 QString string( "1234.56" );
4889 double a = string.toDouble(); // a == 1234.56
4890 \endcode
4891
4892 \sa number()
4893*/
4894
4895double QString::toDouble( bool *ok ) const
4896{
4897 QLocale locale(QLocale::C);
4898 return locale.d->stringToDouble(*this, ok);
4899}
4900
4901/*!
4902 Returns the string converted to a \c float value.
4903
4904 Returns 0.0 if the conversion fails.
4905
4906 If \a ok is not 0: if a conversion error occurs, \a *ok is set to
4907 FALSE; otherwise \a *ok is set to TRUE.
4908
4909 \sa number()
4910*/
4911
4912float QString::toFloat( bool *ok ) const
4913{
4914 return (float)toDouble( ok );
4915}
4916
4917/*!
4918 Sets the string to the printed value of \a n in base \a base and
4919 returns a reference to the string.
4920
4921 The base is 10 by default and must be between 2 and 36.
4922
4923 \code
4924 QString string;
4925 string = string.setNum( 1234 ); // string == "1234"
4926 \endcode
4927*/
4928
4929QString &QString::setNum( Q_LLONG n, int base )
4930{
4931#if defined(QT_CHECK_RANGE)
4932 if ( base < 2 || base > 36 ) {
4933 qWarning( "QString::setNum: Invalid base %d", base );
4934 base = 10;
4935 }
4936#endif
4937 QLocale locale(QLocale::C);
4938 *this = locale.d->longLongToString(n, -1, base);
4939 return *this;
4940}
4941
4942/*!
4943 \overload
4944
4945 Sets the string to the printed value of \a n in base \a base and
4946 returns a reference to the string.
4947
4948 The base is 10 by default and must be between 2 and 36.
4949*/
4950
4951QString &QString::setNum( Q_ULLONG n, int base )
4952{
4953#if defined(QT_CHECK_RANGE)
4954 if ( base < 2 || base > 36 ) {
4955 qWarning( "QString::setNum: Invalid base %d", base );
4956 base = 10;
4957 }
4958#endif
4959 QLocale locale(QLocale::C);
4960 *this = locale.d->unsLongLongToString(n, -1, base);
4961 return *this;
4962}
4963
4964/*!
4965 \fn QString &QString::setNum( long n, int base )
4966
4967 \overload
4968*/
4969// ### 4.0: inline
4970QString &QString::setNum( long n, int base )
4971{
4972 return setNum( (Q_LLONG)n, base );
4973}
4974
4975/*!
4976 \fn QString &QString::setNum( ulong n, int base )
4977
4978 \overload
4979*/
4980// ### 4.0: inline
4981QString &QString::setNum( ulong n, int base )
4982{
4983 return setNum( (Q_ULLONG)n, base );
4984}
4985
4986/*!
4987 \fn QString &QString::setNum( int n, int base )
4988
4989 \overload
4990
4991 Sets the string to the printed value of \a n in base \a base and
4992 returns a reference to the string.
4993
4994 The base is 10 by default and must be between 2 and 36.
4995*/
4996
4997/*!
4998 \fn QString &QString::setNum( uint n, int base )
4999
5000 \overload
5001
5002 Sets the string to the printed value of \a n in base \a base and
5003 returns a reference to the string.
5004
5005 The base is 10 by default and must be between 2 and 36.
5006*/
5007
5008/*!
5009 \fn QString &QString::setNum( short n, int base )
5010
5011 \overload
5012
5013 Sets the string to the printed value of \a n in base \a base and
5014 returns a reference to the string.
5015
5016 The base is 10 by default and must be between 2 and 36.
5017*/
5018
5019/*!
5020 \fn QString &QString::setNum( ushort n, int base )
5021
5022 \overload
5023
5024 Sets the string to the printed value of \a n in base \a base and
5025 returns a reference to the string.
5026
5027 The base is 10 by default and must be between 2 and 36.
5028*/
5029
5030/*!
5031 \overload
5032
5033 Sets the string to the printed value of \a n, formatted in format
5034 \a f with precision \a prec, and returns a reference to the
5035 string.
5036
5037 The format \a f can be 'f', 'F', 'e', 'E', 'g' or 'G'. See \link
5038 #arg-formats arg \endlink() for an explanation of the formats.
5039*/
5040
5041QString &QString::setNum( double n, char f, int prec )
5042{
5043 QLocalePrivate::DoubleForm form = QLocalePrivate::DFDecimal;
5044 uint flags = 0;
5045
5046 if (qIsUpper(f))
5047 flags = QLocalePrivate::CapitalEorX;
5048 f = qToLower(f);
5049
5050 switch (f) {
5051 case 'f':
5052 form = QLocalePrivate::DFDecimal;
5053 break;
5054 case 'e':
5055 form = QLocalePrivate::DFExponent;
5056 break;
5057 case 'g':
5058 form = QLocalePrivate::DFSignificantDigits;
5059 break;
5060 default:
5061#if defined(QT_CHECK_RANGE)
5062 qWarning( "QString::setNum: Invalid format char '%c'", f );
5063#endif
5064 break;
5065 }
5066
5067 QLocale locale(QLocale::C);
5068 *this = locale.d->doubleToString(n, prec, form, -1, flags);
5069 return *this;
5070}
5071
5072/*!
5073 \fn QString &QString::setNum( float n, char f, int prec )
5074
5075 \overload
5076
5077 Sets the string to the printed value of \a n, formatted in format
5078 \a f with precision \a prec, and returns a reference to the
5079 string.
5080
5081 The format \a f can be 'f', 'F', 'e', 'E', 'g' or 'G'. See \link
5082 #arg-formats arg \endlink() for an explanation of the formats.
5083*/
5084
5085
5086/*!
5087 A convenience function that returns a string equivalent of the
5088 number \a n to base \a base, which is 10 by default and must be
5089 between 2 and 36.
5090
5091 \code
5092 long a = 63;
5093 QString str = QString::number( a, 16 ); // str == "3f"
5094 QString str = QString::number( a, 16 ).upper(); // str == "3F"
5095 \endcode
5096
5097 \sa setNum()
5098*/
5099QString QString::number( long n, int base )
5100{
5101 QString s;
5102 s.setNum( n, base );
5103 return s;
5104}
5105
5106/*!
5107 \overload
5108
5109 \sa setNum()
5110*/
5111QString QString::number( ulong n, int base )
5112{
5113 QString s;
5114 s.setNum( n, base );
5115 return s;
5116}
5117
5118/*!
5119 \overload
5120
5121 \sa setNum()
5122*/
5123QString QString::number( Q_LLONG n, int base )
5124{
5125 QString s;
5126 s.setNum( n, base );
5127 return s;
5128}
5129
5130/*!
5131 \overload
5132
5133 \sa setNum()
5134*/
5135QString QString::number( Q_ULLONG n, int base )
5136{
5137 QString s;
5138 s.setNum( n, base );
5139 return s;
5140}
5141
5142/*!
5143 \overload
5144
5145 \sa setNum()
5146*/
5147QString QString::number( int n, int base )
5148{
5149 QString s;
5150 s.setNum( n, base );
5151 return s;
5152}
5153
5154/*!
5155 \overload
5156
5157 A convenience factory function that returns a string
5158 representation of the number \a n to the base \a base, which is 10
5159 by default and must be between 2 and 36.
5160
5161 \sa setNum()
5162*/
5163QString QString::number( uint n, int base )
5164{
5165 QString s;
5166 s.setNum( n, base );
5167 return s;
5168}
5169
5170/*!
5171 \overload
5172
5173 Argument \a n is formatted according to the \a f format specified,
5174 which is \c g by default, and can be any of the following:
5175
5176 \table
5177 \header \i Format \i Meaning
5178 \row \i \c e \i format as [-]9.9e[+|-]999
5179 \row \i \c E \i format as [-]9.9E[+|-]999
5180 \row \i \c f \i format as [-]9.9
5181 \row \i \c g \i use \c e or \c f format, whichever is the most concise
5182 \row \i \c G \i use \c E or \c f format, whichever is the most concise
5183 \endtable
5184
5185 With 'e', 'E', and 'f', \a prec is the number of digits after the
5186 decimal point. With 'g' and 'G', \a prec is the maximum number of
5187 significant digits (trailing zeroes are omitted).
5188
5189 \code
5190 double d = 12.34;
5191 QString ds = QString( "'E' format, precision 3, gives %1" )
5192 .arg( d, 0, 'E', 3 );
5193 // ds == "1.234E+001"
5194 \endcode
5195
5196 \sa setNum()
5197 */
5198QString QString::number( double n, char f, int prec )
5199{
5200 QString s;
5201 s.setNum( n, f, prec );
5202 return s;
5203}
5204
5205
5206/*! \obsolete
5207
5208 Sets the character at position \a index to \a c and expands the
5209 string if necessary, filling with spaces.
5210
5211 This method is redundant in Qt 3.x, because operator[] will expand
5212 the string as necessary.
5213*/
5214
5215void QString::setExpand( uint index, QChar c )
5216{
5217 int spaces = index - d->len;
5218 at(index) = c;
5219 while (spaces-->0)
5220 d->unicode[--index]=' ';
5221}
5222
5223
5224/*!
5225 \fn const char* QString::data() const
5226
5227 \obsolete
5228
5229 Returns a pointer to a '\0'-terminated classic C string.
5230
5231 In Qt 1.x, this returned a char* allowing direct manipulation of the
5232 string as a sequence of bytes. In Qt 2.x where QString is a Unicode
5233 string, char* conversion constructs a temporary string, and hence
5234 direct character operations are meaningless.
5235*/
5236
5237/*!
5238 \fn bool QString::operator!() const
5239
5240 Returns TRUE if this is a null string; otherwise returns FALSE.
5241
5242 \code
5243 QString name = getName();
5244 if ( !name )
5245 name = "Rodney";
5246 \endcode
5247
5248 Note that if you say
5249
5250 \code
5251 QString name = getName();
5252 if ( name )
5253 doSomethingWith(name);
5254 \endcode
5255
5256 It will call "operator const char*()", which is inefficent; you
5257 may wish to define the macro \c QT_NO_ASCII_CAST when writing code
5258 which you wish to remain Unicode-clean.
5259
5260 When you want the above semantics, use:
5261
5262 \code
5263 QString name = getName();
5264 if ( !name.isNull() )
5265 doSomethingWith(name);
5266 \endcode
5267
5268 \sa isEmpty()
5269*/
5270
5271
5272/*!
5273 \fn QString& QString::append( const QString& str )
5274
5275 Appends \a str to the string and returns a reference to the
5276 result.
5277
5278 \code
5279 string = "Test";
5280 string.append( "ing" ); // string == "Testing"
5281 \endcode
5282
5283 Equivalent to operator+=().
5284*/
5285
5286/*!
5287 \fn QString& QString::append( char ch )
5288
5289 \overload
5290
5291 Appends character \a ch to the string and returns a reference to
5292 the result.
5293
5294 Equivalent to operator+=().
5295*/
5296
5297/*!
5298 \fn QString& QString::append( QChar ch )
5299
5300 \overload
5301
5302 Appends character \a ch to the string and returns a reference to
5303 the result.
5304
5305 Equivalent to operator+=().
5306*/
5307
5308/*! \fn QString& QString::append( const QByteArray &str )
5309 \overload
5310
5311 Appends \a str to the string and returns a reference to the result.
5312
5313 Equivalent to operator+=().
5314*/
5315
5316/*! \fn QString& QString::append( const std::string &str )
5317 \overload
5318
5319 Appends \a str to the string and returns a reference to the result.
5320
5321 Equivalent to operator+=().
5322*/
5323
5324/*! \fn QString& QString::append( const char *str )
5325 \overload
5326
5327 Appends \a str to the string and returns a reference to the result.
5328
5329 Equivalent to operator+=().
5330*/
5331
5332/*!
5333 Appends \a str to the string and returns a reference to the string.
5334*/
5335QString& QString::operator+=( const QString &str )
5336{
5337 uint len1 = length();
5338 uint len2 = str.length();
5339 if ( len2 ) {
5340 if ( isEmpty() ) {
5341 operator=( str );
5342 } else {
5343 grow( len1+len2 );
5344 memcpy( d->unicode+len1, str.unicode(), sizeof(QChar)*len2 );
5345 }
5346 } else if ( isNull() && !str.isNull() ) { // ## just for 1.x compat:
5347 *this = fromLatin1( "" );
5348 }
5349 return *this;
5350}
5351
5352#ifndef QT_NO_CAST_ASCII
5353QString &QString::operatorPlusEqHelper( const char *s, uint len2 )
5354{
5355 if ( s ) {
5356#ifndef QT_NO_TEXTCODEC
5357 if ( QTextCodec::codecForCStrings() )
5358 return operator+=( fromAscii( s, len2 ) );
5359#endif
5360
5361 uint len1 = length();
5362 if ( len2 == UINT_MAX )
5363 len2 = strlen( s );
5364 if ( len2 ) {
5365 grow( len1 + len2 );
5366 QChar* uc = d->unicode + len1;
5367 while ( len2-- )
5368 *uc++ = *s++;
5369 } else if ( isNull() ) { // ## just for 1.x compat:
5370 *this = fromLatin1( "" );
5371 }
5372 }
5373 return *this;
5374}
5375#endif
5376
5377/*!
5378 \overload
5379
5380 Appends \a str to the string and returns a reference to the string.
5381*/
5382#ifndef QT_NO_CAST_ASCII
5383QString& QString::operator+=( const char *str )
5384{
5385 // ### Qt 4: make this function inline
5386 return operatorPlusEqHelper( str );
5387}
5388#endif
5389
5390/*! \overload
5391
5392 Appends \a c to the string and returns a reference to the string.
5393*/
5394
5395QString &QString::operator+=( QChar c )
5396{
5397 grow( length()+1 );
5398 d->unicode[length()-1] = c;
5399 return *this;
5400}
5401
5402/*!
5403 \overload
5404
5405 Appends \a c to the string and returns a reference to the string.
5406*/
5407
5408QString &QString::operator+=( char c )
5409{
5410#ifndef QT_NO_TEXTCODEC
5411 if ( QTextCodec::codecForCStrings() )
5412 return operator+=( fromAscii( &c, 1 ) );
5413#endif
5414 grow( length()+1 );
5415 d->unicode[length()-1] = c;
5416 return *this;
5417}
5418
5419/*!
5420 \fn QString &QString::operator+=( const QByteArray &str )
5421 \overload
5422
5423 Appends \a str to the string and returns a reference to the string.
5424*/
5425
5426/*!
5427 \fn QString &QString::operator+=( const std::string &str )
5428 \overload
5429
5430 Appends \a str to the string and returns a reference to the string.
5431*/
5432
5433/*!
5434 \fn char QChar::latin1() const
5435
5436 Returns the Latin-1 value of this character, or 0 if it
5437 cannot be represented in Latin-1.
5438*/
5439
5440
5441/*!
5442 Returns a Latin-1 representation of the string. The
5443 returned value is undefined if the string contains non-Latin-1
5444 characters. If you want to convert strings into formats other than
5445 Unicode, see the QTextCodec classes.
5446
5447 This function is mainly useful for boot-strapping legacy code to
5448 use Unicode.
5449
5450 The result remains valid so long as one unmodified copy of the
5451 source string exists.
5452
5453 \sa fromLatin1(), ascii(), utf8(), local8Bit()
5454*/
5455const char* QString::latin1() const
5456{
5457 if ( !d->ascii || !d->islatin1 ) {
5458 delete [] d->ascii;
5459 d->ascii = unicodeToLatin1( d->unicode, d->len );
5460 d->islatin1 = TRUE;
5461 }
5462 return d->ascii;
5463}
5464
5465/*!
5466 Returns an 8-bit ASCII representation of the string.
5467
5468 If a codec has been set using QTextCodec::codecForCStrings(),
5469 it is used to convert Unicode to 8-bit char. Otherwise, this function
5470 does the same as latin1().
5471
5472 \sa fromAscii(), latin1(), utf8(), local8Bit()
5473*/
5474const char* QString::ascii() const
5475{
5476#ifndef QT_NO_TEXTCODEC
5477 if ( QTextCodec::codecForCStrings() ) {
5478 if ( !d->ascii || d->islatin1 ) {
5479 delete [] d->ascii;
5480 if (d->unicode) {
5481 QCString s = QTextCodec::codecForCStrings()->fromUnicode( *this );
5482 s.detach();
5483 d->ascii = s.data();
5484 s.resetRawData( s.data(), s.size() ); // we have stolen the data
5485 } else {
5486 d->ascii = 0;
5487 }
5488 d->islatin1 = FALSE;
5489 }
5490 return d->ascii;
5491 }
5492#endif // QT_NO_TEXTCODEC
5493 return latin1();
5494}
5495
5496/*!
5497 Returns the string encoded in UTF-8 format.
5498
5499 See QTextCodec for more diverse coding/decoding of Unicode strings.
5500
5501 \sa fromUtf8(), ascii(), latin1(), local8Bit()
5502*/
5503QCString QString::utf8() const
5504{
5505 int l = length();
5506 int rlen = l*3+1;
5507 QCString rstr(rlen);
5508 uchar* cursor = (uchar*)rstr.data();
5509 const QChar *ch = d->unicode;
5510 for (int i=0; i < l; i++) {
5511 uint u = ch->unicode();
5512 if ( u < 0x80 ) {
5513 *cursor++ = (uchar)u;
5514 } else {
5515 if ( u < 0x0800 ) {
5516 *cursor++ = 0xc0 | ((uchar) (u >> 6));
5517 } else {
5518 if (u >= 0xd800 && u < 0xdc00 && i < l-1) {
5519 unsigned short low = ch[1].unicode();
5520 if (low >= 0xdc00 && low < 0xe000) {
5521 ++ch;
5522 ++i;
5523 u = (u - 0xd800)*0x400 + (low - 0xdc00) + 0x10000;
5524 }
5525 }
5526 if (u > 0xffff) {
5527 // if people are working in utf8, but strings are encoded in eg. latin1, the resulting
5528 // name might be invalid utf8. This and the corresponding code in fromUtf8 takes care
5529 // we can handle this without loosing information. This can happen with latin filenames
5530 // and a utf8 locale under Unix.
5531 if (u > 0x10fe00 && u < 0x10ff00) {
5532 *cursor++ = (u - 0x10fe00);
5533 ++ch;
5534 continue;
5535 } else {
5536 *cursor++ = 0xf0 | ((uchar) (u >> 18));
5537 *cursor++ = 0x80 | ( ((uchar) (u >> 12)) & 0x3f);
5538 }
5539 } else {
5540 *cursor++ = 0xe0 | ((uchar) (u >> 12));
5541 }
5542 *cursor++ = 0x80 | ( ((uchar) (u >> 6)) & 0x3f);
5543 }
5544 *cursor++ = 0x80 | ((uchar) (u&0x3f));
5545 }
5546 ++ch;
5547 }
5548 rstr.truncate( cursor - (uchar*)rstr.data() );
5549 return rstr;
5550}
5551
5552/*!
5553 Returns the Unicode string decoded from the first \a len
5554 bytes of \a utf8, ignoring the rest of \a utf8. If \a len is
5555 -1 then the length of \a utf8 is used. If \a len is bigger than
5556 the length of \a utf8 then it will use the length of \a utf8.
5557
5558 \code
5559 QString str = QString::fromUtf8( "123456789", 5 );
5560 // str == "12345"
5561 \endcode
5562
5563 See QTextCodec for more diverse coding/decoding of Unicode strings.
5564*/
5565QString QString::fromUtf8( const char* utf8, int len )
5566{
5567 if ( !utf8 )
5568 return QString::null;
5569
5570 if ( len < 0 )
5571 len = strlen( utf8 );
5572 QString result;
5573 result.setLength( len*2 ); // worst case
5574 QChar *qch = (QChar *)result.unicode();
5575 uint uc = 0;
5576 int need = 0;
5577 int error = -1;
5578 uchar ch;
5579 for (int i=0; i<len; i++) {
5580 ch = utf8[i];
5581 if (need) {
5582 if ( (ch&0xc0) == 0x80 ) {
5583 uc = (uc << 6) | (ch & 0x3f);
5584 need--;
5585 if ( !need ) {
5586 if (uc > 0xffff) {
5587 // surrogate pair
5588 uc -= 0x10000;
5589 unsigned short high = uc/0x400 + 0xd800;
5590 unsigned short low = uc%0x400 + 0xdc00;
5591 *qch++ = QChar(high);
5592 *qch++ = QChar(low);
5593 } else {
5594 *qch++ = uc;
5595 }
5596 }
5597 } else {
5598 // See QString::utf8() for explanation.
5599 //
5600 // The surrogate below corresponds to a Unicode value of (0x10fe00+ch) which
5601 // is in one of the private use areas of Unicode.
5602 i = error;
5603 *qch++ = QChar(0xdbff);
5604 *qch++ = QChar(0xde00+((uchar)utf8[i]));
5605 need = 0;
5606 }
5607 } else {
5608 if ( ch < 128 ) {
5609 *qch++ = ch;
5610 } else if ((ch & 0xe0) == 0xc0) {
5611 uc = ch & 0x1f;
5612 need = 1;
5613 error = i;
5614 } else if ((ch & 0xf0) == 0xe0) {
5615 uc = ch & 0x0f;
5616 need = 2;
5617 error = i;
5618 } else if ((ch&0xf8) == 0xf0) {
5619 uc = ch & 0x07;
5620 need = 3;
5621 error = i;
5622 } else {
5623 // Error
5624 *qch++ = QChar(0xdbff);
5625 *qch++ = QChar(0xde00+((uchar)utf8[i]));
5626 }
5627 }
5628 }
5629 if (need) {
5630 // we have some invalid characters remaining we need to add to the string
5631 for (int i = error; i < len; ++i) {
5632 *qch++ = QChar(0xdbff);
5633 *qch++ = QChar(0xde00+((uchar)utf8[i]));
5634 }
5635 }
5636
5637 result.truncate( qch - result.unicode() );
5638 return result;
5639}
5640
5641/*!
5642 Returns the Unicode string decoded from the first \a len
5643 bytes of \a ascii, ignoring the rest of \a ascii. If \a len
5644 is -1 then the length of \a ascii is used. If \a len is bigger
5645 than the length of \a ascii then it will use the length of \a
5646 ascii.
5647
5648 If a codec has been set using QTextCodec::codecForCStrings(),
5649 it is used to convert Unicode to 8-bit char. Otherwise, this function
5650 does the same as fromLatin1().
5651
5652 This is the same as the QString(const char*) constructor, but you
5653 can make that constructor invisible if you compile with the define
5654 \c QT_NO_CAST_ASCII, in which case you can explicitly create a
5655 QString from 8-bit ASCII text using this function.
5656
5657 \code
5658 QString str = QString::fromAscii( "123456789", 5 );
5659 // str == "12345"
5660 \endcode
5661 */
5662QString QString::fromAscii( const char* ascii, int len )
5663{
5664#ifndef QT_NO_TEXTCODEC
5665 if ( QTextCodec::codecForCStrings() ) {
5666 if ( !ascii )
5667 return QString::null;
5668 if ( len < 0 )
5669 len = strlen( ascii );
5670 if ( len == 0 || *ascii == '\0' )
5671 return QString::fromLatin1( "" );
5672 return QTextCodec::codecForCStrings()->toUnicode( ascii, len );
5673 }
5674#endif
5675 return fromLatin1( ascii, len );
5676}
5677
5678
5679/*!
5680 Returns the Unicode string decoded from the first \a len
5681 bytes of \a chars, ignoring the rest of \a chars. If \a len
5682 is -1 then the length of \a chars is used. If \a len is bigger
5683 than the length of \a chars then it will use the length of \a
5684 chars.
5685
5686 \sa fromAscii()
5687*/
5688QString QString::fromLatin1( const char* chars, int len )
5689{
5690 uint l;
5691 QChar *uc;
5692 if ( len < 0 )
5693 len = -1;
5694 uc = internalLatin1ToUnicode( chars, &l, len );
5695 return QString( new QStringData(uc, l, l), TRUE );
5696}
5697
5698/*!
5699 \fn const QChar* QString::unicode() const
5700
5701 Returns the Unicode representation of the string. The result
5702 remains valid until the string is modified.
5703*/
5704
5705/*!
5706 Returns the string encoded in a locale-specific format. On X11,
5707 this is the QTextCodec::codecForLocale(). On Windows and OS/2, it is a
5708 system-defined encoding. On Mac OS X, this always uses UTF-8 as
5709 the encoding.
5710
5711 See QTextCodec for more diverse coding/decoding of Unicode
5712 strings.
5713
5714 \sa fromLocal8Bit(), ascii(), latin1(), utf8()
5715*/
5716
5717QCString QString::local8Bit() const
5718{
5719#ifdef QT_NO_TEXTCODEC
5720 return latin1();
5721#else
5722#ifdef Q_WS_X11
5723 QTextCodec* codec = QTextCodec::codecForLocale();
5724 return codec
5725 ? codec->fromUnicode(*this)
5726 : QCString(latin1());
5727#endif
5728#if defined( Q_WS_MACX )
5729 return utf8();
5730#endif
5731#if defined( Q_WS_MAC9 )
5732 return QCString(latin1()); //I'm evil..
5733#endif
5734#ifdef Q_WS_WIN
5735 return isNull() ? QCString("") : qt_winQString2MB( *this );
5736#endif
5737#if defined(Q_OS_OS2)
5738 return isNull() ? QCString("") : qt_os2QString2MB( *this );
5739#endif
5740#ifdef Q_WS_QWS
5741 return utf8(); // ### if there is any 8 bit format supported?
5742#endif
5743#endif
5744}
5745
5746/*!
5747 Returns the Unicode string decoded from the first \a len
5748 bytes of \a local8Bit, ignoring the rest of \a local8Bit. If
5749 \a len is -1 then the length of \a local8Bit is used. If \a len is
5750 bigger than the length of \a local8Bit then it will use the length
5751 of \a local8Bit.
5752
5753 \code
5754 QString str = QString::fromLocal8Bit( "123456789", 5 );
5755 // str == "12345"
5756 \endcode
5757
5758 \a local8Bit is assumed to be encoded in a locale-specific format.
5759
5760 See QTextCodec for more diverse coding/decoding of Unicode strings.
5761*/
5762QString QString::fromLocal8Bit( const char* local8Bit, int len )
5763{
5764#ifdef QT_NO_TEXTCODEC
5765 return fromLatin1( local8Bit, len );
5766#else
5767
5768 if ( !local8Bit )
5769 return QString::null;
5770#ifdef Q_WS_X11
5771 QTextCodec* codec = QTextCodec::codecForLocale();
5772 if ( len < 0 )
5773 len = strlen( local8Bit );
5774 return codec
5775 ? codec->toUnicode( local8Bit, len )
5776 : fromLatin1( local8Bit, len );
5777#endif
5778#if defined( Q_WS_MAC )
5779 return fromUtf8(local8Bit,len);
5780#endif
5781// Should this be OS_WIN32?
5782#ifdef Q_WS_WIN
5783 if ( len >= 0 ) {
5784 QCString s(local8Bit,len+1);
5785 return qt_winMB2QString(s);
5786 }
5787 return qt_winMB2QString( local8Bit );
5788#endif
5789#if defined(Q_OS_OS2)
5790 return qt_os2MB2QString( local8Bit, len );
5791#endif
5792#ifdef Q_WS_QWS
5793 return fromUtf8(local8Bit,len);
5794#endif
5795#endif // QT_NO_TEXTCODEC
5796}
5797
5798/*!
5799 \fn QString::operator const char *() const
5800
5801 Returns latin1(). Be sure to see the warnings documented in the
5802 latin1() function. Note that for new code which you wish to be
5803 strictly Unicode-clean, you can define the macro \c
5804 QT_NO_ASCII_CAST when compiling your code to hide this function so
5805 that automatic casts are not done. This has the added advantage
5806 that you catch the programming error described in operator!().
5807*/
5808
5809/*!
5810 \fn QString::operator std::string() const
5811
5812 Returns ascii().
5813*/
5814
5815/*!
5816 Returns the QString as a zero terminated array of unsigned shorts
5817 if the string is not null; otherwise returns zero.
5818
5819 The result remains valid so long as one unmodified
5820 copy of the source string exists.
5821*/
5822const unsigned short *QString::ucs2() const
5823{
5824 if ( ! d->unicode )
5825 return 0;
5826 unsigned int len = d->len;
5827 if ( d->maxl < len + 1 ) {
5828 // detach, grow or shrink
5829 uint newMax = computeNewMax( len + 1 );
5830 QChar* nd = QT_ALLOC_QCHAR_VEC( newMax );
5831 if ( nd ) {
5832 if ( d->unicode )
5833 memcpy( nd, d->unicode, sizeof(QChar)*len );
5834 ((QString *)this)->deref();
5835 ((QString *)this)->d = new QStringData( nd, len, newMax );
5836 }
5837 }
5838 d->unicode[len] = 0;
5839 return (unsigned short *) d->unicode;
5840}
5841
5842/*!
5843 Constructs a string that is a deep copy of \a str, interpreted as a
5844 UCS2 encoded, zero terminated, Unicode string.
5845
5846 If \a str is 0, then a null string is created.
5847
5848 \sa isNull()
5849*/
5850QString QString::fromUcs2( const unsigned short *str )
5851{
5852 if ( !str ) {
5853 return QString::null;
5854 } else {
5855 int length = 0;
5856 while ( str[length] != 0 )
5857 length++;
5858 QChar* uc = QT_ALLOC_QCHAR_VEC( length );
5859 memcpy( uc, str, length*sizeof(QChar) );
5860 return QString( new QStringData( uc, length, length ), TRUE );
5861 }
5862}
5863
5864/*!
5865 \fn QChar QString::at( uint ) const
5866
5867 Returns the character at index \a i, or 0 if \a i is beyond the
5868 length of the string.
5869
5870 \code
5871 const QString string( "abcdefgh" );
5872 QChar ch = string.at( 4 );
5873 // ch == 'e'
5874 \endcode
5875
5876 If the QString is not const (i.e. const QString) or const& (i.e.
5877 const QString &), then the non-const overload of at() will be used
5878 instead.
5879*/
5880
5881/*!
5882 \fn QChar QString::constref(uint i) const
5883
5884 Returns the QChar at index \a i by value.
5885
5886 Equivalent to at(\a i).
5887
5888 \sa ref()
5889*/
5890
5891/*!
5892 \fn QChar& QString::ref(uint i)
5893
5894 Returns the QChar at index \a i by reference, expanding the string
5895 with QChar::null if necessary. The resulting reference can be
5896 assigned to, or otherwise used immediately, but becomes invalid
5897 once furher modifications are made to the string.
5898
5899 \code
5900 QString string("ABCDEF");
5901 QChar ch = string.ref( 3 ); // ch == 'D'
5902 \endcode
5903
5904 \sa constref()
5905*/
5906
5907/*!
5908 \fn QChar QString::operator[]( int ) const
5909
5910 Returns the character at index \a i, or QChar::null if \a i is
5911 beyond the length of the string.
5912
5913 If the QString is not const (i.e., const QString) or const\&
5914 (i.e., const QString\&), then the non-const overload of operator[]
5915 will be used instead.
5916*/
5917
5918/*!
5919 \fn QCharRef QString::operator[]( int )
5920
5921 \overload
5922
5923 The function returns a reference to the character at index \a i.
5924 The resulting reference can then be assigned to, or used
5925 immediately, but it will become invalid once further modifications
5926 are made to the original string.
5927
5928 If \a i is beyond the length of the string then the string is
5929 expanded with QChar::nulls, so that the QCharRef references a
5930 valid (null) character in the string.
5931
5932 The QCharRef internal class can be used much like a constant
5933 QChar, but if you assign to it, you change the original string
5934 (which will detach itself because of QString's copy-on-write
5935 semantics). You will get compilation errors if you try to use the
5936 result as anything but a QChar.
5937*/
5938
5939/*!
5940 \fn QCharRef QString::at( uint i )
5941
5942 \overload
5943
5944 The function returns a reference to the character at index \a i.
5945 The resulting reference can then be assigned to, or used
5946 immediately, but it will become invalid once further modifications
5947 are made to the original string.
5948
5949 If \a i is beyond the length of the string then the string is
5950 expanded with QChar::null.
5951*/
5952
5953/*
5954 Internal chunk of code to handle the
5955 uncommon cases of at() above.
5956*/
5957void QString::subat( uint i )
5958{
5959 uint olen = d->len;
5960 if ( i >= olen ) {
5961 setLength( i+1 ); // i is index; i+1 is needed length
5962 for ( uint j=olen; j<=i; j++ )
5963 d->unicode[j] = QChar::null;
5964 } else {
5965 // Just be sure to detach
5966 real_detach();
5967 }
5968}
5969
5970
5971/*!
5972 Resizes the string to \a len characters and copies \a unicode into
5973 the string. If \a unicode is 0, nothing is copied, but the
5974 string is still resized to \a len. If \a len is zero, then the
5975 string becomes a \link isNull() null\endlink string.
5976
5977 \sa setLatin1(), isNull()
5978*/
5979
5980QString& QString::setUnicode( const QChar *unicode, uint len )
5981{
5982 if ( len == 0 ) { // set to null string
5983 if ( d != shared_null ) { // beware of nullstring being set to nullstring
5984 deref();
5985 d = shared_null ? shared_null : makeSharedNull();
5986 d->ref();
5987 }
5988 } else if ( d->count != 1 || len > d->maxl ||
5989 ( len * 4 < d->maxl && d->maxl > 4 ) ) {
5990 // detach, grown or shrink
5991 uint newMax = computeNewMax( len );
5992 QChar* nd = QT_ALLOC_QCHAR_VEC( newMax );
5993 if ( unicode )
5994 memcpy( nd, unicode, sizeof(QChar)*len );
5995 deref();
5996 d = new QStringData( nd, len, newMax );
5997 } else {
5998 d->len = len;
5999 d->setDirty();
6000 if ( unicode )
6001 memcpy( d->unicode, unicode, sizeof(QChar)*len );
6002 }
6003 return *this;
6004}
6005
6006/*!
6007 Resizes the string to \a len characters and copies \a
6008 unicode_as_ushorts into the string (on some X11 client platforms
6009 this will involve a byte-swapping pass).
6010
6011 If \a unicode_as_ushorts is 0, nothing is copied, but the string
6012 is still resized to \a len. If \a len is zero, the string becomes
6013 a \link isNull() null\endlink string.
6014
6015 \sa setLatin1(), isNull()
6016*/
6017QString& QString::setUnicodeCodes( const ushort* unicode_as_ushorts, uint len )
6018{
6019 return setUnicode((const QChar*)unicode_as_ushorts, len);
6020}
6021
6022
6023/*!
6024 Sets this string to \a str, interpreted as a classic 8-bit ASCII C
6025 string. If \a len is -1 (the default), then it is set to
6026 strlen(str).
6027
6028 If \a str is 0 a null string is created. If \a str is "", an empty
6029 string is created.
6030
6031 \sa isNull(), isEmpty()
6032*/
6033
6034QString &QString::setAscii( const char *str, int len )
6035{
6036#ifndef QT_NO_TEXTCODEC
6037 if ( QTextCodec::codecForCStrings() ) {
6038 *this = QString::fromAscii( str, len );
6039 return *this;
6040 }
6041#endif // QT_NO_TEXTCODEC
6042 return setLatin1( str, len );
6043}
6044
6045/*!
6046 Sets this string to \a str, interpreted as a classic Latin-1 C
6047 string. If \a len is -1 (the default), then it is set to
6048 strlen(str).
6049
6050 If \a str is 0 a null string is created. If \a str is "", an empty
6051 string is created.
6052
6053 \sa isNull(), isEmpty()
6054*/
6055
6056QString &QString::setLatin1( const char *str, int len )
6057{
6058 if ( str == 0 )
6059 return setUnicode(0,0);
6060 if ( len < 0 )
6061 len = strlen( str );
6062 if ( len == 0 ) { // won't make a null string
6063 *this = QString::fromLatin1( "" );
6064 } else {
6065 setUnicode( 0, len ); // resize but not copy
6066 QChar *p = d->unicode;
6067 while ( len-- )
6068 *p++ = *str++;
6069 }
6070 return *this;
6071}
6072
6073/*! \internal
6074 */
6075void QString::checkSimpleText() const
6076{
6077 QChar *p = d->unicode;
6078 QChar *end = p + d->len;
6079 while ( p < end ) {
6080 ushort uc = p->unicode();
6081 // sort out regions of complex text formatting
6082 if ( uc > 0x058f && ( uc < 0x1100 || uc > 0xfb0f ) ) {
6083 d->issimpletext = FALSE;
6084 return;
6085 }
6086 p++;
6087 }
6088 d->issimpletext = TRUE;
6089}
6090
6091/*! \fn bool QString::simpleText() const
6092 \internal
6093*/
6094
6095/*! \internal
6096 */
6097bool QString::isRightToLeft() const
6098{
6099 int len = length();
6100 QChar *p = d->unicode;
6101 while ( len-- ) {
6102 switch( ::direction( *p ) )
6103 {
6104 case QChar::DirL:
6105 case QChar::DirLRO:
6106 case QChar::DirLRE:
6107 return FALSE;
6108 case QChar::DirR:
6109 case QChar::DirAL:
6110 case QChar::DirRLO:
6111 case QChar::DirRLE:
6112 return TRUE;
6113 default:
6114 break;
6115 }
6116 ++p;
6117 }
6118 return FALSE;
6119}
6120
6121
6122/*!
6123 \fn int QString::compare( const QString & s1, const QString & s2 )
6124
6125 Lexically compares \a s1 with \a s2 and returns an integer less
6126 than, equal to, or greater than zero if \a s1 is less than, equal
6127 to, or greater than \a s2.
6128
6129 The comparison is based exclusively on the numeric Unicode values
6130 of the characters and is very fast, but is not what a human would
6131 expect. Consider sorting user-interface strings with
6132 QString::localeAwareCompare().
6133
6134 \code
6135 int a = QString::compare( "def", "abc" ); // a > 0
6136 int b = QString::compare( "abc", "def" ); // b < 0
6137 int c = QString::compare( "abc", "abc" ); // c == 0
6138 \endcode
6139*/
6140
6141/*!
6142 \overload
6143
6144 Lexically compares this string with \a s and returns an integer
6145 less than, equal to, or greater than zero if it is less than, equal
6146 to, or greater than \a s.
6147*/
6148int QString::compare( const QString& s ) const
6149{
6150 return ucstrcmp( *this, s );
6151}
6152
6153/*!
6154 \fn int QString::localeAwareCompare( const QString & s1, const QString & s2 )
6155
6156 Compares \a s1 with \a s2 and returns an integer less than, equal
6157 to, or greater than zero if \a s1 is less than, equal to, or
6158 greater than \a s2.
6159
6160 The comparison is performed in a locale- and also
6161 platform-dependent manner. Use this function to present sorted
6162 lists of strings to the user.
6163
6164 \sa QString::compare() QTextCodec::locale()
6165*/
6166
6167/*!
6168 \overload
6169
6170 Compares this string with \a s.
6171*/
6172
6173#if !defined(CSTR_LESS_THAN)
6174#define CSTR_LESS_THAN 1
6175#define CSTR_EQUAL 2
6176#define CSTR_GREATER_THAN 3
6177#endif
6178
6179int QString::localeAwareCompare( const QString& s ) const
6180{
6181 // do the right thing for null and empty
6182 if ( isEmpty() || s.isEmpty() )
6183 return compare( s );
6184
6185#if defined(Q_WS_WIN)
6186 int res;
6187 QT_WA( {
6188 const TCHAR* s1 = (TCHAR*)ucs2();
6189 const TCHAR* s2 = (TCHAR*)s.ucs2();
6190 res = CompareStringW( LOCALE_USER_DEFAULT, 0, s1, length(), s2, s.length() );
6191 } , {
6192 QCString s1 = local8Bit();
6193 QCString s2 = s.local8Bit();
6194 res = CompareStringA( LOCALE_USER_DEFAULT, 0, s1.data(), s1.length(), s2.data(), s2.length() );
6195 } );
6196
6197 switch ( res ) {
6198 case CSTR_LESS_THAN:
6199 return -1;
6200 case CSTR_GREATER_THAN:
6201 return 1;
6202 default:
6203 return 0;
6204 }
6205#elif defined(Q_OS_OS2)
6206 LocaleObject lo = qt_os2DefLocaleObj();
6207 if (lo)
6208 return UniStrcoll( lo, (UniChar*) ucs2(), (UniChar *) s.ucs2() );
6209 else
6210 return ucstrcmp( *this, s );
6211#elif defined(Q_WS_MACX)
6212 int delta = 0;
6213#if !defined(QT_NO_TEXTCODEC)
6214 QTextCodec *codec = QTextCodec::codecForLocale();
6215 if (codec)
6216 delta = strcoll(codec->fromUnicode(*this), codec->fromUnicode(s));
6217 if (delta == 0)
6218#endif
6219 delta = ucstrcmp(*this, s);
6220 return delta;
6221#elif defined(Q_WS_X11)
6222 // declared in <string.h>
6223 int delta = strcoll( local8Bit(), s.local8Bit() );
6224 if ( delta == 0 )
6225 delta = ucstrcmp( *this, s );
6226 return delta;
6227#else
6228 return ucstrcmp( *this, s );
6229#endif
6230}
6231
6232bool operator==( const QString &s1, const QString &s2 )
6233{
6234 if ( s1.unicode() == s2.unicode() )
6235 return TRUE;
6236 return (s1.length() == s2.length()) && s1.isNull() == s2.isNull() &&
6237 (memcmp((char*)s1.unicode(),(char*)s2.unicode(),
6238 s1.length()*sizeof(QChar)) == 0 );
6239}
6240
6241bool operator!=( const QString &s1, const QString &s2 )
6242{ return !(s1==s2); }
6243
6244bool operator<( const QString &s1, const QString &s2 )
6245{ return ucstrcmp(s1,s2) < 0; }
6246
6247bool operator<=( const QString &s1, const QString &s2 )
6248{ return ucstrcmp(s1,s2) <= 0; }
6249
6250bool operator>( const QString &s1, const QString &s2 )
6251{ return ucstrcmp(s1,s2) > 0; }
6252
6253bool operator>=( const QString &s1, const QString &s2 )
6254{ return ucstrcmp(s1,s2) >= 0; }
6255
6256
6257bool operator==( const QString &s1, const char *s2 )
6258{
6259 if ( !s2 )
6260 return s1.isNull();
6261
6262 int len = s1.length();
6263 const QChar *uc = s1.unicode();
6264 while ( len ) {
6265 if ( !(*s2) || uc->unicode() != (uchar) *s2 )
6266 return FALSE;
6267 ++uc;
6268 ++s2;
6269 --len;
6270 }
6271 return !*s2;
6272}
6273
6274bool operator==( const char *s1, const QString &s2 )
6275{ return (s2 == s1); }
6276
6277bool operator!=( const QString &s1, const char *s2 )
6278{ return !(s1==s2); }
6279
6280bool operator!=( const char *s1, const QString &s2 )
6281{ return !(s1==s2); }
6282
6283bool operator<( const QString &s1, const char *s2 )
6284{ return ucstrcmp(s1,s2) < 0; }
6285
6286bool operator<( const char *s1, const QString &s2 )
6287{ return ucstrcmp(s1,s2) < 0; }
6288
6289bool operator<=( const QString &s1, const char *s2 )
6290{ return ucstrcmp(s1,s2) <= 0; }
6291
6292bool operator<=( const char *s1, const QString &s2 )
6293{ return ucstrcmp(s1,s2) <= 0; }
6294
6295bool operator>( const QString &s1, const char *s2 )
6296{ return ucstrcmp(s1,s2) > 0; }
6297
6298bool operator>( const char *s1, const QString &s2 )
6299{ return ucstrcmp(s1,s2) > 0; }
6300
6301bool operator>=( const QString &s1, const char *s2 )
6302{ return ucstrcmp(s1,s2) >= 0; }
6303
6304bool operator>=( const char *s1, const QString &s2 )
6305{ return ucstrcmp(s1,s2) >= 0; }
6306
6307
6308/*****************************************************************************
6309 Documentation for QString related functions
6310 *****************************************************************************/
6311
6312/*!
6313 \fn bool operator==( const QString &s1, const QString &s2 )
6314
6315 \relates QString
6316
6317 Returns TRUE if \a s1 is equal to \a s2; otherwise returns FALSE.
6318 Note that a null string is not equal to a not-null empty string.
6319
6320 Equivalent to compare(\a s1, \a s2) != 0.
6321
6322 \sa isNull(), isEmpty()
6323*/
6324
6325/*!
6326 \fn bool operator==( const QString &s1, const char *s2 )
6327
6328 \overload
6329 \relates QString
6330
6331 Returns TRUE if \a s1 is equal to \a s2; otherwise returns FALSE.
6332 Note that a null string is not equal to a not-null empty string.
6333
6334 Equivalent to compare(\a s1, \a s2) == 0.
6335
6336 \sa isNull(), isEmpty()
6337*/
6338
6339/*!
6340 \fn bool operator==( const char *s1, const QString &s2 )
6341
6342 \overload
6343 \relates QString
6344
6345 Returns TRUE if \a s1 is equal to \a s2; otherwise returns FALSE.
6346 Note that a null string is not equal to a not-null empty string.
6347
6348 Equivalent to compare(\a s1, \a s2) == 0.
6349
6350 \sa isNull(), isEmpty()
6351*/
6352
6353/*!
6354 \fn bool operator!=( const QString &s1, const QString &s2 )
6355
6356 \relates QString
6357
6358 Returns TRUE if \a s1 is not equal to \a s2; otherwise returns FALSE.
6359 Note that a null string is not equal to a not-null empty string.
6360
6361 Equivalent to compare(\a s1, \a s2) != 0.
6362
6363 \sa isNull(), isEmpty()
6364*/
6365
6366/*!
6367 \fn bool operator!=( const QString &s1, const char *s2 )
6368
6369 \overload
6370 \relates QString
6371
6372 Returns TRUE if \a s1 is not equal to \a s2; otherwise returns FALSE.
6373 Note that a null string is not equal to a not-null empty string.
6374
6375 Equivalent to compare(\a s1, \a s2) != 0.
6376
6377 \sa isNull(), isEmpty()
6378*/
6379
6380/*!
6381 \fn bool operator!=( const char *s1, const QString &s2 )
6382
6383 \overload
6384 \relates QString
6385
6386 Returns TRUE if \a s1 is not equal to \a s2; otherwise returns FALSE.
6387 Note that a null string is not equal to a not-null empty string.
6388
6389 Equivalent to compare(\a s1, \a s2) != 0.
6390
6391 \sa isNull(), isEmpty()
6392*/
6393
6394/*!
6395 \fn bool operator<( const QString &s1, const char *s2 )
6396
6397 \relates QString
6398
6399 Returns TRUE if \a s1 is lexically less than \a s2; otherwise returns FALSE.
6400 The comparison is case sensitive.
6401
6402 Equivalent to compare(\a s1, \a s2) \< 0.
6403*/
6404
6405/*!
6406 \fn bool operator<( const char *s1, const QString &s2 )
6407
6408 \overload
6409 \relates QString
6410
6411 Returns TRUE if \a s1 is lexically less than \a s2; otherwise returns FALSE.
6412 The comparison is case sensitive.
6413
6414 Equivalent to compare(\a s1, \a s2) \< 0.
6415*/
6416
6417/*!
6418 \fn bool operator<=( const QString &s1, const char *s2 )
6419
6420 \relates QString
6421
6422 Returns TRUE if \a s1 is lexically less than or equal to \a s2;
6423 otherwise returns FALSE.
6424 The comparison is case sensitive.
6425 Note that a null string is not equal to a not-null empty string.
6426
6427 Equivalent to compare(\a s1,\a s2) \<= 0.
6428
6429 \sa isNull(), isEmpty()
6430*/
6431
6432/*!
6433 \fn bool operator<=( const char *s1, const QString &s2 )
6434
6435 \overload
6436 \relates QString
6437
6438 Returns TRUE if \a s1 is lexically less than or equal to \a s2;
6439 otherwise returns FALSE.
6440 The comparison is case sensitive.
6441 Note that a null string is not equal to a not-null empty string.
6442
6443 Equivalent to compare(\a s1, \a s2) \<= 0.
6444
6445 \sa isNull(), isEmpty()
6446*/
6447
6448/*!
6449 \fn bool operator>( const QString &s1, const char *s2 )
6450
6451 \relates QString
6452
6453 Returns TRUE if \a s1 is lexically greater than \a s2; otherwise
6454 returns FALSE.
6455 The comparison is case sensitive.
6456
6457 Equivalent to compare(\a s1, \a s2) \> 0.
6458*/
6459
6460/*!
6461 \fn bool operator>( const char *s1, const QString &s2 )
6462
6463 \overload
6464 \relates QString
6465
6466 Returns TRUE if \a s1 is lexically greater than \a s2; otherwise
6467 returns FALSE.
6468 The comparison is case sensitive.
6469
6470 Equivalent to compare(\a s1, \a s2) \> 0.
6471*/
6472
6473/*!
6474 \fn bool operator>=( const QString &s1, const char *s2 )
6475
6476 \relates QString
6477
6478 Returns TRUE if \a s1 is lexically greater than or equal to \a s2;
6479 otherwise returns FALSE.
6480 The comparison is case sensitive.
6481 Note that a null string is not equal to a not-null empty string.
6482
6483 Equivalent to compare(\a s1, \a s2) \>= 0.
6484
6485 \sa isNull(), isEmpty()
6486*/
6487
6488/*!
6489 \fn bool operator>=( const char *s1, const QString &s2 )
6490
6491 \overload
6492 \relates QString
6493
6494 Returns TRUE if \a s1 is lexically greater than or equal to \a s2;
6495 otherwise returns FALSE.
6496 The comparison is case sensitive.
6497 Note that a null string is not equal to a not-null empty string.
6498
6499 Equivalent to compare(\a s1, \a s2) \>= 0.
6500
6501 \sa isNull(), isEmpty()
6502*/
6503
6504/*!
6505 \fn const QString operator+( const QString &s1, const QString &s2 )
6506
6507 \relates QString
6508
6509 Returns a string which is the result of concatenating the string
6510 \a s1 and the string \a s2.
6511
6512 Equivalent to \a {s1}.append(\a s2).
6513*/
6514
6515/*!
6516 \fn const QString operator+( const QString &s1, const char *s2 )
6517
6518 \overload
6519 \relates QString
6520
6521 Returns a string which is the result of concatenating the string
6522 \a s1 and character \a s2.
6523
6524 Equivalent to \a {s1}.append(\a s2).
6525*/
6526
6527/*!
6528 \fn const QString operator+( const char *s1, const QString &s2 )
6529
6530 \overload
6531 \relates QString
6532
6533 Returns a string which is the result of concatenating the
6534 character \a s1 and string \a s2.
6535*/
6536
6537/*!
6538 \fn const QString operator+( const QString &s, char c )
6539
6540 \overload
6541 \relates QString
6542
6543 Returns a string which is the result of concatenating the string
6544 \a s and character \a c.
6545
6546 Equivalent to \a {s}.append(\a c).
6547*/
6548
6549/*!
6550 \fn const QString operator+( char c, const QString &s )
6551
6552 \overload
6553 \relates QString
6554
6555 Returns a string which is the result of concatenating the
6556 character \a c and string \a s.
6557
6558 Equivalent to \a {s}.prepend(\a c).
6559*/
6560
6561
6562/*****************************************************************************
6563 QString stream functions
6564 *****************************************************************************/
6565#ifndef QT_NO_DATASTREAM
6566/*!
6567 \relates QString
6568
6569 Writes the string \a str to the stream \a s.
6570
6571 See also \link datastreamformat.html Format of the QDataStream operators \endlink
6572*/
6573
6574QDataStream &operator<<( QDataStream &s, const QString &str )
6575{
6576 if ( s.version() == 1 ) {
6577 QCString l( str.latin1() );
6578 s << l;
6579 }
6580 else {
6581 int byteOrder = s.byteOrder();
6582 const QChar* ub = str.unicode();
6583 if ( ub || s.version() < 3 ) {
6584 static const uint auto_size = 1024;
6585 char t[auto_size];
6586 char *b;
6587 if ( str.length()*sizeof(QChar) > auto_size ) {
6588 b = new char[str.length()*sizeof(QChar)];
6589 } else {
6590 b = t;
6591 }
6592 int l = str.length();
6593 char *c=b;
6594 while ( l-- ) {
6595 if ( byteOrder == QDataStream::BigEndian ) {
6596 *c++ = (char)ub->row();
6597 *c++ = (char)ub->cell();
6598 } else {
6599 *c++ = (char)ub->cell();
6600 *c++ = (char)ub->row();
6601 }
6602 ub++;
6603 }
6604 s.writeBytes( b, sizeof(QChar)*str.length() );
6605 if ( str.length()*sizeof(QChar) > auto_size )
6606 delete [] b;
6607 } else {
6608 // write null marker
6609 s << (Q_UINT32)0xffffffff;
6610 }
6611 }
6612 return s;
6613}
6614
6615/*!
6616 \relates QString
6617
6618 Reads a string from the stream \a s into string \a str.
6619
6620 See also \link datastreamformat.html Format of the QDataStream operators \endlink
6621*/
6622
6623QDataStream &operator>>( QDataStream &s, QString &str )
6624{
6625#ifdef QT_QSTRING_UCS_4
6626#if defined(Q_CC_GNU)
6627#warning "operator>> not working properly"
6628#endif
6629#endif
6630 if ( s.version() == 1 ) {
6631 QCString l;
6632 s >> l;
6633 str = QString( l );
6634 }
6635 else {
6636 Q_UINT32 bytes;
6637 s >> bytes; // read size of string
6638 if ( bytes == 0xffffffff ) { // null string
6639 str = QString::null;
6640 } else if ( bytes > 0 ) { // not empty
6641 int byteOrder = s.byteOrder();
6642 str.setLength( bytes/2 );
6643 QChar* ch = str.d->unicode;
6644 static const uint auto_size = 1024;
6645 char t[auto_size];
6646 char *b;
6647 if ( bytes > auto_size ) {
6648 b = new char[bytes];
6649 } else {
6650 b = t;
6651 }
6652 s.readRawBytes( b, bytes );
6653 int bt = bytes/2;
6654 char *oldb = b;
6655 while ( bt-- ) {
6656 if ( byteOrder == QDataStream::BigEndian )
6657 *ch++ = (ushort) (((ushort)b[0])<<8) | (uchar)b[1];
6658 else
6659 *ch++ = (ushort) (((ushort)b[1])<<8) | (uchar)b[0];
6660 b += 2;
6661 }
6662 if ( bytes > auto_size )
6663 delete [] oldb;
6664 } else {
6665 str = "";
6666 }
6667 }
6668 return s;
6669}
6670#endif // QT_NO_DATASTREAM
6671
6672/*****************************************************************************
6673 QConstString member functions
6674 *****************************************************************************/
6675
6676/*!
6677 \class QConstString qstring.h
6678 \reentrant
6679 \ingroup text
6680 \brief The QConstString class provides string objects using constant Unicode data.
6681
6682 In order to minimize copying, highly optimized applications can
6683 use QConstString to provide a QString-compatible object from
6684 existing Unicode data. It is then the programmer's responsibility
6685 to ensure that the Unicode data exists for the entire lifetime of
6686 the QConstString object.
6687
6688 A QConstString is created with the QConstString constructor. The
6689 string held by the object can be obtained by calling string().
6690*/
6691
6692/*!
6693 Constructs a QConstString that uses the first \a length Unicode
6694 characters in the array \a unicode. Any attempt to modify copies
6695 of the string will cause it to create a copy of the data, thus it
6696 remains forever unmodified.
6697
6698 The data in \a unicode is not copied. The caller must be able to
6699 guarantee that \a unicode will not be deleted or modified.
6700*/
6701QConstString::QConstString( const QChar* unicode, uint length ) :
6702 QString( new QStringData( (QChar*)unicode, length, length ), TRUE )
6703{
6704}
6705
6706/*!
6707 Destroys the QConstString, creating a copy of the data if other
6708 strings are still using it.
6709*/
6710QConstString::~QConstString()
6711{
6712 if ( d->count > 1 ) {
6713 QChar* cp = QT_ALLOC_QCHAR_VEC( d->len );
6714 memcpy( cp, d->unicode, d->len*sizeof(QChar) );
6715 d->unicode = cp;
6716 } else {
6717 d->unicode = 0;
6718 }
6719
6720 // The original d->unicode is now unlinked.
6721}
6722
6723/*!
6724 \fn const QString& QConstString::string() const
6725
6726 Returns a constant string referencing the data passed during
6727 construction.
6728*/
6729
6730/*!
6731 Returns TRUE if the string starts with \a s; otherwise returns
6732 FALSE.
6733
6734 If \a cs is TRUE (the default), the search is case sensitive;
6735 otherwise the search is case insensitive.
6736
6737 \code
6738 QString str( "Bananas" );
6739 str.startsWith( "Ban" ); // returns TRUE
6740 str.startsWith( "Car" ); // returns FALSE
6741 \endcode
6742
6743 \sa endsWith()
6744*/
6745bool QString::startsWith( const QString& s, bool cs ) const
6746{
6747 if ( isNull() )
6748 return s.isNull();
6749 if ( s.length() > length() )
6750 return FALSE;
6751 if ( cs ) {
6752 for ( int i = 0; i < (int) s.length(); i++ ) {
6753 if ( d->unicode[i] != s[i] )
6754 return FALSE;
6755 }
6756 } else {
6757 for ( int i = 0; i < (int) s.length(); i++ ) {
6758 if ( d->unicode[i].lower() != s[i].lower() )
6759 return FALSE;
6760 }
6761 }
6762 return TRUE;
6763}
6764
6765bool QString::startsWith( const QString& s ) const
6766{
6767 return startsWith( s, TRUE );
6768}
6769
6770/*!
6771 Returns TRUE if the string ends with \a s; otherwise returns
6772 FALSE.
6773
6774 If \a cs is TRUE (the default), the search is case sensitive;
6775 otherwise the search is case insensitive.
6776
6777 \code
6778 QString str( "Bananas" );
6779 str.endsWith( "anas" ); // returns TRUE
6780 str.endsWith( "pple" ); // returns FALSE
6781 \endcode
6782
6783 \sa startsWith()
6784*/
6785bool QString::endsWith( const QString& s, bool cs ) const
6786{
6787 if ( isNull() )
6788 return s.isNull();
6789 int pos = length() - s.length();
6790 if ( pos < 0 )
6791 return FALSE;
6792 if ( cs ) {
6793 for ( int i = 0; i < (int) s.length(); i++ ) {
6794 if ( d->unicode[pos + i] != s[i] )
6795 return FALSE;
6796 }
6797 } else {
6798 for ( int i = 0; i < (int) s.length(); i++ ) {
6799 if ( d->unicode[pos + i].lower() != s[i].lower() )
6800 return FALSE;
6801 }
6802 }
6803 return TRUE;
6804}
6805
6806bool QString::endsWith( const QString& s ) const
6807{
6808 return endsWith( s, TRUE );
6809}
6810
6811/*! \fn void QString::detach()
6812 If the string does not share its data with another QString instance,
6813 nothing happens; otherwise the function creates a new, unique copy of
6814 this string. This function is called whenever the string is modified. The
6815 implicit sharing mechanism is implemented this way.
6816*/
6817
6818#if defined(Q_OS_WIN32)
6819
6820#include <windows.h>
6821
6822/*!
6823 \obsolete
6824
6825 Returns a static Windows TCHAR* from a QString, adding NUL if \a
6826 addnul is TRUE.
6827
6828 The lifetime of the return value is until the next call to this function,
6829 or until the last copy of str is deleted, whatever comes first.
6830
6831 Use ucs2() instead.
6832*/
6833const void* qt_winTchar(const QString& str, bool)
6834{
6835 // So that the return value lives long enough.
6836 static QString str_cache;
6837 str_cache = str;
6838#ifdef UNICODE
6839 return str_cache.ucs2();
6840#else
6841 return str_cache.latin1();
6842#endif
6843}
6844
6845/*!
6846 Makes a new '\0'-terminated Windows TCHAR* from a QString.
6847*/
6848void* qt_winTchar_new(const QString& str)
6849{
6850 if ( str.isNull() )
6851 return 0;
6852 int l = str.length()+1;
6853 TCHAR *tc = new TCHAR[ l ];
6854#ifdef UNICODE
6855 memcpy( tc, str.ucs2(), sizeof(TCHAR)*l );
6856#else
6857 memcpy( tc, str.latin1(), sizeof(TCHAR)*l );
6858#endif
6859 return tc;
6860}
6861
6862/*!
6863 Makes a QString from a Windows TCHAR*.
6864*/
6865QString qt_winQString(void* tc)
6866{
6867#ifdef UNICODE
6868 return QString::fromUcs2( (ushort*)tc );
6869#else
6870 return QString::fromLatin1( (TCHAR *)tc );
6871#endif
6872}
6873
6874QCString qt_winQString2MB( const QString& s, int uclen )
6875{
6876 if ( uclen < 0 )
6877 uclen = s.length();
6878 if ( s.isNull() )
6879 return QCString();
6880 if ( uclen == 0 )
6881 return QCString("");
6882 BOOL used_def;
6883 QCString mb(4096);
6884 int len;
6885 while ( !(len=WideCharToMultiByte(CP_ACP, 0, (const WCHAR*)s.unicode(), uclen,
6886 mb.data(), mb.size()-1, 0, &used_def)) )
6887 {
6888 int r = GetLastError();
6889 if ( r == ERROR_INSUFFICIENT_BUFFER ) {
6890 mb.resize(1+WideCharToMultiByte( CP_ACP, 0,
6891 (const WCHAR*)s.unicode(), uclen,
6892 0, 0, 0, &used_def));
6893 // and try again...
6894 } else {
6895#ifndef QT_NO_DEBUG
6896 // Fail.
6897 qWarning("WideCharToMultiByte cannot convert multibyte text (error %d): %s (UTF8)",
6898 r, s.utf8().data());
6899#endif
6900 break;
6901 }
6902 }
6903 mb[len]='\0';
6904 return mb;
6905}
6906
6907// WATCH OUT: mblen must include the NUL (or just use -1)
6908QString qt_winMB2QString( const char* mb, int mblen )
6909{
6910 if ( !mb || !mblen )
6911 return QString::null;
6912 const int wclen_auto = 4096;
6913 WCHAR wc_auto[wclen_auto];
6914 int wclen = wclen_auto;
6915 WCHAR *wc = wc_auto;
6916 int len;
6917 while ( !(len=MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED,
6918 mb, mblen, wc, wclen )) )
6919 {
6920 int r = GetLastError();
6921 if ( r == ERROR_INSUFFICIENT_BUFFER ) {
6922 if ( wc != wc_auto ) {
6923 qWarning("Size changed in MultiByteToWideChar");
6924 break;
6925 } else {
6926 wclen = MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED,
6927 mb, mblen, 0, 0 );
6928 wc = new WCHAR[wclen];
6929 // and try again...
6930 }
6931 } else {
6932 // Fail.
6933 qWarning("MultiByteToWideChar cannot convert multibyte text");
6934 break;
6935 }
6936 }
6937 if ( len <= 0 )
6938 return QString::null;
6939 QString s( (QChar*)wc, len - 1 ); // len - 1: we don't want terminator
6940 if ( wc != wc_auto )
6941 delete [] wc;
6942 return s;
6943}
6944
6945#endif // Q_OS_WIN32
6946
6947#if defined (Q_OS_OS2)
6948
6949static LocaleObject qt_os2DefLocaleObj_var = NULL;
6950static UconvObject qt_os2DefUconvObj_var = NULL;
6951
6952const LocaleObject qt_os2DefLocaleObj()
6953{
6954 if ( !qt_os2DefLocaleObj_var )
6955 UniCreateLocaleObject( UNI_UCS_STRING_POINTER, (UniChar *)L"",
6956 &qt_os2DefLocaleObj_var );
6957 return qt_os2DefLocaleObj_var;
6958}
6959
6960const UconvObject qt_os2DefUconvObj()
6961{
6962 if ( !qt_os2DefUconvObj_var )
6963 UniCreateUconvObject( (UniChar *)L"", &qt_os2DefUconvObj_var );
6964 return qt_os2DefUconvObj_var;
6965}
6966
6967QCString qt_os2QString2MB( const QString& s, int uclen )
6968{
6969 if ( uclen < 0 || (uint) uclen > s.length() )
6970 uclen = s.length();
6971 if ( s.isNull() )
6972 return QCString();
6973 if ( uclen == 0 )
6974 return QCString("");
6975
6976 UniChar *ucPtr = (UniChar *) s.unicode();
6977 size_t ucLeft = uclen;
6978
6979 // be optimistic (imply that one byte is necessary per every Unicode char)
6980 size_t mblen = uclen;
6981 QCString mbBuf( mblen + 1 );
6982 char *mbPtr = mbBuf.data();
6983 size_t mbLeft = mblen;
6984
6985 size_t nonIdent = 0;
6986 UconvObject uo = qt_os2DefUconvObj();
6987 int rc;
6988
6989 while ( ucLeft ) {
6990 rc = UniUconvFromUcs ( uo, &ucPtr, &ucLeft, (void**)&mbPtr, &mbLeft, &nonIdent );
6991 if ( rc == ULS_BUFFERFULL ) {
6992 size_t mbDone = mblen - mbLeft;
6993 size_t ucDone = uclen - ucLeft;
6994 size_t newLen = mblen;
6995 if ( ucDone ) {
6996 // assume that ucLeft/mbLeft is an approximation of ucDone/mbDone
6997 newLen = mbDone + (ucLeft * mbDone) / ucDone;
6998 }
6999 if ( newLen == mblen ) {
7000 // could not process a single Unicode char, double the size
7001 mblen *= 2;
7002 } else {
7003 mblen = newLen;
7004 }
7005 mbBuf.resize( mblen + 1 );
7006 mbPtr = mbBuf.data() + mbDone;
7007 mbLeft = mblen - mbDone;
7008 } else if ( rc != ULS_SUCCESS ) {
7009#ifndef QT_NO_DEBUG
7010 // Fail.
7011 qWarning("UniUconvFromUcs cannot convert multibyte text (error %d): %s (UTF8)",
7012 rc, s.utf8().data());
7013#endif
7014 break;
7015 }
7016 }
7017 mbBuf.truncate( mblen - mbLeft );
7018 return mbBuf;
7019}
7020
7021// WATCH OUT: mblen must NOT include the NUL (in contrast with qt_winMB2QString)
7022QString qt_os2MB2QString( const char* mb, int mblen )
7023{
7024 if ( !mb )
7025 return QString::null;
7026 if ( mblen < 0 ) {
7027 mblen = strlen( mb );
7028 } else {
7029 char *end = (char *) memchr (mb, '\0', mblen);
7030 if ( end )
7031 mblen = end - mb;
7032 }
7033 if ( !mblen )
7034 return QString::null;
7035
7036 const char *mbPtr = mb;
7037 size_t mbLeft = mblen;
7038
7039 size_t uclen = mblen;
7040 QMemArray<UniChar> ucBuf( uclen );
7041 UniChar *ucPtr = ucBuf.data();
7042 size_t ucLeft = uclen;
7043
7044 size_t nonIdent = 0;
7045 UconvObject uo = qt_os2DefUconvObj();
7046 int rc;
7047
7048 while ( mbLeft ) {
7049 rc = UniUconvToUcs ( uo, (void**)&mbPtr, &mbLeft, &ucPtr, &ucLeft, &nonIdent );
7050 if ( rc == ULS_BUFFERFULL ) {
7051 size_t ucDone = uclen - ucLeft;
7052 size_t mbDone = mblen - mbLeft;
7053 // assume that mbLeft/ucLeft is an approximation of mbDone/ucDone
7054 uclen = ucDone + (mbLeft * ucDone) / mbDone;
7055 ucBuf.resize( uclen );
7056 ucPtr = ucBuf.data() + ucDone;
7057 } else if ( rc != ULS_SUCCESS ) {
7058#ifndef QT_NO_DEBUG
7059 // Fail.
7060 qWarning("UniUconvFromUcs cannot convert multibyte text (error %d)", rc);
7061#endif
7062 break;
7063 }
7064 }
7065 uclen -= ucLeft;
7066 if ( uclen == 0 )
7067 return QString::null;
7068 QString s( (QChar*)ucBuf.data(), uclen );
7069 return s;
7070}
7071
7072#endif // Q_OS_OS2
Note: See TracBrowser for help on using the repository browser.