-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,20 +1,20 @@ | |||
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 | ** |
@@ -38,36 +38,40 @@ | |||
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 | ||
@@ -11778,36 +11782,38 @@ static inline QChar upper( const QChar &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, |
@@ -11891,25 +11897,25 @@ static int ucstrnicmp( const QChar *a, const QChar *b, int l ) | |||
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 | ||
@@ -12882,44 +12888,44 @@ void QString::compose() | |||
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 ) |
@@ -12931,79 +12937,79 @@ static QChar* internalAsciiToUnicode( const QByteArray& ba, uint* len ) | |||
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; |
@@ -13148,53 +13154,24 @@ 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 | */ |
@@ -13202,65 +13179,67 @@ 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 |
@@ -13293,30 +13272,57 @@ QString::QString( const QChar* unicode, uint length ) | |||
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 | ||
@@ -13324,91 +13330,99 @@ QString::QString( const char *str ) | |||
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 |
@@ -13462,98 +13476,94 @@ QString &QString::operator=( const char *str ) | |||
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 ) ) { |
@@ -13765,25 +13775,25 @@ bool QString::findArg( int& pos, int& len ) const | |||
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 ) |
@@ -13904,25 +13914,25 @@ QString &QString::sprintf( const char* cformat, ... ) | |||
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 |
@@ -14115,26 +14125,28 @@ static int bm_find( const QString &str, int index, const QString &pattern, uint | |||
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 |
@@ -14212,40 +14224,44 @@ int QString::find( const QString& str, int index, bool cs ) const | |||
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 | ||
@@ -14264,26 +14280,28 @@ int QString::findRev( const QString& str, int index, bool cs ) const | |||
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() ); |
@@ -14970,65 +14988,75 @@ QString QString::rightJustify( uint width, QChar fill, bool truncate ) const | |||
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 | ||
@@ -15128,26 +15156,26 @@ QString QString::simplifyWhiteSpace() const | |||
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 ); |
@@ -15242,24 +15270,34 @@ QString &QString::insert( uint index, QChar c ) // insert char | |||
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) ); |
@@ -15839,27 +15877,27 @@ static bool ok_in_base( QChar c, int base ) | |||
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; |
@@ -15874,82 +15912,80 @@ long QString::toLong( bool *ok, int base ) const | |||
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; |
@@ -15958,44 +15994,44 @@ bye: | |||
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 |
@@ -16004,64 +16040,74 @@ ushort QString::toUShort( bool *ok, int base ) const | |||
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 | ||
@@ -16089,29 +16135,29 @@ QString &QString::setNum( long n, int base ) | |||
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 ); |
@@ -16452,24 +16498,33 @@ void QString::setExpand( uint index, QChar c ) | |||
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 ) |
@@ -16481,122 +16536,158 @@ QString& QString::operator+=( const QString &str ) | |||
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; |
@@ -16624,25 +16715,26 @@ QCString QString::utf8() const | |||
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 ) { |
@@ -16665,91 +16757,120 @@ QString QString::fromUtf8( const char* utf8, int len ) | |||
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 |
@@ -16762,25 +16883,26 @@ QCString QString::local8Bit() const | |||
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); |
@@ -16795,41 +16917,45 @@ QString QString::fromLocal8Bit( const char* local8Bit, int len ) | |||
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 | } |
@@ -16974,26 +17100,24 @@ void QString::subat( uint i ) | |||
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 | } |
@@ -17009,67 +17133,89 @@ QString& QString::setUnicode( const QChar *unicode, uint len ) | |||
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; |