-rw-r--r-- | qmake/tools/qstring.cpp | 434 |
1 files changed, 290 insertions, 144 deletions
diff --git a/qmake/tools/qstring.cpp b/qmake/tools/qstring.cpp index 56df62b..7f1fac3 100644 --- a/qmake/tools/qstring.cpp +++ b/qmake/tools/qstring.cpp | |||
@@ -1,109 +1,113 @@ | |||
1 | /**************************************************************************** | 1 | /**************************************************************************** |
2 | ** $Id$ | 2 | ** $Id$ |
3 | ** | 3 | ** |
4 | ** Implementation of the QString class and related Unicode functions | 4 | ** Implementation of the QString class and related Unicode functions |
5 | ** | 5 | ** |
6 | ** Created : 920722 | 6 | ** Created : 920722 |
7 | ** | 7 | ** |
8 | ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved. | 8 | ** Copyright (C) 1992-2002 Trolltech AS. All rights reserved. |
9 | ** | 9 | ** |
10 | ** This file is part of the tools module of the Qt GUI Toolkit. | 10 | ** This file is part of the tools module of the Qt GUI Toolkit. |
11 | ** | 11 | ** |
12 | ** This file may be distributed under the terms of the Q Public License | 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 | 13 | ** as defined by Trolltech AS of Norway and appearing in the file |
14 | ** LICENSE.QPL included in the packaging of this file. | 14 | ** LICENSE.QPL included in the packaging of this file. |
15 | ** | 15 | ** |
16 | ** This file may be distributed and/or modified under the terms of the | 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 | 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 | 18 | ** Foundation and appearing in the file LICENSE.GPL included in the |
19 | ** packaging of this file. | 19 | ** packaging of this file. |
20 | ** | 20 | ** |
21 | ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition | 21 | ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition |
22 | ** licenses may use this file in accordance with the Qt Commercial License | 22 | ** licenses may use this file in accordance with the Qt Commercial License |
23 | ** Agreement provided with the Software. | 23 | ** Agreement provided with the Software. |
24 | ** | 24 | ** |
25 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | 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. | 26 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
27 | ** | 27 | ** |
28 | ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for | 28 | ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for |
29 | ** information about Qt Commercial License Agreements. | 29 | ** information about Qt Commercial License Agreements. |
30 | ** See http://www.trolltech.com/qpl/ for QPL licensing information. | 30 | ** See http://www.trolltech.com/qpl/ for QPL licensing information. |
31 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | 31 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. |
32 | ** | 32 | ** |
33 | ** Contact info@trolltech.com if any conditions of this licensing are | 33 | ** Contact info@trolltech.com if any conditions of this licensing are |
34 | ** not clear to you. | 34 | ** not clear to you. |
35 | ** | 35 | ** |
36 | **********************************************************************/ | 36 | **********************************************************************/ |
37 | 37 | ||
38 | // Don't define it while compiling this module, or USERS of Qt will | 38 | // Don't define it while compiling this module, or USERS of Qt will |
39 | // not be able to link. | 39 | // not be able to link. |
40 | #ifdef QT_NO_CAST_ASCII | 40 | #ifdef QT_NO_CAST_ASCII |
41 | #undef QT_NO_CAST_ASCII | 41 | #undef QT_NO_CAST_ASCII |
42 | #endif | 42 | #endif |
43 | 43 | ||
44 | #include "qstring.h" | 44 | #include "qstring.h" |
45 | #include "qregexp.h" | 45 | #include "qregexp.h" |
46 | #include "qdatastream.h" | 46 | #include "qdatastream.h" |
47 | #ifndef QT_NO_TEXTCODEC | 47 | #ifndef QT_NO_TEXTCODEC |
48 | #include "qtextcodec.h" | 48 | #include "qtextcodec.h" |
49 | #endif | 49 | #endif |
50 | #include <ctype.h> | ||
51 | #include <limits.h> | 50 | #include <limits.h> |
52 | #include <stdarg.h> | 51 | #include <stdarg.h> |
53 | #include <stdio.h> | 52 | #include <stdio.h> |
54 | #include <stdlib.h> | 53 | #include <stdlib.h> |
54 | #include <string.h> | ||
55 | #if defined(Q_WS_WIN) | 55 | #if defined(Q_WS_WIN) |
56 | #include "qt_windows.h" | 56 | #include "qt_windows.h" |
57 | #endif | 57 | #endif |
58 | #if !defined( QT_NO_COMPONENT ) && !defined( QT_LITE_COMPONENT ) | 58 | #if !defined( QT_NO_COMPONENT ) && !defined( QT_LITE_COMPONENT ) |
59 | #include "qcleanuphandler.h" | 59 | #include "qcleanuphandler.h" |
60 | #endif | 60 | #endif |
61 | 61 | ||
62 | #ifdef QT_NO_UNICODETABLES | ||
63 | # include <ctype.h> | ||
64 | #endif | ||
65 | |||
62 | 66 | ||
63 | /* ------------------------------------------------------------------------- | 67 | /* ------------------------------------------------------------------------- |
64 | * unicode information | 68 | * unicode information |
65 | * these tables are generated from the unicode reference file | 69 | * these tables are generated from the unicode reference file |
66 | * ftp://ftp.unicode.org/Public/3.2-Update/UnicodeData.txt | 70 | * ftp://ftp.unicode.org/Public/3.2-Update/UnicodeData.txt |
67 | * | 71 | * |
68 | * Lars | 72 | * Lars |
69 | * ------------------------------------------------------------------------- | 73 | * ------------------------------------------------------------------------- |
70 | */ | 74 | */ |
71 | 75 | ||
72 | /* Perl script to generate (run perl -x tools/qstring.cpp) | 76 | /* Perl script to generate (run perl -x tools/qstring.cpp) |
73 | 77 | ||
74 | #!perl | 78 | #!perl |
75 | 79 | ||
76 | sub numberize | 80 | sub numberize |
77 | { | 81 | { |
78 | my(%r, $n, $id); | 82 | my(%r, $n, $id); |
79 | for $id ( @_ ) { | 83 | for $id ( @_ ) { |
80 | $i = $id; | 84 | $i = $id; |
81 | $i="" if $i eq "EMPTY"; | 85 | $i="" if $i eq "EMPTY"; |
82 | $r{$i}=$n++; | 86 | $r{$i}=$n++; |
83 | } | 87 | } |
84 | return %r; | 88 | return %r; |
85 | } | 89 | } |
86 | 90 | ||
87 | 91 | ||
88 | sub readUnicodeDataLine { | 92 | sub readUnicodeDataLine { |
89 | $code = shift @_; | 93 | $code = shift @_; |
90 | for $n (qw{ | 94 | for $n (qw{ |
91 | name category combining_class bidi_category | 95 | name category combining_class bidi_category |
92 | character_decomposition decimal_digit_value digit_value | 96 | character_decomposition decimal_digit_value digit_value |
93 | numeric_value mirrored oldname comment | 97 | numeric_value mirrored oldname comment |
94 | uppercase lowercase titlecase}) | 98 | uppercase lowercase titlecase}) |
95 | { | 99 | { |
96 | $id = shift @_; | 100 | $id = shift @_; |
97 | $codes = "${n}_code"; | 101 | $codes = "${n}_code"; |
98 | if ( defined %$codes && defined $$codes{$id} ) { | 102 | if ( defined %$codes && defined $$codes{$id} ) { |
99 | $id = $$codes{$id}; | 103 | $id = $$codes{$id}; |
100 | } | 104 | } |
101 | ${$n}{$code}=$id; | 105 | ${$n}{$code}=$id; |
102 | } | 106 | } |
103 | $decomp = $character_decomposition{$code}; | 107 | $decomp = $character_decomposition{$code}; |
104 | if ( length $decomp == 0 ) { | 108 | if ( length $decomp == 0 ) { |
105 | $decomp = "<single>"; | 109 | $decomp = "<single>"; |
106 | } | 110 | } |
107 | if (substr($decomp, 0, 1) ne '<') { | 111 | if (substr($decomp, 0, 1) ne '<') { |
108 | $decomp = "<canonical> " . $decomp; | 112 | $decomp = "<canonical> " . $decomp; |
109 | } | 113 | } |
@@ -11742,108 +11746,110 @@ static inline QChar::Category category( const QChar &c ) | |||
11742 | #endif | 11746 | #endif |
11743 | } | 11747 | } |
11744 | 11748 | ||
11745 | static inline QChar lower( const QChar &c ) | 11749 | static inline QChar lower( const QChar &c ) |
11746 | { | 11750 | { |
11747 | #ifndef QT_NO_UNICODETABLES | 11751 | #ifndef QT_NO_UNICODETABLES |
11748 | uchar row = c.row(); | 11752 | uchar row = c.row(); |
11749 | uchar cell = c.cell(); | 11753 | uchar cell = c.cell(); |
11750 | if ( unicode_info[row][cell] != QChar::Letter_Uppercase ) | 11754 | if ( unicode_info[row][cell] != QChar::Letter_Uppercase ) |
11751 | return c; | 11755 | return c; |
11752 | Q_UINT16 lower = *( case_info[row] + cell ); | 11756 | Q_UINT16 lower = *( case_info[row] + cell ); |
11753 | if ( lower == 0 ) | 11757 | if ( lower == 0 ) |
11754 | return c; | 11758 | return c; |
11755 | return lower; | 11759 | return lower; |
11756 | #else | 11760 | #else |
11757 | if ( c.row() ) | 11761 | if ( c.row() ) |
11758 | return c; | 11762 | return c; |
11759 | else | 11763 | else |
11760 | return QChar( tolower((uchar) c.latin1()) ); | 11764 | return QChar( tolower((uchar) c.latin1()) ); |
11761 | #endif | 11765 | #endif |
11762 | } | 11766 | } |
11763 | 11767 | ||
11764 | static inline QChar upper( const QChar &c ) | 11768 | static inline QChar upper( const QChar &c ) |
11765 | { | 11769 | { |
11766 | #ifndef QT_NO_UNICODETABLES | 11770 | #ifndef QT_NO_UNICODETABLES |
11767 | uchar row = c.row(); | 11771 | uchar row = c.row(); |
11768 | uchar cell = c.cell(); | 11772 | uchar cell = c.cell(); |
11769 | if ( unicode_info[row][cell] != QChar::Letter_Lowercase ) | 11773 | if ( unicode_info[row][cell] != QChar::Letter_Lowercase ) |
11770 | return c; | 11774 | return c; |
11771 | Q_UINT16 upper = *(case_info[row]+cell); | 11775 | Q_UINT16 upper = *(case_info[row]+cell); |
11772 | if ( upper == 0 ) | 11776 | if ( upper == 0 ) |
11773 | return c; | 11777 | return c; |
11774 | return upper; | 11778 | return upper; |
11775 | #else | 11779 | #else |
11776 | if ( c.row() ) | 11780 | if ( c.row() ) |
11777 | return c; | 11781 | return c; |
11778 | else | 11782 | else |
11779 | return QChar( toupper((uchar) c.latin1()) ); | 11783 | return QChar( toupper((uchar) c.latin1()) ); |
11780 | #endif | 11784 | #endif |
11781 | } | 11785 | } |
11782 | 11786 | ||
11783 | static inline QChar::Direction direction( const QChar &c ) | 11787 | static inline QChar::Direction direction( const QChar &c ) |
11784 | { | 11788 | { |
11785 | #ifndef QT_NO_UNICODETABLES | 11789 | #ifndef QT_NO_UNICODETABLES |
11786 | const Q_UINT8 *rowp = direction_info[c.row()]; | 11790 | const Q_UINT8 *rowp = direction_info[c.row()]; |
11787 | if(!rowp) return QChar::DirL; | 11791 | if(!rowp) return QChar::DirL; |
11788 | return (QChar::Direction) ( *(rowp+c.cell()) & 0x1f ); | 11792 | return (QChar::Direction) ( *(rowp+c.cell()) & 0x1f ); |
11789 | #else | 11793 | #else |
11794 | Q_UNUSED(c); | ||
11790 | return QChar::DirL; | 11795 | return QChar::DirL; |
11791 | #endif | 11796 | #endif |
11792 | } | 11797 | } |
11793 | 11798 | ||
11794 | static inline bool mirrored( const QChar &c ) | 11799 | static inline bool mirrored( const QChar &c ) |
11795 | { | 11800 | { |
11796 | #ifndef QT_NO_UNICODETABLES | 11801 | #ifndef QT_NO_UNICODETABLES |
11797 | const Q_UINT8 *rowp = direction_info[c.row()]; | 11802 | const Q_UINT8 *rowp = direction_info[c.row()]; |
11798 | if ( !rowp ) | 11803 | if ( !rowp ) |
11799 | return FALSE; | 11804 | return FALSE; |
11800 | return *(rowp+c.cell())>128; | 11805 | return *(rowp+c.cell())>128; |
11801 | #else | 11806 | #else |
11807 | Q_UNUSED(c); | ||
11802 | return FALSE; | 11808 | return FALSE; |
11803 | #endif | 11809 | #endif |
11804 | } | 11810 | } |
11805 | 11811 | ||
11806 | #ifndef QT_NO_UNICODETABLES | 11812 | #ifndef QT_NO_UNICODETABLES |
11807 | static const Q_UINT16 symmetricPairs[] = { | 11813 | static const Q_UINT16 symmetricPairs[] = { |
11808 | 0x0028, 0x0029, 0x003C, 0x003E, 0x005B, 0x005D, 0x007B, 0x007D, | 11814 | 0x0028, 0x0029, 0x003C, 0x003E, 0x005B, 0x005D, 0x007B, 0x007D, |
11809 | 0x00AB, 0x00BB, 0x2039, 0x203A, 0x2045, 0x2046, 0x207D, 0x207E, | 11815 | 0x00AB, 0x00BB, 0x2039, 0x203A, 0x2045, 0x2046, 0x207D, 0x207E, |
11810 | 0x208D, 0x208E, 0x2208, 0x220B, 0x2209, 0x220C, 0x220A, 0x220D, | 11816 | 0x208D, 0x208E, 0x2208, 0x220B, 0x2209, 0x220C, 0x220A, 0x220D, |
11811 | 0x2215, 0x29F5, 0x223C, 0x223D, 0x2243, 0x22CD, 0x2252, 0x2253, | 11817 | 0x2215, 0x29F5, 0x223C, 0x223D, 0x2243, 0x22CD, 0x2252, 0x2253, |
11812 | 0x2254, 0x2255, 0x2264, 0x2265, 0x2266, 0x2267, 0x2268, 0x2269, | 11818 | 0x2254, 0x2255, 0x2264, 0x2265, 0x2266, 0x2267, 0x2268, 0x2269, |
11813 | 0x226A, 0x226B, 0x226E, 0x226F, 0x2270, 0x2271, 0x2272, 0x2273, | 11819 | 0x226A, 0x226B, 0x226E, 0x226F, 0x2270, 0x2271, 0x2272, 0x2273, |
11814 | 0x2274, 0x2275, 0x2276, 0x2277, 0x2278, 0x2279, 0x227A, 0x227B, | 11820 | 0x2274, 0x2275, 0x2276, 0x2277, 0x2278, 0x2279, 0x227A, 0x227B, |
11815 | 0x227C, 0x227D, 0x227E, 0x227F, 0x2280, 0x2281, 0x2282, 0x2283, | 11821 | 0x227C, 0x227D, 0x227E, 0x227F, 0x2280, 0x2281, 0x2282, 0x2283, |
11816 | 0x2284, 0x2285, 0x2286, 0x2287, 0x2288, 0x2289, 0x228A, 0x228B, | 11822 | 0x2284, 0x2285, 0x2286, 0x2287, 0x2288, 0x2289, 0x228A, 0x228B, |
11817 | 0x228F, 0x2290, 0x2291, 0x2292, 0x2298, 0x29B8, 0x22A2, 0x22A3, | 11823 | 0x228F, 0x2290, 0x2291, 0x2292, 0x2298, 0x29B8, 0x22A2, 0x22A3, |
11818 | 0x22A6, 0x2ADE, 0x22A8, 0x2AE4, 0x22A9, 0x2AE3, 0x22AB, 0x2AE5, | 11824 | 0x22A6, 0x2ADE, 0x22A8, 0x2AE4, 0x22A9, 0x2AE3, 0x22AB, 0x2AE5, |
11819 | 0x22B0, 0x22B1, 0x22B2, 0x22B3, 0x22B4, 0x22B5, 0x22B6, 0x22B7, | 11825 | 0x22B0, 0x22B1, 0x22B2, 0x22B3, 0x22B4, 0x22B5, 0x22B6, 0x22B7, |
11820 | 0x22C9, 0x22CA, 0x22CB, 0x22CC, 0x22D0, 0x22D1, 0x22D6, 0x22D7, | 11826 | 0x22C9, 0x22CA, 0x22CB, 0x22CC, 0x22D0, 0x22D1, 0x22D6, 0x22D7, |
11821 | 0x22D8, 0x22D9, 0x22DA, 0x22DB, 0x22DC, 0x22DD, 0x22DE, 0x22DF, | 11827 | 0x22D8, 0x22D9, 0x22DA, 0x22DB, 0x22DC, 0x22DD, 0x22DE, 0x22DF, |
11822 | 0x22E0, 0x22E1, 0x22E2, 0x22E3, 0x22E4, 0x22E5, 0x22E6, 0x22E7, | 11828 | 0x22E0, 0x22E1, 0x22E2, 0x22E3, 0x22E4, 0x22E5, 0x22E6, 0x22E7, |
11823 | 0x22E8, 0x22E9, 0x22EA, 0x22EB, 0x22EC, 0x22ED, 0x22F0, 0x22F1, | 11829 | 0x22E8, 0x22E9, 0x22EA, 0x22EB, 0x22EC, 0x22ED, 0x22F0, 0x22F1, |
11824 | 0x22F2, 0x22FA, 0x22F3, 0x22FB, 0x22F4, 0x22FC, 0x22F6, 0x22FD, | 11830 | 0x22F2, 0x22FA, 0x22F3, 0x22FB, 0x22F4, 0x22FC, 0x22F6, 0x22FD, |
11825 | 0x22F7, 0x22FE, 0x2308, 0x2309, 0x230A, 0x230B, 0x2329, 0x232A, | 11831 | 0x22F7, 0x22FE, 0x2308, 0x2309, 0x230A, 0x230B, 0x2329, 0x232A, |
11826 | 0x2768, 0x2769, 0x276A, 0x276B, 0x276C, 0x276D, 0x276E, 0x276F, | 11832 | 0x2768, 0x2769, 0x276A, 0x276B, 0x276C, 0x276D, 0x276E, 0x276F, |
11827 | 0x2770, 0x2771, 0x2772, 0x2773, 0x2774, 0x2775, 0x27D5, 0x27D6, | 11833 | 0x2770, 0x2771, 0x2772, 0x2773, 0x2774, 0x2775, 0x27D5, 0x27D6, |
11828 | 0x27DD, 0x27DE, 0x27E2, 0x27E3, 0x27E4, 0x27E5, 0x27E6, 0x27E7, | 11834 | 0x27DD, 0x27DE, 0x27E2, 0x27E3, 0x27E4, 0x27E5, 0x27E6, 0x27E7, |
11829 | 0x27E8, 0x27E9, 0x27EA, 0x27EB, 0x2983, 0x2984, 0x2985, 0x2986, | 11835 | 0x27E8, 0x27E9, 0x27EA, 0x27EB, 0x2983, 0x2984, 0x2985, 0x2986, |
11830 | 0x2987, 0x2988, 0x2989, 0x298A, 0x298B, 0x298C, 0x298D, 0x2990, | 11836 | 0x2987, 0x2988, 0x2989, 0x298A, 0x298B, 0x298C, 0x298D, 0x2990, |
11831 | 0x298E, 0x298F, 0x2991, 0x2992, 0x2993, 0x2994, 0x2995, 0x2996, | 11837 | 0x298E, 0x298F, 0x2991, 0x2992, 0x2993, 0x2994, 0x2995, 0x2996, |
11832 | 0x2997, 0x2998, 0x29C0, 0x29C1, 0x29C4, 0x29C5, 0x29CF, 0x29D0, | 11838 | 0x2997, 0x2998, 0x29C0, 0x29C1, 0x29C4, 0x29C5, 0x29CF, 0x29D0, |
11833 | 0x29D1, 0x29D2, 0x29D4, 0x29D5, 0x29D8, 0x29D9, 0x29DA, 0x29DB, | 11839 | 0x29D1, 0x29D2, 0x29D4, 0x29D5, 0x29D8, 0x29D9, 0x29DA, 0x29DB, |
11834 | 0x29F8, 0x29F9, 0x29FC, 0x29FD, 0x2A2B, 0x2A2C, 0x2A34, 0x2A35, | 11840 | 0x29F8, 0x29F9, 0x29FC, 0x29FD, 0x2A2B, 0x2A2C, 0x2A34, 0x2A35, |
11835 | 0x2A3C, 0x2A3D, 0x2A64, 0x2A65, 0x2A79, 0x2A7A, 0x2A7D, 0x2A7E, | 11841 | 0x2A3C, 0x2A3D, 0x2A64, 0x2A65, 0x2A79, 0x2A7A, 0x2A7D, 0x2A7E, |
11836 | 0x2A7F, 0x2A80, 0x2A81, 0x2A82, 0x2A83, 0x2A84, 0x2A8B, 0x2A8C, | 11842 | 0x2A7F, 0x2A80, 0x2A81, 0x2A82, 0x2A83, 0x2A84, 0x2A8B, 0x2A8C, |
11837 | 0x2A91, 0x2A92, 0x2A93, 0x2A94, 0x2A95, 0x2A96, 0x2A97, 0x2A98, | 11843 | 0x2A91, 0x2A92, 0x2A93, 0x2A94, 0x2A95, 0x2A96, 0x2A97, 0x2A98, |
11838 | 0x2A99, 0x2A9A, 0x2A9B, 0x2A9C, 0x2AA1, 0x2AA2, 0x2AA6, 0x2AA7, | 11844 | 0x2A99, 0x2A9A, 0x2A9B, 0x2A9C, 0x2AA1, 0x2AA2, 0x2AA6, 0x2AA7, |
11839 | 0x2AA8, 0x2AA9, 0x2AAA, 0x2AAB, 0x2AAC, 0x2AAD, 0x2AAF, 0x2AB0, | 11845 | 0x2AA8, 0x2AA9, 0x2AAA, 0x2AAB, 0x2AAC, 0x2AAD, 0x2AAF, 0x2AB0, |
11840 | 0x2AB3, 0x2AB4, 0x2ABB, 0x2ABC, 0x2ABD, 0x2ABE, 0x2ABF, 0x2AC0, | 11846 | 0x2AB3, 0x2AB4, 0x2ABB, 0x2ABC, 0x2ABD, 0x2ABE, 0x2ABF, 0x2AC0, |
11841 | 0x2AC1, 0x2AC2, 0x2AC3, 0x2AC4, 0x2AC5, 0x2AC6, 0x2ACD, 0x2ACE, | 11847 | 0x2AC1, 0x2AC2, 0x2AC3, 0x2AC4, 0x2AC5, 0x2AC6, 0x2ACD, 0x2ACE, |
11842 | 0x2ACF, 0x2AD0, 0x2AD1, 0x2AD2, 0x2AD3, 0x2AD4, 0x2AD5, 0x2AD6, | 11848 | 0x2ACF, 0x2AD0, 0x2AD1, 0x2AD2, 0x2AD3, 0x2AD4, 0x2AD5, 0x2AD6, |
11843 | 0x2AEC, 0x2AED, 0x2AF7, 0x2AF8, 0x2AF9, 0x2AFA, 0x3008, 0x3009, | 11849 | 0x2AEC, 0x2AED, 0x2AF7, 0x2AF8, 0x2AF9, 0x2AFA, 0x3008, 0x3009, |
11844 | 0x300A, 0x300B, 0x300C, 0x300D, 0x300E, 0x300F, 0x3010, 0x3011, | 11850 | 0x300A, 0x300B, 0x300C, 0x300D, 0x300E, 0x300F, 0x3010, 0x3011, |
11845 | 0x3014, 0x3015, 0x3016, 0x3017, 0x3018, 0x3019, 0x301A, 0x301B, | 11851 | 0x3014, 0x3015, 0x3016, 0x3017, 0x3018, 0x3019, 0x301A, 0x301B, |
11846 | 0xFF08, 0xFF09, 0xFF1C, 0xFF1E, 0xFF3B, 0xFF3D, 0xFF5B, 0xFF5D, | 11852 | 0xFF08, 0xFF09, 0xFF1C, 0xFF1E, 0xFF3B, 0xFF3D, 0xFF5B, 0xFF5D, |
11847 | 0xFF5F, 0xFF60, 0xFF62, 0xFF63, | 11853 | 0xFF5F, 0xFF60, 0xFF62, 0xFF63, |
11848 | }; | 11854 | }; |
11849 | 11855 | ||
@@ -11855,97 +11861,97 @@ static const int symmetricPairsSize = | |||
11855 | * ---------------------------------------------------------------------- | 11861 | * ---------------------------------------------------------------------- |
11856 | * End of unicode tables | 11862 | * End of unicode tables |
11857 | * ---------------------------------------------------------------------- | 11863 | * ---------------------------------------------------------------------- |
11858 | */ | 11864 | */ |
11859 | 11865 | ||
11860 | #endif | 11866 | #endif |
11861 | 11867 | ||
11862 | static int ucstrcmp( const QString &as, const QString &bs ) | 11868 | static int ucstrcmp( const QString &as, const QString &bs ) |
11863 | { | 11869 | { |
11864 | const QChar *a = as.unicode(); | 11870 | const QChar *a = as.unicode(); |
11865 | const QChar *b = bs.unicode(); | 11871 | const QChar *b = bs.unicode(); |
11866 | if ( a == b ) | 11872 | if ( a == b ) |
11867 | return 0; | 11873 | return 0; |
11868 | if ( a == 0 ) | 11874 | if ( a == 0 ) |
11869 | return 1; | 11875 | return 1; |
11870 | if ( b == 0 ) | 11876 | if ( b == 0 ) |
11871 | return -1; | 11877 | return -1; |
11872 | int l=QMIN(as.length(),bs.length()); | 11878 | int l=QMIN(as.length(),bs.length()); |
11873 | while ( l-- && *a == *b ) | 11879 | while ( l-- && *a == *b ) |
11874 | a++,b++; | 11880 | a++,b++; |
11875 | if ( l==-1 ) | 11881 | if ( l==-1 ) |
11876 | return ( as.length()-bs.length() ); | 11882 | return ( as.length()-bs.length() ); |
11877 | return a->unicode() - b->unicode(); | 11883 | return a->unicode() - b->unicode(); |
11878 | } | 11884 | } |
11879 | 11885 | ||
11880 | static int ucstrncmp( const QChar *a, const QChar *b, int l ) | 11886 | static int ucstrncmp( const QChar *a, const QChar *b, int l ) |
11881 | { | 11887 | { |
11882 | while ( l-- && *a == *b ) | 11888 | while ( l-- && *a == *b ) |
11883 | a++,b++; | 11889 | a++,b++; |
11884 | if ( l==-1 ) | 11890 | if ( l==-1 ) |
11885 | return 0; | 11891 | return 0; |
11886 | return a->unicode() - b->unicode(); | 11892 | return a->unicode() - b->unicode(); |
11887 | } | 11893 | } |
11888 | 11894 | ||
11889 | static int ucstrnicmp( const QChar *a, const QChar *b, int l ) | 11895 | static int ucstrnicmp( const QChar *a, const QChar *b, int l ) |
11890 | { | 11896 | { |
11891 | while ( l-- && ::lower( *a ) == ::lower( *b ) ) | 11897 | while ( l-- && ::lower( *a ) == ::lower( *b ) ) |
11892 | a++,b++; | 11898 | a++,b++; |
11893 | if ( l==-1 ) | 11899 | if ( l==-1 ) |
11894 | return 0; | 11900 | return 0; |
11895 | return ::lower( *a ).unicode() - ::lower( *b ).unicode(); | 11901 | return ::lower( *a ).unicode() - ::lower( *b ).unicode(); |
11896 | } | 11902 | } |
11897 | 11903 | ||
11898 | static uint computeNewMax( uint len ) | 11904 | static uint computeNewMax( uint len ) |
11899 | { | 11905 | { |
11900 | uint newMax = 4; | 11906 | uint newMax = 4; |
11901 | while ( newMax < len ) | 11907 | while ( newMax < len ) |
11902 | newMax *= 2; | 11908 | newMax *= 2; |
11903 | // try to spare some memory | 11909 | // try to save some memory |
11904 | if ( newMax >= 1024 * 1024 && len <= newMax - (newMax >> 2) ) | 11910 | if ( newMax >= 1024 * 1024 && len <= newMax - (newMax >> 2) ) |
11905 | newMax -= newMax >> 2; | 11911 | newMax -= newMax >> 2; |
11906 | return newMax; | 11912 | return newMax; |
11907 | } | 11913 | } |
11908 | 11914 | ||
11909 | /*! | 11915 | /*! |
11910 | \class QCharRef qstring.h | 11916 | \class QCharRef qstring.h |
11911 | \reentrant | 11917 | \reentrant |
11912 | \brief The QCharRef class is a helper class for QString. | 11918 | \brief The QCharRef class is a helper class for QString. |
11913 | 11919 | ||
11914 | \ingroup text | 11920 | \ingroup text |
11915 | 11921 | ||
11916 | When you get an object of type QCharRef, if you can assign to it, | 11922 | When you get an object of type QCharRef, if you can assign to it, |
11917 | the assignment will apply to the character in the string from | 11923 | the assignment will apply to the character in the string from |
11918 | which you got the reference. That is its whole purpose in life. | 11924 | which you got the reference. That is its whole purpose in life. |
11919 | The QCharRef becomes invalid once modifications are made to the | 11925 | The QCharRef becomes invalid once modifications are made to the |
11920 | string: if you want to keep the character, copy it into a QChar. | 11926 | string: if you want to keep the character, copy it into a QChar. |
11921 | 11927 | ||
11922 | Most of the QChar member functions also exist in QCharRef. | 11928 | Most of the QChar member functions also exist in QCharRef. |
11923 | However, they are not explicitly documented here. | 11929 | However, they are not explicitly documented here. |
11924 | 11930 | ||
11925 | \sa QString::operator[]() QString::at() QChar | 11931 | \sa QString::operator[]() QString::at() QChar |
11926 | */ | 11932 | */ |
11927 | 11933 | ||
11928 | /*! | 11934 | /*! |
11929 | \class QChar qstring.h | 11935 | \class QChar qstring.h |
11930 | \reentrant | 11936 | \reentrant |
11931 | \brief The QChar class provides a lightweight Unicode character. | 11937 | \brief The QChar class provides a lightweight Unicode character. |
11932 | 11938 | ||
11933 | \ingroup text | 11939 | \ingroup text |
11934 | 11940 | ||
11935 | Unicode characters are (so far) 16-bit entities without any markup | 11941 | Unicode characters are (so far) 16-bit entities without any markup |
11936 | or structure. This class represents such an entity. It is | 11942 | or structure. This class represents such an entity. It is |
11937 | lightweight, so it can be used everywhere. Most compilers treat it | 11943 | lightweight, so it can be used everywhere. Most compilers treat it |
11938 | like a "short int". (In a few years it may be necessary to make | 11944 | like a "short int". (In a few years it may be necessary to make |
11939 | QChar 32-bit when more than 65536 Unicode code points have been | 11945 | QChar 32-bit when more than 65536 Unicode code points have been |
11940 | defined and come into use.) | 11946 | defined and come into use.) |
11941 | 11947 | ||
11942 | QChar provides a full complement of testing/classification | 11948 | QChar provides a full complement of testing/classification |
11943 | functions, converting to and from other formats, converting from | 11949 | functions, converting to and from other formats, converting from |
11944 | composed to decomposed Unicode, and trying to compare and | 11950 | composed to decomposed Unicode, and trying to compare and |
11945 | case-convert if you ask it to. | 11951 | case-convert if you ask it to. |
11946 | 11952 | ||
11947 | The classification functions include functions like those in | 11953 | The classification functions include functions like those in |
11948 | ctype.h, but operating on the full range of Unicode characters. | 11954 | ctype.h, but operating on the full range of Unicode characters. |
11949 | They all return TRUE if the character is a certain type of | 11955 | They all return TRUE if the character is a certain type of |
11950 | character; otherwise they return FALSE. These classification | 11956 | character; otherwise they return FALSE. These classification |
11951 | functions are isNull() (returns TRUE if the character is U+0000), | 11957 | functions are isNull() (returns TRUE if the character is U+0000), |
@@ -12846,200 +12852,200 @@ void QString::compose() | |||
12846 | ligature.first(); | 12852 | ligature.first(); |
12847 | while(ligature.current()) { | 12853 | while(ligature.current()) { |
12848 | if ((len = ligature.match(*this, index)) != 0) { | 12854 | if ((len = ligature.match(*this, index)) != 0) { |
12849 | head = ligature.head(); | 12855 | head = ligature.head(); |
12850 | unsigned short code = head.unicode(); | 12856 | unsigned short code = head.unicode(); |
12851 | // we exclude Arabic presentation forms A and a few | 12857 | // we exclude Arabic presentation forms A and a few |
12852 | // other ligatures, which are undefined in most fonts | 12858 | // other ligatures, which are undefined in most fonts |
12853 | if(!(code > 0xfb50 && code < 0xfe80) && | 12859 | if(!(code > 0xfb50 && code < 0xfe80) && |
12854 | !(code > 0xfb00 && code < 0xfb2a)) { | 12860 | !(code > 0xfb00 && code < 0xfb2a)) { |
12855 | // joining info is only needed for Arabic | 12861 | // joining info is only needed for Arabic |
12856 | if (format(ligature.tag(), *this, index, len)) { | 12862 | if (format(ligature.tag(), *this, index, len)) { |
12857 | //printf("using ligature 0x%x, len=%d\n",code,len); | 12863 | //printf("using ligature 0x%x, len=%d\n",code,len); |
12858 | // replace letter | 12864 | // replace letter |
12859 | composed.replace(cindex, len, QChar(head)); | 12865 | composed.replace(cindex, len, QChar(head)); |
12860 | index += len-1; | 12866 | index += len-1; |
12861 | // we continue searching in case we have a final | 12867 | // we continue searching in case we have a final |
12862 | // form because medial ones are preferred. | 12868 | // form because medial ones are preferred. |
12863 | if ( len != 1 || ligature.tag() !=QChar::Final ) | 12869 | if ( len != 1 || ligature.tag() !=QChar::Final ) |
12864 | break; | 12870 | break; |
12865 | } | 12871 | } |
12866 | } | 12872 | } |
12867 | } | 12873 | } |
12868 | ligature.next(); | 12874 | ligature.next(); |
12869 | } | 12875 | } |
12870 | cindex++; | 12876 | cindex++; |
12871 | index++; | 12877 | index++; |
12872 | } | 12878 | } |
12873 | *this = composed; | 12879 | *this = composed; |
12874 | #endif | 12880 | #endif |
12875 | } | 12881 | } |
12876 | 12882 | ||
12877 | 12883 | ||
12878 | // These macros are used for efficient allocation of QChar strings. | 12884 | // These macros are used for efficient allocation of QChar strings. |
12879 | // IMPORTANT! If you change these, make sure you also change the | 12885 | // IMPORTANT! If you change these, make sure you also change the |
12880 | // "delete unicode" statement in ~QStringData() in qstring.h correspondingly! | 12886 | // "delete unicode" statement in ~QStringData() in qstring.h correspondingly! |
12881 | 12887 | ||
12882 | #define QT_ALLOC_QCHAR_VEC( N ) (QChar*) new char[ sizeof(QChar)*( N ) ] | 12888 | #define QT_ALLOC_QCHAR_VEC( N ) (QChar*) new char[ sizeof(QChar)*( N ) ] |
12883 | #define QT_DELETE_QCHAR_VEC( P ) delete[] ((char*)( P )) | 12889 | #define QT_DELETE_QCHAR_VEC( P ) delete[] ((char*)( P )) |
12884 | 12890 | ||
12885 | 12891 | ||
12886 | /*! | 12892 | /*! |
12887 | This utility function converts the 8-bit string \a ba to Unicode, | 12893 | This utility function converts the 8-bit string \a ba to Unicode, |
12888 | returning the result. | 12894 | returning the result. |
12889 | 12895 | ||
12890 | The caller is responsible for deleting the return value with | 12896 | The caller is responsible for deleting the return value with |
12891 | delete[]. | 12897 | delete[]. |
12892 | */ | 12898 | */ |
12893 | 12899 | ||
12894 | QChar* QString::asciiToUnicode( const QByteArray& ba, uint* len ) | 12900 | QChar* QString::latin1ToUnicode( const QByteArray& ba, uint* len ) |
12895 | { | 12901 | { |
12896 | if ( ba.isNull() ) { | 12902 | if ( ba.isNull() ) { |
12897 | *len = 0; | 12903 | *len = 0; |
12898 | return 0; | 12904 | return 0; |
12899 | } | 12905 | } |
12900 | int l = 0; | 12906 | int l = 0; |
12901 | while ( l < (int)ba.size() && ba[l] ) | 12907 | while ( l < (int)ba.size() && ba[l] ) |
12902 | l++; | 12908 | l++; |
12903 | char* str = ba.data(); | 12909 | char* str = ba.data(); |
12904 | QChar *uc = new QChar[ l ]; // Can't use macro, since function is public | 12910 | QChar *uc = new QChar[ l ]; // Can't use macro, since function is public |
12905 | QChar *result = uc; | 12911 | QChar *result = uc; |
12906 | if ( len ) | 12912 | if ( len ) |
12907 | *len = l; | 12913 | *len = l; |
12908 | while (l--) | 12914 | while (l--) |
12909 | *uc++ = *str++; | 12915 | *uc++ = *str++; |
12910 | return result; | 12916 | return result; |
12911 | } | 12917 | } |
12912 | 12918 | ||
12913 | static QChar* internalAsciiToUnicode( const QByteArray& ba, uint* len ) | 12919 | static QChar* internalLatin1ToUnicode( const QByteArray& ba, uint* len ) |
12914 | { | 12920 | { |
12915 | if ( ba.isNull() ) { | 12921 | if ( ba.isNull() ) { |
12916 | *len = 0; | 12922 | *len = 0; |
12917 | return 0; | 12923 | return 0; |
12918 | } | 12924 | } |
12919 | int l = 0; | 12925 | int l = 0; |
12920 | while ( l < (int)ba.size() && ba[l] ) | 12926 | while ( l < (int)ba.size() && ba[l] ) |
12921 | l++; | 12927 | l++; |
12922 | char* str = ba.data(); | 12928 | char* str = ba.data(); |
12923 | QChar *uc = QT_ALLOC_QCHAR_VEC( l ); | 12929 | QChar *uc = QT_ALLOC_QCHAR_VEC( l ); |
12924 | QChar *result = uc; | 12930 | QChar *result = uc; |
12925 | if ( len ) | 12931 | if ( len ) |
12926 | *len = l; | 12932 | *len = l; |
12927 | while (l--) | 12933 | while (l--) |
12928 | *uc++ = *str++; | 12934 | *uc++ = *str++; |
12929 | return result; | 12935 | return result; |
12930 | } | 12936 | } |
12931 | 12937 | ||
12932 | /*! | 12938 | /*! |
12933 | \overload | 12939 | \overload |
12934 | 12940 | ||
12935 | This utility function converts the '\0'-terminated 8-bit string \a | 12941 | This utility function converts the '\0'-terminated 8-bit string \a |
12936 | str to Unicode, returning the result and setting \a *len to the | 12942 | str to Unicode, returning the result and setting \a *len to the |
12937 | length of the Unicode string. | 12943 | length of the Unicode string. |
12938 | 12944 | ||
12939 | The caller is responsible for deleting the return value with | 12945 | The caller is responsible for deleting the return value with |
12940 | delete[]. | 12946 | delete[]. |
12941 | */ | 12947 | */ |
12942 | 12948 | ||
12943 | QChar* QString::asciiToUnicode( const char *str, uint* len, uint maxlen ) | 12949 | QChar* QString::latin1ToUnicode( const char *str, uint* len, uint maxlen ) |
12944 | { | 12950 | { |
12945 | QChar* result = 0; | 12951 | QChar* result = 0; |
12946 | uint l = 0; | 12952 | uint l = 0; |
12947 | if ( str ) { | 12953 | if ( str ) { |
12948 | if ( maxlen != (uint)-1 ) { | 12954 | if ( maxlen != (uint)-1 ) { |
12949 | while ( l < maxlen && str[l] ) | 12955 | while ( l < maxlen && str[l] ) |
12950 | l++; | 12956 | l++; |
12951 | } else { | 12957 | } else { |
12952 | // Faster? | 12958 | // Faster? |
12953 | l = qstrlen(str); | 12959 | l = strlen( str ); |
12954 | } | 12960 | } |
12955 | QChar *uc = new QChar[ l ]; // Can't use macro since function is public | 12961 | QChar *uc = new QChar[ l ]; // Can't use macro since function is public |
12956 | result = uc; | 12962 | result = uc; |
12957 | uint i = l; | 12963 | uint i = l; |
12958 | while ( i-- ) | 12964 | while ( i-- ) |
12959 | *uc++ = *str++; | 12965 | *uc++ = *str++; |
12960 | } | 12966 | } |
12961 | if ( len ) | 12967 | if ( len ) |
12962 | *len = l; | 12968 | *len = l; |
12963 | return result; | 12969 | return result; |
12964 | } | 12970 | } |
12965 | 12971 | ||
12966 | static QChar* internalAsciiToUnicode( const char *str, uint* len, | 12972 | static QChar* internalLatin1ToUnicode( const char *str, uint* len, |
12967 | uint maxlen = (uint)-1 ) | 12973 | uint maxlen = (uint)-1 ) |
12968 | { | 12974 | { |
12969 | QChar* result = 0; | 12975 | QChar* result = 0; |
12970 | uint l = 0; | 12976 | uint l = 0; |
12971 | if ( str ) { | 12977 | if ( str ) { |
12972 | if ( maxlen != (uint)-1 ) { | 12978 | if ( maxlen != (uint)-1 ) { |
12973 | while ( l < maxlen && str[l] ) | 12979 | while ( l < maxlen && str[l] ) |
12974 | l++; | 12980 | l++; |
12975 | } else { | 12981 | } else { |
12976 | // Faster? | 12982 | // Faster? |
12977 | l = qstrlen(str); | 12983 | l = strlen( str ); |
12978 | } | 12984 | } |
12979 | QChar *uc = QT_ALLOC_QCHAR_VEC( l ); | 12985 | QChar *uc = QT_ALLOC_QCHAR_VEC( l ); |
12980 | result = uc; | 12986 | result = uc; |
12981 | uint i = l; | 12987 | uint i = l; |
12982 | while ( i-- ) | 12988 | while ( i-- ) |
12983 | *uc++ = *str++; | 12989 | *uc++ = *str++; |
12984 | } | 12990 | } |
12985 | if ( len ) | 12991 | if ( len ) |
12986 | *len = l; | 12992 | *len = l; |
12987 | return result; | 12993 | return result; |
12988 | } | 12994 | } |
12989 | 12995 | ||
12990 | /*! | 12996 | /*! |
12991 | This utility function converts \a l 16-bit characters from \a uc | 12997 | This utility function converts \a l 16-bit characters from \a uc |
12992 | to ASCII, returning a '\0'-terminated string. | 12998 | to ASCII, returning a '\0'-terminated string. |
12993 | 12999 | ||
12994 | The caller is responsible for deleting the resultant string with | 13000 | The caller is responsible for deleting the resultant string with |
12995 | delete[]. | 13001 | delete[]. |
12996 | */ | 13002 | */ |
12997 | char* QString::unicodeToAscii(const QChar *uc, uint l) | 13003 | char* QString::unicodeToLatin1(const QChar *uc, uint l) |
12998 | { | 13004 | { |
12999 | if (!uc) { | 13005 | if (!uc) { |
13000 | return 0; | 13006 | return 0; |
13001 | } | 13007 | } |
13002 | char *a = new char[l+1]; | 13008 | char *a = new char[l+1]; |
13003 | char *result = a; | 13009 | char *result = a; |
13004 | while (l--) { | 13010 | while (l--) { |
13005 | *a++ = (uc->unicode() > 0xff) ? '?' : (char)uc->unicode(); | 13011 | *a++ = (uc->unicode() > 0xff) ? '?' : (char)uc->unicode(); |
13006 | uc++; | 13012 | uc++; |
13007 | } | 13013 | } |
13008 | *a = '\0'; | 13014 | *a = '\0'; |
13009 | return result; | 13015 | return result; |
13010 | } | 13016 | } |
13011 | 13017 | ||
13012 | /***************************************************************************** | 13018 | /***************************************************************************** |
13013 | QString member functions | 13019 | QString member functions |
13014 | *****************************************************************************/ | 13020 | *****************************************************************************/ |
13015 | 13021 | ||
13016 | /*! | 13022 | /*! |
13017 | \class QString qstring.h | 13023 | \class QString qstring.h |
13018 | \reentrant | 13024 | \reentrant |
13019 | 13025 | ||
13020 | \brief The QString class provides an abstraction of Unicode text | 13026 | \brief The QString class provides an abstraction of Unicode text |
13021 | and the classic C '\0'-terminated char array. | 13027 | and the classic C '\0'-terminated char array. |
13022 | 13028 | ||
13023 | \ingroup tools | 13029 | \ingroup tools |
13024 | \ingroup shared | 13030 | \ingroup shared |
13025 | \ingroup text | 13031 | \ingroup text |
13026 | \mainclass | 13032 | \mainclass |
13027 | 13033 | ||
13028 | QString uses \link shclass.html implicit sharing\endlink, which | 13034 | QString uses \link shclass.html implicit sharing\endlink, which |
13029 | makes it very efficient and easy to use. | 13035 | makes it very efficient and easy to use. |
13030 | 13036 | ||
13031 | In all of the QString methods that take \c {const char *} | 13037 | In all of the QString methods that take \c {const char *} |
13032 | parameters, the \c {const char *} is interpreted as a classic | 13038 | parameters, the \c {const char *} is interpreted as a classic |
13033 | C-style '\0'-terminated ASCII string. It is legal for the \c | 13039 | C-style '\0'-terminated ASCII string. It is legal for the \c |
13034 | {const char *} parameter to be 0. If the \c {const char *} is not | 13040 | {const char *} parameter to be 0. If the \c {const char *} is not |
13035 | '\0'-terminated, the results are undefined. Functions that copy | 13041 | '\0'-terminated, the results are undefined. Functions that copy |
13036 | classic C strings into a QString will not copy the terminating | 13042 | classic C strings into a QString will not copy the terminating |
13037 | '\0' character. The QChar array of the QString (as returned by | 13043 | '\0' character. The QChar array of the QString (as returned by |
13038 | unicode()) is generally not terminated by a '\0'. If you need to | 13044 | unicode()) is generally not terminated by a '\0'. If you need to |
13039 | pass a QString to a function that requires a C '\0'-terminated | 13045 | pass a QString to a function that requires a C '\0'-terminated |
13040 | string use latin1(). | 13046 | string use latin1(). |
13041 | 13047 | ||
13042 | \keyword QString::null | 13048 | \keyword QString::null |
13043 | A QString that has not been assigned to anything is \e null, i.e. | 13049 | A QString that has not been assigned to anything is \e null, i.e. |
13044 | both the length and data pointer is 0. A QString that references | 13050 | both the length and data pointer is 0. A QString that references |
13045 | the empty string ("", a single '\0' char) is \e empty. Both null | 13051 | the empty string ("", a single '\0' char) is \e empty. Both null |
@@ -13112,484 +13118,488 @@ char* QString::unicodeToAscii(const QChar *uc, uint l) | |||
13112 | 13118 | ||
13113 | \sa QChar QCString QByteArray QConstString | 13119 | \sa QChar QCString QByteArray QConstString |
13114 | */ | 13120 | */ |
13115 | 13121 | ||
13116 | /*! \enum Qt::ComparisonFlags | 13122 | /*! \enum Qt::ComparisonFlags |
13117 | \internal | 13123 | \internal |
13118 | */ | 13124 | */ |
13119 | /*! | 13125 | /*! |
13120 | \enum Qt::StringComparisonMode | 13126 | \enum Qt::StringComparisonMode |
13121 | 13127 | ||
13122 | This enum type is used to set the string comparison mode when | 13128 | This enum type is used to set the string comparison mode when |
13123 | searching for an item. It is used by QListBox, QListView and | 13129 | searching for an item. It is used by QListBox, QListView and |
13124 | QIconView, for example. We'll refer to the string being searched | 13130 | QIconView, for example. We'll refer to the string being searched |
13125 | as the 'target' string. | 13131 | as the 'target' string. |
13126 | 13132 | ||
13127 | \value CaseSensitive The strings must match case sensitively. | 13133 | \value CaseSensitive The strings must match case sensitively. |
13128 | \value ExactMatch The target and search strings must match exactly. | 13134 | \value ExactMatch The target and search strings must match exactly. |
13129 | \value BeginsWith The target string begins with the search string. | 13135 | \value BeginsWith The target string begins with the search string. |
13130 | \value EndsWith The target string ends with the search string. | 13136 | \value EndsWith The target string ends with the search string. |
13131 | \value Contains The target string contains the search string. | 13137 | \value Contains The target string contains the search string. |
13132 | 13138 | ||
13133 | If you OR these flags together (excluding \c CaseSensitive), the | 13139 | If you OR these flags together (excluding \c CaseSensitive), the |
13134 | search criteria be applied in the following order: \c ExactMatch, | 13140 | search criteria be applied in the following order: \c ExactMatch, |
13135 | \c BeginsWith, \c EndsWith, \c Contains. | 13141 | \c BeginsWith, \c EndsWith, \c Contains. |
13136 | 13142 | ||
13137 | Matching is case-insensitive unless \c CaseSensitive is set. \c | 13143 | Matching is case-insensitive unless \c CaseSensitive is set. \c |
13138 | CaseSensitive may be OR-ed with any combination of the other | 13144 | CaseSensitive may be OR-ed with any combination of the other |
13139 | flags. | 13145 | flags. |
13140 | 13146 | ||
13141 | */ | 13147 | */ |
13142 | Q_EXPORT QStringData *QString::shared_null = 0; | 13148 | Q_EXPORT QStringData *QString::shared_null = 0; |
13143 | QT_STATIC_CONST_IMPL QString QString::null; | 13149 | QT_STATIC_CONST_IMPL QString QString::null; |
13144 | QT_STATIC_CONST_IMPL QChar QChar::null; | 13150 | QT_STATIC_CONST_IMPL QChar QChar::null; |
13145 | QT_STATIC_CONST_IMPL QChar QChar::replacement((ushort)0xfffd); | 13151 | QT_STATIC_CONST_IMPL QChar QChar::replacement((ushort)0xfffd); |
13146 | QT_STATIC_CONST_IMPL QChar QChar::byteOrderMark((ushort)0xfeff); | 13152 | QT_STATIC_CONST_IMPL QChar QChar::byteOrderMark((ushort)0xfeff); |
13147 | QT_STATIC_CONST_IMPL QChar QChar::byteOrderSwapped((ushort)0xfffe); | 13153 | QT_STATIC_CONST_IMPL QChar QChar::byteOrderSwapped((ushort)0xfffe); |
13148 | QT_STATIC_CONST_IMPL QChar QChar::nbsp((ushort)0x00a0); | 13154 | QT_STATIC_CONST_IMPL QChar QChar::nbsp((ushort)0x00a0); |
13149 | 13155 | ||
13150 | QStringData* QString::makeSharedNull() | 13156 | QStringData* QString::makeSharedNull() |
13151 | { | 13157 | { |
13152 | QString::shared_null = new QStringData; | 13158 | QString::shared_null = new QStringData; |
13153 | #if defined( Q_OS_MAC ) | 13159 | #if defined( Q_OS_MAC ) |
13154 | QString *that = const_cast<QString *>(&QString::null); | 13160 | QString *that = const_cast<QString *>(&QString::null); |
13155 | that->d = QString::shared_null; | 13161 | that->d = QString::shared_null; |
13156 | #endif | 13162 | #endif |
13157 | return QString::shared_null; | 13163 | return QString::shared_null; |
13158 | } | 13164 | } |
13159 | 13165 | ||
13160 | // Uncomment this to get some useful statistics. | ||
13161 | // #define Q2HELPER(x) x | ||
13162 | |||
13163 | #ifdef Q2HELPER | ||
13164 | static int stat_construct_charstar=0; | ||
13165 | static int stat_construct_charstar_size=0; | ||
13166 | static int stat_construct_null=0; | ||
13167 | static int stat_construct_int=0; | ||
13168 | static int stat_construct_int_size=0; | ||
13169 | static int stat_construct_ba=0; | ||
13170 | static int stat_get_ascii=0; | ||
13171 | static int stat_get_ascii_size=0; | ||
13172 | static int stat_copy_on_write=0; | ||
13173 | static int stat_copy_on_write_size=0; | ||
13174 | static int stat_fast_copy=0; | ||
13175 | Q_EXPORT void qt_qstring_stats() | ||
13176 | { | ||
13177 | qDebug("construct_charstar = %d (%d chars)", stat_construct_charstar, stat_construct_charstar_size); | ||
13178 | qDebug("construct_null = %d", stat_construct_null); | ||
13179 | qDebug("construct_int = %d (%d chars)", stat_construct_int, stat_construct_int_size); | ||
13180 | qDebug("construct_ba = %d", stat_construct_ba); | ||
13181 | qDebug("get_ascii = %d (%d chars)", stat_get_ascii, stat_get_ascii_size); | ||
13182 | qDebug("copy_on_write = %d (%d chars)", stat_copy_on_write, stat_copy_on_write_size); | ||
13183 | qDebug("fast_copy = %d", stat_fast_copy); | ||
13184 | } | ||
13185 | #else | ||
13186 | #define Q2HELPER(x) | ||
13187 | #endif | ||
13188 | |||
13189 | /*! | 13166 | /*! |
13190 | \fn QString::QString() | 13167 | \fn QString::QString() |
13191 | 13168 | ||
13192 | Constructs a null string, i.e. both the length and data pointer | 13169 | Constructs a null string, i.e. both the length and data pointer |
13193 | are 0. | 13170 | are 0. |
13194 | 13171 | ||
13195 | \sa isNull() | 13172 | \sa isNull() |
13196 | */ | 13173 | */ |
13197 | 13174 | ||
13198 | /*! | 13175 | /*! |
13199 | Constructs a string of length one, containing the character \a ch. | 13176 | Constructs a string of length one, containing the character \a ch. |
13200 | */ | 13177 | */ |
13201 | QString::QString( QChar ch ) | 13178 | QString::QString( QChar ch ) |
13202 | { | 13179 | { |
13203 | d = new QStringData( QT_ALLOC_QCHAR_VEC( 1 ), 1, 1 ); | 13180 | d = new QStringData( QT_ALLOC_QCHAR_VEC( 1 ), 1, 1 ); |
13204 | d->unicode[0] = ch; | 13181 | d->unicode[0] = ch; |
13205 | } | 13182 | } |
13206 | 13183 | ||
13207 | /*! | 13184 | /*! |
13208 | Constructs an implicitly shared copy of \a s. This is very fast | 13185 | Constructs an implicitly shared copy of \a s. This is very fast |
13209 | since it only involves incrementing a reference count. | 13186 | since it only involves incrementing a reference count. |
13210 | */ | 13187 | */ |
13211 | QString::QString( const QString &s ) : | 13188 | QString::QString( const QString &s ) : |
13212 | d(s.d) | 13189 | d(s.d) |
13213 | { | 13190 | { |
13214 | Q2HELPER(stat_fast_copy++) | ||
13215 | d->ref(); | 13191 | d->ref(); |
13216 | } | 13192 | } |
13217 | 13193 | ||
13218 | /*! | 13194 | /*! |
13219 | \internal | 13195 | \internal |
13220 | 13196 | ||
13221 | Private function. | 13197 | Private function. |
13222 | 13198 | ||
13223 | Constructs a string with preallocated space for \a size characters. | 13199 | Constructs a string with preallocated space for \a size characters. |
13224 | 13200 | ||
13225 | The string is empty. | 13201 | The string is empty. |
13226 | 13202 | ||
13227 | \sa isNull() | 13203 | \sa isNull() |
13228 | */ | 13204 | */ |
13229 | 13205 | ||
13230 | QString::QString( int size, bool /*dummy*/ ) | 13206 | QString::QString( int size, bool /*dummy*/ ) |
13231 | { | 13207 | { |
13232 | if ( size ) { | 13208 | if ( size ) { |
13233 | Q2HELPER(stat_construct_int++) | ||
13234 | int l = size; | 13209 | int l = size; |
13235 | Q2HELPER(stat_construct_int_size+=l) | ||
13236 | QChar* uc = QT_ALLOC_QCHAR_VEC( l ); | 13210 | QChar* uc = QT_ALLOC_QCHAR_VEC( l ); |
13237 | d = new QStringData( uc, 0, l ); | 13211 | d = new QStringData( uc, 0, l ); |
13238 | } else { | 13212 | } else { |
13239 | Q2HELPER(stat_construct_null++) | ||
13240 | d = shared_null ? shared_null : (shared_null=new QStringData); | 13213 | d = shared_null ? shared_null : (shared_null=new QStringData); |
13241 | d->ref(); | 13214 | d->ref(); |
13242 | } | 13215 | } |
13243 | } | 13216 | } |
13244 | 13217 | ||
13245 | /*! | 13218 | /*! |
13246 | Constructs a string that is a deep copy of \a ba interpreted as a | 13219 | Constructs a string that is a deep copy of \a ba interpreted as a |
13247 | classic C string. | 13220 | classic C string. |
13248 | */ | 13221 | */ |
13249 | 13222 | ||
13250 | QString::QString( const QByteArray& ba ) | 13223 | QString::QString( const QByteArray& ba ) |
13251 | { | 13224 | { |
13252 | Q2HELPER(stat_construct_ba++) | 13225 | #ifndef QT_NO_TEXTCODEC |
13226 | if ( QTextCodec::codecForCStrings() ) { | ||
13227 | d = 0; | ||
13228 | *this = fromAscii( ba.data(), ba.size() ); | ||
13229 | return; | ||
13230 | } | ||
13231 | #endif | ||
13253 | uint l; | 13232 | uint l; |
13254 | QChar *uc = internalAsciiToUnicode(ba,&l); | 13233 | QChar *uc = internalLatin1ToUnicode(ba,&l); |
13255 | d = new QStringData(uc,l,l); | 13234 | d = new QStringData(uc,l,l); |
13256 | } | 13235 | } |
13257 | 13236 | ||
13258 | /*! | 13237 | /*! |
13259 | Constructs a string that is a deep copy of the first \a length | 13238 | Constructs a string that is a deep copy of the first \a length |
13260 | characters in the QChar array. | 13239 | characters in the QChar array. |
13261 | 13240 | ||
13262 | If \a unicode and \a length are 0, then a null string is created. | 13241 | If \a unicode and \a length are 0, then a null string is created. |
13263 | 13242 | ||
13264 | If only \a unicode is 0, the string is empty but has \a length | 13243 | If only \a unicode is 0, the string is empty but has \a length |
13265 | characters of space preallocated: QString expands automatically | 13244 | characters of space preallocated: QString expands automatically |
13266 | anyway, but this may speed up some cases a little. We recommend | 13245 | anyway, but this may speed up some cases a little. We recommend |
13267 | using the plain constructor and setLength() for this purpose since | 13246 | using the plain constructor and setLength() for this purpose since |
13268 | it will result in more readable code. | 13247 | it will result in more readable code. |
13269 | 13248 | ||
13270 | \sa isNull() setLength() | 13249 | \sa isNull() setLength() |
13271 | */ | 13250 | */ |
13272 | 13251 | ||
13273 | QString::QString( const QChar* unicode, uint length ) | 13252 | QString::QString( const QChar* unicode, uint length ) |
13274 | { | 13253 | { |
13275 | if ( !unicode && !length ) { | 13254 | if ( !unicode && !length ) { |
13276 | d = shared_null ? shared_null : makeSharedNull(); | 13255 | d = shared_null ? shared_null : makeSharedNull(); |
13277 | d->ref(); | 13256 | d->ref(); |
13278 | } else { | 13257 | } else { |
13279 | QChar* uc = QT_ALLOC_QCHAR_VEC( length ); | 13258 | QChar* uc = QT_ALLOC_QCHAR_VEC( length ); |
13280 | if ( unicode ) | 13259 | if ( unicode ) |
13281 | memcpy(uc, unicode, length*sizeof(QChar)); | 13260 | memcpy(uc, unicode, length*sizeof(QChar)); |
13282 | d = new QStringData(uc,unicode ? length : 0,length); | 13261 | d = new QStringData(uc,unicode ? length : 0,length); |
13283 | } | 13262 | } |
13284 | } | 13263 | } |
13285 | 13264 | ||
13286 | /*! | 13265 | /*! |
13287 | Constructs a string that is a deep copy of \a str, interpreted as | 13266 | Constructs a string that is a deep copy of \a str, interpreted as |
13288 | a classic C string. | 13267 | a classic C string. |
13289 | 13268 | ||
13290 | If \a str is 0, then a null string is created. | 13269 | If \a str is 0, then a null string is created. |
13291 | 13270 | ||
13292 | This is a cast constructor, but it is perfectly safe: converting a | 13271 | This is a cast constructor, but it is perfectly safe: converting a |
13293 | Latin1 const char* to QString preserves all the information. You | 13272 | Latin1 const char* to QString preserves all the information. You |
13294 | can disable this constructor by defining \c QT_NO_CAST_ASCII when | 13273 | can disable this constructor by defining \c QT_NO_CAST_ASCII when |
13295 | you compile your applications. You can also make QString objects | 13274 | you compile your applications. You can also make QString objects |
13296 | by using setLatin1(), fromLatin1(), fromLocal8Bit(), and | 13275 | by using setLatin1(), fromLatin1(), fromLocal8Bit(), and |
13297 | fromUtf8(). Or whatever encoding is appropriate for the 8-bit data | 13276 | fromUtf8(). Or whatever encoding is appropriate for the 8-bit data |
13298 | you have. | 13277 | you have. |
13299 | 13278 | ||
13300 | \sa isNull() | 13279 | \sa isNull() |
13301 | */ | 13280 | */ |
13302 | 13281 | ||
13303 | QString::QString( const char *str ) | 13282 | QString::QString( const char *str ) |
13304 | { | 13283 | { |
13305 | Q2HELPER(stat_construct_charstar++) | 13284 | #ifndef QT_NO_TEXTCODEC |
13285 | if ( QTextCodec::codecForCStrings() ) { | ||
13286 | d = 0; | ||
13287 | *this = fromAscii( str ); | ||
13288 | return; | ||
13289 | } | ||
13290 | #endif | ||
13291 | uint l; | ||
13292 | QChar *uc = internalLatin1ToUnicode(str,&l); | ||
13293 | d = new QStringData(uc,l,l); | ||
13294 | } | ||
13295 | |||
13296 | #ifndef QT_NO_STL | ||
13297 | /*! | ||
13298 | Constructs a string that is a deep copy of \a str. | ||
13299 | |||
13300 | This is the same as fromAscii(\a str). | ||
13301 | */ | ||
13302 | |||
13303 | QString::QString( const std::string &str ) | ||
13304 | { | ||
13305 | #ifndef QT_NO_TEXTCODEC | ||
13306 | if ( QTextCodec::codecForCStrings() ) { | ||
13307 | d = 0; | ||
13308 | *this = fromAscii( str.c_str() ); | ||
13309 | return; | ||
13310 | } | ||
13311 | #endif | ||
13306 | uint l; | 13312 | uint l; |
13307 | QChar *uc = internalAsciiToUnicode(str,&l); | 13313 | QChar *uc = internalLatin1ToUnicode(str.c_str(),&l); |
13308 | Q2HELPER(stat_construct_charstar_size+=l) | ||
13309 | d = new QStringData(uc,l,l); | 13314 | d = new QStringData(uc,l,l); |
13310 | } | 13315 | } |
13316 | #endif | ||
13311 | 13317 | ||
13312 | /*! | 13318 | /*! |
13313 | \fn QString::~QString() | 13319 | \fn QString::~QString() |
13314 | 13320 | ||
13315 | Destroys the string and frees the string's data if this is the | 13321 | Destroys the string and frees the string's data if this is the |
13316 | last reference to the string. | 13322 | last reference to the string. |
13317 | */ | 13323 | */ |
13318 | 13324 | ||
13319 | 13325 | ||
13320 | /*! | 13326 | /*! |
13321 | Deallocates any space reserved solely by this QString. | 13327 | Deallocates any space reserved solely by this QString. |
13322 | 13328 | ||
13323 | If the string does not share its data with another QString | 13329 | If the string does not share its data with another QString |
13324 | instance, nothing happens; otherwise the function creates a new, | 13330 | instance, nothing happens; otherwise the function creates a new, |
13325 | unique copy of this string. This function is called whenever the | 13331 | unique copy of this string. This function is called whenever the |
13326 | string is modified. | 13332 | string is modified. |
13327 | */ | 13333 | */ |
13328 | 13334 | ||
13329 | void QString::real_detach() | 13335 | void QString::real_detach() |
13330 | { | 13336 | { |
13331 | setLength( length() ); | 13337 | setLength( length() ); |
13332 | } | 13338 | } |
13333 | 13339 | ||
13334 | void QString::deref() | 13340 | void QString::deref() |
13335 | { | 13341 | { |
13336 | if ( d->deref() ) { | 13342 | if ( d && d->deref() ) { |
13337 | if ( d != shared_null ) | 13343 | if ( d != shared_null ) |
13338 | delete d; | 13344 | delete d; |
13339 | d = 0; // helps debugging | 13345 | d = 0; |
13340 | } | 13346 | } |
13341 | } | 13347 | } |
13342 | 13348 | ||
13343 | void QStringData::deleteSelf() | 13349 | void QStringData::deleteSelf() |
13344 | { | 13350 | { |
13345 | delete this; | 13351 | delete this; |
13346 | } | 13352 | } |
13347 | 13353 | ||
13348 | /*! | 13354 | /*! |
13349 | \fn QString& QString::operator=( QChar c ) | 13355 | \fn QString& QString::operator=( QChar c ) |
13350 | 13356 | ||
13351 | Sets the string to contain just the single character \a c. | 13357 | Sets the string to contain just the single character \a c. |
13352 | */ | 13358 | */ |
13353 | 13359 | ||
13354 | /*! | 13360 | /*! |
13361 | \fn QString& QString::operator=( const std::string& s ) | ||
13362 | |||
13363 | \overload | ||
13364 | |||
13365 | Makes a deep copy of \a s and returns a reference to the deep | ||
13366 | copy. | ||
13367 | */ | ||
13368 | |||
13369 | /*! | ||
13355 | \fn QString& QString::operator=( char c ) | 13370 | \fn QString& QString::operator=( char c ) |
13356 | 13371 | ||
13357 | \overload | 13372 | \overload |
13358 | 13373 | ||
13359 | Sets the string to contain just the single character \a c. | 13374 | Sets the string to contain just the single character \a c. |
13360 | */ | 13375 | */ |
13361 | 13376 | ||
13362 | /*! | 13377 | /*! |
13363 | \overload | 13378 | \overload |
13364 | 13379 | ||
13365 | Assigns a shallow copy of \a s to this string and returns a | 13380 | Assigns a shallow copy of \a s to this string and returns a |
13366 | reference to this string. This is very fast because the string | 13381 | reference to this string. This is very fast because the string |
13367 | isn't actually copied. | 13382 | isn't actually copied. |
13368 | */ | 13383 | */ |
13369 | QString &QString::operator=( const QString &s ) | 13384 | QString &QString::operator=( const QString &s ) |
13370 | { | 13385 | { |
13371 | Q2HELPER(stat_fast_copy++) | ||
13372 | s.d->ref(); | 13386 | s.d->ref(); |
13373 | deref(); | 13387 | deref(); |
13374 | d = s.d; | 13388 | d = s.d; |
13375 | return *this; | 13389 | return *this; |
13376 | } | 13390 | } |
13377 | 13391 | ||
13378 | /*! | 13392 | /*! |
13379 | \overload | 13393 | \overload |
13380 | 13394 | ||
13381 | Assigns a deep copy of \a cs, interpreted as a classic C string, | 13395 | Assigns a deep copy of \a cs, interpreted as a classic C string, |
13382 | to this string and returns a reference to this string. | 13396 | to this string and returns a reference to this string. |
13383 | */ | 13397 | */ |
13384 | QString &QString::operator=( const QCString& cs ) | 13398 | QString &QString::operator=( const QCString& cs ) |
13385 | { | 13399 | { |
13386 | return setLatin1(cs); | 13400 | return setAscii(cs); |
13387 | } | 13401 | } |
13388 | 13402 | ||
13389 | 13403 | ||
13390 | /*! | 13404 | /*! |
13391 | \overload | 13405 | \overload |
13392 | 13406 | ||
13393 | Assigns a deep copy of \a str, interpreted as a classic C string | 13407 | Assigns a deep copy of \a str, interpreted as a classic C string |
13394 | to this string and returns a reference to this string. | 13408 | to this string and returns a reference to this string. |
13395 | 13409 | ||
13396 | If \a str is 0, then a null string is created. | 13410 | If \a str is 0, then a null string is created. |
13397 | 13411 | ||
13398 | \sa isNull() | 13412 | \sa isNull() |
13399 | */ | 13413 | */ |
13400 | QString &QString::operator=( const char *str ) | 13414 | QString &QString::operator=( const char *str ) |
13401 | { | 13415 | { |
13402 | return setLatin1(str); | 13416 | return setAscii(str); |
13403 | } | 13417 | } |
13404 | 13418 | ||
13405 | 13419 | ||
13406 | /*! | 13420 | /*! |
13407 | \fn bool QString::isNull() const | 13421 | \fn bool QString::isNull() const |
13408 | 13422 | ||
13409 | Returns TRUE if the string is null; otherwise returns FALSE. A | 13423 | Returns TRUE if the string is null; otherwise returns FALSE. A |
13410 | null string is always empty. | 13424 | null string is always empty. |
13411 | 13425 | ||
13412 | \code | 13426 | \code |
13413 | QString a; // a.unicode() == 0, a.length() == 0 | 13427 | QString a; // a.unicode() == 0, a.length() == 0 |
13414 | a.isNull(); // TRUE, because a.unicode() == 0 | 13428 | a.isNull(); // TRUE, because a.unicode() == 0 |
13415 | a.isEmpty(); // TRUE | 13429 | a.isEmpty(); // TRUE |
13416 | \endcode | 13430 | \endcode |
13417 | 13431 | ||
13418 | \sa isEmpty(), length() | 13432 | \sa isEmpty(), length() |
13419 | */ | 13433 | */ |
13420 | 13434 | ||
13421 | /*! | 13435 | /*! |
13422 | \fn bool QString::isEmpty() const | 13436 | \fn bool QString::isEmpty() const |
13423 | 13437 | ||
13424 | Returns TRUE if the string is empty, i.e. if length() == 0; | 13438 | Returns TRUE if the string is empty, i.e. if length() == 0; |
13425 | otherwise returns FALSE. Null strings are also empty. | 13439 | otherwise returns FALSE. Null strings are also empty. |
13426 | 13440 | ||
13427 | \code | 13441 | \code |
13428 | QString a( "" ); | 13442 | QString a( "" ); |
13429 | a.isEmpty(); // TRUE | 13443 | a.isEmpty(); // TRUE |
13430 | a.isNull(); // FALSE | 13444 | a.isNull(); // FALSE |
13431 | 13445 | ||
13432 | QString b; | 13446 | QString b; |
13433 | b.isEmpty(); // TRUE | 13447 | b.isEmpty(); // TRUE |
13434 | b.isNull(); // TRUE | 13448 | b.isNull(); // TRUE |
13435 | \endcode | 13449 | \endcode |
13436 | 13450 | ||
13437 | \sa isNull(), length() | 13451 | \sa isNull(), length() |
13438 | */ | 13452 | */ |
13439 | 13453 | ||
13440 | /*! | 13454 | /*! |
13441 | \fn uint QString::length() const | 13455 | \fn uint QString::length() const |
13442 | 13456 | ||
13443 | Returns the length of the string. | 13457 | Returns the length of the string. |
13444 | 13458 | ||
13445 | Null strings and empty strings have zero length. | 13459 | Null strings and empty strings have zero length. |
13446 | 13460 | ||
13447 | \sa isNull(), isEmpty() | 13461 | \sa isNull(), isEmpty() |
13448 | */ | 13462 | */ |
13449 | 13463 | ||
13450 | /*! | 13464 | /*! |
13451 | If \a newLen is less than the length of the string, then the | 13465 | If \a newLen is less than the length of the string, then the |
13452 | string is truncated at position \a newLen. Otherwise nothing | 13466 | string is truncated at position \a newLen. Otherwise nothing |
13453 | happens. | 13467 | happens. |
13454 | 13468 | ||
13455 | \code | 13469 | \code |
13456 | QString s = "truncate me"; | 13470 | QString s = "truncate me"; |
13457 | s.truncate( 5 ); // s == "trunc" | 13471 | s.truncate( 5 ); // s == "trunc" |
13458 | \endcode | 13472 | \endcode |
13459 | 13473 | ||
13460 | \sa setLength() | 13474 | \sa setLength() |
13461 | */ | 13475 | */ |
13462 | 13476 | ||
13463 | void QString::truncate( uint newLen ) | 13477 | void QString::truncate( uint newLen ) |
13464 | { | 13478 | { |
13465 | if ( newLen < d->len ) | 13479 | if ( newLen < d->len ) |
13466 | setLength( newLen ); | 13480 | setLength( newLen ); |
13467 | } | 13481 | } |
13468 | 13482 | ||
13469 | /*! | 13483 | /*! |
13470 | Ensures that at least \a newLen characters are allocated to the | 13484 | Ensures that at least \a newLen characters are allocated to the |
13471 | string, and sets the length of the string to \a newLen. Any new | 13485 | string, and sets the length of the string to \a newLen. Any new |
13472 | space allocated contains arbitrary data. | 13486 | space allocated contains arbitrary data. |
13473 | 13487 | ||
13474 | If \a newLen is 0, then the string becomes empty, unless the | 13488 | If \a newLen is 0, then the string becomes empty (non-null). |
13475 | string is null, in which case it remains null. | ||
13476 | 13489 | ||
13477 | If it is not possible to allocate enough memory, the string | 13490 | If it is not possible to allocate enough memory, the string |
13478 | remains unchanged. | 13491 | remains unchanged. |
13479 | 13492 | ||
13480 | This function always detaches the string from other references to | 13493 | This function always detaches the string from other references to |
13481 | the same data. | 13494 | the same data. |
13482 | 13495 | ||
13483 | This function is useful for code that needs to build up a long | 13496 | This function is useful for code that needs to build up a long |
13484 | string and wants to avoid repeated reallocation. In this example, | 13497 | string and wants to avoid repeated reallocation. In this example, |
13485 | we want to add to the string until some condition is true, and | 13498 | we want to add to the string until some condition is true, and |
13486 | we're fairly sure that size is big enough: | 13499 | we're fairly sure that size is big enough: |
13487 | \code | 13500 | \code |
13488 | QString result; | 13501 | QString result; |
13489 | int resultLength = 0; | 13502 | int len = 0; |
13490 | result.setLength( newLen ) // allocate some space | 13503 | result.setLength( maxLen ); // allocate some space |
13491 | while ( ... ) { | 13504 | while ( ... ) { |
13492 | result[resultLength++] = ... // fill (part of) the space with data | 13505 | result[len++] = ... // fill part of the space |
13493 | } | 13506 | } |
13494 | result.truncate[resultLength]; // and get rid of the undefined junk | 13507 | result.truncate( len ); // and get rid of the rest |
13495 | \endcode | 13508 | \endcode |
13496 | 13509 | ||
13497 | If \a newLen is an underestimate, the worst that will happen is | 13510 | If \a newLen is an underestimate, the worst that will happen is |
13498 | that the loop will slow down. | 13511 | that the loop will slow down. |
13499 | 13512 | ||
13500 | \sa truncate(), isNull(), isEmpty(), length() | 13513 | \sa truncate(), isNull(), isEmpty(), length() |
13501 | */ | 13514 | */ |
13502 | 13515 | ||
13503 | void QString::setLength( uint newLen ) | 13516 | void QString::setLength( uint newLen ) |
13504 | { | 13517 | { |
13505 | if ( d->count != 1 || newLen > d->maxl || | 13518 | if ( d->count != 1 || newLen > d->maxl || |
13506 | ( newLen * 4 < d->maxl && d->maxl > 4 ) ) { | 13519 | ( newLen * 4 < d->maxl && d->maxl > 4 ) ) { |
13507 | // detach, grow or shrink | 13520 | // detach, grow or shrink |
13508 | Q2HELPER(stat_copy_on_write++) | ||
13509 | Q2HELPER(stat_copy_on_write_size+=d->len) | ||
13510 | uint newMax = computeNewMax( newLen ); | 13521 | uint newMax = computeNewMax( newLen ); |
13511 | QChar* nd = QT_ALLOC_QCHAR_VEC( newMax ); | 13522 | QChar* nd = QT_ALLOC_QCHAR_VEC( newMax ); |
13512 | if ( nd ) { | 13523 | if ( nd ) { |
13513 | uint len = QMIN( d->len, newLen ); | 13524 | uint len = QMIN( d->len, newLen ); |
13514 | if ( d->unicode ) | 13525 | if ( d->unicode ) |
13515 | memcpy( nd, d->unicode, sizeof(QChar)*len ); | 13526 | memcpy( nd, d->unicode, sizeof(QChar)*len ); |
13516 | deref(); | 13527 | deref(); |
13517 | d = new QStringData( nd, newLen, newMax ); | 13528 | d = new QStringData( nd, newLen, newMax ); |
13518 | } | 13529 | } |
13519 | } else { | 13530 | } else { |
13520 | d->len = newLen; | 13531 | d->len = newLen; |
13521 | d->setDirty(); | 13532 | d->setDirty(); |
13522 | } | 13533 | } |
13523 | } | 13534 | } |
13524 | 13535 | ||
13525 | /*! | 13536 | /*! |
13526 | This function will return a string that replaces the lowest | 13537 | This function will return a string that replaces the lowest |
13527 | numbered occurrence of \c %1, \c %2, ..., \c %9 with \a a. | 13538 | numbered occurrence of \c %1, \c %2, ..., \c %9 with \a a. |
13528 | 13539 | ||
13529 | The \a fieldwidth value specifies the minimum amount of space that | 13540 | The \a fieldwidth value specifies the minimum amount of space that |
13530 | \a a is padded to. A positive value will produce right-aligned | 13541 | \a a is padded to. A positive value will produce right-aligned |
13531 | text, whereas a negative value will produce left-aligned text. | 13542 | text, whereas a negative value will produce left-aligned text. |
13532 | 13543 | ||
13544 | The following example shows how we could create a 'status' string | ||
13545 | when processing a list of files: | ||
13533 | \code | 13546 | \code |
13534 | QString firstName( "Joe" ); | 13547 | QString status = QString( "Processing file %1 of %2: %3" ) |
13535 | QString lastName( "Bloggs" ); | 13548 | .arg( i ) // current file's number |
13536 | QString fullName; | 13549 | .arg( total ) // number of files to process |
13537 | fullName = QString( "First name is '%1', last name is '%2'" ) | 13550 | .arg( fileName ); // current file's name |
13538 | .arg( firstName ) | ||
13539 | .arg( lastName ); | ||
13540 | |||
13541 | // fullName == First name is 'Joe', last name is 'Bloggs' | ||
13542 | \endcode | 13551 | \endcode |
13543 | 13552 | ||
13544 | Note that using arg() to construct sentences as we've done in the | 13553 | It is generally fine to use filenames and numbers as we have done |
13545 | example above does not usually translate well into other languages | 13554 | in the example above. But note that using arg() to construct |
13546 | because sentence structure and word order often differ between | 13555 | natural language sentences does not usually translate well into |
13547 | languages. | 13556 | other languages because sentence structure and word order often |
13557 | differ between languages. | ||
13548 | 13558 | ||
13549 | If there is no place marker (\c %1 or \c %2, etc.), a warning | 13559 | If there is no place marker (\c %1 or \c %2, etc.), a warning |
13550 | message (qWarning()) is output and the text is appended at the | 13560 | message (qWarning()) is output and the text is appended at the |
13551 | end of the string. We recommend that the correct number of place | 13561 | end of the string. We recommend that the correct number of place |
13552 | markers is always used in production code. | 13562 | markers is always used in production code. |
13553 | */ | 13563 | */ |
13554 | QString QString::arg( const QString& a, int fieldwidth ) const | 13564 | QString QString::arg( const QString& a, int fieldwidth ) const |
13555 | { | 13565 | { |
13556 | int pos, len; | 13566 | int pos, len; |
13557 | QString r = *this; | 13567 | QString r = *this; |
13558 | 13568 | ||
13559 | if ( !findArg( pos, len ) ) { | 13569 | if ( !findArg( pos, len ) ) { |
13560 | qWarning( "QString::arg(): Argument missing: %s, %s", | 13570 | qWarning( "QString::arg(): Argument missing: %s, %s", |
13561 | latin1(), a.latin1() ); | 13571 | latin1(), a.latin1() ); |
13562 | // Make sure the text at least appears SOMEWHERE | 13572 | // Make sure the text at least appears SOMEWHERE |
13563 | r += ' '; | 13573 | r += ' '; |
13564 | pos = r.length(); | 13574 | pos = r.length(); |
13565 | len = 0; | 13575 | len = 0; |
13566 | } | 13576 | } |
13567 | 13577 | ||
13568 | r.replace( pos, len, a ); | 13578 | r.replace( pos, len, a ); |
13569 | if ( fieldwidth < 0 ) { | 13579 | if ( fieldwidth < 0 ) { |
13570 | QString s; | 13580 | QString s; |
13571 | while ( (uint)-fieldwidth > a.length() ) { | 13581 | while ( (uint)-fieldwidth > a.length() ) { |
13572 | s += ' '; | 13582 | s += ' '; |
13573 | fieldwidth++; | 13583 | fieldwidth++; |
13574 | } | 13584 | } |
13575 | r.insert( pos + a.length(), s ); | 13585 | r.insert( pos + a.length(), s ); |
13576 | } else if ( fieldwidth ) { | 13586 | } else if ( fieldwidth ) { |
13577 | QString s; | 13587 | QString s; |
13578 | while ( (uint)fieldwidth > a.length() ) { | 13588 | while ( (uint)fieldwidth > a.length() ) { |
13579 | s += ' '; | 13589 | s += ' '; |
13580 | fieldwidth--; | 13590 | fieldwidth--; |
13581 | } | 13591 | } |
13582 | r.insert( pos, s ); | 13592 | r.insert( pos, s ); |
13583 | } | 13593 | } |
13584 | 13594 | ||
13585 | return r; | 13595 | return r; |
13586 | } | 13596 | } |
13587 | 13597 | ||
13588 | 13598 | ||
13589 | /*! | 13599 | /*! |
13590 | \overload | 13600 | \overload |
13591 | 13601 | ||
13592 | The \a fieldwidth value specifies the minimum amount of space that | 13602 | The \a fieldwidth value specifies the minimum amount of space that |
13593 | \a a is padded to. A positive value will produce a right-aligned | 13603 | \a a is padded to. A positive value will produce a right-aligned |
13594 | number, whereas a negative value will produce a left-aligned | 13604 | number, whereas a negative value will produce a left-aligned |
13595 | number. | 13605 | number. |
@@ -13729,97 +13739,97 @@ bool QString::findArg( int& pos, int& len ) const | |||
13729 | lowest = dig; | 13739 | lowest = dig; |
13730 | pos = i; | 13740 | pos = i; |
13731 | len = 2; | 13741 | len = 2; |
13732 | } | 13742 | } |
13733 | } | 13743 | } |
13734 | } | 13744 | } |
13735 | } | 13745 | } |
13736 | return lowest != 0; | 13746 | return lowest != 0; |
13737 | } | 13747 | } |
13738 | 13748 | ||
13739 | /*! | 13749 | /*! |
13740 | Safely builds a formatted string from the format string \a cformat | 13750 | Safely builds a formatted string from the format string \a cformat |
13741 | and an arbitrary list of arguments. The format string supports all | 13751 | and an arbitrary list of arguments. The format string supports all |
13742 | the escape sequences of printf() in the standard C library. | 13752 | the escape sequences of printf() in the standard C library. |
13743 | 13753 | ||
13744 | The %s escape sequence expects a utf8() encoded string. The format | 13754 | The %s escape sequence expects a utf8() encoded string. The format |
13745 | string \e cformat is expected to be in latin1. If you need a | 13755 | string \e cformat is expected to be in latin1. If you need a |
13746 | Unicode format string, use arg() instead. For typesafe string | 13756 | Unicode format string, use arg() instead. For typesafe string |
13747 | building, with full Unicode support, you can use QTextOStream like | 13757 | building, with full Unicode support, you can use QTextOStream like |
13748 | this: | 13758 | this: |
13749 | 13759 | ||
13750 | \code | 13760 | \code |
13751 | QString str; | 13761 | QString str; |
13752 | QString s = ...; | 13762 | QString s = ...; |
13753 | int x = ...; | 13763 | int x = ...; |
13754 | QTextOStream( &str ) << s << " : " << x; | 13764 | QTextOStream( &str ) << s << " : " << x; |
13755 | \endcode | 13765 | \endcode |
13756 | 13766 | ||
13757 | For \link QObject::tr() translations,\endlink especially if the | 13767 | For \link QObject::tr() translations,\endlink especially if the |
13758 | strings contains more than one escape sequence, you should | 13768 | strings contains more than one escape sequence, you should |
13759 | consider using the arg() function instead. This allows the order | 13769 | consider using the arg() function instead. This allows the order |
13760 | of the replacements to be controlled by the translator, and has | 13770 | of the replacements to be controlled by the translator, and has |
13761 | Unicode support. | 13771 | Unicode support. |
13762 | 13772 | ||
13763 | \sa arg() | 13773 | \sa arg() |
13764 | */ | 13774 | */ |
13765 | 13775 | ||
13766 | #ifndef QT_NO_SPRINTF | 13776 | #ifndef QT_NO_SPRINTF |
13767 | QString &QString::sprintf( const char* cformat, ... ) | 13777 | QString &QString::sprintf( const char* cformat, ... ) |
13768 | { | 13778 | { |
13769 | va_list ap; | 13779 | va_list ap; |
13770 | va_start( ap, cformat ); | 13780 | va_start( ap, cformat ); |
13771 | 13781 | ||
13772 | if ( !cformat || !*cformat ) { | 13782 | if ( !cformat || !*cformat ) { |
13773 | // Qt 1.x compat | 13783 | // Qt 1.x compat |
13774 | *this = fromLatin1( "" ); | 13784 | *this = fromLatin1( "" ); |
13775 | return *this; | 13785 | return *this; |
13776 | } | 13786 | } |
13777 | QString format = fromLatin1( cformat ); | 13787 | QString format = fromAscii( cformat ); |
13778 | 13788 | ||
13779 | QRegExp escape( "%#?0?-? ?\\+?'?[0-9*]*\\.?[0-9*]*h?l?L?q?Z?" ); | 13789 | QRegExp escape( "%#?0?-? ?\\+?'?[0-9*]*\\.?[0-9*]*h?l?L?q?Z?" ); |
13780 | QString result; | 13790 | QString result; |
13781 | uint last = 0; | 13791 | uint last = 0; |
13782 | int pos; | 13792 | int pos; |
13783 | int len = 0; | 13793 | int len = 0; |
13784 | 13794 | ||
13785 | for (;;) { | 13795 | for (;;) { |
13786 | pos = escape.search( format, last ); | 13796 | pos = escape.search( format, last ); |
13787 | len = escape.matchedLength(); | 13797 | len = escape.matchedLength(); |
13788 | // Non-escaped text | 13798 | // Non-escaped text |
13789 | if ( pos > (int)last ) | 13799 | if ( pos > (int)last ) |
13790 | result += format.mid( last, pos - last ); | 13800 | result += format.mid( last, pos - last ); |
13791 | if ( pos < 0 ) { | 13801 | if ( pos < 0 ) { |
13792 | // The rest | 13802 | // The rest |
13793 | if ( last < format.length() ) | 13803 | if ( last < format.length() ) |
13794 | result += format.mid( last ); | 13804 | result += format.mid( last ); |
13795 | break; | 13805 | break; |
13796 | } | 13806 | } |
13797 | last = pos + len + 1; | 13807 | last = pos + len + 1; |
13798 | 13808 | ||
13799 | // Escape | 13809 | // Escape |
13800 | QString f = format.mid( pos, len ); | 13810 | QString f = format.mid( pos, len ); |
13801 | uint width, decimals; | 13811 | uint width, decimals; |
13802 | int params = 0; | 13812 | int params = 0; |
13803 | int wpos = f.find('*'); | 13813 | int wpos = f.find('*'); |
13804 | if ( wpos >= 0 ) { | 13814 | if ( wpos >= 0 ) { |
13805 | params++; | 13815 | params++; |
13806 | width = va_arg( ap, int ); | 13816 | width = va_arg( ap, int ); |
13807 | if ( f.find('*', wpos + 1) >= 0 ) { | 13817 | if ( f.find('*', wpos + 1) >= 0 ) { |
13808 | decimals = va_arg( ap, int ); | 13818 | decimals = va_arg( ap, int ); |
13809 | params++; | 13819 | params++; |
13810 | } else { | 13820 | } else { |
13811 | decimals = 0; | 13821 | decimals = 0; |
13812 | } | 13822 | } |
13813 | } else { | 13823 | } else { |
13814 | decimals = width = 0; | 13824 | decimals = width = 0; |
13815 | } | 13825 | } |
13816 | QString replacement; | 13826 | QString replacement; |
13817 | if ( format[pos + len] == 's' || format[pos + len] == 'S' || | 13827 | if ( format[pos + len] == 's' || format[pos + len] == 'S' || |
13818 | format[pos + len] == 'c' ) | 13828 | format[pos + len] == 'c' ) |
13819 | { | 13829 | { |
13820 | bool rightjust = ( f.find('-') < 0 ); | 13830 | bool rightjust = ( f.find('-') < 0 ); |
13821 | // %-5s really means left adjust in sprintf | 13831 | // %-5s really means left adjust in sprintf |
13822 | 13832 | ||
13823 | if ( wpos < 0 ) { | 13833 | if ( wpos < 0 ) { |
13824 | QRegExp num( fromLatin1("[0-9]+") ); | 13834 | QRegExp num( fromLatin1("[0-9]+") ); |
13825 | int p = num.search( f ); | 13835 | int p = num.search( f ); |
@@ -13868,97 +13878,97 @@ QString &QString::sprintf( const char* cformat, ... ) | |||
13868 | { | 13878 | { |
13869 | int value = va_arg( ap, int ); | 13879 | int value = va_arg( ap, int ); |
13870 | switch ( params ) { | 13880 | switch ( params ) { |
13871 | case 0: | 13881 | case 0: |
13872 | ::sprintf( out, in, value ); | 13882 | ::sprintf( out, in, value ); |
13873 | break; | 13883 | break; |
13874 | case 1: | 13884 | case 1: |
13875 | ::sprintf( out, in, width, value ); | 13885 | ::sprintf( out, in, width, value ); |
13876 | break; | 13886 | break; |
13877 | case 2: | 13887 | case 2: |
13878 | ::sprintf( out, in, width, decimals, value ); | 13888 | ::sprintf( out, in, width, decimals, value ); |
13879 | } | 13889 | } |
13880 | } | 13890 | } |
13881 | break; | 13891 | break; |
13882 | case 'e': | 13892 | case 'e': |
13883 | case 'E': | 13893 | case 'E': |
13884 | case 'f': | 13894 | case 'f': |
13885 | case 'g': | 13895 | case 'g': |
13886 | case 'G': | 13896 | case 'G': |
13887 | { | 13897 | { |
13888 | double value = va_arg( ap, double ); | 13898 | double value = va_arg( ap, double ); |
13889 | switch ( params ) { | 13899 | switch ( params ) { |
13890 | case 0: | 13900 | case 0: |
13891 | ::sprintf( out, in, value ); | 13901 | ::sprintf( out, in, value ); |
13892 | break; | 13902 | break; |
13893 | case 1: | 13903 | case 1: |
13894 | ::sprintf( out, in, width, value ); | 13904 | ::sprintf( out, in, width, value ); |
13895 | break; | 13905 | break; |
13896 | case 2: | 13906 | case 2: |
13897 | ::sprintf( out, in, width, decimals, value ); | 13907 | ::sprintf( out, in, width, decimals, value ); |
13898 | } | 13908 | } |
13899 | } | 13909 | } |
13900 | break; | 13910 | break; |
13901 | case 'p': | 13911 | case 'p': |
13902 | { | 13912 | { |
13903 | void* value = va_arg( ap, void * ); | 13913 | void* value = va_arg( ap, void * ); |
13904 | switch ( params ) { | 13914 | switch ( params ) { |
13905 | case 0: | 13915 | case 0: |
13906 | ::sprintf( out, in, value ); | 13916 | ::sprintf( out, in, value ); |
13907 | break; | 13917 | break; |
13908 | case 1: | 13918 | case 1: |
13909 | ::sprintf( out, in, width, value ); | 13919 | ::sprintf( out, in, width, value ); |
13910 | break; | 13920 | break; |
13911 | case 2: | 13921 | case 2: |
13912 | ::sprintf( out, in, width, decimals, value ); | 13922 | ::sprintf( out, in, width, decimals, value ); |
13913 | } | 13923 | } |
13914 | } | 13924 | } |
13915 | } | 13925 | } |
13916 | replacement = fromLatin1( out ); | 13926 | replacement = fromAscii( out ); |
13917 | } | 13927 | } |
13918 | result += replacement; | 13928 | result += replacement; |
13919 | } | 13929 | } |
13920 | *this = result; | 13930 | *this = result; |
13921 | 13931 | ||
13922 | va_end( ap ); | 13932 | va_end( ap ); |
13923 | return *this; | 13933 | return *this; |
13924 | } | 13934 | } |
13925 | #endif | 13935 | #endif |
13926 | 13936 | ||
13927 | /*! | 13937 | /*! |
13928 | Fills the string with \a len characters of value \a c, and returns | 13938 | Fills the string with \a len characters of value \a c, and returns |
13929 | a reference to the string. | 13939 | a reference to the string. |
13930 | 13940 | ||
13931 | If \a len is negative (the default), the current string length is | 13941 | If \a len is negative (the default), the current string length is |
13932 | used. | 13942 | used. |
13933 | 13943 | ||
13934 | \code | 13944 | \code |
13935 | QString str; | 13945 | QString str; |
13936 | str.fill( 'g', 5 ); // string == "ggggg" | 13946 | str.fill( 'g', 5 ); // string == "ggggg" |
13937 | \endcode | 13947 | \endcode |
13938 | */ | 13948 | */ |
13939 | 13949 | ||
13940 | QString& QString::fill( QChar c, int len ) | 13950 | QString& QString::fill( QChar c, int len ) |
13941 | { | 13951 | { |
13942 | if ( len < 0 ) | 13952 | if ( len < 0 ) |
13943 | len = length(); | 13953 | len = length(); |
13944 | if ( len == 0 ) { | 13954 | if ( len == 0 ) { |
13945 | *this = ""; | 13955 | *this = ""; |
13946 | } else { | 13956 | } else { |
13947 | deref(); | 13957 | deref(); |
13948 | QChar * nd = QT_ALLOC_QCHAR_VEC( len ); | 13958 | QChar * nd = QT_ALLOC_QCHAR_VEC( len ); |
13949 | d = new QStringData(nd,len,len); | 13959 | d = new QStringData(nd,len,len); |
13950 | while (len--) *nd++ = c; | 13960 | while (len--) *nd++ = c; |
13951 | } | 13961 | } |
13952 | return *this; | 13962 | return *this; |
13953 | } | 13963 | } |
13954 | 13964 | ||
13955 | 13965 | ||
13956 | /*! | 13966 | /*! |
13957 | \fn QString QString::copy() const | 13967 | \fn QString QString::copy() const |
13958 | 13968 | ||
13959 | \obsolete | 13969 | \obsolete |
13960 | 13970 | ||
13961 | In Qt 2.0 and later, all calls to this function are needless. Just | 13971 | In Qt 2.0 and later, all calls to this function are needless. Just |
13962 | remove them. | 13972 | remove them. |
13963 | */ | 13973 | */ |
13964 | 13974 | ||
@@ -14079,247 +14089,255 @@ static int bm_find( const QString &str, int index, const QString &pattern, uint | |||
14079 | } | 14089 | } |
14080 | if ( skip > pl_minus_one ) // we have a match | 14090 | if ( skip > pl_minus_one ) // we have a match |
14081 | return (current - uc) - skip + 1; | 14091 | return (current - uc) - skip + 1; |
14082 | // in case we don't have a match we are a bit inefficient as we only skip by one | 14092 | // in case we don't have a match we are a bit inefficient as we only skip by one |
14083 | // when we have the non matching char in the string. | 14093 | // when we have the non matching char in the string. |
14084 | if ( skiptable[ ::lower( (current - skip)->cell() ) ] == pl ) | 14094 | if ( skiptable[ ::lower( (current - skip)->cell() ) ] == pl ) |
14085 | skip = pl - skip; | 14095 | skip = pl - skip; |
14086 | else | 14096 | else |
14087 | skip = 1; | 14097 | skip = 1; |
14088 | } | 14098 | } |
14089 | current += skip; | 14099 | current += skip; |
14090 | } | 14100 | } |
14091 | } | 14101 | } |
14092 | // not found | 14102 | // not found |
14093 | return -1; | 14103 | return -1; |
14094 | } | 14104 | } |
14095 | 14105 | ||
14096 | 14106 | ||
14097 | #define REHASH( a ) \ | 14107 | #define REHASH( a ) \ |
14098 | if ( sl_minus_1 < sizeof(uint) * CHAR_BIT ) \ | 14108 | if ( sl_minus_1 < sizeof(uint) * CHAR_BIT ) \ |
14099 | hashHaystack -= (a) << sl_minus_1; \ | 14109 | hashHaystack -= (a) << sl_minus_1; \ |
14100 | hashHaystack <<= 1 | 14110 | hashHaystack <<= 1 |
14101 | 14111 | ||
14102 | /*! | 14112 | /*! |
14103 | \overload | 14113 | \overload |
14104 | 14114 | ||
14105 | Finds the first occurrence of the string \a str, starting at | 14115 | Finds the first occurrence of the string \a str, starting at |
14106 | position \a index. If \a index is -1, the search starts at the | 14116 | position \a index. If \a index is -1, the search starts at the |
14107 | last character, if it is -2, at the next to last character and so | 14117 | last character, if it is -2, at the next to last character and so |
14108 | on. (See findRev() for searching backwards.) | 14118 | on. (See findRev() for searching backwards.) |
14109 | 14119 | ||
14110 | If \a cs is TRUE, the search is case sensitive; otherwise the | 14120 | If \a cs is TRUE, the search is case sensitive; otherwise the |
14111 | search is case insensitive. | 14121 | search is case insensitive. |
14112 | 14122 | ||
14113 | Returns the position of \a str or -1 if \a str could not be found. | 14123 | Returns the position of \a str or -1 if \a str could not be found. |
14114 | */ | 14124 | */ |
14115 | 14125 | ||
14116 | int QString::find( const QString& str, int index, bool cs ) const | 14126 | int QString::find( const QString& str, int index, bool cs ) const |
14117 | { | 14127 | { |
14118 | const uint l = length(); | 14128 | const uint l = length(); |
14119 | const uint sl = str.length(); | 14129 | const uint sl = str.length(); |
14120 | if ( index < 0 ) | 14130 | if ( index < 0 ) |
14121 | index += l; | 14131 | index += l; |
14122 | if ( sl + index > l ) | 14132 | if ( sl + index > l ) |
14123 | return -1; | 14133 | return -1; |
14124 | if ( !sl ) | 14134 | if ( !sl ) |
14125 | return index; | 14135 | return index; |
14126 | 14136 | ||
14137 | #ifndef MACOSX_101 | ||
14127 | if ( sl == 1 ) | 14138 | if ( sl == 1 ) |
14128 | return find( *str.unicode(), index, cs ); | 14139 | return find( *str.unicode(), index, cs ); |
14140 | #endif | ||
14129 | 14141 | ||
14130 | // we use the Boyer-Moore algorithm in cases where the overhead | 14142 | // we use the Boyer-Moore algorithm in cases where the overhead |
14131 | // for the hash table should pay off, otherwise we use a simple | 14143 | // for the hash table should pay off, otherwise we use a simple |
14132 | // hash function | 14144 | // hash function |
14133 | if ( l > 500 && sl > 5 ) { | 14145 | if ( l > 500 && sl > 5 ) { |
14134 | uint skiptable[0x100]; | 14146 | uint skiptable[0x100]; |
14135 | bm_init_skiptable( str, skiptable, cs ); | 14147 | bm_init_skiptable( str, skiptable, cs ); |
14136 | return bm_find( *this, index, str, skiptable, cs ); | 14148 | return bm_find( *this, index, str, skiptable, cs ); |
14137 | } | 14149 | } |
14138 | 14150 | ||
14139 | /* | 14151 | /* |
14140 | We use some hashing for efficiency's sake. Instead of | 14152 | We use some hashing for efficiency's sake. Instead of |
14141 | comparing strings, we compare the hash value of str with that of | 14153 | comparing strings, we compare the hash value of str with that of |
14142 | a part of this QString. Only if that matches, we call ucstrncmp | 14154 | a part of this QString. Only if that matches, we call ucstrncmp |
14143 | or ucstrnicmp. | 14155 | or ucstrnicmp. |
14144 | */ | 14156 | */ |
14145 | const QChar* needle = str.unicode(); | 14157 | const QChar* needle = str.unicode(); |
14146 | const QChar* haystack = unicode() + index; | 14158 | const QChar* haystack = unicode() + index; |
14147 | const QChar* end = unicode() + (l-sl); | 14159 | const QChar* end = unicode() + (l-sl); |
14148 | const uint sl_minus_1 = sl-1; | 14160 | const uint sl_minus_1 = sl-1; |
14149 | uint hashNeedle = 0, hashHaystack = 0, i; | 14161 | uint hashNeedle = 0, hashHaystack = 0, i; |
14150 | 14162 | ||
14151 | if ( cs ) { | 14163 | if ( cs ) { |
14152 | for ( i = 0; i < sl; ++i ) { | 14164 | for ( i = 0; i < sl; ++i ) { |
14153 | hashNeedle = ((hashNeedle<<1) + needle[i].unicode() ); | 14165 | hashNeedle = ((hashNeedle<<1) + needle[i].unicode() ); |
14154 | hashHaystack = ((hashHaystack<<1) + haystack[i].unicode() ); | 14166 | hashHaystack = ((hashHaystack<<1) + haystack[i].unicode() ); |
14155 | } | 14167 | } |
14156 | hashHaystack -= (haystack+sl_minus_1)->unicode(); | 14168 | hashHaystack -= (haystack+sl_minus_1)->unicode(); |
14157 | 14169 | ||
14158 | while ( haystack <= end ) { | 14170 | while ( haystack <= end ) { |
14159 | hashHaystack += (haystack+sl_minus_1)->unicode(); | 14171 | hashHaystack += (haystack+sl_minus_1)->unicode(); |
14160 | if ( hashHaystack == hashNeedle | 14172 | if ( hashHaystack == hashNeedle |
14161 | && ucstrncmp( needle, haystack, sl ) == 0 ) | 14173 | && ucstrncmp( needle, haystack, sl ) == 0 ) |
14162 | return haystack-unicode(); | 14174 | return haystack-unicode(); |
14163 | 14175 | ||
14164 | REHASH( haystack->unicode() ); | 14176 | REHASH( haystack->unicode() ); |
14165 | ++haystack; | 14177 | ++haystack; |
14166 | } | 14178 | } |
14167 | } else { | 14179 | } else { |
14168 | for ( i = 0; i < sl; ++i ) { | 14180 | for ( i = 0; i < sl; ++i ) { |
14169 | hashNeedle = ((hashNeedle<<1) + | 14181 | hashNeedle = ((hashNeedle<<1) + |
14170 | ::lower( needle[i].unicode() ).unicode() ); | 14182 | ::lower( needle[i].unicode() ).unicode() ); |
14171 | hashHaystack = ((hashHaystack<<1) + | 14183 | hashHaystack = ((hashHaystack<<1) + |
14172 | ::lower( haystack[i].unicode() ).unicode() ); | 14184 | ::lower( haystack[i].unicode() ).unicode() ); |
14173 | } | 14185 | } |
14174 | 14186 | ||
14175 | hashHaystack -= ::lower(*(haystack+sl_minus_1)).unicode(); | 14187 | hashHaystack -= ::lower(*(haystack+sl_minus_1)).unicode(); |
14176 | while ( haystack <= end ) { | 14188 | while ( haystack <= end ) { |
14177 | hashHaystack += ::lower(*(haystack+sl_minus_1)).unicode(); | 14189 | hashHaystack += ::lower(*(haystack+sl_minus_1)).unicode(); |
14178 | if ( hashHaystack == hashNeedle | 14190 | if ( hashHaystack == hashNeedle |
14179 | && ucstrnicmp( needle, haystack, sl ) == 0 ) | 14191 | && ucstrnicmp( needle, haystack, sl ) == 0 ) |
14180 | return haystack-unicode(); | 14192 | return haystack-unicode(); |
14181 | 14193 | ||
14182 | REHASH( ::lower(*haystack).unicode() ); | 14194 | REHASH( ::lower(*haystack).unicode() ); |
14183 | ++haystack; | 14195 | ++haystack; |
14184 | } | 14196 | } |
14185 | } | 14197 | } |
14186 | return -1; | 14198 | return -1; |
14187 | } | 14199 | } |
14188 | 14200 | ||
14189 | /*! | 14201 | /*! |
14190 | \fn int QString::findRev( const char* str, int index ) const | 14202 | \fn int QString::findRev( const char* str, int index ) const |
14191 | 14203 | ||
14192 | Equivalent to findRev(QString(\a str), \a index). | 14204 | Equivalent to findRev(QString(\a str), \a index). |
14193 | */ | 14205 | */ |
14194 | 14206 | ||
14195 | /*! | 14207 | /*! |
14196 | \fn int QString::find( const char* str, int index ) const | 14208 | \fn int QString::find( const char* str, int index ) const |
14197 | 14209 | ||
14198 | \overload | 14210 | \overload |
14199 | 14211 | ||
14200 | Equivalent to find(QString(\a str), \a index). | 14212 | Equivalent to find(QString(\a str), \a index). |
14201 | */ | 14213 | */ |
14202 | 14214 | ||
14203 | /*! | 14215 | /*! |
14204 | \overload | 14216 | \overload |
14205 | 14217 | ||
14206 | Finds the first occurrence of the character \a c, starting at | 14218 | Finds the first occurrence of the character \a c, starting at |
14207 | position \a index and searching backwards. If the index is -1, the | 14219 | position \a index and searching backwards. If the index is -1, the |
14208 | search starts at the last character, if it is -2, at the next to | 14220 | search starts at the last character, if it is -2, at the next to |
14209 | last character and so on. | 14221 | last character and so on. |
14210 | 14222 | ||
14211 | Returns the position of \a c or -1 if \a c could not be found. | 14223 | Returns the position of \a c or -1 if \a c could not be found. |
14212 | 14224 | ||
14213 | If \a cs is TRUE, the search is case sensitive; otherwise the | 14225 | If \a cs is TRUE, the search is case sensitive; otherwise the |
14214 | search is case insensitive. | 14226 | search is case insensitive. |
14215 | 14227 | ||
14216 | \code | 14228 | \code |
14217 | QString string( "bananas" ); | 14229 | QString string( "bananas" ); |
14218 | int i = string.findRev( 'a' ); // i == 5 | 14230 | int i = string.findRev( 'a' ); // i == 5 |
14219 | \endcode | 14231 | \endcode |
14220 | */ | 14232 | */ |
14221 | 14233 | ||
14222 | int QString::findRev( QChar c, int index, bool cs ) const | 14234 | int QString::findRev( QChar c, int index, bool cs ) const |
14223 | { | 14235 | { |
14236 | #ifdef MACOSX_101 | ||
14237 | return findRev( QString( c ), index, cs ); | ||
14238 | #else | ||
14224 | const uint l = length(); | 14239 | const uint l = length(); |
14225 | if ( index < 0 ) | 14240 | if ( index < 0 ) |
14226 | index += l; | 14241 | index += l; |
14227 | if ( (uint)index >= l ) | 14242 | if ( (uint)index >= l ) |
14228 | return -1; | 14243 | return -1; |
14229 | const QChar *end = unicode(); | 14244 | const QChar *end = unicode(); |
14230 | register const QChar *uc = end + index; | 14245 | register const QChar *uc = end + index; |
14231 | if ( cs ) { | 14246 | if ( cs ) { |
14232 | while ( uc >= end && *uc != c ) | 14247 | while ( uc >= end && *uc != c ) |
14233 | uc--; | 14248 | uc--; |
14234 | } else { | 14249 | } else { |
14235 | c = ::lower( c ); | 14250 | c = ::lower( c ); |
14236 | while ( uc >= end && ::lower( *uc ) != c ) | 14251 | while ( uc >= end && ::lower( *uc ) != c ) |
14237 | uc--; | 14252 | uc--; |
14238 | } | 14253 | } |
14239 | return uc - end; | 14254 | return uc - end; |
14255 | #endif | ||
14240 | } | 14256 | } |
14241 | 14257 | ||
14242 | /*! | 14258 | /*! |
14243 | \overload | 14259 | \overload |
14244 | 14260 | ||
14245 | Finds the first occurrence of the string \a str, starting at | 14261 | Finds the first occurrence of the string \a str, starting at |
14246 | position \a index and searching backwards. If the index is -1, the | 14262 | position \a index and searching backwards. If the index is -1, the |
14247 | search starts at the last character, if it is -2, at the next to | 14263 | search starts at the last character, if it is -2, at the next to |
14248 | last character and so on. | 14264 | last character and so on. |
14249 | 14265 | ||
14250 | Returns the position of \a str or -1 if \a str could not be found. | 14266 | Returns the position of \a str or -1 if \a str could not be found. |
14251 | 14267 | ||
14252 | If \a cs is TRUE, the search is case sensitive; otherwise the | 14268 | If \a cs is TRUE, the search is case sensitive; otherwise the |
14253 | search is case insensitive. | 14269 | search is case insensitive. |
14254 | 14270 | ||
14255 | \code | 14271 | \code |
14256 | QString string("bananas"); | 14272 | QString string("bananas"); |
14257 | int i = string.findRev( "ana" ); // i == 3 | 14273 | int i = string.findRev( "ana" ); // i == 3 |
14258 | \endcode | 14274 | \endcode |
14259 | */ | 14275 | */ |
14260 | 14276 | ||
14261 | int QString::findRev( const QString& str, int index, bool cs ) const | 14277 | int QString::findRev( const QString& str, int index, bool cs ) const |
14262 | { | 14278 | { |
14263 | /* | 14279 | /* |
14264 | See QString::find() for explanations. | 14280 | See QString::find() for explanations. |
14265 | */ | 14281 | */ |
14266 | const uint l = length(); | 14282 | const uint l = length(); |
14267 | if ( index < 0 ) | 14283 | if ( index < 0 ) |
14268 | index += l; | 14284 | index += l; |
14269 | const uint sl = str.length(); | 14285 | const uint sl = str.length(); |
14270 | int delta = l-sl; | 14286 | int delta = l-sl; |
14271 | if ( index < 0 || index > (int)l || delta < 0 ) | 14287 | if ( index < 0 || index > (int)l || delta < 0 ) |
14272 | return -1; | 14288 | return -1; |
14273 | if ( index > delta ) | 14289 | if ( index > delta ) |
14274 | index = delta; | 14290 | index = delta; |
14275 | 14291 | ||
14292 | #ifndef MACOSX_101 | ||
14276 | if ( sl == 1 ) | 14293 | if ( sl == 1 ) |
14277 | return findRev( *str.unicode(), index, cs ); | 14294 | return findRev( *str.unicode(), index, cs ); |
14295 | #endif | ||
14278 | 14296 | ||
14279 | const QChar* needle = str.unicode(); | 14297 | const QChar* needle = str.unicode(); |
14280 | const QChar* haystack = unicode() + index; | 14298 | const QChar* haystack = unicode() + index; |
14281 | const QChar* end = unicode(); | 14299 | const QChar* end = unicode(); |
14282 | const uint sl_minus_1 = sl-1; | 14300 | const uint sl_minus_1 = sl-1; |
14283 | const QChar* n = needle+sl_minus_1; | 14301 | const QChar* n = needle+sl_minus_1; |
14284 | const QChar* h = haystack+sl_minus_1; | 14302 | const QChar* h = haystack+sl_minus_1; |
14285 | uint hashNeedle = 0, hashHaystack = 0, i; | 14303 | uint hashNeedle = 0, hashHaystack = 0, i; |
14286 | 14304 | ||
14287 | if ( cs ) { | 14305 | if ( cs ) { |
14288 | for ( i = 0; i < sl; ++i ) { | 14306 | for ( i = 0; i < sl; ++i ) { |
14289 | hashNeedle = ((hashNeedle<<1) + (n-i)->unicode() ); | 14307 | hashNeedle = ((hashNeedle<<1) + (n-i)->unicode() ); |
14290 | hashHaystack = ((hashHaystack<<1) + (h-i)->unicode() ); | 14308 | hashHaystack = ((hashHaystack<<1) + (h-i)->unicode() ); |
14291 | } | 14309 | } |
14292 | hashHaystack -= haystack->unicode(); | 14310 | hashHaystack -= haystack->unicode(); |
14293 | 14311 | ||
14294 | while ( haystack >= end ) { | 14312 | while ( haystack >= end ) { |
14295 | hashHaystack += haystack->unicode(); | 14313 | hashHaystack += haystack->unicode(); |
14296 | if ( hashHaystack == hashNeedle | 14314 | if ( hashHaystack == hashNeedle |
14297 | && ucstrncmp( needle, haystack, sl ) == 0 ) | 14315 | && ucstrncmp( needle, haystack, sl ) == 0 ) |
14298 | return haystack-unicode(); | 14316 | return haystack-unicode(); |
14299 | --haystack; | 14317 | --haystack; |
14300 | REHASH( (haystack+sl)->unicode() ); | 14318 | REHASH( (haystack+sl)->unicode() ); |
14301 | } | 14319 | } |
14302 | } else { | 14320 | } else { |
14303 | for ( i = 0; i < sl; ++i ) { | 14321 | for ( i = 0; i < sl; ++i ) { |
14304 | hashNeedle = ((hashNeedle<<1) | 14322 | hashNeedle = ((hashNeedle<<1) |
14305 | + ::lower( (n-i)->unicode() ).unicode() ); | 14323 | + ::lower( (n-i)->unicode() ).unicode() ); |
14306 | hashHaystack = ((hashHaystack<<1) | 14324 | hashHaystack = ((hashHaystack<<1) |
14307 | + ::lower( (h-i)->unicode() ).unicode() ); | 14325 | + ::lower( (h-i)->unicode() ).unicode() ); |
14308 | } | 14326 | } |
14309 | hashHaystack -= ::lower(*haystack).unicode(); | 14327 | hashHaystack -= ::lower(*haystack).unicode(); |
14310 | 14328 | ||
14311 | while ( haystack >= end ) { | 14329 | while ( haystack >= end ) { |
14312 | hashHaystack += ::lower(*haystack).unicode(); | 14330 | hashHaystack += ::lower(*haystack).unicode(); |
14313 | if ( hashHaystack == hashNeedle | 14331 | if ( hashHaystack == hashNeedle |
14314 | && ucstrnicmp( needle, haystack, sl ) == 0 ) | 14332 | && ucstrnicmp( needle, haystack, sl ) == 0 ) |
14315 | return haystack-unicode(); | 14333 | return haystack-unicode(); |
14316 | --haystack; | 14334 | --haystack; |
14317 | REHASH( ::lower(*(haystack+sl)).unicode() ); | 14335 | REHASH( ::lower(*(haystack+sl)).unicode() ); |
14318 | } | 14336 | } |
14319 | } | 14337 | } |
14320 | return -1; | 14338 | return -1; |
14321 | } | 14339 | } |
14322 | 14340 | ||
14323 | #undef REHASH | 14341 | #undef REHASH |
14324 | 14342 | ||
14325 | /*! | 14343 | /*! |
@@ -14934,137 +14952,147 @@ QString QString::leftJustify( uint width, QChar fill, bool truncate ) const | |||
14934 | \a width, then the returned string is a copy of the string. | 14952 | \a width, then the returned string is a copy of the string. |
14935 | 14953 | ||
14936 | If \a truncate is TRUE and the length of the string is more than | 14954 | If \a truncate is TRUE and the length of the string is more than |
14937 | \a width, then the resulting string is truncated at position \a | 14955 | \a width, then the resulting string is truncated at position \a |
14938 | width. | 14956 | width. |
14939 | 14957 | ||
14940 | \code | 14958 | \code |
14941 | QString string( "apple" ); | 14959 | QString string( "apple" ); |
14942 | QString t = string.rightJustify( 8, '.' ); // t == "...apple" | 14960 | QString t = string.rightJustify( 8, '.' ); // t == "...apple" |
14943 | \endcode | 14961 | \endcode |
14944 | 14962 | ||
14945 | \sa leftJustify() | 14963 | \sa leftJustify() |
14946 | */ | 14964 | */ |
14947 | 14965 | ||
14948 | QString QString::rightJustify( uint width, QChar fill, bool truncate ) const | 14966 | QString QString::rightJustify( uint width, QChar fill, bool truncate ) const |
14949 | { | 14967 | { |
14950 | QString result; | 14968 | QString result; |
14951 | int len = length(); | 14969 | int len = length(); |
14952 | int padlen = width - len; | 14970 | int padlen = width - len; |
14953 | if ( padlen > 0 ) { | 14971 | if ( padlen > 0 ) { |
14954 | result.setLength( len+padlen ); | 14972 | result.setLength( len+padlen ); |
14955 | QChar* uc = result.d->unicode; | 14973 | QChar* uc = result.d->unicode; |
14956 | while (padlen--) | 14974 | while (padlen--) |
14957 | *uc++ = fill; | 14975 | *uc++ = fill; |
14958 | if ( len ) | 14976 | if ( len ) |
14959 | memcpy( uc, unicode(), sizeof(QChar)*len ); | 14977 | memcpy( uc, unicode(), sizeof(QChar)*len ); |
14960 | } else { | 14978 | } else { |
14961 | if ( truncate ) | 14979 | if ( truncate ) |
14962 | result = left( width ); | 14980 | result = left( width ); |
14963 | else | 14981 | else |
14964 | result = *this; | 14982 | result = *this; |
14965 | } | 14983 | } |
14966 | return result; | 14984 | return result; |
14967 | } | 14985 | } |
14968 | 14986 | ||
14969 | /*! | 14987 | /*! |
14970 | Returns a lowercase copy of the string. | 14988 | Returns a lowercase copy of the string. |
14971 | 14989 | ||
14972 | \code | 14990 | \code |
14973 | QString string( "TROlltECH" ); | 14991 | QString string( "TROlltECH" ); |
14974 | str = string.lower(); // str == "trolltech" | 14992 | str = string.lower(); // str == "trolltech" |
14975 | \endcode | 14993 | \endcode |
14976 | 14994 | ||
14977 | \sa upper() | 14995 | \sa upper() |
14978 | */ | 14996 | */ |
14979 | 14997 | ||
14980 | QString QString::lower() const | 14998 | QString QString::lower() const |
14981 | { | 14999 | { |
14982 | QString s(*this); | 15000 | int l = length(); |
14983 | int l=length(); | 15001 | register QChar *p = d->unicode; |
14984 | if ( l ) { | 15002 | while ( l ) { |
14985 | s.real_detach(); // could do this only when we find a change | 15003 | if ( *p != ::lower(*p) ) { |
14986 | register QChar *p=s.d->unicode; | 15004 | QString s( *this ); |
14987 | if ( p ) { | 15005 | s.real_detach(); |
14988 | while ( l-- ) { | 15006 | p = s.d->unicode + ( p - d->unicode ); |
15007 | while ( l ) { | ||
14989 | *p = ::lower( *p ); | 15008 | *p = ::lower( *p ); |
15009 | l--; | ||
14990 | p++; | 15010 | p++; |
14991 | } | 15011 | } |
15012 | return s; | ||
14992 | } | 15013 | } |
15014 | l--; | ||
15015 | p++; | ||
14993 | } | 15016 | } |
14994 | return s; | 15017 | return *this; |
14995 | } | 15018 | } |
14996 | 15019 | ||
14997 | /*! | 15020 | /*! |
14998 | Returns an uppercase copy of the string. | 15021 | Returns an uppercase copy of the string. |
14999 | 15022 | ||
15000 | \code | 15023 | \code |
15001 | QString string( "TeXt" ); | 15024 | QString string( "TeXt" ); |
15002 | str = string.upper(); // t == "TEXT" | 15025 | str = string.upper(); // t == "TEXT" |
15003 | \endcode | 15026 | \endcode |
15004 | 15027 | ||
15005 | \sa lower() | 15028 | \sa lower() |
15006 | */ | 15029 | */ |
15007 | 15030 | ||
15008 | QString QString::upper() const | 15031 | QString QString::upper() const |
15009 | { | 15032 | { |
15010 | QString s(*this); | 15033 | int l = length(); |
15011 | int l=length(); | 15034 | register QChar *p = d->unicode; |
15012 | if ( l ) { | 15035 | while ( l ) { |
15013 | s.real_detach(); // could do this only when we find a change | 15036 | if ( *p != ::upper(*p) ) { |
15014 | register QChar *p=s.d->unicode; | 15037 | QString s( *this ); |
15015 | if ( p ) { | 15038 | s.real_detach(); |
15016 | while ( l-- ) { | 15039 | p = s.d->unicode + ( p - d->unicode ); |
15040 | while ( l ) { | ||
15017 | *p = ::upper( *p ); | 15041 | *p = ::upper( *p ); |
15042 | l--; | ||
15018 | p++; | 15043 | p++; |
15019 | } | 15044 | } |
15045 | return s; | ||
15020 | } | 15046 | } |
15047 | l--; | ||
15048 | p++; | ||
15021 | } | 15049 | } |
15022 | return s; | 15050 | return *this; |
15023 | } | 15051 | } |
15024 | 15052 | ||
15025 | 15053 | ||
15026 | /*! | 15054 | /*! |
15027 | Returns a string that has whitespace removed from the start and | 15055 | Returns a string that has whitespace removed from the start and |
15028 | the end. | 15056 | the end. |
15029 | 15057 | ||
15030 | Whitespace means any character for which QChar::isSpace() returns | 15058 | Whitespace means any character for which QChar::isSpace() returns |
15031 | TRUE. This includes Unicode characters with decimal values 9 | 15059 | TRUE. This includes Unicode characters with decimal values 9 |
15032 | (TAB), 10 (LF), 11 (VT), 12 (FF), 13 (CR) and 32 (Space), and may | 15060 | (TAB), 10 (LF), 11 (VT), 12 (FF), 13 (CR) and 32 (Space), and may |
15033 | also include other Unicode characters. | 15061 | also include other Unicode characters. |
15034 | 15062 | ||
15035 | \code | 15063 | \code |
15036 | QString string = " white space "; | 15064 | QString string = " white space "; |
15037 | QString s = string.stripWhiteSpace(); // s == "white space" | 15065 | QString s = string.stripWhiteSpace(); // s == "white space" |
15038 | \endcode | 15066 | \endcode |
15039 | 15067 | ||
15040 | \sa simplifyWhiteSpace() | 15068 | \sa simplifyWhiteSpace() |
15041 | */ | 15069 | */ |
15042 | 15070 | ||
15043 | QString QString::stripWhiteSpace() const | 15071 | QString QString::stripWhiteSpace() const |
15044 | { | 15072 | { |
15045 | if ( isEmpty() ) // nothing to do | 15073 | if ( isEmpty() ) // nothing to do |
15046 | return *this; | 15074 | return *this; |
15047 | register const QChar *s = unicode(); | 15075 | register const QChar *s = unicode(); |
15048 | if ( !s->isSpace() && !s[length()-1].isSpace() ) | 15076 | if ( !s->isSpace() && !s[length()-1].isSpace() ) |
15049 | return *this; | 15077 | return *this; |
15050 | 15078 | ||
15051 | int start = 0; | 15079 | int start = 0; |
15052 | int end = length() - 1; | 15080 | int end = length() - 1; |
15053 | while ( start<=end && s[start].isSpace() ) // skip white space from start | 15081 | while ( start<=end && s[start].isSpace() ) // skip white space from start |
15054 | start++; | 15082 | start++; |
15055 | if ( start <= end ) { // only white space | 15083 | if ( start <= end ) { // only white space |
15056 | while ( end && s[end].isSpace() ) // skip white space from end | 15084 | while ( end && s[end].isSpace() ) // skip white space from end |
15057 | end--; | 15085 | end--; |
15058 | } | 15086 | } |
15059 | int l = end - start + 1; | 15087 | int l = end - start + 1; |
15060 | if ( l <= 0 ) | 15088 | if ( l <= 0 ) |
15061 | return QString::fromLatin1(""); | 15089 | return QString::fromLatin1(""); |
15062 | 15090 | ||
15063 | QString result( l, TRUE ); | 15091 | QString result( l, TRUE ); |
15064 | memcpy( result.d->unicode, &s[start], sizeof(QChar)*l ); | 15092 | memcpy( result.d->unicode, &s[start], sizeof(QChar)*l ); |
15065 | result.d->len = l; | 15093 | result.d->len = l; |
15066 | return result; | 15094 | return result; |
15067 | } | 15095 | } |
15068 | 15096 | ||
15069 | 15097 | ||
15070 | /*! | 15098 | /*! |
@@ -15092,98 +15120,98 @@ QString QString::simplifyWhiteSpace() const | |||
15092 | QString result; | 15120 | QString result; |
15093 | result.setLength( length() ); | 15121 | result.setLength( length() ); |
15094 | const QChar *from = unicode(); | 15122 | const QChar *from = unicode(); |
15095 | const QChar *fromend = from+length(); | 15123 | const QChar *fromend = from+length(); |
15096 | int outc=0; | 15124 | int outc=0; |
15097 | QChar *to = result.d->unicode; | 15125 | QChar *to = result.d->unicode; |
15098 | for (;;) { | 15126 | for (;;) { |
15099 | while ( from!=fromend && from->isSpace() ) | 15127 | while ( from!=fromend && from->isSpace() ) |
15100 | from++; | 15128 | from++; |
15101 | while ( from!=fromend && !from->isSpace() ) | 15129 | while ( from!=fromend && !from->isSpace() ) |
15102 | to[outc++] = *from++; | 15130 | to[outc++] = *from++; |
15103 | if ( from!=fromend ) | 15131 | if ( from!=fromend ) |
15104 | to[outc++] = ' '; | 15132 | to[outc++] = ' '; |
15105 | else | 15133 | else |
15106 | break; | 15134 | break; |
15107 | } | 15135 | } |
15108 | if ( outc > 0 && to[outc-1] == ' ' ) | 15136 | if ( outc > 0 && to[outc-1] == ' ' ) |
15109 | outc--; | 15137 | outc--; |
15110 | result.truncate( outc ); | 15138 | result.truncate( outc ); |
15111 | return result; | 15139 | return result; |
15112 | } | 15140 | } |
15113 | 15141 | ||
15114 | 15142 | ||
15115 | /*! | 15143 | /*! |
15116 | Inserts \a s into the string at position \a index. | 15144 | Inserts \a s into the string at position \a index. |
15117 | 15145 | ||
15118 | If \a index is beyond the end of the string, the string is | 15146 | If \a index is beyond the end of the string, the string is |
15119 | extended with spaces to length \a index and \a s is then appended | 15147 | extended with spaces to length \a index and \a s is then appended |
15120 | and returns a reference to the string. | 15148 | and returns a reference to the string. |
15121 | 15149 | ||
15122 | \code | 15150 | \code |
15123 | QString string( "I like fish" ); | 15151 | QString string( "I like fish" ); |
15124 | str = string.insert( 2, "don't " ); | 15152 | str = string.insert( 2, "don't " ); |
15125 | // str == "I don't like fish" | 15153 | // str == "I don't like fish" |
15126 | \endcode | 15154 | \endcode |
15127 | 15155 | ||
15128 | \sa remove(), replace() | 15156 | \sa remove(), replace() |
15129 | */ | 15157 | */ |
15130 | 15158 | ||
15131 | QString &QString::insert( uint index, const QString &s ) | 15159 | QString &QString::insert( uint index, const QString &s ) |
15132 | { | 15160 | { |
15133 | // the sub function takes care of &s == this case. | 15161 | // the sub function takes care of &s == this case. |
15134 | return insert( index, s.unicode(), s.length() ); | 15162 | return insert( index, s.unicode(), s.length() ); |
15135 | } | 15163 | } |
15136 | 15164 | ||
15137 | /*! | 15165 | /*! |
15138 | \overload | 15166 | \overload |
15139 | 15167 | ||
15140 | Inserts the character in \a s into the string at position \a index | 15168 | Inserts the first \a len characters in \a s into the string at |
15141 | \a len number of times and returns a reference to the string. | 15169 | position \a index and returns a reference to the string. |
15142 | */ | 15170 | */ |
15143 | 15171 | ||
15144 | QString &QString::insert( uint index, const QChar* s, uint len ) | 15172 | QString &QString::insert( uint index, const QChar* s, uint len ) |
15145 | { | 15173 | { |
15146 | if ( len == 0 ) | 15174 | if ( len == 0 ) |
15147 | return *this; | 15175 | return *this; |
15148 | uint olen = length(); | 15176 | uint olen = length(); |
15149 | int nlen = olen + len; | 15177 | int nlen = olen + len; |
15150 | 15178 | ||
15151 | if ( s >= d->unicode && (uint)(s - d->unicode) < d->maxl ) { | 15179 | if ( s >= d->unicode && (uint)(s - d->unicode) < d->maxl ) { |
15152 | // Part of me - take a copy. | 15180 | // Part of me - take a copy. |
15153 | QChar *tmp = QT_ALLOC_QCHAR_VEC( len ); | 15181 | QChar *tmp = QT_ALLOC_QCHAR_VEC( len ); |
15154 | memcpy(tmp,s,len*sizeof(QChar)); | 15182 | memcpy(tmp,s,len*sizeof(QChar)); |
15155 | insert(index,tmp,len); | 15183 | insert(index,tmp,len); |
15156 | QT_DELETE_QCHAR_VEC( tmp ); | 15184 | QT_DELETE_QCHAR_VEC( tmp ); |
15157 | return *this; | 15185 | return *this; |
15158 | } | 15186 | } |
15159 | 15187 | ||
15160 | if ( index >= olen ) { // insert after end of string | 15188 | if ( index >= olen ) { // insert after end of string |
15161 | setLength( len + index ); | 15189 | setLength( len + index ); |
15162 | int n = index - olen; | 15190 | int n = index - olen; |
15163 | QChar* uc = d->unicode+olen; | 15191 | QChar* uc = d->unicode+olen; |
15164 | while (n--) | 15192 | while (n--) |
15165 | *uc++ = ' '; | 15193 | *uc++ = ' '; |
15166 | memcpy( d->unicode+index, s, sizeof(QChar)*len ); | 15194 | memcpy( d->unicode+index, s, sizeof(QChar)*len ); |
15167 | } else { // normal insert | 15195 | } else { // normal insert |
15168 | setLength( nlen ); | 15196 | setLength( nlen ); |
15169 | memmove( d->unicode + index + len, unicode() + index, | 15197 | memmove( d->unicode + index + len, unicode() + index, |
15170 | sizeof(QChar) * (olen - index) ); | 15198 | sizeof(QChar) * (olen - index) ); |
15171 | memcpy( d->unicode + index, s, sizeof(QChar) * len ); | 15199 | memcpy( d->unicode + index, s, sizeof(QChar) * len ); |
15172 | } | 15200 | } |
15173 | return *this; | 15201 | return *this; |
15174 | } | 15202 | } |
15175 | 15203 | ||
15176 | /*! | 15204 | /*! |
15177 | \overload | 15205 | \overload |
15178 | 15206 | ||
15179 | Insert \a c into the string at position \a index and returns a | 15207 | Insert \a c into the string at position \a index and returns a |
15180 | reference to the string. | 15208 | reference to the string. |
15181 | 15209 | ||
15182 | If \a index is beyond the end of the string, the string is | 15210 | If \a index is beyond the end of the string, the string is |
15183 | extended with spaces (ASCII 32) to length \a index and \a c is | 15211 | extended with spaces (ASCII 32) to length \a index and \a c is |
15184 | then appended. | 15212 | then appended. |
15185 | */ | 15213 | */ |
15186 | 15214 | ||
15187 | QString &QString::insert( uint index, QChar c ) // insert char | 15215 | QString &QString::insert( uint index, QChar c ) // insert char |
15188 | { | 15216 | { |
15189 | QString s( c ); | 15217 | QString s( c ); |
@@ -15206,96 +15234,106 @@ QString &QString::insert( uint index, QChar c ) // insert char | |||
15206 | 15234 | ||
15207 | Equivalent to insert(0, \a s). | 15235 | Equivalent to insert(0, \a s). |
15208 | 15236 | ||
15209 | \code | 15237 | \code |
15210 | QString string = "42"; | 15238 | QString string = "42"; |
15211 | string.prepend( "The answer is " ); | 15239 | string.prepend( "The answer is " ); |
15212 | // string == "The answer is 42" | 15240 | // string == "The answer is 42" |
15213 | \endcode | 15241 | \endcode |
15214 | 15242 | ||
15215 | \sa insert() | 15243 | \sa insert() |
15216 | */ | 15244 | */ |
15217 | 15245 | ||
15218 | /*! | 15246 | /*! |
15219 | \fn QString& QString::prepend( char ch ) | 15247 | \fn QString& QString::prepend( char ch ) |
15220 | 15248 | ||
15221 | \overload | 15249 | \overload |
15222 | 15250 | ||
15223 | Inserts \a ch at the beginning of the string and returns a | 15251 | Inserts \a ch at the beginning of the string and returns a |
15224 | reference to the string. | 15252 | reference to the string. |
15225 | 15253 | ||
15226 | Equivalent to insert(0, \a ch). | 15254 | Equivalent to insert(0, \a ch). |
15227 | 15255 | ||
15228 | \sa insert() | 15256 | \sa insert() |
15229 | */ | 15257 | */ |
15230 | 15258 | ||
15231 | /*! | 15259 | /*! |
15232 | \fn QString& QString::prepend( QChar ch ) | 15260 | \fn QString& QString::prepend( QChar ch ) |
15233 | 15261 | ||
15234 | \overload | 15262 | \overload |
15235 | 15263 | ||
15236 | Inserts \a ch at the beginning of the string and returns a | 15264 | Inserts \a ch at the beginning of the string and returns a |
15237 | reference to the string. | 15265 | reference to the string. |
15238 | 15266 | ||
15239 | Equivalent to insert(0, \a ch). | 15267 | Equivalent to insert(0, \a ch). |
15240 | 15268 | ||
15241 | \sa insert() | 15269 | \sa insert() |
15242 | */ | 15270 | */ |
15243 | 15271 | ||
15244 | /*! \fn QString& QString::prepend( const QByteArray &s ) | 15272 | /*! \fn QString& QString::prepend( const QByteArray &s ) |
15245 | \overload | 15273 | \overload |
15246 | 15274 | ||
15247 | Inserts \a s at the beginning of the string and returns a reference to the string. | 15275 | Inserts \a s at the beginning of the string and returns a reference to the string. |
15248 | 15276 | ||
15249 | Equivalent to insert(0, \a s). | 15277 | Equivalent to insert(0, \a s). |
15250 | 15278 | ||
15251 | \sa insert() | 15279 | \sa insert() |
15252 | */ | 15280 | */ |
15253 | 15281 | ||
15282 | /*! \fn QString& QString::prepend( const std::string &s ) | ||
15283 | \overload | ||
15284 | |||
15285 | Inserts \a s at the beginning of the string and returns a reference to the string. | ||
15286 | |||
15287 | Equivalent to insert(0, \a s). | ||
15288 | |||
15289 | \sa insert() | ||
15290 | */ | ||
15291 | |||
15254 | /*! | 15292 | /*! |
15255 | \overload | 15293 | \overload |
15256 | 15294 | ||
15257 | Inserts \a s at the beginning of the string and returns a reference to the string. | 15295 | Inserts \a s at the beginning of the string and returns a reference to the string. |
15258 | 15296 | ||
15259 | Equivalent to insert(0, \a s). | 15297 | Equivalent to insert(0, \a s). |
15260 | 15298 | ||
15261 | \sa insert() | 15299 | \sa insert() |
15262 | */ | 15300 | */ |
15263 | QString &QString::prepend( const char *s ) | 15301 | QString &QString::prepend( const char *s ) |
15264 | { | 15302 | { |
15265 | return insert( 0, QString(s) ); | 15303 | return insert( 0, QString(s) ); |
15266 | } | 15304 | } |
15267 | 15305 | ||
15268 | /*! | 15306 | /*! |
15269 | Removes \a len characters from the string starting at position \a | 15307 | Removes \a len characters from the string starting at position \a |
15270 | index, and returns a reference to the string. | 15308 | index, and returns a reference to the string. |
15271 | 15309 | ||
15272 | If \a index is beyond the length of the string, nothing happens. | 15310 | If \a index is beyond the length of the string, nothing happens. |
15273 | If \a index is within the string, but \a index + \a len is beyond | 15311 | If \a index is within the string, but \a index + \a len is beyond |
15274 | the end of the string, the string is truncated at position \a | 15312 | the end of the string, the string is truncated at position \a |
15275 | index. | 15313 | index. |
15276 | 15314 | ||
15277 | \code | 15315 | \code |
15278 | QString string( "Montreal" ); | 15316 | QString string( "Montreal" ); |
15279 | string.remove( 1, 4 ); // string == "Meal" | 15317 | string.remove( 1, 4 ); // string == "Meal" |
15280 | \endcode | 15318 | \endcode |
15281 | 15319 | ||
15282 | \sa insert(), replace() | 15320 | \sa insert(), replace() |
15283 | */ | 15321 | */ |
15284 | 15322 | ||
15285 | QString &QString::remove( uint index, uint len ) | 15323 | QString &QString::remove( uint index, uint len ) |
15286 | { | 15324 | { |
15287 | uint olen = length(); | 15325 | uint olen = length(); |
15288 | if ( index >= olen ) { | 15326 | if ( index >= olen ) { |
15289 | // range problems | 15327 | // range problems |
15290 | } else if ( index + len >= olen ) { // index ok | 15328 | } else if ( index + len >= olen ) { // index ok |
15291 | setLength( index ); | 15329 | setLength( index ); |
15292 | } else if ( len != 0 ) { | 15330 | } else if ( len != 0 ) { |
15293 | real_detach(); | 15331 | real_detach(); |
15294 | memmove( d->unicode+index, d->unicode+index+len, | 15332 | memmove( d->unicode+index, d->unicode+index+len, |
15295 | sizeof(QChar)*(olen-index-len) ); | 15333 | sizeof(QChar)*(olen-index-len) ); |
15296 | setLength( olen-len ); | 15334 | setLength( olen-len ); |
15297 | } | 15335 | } |
15298 | return *this; | 15336 | return *this; |
15299 | } | 15337 | } |
15300 | 15338 | ||
15301 | /*! \overload | 15339 | /*! \overload |
@@ -15803,351 +15841,359 @@ int QString::findRev( const QRegExp &rx, int index ) const | |||
15803 | 15841 | ||
15804 | \code | 15842 | \code |
15805 | QString str = "banana and panama"; | 15843 | QString str = "banana and panama"; |
15806 | QRegExp rxp = QRegExp( "a[nm]a", TRUE, FALSE ); | 15844 | QRegExp rxp = QRegExp( "a[nm]a", TRUE, FALSE ); |
15807 | int i = str.contains( rxp ); // i == 4 | 15845 | int i = str.contains( rxp ); // i == 4 |
15808 | \endcode | 15846 | \endcode |
15809 | 15847 | ||
15810 | \sa find() findRev() | 15848 | \sa find() findRev() |
15811 | */ | 15849 | */ |
15812 | 15850 | ||
15813 | int QString::contains( const QRegExp &rx ) const | 15851 | int QString::contains( const QRegExp &rx ) const |
15814 | { | 15852 | { |
15815 | int count = 0; | 15853 | int count = 0; |
15816 | int index = -1; | 15854 | int index = -1; |
15817 | int len = length(); | 15855 | int len = length(); |
15818 | while ( index < len - 1 ) { // count overlapping matches | 15856 | while ( index < len - 1 ) { // count overlapping matches |
15819 | index = rx.search( *this, index + 1 ); | 15857 | index = rx.search( *this, index + 1 ); |
15820 | if ( index == -1 ) | 15858 | if ( index == -1 ) |
15821 | break; | 15859 | break; |
15822 | count++; | 15860 | count++; |
15823 | } | 15861 | } |
15824 | return count; | 15862 | return count; |
15825 | } | 15863 | } |
15826 | 15864 | ||
15827 | #endif //QT_NO_REGEXP | 15865 | #endif //QT_NO_REGEXP |
15828 | 15866 | ||
15829 | static bool ok_in_base( QChar c, int base ) | 15867 | static bool ok_in_base( QChar c, int base ) |
15830 | { | 15868 | { |
15831 | if ( base <= 10 ) | 15869 | if ( base <= 10 ) |
15832 | return c.isDigit() && c.digitValue() < base; | 15870 | return c.isDigit() && c.digitValue() < base; |
15833 | else | 15871 | else |
15834 | return c.isDigit() || (c >= 'a' && c < char('a'+base-10)) | 15872 | return c.isDigit() || (c >= 'a' && c < char('a'+base-10)) |
15835 | || (c >= 'A' && c < char('A'+base-10)); | 15873 | || (c >= 'A' && c < char('A'+base-10)); |
15836 | } | 15874 | } |
15837 | 15875 | ||
15838 | /*! | 15876 | /*! |
15839 | Returns the string converted to a \c long value to the base \a | 15877 | Returns the string converted to a \c long value to the base \a |
15840 | base, which is 10 by default and must be between 2 and 36. | 15878 | base, which is 10 by default and must be between 2 and 36. |
15841 | 15879 | ||
15842 | If \a ok is not 0: if a conversion error occurs, \a *ok is set to | 15880 | If \a ok is not 0: if a conversion error occurs, \a *ok is set to |
15843 | FALSE; otherwise \a *ok is set to TRUE. | 15881 | FALSE; otherwise \a *ok is set to TRUE. |
15844 | 15882 | ||
15845 | \sa number() | 15883 | \sa number() |
15846 | */ | 15884 | */ |
15847 | 15885 | ||
15848 | long QString::toLong( bool *ok, int base ) const | 15886 | long QString::toLong( bool *ok, int base ) const |
15849 | { | 15887 | { |
15850 | const QChar *p = unicode(); | 15888 | const QChar *p = unicode(); |
15851 | long val = 0; | 15889 | ulong val = 0; |
15852 | int l = length(); | 15890 | int l = length(); |
15853 | const long max_mult = INT_MAX / base; | 15891 | const ulong max_mult = LONG_MAX / base; |
15854 | bool is_ok = FALSE; | 15892 | bool is_ok = FALSE; |
15855 | int neg = 0; | 15893 | int neg = 0; |
15856 | if ( !p ) | 15894 | if ( !p ) |
15857 | goto bye; | 15895 | goto bye; |
15858 | while ( l && p->isSpace() ) // skip leading space | 15896 | while ( l && p->isSpace() ) // skip leading space |
15859 | l--,p++; | 15897 | l--,p++; |
15860 | if ( !l ) | 15898 | if ( !l ) |
15861 | goto bye; | 15899 | goto bye; |
15862 | if ( *p == '-' ) { | 15900 | if ( *p == '-' ) { |
15863 | l--; | 15901 | l--; |
15864 | p++; | 15902 | p++; |
15865 | neg = 1; | 15903 | neg = 1; |
15866 | } else if ( *p == '+' ) { | 15904 | } else if ( *p == '+' ) { |
15867 | l--; | 15905 | l--; |
15868 | p++; | 15906 | p++; |
15869 | } | 15907 | } |
15870 | 15908 | ||
15871 | // NOTE: toULong() code is similar | 15909 | // NOTE: toULong() code is similar |
15872 | if ( !l || !ok_in_base(*p,base) ) | 15910 | if ( !l || !ok_in_base(*p,base) ) |
15873 | goto bye; | 15911 | goto bye; |
15874 | while ( l && ok_in_base(*p,base) ) { | 15912 | while ( l && ok_in_base(*p,base) ) { |
15875 | l--; | 15913 | l--; |
15876 | int dv; | 15914 | int dv; |
15877 | if ( p->isDigit() ) { | 15915 | if ( p->isDigit() ) { |
15878 | dv = p->digitValue(); | 15916 | dv = p->digitValue(); |
15879 | } else { | 15917 | } else { |
15880 | if ( *p >= 'a' && *p <= 'z' ) | 15918 | if ( *p >= 'a' && *p <= 'z' ) |
15881 | dv = *p - 'a' + 10; | 15919 | dv = *p - 'a' + 10; |
15882 | else | 15920 | else |
15883 | dv = *p - 'A' + 10; | 15921 | dv = *p - 'A' + 10; |
15884 | } | 15922 | } |
15885 | if ( val > max_mult || | 15923 | if ( val > max_mult || |
15886 | (val == max_mult && dv > (INT_MAX % base) + neg) ) | 15924 | (val == max_mult && dv > (LONG_MAX % base) + neg) ) |
15887 | goto bye; | 15925 | goto bye; |
15888 | val = base * val + dv; | 15926 | val = base * val + dv; |
15889 | p++; | 15927 | p++; |
15890 | } | 15928 | } |
15891 | if ( neg ) | ||
15892 | val = -val; | ||
15893 | while ( l && p->isSpace() ) // skip trailing space | 15929 | while ( l && p->isSpace() ) // skip trailing space |
15894 | l--,p++; | 15930 | l--, p++; |
15895 | if ( !l ) | 15931 | if ( !l ) |
15896 | is_ok = TRUE; | 15932 | is_ok = TRUE; |
15897 | bye: | 15933 | bye: |
15898 | if ( ok ) | 15934 | if ( ok ) |
15899 | *ok = is_ok; | 15935 | *ok = is_ok; |
15900 | return is_ok ? val : 0; | 15936 | return is_ok ? ( neg ? -( (long) val ) : (long) val ) : 0L; |
15901 | } | 15937 | } |
15902 | 15938 | ||
15903 | /*! | 15939 | /*! |
15904 | Returns the string converted to an \c {unsigned long} value to the | 15940 | Returns the string converted to an \c {unsigned long} value to the |
15905 | base \a base, which is 10 by default and must be between 2 and 36. | 15941 | base \a base, which is 10 by default and must be between 2 and 36. |
15906 | 15942 | ||
15907 | If \a ok is not 0: if a conversion error occurs, \a *ok is set to | 15943 | If \a ok is not 0: if a conversion error occurs, \a *ok is set to |
15908 | FALSE; otherwise \a *ok is set to TRUE. | 15944 | FALSE; otherwise \a *ok is set to TRUE. |
15909 | 15945 | ||
15910 | \sa number() | 15946 | \sa number() |
15911 | */ | 15947 | */ |
15912 | 15948 | ||
15913 | ulong QString::toULong( bool *ok, int base ) const | 15949 | ulong QString::toULong( bool *ok, int base ) const |
15914 | { | 15950 | { |
15915 | const QChar *p = unicode(); | 15951 | const QChar *p = unicode(); |
15916 | ulong val = 0; | 15952 | ulong val = 0; |
15917 | int l = length(); | 15953 | int l = length(); |
15918 | const ulong max_mult = UINT_MAX / base; | 15954 | const ulong max_mult = ULONG_MAX / base; |
15919 | bool is_ok = FALSE; | 15955 | bool is_ok = FALSE; |
15920 | if ( !p ) | 15956 | if ( !p ) |
15921 | goto bye; | 15957 | goto bye; |
15922 | while ( l && p->isSpace() ) // skip leading space | 15958 | while ( l && p->isSpace() ) // skip leading space |
15923 | l--,p++; | 15959 | l--,p++; |
15924 | if ( !l ) | 15960 | if ( !l ) |
15925 | goto bye; | 15961 | goto bye; |
15926 | if ( *p == '+' ) | 15962 | if ( *p == '+' ) |
15927 | l--,p++; | 15963 | l--,p++; |
15928 | 15964 | ||
15929 | // NOTE: toLong() code is similar | 15965 | // NOTE: toLong() code is similar |
15930 | if ( !l || !ok_in_base(*p,base) ) | 15966 | if ( !l || !ok_in_base(*p,base) ) |
15931 | goto bye; | 15967 | goto bye; |
15932 | while ( l && ok_in_base(*p,base) ) { | 15968 | while ( l && ok_in_base(*p,base) ) { |
15933 | l--; | 15969 | l--; |
15934 | uint dv; | 15970 | uint dv; |
15935 | if ( p->isDigit() ) { | 15971 | if ( p->isDigit() ) { |
15936 | dv = p->digitValue(); | 15972 | dv = p->digitValue(); |
15937 | } else { | 15973 | } else { |
15938 | if ( *p >= 'a' && *p <= 'z' ) | 15974 | if ( *p >= 'a' && *p <= 'z' ) |
15939 | dv = *p - 'a' + 10; | 15975 | dv = *p - 'a' + 10; |
15940 | else | 15976 | else |
15941 | dv = *p - 'A' + 10; | 15977 | dv = *p - 'A' + 10; |
15942 | } | 15978 | } |
15943 | if ( val > max_mult || (val == max_mult && dv > UINT_MAX % base) ) | 15979 | if ( val > max_mult || (val == max_mult && dv > ULONG_MAX % base) ) |
15944 | goto bye; | 15980 | goto bye; |
15945 | val = base * val + dv; | 15981 | val = base * val + dv; |
15946 | p++; | 15982 | p++; |
15947 | } | 15983 | } |
15948 | 15984 | ||
15949 | while ( l && p->isSpace() ) // skip trailing space | 15985 | while ( l && p->isSpace() ) // skip trailing space |
15950 | l--,p++; | 15986 | l--,p++; |
15951 | if ( !l ) | 15987 | if ( !l ) |
15952 | is_ok = TRUE; | 15988 | is_ok = TRUE; |
15953 | bye: | 15989 | bye: |
15954 | if ( ok ) | 15990 | if ( ok ) |
15955 | *ok = is_ok; | 15991 | *ok = is_ok; |
15956 | return is_ok ? val : 0; | 15992 | return is_ok ? val : 0; |
15957 | } | 15993 | } |
15958 | 15994 | ||
15959 | /*! | 15995 | /*! |
15960 | Returns the string converted to a \c short value to the base \a | 15996 | Returns the string converted to a \c short value to the base \a |
15961 | base, which is 10 by default and must be between 2 and 36. | 15997 | base, which is 10 by default and must be between 2 and 36. |
15962 | 15998 | ||
15963 | If \a ok is not 0: if a conversion error occurs, \a *ok is set to | 15999 | If \a ok is not 0: if a conversion error occurs, \a *ok is set to |
15964 | FALSE; otherwise \a *ok is set to TRUE. | 16000 | FALSE; otherwise \a *ok is set to TRUE. |
15965 | */ | 16001 | */ |
15966 | 16002 | ||
15967 | short QString::toShort( bool *ok, int base ) const | 16003 | short QString::toShort( bool *ok, int base ) const |
15968 | { | 16004 | { |
15969 | long v = toLong( ok, base ); | 16005 | long v = toLong( ok, base ); |
15970 | if ( ok && *ok && (v < -32768 || v > 32767) ) { | 16006 | if ( ok && *ok && (v < SHRT_MIN || v > SHRT_MAX) ) { |
15971 | *ok = FALSE; | 16007 | *ok = FALSE; |
15972 | v = 0; | 16008 | v = 0; |
15973 | } | 16009 | } |
15974 | return (short)v; | 16010 | return (short)v; |
15975 | } | 16011 | } |
15976 | 16012 | ||
15977 | /*! | 16013 | /*! |
15978 | Returns the string converted to an \c {unsigned short} value to | 16014 | Returns the string converted to an \c {unsigned short} value to |
15979 | the base \a base, which is 10 by default and must be between 2 and | 16015 | the base \a base, which is 10 by default and must be between 2 and |
15980 | 36. | 16016 | 36. |
15981 | 16017 | ||
15982 | If \a ok is not 0: if a conversion error occurs, \a *ok is set to | 16018 | If \a ok is not 0: if a conversion error occurs, \a *ok is set to |
15983 | FALSE; otherwise \a *ok is set to TRUE. | 16019 | FALSE; otherwise \a *ok is set to TRUE. |
15984 | */ | 16020 | */ |
15985 | 16021 | ||
15986 | ushort QString::toUShort( bool *ok, int base ) const | 16022 | ushort QString::toUShort( bool *ok, int base ) const |
15987 | { | 16023 | { |
15988 | ulong v = toULong( ok, base ); | 16024 | ulong v = toULong( ok, base ); |
15989 | if ( ok && *ok && (v > 65535) ) { | 16025 | if ( ok && *ok && (v > USHRT_MAX) ) { |
15990 | *ok = FALSE; | 16026 | *ok = FALSE; |
15991 | v = 0; | 16027 | v = 0; |
15992 | } | 16028 | } |
15993 | return (ushort)v; | 16029 | return (ushort)v; |
15994 | } | 16030 | } |
15995 | 16031 | ||
15996 | 16032 | ||
15997 | /*! | 16033 | /*! |
15998 | Returns the string converted to an \c int value to the base \a | 16034 | Returns the string converted to an \c int value to the base \a |
15999 | base, which is 10 by default and must be between 2 and 36. | 16035 | base, which is 10 by default and must be between 2 and 36. |
16000 | 16036 | ||
16001 | If \a ok is not 0: if a conversion error occurs, \a *ok is set to | 16037 | If \a ok is not 0: if a conversion error occurs, \a *ok is set to |
16002 | FALSE; otherwise \a *ok is set to TRUE. | 16038 | FALSE; otherwise \a *ok is set to TRUE. |
16003 | 16039 | ||
16004 | \code | 16040 | \code |
16005 | QString str( "FF" ); | 16041 | QString str( "FF" ); |
16006 | bool ok; | 16042 | bool ok; |
16007 | int hex = str.toInt( &ok, 16 ); // hex == 255, ok == TRUE | 16043 | int hex = str.toInt( &ok, 16 ); // hex == 255, ok == TRUE |
16008 | int dec = str.toInt( &ok, 10 ); // dec == 0, ok == FALSE | 16044 | int dec = str.toInt( &ok, 10 ); // dec == 0, ok == FALSE |
16009 | \endcode | 16045 | \endcode |
16010 | 16046 | ||
16011 | \sa number() | 16047 | \sa number() |
16012 | */ | 16048 | */ |
16013 | 16049 | ||
16014 | int QString::toInt( bool *ok, int base ) const | 16050 | int QString::toInt( bool *ok, int base ) const |
16015 | { | 16051 | { |
16016 | return (int)toLong( ok, base ); | 16052 | long v = toLong( ok, base ); |
16053 | if ( ok && *ok && (v < INT_MIN || v > INT_MAX) ) { | ||
16054 | *ok = FALSE; | ||
16055 | v = 0; | ||
16056 | } | ||
16057 | return (int)v; | ||
16017 | } | 16058 | } |
16018 | 16059 | ||
16019 | /*! | 16060 | /*! |
16020 | Returns the string converted to an \c{unsigned int} value to the | 16061 | Returns the string converted to an \c{unsigned int} value to the |
16021 | base \a base, which is 10 by default and must be between 2 and 36. | 16062 | base \a base, which is 10 by default and must be between 2 and 36. |
16022 | 16063 | ||
16023 | If \a ok is not 0: if a conversion error occurs, \a *ok is set to | 16064 | If \a ok is not 0: if a conversion error occurs, \a *ok is set to |
16024 | FALSE; otherwise \a *ok is set to TRUE. | 16065 | FALSE; otherwise \a *ok is set to TRUE. |
16025 | 16066 | ||
16026 | \sa number() | 16067 | \sa number() |
16027 | */ | 16068 | */ |
16028 | 16069 | ||
16029 | uint QString::toUInt( bool *ok, int base ) const | 16070 | uint QString::toUInt( bool *ok, int base ) const |
16030 | { | 16071 | { |
16031 | return (uint)toULong( ok, base ); | 16072 | ulong v = toULong( ok, base ); |
16073 | if ( ok && *ok && (v > UINT_MAX) ) { | ||
16074 | *ok = FALSE; | ||
16075 | v = 0; | ||
16076 | } | ||
16077 | return (uint)v; | ||
16032 | } | 16078 | } |
16033 | 16079 | ||
16034 | /*! | 16080 | /*! |
16035 | Returns the string converted to a \c double value. | 16081 | Returns the string converted to a \c double value. |
16036 | 16082 | ||
16037 | If \a ok is not 0: if a conversion error occurs, \a *ok is set to | 16083 | If \a ok is not 0: if a conversion error occurs, \a *ok is set to |
16038 | FALSE; otherwise \a *ok is set to TRUE. | 16084 | FALSE; otherwise \a *ok is set to TRUE. |
16039 | 16085 | ||
16040 | \code | 16086 | \code |
16041 | QString string( "1234.56" ); | 16087 | QString string( "1234.56" ); |
16042 | double a = string.toDouble(); // a == 1234.56 | 16088 | double a = string.toDouble(); // a == 1234.56 |
16043 | \endcode | 16089 | \endcode |
16044 | 16090 | ||
16045 | \sa number() | 16091 | \sa number() |
16046 | */ | 16092 | */ |
16047 | 16093 | ||
16048 | double QString::toDouble( bool *ok ) const | 16094 | double QString::toDouble( bool *ok ) const |
16049 | { | 16095 | { |
16050 | char *end; | 16096 | char *end; |
16051 | 16097 | ||
16052 | const char *a = latin1(); | 16098 | const char *a = latin1(); |
16053 | double val = strtod( a ? a : "", &end ); | 16099 | double val = strtod( a ? a : "", &end ); |
16054 | if ( ok ) | 16100 | if ( ok ) |
16055 | *ok = ( a && *a && (end == 0 || (end - a) == (int)length()) ); | 16101 | *ok = ( a && *a && (end == 0 || *end == '\0') ); |
16056 | return val; | 16102 | return val; |
16057 | } | 16103 | } |
16058 | 16104 | ||
16059 | /*! | 16105 | /*! |
16060 | Returns the string converted to a \c float value. | 16106 | Returns the string converted to a \c float value. |
16061 | 16107 | ||
16062 | If \a ok is not 0: if a conversion error occurs, \a *ok is set to | 16108 | If \a ok is not 0: if a conversion error occurs, \a *ok is set to |
16063 | FALSE; otherwise \a *ok is set to TRUE. | 16109 | FALSE; otherwise \a *ok is set to TRUE. |
16064 | 16110 | ||
16065 | \sa number() | 16111 | \sa number() |
16066 | */ | 16112 | */ |
16067 | 16113 | ||
16068 | float QString::toFloat( bool *ok ) const | 16114 | float QString::toFloat( bool *ok ) const |
16069 | { | 16115 | { |
16070 | return (float)toDouble( ok ); | 16116 | return (float)toDouble( ok ); |
16071 | } | 16117 | } |
16072 | 16118 | ||
16073 | 16119 | ||
16074 | /*! | 16120 | /*! |
16075 | Sets the string to the printed value of \a n in base \a base and | 16121 | Sets the string to the printed value of \a n in base \a base and |
16076 | returns a reference to the string. | 16122 | returns a reference to the string. |
16077 | 16123 | ||
16078 | The base is 10 by default and must be between 2 and 36. | 16124 | The base is 10 by default and must be between 2 and 36. |
16079 | 16125 | ||
16080 | \code | 16126 | \code |
16081 | QString string; | 16127 | QString string; |
16082 | string = string.setNum( 1234 ); // string == "1234" | 16128 | string = string.setNum( 1234 ); // string == "1234" |
16083 | \endcode | 16129 | \endcode |
16084 | */ | 16130 | */ |
16085 | 16131 | ||
16086 | QString &QString::setNum( long n, int base ) | 16132 | QString &QString::setNum( long n, int base ) |
16087 | { | 16133 | { |
16088 | #if defined(QT_CHECK_RANGE) | 16134 | #if defined(QT_CHECK_RANGE) |
16089 | if ( base < 2 || base > 36 ) { | 16135 | if ( base < 2 || base > 36 ) { |
16090 | qWarning( "QString::setNum: Invalid base %d", base ); | 16136 | qWarning( "QString::setNum: Invalid base %d", base ); |
16091 | base = 10; | 16137 | base = 10; |
16092 | } | 16138 | } |
16093 | #endif | 16139 | #endif |
16094 | char charbuf[65*sizeof(QChar)]; | 16140 | char charbuf[65*sizeof(QChar)]; |
16095 | QChar *buf = (QChar*)charbuf; | 16141 | QChar *buf = (QChar*)charbuf; |
16096 | QChar *p = &buf[64]; | 16142 | QChar *p = &buf[64]; |
16097 | int len = 0; | 16143 | int len = 0; |
16098 | bool neg; | 16144 | bool neg; |
16099 | if ( n < 0 ) { | 16145 | if ( n < 0 ) { |
16100 | neg = TRUE; | 16146 | neg = TRUE; |
16101 | if ( n == INT_MIN ) { | 16147 | if ( n == LONG_MIN ) { |
16102 | // Cannot always negate this special case | 16148 | // Cannot always negate this special case |
16103 | QString s1, s2; | 16149 | QString s1, s2; |
16104 | s1.setNum(n/base); | 16150 | s1.setNum(n/base, base ); |
16105 | s2.setNum((-(n+base))%base); | 16151 | s2.setNum((-(n+base))%base, base ); |
16106 | *this = s1 + s2; | 16152 | *this = s1 + s2; |
16107 | return *this; | 16153 | return *this; |
16108 | } | 16154 | } |
16109 | n = -n; | 16155 | n = -n; |
16110 | } else { | 16156 | } else { |
16111 | neg = FALSE; | 16157 | neg = FALSE; |
16112 | } | 16158 | } |
16113 | do { | 16159 | do { |
16114 | *--p = "0123456789abcdefghijklmnopqrstuvwxyz"[((int)(n%base))]; | 16160 | *--p = "0123456789abcdefghijklmnopqrstuvwxyz"[((int)(n%base))]; |
16115 | n /= base; | 16161 | n /= base; |
16116 | ++len; | 16162 | ++len; |
16117 | } while ( n ); | 16163 | } while ( n ); |
16118 | if ( neg ) { | 16164 | if ( neg ) { |
16119 | *--p = '-'; | 16165 | *--p = '-'; |
16120 | ++len; | 16166 | ++len; |
16121 | } | 16167 | } |
16122 | return setUnicode( p, len ); | 16168 | return setUnicode( p, len ); |
16123 | } | 16169 | } |
16124 | 16170 | ||
16125 | /*! | 16171 | /*! |
16126 | \overload | 16172 | \overload |
16127 | 16173 | ||
16128 | Sets the string to the printed value of \a n in base \a base and | 16174 | Sets the string to the printed value of \a n in base \a base and |
16129 | returns a reference to the string. | 16175 | returns a reference to the string. |
16130 | 16176 | ||
16131 | The base is 10 by default and must be between 2 and 36. | 16177 | The base is 10 by default and must be between 2 and 36. |
16132 | */ | 16178 | */ |
16133 | 16179 | ||
16134 | QString &QString::setNum( ulong n, int base ) | 16180 | QString &QString::setNum( ulong n, int base ) |
16135 | { | 16181 | { |
16136 | #if defined(QT_CHECK_RANGE) | 16182 | #if defined(QT_CHECK_RANGE) |
16137 | if ( base < 2 || base > 36 ) { | 16183 | if ( base < 2 || base > 36 ) { |
16138 | qWarning( "QString::setNum: Invalid base %d", base ); | 16184 | qWarning( "QString::setNum: Invalid base %d", base ); |
16139 | base = 10; | 16185 | base = 10; |
16140 | } | 16186 | } |
16141 | #endif | 16187 | #endif |
16142 | char charbuf[65*sizeof(QChar)]; | 16188 | char charbuf[65*sizeof(QChar)]; |
16143 | QChar *buf = (QChar*)charbuf; | 16189 | QChar *buf = (QChar*)charbuf; |
16144 | QChar *p = &buf[64]; | 16190 | QChar *p = &buf[64]; |
16145 | int len = 0; | 16191 | int len = 0; |
16146 | do { | 16192 | do { |
16147 | *--p = "0123456789abcdefghijklmnopqrstuvwxyz"[((int)(n%base))]; | 16193 | *--p = "0123456789abcdefghijklmnopqrstuvwxyz"[((int)(n%base))]; |
16148 | n /= base; | 16194 | n /= base; |
16149 | len++; | 16195 | len++; |
16150 | } while ( n ); | 16196 | } while ( n ); |
16151 | return setUnicode(p,len); | 16197 | return setUnicode(p,len); |
16152 | } | 16198 | } |
16153 | 16199 | ||
@@ -16416,456 +16462,536 @@ void QString::setExpand( uint index, QChar c ) | |||
16416 | \sa isEmpty() | 16462 | \sa isEmpty() |
16417 | */ | 16463 | */ |
16418 | 16464 | ||
16419 | 16465 | ||
16420 | /*! | 16466 | /*! |
16421 | \fn QString& QString::append( const QString& str ) | 16467 | \fn QString& QString::append( const QString& str ) |
16422 | 16468 | ||
16423 | Appends \a str to the string and returns a reference to the | 16469 | Appends \a str to the string and returns a reference to the |
16424 | result. | 16470 | result. |
16425 | 16471 | ||
16426 | \code | 16472 | \code |
16427 | string = "Test"; | 16473 | string = "Test"; |
16428 | string.append( "ing" ); // string == "Testing" | 16474 | string.append( "ing" ); // string == "Testing" |
16429 | \endcode | 16475 | \endcode |
16430 | 16476 | ||
16431 | Equivalent to operator+=(). | 16477 | Equivalent to operator+=(). |
16432 | */ | 16478 | */ |
16433 | 16479 | ||
16434 | /*! | 16480 | /*! |
16435 | \fn QString& QString::append( char ch ) | 16481 | \fn QString& QString::append( char ch ) |
16436 | 16482 | ||
16437 | \overload | 16483 | \overload |
16438 | 16484 | ||
16439 | Appends character \a ch to the string and returns a reference to | 16485 | Appends character \a ch to the string and returns a reference to |
16440 | the result. | 16486 | the result. |
16441 | 16487 | ||
16442 | Equivalent to operator+=(). | 16488 | Equivalent to operator+=(). |
16443 | */ | 16489 | */ |
16444 | 16490 | ||
16445 | /*! | 16491 | /*! |
16446 | \fn QString& QString::append( QChar ch ) | 16492 | \fn QString& QString::append( QChar ch ) |
16447 | 16493 | ||
16448 | \overload | 16494 | \overload |
16449 | 16495 | ||
16450 | Appends character \a ch to the string and returns a reference to | 16496 | Appends character \a ch to the string and returns a reference to |
16451 | the result. | 16497 | the result. |
16452 | 16498 | ||
16453 | Equivalent to operator+=(). | 16499 | Equivalent to operator+=(). |
16454 | */ | 16500 | */ |
16455 | 16501 | ||
16456 | /*! \fn QString& QString::append( const QByteArray &str ) | 16502 | /*! \fn QString& QString::append( const QByteArray &str ) |
16457 | \overload | 16503 | \overload |
16458 | 16504 | ||
16459 | Appends \a str to the string and returns a reference to the result. | 16505 | Appends \a str to the string and returns a reference to the result. |
16460 | 16506 | ||
16461 | Equivalent to operator+=(). | 16507 | Equivalent to operator+=(). |
16462 | */ | 16508 | */ |
16463 | 16509 | ||
16510 | /*! \fn QString& QString::append( const std::string &str ) | ||
16511 | \overload | ||
16512 | |||
16513 | Appends \a str to the string and returns a reference to the result. | ||
16514 | |||
16515 | Equivalent to operator+=(). | ||
16516 | */ | ||
16517 | |||
16518 | |||
16464 | /*! \fn QString& QString::append( const char *str ) | 16519 | /*! \fn QString& QString::append( const char *str ) |
16465 | \overload | 16520 | \overload |
16466 | 16521 | ||
16467 | Appends \a str to the string and returns a reference to the result. | 16522 | Appends \a str to the string and returns a reference to the result. |
16468 | 16523 | ||
16469 | Equivalent to operator+=(). | 16524 | Equivalent to operator+=(). |
16470 | */ | 16525 | */ |
16471 | 16526 | ||
16472 | /*! | 16527 | /*! |
16473 | Appends \a str to the string and returns a reference to the string. | 16528 | Appends \a str to the string and returns a reference to the string. |
16474 | */ | 16529 | */ |
16475 | QString& QString::operator+=( const QString &str ) | 16530 | QString& QString::operator+=( const QString &str ) |
16476 | { | 16531 | { |
16477 | uint len1 = length(); | 16532 | uint len1 = length(); |
16478 | uint len2 = str.length(); | 16533 | uint len2 = str.length(); |
16479 | if ( len2 ) { | 16534 | if ( len2 ) { |
16480 | setLength(len1+len2); | 16535 | setLength(len1+len2); |
16481 | memcpy( d->unicode+len1, str.unicode(), sizeof(QChar)*len2 ); | 16536 | memcpy( d->unicode+len1, str.unicode(), sizeof(QChar)*len2 ); |
16482 | } else if ( isNull() && !str.isNull() ) { // ## just for 1.x compat: | 16537 | } else if ( isNull() && !str.isNull() ) { // ## just for 1.x compat: |
16483 | *this = fromLatin1( "" ); | 16538 | *this = fromLatin1( "" ); |
16484 | } | 16539 | } |
16485 | return *this; | 16540 | return *this; |
16486 | } | 16541 | } |
16487 | 16542 | ||
16488 | /*! | 16543 | /*! |
16489 | \overload | 16544 | \overload |
16490 | 16545 | ||
16491 | Appends \a str to the string and returns a reference to the string. | 16546 | Appends \a str to the string and returns a reference to the string. |
16492 | */ | 16547 | */ |
16548 | #ifndef QT_NO_CAST_ASCII | ||
16493 | QString& QString::operator+=( const char *str ) | 16549 | QString& QString::operator+=( const char *str ) |
16494 | { | 16550 | { |
16495 | if ( str ) { | 16551 | if ( str ) { |
16552 | #ifndef QT_NO_TEXTCODEC | ||
16553 | if ( QTextCodec::codecForCStrings() ) | ||
16554 | return operator+=( fromAscii( str ) ); | ||
16555 | #endif | ||
16556 | |||
16496 | uint len1 = length(); | 16557 | uint len1 = length(); |
16497 | uint len2 = strlen( str ); | 16558 | uint len2 = strlen( str ); |
16498 | if ( len2 ) { | 16559 | if ( len2 ) { |
16499 | setLength(len1+len2); | 16560 | setLength(len1+len2); |
16500 | uint i = 0; | 16561 | uint i = 0; |
16501 | while( i < len2 ) { | 16562 | while( i < len2 ) { |
16502 | d->unicode[len1+i] = str[i]; | 16563 | d->unicode[len1+i] = str[i]; |
16503 | i++; | 16564 | i++; |
16504 | } | 16565 | } |
16505 | } else if ( isNull() ) { // ## just for 1.x compat: | 16566 | } else if ( isNull() ) { // ## just for 1.x compat: |
16506 | *this = fromLatin1( "" ); | 16567 | *this = fromLatin1( "" ); |
16507 | } | 16568 | } |
16508 | } | 16569 | } |
16509 | return *this; | 16570 | return *this; |
16510 | } | 16571 | } |
16572 | #endif | ||
16511 | 16573 | ||
16512 | /*! \overload | 16574 | /*! \overload |
16513 | 16575 | ||
16514 | Appends \a c to the string and returns a reference to the string. | 16576 | Appends \a c to the string and returns a reference to the string. |
16515 | */ | 16577 | */ |
16516 | 16578 | ||
16517 | QString &QString::operator+=( QChar c ) | 16579 | QString &QString::operator+=( QChar c ) |
16518 | { | 16580 | { |
16519 | setLength(length()+1); | 16581 | setLength(length()+1); |
16520 | d->unicode[length()-1] = c; | 16582 | d->unicode[length()-1] = c; |
16521 | return *this; | 16583 | return *this; |
16522 | } | 16584 | } |
16523 | 16585 | ||
16524 | /*! | 16586 | /*! |
16525 | \overload | 16587 | \overload |
16526 | 16588 | ||
16527 | Appends \a c to the string and returns a reference to the string. | 16589 | Appends \a c to the string and returns a reference to the string. |
16528 | */ | 16590 | */ |
16529 | 16591 | ||
16530 | QString &QString::operator+=( char c ) | 16592 | QString &QString::operator+=( char c ) |
16531 | { | 16593 | { |
16594 | #ifndef QT_NO_TEXTCODEC | ||
16595 | if ( QTextCodec::codecForCStrings() ) | ||
16596 | return operator+=( fromAscii( &c, 1 ) ); | ||
16597 | #endif | ||
16532 | setLength(length()+1); | 16598 | setLength(length()+1); |
16533 | d->unicode[length()-1] = c; | 16599 | d->unicode[length()-1] = c; |
16534 | return *this; | 16600 | return *this; |
16535 | } | 16601 | } |
16536 | 16602 | ||
16537 | /*! | 16603 | /*! |
16538 | \fn QString &QString::operator+=( const QByteArray &str ) | 16604 | \fn QString &QString::operator+=( const QByteArray &str ) |
16539 | \overload | 16605 | \overload |
16540 | 16606 | ||
16541 | Appends \a str to the string and returns a reference to the string. | 16607 | Appends \a str to the string and returns a reference to the string. |
16542 | */ | 16608 | */ |
16543 | 16609 | ||
16610 | /*! | ||
16611 | \fn QString &QString::operator+=( const std::string &str ) | ||
16612 | \overload | ||
16613 | |||
16614 | Appends \a str to the string and returns a reference to the string. | ||
16615 | */ | ||
16616 | |||
16544 | 16617 | ||
16545 | 16618 | ||
16546 | /*! | 16619 | /*! |
16547 | \fn char QChar::latin1() const | 16620 | \fn char QChar::latin1() const |
16548 | 16621 | ||
16549 | Returns a latin-1 copy of this character, if this character is in | 16622 | Returns the Latin-1 value of this character, or 0 if it |
16550 | the latin-1 character set. If not, this function returns 0. | 16623 | cannot be represented in Latin-1. |
16551 | */ | 16624 | */ |
16552 | 16625 | ||
16553 | 16626 | ||
16554 | /*! | 16627 | /*! |
16555 | Returns a Latin-1 representation of the string. Note that the | 16628 | Returns a Latin-1 representation of the string. The |
16556 | returned value is undefined if the string contains non-Latin-1 | 16629 | returned value is undefined if the string contains non-Latin-1 |
16557 | characters. If you want to convert strings into formats other than | 16630 | characters. If you want to convert strings into formats other than |
16558 | Unicode, see the QTextCodec classes. | 16631 | Unicode, see the QTextCodec classes. |
16559 | 16632 | ||
16560 | This function is mainly useful for boot-strapping legacy code to | 16633 | This function is mainly useful for boot-strapping legacy code to |
16561 | use Unicode. | 16634 | use Unicode. |
16562 | 16635 | ||
16563 | The result remains valid so long as one unmodified copy of the | 16636 | The result remains valid so long as one unmodified copy of the |
16564 | source string exists. | 16637 | source string exists. |
16565 | 16638 | ||
16566 | \sa utf8(), local8Bit() | 16639 | \sa fromLatin1(), ascii(), utf8(), local8Bit() |
16567 | */ | 16640 | */ |
16568 | const char* QString::latin1() const | 16641 | const char* QString::latin1() const |
16569 | { | 16642 | { |
16570 | if ( !d->ascii ) { | 16643 | if ( !d->ascii || !d->islatin1 ) { |
16571 | Q2HELPER(stat_get_ascii++) | 16644 | d->ascii = unicodeToLatin1( d->unicode, d->len ); |
16572 | Q2HELPER(stat_get_ascii_size+=d->len) | 16645 | d->islatin1 = TRUE; |
16573 | d->ascii = unicodeToAscii( d->unicode, d->len ); | ||
16574 | } | 16646 | } |
16575 | return d->ascii; | 16647 | return d->ascii; |
16576 | } | 16648 | } |
16577 | 16649 | ||
16578 | /*! | 16650 | /*! |
16579 | \fn const char* QString::ascii() const | 16651 | Returns an 8-bit ASCII representation of the string. |
16580 | \obsolete | ||
16581 | 16652 | ||
16582 | This function simply calls latin1() and returns the result. | 16653 | If a codec has been set using QTextCodec::codecForCStrings(), |
16654 | it is used to convert Unicode to 8-bit char. Otherwise, this function | ||
16655 | does the same as latin1(). | ||
16656 | |||
16657 | \sa fromAscii(), latin1(), utf8(), local8Bit() | ||
16583 | */ | 16658 | */ |
16659 | const char* QString::ascii() const | ||
16660 | { | ||
16661 | #ifndef QT_NO_TEXTCODEC | ||
16662 | if ( QTextCodec::codecForCStrings() ) { | ||
16663 | if ( !d->ascii || d->islatin1 ) { | ||
16664 | QCString s = QTextCodec::codecForCStrings()->fromUnicode( *this ); | ||
16665 | s.detach(); | ||
16666 | d->ascii = s.data(); | ||
16667 | d->islatin1 = FALSE; | ||
16668 | s.resetRawData( s.data(), s.size() ); // we have stolen the data | ||
16669 | } | ||
16670 | return d->ascii; | ||
16671 | } | ||
16672 | #endif // QT_NO_TEXTCODEC | ||
16673 | return latin1(); | ||
16674 | } | ||
16584 | 16675 | ||
16585 | /*! | 16676 | /*! |
16586 | Returns the string encoded in UTF8 format. | 16677 | Returns the string encoded in UTF-8 format. |
16587 | 16678 | ||
16588 | See QTextCodec for more diverse coding/decoding of Unicode strings. | 16679 | See QTextCodec for more diverse coding/decoding of Unicode strings. |
16589 | 16680 | ||
16590 | \sa QString::fromUtf8(), local8Bit(), latin1() | 16681 | \sa fromUtf8(), ascii(), latin1(), local8Bit() |
16591 | */ | 16682 | */ |
16592 | QCString QString::utf8() const | 16683 | QCString QString::utf8() const |
16593 | { | 16684 | { |
16594 | int l = length(); | 16685 | int l = length(); |
16595 | int rlen = l*3+1; | 16686 | int rlen = l*3+1; |
16596 | QCString rstr(rlen); | 16687 | QCString rstr(rlen); |
16597 | uchar* cursor = (uchar*)rstr.data(); | 16688 | uchar* cursor = (uchar*)rstr.data(); |
16598 | const QChar *ch = d->unicode; | 16689 | const QChar *ch = d->unicode; |
16599 | for (int i=0; i<l; i++) { | 16690 | for (int i=0; i<l; i++) { |
16600 | ushort u = ch->unicode(); | 16691 | ushort u = ch->unicode(); |
16601 | if ( u < 0x80 ) { | 16692 | if ( u < 0x80 ) { |
16602 | *cursor++ = (uchar)u; | 16693 | *cursor++ = (uchar)u; |
16603 | } else { | 16694 | } else { |
16604 | if ( u < 0x0800 ) { | 16695 | if ( u < 0x0800 ) { |
16605 | *cursor++ = 0xc0 | ((uchar) (u >> 6)); | 16696 | *cursor++ = 0xc0 | ((uchar) (u >> 6)); |
16606 | } else { | 16697 | } else { |
16607 | *cursor++ = 0xe0 | ((uchar) (u >> 12)); | 16698 | *cursor++ = 0xe0 | ((uchar) (u >> 12)); |
16608 | *cursor++ = 0x80 | ( ((uchar) (u >> 6)) & 0x3f); | 16699 | *cursor++ = 0x80 | ( ((uchar) (u >> 6)) & 0x3f); |
16609 | } | 16700 | } |
16610 | *cursor++ = 0x80 | ((uchar) (u&0x3f)); | 16701 | *cursor++ = 0x80 | ((uchar) (u&0x3f)); |
16611 | } | 16702 | } |
16612 | ch++; | 16703 | ch++; |
16613 | } | 16704 | } |
16614 | rstr.truncate( cursor - (uchar*)rstr.data() ); | 16705 | rstr.truncate( cursor - (uchar*)rstr.data() ); |
16615 | return rstr; | 16706 | return rstr; |
16616 | } | 16707 | } |
16617 | 16708 | ||
16618 | /*! | 16709 | /*! |
16619 | Returns the Unicode string decoded from the first \a len | 16710 | Returns the Unicode string decoded from the first \a len |
16620 | characters of \a utf8, ignoring the rest of \a utf8. If \a len is | 16711 | characters of \a utf8, ignoring the rest of \a utf8. If \a len is |
16621 | -1 then the length of \a utf8 is used. If \a len is bigger than | 16712 | -1 then the length of \a utf8 is used. If \a len is bigger than |
16622 | the length of \a utf8 then it will use the length of \a utf8. | 16713 | the length of \a utf8 then it will use the length of \a utf8. |
16623 | 16714 | ||
16624 | \code | 16715 | \code |
16625 | QString str = QString::fromUtf8( "123456789", 5 ); | 16716 | QString str = QString::fromUtf8( "123456789", 5 ); |
16626 | // str == "12345" | 16717 | // str == "12345" |
16627 | \endcode | 16718 | \endcode |
16628 | 16719 | ||
16629 | See QTextCodec for more diverse coding/decoding of Unicode strings. | 16720 | See QTextCodec for more diverse coding/decoding of Unicode strings. |
16630 | */ | 16721 | */ |
16631 | QString QString::fromUtf8( const char* utf8, int len ) | 16722 | QString QString::fromUtf8( const char* utf8, int len ) |
16632 | { | 16723 | { |
16633 | if ( !utf8 ) | 16724 | if ( !utf8 ) |
16634 | return QString::null; | 16725 | return QString::null; |
16635 | 16726 | ||
16636 | if ( len < 0 ) len = qstrlen( utf8 ); | 16727 | if ( len < 0 ) |
16728 | len = strlen( utf8 ); | ||
16637 | QString result; | 16729 | QString result; |
16638 | result.setLength( len ); // worst case | 16730 | result.setLength( len ); // worst case |
16639 | QChar *qch = (QChar *)result.unicode(); | 16731 | QChar *qch = (QChar *)result.unicode(); |
16640 | ushort uc = 0; | 16732 | ushort uc = 0; |
16641 | int need = 0; | 16733 | int need = 0; |
16642 | for (int i=0; i<len; i++) { | 16734 | for (int i=0; i<len; i++) { |
16643 | uchar ch = utf8[i]; | 16735 | uchar ch = utf8[i]; |
16644 | if (need) { | 16736 | if (need) { |
16645 | if ( (ch&0xc0) == 0x80 ) { | 16737 | if ( (ch&0xc0) == 0x80 ) { |
16646 | uc = (uc << 6) | (ch & 0x3f); | 16738 | uc = (uc << 6) | (ch & 0x3f); |
16647 | need--; | 16739 | need--; |
16648 | if ( !need ) { | 16740 | if ( !need ) { |
16649 | *qch = uc; | 16741 | *qch = uc; |
16650 | qch++; | 16742 | qch++; |
16651 | } | 16743 | } |
16652 | } else { | 16744 | } else { |
16653 | // error | 16745 | // error |
16654 | *qch = QChar::replacement; | 16746 | *qch = QChar::replacement; |
16655 | qch++; | 16747 | qch++; |
16656 | need = 0; | 16748 | need = 0; |
16657 | } | 16749 | } |
16658 | } else { | 16750 | } else { |
16659 | if ( ch < 128 ) { | 16751 | if ( ch < 128 ) { |
16660 | *qch = ch; | 16752 | *qch = ch; |
16661 | qch++; | 16753 | qch++; |
16662 | } else if ( (ch&0xe0) == 0xc0 ) { | 16754 | } else if ( (ch&0xe0) == 0xc0 ) { |
16663 | uc = ch &0x1f; | 16755 | uc = ch &0x1f; |
16664 | need = 1; | 16756 | need = 1; |
16665 | } else if ( (ch&0xf0) == 0xe0 ) { | 16757 | } else if ( (ch&0xf0) == 0xe0 ) { |
16666 | uc = ch &0x0f; | 16758 | uc = ch &0x0f; |
16667 | need = 2; | 16759 | need = 2; |
16668 | } | 16760 | } |
16669 | } | 16761 | } |
16670 | } | 16762 | } |
16671 | result.truncate( qch - result.unicode() ); | 16763 | result.truncate( qch - result.unicode() ); |
16672 | return result; | 16764 | return result; |
16673 | } | 16765 | } |
16674 | 16766 | ||
16675 | /*! | 16767 | /*! |
16676 | Returns the Unicode string decoded from the first \a len | 16768 | Returns the Unicode string decoded from the first \a len |
16677 | characters of \a chars, ignoring the rest of \a chars. If \a len | 16769 | characters of \a ascii, ignoring the rest of \a ascii. If \a len |
16678 | is -1 then the length of \a chars is used. If \a len is bigger | 16770 | is -1 then the length of \a ascii is used. If \a len is bigger |
16679 | than the length of \a chars then it will use the length of \a | 16771 | than the length of \a ascii then it will use the length of \a |
16680 | chars. | 16772 | ascii. |
16773 | |||
16774 | If a codec has been set using QTextCodec::codecForCStrings(), | ||
16775 | it is used to convert Unicode to 8-bit char. Otherwise, this function | ||
16776 | does the same as fromLatin1(). | ||
16681 | 16777 | ||
16682 | This is the same as the QString(const char*) constructor, but you | 16778 | This is the same as the QString(const char*) constructor, but you |
16683 | can make that constructor invisible if you compile with the define | 16779 | can make that constructor invisible if you compile with the define |
16684 | \c QT_NO_CAST_ASCII, in which case you can explicitly create a | 16780 | \c QT_NO_CAST_ASCII, in which case you can explicitly create a |
16685 | QString from Latin-1 text using this function. | 16781 | QString from 8-bit ASCII text using this function. |
16686 | 16782 | ||
16687 | \code | 16783 | \code |
16688 | QString str = QString::fromLatin1( "123456789", 5 ); | 16784 | QString str = QString::fromAscii( "123456789", 5 ); |
16689 | // str == "12345" | 16785 | // str == "12345" |
16690 | \endcode | 16786 | \endcode |
16787 | */ | ||
16788 | QString QString::fromAscii( const char* ascii, int len ) | ||
16789 | { | ||
16790 | #ifndef QT_NO_TEXTCODEC | ||
16791 | if ( QTextCodec::codecForCStrings() ) { | ||
16792 | if ( !ascii ) | ||
16793 | return QString::null; | ||
16794 | if ( len < 0 ) | ||
16795 | len = strlen( ascii ); | ||
16796 | if ( len == 0 || *ascii == '\0' ) | ||
16797 | return QString::fromLatin1( "" ); | ||
16798 | return QTextCodec::codecForCStrings()->toUnicode( ascii, len ); | ||
16799 | } | ||
16800 | #endif | ||
16801 | return fromLatin1( ascii, len ); | ||
16802 | } | ||
16803 | |||
16804 | |||
16805 | /*! | ||
16806 | Returns the Unicode string decoded from the first \a len | ||
16807 | characters of \a chars, ignoring the rest of \a chars. If \a len | ||
16808 | is -1 then the length of \a chars is used. If \a len is bigger | ||
16809 | than the length of \a chars then it will use the length of \a | ||
16810 | chars. | ||
16811 | |||
16812 | \sa fromAscii() | ||
16691 | */ | 16813 | */ |
16692 | QString QString::fromLatin1( const char* chars, int len ) | 16814 | QString QString::fromLatin1( const char* chars, int len ) |
16693 | { | 16815 | { |
16694 | uint l; | 16816 | uint l; |
16695 | QChar *uc; | 16817 | QChar *uc; |
16696 | if ( len < 0 ) | 16818 | if ( len < 0 ) |
16697 | len = -1; | 16819 | len = -1; |
16698 | uc = internalAsciiToUnicode( chars, &l, len ); | 16820 | uc = internalLatin1ToUnicode( chars, &l, len ); |
16699 | return QString( new QStringData(uc, l, l), TRUE ); | 16821 | return QString( new QStringData(uc, l, l), TRUE ); |
16700 | } | 16822 | } |
16701 | 16823 | ||
16702 | /*! | 16824 | /*! |
16703 | \fn const QChar* QString::unicode() const | 16825 | \fn const QChar* QString::unicode() const |
16704 | 16826 | ||
16705 | Returns the Unicode representation of the string. The result | 16827 | Returns the Unicode representation of the string. The result |
16706 | remains valid until the string is modified. | 16828 | remains valid until the string is modified. |
16707 | */ | 16829 | */ |
16708 | 16830 | ||
16709 | /*! | 16831 | /*! |
16710 | Returns the string encoded in a locale-specific format. On X11, | 16832 | Returns the string encoded in a locale-specific format. On X11, |
16711 | this is the QTextCodec::codecForLocale(). On Windows, it is a | 16833 | this is the QTextCodec::codecForLocale(). On Windows, it is a |
16712 | system-defined encoding. On Mac OS X, this always uses utf8 as the | 16834 | system-defined encoding. On Mac OS X, this always uses UTF-8 as |
16713 | encoding. | 16835 | the encoding. |
16714 | 16836 | ||
16715 | See QTextCodec for more diverse coding/decoding of Unicode | 16837 | See QTextCodec for more diverse coding/decoding of Unicode |
16716 | strings. | 16838 | strings. |
16717 | 16839 | ||
16718 | \sa QString::fromLocal8Bit(), latin1(), utf8() | 16840 | \sa fromLocal8Bit(), ascii(), latin1(), utf8() |
16719 | */ | 16841 | */ |
16720 | 16842 | ||
16721 | |||
16722 | QCString QString::local8Bit() const | 16843 | QCString QString::local8Bit() const |
16723 | { | 16844 | { |
16724 | #ifdef QT_NO_TEXTCODEC | 16845 | #ifdef QT_NO_TEXTCODEC |
16725 | return latin1(); | 16846 | return latin1(); |
16726 | #else | 16847 | #else |
16727 | #ifdef Q_WS_X11 | 16848 | #ifdef Q_WS_X11 |
16728 | QTextCodec* codec = QTextCodec::codecForLocale(); | 16849 | QTextCodec* codec = QTextCodec::codecForLocale(); |
16729 | return codec | 16850 | return codec |
16730 | ? codec->fromUnicode(*this) | 16851 | ? codec->fromUnicode(*this) |
16731 | : QCString(latin1()); | 16852 | : QCString(latin1()); |
16732 | #endif | 16853 | #endif |
16733 | #if defined( Q_WS_MACX ) | 16854 | #if defined( Q_WS_MACX ) |
16734 | return utf8(); | 16855 | return utf8(); |
16735 | #endif | 16856 | #endif |
16736 | #if defined( Q_WS_MAC9 ) | 16857 | #if defined( Q_WS_MAC9 ) |
16737 | return QCString(latin1()); //I'm evil.. | 16858 | return QCString(latin1()); //I'm evil.. |
16738 | #endif | 16859 | #endif |
16739 | #ifdef Q_WS_WIN | 16860 | #ifdef Q_WS_WIN |
16740 | return qt_winQString2MB( *this ); | 16861 | return qt_winQString2MB( *this ); |
16741 | #endif | 16862 | #endif |
16742 | #ifdef Q_WS_QWS | 16863 | #ifdef Q_WS_QWS |
16743 | return utf8(); // ##### if there is ANY 8 bit format supported? | 16864 | return utf8(); // ### if there is any 8 bit format supported? |
16744 | #endif | 16865 | #endif |
16745 | #endif | 16866 | #endif |
16746 | } | 16867 | } |
16747 | 16868 | ||
16748 | /*! | 16869 | /*! |
16749 | Returns the Unicode string decoded from the first \a len | 16870 | Returns the Unicode string decoded from the first \a len |
16750 | characters of \a local8Bit, ignoring the rest of \a local8Bit. If | 16871 | characters of \a local8Bit, ignoring the rest of \a local8Bit. If |
16751 | \a len is -1 then the length of \a local8Bit is used. If \a len is | 16872 | \a len is -1 then the length of \a local8Bit is used. If \a len is |
16752 | bigger than the length of \a local8Bit then it will use the length | 16873 | bigger than the length of \a local8Bit then it will use the length |
16753 | of \a local8Bit. | 16874 | of \a local8Bit. |
16754 | 16875 | ||
16755 | \code | 16876 | \code |
16756 | QString str = QString::fromLocal8Bit( "123456789", 5 ); | 16877 | QString str = QString::fromLocal8Bit( "123456789", 5 ); |
16757 | // str == "12345" | 16878 | // str == "12345" |
16758 | \endcode | 16879 | \endcode |
16759 | 16880 | ||
16760 | \a local8Bit is assumed to be encoded in a locale-specific format. | 16881 | \a local8Bit is assumed to be encoded in a locale-specific format. |
16761 | 16882 | ||
16762 | See QTextCodec for more diverse coding/decoding of Unicode strings. | 16883 | See QTextCodec for more diverse coding/decoding of Unicode strings. |
16763 | */ | 16884 | */ |
16764 | QString QString::fromLocal8Bit( const char* local8Bit, int len ) | 16885 | QString QString::fromLocal8Bit( const char* local8Bit, int len ) |
16765 | { | 16886 | { |
16766 | #ifdef QT_NO_TEXTCODEC | 16887 | #ifdef QT_NO_TEXTCODEC |
16767 | return fromLatin1( local8Bit, len ); | 16888 | return fromLatin1( local8Bit, len ); |
16768 | #else | 16889 | #else |
16769 | 16890 | ||
16770 | if ( !local8Bit ) | 16891 | if ( !local8Bit ) |
16771 | return QString::null; | 16892 | return QString::null; |
16772 | #ifdef Q_WS_X11 | 16893 | #ifdef Q_WS_X11 |
16773 | QTextCodec* codec = QTextCodec::codecForLocale(); | 16894 | QTextCodec* codec = QTextCodec::codecForLocale(); |
16774 | if ( len < 0 ) len = qstrlen(local8Bit); | 16895 | if ( len < 0 ) |
16896 | len = strlen( local8Bit ); | ||
16775 | return codec | 16897 | return codec |
16776 | ? codec->toUnicode( local8Bit, len ) | 16898 | ? codec->toUnicode( local8Bit, len ) |
16777 | : fromLatin1( local8Bit, len ); | 16899 | : fromLatin1( local8Bit, len ); |
16778 | #endif | 16900 | #endif |
16779 | #if defined( Q_WS_MAC ) | 16901 | #if defined( Q_WS_MAC ) |
16780 | return fromUtf8(local8Bit,len); | 16902 | return fromUtf8(local8Bit,len); |
16781 | #endif | 16903 | #endif |
16782 | // Should this be OS_WIN32? | 16904 | // Should this be OS_WIN32? |
16783 | #ifdef Q_WS_WIN | 16905 | #ifdef Q_WS_WIN |
16784 | if ( len >= 0 ) { | 16906 | if ( len >= 0 ) { |
16785 | QCString s(local8Bit,len+1); | 16907 | QCString s(local8Bit,len+1); |
16786 | return qt_winMB2QString(s); | 16908 | return qt_winMB2QString(s); |
16787 | } | 16909 | } |
16788 | return qt_winMB2QString( local8Bit ); | 16910 | return qt_winMB2QString( local8Bit ); |
16789 | #endif | 16911 | #endif |
16790 | #ifdef Q_WS_QWS | 16912 | #ifdef Q_WS_QWS |
16791 | return fromUtf8(local8Bit,len); | 16913 | return fromUtf8(local8Bit,len); |
16792 | #endif | 16914 | #endif |
16793 | #endif // QT_NO_TEXTCODEC | 16915 | #endif // QT_NO_TEXTCODEC |
16794 | } | 16916 | } |
16795 | 16917 | ||
16796 | /*! | 16918 | /*! |
16797 | \fn QString::operator const char *() const | 16919 | \fn QString::operator const char *() const |
16798 | 16920 | ||
16799 | Returns latin1(). Be sure to see the warnings documented in the | 16921 | Returns latin1(). Be sure to see the warnings documented in the |
16800 | latin1() function. Note that for new code which you wish to be | 16922 | latin1() function. Note that for new code which you wish to be |
16801 | strictly Unicode-clean, you can define the macro \c | 16923 | strictly Unicode-clean, you can define the macro \c |
16802 | QT_NO_ASCII_CAST when compiling your code to hide this function so | 16924 | QT_NO_ASCII_CAST when compiling your code to hide this function so |
16803 | that automatic casts are not done. This has the added advantage | 16925 | that automatic casts are not done. This has the added advantage |
16804 | that you catch the programming error described in operator!(). | 16926 | that you catch the programming error described in operator!(). |
16805 | */ | 16927 | */ |
16806 | 16928 | ||
16929 | /*! | ||
16930 | \fn QString::operator std::string() const | ||
16931 | |||
16932 | Returns ascii(). | ||
16933 | */ | ||
16934 | |||
16807 | 16935 | ||
16808 | /*! | 16936 | /*! |
16809 | Returns the QString as a zero terminated array of unsigned shorts | 16937 | Returns the QString as a zero terminated array of unsigned shorts |
16810 | if the string is not null; otherwise returns zero. | 16938 | if the string is not null; otherwise returns zero. |
16811 | 16939 | ||
16812 | The result remains valid so long as one unmodified | 16940 | The result remains valid so long as one unmodified |
16813 | copy of the source string exists. | 16941 | copy of the source string exists. |
16814 | */ | 16942 | */ |
16815 | const unsigned short *QString::ucs2() const | 16943 | const unsigned short *QString::ucs2() const |
16816 | { | 16944 | { |
16817 | if ( ! d->unicode ) | 16945 | if ( ! d->unicode ) |
16818 | return 0; | 16946 | return 0; |
16819 | unsigned int len = d->len; | 16947 | unsigned int len = d->len; |
16820 | if ( d->maxl < len + 1 ) { | 16948 | if ( d->maxl < len + 1 ) { |
16821 | // detach, grow or shrink | 16949 | // detach, grow or shrink |
16822 | Q2HELPER(stat_copy_on_write++) | ||
16823 | Q2HELPER(stat_copy_on_write_size += len) | ||
16824 | uint newMax = computeNewMax( len + 1 ); | 16950 | uint newMax = computeNewMax( len + 1 ); |
16825 | QChar* nd = QT_ALLOC_QCHAR_VEC( newMax ); | 16951 | QChar* nd = QT_ALLOC_QCHAR_VEC( newMax ); |
16826 | if ( nd ) { | 16952 | if ( nd ) { |
16827 | if ( d->unicode ) | 16953 | if ( d->unicode ) |
16828 | memcpy( nd, d->unicode, sizeof(QChar)*len ); | 16954 | memcpy( nd, d->unicode, sizeof(QChar)*len ); |
16829 | ((QString *)this)->deref(); | 16955 | ((QString *)this)->deref(); |
16830 | ((QString *)this)->d = new QStringData( nd, len, newMax ); | 16956 | ((QString *)this)->d = new QStringData( nd, len, newMax ); |
16831 | } | 16957 | } |
16832 | } | 16958 | } |
16833 | d->unicode[len] = 0; | 16959 | d->unicode[len] = 0; |
16834 | return (unsigned short *) d->unicode; | 16960 | return (unsigned short *) d->unicode; |
16835 | } | 16961 | } |
16836 | 16962 | ||
16837 | /*! | 16963 | /*! |
16838 | Constructs a string that is a deep copy of \a str, interpreted as a | 16964 | Constructs a string that is a deep copy of \a str, interpreted as a |
16839 | UCS2 encoded, zero terminated, Unicode string. | 16965 | UCS2 encoded, zero terminated, Unicode string. |
16840 | 16966 | ||
16841 | If \a str is 0, then a null string is created. | 16967 | If \a str is 0, then a null string is created. |
16842 | 16968 | ||
16843 | \sa isNull() | 16969 | \sa isNull() |
16844 | */ | 16970 | */ |
16845 | QString QString::fromUcs2( const unsigned short *str ) | 16971 | QString QString::fromUcs2( const unsigned short *str ) |
16846 | { | 16972 | { |
16847 | if ( !str ) { | 16973 | if ( !str ) { |
16848 | return QString::null; | 16974 | return QString::null; |
16849 | } else { | 16975 | } else { |
16850 | int length = 0; | 16976 | int length = 0; |
16851 | while( str[length] != 0 ) | 16977 | while( str[length] != 0 ) |
16852 | length++; | 16978 | length++; |
16853 | QChar* uc = QT_ALLOC_QCHAR_VEC( length ); | 16979 | QChar* uc = QT_ALLOC_QCHAR_VEC( length ); |
16854 | memcpy( uc, str, length*sizeof(QChar) ); | 16980 | memcpy( uc, str, length*sizeof(QChar) ); |
16855 | return QString( new QStringData( uc, length, length ), TRUE ); | 16981 | return QString( new QStringData( uc, length, length ), TRUE ); |
16856 | } | 16982 | } |
16857 | } | 16983 | } |
16858 | 16984 | ||
16859 | /*! | 16985 | /*! |
16860 | \fn QChar QString::at( uint ) const | 16986 | \fn QChar QString::at( uint ) const |
16861 | 16987 | ||
16862 | Returns the character at index \a i, or 0 if \a i is beyond the | 16988 | Returns the character at index \a i, or 0 if \a i is beyond the |
16863 | length of the string. | 16989 | length of the string. |
16864 | 16990 | ||
16865 | \code | 16991 | \code |
16866 | const QString string( "abcdefgh" ); | 16992 | const QString string( "abcdefgh" ); |
16867 | QChar ch = string.at( 4 ); | 16993 | QChar ch = string.at( 4 ); |
16868 | // ch == 'e' | 16994 | // ch == 'e' |
16869 | \endcode | 16995 | \endcode |
16870 | 16996 | ||
16871 | If the QString is not const (i.e. const QString) or const& (i.e. | 16997 | If the QString is not const (i.e. const QString) or const& (i.e. |
@@ -16938,174 +17064,194 @@ QString QString::fromUcs2( const unsigned short *str ) | |||
16938 | 17064 | ||
16939 | The function returns a reference to the character at index \a i. | 17065 | The function returns a reference to the character at index \a i. |
16940 | The resulting reference can then be assigned to, or used | 17066 | The resulting reference can then be assigned to, or used |
16941 | immediately, but it will become invalid once further modifications | 17067 | immediately, but it will become invalid once further modifications |
16942 | are made to the original string. | 17068 | are made to the original string. |
16943 | 17069 | ||
16944 | If \a i is beyond the length of the string then the string is | 17070 | If \a i is beyond the length of the string then the string is |
16945 | expanded with QChar::null. | 17071 | expanded with QChar::null. |
16946 | */ | 17072 | */ |
16947 | 17073 | ||
16948 | /* | 17074 | /* |
16949 | Internal chunk of code to handle the | 17075 | Internal chunk of code to handle the |
16950 | uncommon cases of at() above. | 17076 | uncommon cases of at() above. |
16951 | */ | 17077 | */ |
16952 | void QString::subat( uint i ) | 17078 | void QString::subat( uint i ) |
16953 | { | 17079 | { |
16954 | uint olen = d->len; | 17080 | uint olen = d->len; |
16955 | if ( i >= olen ) { | 17081 | if ( i >= olen ) { |
16956 | setLength( i+1 ); // i is index; i+1 is needed length | 17082 | setLength( i+1 ); // i is index; i+1 is needed length |
16957 | for ( uint j=olen; j<=i; j++ ) | 17083 | for ( uint j=olen; j<=i; j++ ) |
16958 | d->unicode[j] = QChar::null; | 17084 | d->unicode[j] = QChar::null; |
16959 | } else { | 17085 | } else { |
16960 | // Just be sure to detach | 17086 | // Just be sure to detach |
16961 | real_detach(); | 17087 | real_detach(); |
16962 | } | 17088 | } |
16963 | } | 17089 | } |
16964 | 17090 | ||
16965 | 17091 | ||
16966 | /*! | 17092 | /*! |
16967 | Resizes the string to \a len characters and copies \a unicode into | 17093 | Resizes the string to \a len characters and copies \a unicode into |
16968 | the string. If \a unicode is 0, nothing is copied, but the | 17094 | the string. If \a unicode is 0, nothing is copied, but the |
16969 | string is still resized to \a len. If \a len is zero, then the | 17095 | string is still resized to \a len. If \a len is zero, then the |
16970 | string becomes a \link isNull() null\endlink string. | 17096 | string becomes a \link isNull() null\endlink string. |
16971 | 17097 | ||
16972 | \sa setLatin1(), isNull() | 17098 | \sa setLatin1(), isNull() |
16973 | */ | 17099 | */ |
16974 | 17100 | ||
16975 | QString& QString::setUnicode( const QChar *unicode, uint len ) | 17101 | QString& QString::setUnicode( const QChar *unicode, uint len ) |
16976 | { | 17102 | { |
16977 | if ( len == 0 ) { // set to null string | 17103 | if ( len == 0 ) { // set to null string |
16978 | if ( d != shared_null ) { // beware of nullstring being set to nullstring | 17104 | if ( d != shared_null ) { // beware of nullstring being set to nullstring |
16979 | deref(); | 17105 | deref(); |
16980 | d = shared_null ? shared_null : makeSharedNull(); | 17106 | d = shared_null ? shared_null : makeSharedNull(); |
16981 | d->ref(); | 17107 | d->ref(); |
16982 | } | 17108 | } |
16983 | } else if ( d->count != 1 || len > d->maxl || | 17109 | } else if ( d->count != 1 || len > d->maxl || |
16984 | ( len * 4 < d->maxl && d->maxl > 4 ) ) { | 17110 | ( len * 4 < d->maxl && d->maxl > 4 ) ) { |
16985 | // detach, grown or shrink | 17111 | // detach, grown or shrink |
16986 | Q2HELPER(stat_copy_on_write++) | ||
16987 | Q2HELPER(stat_copy_on_write_size+=d->len) | ||
16988 | uint newMax = computeNewMax( len ); | 17112 | uint newMax = computeNewMax( len ); |
16989 | QChar* nd = QT_ALLOC_QCHAR_VEC( newMax ); | 17113 | QChar* nd = QT_ALLOC_QCHAR_VEC( newMax ); |
16990 | if ( unicode ) | 17114 | if ( unicode ) |
16991 | memcpy( nd, unicode, sizeof(QChar)*len ); | 17115 | memcpy( nd, unicode, sizeof(QChar)*len ); |
16992 | deref(); | 17116 | deref(); |
16993 | d = new QStringData( nd, len, newMax ); | 17117 | d = new QStringData( nd, len, newMax ); |
16994 | } else { | 17118 | } else { |
16995 | d->len = len; | 17119 | d->len = len; |
16996 | d->setDirty(); | 17120 | d->setDirty(); |
16997 | if ( unicode ) | 17121 | if ( unicode ) |
16998 | memcpy( d->unicode, unicode, sizeof(QChar)*len ); | 17122 | memcpy( d->unicode, unicode, sizeof(QChar)*len ); |
16999 | } | 17123 | } |
17000 | return *this; | 17124 | return *this; |
17001 | } | 17125 | } |
17002 | 17126 | ||
17003 | /*! | 17127 | /*! |
17004 | Resizes the string to \a len characters and copies \a | 17128 | Resizes the string to \a len characters and copies \a |
17005 | unicode_as_ushorts into the string (on some X11 client platforms | 17129 | unicode_as_ushorts into the string (on some X11 client platforms |
17006 | this will involve a byte-swapping pass). | 17130 | this will involve a byte-swapping pass). |
17007 | 17131 | ||
17008 | If \a unicode_as_ushorts is 0, nothing is copied, but the string | 17132 | If \a unicode_as_ushorts is 0, nothing is copied, but the string |
17009 | is still resized to \a len. If \a len is zero, the string becomes | 17133 | is still resized to \a len. If \a len is zero, the string becomes |
17010 | a \link isNull() null\endlink string. | 17134 | a \link isNull() null\endlink string. |
17011 | 17135 | ||
17012 | \sa setLatin1(), isNull() | 17136 | \sa setLatin1(), isNull() |
17013 | */ | 17137 | */ |
17014 | QString& QString::setUnicodeCodes( const ushort* unicode_as_ushorts, uint len ) | 17138 | QString& QString::setUnicodeCodes( const ushort* unicode_as_ushorts, uint len ) |
17015 | { | 17139 | { |
17016 | return setUnicode((const QChar*)unicode_as_ushorts, len); | 17140 | return setUnicode((const QChar*)unicode_as_ushorts, len); |
17017 | } | 17141 | } |
17018 | 17142 | ||
17019 | 17143 | ||
17020 | /*! | 17144 | /*! |
17145 | Sets this string to \a str, interpreted as a classic 8-bit ASCII C | ||
17146 | string. If \a len is -1 (the default), then it is set to | ||
17147 | strlen(str). | ||
17148 | |||
17149 | If \a str is 0 a null string is created. If \a str is "", an empty | ||
17150 | string is created. | ||
17151 | |||
17152 | \sa isNull(), isEmpty() | ||
17153 | */ | ||
17154 | |||
17155 | QString &QString::setAscii( const char *str, int len ) | ||
17156 | { | ||
17157 | #ifndef QT_NO_TEXTCODEC | ||
17158 | if ( QTextCodec::codecForCStrings() ) { | ||
17159 | *this = QString::fromAscii( str, len ); | ||
17160 | return *this; | ||
17161 | } | ||
17162 | #endif // QT_NO_TEXTCODEC | ||
17163 | return setLatin1( str, len ); | ||
17164 | } | ||
17165 | |||
17166 | /*! | ||
17021 | Sets this string to \a str, interpreted as a classic Latin1 C | 17167 | Sets this string to \a str, interpreted as a classic Latin1 C |
17022 | string. If \a len is -1 (the default), then it is set to | 17168 | string. If \a len is -1 (the default), then it is set to |
17023 | strlen(str). | 17169 | strlen(str). |
17024 | 17170 | ||
17025 | If \a str is 0 a null string is created. If \a str is "", an empty | 17171 | If \a str is 0 a null string is created. If \a str is "", an empty |
17026 | string is created. | 17172 | string is created. |
17027 | 17173 | ||
17028 | \sa isNull(), isEmpty() | 17174 | \sa isNull(), isEmpty() |
17029 | */ | 17175 | */ |
17030 | 17176 | ||
17031 | QString &QString::setLatin1( const char *str, int len ) | 17177 | QString &QString::setLatin1( const char *str, int len ) |
17032 | { | 17178 | { |
17033 | if ( str == 0 ) | 17179 | if ( str == 0 ) |
17034 | return setUnicode(0,0); | 17180 | return setUnicode(0,0); |
17035 | if ( len < 0 ) | 17181 | if ( len < 0 ) |
17036 | len = qstrlen(str); | 17182 | len = strlen( str ); |
17037 | if ( len == 0 ) { // won't make a null string | 17183 | if ( len == 0 ) { // won't make a null string |
17038 | *this = QString::fromLatin1( "" ); | 17184 | *this = QString::fromLatin1( "" ); |
17039 | } else { | 17185 | } else { |
17040 | setUnicode( 0, len ); // resize but not copy | 17186 | setUnicode( 0, len ); // resize but not copy |
17041 | QChar *p = d->unicode; | 17187 | QChar *p = d->unicode; |
17042 | while ( len-- ) | 17188 | while ( len-- ) |
17043 | *p++ = *str++; | 17189 | *p++ = *str++; |
17044 | } | 17190 | } |
17045 | return *this; | 17191 | return *this; |
17046 | } | 17192 | } |
17047 | 17193 | ||
17048 | /*! \internal | 17194 | /*! \internal |
17049 | */ | 17195 | */ |
17050 | void QString::checkSimpleText() const | 17196 | void QString::checkSimpleText() const |
17051 | { | 17197 | { |
17052 | QChar *p = d->unicode; | 17198 | QChar *p = d->unicode; |
17053 | QChar *end = p + d->len; | 17199 | QChar *end = p + d->len; |
17054 | d->simpletext = 1; | ||
17055 | while( p < end ) { | 17200 | while( p < end ) { |
17056 | ushort uc = p->unicode(); | 17201 | ushort uc = p->unicode(); |
17057 | // sort out regions of complex text formatting | 17202 | // sort out regions of complex text formatting |
17058 | if ( uc > 0x058f && ( uc < 0x1100 || uc > 0xfb0f ) ) { | 17203 | if ( uc > 0x058f && ( uc < 0x1100 || uc > 0xfb0f ) ) { |
17059 | d->simpletext = 0; | 17204 | d->issimpletext = FALSE; |
17060 | return; | 17205 | return; |
17061 | } | 17206 | } |
17062 | p++; | 17207 | p++; |
17063 | } | 17208 | } |
17209 | d->issimpletext = TRUE; | ||
17064 | } | 17210 | } |
17065 | 17211 | ||
17066 | /*! \fn bool QString::simpleText() const | 17212 | /*! \fn bool QString::simpleText() const |
17067 | \internal | 17213 | \internal |
17068 | */ | 17214 | */ |
17069 | 17215 | ||
17070 | /*! \internal | 17216 | /*! \internal |
17071 | */ | 17217 | */ |
17072 | bool QString::isRightToLeft() const | 17218 | bool QString::isRightToLeft() const |
17073 | { | 17219 | { |
17074 | int len = length(); | 17220 | int len = length(); |
17075 | QChar *p = d->unicode; | 17221 | QChar *p = d->unicode; |
17076 | while( len-- ) { | 17222 | while( len-- ) { |
17077 | switch( ::direction( *p ) ) | 17223 | switch( ::direction( *p ) ) |
17078 | { | 17224 | { |
17079 | case QChar::DirL: | 17225 | case QChar::DirL: |
17080 | case QChar::DirLRO: | 17226 | case QChar::DirLRO: |
17081 | case QChar::DirLRE: | 17227 | case QChar::DirLRE: |
17082 | return FALSE; | 17228 | return FALSE; |
17083 | case QChar::DirR: | 17229 | case QChar::DirR: |
17084 | case QChar::DirAL: | 17230 | case QChar::DirAL: |
17085 | case QChar::DirRLO: | 17231 | case QChar::DirRLO: |
17086 | case QChar::DirRLE: | 17232 | case QChar::DirRLE: |
17087 | return TRUE; | 17233 | return TRUE; |
17088 | default: | 17234 | default: |
17089 | break; | 17235 | break; |
17090 | } | 17236 | } |
17091 | ++p; | 17237 | ++p; |
17092 | } | 17238 | } |
17093 | return FALSE; | 17239 | return FALSE; |
17094 | } | 17240 | } |
17095 | 17241 | ||
17096 | 17242 | ||
17097 | /*! | 17243 | /*! |
17098 | \fn int QString::compare( const QString & s1, const QString & s2 ) | 17244 | \fn int QString::compare( const QString & s1, const QString & s2 ) |
17099 | 17245 | ||
17100 | Lexically compares \a s1 with \a s2 and returns an integer less | 17246 | Lexically compares \a s1 with \a s2 and returns an integer less |
17101 | than, equal to, or greater than zero if \a s1 is less than, equal | 17247 | than, equal to, or greater than zero if \a s1 is less than, equal |
17102 | to, or greater than \a s2. | 17248 | to, or greater than \a s2. |
17103 | 17249 | ||
17104 | The comparison is based exclusively on the numeric Unicode values | 17250 | The comparison is based exclusively on the numeric Unicode values |
17105 | of the characters and is very fast, but is not what a human would | 17251 | of the characters and is very fast, but is not what a human would |
17106 | expect. Consider sorting user-interface strings with | 17252 | expect. Consider sorting user-interface strings with |
17107 | QString::localeAwareCompare(). | 17253 | QString::localeAwareCompare(). |
17108 | 17254 | ||
17109 | \code | 17255 | \code |
17110 | int a = QString::compare( "def", "abc" ); // a > 0 | 17256 | int a = QString::compare( "def", "abc" ); // a > 0 |
17111 | int b = QString::compare( "abc", "def" ); // b < 0 | 17257 | int b = QString::compare( "abc", "def" ); // b < 0 |