summaryrefslogtreecommitdiff
path: root/qmake/tools/qstring.cpp
Unidiff
Diffstat (limited to 'qmake/tools/qstring.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--qmake/tools/qstring.cpp434
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,85 +1,89 @@
1/**************************************************************************** 1/****************************************************************************
2** $Id$ 2** $Id$
3** 3**
4** Implementation of the QString class and related Unicode functions 4** Implementation of the QString class and related Unicode functions
5** 5**
6** Created : 920722 6** Created : 920722
7** 7**
8** Copyright (C) 1992-2000 Trolltech AS. All rights reserved. 8** Copyright (C) 1992-2002 Trolltech AS. All rights reserved.
9** 9**
10** This file is part of the tools module of the Qt GUI Toolkit. 10** This file is part of the tools module of the Qt GUI Toolkit.
11** 11**
12** This file may be distributed under the terms of the Q Public License 12** This file may be distributed under the terms of the Q Public License
13** as defined by Trolltech AS of Norway and appearing in the file 13** as defined by Trolltech AS of Norway and appearing in the file
14** LICENSE.QPL included in the packaging of this file. 14** LICENSE.QPL included in the packaging of this file.
15** 15**
16** This file may be distributed and/or modified under the terms of the 16** This file may be distributed and/or modified under the terms of the
17** GNU General Public License version 2 as published by the Free Software 17** GNU General Public License version 2 as published by the Free Software
18** Foundation and appearing in the file LICENSE.GPL included in the 18** Foundation and appearing in the file LICENSE.GPL included in the
19** packaging of this file. 19** packaging of this file.
20** 20**
21** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition 21** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
22** licenses may use this file in accordance with the Qt Commercial License 22** licenses may use this file in accordance with the Qt Commercial License
23** Agreement provided with the Software. 23** Agreement provided with the Software.
24** 24**
25** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 25** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
26** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 26** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27** 27**
28** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for 28** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
29** information about Qt Commercial License Agreements. 29** information about Qt Commercial License Agreements.
30** See http://www.trolltech.com/qpl/ for QPL licensing information. 30** See http://www.trolltech.com/qpl/ for QPL licensing information.
31** See http://www.trolltech.com/gpl/ for GPL licensing information. 31** See http://www.trolltech.com/gpl/ for GPL licensing information.
32** 32**
33** Contact info@trolltech.com if any conditions of this licensing are 33** Contact info@trolltech.com if any conditions of this licensing are
34** not clear to you. 34** not clear to you.
35** 35**
36**********************************************************************/ 36**********************************************************************/
37 37
38// Don't define it while compiling this module, or USERS of Qt will 38// Don't define it while compiling this module, or USERS of Qt will
39// not be able to link. 39// not be able to link.
40#ifdef QT_NO_CAST_ASCII 40#ifdef QT_NO_CAST_ASCII
41#undef QT_NO_CAST_ASCII 41#undef QT_NO_CAST_ASCII
42#endif 42#endif
43 43
44#include "qstring.h" 44#include "qstring.h"
45#include "qregexp.h" 45#include "qregexp.h"
46#include "qdatastream.h" 46#include "qdatastream.h"
47#ifndef QT_NO_TEXTCODEC 47#ifndef QT_NO_TEXTCODEC
48#include "qtextcodec.h" 48#include "qtextcodec.h"
49#endif 49#endif
50#include <ctype.h>
51#include <limits.h> 50#include <limits.h>
52#include <stdarg.h> 51#include <stdarg.h>
53#include <stdio.h> 52#include <stdio.h>
54#include <stdlib.h> 53#include <stdlib.h>
54#include <string.h>
55#if defined(Q_WS_WIN) 55#if defined(Q_WS_WIN)
56#include "qt_windows.h" 56#include "qt_windows.h"
57#endif 57#endif
58#if !defined( QT_NO_COMPONENT ) && !defined( QT_LITE_COMPONENT ) 58#if !defined( QT_NO_COMPONENT ) && !defined( QT_LITE_COMPONENT )
59#include "qcleanuphandler.h" 59#include "qcleanuphandler.h"
60#endif 60#endif
61 61
62#ifdef QT_NO_UNICODETABLES
63# include <ctype.h>
64#endif
65
62 66
63/* ------------------------------------------------------------------------- 67/* -------------------------------------------------------------------------
64 * unicode information 68 * unicode information
65 * these tables are generated from the unicode reference file 69 * these tables are generated from the unicode reference file
66 * ftp://ftp.unicode.org/Public/3.2-Update/UnicodeData.txt 70 * ftp://ftp.unicode.org/Public/3.2-Update/UnicodeData.txt
67 * 71 *
68 * Lars 72 * Lars
69 * ------------------------------------------------------------------------- 73 * -------------------------------------------------------------------------
70 */ 74 */
71 75
72/* Perl script to generate (run perl -x tools/qstring.cpp) 76/* Perl script to generate (run perl -x tools/qstring.cpp)
73 77
74#!perl 78#!perl
75 79
76sub numberize 80sub numberize
77{ 81{
78 my(%r, $n, $id); 82 my(%r, $n, $id);
79 for $id ( @_ ) { 83 for $id ( @_ ) {
80 $i = $id; 84 $i = $id;
81 $i="" if $i eq "EMPTY"; 85 $i="" if $i eq "EMPTY";
82 $r{$i}=$n++; 86 $r{$i}=$n++;
83 } 87 }
84 return %r; 88 return %r;
85} 89}
@@ -11766,60 +11770,62 @@ static inline QChar upper( const QChar &c )
11766#ifndef QT_NO_UNICODETABLES 11770#ifndef QT_NO_UNICODETABLES
11767 uchar row = c.row(); 11771 uchar row = c.row();
11768 uchar cell = c.cell(); 11772 uchar cell = c.cell();
11769 if ( unicode_info[row][cell] != QChar::Letter_Lowercase ) 11773 if ( unicode_info[row][cell] != QChar::Letter_Lowercase )
11770 return c; 11774 return c;
11771 Q_UINT16 upper = *(case_info[row]+cell); 11775 Q_UINT16 upper = *(case_info[row]+cell);
11772 if ( upper == 0 ) 11776 if ( upper == 0 )
11773 return c; 11777 return c;
11774 return upper; 11778 return upper;
11775#else 11779#else
11776 if ( c.row() ) 11780 if ( c.row() )
11777 return c; 11781 return c;
11778 else 11782 else
11779 return QChar( toupper((uchar) c.latin1()) ); 11783 return QChar( toupper((uchar) c.latin1()) );
11780#endif 11784#endif
11781} 11785}
11782 11786
11783static inline QChar::Direction direction( const QChar &c ) 11787static 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
11794static inline bool mirrored( const QChar &c ) 11799static 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
11807static const Q_UINT16 symmetricPairs[] = { 11813static const Q_UINT16 symmetricPairs[] = {
11808 0x0028, 0x0029, 0x003C, 0x003E, 0x005B, 0x005D, 0x007B, 0x007D, 11814 0x0028, 0x0029, 0x003C, 0x003E, 0x005B, 0x005D, 0x007B, 0x007D,
11809 0x00AB, 0x00BB, 0x2039, 0x203A, 0x2045, 0x2046, 0x207D, 0x207E, 11815 0x00AB, 0x00BB, 0x2039, 0x203A, 0x2045, 0x2046, 0x207D, 0x207E,
11810 0x208D, 0x208E, 0x2208, 0x220B, 0x2209, 0x220C, 0x220A, 0x220D, 11816 0x208D, 0x208E, 0x2208, 0x220B, 0x2209, 0x220C, 0x220A, 0x220D,
11811 0x2215, 0x29F5, 0x223C, 0x223D, 0x2243, 0x22CD, 0x2252, 0x2253, 11817 0x2215, 0x29F5, 0x223C, 0x223D, 0x2243, 0x22CD, 0x2252, 0x2253,
11812 0x2254, 0x2255, 0x2264, 0x2265, 0x2266, 0x2267, 0x2268, 0x2269, 11818 0x2254, 0x2255, 0x2264, 0x2265, 0x2266, 0x2267, 0x2268, 0x2269,
11813 0x226A, 0x226B, 0x226E, 0x226F, 0x2270, 0x2271, 0x2272, 0x2273, 11819 0x226A, 0x226B, 0x226E, 0x226F, 0x2270, 0x2271, 0x2272, 0x2273,
11814 0x2274, 0x2275, 0x2276, 0x2277, 0x2278, 0x2279, 0x227A, 0x227B, 11820 0x2274, 0x2275, 0x2276, 0x2277, 0x2278, 0x2279, 0x227A, 0x227B,
11815 0x227C, 0x227D, 0x227E, 0x227F, 0x2280, 0x2281, 0x2282, 0x2283, 11821 0x227C, 0x227D, 0x227E, 0x227F, 0x2280, 0x2281, 0x2282, 0x2283,
11816 0x2284, 0x2285, 0x2286, 0x2287, 0x2288, 0x2289, 0x228A, 0x228B, 11822 0x2284, 0x2285, 0x2286, 0x2287, 0x2288, 0x2289, 0x228A, 0x228B,
11817 0x228F, 0x2290, 0x2291, 0x2292, 0x2298, 0x29B8, 0x22A2, 0x22A3, 11823 0x228F, 0x2290, 0x2291, 0x2292, 0x2298, 0x29B8, 0x22A2, 0x22A3,
11818 0x22A6, 0x2ADE, 0x22A8, 0x2AE4, 0x22A9, 0x2AE3, 0x22AB, 0x2AE5, 11824 0x22A6, 0x2ADE, 0x22A8, 0x2AE4, 0x22A9, 0x2AE3, 0x22AB, 0x2AE5,
11819 0x22B0, 0x22B1, 0x22B2, 0x22B3, 0x22B4, 0x22B5, 0x22B6, 0x22B7, 11825 0x22B0, 0x22B1, 0x22B2, 0x22B3, 0x22B4, 0x22B5, 0x22B6, 0x22B7,
11820 0x22C9, 0x22CA, 0x22CB, 0x22CC, 0x22D0, 0x22D1, 0x22D6, 0x22D7, 11826 0x22C9, 0x22CA, 0x22CB, 0x22CC, 0x22D0, 0x22D1, 0x22D6, 0x22D7,
11821 0x22D8, 0x22D9, 0x22DA, 0x22DB, 0x22DC, 0x22DD, 0x22DE, 0x22DF, 11827 0x22D8, 0x22D9, 0x22DA, 0x22DB, 0x22DC, 0x22DD, 0x22DE, 0x22DF,
11822 0x22E0, 0x22E1, 0x22E2, 0x22E3, 0x22E4, 0x22E5, 0x22E6, 0x22E7, 11828 0x22E0, 0x22E1, 0x22E2, 0x22E3, 0x22E4, 0x22E5, 0x22E6, 0x22E7,
11823 0x22E8, 0x22E9, 0x22EA, 0x22EB, 0x22EC, 0x22ED, 0x22F0, 0x22F1, 11829 0x22E8, 0x22E9, 0x22EA, 0x22EB, 0x22EC, 0x22ED, 0x22F0, 0x22F1,
11824 0x22F2, 0x22FA, 0x22F3, 0x22FB, 0x22F4, 0x22FC, 0x22F6, 0x22FD, 11830 0x22F2, 0x22FA, 0x22F3, 0x22FB, 0x22F4, 0x22FC, 0x22F6, 0x22FD,
11825 0x22F7, 0x22FE, 0x2308, 0x2309, 0x230A, 0x230B, 0x2329, 0x232A, 11831 0x22F7, 0x22FE, 0x2308, 0x2309, 0x230A, 0x230B, 0x2329, 0x232A,
@@ -11879,49 +11885,49 @@ static int ucstrcmp( const QString &as, const QString &bs )
11879 11885
11880static int ucstrncmp( const QChar *a, const QChar *b, int l ) 11886static int ucstrncmp( const QChar *a, const QChar *b, int l )
11881{ 11887{
11882 while ( l-- && *a == *b ) 11888 while ( l-- && *a == *b )
11883 a++,b++; 11889 a++,b++;
11884 if ( l==-1 ) 11890 if ( l==-1 )
11885 return 0; 11891 return 0;
11886 return a->unicode() - b->unicode(); 11892 return a->unicode() - b->unicode();
11887} 11893}
11888 11894
11889static int ucstrnicmp( const QChar *a, const QChar *b, int l ) 11895static int ucstrnicmp( const QChar *a, const QChar *b, int l )
11890{ 11896{
11891 while ( l-- && ::lower( *a ) == ::lower( *b ) ) 11897 while ( l-- && ::lower( *a ) == ::lower( *b ) )
11892 a++,b++; 11898 a++,b++;
11893 if ( l==-1 ) 11899 if ( l==-1 )
11894 return 0; 11900 return 0;
11895 return ::lower( *a ).unicode() - ::lower( *b ).unicode(); 11901 return ::lower( *a ).unicode() - ::lower( *b ).unicode();
11896} 11902}
11897 11903
11898static uint computeNewMax( uint len ) 11904static uint computeNewMax( uint len )
11899{ 11905{
11900 uint newMax = 4; 11906 uint newMax = 4;
11901 while ( newMax < len ) 11907 while ( newMax < len )
11902 newMax *= 2; 11908 newMax *= 2;
11903 // try to spare some memory 11909 // try to save some memory
11904 if ( newMax >= 1024 * 1024 && len <= newMax - (newMax >> 2) ) 11910 if ( newMax >= 1024 * 1024 && len <= newMax - (newMax >> 2) )
11905 newMax -= newMax >> 2; 11911 newMax -= newMax >> 2;
11906 return newMax; 11912 return newMax;
11907} 11913}
11908 11914
11909/*! 11915/*!
11910 \class QCharRef qstring.h 11916 \class QCharRef qstring.h
11911 \reentrant 11917 \reentrant
11912 \brief The QCharRef class is a helper class for QString. 11918 \brief The QCharRef class is a helper class for QString.
11913 11919
11914 \ingroup text 11920 \ingroup text
11915 11921
11916 When you get an object of type QCharRef, if you can assign to it, 11922 When you get an object of type QCharRef, if you can assign to it,
11917 the assignment will apply to the character in the string from 11923 the assignment will apply to the character in the string from
11918 which you got the reference. That is its whole purpose in life. 11924 which you got the reference. That is its whole purpose in life.
11919 The QCharRef becomes invalid once modifications are made to the 11925 The QCharRef becomes invalid once modifications are made to the
11920 string: if you want to keep the character, copy it into a QChar. 11926 string: if you want to keep the character, copy it into a QChar.
11921 11927
11922 Most of the QChar member functions also exist in QCharRef. 11928 Most of the QChar member functions also exist in QCharRef.
11923 However, they are not explicitly documented here. 11929 However, they are not explicitly documented here.
11924 11930
11925 \sa QString::operator[]() QString::at() QChar 11931 \sa QString::operator[]() QString::at() QChar
11926*/ 11932*/
11927 11933
@@ -12870,152 +12876,152 @@ void QString::compose()
12870 cindex++; 12876 cindex++;
12871 index++; 12877 index++;
12872 } 12878 }
12873 *this = composed; 12879 *this = composed;
12874#endif 12880#endif
12875} 12881}
12876 12882
12877 12883
12878// These macros are used for efficient allocation of QChar strings. 12884// These macros are used for efficient allocation of QChar strings.
12879// IMPORTANT! If you change these, make sure you also change the 12885// IMPORTANT! If you change these, make sure you also change the
12880// "delete unicode" statement in ~QStringData() in qstring.h correspondingly! 12886// "delete unicode" statement in ~QStringData() in qstring.h correspondingly!
12881 12887
12882#define QT_ALLOC_QCHAR_VEC( N ) (QChar*) new char[ sizeof(QChar)*( N ) ] 12888#define QT_ALLOC_QCHAR_VEC( N ) (QChar*) new char[ sizeof(QChar)*( N ) ]
12883#define QT_DELETE_QCHAR_VEC( P ) delete[] ((char*)( P )) 12889#define QT_DELETE_QCHAR_VEC( P ) delete[] ((char*)( P ))
12884 12890
12885 12891
12886/*! 12892/*!
12887 This utility function converts the 8-bit string \a ba to Unicode, 12893 This utility function converts the 8-bit string \a ba to Unicode,
12888 returning the result. 12894 returning the result.
12889 12895
12890 The caller is responsible for deleting the return value with 12896 The caller is responsible for deleting the return value with
12891 delete[]. 12897 delete[].
12892*/ 12898*/
12893 12899
12894QChar* QString::asciiToUnicode( const QByteArray& ba, uint* len ) 12900QChar* 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
12913static QChar* internalAsciiToUnicode( const QByteArray& ba, uint* len ) 12919static QChar* internalLatin1ToUnicode( const QByteArray& ba, uint* len )
12914{ 12920{
12915 if ( ba.isNull() ) { 12921 if ( ba.isNull() ) {
12916 *len = 0; 12922 *len = 0;
12917 return 0; 12923 return 0;
12918 } 12924 }
12919 int l = 0; 12925 int l = 0;
12920 while ( l < (int)ba.size() && ba[l] ) 12926 while ( l < (int)ba.size() && ba[l] )
12921 l++; 12927 l++;
12922 char* str = ba.data(); 12928 char* str = ba.data();
12923 QChar *uc = QT_ALLOC_QCHAR_VEC( l ); 12929 QChar *uc = QT_ALLOC_QCHAR_VEC( l );
12924 QChar *result = uc; 12930 QChar *result = uc;
12925 if ( len ) 12931 if ( len )
12926 *len = l; 12932 *len = l;
12927 while (l--) 12933 while (l--)
12928 *uc++ = *str++; 12934 *uc++ = *str++;
12929 return result; 12935 return result;
12930} 12936}
12931 12937
12932/*! 12938/*!
12933 \overload 12939 \overload
12934 12940
12935 This utility function converts the '\0'-terminated 8-bit string \a 12941 This utility function converts the '\0'-terminated 8-bit string \a
12936 str to Unicode, returning the result and setting \a *len to the 12942 str to Unicode, returning the result and setting \a *len to the
12937 length of the Unicode string. 12943 length of the Unicode string.
12938 12944
12939 The caller is responsible for deleting the return value with 12945 The caller is responsible for deleting the return value with
12940 delete[]. 12946 delete[].
12941*/ 12947*/
12942 12948
12943QChar* QString::asciiToUnicode( const char *str, uint* len, uint maxlen ) 12949QChar* 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
12966static QChar* internalAsciiToUnicode( const char *str, uint* len, 12972static 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*/
12997char* QString::unicodeToAscii(const QChar *uc, uint l) 13003char* QString::unicodeToLatin1(const QChar *uc, uint l)
12998{ 13004{
12999 if (!uc) { 13005 if (!uc) {
13000 return 0; 13006 return 0;
13001 } 13007 }
13002 char *a = new char[l+1]; 13008 char *a = new char[l+1];
13003 char *result = a; 13009 char *result = a;
13004 while (l--) { 13010 while (l--) {
13005 *a++ = (uc->unicode() > 0xff) ? '?' : (char)uc->unicode(); 13011 *a++ = (uc->unicode() > 0xff) ? '?' : (char)uc->unicode();
13006 uc++; 13012 uc++;
13007 } 13013 }
13008 *a = '\0'; 13014 *a = '\0';
13009 return result; 13015 return result;
13010} 13016}
13011 13017
13012/***************************************************************************** 13018/*****************************************************************************
13013 QString member functions 13019 QString member functions
13014 *****************************************************************************/ 13020 *****************************************************************************/
13015 13021
13016/*! 13022/*!
13017 \class QString qstring.h 13023 \class QString qstring.h
13018 \reentrant 13024 \reentrant
13019 13025
13020 \brief The QString class provides an abstraction of Unicode text 13026 \brief The QString class provides an abstraction of Unicode text
13021 and the classic C '\0'-terminated char array. 13027 and the classic C '\0'-terminated char array.
@@ -13136,143 +13142,116 @@ char* QString::unicodeToAscii(const QChar *uc, uint l)
13136 13142
13137 Matching is case-insensitive unless \c CaseSensitive is set. \c 13143 Matching is case-insensitive unless \c CaseSensitive is set. \c
13138 CaseSensitive may be OR-ed with any combination of the other 13144 CaseSensitive may be OR-ed with any combination of the other
13139 flags. 13145 flags.
13140 13146
13141*/ 13147*/
13142Q_EXPORT QStringData *QString::shared_null = 0; 13148Q_EXPORT QStringData *QString::shared_null = 0;
13143QT_STATIC_CONST_IMPL QString QString::null; 13149QT_STATIC_CONST_IMPL QString QString::null;
13144QT_STATIC_CONST_IMPL QChar QChar::null; 13150QT_STATIC_CONST_IMPL QChar QChar::null;
13145QT_STATIC_CONST_IMPL QChar QChar::replacement((ushort)0xfffd); 13151QT_STATIC_CONST_IMPL QChar QChar::replacement((ushort)0xfffd);
13146QT_STATIC_CONST_IMPL QChar QChar::byteOrderMark((ushort)0xfeff); 13152QT_STATIC_CONST_IMPL QChar QChar::byteOrderMark((ushort)0xfeff);
13147QT_STATIC_CONST_IMPL QChar QChar::byteOrderSwapped((ushort)0xfffe); 13153QT_STATIC_CONST_IMPL QChar QChar::byteOrderSwapped((ushort)0xfffe);
13148QT_STATIC_CONST_IMPL QChar QChar::nbsp((ushort)0x00a0); 13154QT_STATIC_CONST_IMPL QChar QChar::nbsp((ushort)0x00a0);
13149 13155
13150QStringData* QString::makeSharedNull() 13156QStringData* 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
13164static int stat_construct_charstar=0;
13165static int stat_construct_charstar_size=0;
13166static int stat_construct_null=0;
13167static int stat_construct_int=0;
13168static int stat_construct_int_size=0;
13169static int stat_construct_ba=0;
13170static int stat_get_ascii=0;
13171static int stat_get_ascii_size=0;
13172static int stat_copy_on_write=0;
13173static int stat_copy_on_write_size=0;
13174static int stat_fast_copy=0;
13175Q_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*/
13201QString::QString( QChar ch ) 13178QString::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*/
13211QString::QString( const QString &s ) : 13188QString::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
13230QString::QString( int size, bool /*dummy*/ ) 13206QString::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
13250QString::QString( const QByteArray& ba ) 13223QString::QString( const QByteArray& ba )
13251{ 13224{
13252 Q2HELPER(stat_construct_ba++) 13225#ifndef QT_NO_TEXTCODEC
13226 if ( QTextCodec::codecForCStrings() ) {
13227 d = 0;
13228 *this = fromAscii( ba.data(), ba.size() );
13229 return;
13230 }
13231#endif
13253 uint l; 13232 uint l;
13254 QChar *uc = internalAsciiToUnicode(ba,&l); 13233 QChar *uc = internalLatin1ToUnicode(ba,&l);
13255 d = new QStringData(uc,l,l); 13234 d = new QStringData(uc,l,l);
13256} 13235}
13257 13236
13258/*! 13237/*!
13259 Constructs a string that is a deep copy of the first \a length 13238 Constructs a string that is a deep copy of the first \a length
13260 characters in the QChar array. 13239 characters in the QChar array.
13261 13240
13262 If \a unicode and \a length are 0, then a null string is created. 13241 If \a unicode and \a length are 0, then a null string is created.
13263 13242
13264 If only \a unicode is 0, the string is empty but has \a length 13243 If only \a unicode is 0, the string is empty but has \a length
13265 characters of space preallocated: QString expands automatically 13244 characters of space preallocated: QString expands automatically
13266 anyway, but this may speed up some cases a little. We recommend 13245 anyway, but this may speed up some cases a little. We recommend
13267 using the plain constructor and setLength() for this purpose since 13246 using the plain constructor and setLength() for this purpose since
13268 it will result in more readable code. 13247 it will result in more readable code.
13269 13248
13270 \sa isNull() setLength() 13249 \sa isNull() setLength()
13271*/ 13250*/
13272 13251
13273QString::QString( const QChar* unicode, uint length ) 13252QString::QString( const QChar* unicode, uint length )
13274{ 13253{
13275 if ( !unicode && !length ) { 13254 if ( !unicode && !length ) {
13276 d = shared_null ? shared_null : makeSharedNull(); 13255 d = shared_null ? shared_null : makeSharedNull();
13277 d->ref(); 13256 d->ref();
13278 } else { 13257 } else {
@@ -13281,146 +13260,181 @@ QString::QString( const QChar* unicode, uint length )
13281 memcpy(uc, unicode, length*sizeof(QChar)); 13260 memcpy(uc, unicode, length*sizeof(QChar));
13282 d = new QStringData(uc,unicode ? length : 0,length); 13261 d = new QStringData(uc,unicode ? length : 0,length);
13283 } 13262 }
13284} 13263}
13285 13264
13286/*! 13265/*!
13287 Constructs a string that is a deep copy of \a str, interpreted as 13266 Constructs a string that is a deep copy of \a str, interpreted as
13288 a classic C string. 13267 a classic C string.
13289 13268
13290 If \a str is 0, then a null string is created. 13269 If \a str is 0, then a null string is created.
13291 13270
13292 This is a cast constructor, but it is perfectly safe: converting a 13271 This is a cast constructor, but it is perfectly safe: converting a
13293 Latin1 const char* to QString preserves all the information. You 13272 Latin1 const char* to QString preserves all the information. You
13294 can disable this constructor by defining \c QT_NO_CAST_ASCII when 13273 can disable this constructor by defining \c QT_NO_CAST_ASCII when
13295 you compile your applications. You can also make QString objects 13274 you compile your applications. You can also make QString objects
13296 by using setLatin1(), fromLatin1(), fromLocal8Bit(), and 13275 by using setLatin1(), fromLatin1(), fromLocal8Bit(), and
13297 fromUtf8(). Or whatever encoding is appropriate for the 8-bit data 13276 fromUtf8(). Or whatever encoding is appropriate for the 8-bit data
13298 you have. 13277 you have.
13299 13278
13300 \sa isNull() 13279 \sa isNull()
13301*/ 13280*/
13302 13281
13303QString::QString( const char *str ) 13282QString::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
13303QString::QString( const std::string &str )
13304{
13305#ifndef QT_NO_TEXTCODEC
13306 if ( QTextCodec::codecForCStrings() ) {
13307 d = 0;
13308 *this = fromAscii( str.c_str() );
13309 return;
13310 }
13311#endif
13306 uint l; 13312 uint l;
13307 QChar *uc = internalAsciiToUnicode(str,&l); 13313 QChar *uc = internalLatin1ToUnicode(str.c_str(),&l);
13308 Q2HELPER(stat_construct_charstar_size+=l)
13309 d = new QStringData(uc,l,l); 13314 d = new QStringData(uc,l,l);
13310} 13315}
13316#endif
13311 13317
13312/*! 13318/*!
13313 \fn QString::~QString() 13319 \fn QString::~QString()
13314 13320
13315 Destroys the string and frees the string's data if this is the 13321 Destroys the string and frees the string's data if this is the
13316 last reference to the string. 13322 last reference to the string.
13317*/ 13323*/
13318 13324
13319 13325
13320/*! 13326/*!
13321 Deallocates any space reserved solely by this QString. 13327 Deallocates any space reserved solely by this QString.
13322 13328
13323 If the string does not share its data with another QString 13329 If the string does not share its data with another QString
13324 instance, nothing happens; otherwise the function creates a new, 13330 instance, nothing happens; otherwise the function creates a new,
13325 unique copy of this string. This function is called whenever the 13331 unique copy of this string. This function is called whenever the
13326 string is modified. 13332 string is modified.
13327*/ 13333*/
13328 13334
13329void QString::real_detach() 13335void QString::real_detach()
13330{ 13336{
13331 setLength( length() ); 13337 setLength( length() );
13332} 13338}
13333 13339
13334void QString::deref() 13340void 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
13343void QStringData::deleteSelf() 13349void 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*/
13369QString &QString::operator=( const QString &s ) 13384QString &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*/
13384QString &QString::operator=( const QCString& cs ) 13398QString &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*/
13400QString &QString::operator=( const char *str ) 13414QString &QString::operator=( const char *str )
13401{ 13415{
13402 return setLatin1(str); 13416 return setAscii(str);
13403} 13417}
13404 13418
13405 13419
13406/*! 13420/*!
13407 \fn bool QString::isNull() const 13421 \fn bool QString::isNull() const
13408 13422
13409 Returns TRUE if the string is null; otherwise returns FALSE. A 13423 Returns TRUE if the string is null; otherwise returns FALSE. A
13410 null string is always empty. 13424 null string is always empty.
13411 13425
13412 \code 13426 \code
13413 QString a; // a.unicode() == 0, a.length() == 0 13427 QString a; // a.unicode() == 0, a.length() == 0
13414 a.isNull(); // TRUE, because a.unicode() == 0 13428 a.isNull(); // TRUE, because a.unicode() == 0
13415 a.isEmpty(); // TRUE 13429 a.isEmpty(); // TRUE
13416 \endcode 13430 \endcode
13417 13431
13418 \sa isEmpty(), length() 13432 \sa isEmpty(), length()
13419*/ 13433*/
13420 13434
13421/*! 13435/*!
13422 \fn bool QString::isEmpty() const 13436 \fn bool QString::isEmpty() const
13423 13437
13424 Returns TRUE if the string is empty, i.e. if length() == 0; 13438 Returns TRUE if the string is empty, i.e. if length() == 0;
13425 otherwise returns FALSE. Null strings are also empty. 13439 otherwise returns FALSE. Null strings are also empty.
13426 13440
@@ -13450,122 +13464,118 @@ QString &QString::operator=( const char *str )
13450/*! 13464/*!
13451 If \a newLen is less than the length of the string, then the 13465 If \a newLen is less than the length of the string, then the
13452 string is truncated at position \a newLen. Otherwise nothing 13466 string is truncated at position \a newLen. Otherwise nothing
13453 happens. 13467 happens.
13454 13468
13455 \code 13469 \code
13456 QString s = "truncate me"; 13470 QString s = "truncate me";
13457 s.truncate( 5 ); // s == "trunc" 13471 s.truncate( 5 ); // s == "trunc"
13458 \endcode 13472 \endcode
13459 13473
13460 \sa setLength() 13474 \sa setLength()
13461*/ 13475*/
13462 13476
13463void QString::truncate( uint newLen ) 13477void 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
13503void QString::setLength( uint newLen ) 13516void 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*/
13554QString QString::arg( const QString& a, int fieldwidth ) const 13564QString QString::arg( const QString& a, int fieldwidth ) const
13555{ 13565{
13556 int pos, len; 13566 int pos, len;
13557 QString r = *this; 13567 QString r = *this;
13558 13568
13559 if ( !findArg( pos, len ) ) { 13569 if ( !findArg( pos, len ) ) {
13560 qWarning( "QString::arg(): Argument missing: %s, %s", 13570 qWarning( "QString::arg(): Argument missing: %s, %s",
13561 latin1(), a.latin1() ); 13571 latin1(), a.latin1() );
13562 // Make sure the text at least appears SOMEWHERE 13572 // Make sure the text at least appears SOMEWHERE
13563 r += ' '; 13573 r += ' ';
13564 pos = r.length(); 13574 pos = r.length();
13565 len = 0; 13575 len = 0;
13566 } 13576 }
13567 13577
13568 r.replace( pos, len, a ); 13578 r.replace( pos, len, a );
13569 if ( fieldwidth < 0 ) { 13579 if ( fieldwidth < 0 ) {
13570 QString s; 13580 QString s;
13571 while ( (uint)-fieldwidth > a.length() ) { 13581 while ( (uint)-fieldwidth > a.length() ) {
@@ -13753,49 +13763,49 @@ bool QString::findArg( int& pos, int& len ) const
13753 int x = ...; 13763 int x = ...;
13754 QTextOStream( &str ) << s << " : " << x; 13764 QTextOStream( &str ) << s << " : " << x;
13755 \endcode 13765 \endcode
13756 13766
13757 For \link QObject::tr() translations,\endlink especially if the 13767 For \link QObject::tr() translations,\endlink especially if the
13758 strings contains more than one escape sequence, you should 13768 strings contains more than one escape sequence, you should
13759 consider using the arg() function instead. This allows the order 13769 consider using the arg() function instead. This allows the order
13760 of the replacements to be controlled by the translator, and has 13770 of the replacements to be controlled by the translator, and has
13761 Unicode support. 13771 Unicode support.
13762 13772
13763 \sa arg() 13773 \sa arg()
13764*/ 13774*/
13765 13775
13766#ifndef QT_NO_SPRINTF 13776#ifndef QT_NO_SPRINTF
13767QString &QString::sprintf( const char* cformat, ... ) 13777QString &QString::sprintf( const char* cformat, ... )
13768{ 13778{
13769 va_list ap; 13779 va_list ap;
13770 va_start( ap, cformat ); 13780 va_start( ap, cformat );
13771 13781
13772 if ( !cformat || !*cformat ) { 13782 if ( !cformat || !*cformat ) {
13773 // Qt 1.x compat 13783 // Qt 1.x compat
13774 *this = fromLatin1( "" ); 13784 *this = fromLatin1( "" );
13775 return *this; 13785 return *this;
13776 } 13786 }
13777 QString format = fromLatin1( cformat ); 13787 QString format = fromAscii( cformat );
13778 13788
13779 QRegExp escape( "%#?0?-? ?\\+?'?[0-9*]*\\.?[0-9*]*h?l?L?q?Z?" ); 13789 QRegExp escape( "%#?0?-? ?\\+?'?[0-9*]*\\.?[0-9*]*h?l?L?q?Z?" );
13780 QString result; 13790 QString result;
13781 uint last = 0; 13791 uint last = 0;
13782 int pos; 13792 int pos;
13783 int len = 0; 13793 int len = 0;
13784 13794
13785 for (;;) { 13795 for (;;) {
13786 pos = escape.search( format, last ); 13796 pos = escape.search( format, last );
13787 len = escape.matchedLength(); 13797 len = escape.matchedLength();
13788 // Non-escaped text 13798 // Non-escaped text
13789 if ( pos > (int)last ) 13799 if ( pos > (int)last )
13790 result += format.mid( last, pos - last ); 13800 result += format.mid( last, pos - last );
13791 if ( pos < 0 ) { 13801 if ( pos < 0 ) {
13792 // The rest 13802 // The rest
13793 if ( last < format.length() ) 13803 if ( last < format.length() )
13794 result += format.mid( last ); 13804 result += format.mid( last );
13795 break; 13805 break;
13796 } 13806 }
13797 last = pos + len + 1; 13807 last = pos + len + 1;
13798 13808
13799 // Escape 13809 // Escape
13800 QString f = format.mid( pos, len ); 13810 QString f = format.mid( pos, len );
13801 uint width, decimals; 13811 uint width, decimals;
@@ -13892,49 +13902,49 @@ QString &QString::sprintf( const char* cformat, ... )
13892 break; 13902 break;
13893 case 1: 13903 case 1:
13894 ::sprintf( out, in, width, value ); 13904 ::sprintf( out, in, width, value );
13895 break; 13905 break;
13896 case 2: 13906 case 2:
13897 ::sprintf( out, in, width, decimals, value ); 13907 ::sprintf( out, in, width, decimals, value );
13898 } 13908 }
13899 } 13909 }
13900 break; 13910 break;
13901 case 'p': 13911 case 'p':
13902 { 13912 {
13903 void* value = va_arg( ap, void * ); 13913 void* value = va_arg( ap, void * );
13904 switch ( params ) { 13914 switch ( params ) {
13905 case 0: 13915 case 0:
13906 ::sprintf( out, in, value ); 13916 ::sprintf( out, in, value );
13907 break; 13917 break;
13908 case 1: 13918 case 1:
13909 ::sprintf( out, in, width, value ); 13919 ::sprintf( out, in, width, value );
13910 break; 13920 break;
13911 case 2: 13921 case 2:
13912 ::sprintf( out, in, width, decimals, value ); 13922 ::sprintf( out, in, width, decimals, value );
13913 } 13923 }
13914 } 13924 }
13915 } 13925 }
13916 replacement = fromLatin1( out ); 13926 replacement = fromAscii( out );
13917 } 13927 }
13918 result += replacement; 13928 result += replacement;
13919 } 13929 }
13920 *this = result; 13930 *this = result;
13921 13931
13922 va_end( ap ); 13932 va_end( ap );
13923 return *this; 13933 return *this;
13924} 13934}
13925#endif 13935#endif
13926 13936
13927/*! 13937/*!
13928 Fills the string with \a len characters of value \a c, and returns 13938 Fills the string with \a len characters of value \a c, and returns
13929 a reference to the string. 13939 a reference to the string.
13930 13940
13931 If \a len is negative (the default), the current string length is 13941 If \a len is negative (the default), the current string length is
13932 used. 13942 used.
13933 13943
13934 \code 13944 \code
13935 QString str; 13945 QString str;
13936 str.fill( 'g', 5 ); // string == "ggggg" 13946 str.fill( 'g', 5 ); // string == "ggggg"
13937 \endcode 13947 \endcode
13938*/ 13948*/
13939 13949
13940QString& QString::fill( QChar c, int len ) 13950QString& QString::fill( QChar c, int len )
@@ -14103,50 +14113,52 @@ static int bm_find( const QString &str, int index, const QString &pattern, uint
14103 \overload 14113 \overload
14104 14114
14105 Finds the first occurrence of the string \a str, starting at 14115 Finds the first occurrence of the string \a str, starting at
14106 position \a index. If \a index is -1, the search starts at the 14116 position \a index. If \a index is -1, the search starts at the
14107 last character, if it is -2, at the next to last character and so 14117 last character, if it is -2, at the next to last character and so
14108 on. (See findRev() for searching backwards.) 14118 on. (See findRev() for searching backwards.)
14109 14119
14110 If \a cs is TRUE, the search is case sensitive; otherwise the 14120 If \a cs is TRUE, the search is case sensitive; otherwise the
14111 search is case insensitive. 14121 search is case insensitive.
14112 14122
14113 Returns the position of \a str or -1 if \a str could not be found. 14123 Returns the position of \a str or -1 if \a str could not be found.
14114*/ 14124*/
14115 14125
14116int QString::find( const QString& str, int index, bool cs ) const 14126int QString::find( const QString& str, int index, bool cs ) const
14117{ 14127{
14118 const uint l = length(); 14128 const uint l = length();
14119 const uint sl = str.length(); 14129 const uint sl = str.length();
14120 if ( index < 0 ) 14130 if ( index < 0 )
14121 index += l; 14131 index += l;
14122 if ( sl + index > l ) 14132 if ( sl + index > l )
14123 return -1; 14133 return -1;
14124 if ( !sl ) 14134 if ( !sl )
14125 return index; 14135 return index;
14126 14136
14137#ifndef MACOSX_101
14127 if ( sl == 1 ) 14138 if ( sl == 1 )
14128 return find( *str.unicode(), index, cs ); 14139 return find( *str.unicode(), index, cs );
14140#endif
14129 14141
14130 // we use the Boyer-Moore algorithm in cases where the overhead 14142 // we use the Boyer-Moore algorithm in cases where the overhead
14131 // for the hash table should pay off, otherwise we use a simple 14143 // for the hash table should pay off, otherwise we use a simple
14132 // hash function 14144 // hash function
14133 if ( l > 500 && sl > 5 ) { 14145 if ( l > 500 && sl > 5 ) {
14134 uint skiptable[0x100]; 14146 uint skiptable[0x100];
14135 bm_init_skiptable( str, skiptable, cs ); 14147 bm_init_skiptable( str, skiptable, cs );
14136 return bm_find( *this, index, str, skiptable, cs ); 14148 return bm_find( *this, index, str, skiptable, cs );
14137 } 14149 }
14138 14150
14139 /* 14151 /*
14140 We use some hashing for efficiency's sake. Instead of 14152 We use some hashing for efficiency's sake. Instead of
14141 comparing strings, we compare the hash value of str with that of 14153 comparing strings, we compare the hash value of str with that of
14142 a part of this QString. Only if that matches, we call ucstrncmp 14154 a part of this QString. Only if that matches, we call ucstrncmp
14143 or ucstrnicmp. 14155 or ucstrnicmp.
14144 */ 14156 */
14145 const QChar* needle = str.unicode(); 14157 const QChar* needle = str.unicode();
14146 const QChar* haystack = unicode() + index; 14158 const QChar* haystack = unicode() + index;
14147 const QChar* end = unicode() + (l-sl); 14159 const QChar* end = unicode() + (l-sl);
14148 const uint sl_minus_1 = sl-1; 14160 const uint sl_minus_1 = sl-1;
14149 uint hashNeedle = 0, hashHaystack = 0, i; 14161 uint hashNeedle = 0, hashHaystack = 0, i;
14150 14162
14151 if ( cs ) { 14163 if ( cs ) {
14152 for ( i = 0; i < sl; ++i ) { 14164 for ( i = 0; i < sl; ++i ) {
@@ -14200,102 +14212,108 @@ int QString::find( const QString& str, int index, bool cs ) const
14200 Equivalent to find(QString(\a str), \a index). 14212 Equivalent to find(QString(\a str), \a index).
14201*/ 14213*/
14202 14214
14203/*! 14215/*!
14204 \overload 14216 \overload
14205 14217
14206 Finds the first occurrence of the character \a c, starting at 14218 Finds the first occurrence of the character \a c, starting at
14207 position \a index and searching backwards. If the index is -1, the 14219 position \a index and searching backwards. If the index is -1, the
14208 search starts at the last character, if it is -2, at the next to 14220 search starts at the last character, if it is -2, at the next to
14209 last character and so on. 14221 last character and so on.
14210 14222
14211 Returns the position of \a c or -1 if \a c could not be found. 14223 Returns the position of \a c or -1 if \a c could not be found.
14212 14224
14213 If \a cs is TRUE, the search is case sensitive; otherwise the 14225 If \a cs is TRUE, the search is case sensitive; otherwise the
14214 search is case insensitive. 14226 search is case insensitive.
14215 14227
14216 \code 14228 \code
14217 QString string( "bananas" ); 14229 QString string( "bananas" );
14218 int i = string.findRev( 'a' ); // i == 5 14230 int i = string.findRev( 'a' ); // i == 5
14219 \endcode 14231 \endcode
14220*/ 14232*/
14221 14233
14222int QString::findRev( QChar c, int index, bool cs ) const 14234int QString::findRev( QChar c, int index, bool cs ) const
14223{ 14235{
14236#ifdef MACOSX_101
14237 return findRev( QString( c ), index, cs );
14238#else
14224 const uint l = length(); 14239 const uint l = length();
14225 if ( index < 0 ) 14240 if ( index < 0 )
14226 index += l; 14241 index += l;
14227 if ( (uint)index >= l ) 14242 if ( (uint)index >= l )
14228 return -1; 14243 return -1;
14229 const QChar *end = unicode(); 14244 const QChar *end = unicode();
14230 register const QChar *uc = end + index; 14245 register const QChar *uc = end + index;
14231 if ( cs ) { 14246 if ( cs ) {
14232 while ( uc >= end && *uc != c ) 14247 while ( uc >= end && *uc != c )
14233 uc--; 14248 uc--;
14234 } else { 14249 } else {
14235 c = ::lower( c ); 14250 c = ::lower( c );
14236 while ( uc >= end && ::lower( *uc ) != c ) 14251 while ( uc >= end && ::lower( *uc ) != c )
14237 uc--; 14252 uc--;
14238 } 14253 }
14239 return uc - end; 14254 return uc - end;
14255#endif
14240} 14256}
14241 14257
14242/*! 14258/*!
14243 \overload 14259 \overload
14244 14260
14245 Finds the first occurrence of the string \a str, starting at 14261 Finds the first occurrence of the string \a str, starting at
14246 position \a index and searching backwards. If the index is -1, the 14262 position \a index and searching backwards. If the index is -1, the
14247 search starts at the last character, if it is -2, at the next to 14263 search starts at the last character, if it is -2, at the next to
14248 last character and so on. 14264 last character and so on.
14249 14265
14250 Returns the position of \a str or -1 if \a str could not be found. 14266 Returns the position of \a str or -1 if \a str could not be found.
14251 14267
14252 If \a cs is TRUE, the search is case sensitive; otherwise the 14268 If \a cs is TRUE, the search is case sensitive; otherwise the
14253 search is case insensitive. 14269 search is case insensitive.
14254 14270
14255 \code 14271 \code
14256 QString string("bananas"); 14272 QString string("bananas");
14257 int i = string.findRev( "ana" ); // i == 3 14273 int i = string.findRev( "ana" ); // i == 3
14258 \endcode 14274 \endcode
14259*/ 14275*/
14260 14276
14261int QString::findRev( const QString& str, int index, bool cs ) const 14277int QString::findRev( const QString& str, int index, bool cs ) const
14262{ 14278{
14263 /* 14279 /*
14264 See QString::find() for explanations. 14280 See QString::find() for explanations.
14265 */ 14281 */
14266 const uint l = length(); 14282 const uint l = length();
14267 if ( index < 0 ) 14283 if ( index < 0 )
14268 index += l; 14284 index += l;
14269 const uint sl = str.length(); 14285 const uint sl = str.length();
14270 int delta = l-sl; 14286 int delta = l-sl;
14271 if ( index < 0 || index > (int)l || delta < 0 ) 14287 if ( index < 0 || index > (int)l || delta < 0 )
14272 return -1; 14288 return -1;
14273 if ( index > delta ) 14289 if ( index > delta )
14274 index = delta; 14290 index = delta;
14275 14291
14292#ifndef MACOSX_101
14276 if ( sl == 1 ) 14293 if ( sl == 1 )
14277 return findRev( *str.unicode(), index, cs ); 14294 return findRev( *str.unicode(), index, cs );
14295#endif
14278 14296
14279 const QChar* needle = str.unicode(); 14297 const QChar* needle = str.unicode();
14280 const QChar* haystack = unicode() + index; 14298 const QChar* haystack = unicode() + index;
14281 const QChar* end = unicode(); 14299 const QChar* end = unicode();
14282 const uint sl_minus_1 = sl-1; 14300 const uint sl_minus_1 = sl-1;
14283 const QChar* n = needle+sl_minus_1; 14301 const QChar* n = needle+sl_minus_1;
14284 const QChar* h = haystack+sl_minus_1; 14302 const QChar* h = haystack+sl_minus_1;
14285 uint hashNeedle = 0, hashHaystack = 0, i; 14303 uint hashNeedle = 0, hashHaystack = 0, i;
14286 14304
14287 if ( cs ) { 14305 if ( cs ) {
14288 for ( i = 0; i < sl; ++i ) { 14306 for ( i = 0; i < sl; ++i ) {
14289 hashNeedle = ((hashNeedle<<1) + (n-i)->unicode() ); 14307 hashNeedle = ((hashNeedle<<1) + (n-i)->unicode() );
14290 hashHaystack = ((hashHaystack<<1) + (h-i)->unicode() ); 14308 hashHaystack = ((hashHaystack<<1) + (h-i)->unicode() );
14291 } 14309 }
14292 hashHaystack -= haystack->unicode(); 14310 hashHaystack -= haystack->unicode();
14293 14311
14294 while ( haystack >= end ) { 14312 while ( haystack >= end ) {
14295 hashHaystack += haystack->unicode(); 14313 hashHaystack += haystack->unicode();
14296 if ( hashHaystack == hashNeedle 14314 if ( hashHaystack == hashNeedle
14297 && ucstrncmp( needle, haystack, sl ) == 0 ) 14315 && ucstrncmp( needle, haystack, sl ) == 0 )
14298 return haystack-unicode(); 14316 return haystack-unicode();
14299 --haystack; 14317 --haystack;
14300 REHASH( (haystack+sl)->unicode() ); 14318 REHASH( (haystack+sl)->unicode() );
14301 } 14319 }
@@ -14958,89 +14976,99 @@ QString QString::rightJustify( uint width, QChar fill, bool truncate ) const
14958 if ( len ) 14976 if ( len )
14959 memcpy( uc, unicode(), sizeof(QChar)*len ); 14977 memcpy( uc, unicode(), sizeof(QChar)*len );
14960 } else { 14978 } else {
14961 if ( truncate ) 14979 if ( truncate )
14962 result = left( width ); 14980 result = left( width );
14963 else 14981 else
14964 result = *this; 14982 result = *this;
14965 } 14983 }
14966 return result; 14984 return result;
14967} 14985}
14968 14986
14969/*! 14987/*!
14970 Returns a lowercase copy of the string. 14988 Returns a lowercase copy of the string.
14971 14989
14972 \code 14990 \code
14973 QString string( "TROlltECH" ); 14991 QString string( "TROlltECH" );
14974 str = string.lower(); // str == "trolltech" 14992 str = string.lower(); // str == "trolltech"
14975 \endcode 14993 \endcode
14976 14994
14977 \sa upper() 14995 \sa upper()
14978*/ 14996*/
14979 14997
14980QString QString::lower() const 14998QString 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
15008QString QString::upper() const 15031QString QString::upper() const
15009{ 15032{
15010 QString s(*this); 15033 int l = length();
15011 int l=length(); 15034 register QChar *p = d->unicode;
15012 if ( l ) { 15035 while ( l ) {
15013 s.real_detach(); // could do this only when we find a change 15036 if ( *p != ::upper(*p) ) {
15014 register QChar *p=s.d->unicode; 15037 QString s( *this );
15015 if ( p ) { 15038 s.real_detach();
15016 while ( l-- ) { 15039 p = s.d->unicode + ( p - d->unicode );
15040 while ( l ) {
15017 *p = ::upper( *p ); 15041 *p = ::upper( *p );
15042 l--;
15018 p++; 15043 p++;
15019 } 15044 }
15045 return s;
15020 } 15046 }
15047 l--;
15048 p++;
15021 } 15049 }
15022 return s; 15050 return *this;
15023} 15051}
15024 15052
15025 15053
15026/*! 15054/*!
15027 Returns a string that has whitespace removed from the start and 15055 Returns a string that has whitespace removed from the start and
15028 the end. 15056 the end.
15029 15057
15030 Whitespace means any character for which QChar::isSpace() returns 15058 Whitespace means any character for which QChar::isSpace() returns
15031 TRUE. This includes Unicode characters with decimal values 9 15059 TRUE. This includes Unicode characters with decimal values 9
15032 (TAB), 10 (LF), 11 (VT), 12 (FF), 13 (CR) and 32 (Space), and may 15060 (TAB), 10 (LF), 11 (VT), 12 (FF), 13 (CR) and 32 (Space), and may
15033 also include other Unicode characters. 15061 also include other Unicode characters.
15034 15062
15035 \code 15063 \code
15036 QString string = " white space "; 15064 QString string = " white space ";
15037 QString s = string.stripWhiteSpace(); // s == "white space" 15065 QString s = string.stripWhiteSpace(); // s == "white space"
15038 \endcode 15066 \endcode
15039 15067
15040 \sa simplifyWhiteSpace() 15068 \sa simplifyWhiteSpace()
15041*/ 15069*/
15042 15070
15043QString QString::stripWhiteSpace() const 15071QString QString::stripWhiteSpace() const
15044{ 15072{
15045 if ( isEmpty() ) // nothing to do 15073 if ( isEmpty() ) // nothing to do
15046 return *this; 15074 return *this;
@@ -15116,50 +15144,50 @@ QString QString::simplifyWhiteSpace() const
15116 Inserts \a s into the string at position \a index. 15144 Inserts \a s into the string at position \a index.
15117 15145
15118 If \a index is beyond the end of the string, the string is 15146 If \a index is beyond the end of the string, the string is
15119 extended with spaces to length \a index and \a s is then appended 15147 extended with spaces to length \a index and \a s is then appended
15120 and returns a reference to the string. 15148 and returns a reference to the string.
15121 15149
15122 \code 15150 \code
15123 QString string( "I like fish" ); 15151 QString string( "I like fish" );
15124 str = string.insert( 2, "don't " ); 15152 str = string.insert( 2, "don't " );
15125 // str == "I don't like fish" 15153 // str == "I don't like fish"
15126 \endcode 15154 \endcode
15127 15155
15128 \sa remove(), replace() 15156 \sa remove(), replace()
15129*/ 15157*/
15130 15158
15131QString &QString::insert( uint index, const QString &s ) 15159QString &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
15144QString &QString::insert( uint index, const QChar* s, uint len ) 15172QString &QString::insert( uint index, const QChar* s, uint len )
15145{ 15173{
15146 if ( len == 0 ) 15174 if ( len == 0 )
15147 return *this; 15175 return *this;
15148 uint olen = length(); 15176 uint olen = length();
15149 int nlen = olen + len; 15177 int nlen = olen + len;
15150 15178
15151 if ( s >= d->unicode && (uint)(s - d->unicode) < d->maxl ) { 15179 if ( s >= d->unicode && (uint)(s - d->unicode) < d->maxl ) {
15152 // Part of me - take a copy. 15180 // Part of me - take a copy.
15153 QChar *tmp = QT_ALLOC_QCHAR_VEC( len ); 15181 QChar *tmp = QT_ALLOC_QCHAR_VEC( len );
15154 memcpy(tmp,s,len*sizeof(QChar)); 15182 memcpy(tmp,s,len*sizeof(QChar));
15155 insert(index,tmp,len); 15183 insert(index,tmp,len);
15156 QT_DELETE_QCHAR_VEC( tmp ); 15184 QT_DELETE_QCHAR_VEC( tmp );
15157 return *this; 15185 return *this;
15158 } 15186 }
15159 15187
15160 if ( index >= olen ) { // insert after end of string 15188 if ( index >= olen ) { // insert after end of string
15161 setLength( len + index ); 15189 setLength( len + index );
15162 int n = index - olen; 15190 int n = index - olen;
15163 QChar* uc = d->unicode+olen; 15191 QChar* uc = d->unicode+olen;
15164 while (n--) 15192 while (n--)
15165 *uc++ = ' '; 15193 *uc++ = ' ';
@@ -15230,48 +15258,58 @@ QString &QString::insert( uint index, QChar c ) // insert char
15230 15258
15231/*! 15259/*!
15232 \fn QString& QString::prepend( QChar ch ) 15260 \fn QString& QString::prepend( QChar ch )
15233 15261
15234 \overload 15262 \overload
15235 15263
15236 Inserts \a ch at the beginning of the string and returns a 15264 Inserts \a ch at the beginning of the string and returns a
15237 reference to the string. 15265 reference to the string.
15238 15266
15239 Equivalent to insert(0, \a ch). 15267 Equivalent to insert(0, \a ch).
15240 15268
15241 \sa insert() 15269 \sa insert()
15242*/ 15270*/
15243 15271
15244/*! \fn QString& QString::prepend( const QByteArray &s ) 15272/*! \fn QString& QString::prepend( const QByteArray &s )
15245 \overload 15273 \overload
15246 15274
15247 Inserts \a s at the beginning of the string and returns a reference to the string. 15275 Inserts \a s at the beginning of the string and returns a reference to the string.
15248 15276
15249 Equivalent to insert(0, \a s). 15277 Equivalent to insert(0, \a s).
15250 15278
15251 \sa insert() 15279 \sa insert()
15252 */ 15280 */
15253 15281
15282/*! \fn QString& QString::prepend( const std::string &s )
15283 \overload
15284
15285 Inserts \a s at the beginning of the string and returns a reference to the string.
15286
15287 Equivalent to insert(0, \a s).
15288
15289 \sa insert()
15290 */
15291
15254/*! 15292/*!
15255 \overload 15293 \overload
15256 15294
15257 Inserts \a s at the beginning of the string and returns a reference to the string. 15295 Inserts \a s at the beginning of the string and returns a reference to the string.
15258 15296
15259 Equivalent to insert(0, \a s). 15297 Equivalent to insert(0, \a s).
15260 15298
15261 \sa insert() 15299 \sa insert()
15262 */ 15300 */
15263QString &QString::prepend( const char *s ) 15301QString &QString::prepend( const char *s )
15264{ 15302{
15265 return insert( 0, QString(s) ); 15303 return insert( 0, QString(s) );
15266} 15304}
15267 15305
15268/*! 15306/*!
15269 Removes \a len characters from the string starting at position \a 15307 Removes \a len characters from the string starting at position \a
15270 index, and returns a reference to the string. 15308 index, and returns a reference to the string.
15271 15309
15272 If \a index is beyond the length of the string, nothing happens. 15310 If \a index is beyond the length of the string, nothing happens.
15273 If \a index is within the string, but \a index + \a len is beyond 15311 If \a index is within the string, but \a index + \a len is beyond
15274 the end of the string, the string is truncated at position \a 15312 the end of the string, the string is truncated at position \a
15275 index. 15313 index.
15276 15314
15277 \code 15315 \code
@@ -15827,303 +15865,311 @@ int QString::contains( const QRegExp &rx ) const
15827#endif //QT_NO_REGEXP 15865#endif //QT_NO_REGEXP
15828 15866
15829static bool ok_in_base( QChar c, int base ) 15867static bool ok_in_base( QChar c, int base )
15830{ 15868{
15831 if ( base <= 10 ) 15869 if ( base <= 10 )
15832 return c.isDigit() && c.digitValue() < base; 15870 return c.isDigit() && c.digitValue() < base;
15833 else 15871 else
15834 return c.isDigit() || (c >= 'a' && c < char('a'+base-10)) 15872 return c.isDigit() || (c >= 'a' && c < char('a'+base-10))
15835 || (c >= 'A' && c < char('A'+base-10)); 15873 || (c >= 'A' && c < char('A'+base-10));
15836} 15874}
15837 15875
15838/*! 15876/*!
15839 Returns the string converted to a \c long value to the base \a 15877 Returns the string converted to a \c long value to the base \a
15840 base, which is 10 by default and must be between 2 and 36. 15878 base, which is 10 by default and must be between 2 and 36.
15841 15879
15842 If \a ok is not 0: if a conversion error occurs, \a *ok is set to 15880 If \a ok is not 0: if a conversion error occurs, \a *ok is set to
15843 FALSE; otherwise \a *ok is set to TRUE. 15881 FALSE; otherwise \a *ok is set to TRUE.
15844 15882
15845 \sa number() 15883 \sa number()
15846*/ 15884*/
15847 15885
15848long QString::toLong( bool *ok, int base ) const 15886long QString::toLong( bool *ok, int base ) const
15849{ 15887{
15850 const QChar *p = unicode(); 15888 const QChar *p = unicode();
15851 long val = 0; 15889 ulong val = 0;
15852 int l = length(); 15890 int l = length();
15853 const long max_mult = INT_MAX / base; 15891 const ulong max_mult = LONG_MAX / base;
15854 bool is_ok = FALSE; 15892 bool is_ok = FALSE;
15855 int neg = 0; 15893 int neg = 0;
15856 if ( !p ) 15894 if ( !p )
15857 goto bye; 15895 goto bye;
15858 while ( l && p->isSpace() ) // skip leading space 15896 while ( l && p->isSpace() ) // skip leading space
15859 l--,p++; 15897 l--,p++;
15860 if ( !l ) 15898 if ( !l )
15861 goto bye; 15899 goto bye;
15862 if ( *p == '-' ) { 15900 if ( *p == '-' ) {
15863 l--; 15901 l--;
15864 p++; 15902 p++;
15865 neg = 1; 15903 neg = 1;
15866 } else if ( *p == '+' ) { 15904 } else if ( *p == '+' ) {
15867 l--; 15905 l--;
15868 p++; 15906 p++;
15869 } 15907 }
15870 15908
15871 // NOTE: toULong() code is similar 15909 // NOTE: toULong() code is similar
15872 if ( !l || !ok_in_base(*p,base) ) 15910 if ( !l || !ok_in_base(*p,base) )
15873 goto bye; 15911 goto bye;
15874 while ( l && ok_in_base(*p,base) ) { 15912 while ( l && ok_in_base(*p,base) ) {
15875 l--; 15913 l--;
15876 int dv; 15914 int dv;
15877 if ( p->isDigit() ) { 15915 if ( p->isDigit() ) {
15878 dv = p->digitValue(); 15916 dv = p->digitValue();
15879 } else { 15917 } else {
15880 if ( *p >= 'a' && *p <= 'z' ) 15918 if ( *p >= 'a' && *p <= 'z' )
15881 dv = *p - 'a' + 10; 15919 dv = *p - 'a' + 10;
15882 else 15920 else
15883 dv = *p - 'A' + 10; 15921 dv = *p - 'A' + 10;
15884 } 15922 }
15885 if ( val > max_mult || 15923 if ( val > max_mult ||
15886 (val == max_mult && dv > (INT_MAX % base) + neg) ) 15924 (val == max_mult && dv > (LONG_MAX % base) + neg) )
15887 goto bye; 15925 goto bye;
15888 val = base * val + dv; 15926 val = base * val + dv;
15889 p++; 15927 p++;
15890 } 15928 }
15891 if ( neg )
15892 val = -val;
15893 while ( l && p->isSpace() ) // skip trailing space 15929 while ( l && p->isSpace() ) // skip trailing space
15894 l--,p++; 15930 l--, p++;
15895 if ( !l ) 15931 if ( !l )
15896 is_ok = TRUE; 15932 is_ok = TRUE;
15897bye: 15933bye:
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
15913ulong QString::toULong( bool *ok, int base ) const 15949ulong 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;
15953bye: 15989bye:
15954 if ( ok ) 15990 if ( ok )
15955 *ok = is_ok; 15991 *ok = is_ok;
15956 return is_ok ? val : 0; 15992 return is_ok ? val : 0;
15957} 15993}
15958 15994
15959/*! 15995/*!
15960 Returns the string converted to a \c short value to the base \a 15996 Returns the string converted to a \c short value to the base \a
15961 base, which is 10 by default and must be between 2 and 36. 15997 base, which is 10 by default and must be between 2 and 36.
15962 15998
15963 If \a ok is not 0: if a conversion error occurs, \a *ok is set to 15999 If \a ok is not 0: if a conversion error occurs, \a *ok is set to
15964 FALSE; otherwise \a *ok is set to TRUE. 16000 FALSE; otherwise \a *ok is set to TRUE.
15965*/ 16001*/
15966 16002
15967short QString::toShort( bool *ok, int base ) const 16003short 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
15986ushort QString::toUShort( bool *ok, int base ) const 16022ushort QString::toUShort( bool *ok, int base ) const
15987{ 16023{
15988 ulong v = toULong( ok, base ); 16024 ulong v = toULong( ok, base );
15989 if ( ok && *ok && (v > 65535) ) { 16025 if ( ok && *ok && (v > USHRT_MAX) ) {
15990 *ok = FALSE; 16026 *ok = FALSE;
15991 v = 0; 16027 v = 0;
15992 } 16028 }
15993 return (ushort)v; 16029 return (ushort)v;
15994} 16030}
15995 16031
15996 16032
15997/*! 16033/*!
15998 Returns the string converted to an \c int value to the base \a 16034 Returns the string converted to an \c int value to the base \a
15999 base, which is 10 by default and must be between 2 and 36. 16035 base, which is 10 by default and must be between 2 and 36.
16000 16036
16001 If \a ok is not 0: if a conversion error occurs, \a *ok is set to 16037 If \a ok is not 0: if a conversion error occurs, \a *ok is set to
16002 FALSE; otherwise \a *ok is set to TRUE. 16038 FALSE; otherwise \a *ok is set to TRUE.
16003 16039
16004 \code 16040 \code
16005 QString str( "FF" ); 16041 QString str( "FF" );
16006 bool ok; 16042 bool ok;
16007 int hex = str.toInt( &ok, 16 ); // hex == 255, ok == TRUE 16043 int hex = str.toInt( &ok, 16 ); // hex == 255, ok == TRUE
16008 int dec = str.toInt( &ok, 10 ); // dec == 0, ok == FALSE 16044 int dec = str.toInt( &ok, 10 ); // dec == 0, ok == FALSE
16009 \endcode 16045 \endcode
16010 16046
16011 \sa number() 16047 \sa number()
16012*/ 16048*/
16013 16049
16014int QString::toInt( bool *ok, int base ) const 16050int 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
16029uint QString::toUInt( bool *ok, int base ) const 16070uint 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
16048double QString::toDouble( bool *ok ) const 16094double 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
16068float QString::toFloat( bool *ok ) const 16114float QString::toFloat( bool *ok ) const
16069{ 16115{
16070 return (float)toDouble( ok ); 16116 return (float)toDouble( ok );
16071} 16117}
16072 16118
16073 16119
16074/*! 16120/*!
16075 Sets the string to the printed value of \a n in base \a base and 16121 Sets the string to the printed value of \a n in base \a base and
16076 returns a reference to the string. 16122 returns a reference to the string.
16077 16123
16078 The base is 10 by default and must be between 2 and 36. 16124 The base is 10 by default and must be between 2 and 36.
16079 16125
16080 \code 16126 \code
16081 QString string; 16127 QString string;
16082 string = string.setNum( 1234 ); // string == "1234" 16128 string = string.setNum( 1234 ); // string == "1234"
16083 \endcode 16129 \endcode
16084*/ 16130*/
16085 16131
16086QString &QString::setNum( long n, int base ) 16132QString &QString::setNum( long n, int base )
16087{ 16133{
16088#if defined(QT_CHECK_RANGE) 16134#if defined(QT_CHECK_RANGE)
16089 if ( base < 2 || base > 36 ) { 16135 if ( base < 2 || base > 36 ) {
16090 qWarning( "QString::setNum: Invalid base %d", base ); 16136 qWarning( "QString::setNum: Invalid base %d", base );
16091 base = 10; 16137 base = 10;
16092 } 16138 }
16093#endif 16139#endif
16094 char charbuf[65*sizeof(QChar)]; 16140 char charbuf[65*sizeof(QChar)];
16095 QChar *buf = (QChar*)charbuf; 16141 QChar *buf = (QChar*)charbuf;
16096 QChar *p = &buf[64]; 16142 QChar *p = &buf[64];
16097 int len = 0; 16143 int len = 0;
16098 bool neg; 16144 bool neg;
16099 if ( n < 0 ) { 16145 if ( n < 0 ) {
16100 neg = TRUE; 16146 neg = TRUE;
16101 if ( n == INT_MIN ) { 16147 if ( n == LONG_MIN ) {
16102 // Cannot always negate this special case 16148 // Cannot always negate this special case
16103 QString s1, s2; 16149 QString s1, s2;
16104 s1.setNum(n/base); 16150 s1.setNum(n/base, base );
16105 s2.setNum((-(n+base))%base); 16151 s2.setNum((-(n+base))%base, base );
16106 *this = s1 + s2; 16152 *this = s1 + s2;
16107 return *this; 16153 return *this;
16108 } 16154 }
16109 n = -n; 16155 n = -n;
16110 } else { 16156 } else {
16111 neg = FALSE; 16157 neg = FALSE;
16112 } 16158 }
16113 do { 16159 do {
16114 *--p = "0123456789abcdefghijklmnopqrstuvwxyz"[((int)(n%base))]; 16160 *--p = "0123456789abcdefghijklmnopqrstuvwxyz"[((int)(n%base))];
16115 n /= base; 16161 n /= base;
16116 ++len; 16162 ++len;
16117 } while ( n ); 16163 } while ( n );
16118 if ( neg ) { 16164 if ( neg ) {
16119 *--p = '-'; 16165 *--p = '-';
16120 ++len; 16166 ++len;
16121 } 16167 }
16122 return setUnicode( p, len ); 16168 return setUnicode( p, len );
16123} 16169}
16124 16170
16125/*! 16171/*!
16126 \overload 16172 \overload
16127 16173
16128 Sets the string to the printed value of \a n in base \a base and 16174 Sets the string to the printed value of \a n in base \a base and
16129 returns a reference to the string. 16175 returns a reference to the string.
@@ -16440,408 +16486,488 @@ void QString::setExpand( uint index, QChar c )
16440 the result. 16486 the result.
16441 16487
16442 Equivalent to operator+=(). 16488 Equivalent to operator+=().
16443*/ 16489*/
16444 16490
16445/*! 16491/*!
16446 \fn QString& QString::append( QChar ch ) 16492 \fn QString& QString::append( QChar ch )
16447 16493
16448 \overload 16494 \overload
16449 16495
16450 Appends character \a ch to the string and returns a reference to 16496 Appends character \a ch to the string and returns a reference to
16451 the result. 16497 the result.
16452 16498
16453 Equivalent to operator+=(). 16499 Equivalent to operator+=().
16454*/ 16500*/
16455 16501
16456/*! \fn QString& QString::append( const QByteArray &str ) 16502/*! \fn QString& QString::append( const QByteArray &str )
16457 \overload 16503 \overload
16458 16504
16459 Appends \a str to the string and returns a reference to the result. 16505 Appends \a str to the string and returns a reference to the result.
16460 16506
16461 Equivalent to operator+=(). 16507 Equivalent to operator+=().
16462 */ 16508 */
16463 16509
16510/*! \fn QString& QString::append( const std::string &str )
16511 \overload
16512
16513 Appends \a str to the string and returns a reference to the result.
16514
16515 Equivalent to operator+=().
16516 */
16517
16518
16464/*! \fn QString& QString::append( const char *str ) 16519/*! \fn QString& QString::append( const char *str )
16465 \overload 16520 \overload
16466 16521
16467 Appends \a str to the string and returns a reference to the result. 16522 Appends \a str to the string and returns a reference to the result.
16468 16523
16469 Equivalent to operator+=(). 16524 Equivalent to operator+=().
16470 */ 16525 */
16471 16526
16472/*! 16527/*!
16473 Appends \a str to the string and returns a reference to the string. 16528 Appends \a str to the string and returns a reference to the string.
16474*/ 16529*/
16475QString& QString::operator+=( const QString &str ) 16530QString& QString::operator+=( const QString &str )
16476{ 16531{
16477 uint len1 = length(); 16532 uint len1 = length();
16478 uint len2 = str.length(); 16533 uint len2 = str.length();
16479 if ( len2 ) { 16534 if ( len2 ) {
16480 setLength(len1+len2); 16535 setLength(len1+len2);
16481 memcpy( d->unicode+len1, str.unicode(), sizeof(QChar)*len2 ); 16536 memcpy( d->unicode+len1, str.unicode(), sizeof(QChar)*len2 );
16482 } else if ( isNull() && !str.isNull() ) { // ## just for 1.x compat: 16537 } else if ( isNull() && !str.isNull() ) { // ## just for 1.x compat:
16483 *this = fromLatin1( "" ); 16538 *this = fromLatin1( "" );
16484 } 16539 }
16485 return *this; 16540 return *this;
16486} 16541}
16487 16542
16488/*! 16543/*!
16489 \overload 16544 \overload
16490 16545
16491 Appends \a str to the string and returns a reference to the string. 16546 Appends \a str to the string and returns a reference to the string.
16492*/ 16547*/
16548#ifndef QT_NO_CAST_ASCII
16493QString& QString::operator+=( const char *str ) 16549QString& 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
16517QString &QString::operator+=( QChar c ) 16579QString &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
16530QString &QString::operator+=( char c ) 16592QString &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*/
16568const char* QString::latin1() const 16641const 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*/
16659const 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*/
16592QCString QString::utf8() const 16683QCString QString::utf8() const
16593{ 16684{
16594 int l = length(); 16685 int l = length();
16595 int rlen = l*3+1; 16686 int rlen = l*3+1;
16596 QCString rstr(rlen); 16687 QCString rstr(rlen);
16597 uchar* cursor = (uchar*)rstr.data(); 16688 uchar* cursor = (uchar*)rstr.data();
16598 const QChar *ch = d->unicode; 16689 const QChar *ch = d->unicode;
16599 for (int i=0; i<l; i++) { 16690 for (int i=0; i<l; i++) {
16600 ushort u = ch->unicode(); 16691 ushort u = ch->unicode();
16601 if ( u < 0x80 ) { 16692 if ( u < 0x80 ) {
16602 *cursor++ = (uchar)u; 16693 *cursor++ = (uchar)u;
16603 } else { 16694 } else {
16604 if ( u < 0x0800 ) { 16695 if ( u < 0x0800 ) {
16605 *cursor++ = 0xc0 | ((uchar) (u >> 6)); 16696 *cursor++ = 0xc0 | ((uchar) (u >> 6));
16606 } else { 16697 } else {
16607 *cursor++ = 0xe0 | ((uchar) (u >> 12)); 16698 *cursor++ = 0xe0 | ((uchar) (u >> 12));
16608 *cursor++ = 0x80 | ( ((uchar) (u >> 6)) & 0x3f); 16699 *cursor++ = 0x80 | ( ((uchar) (u >> 6)) & 0x3f);
16609 } 16700 }
16610 *cursor++ = 0x80 | ((uchar) (u&0x3f)); 16701 *cursor++ = 0x80 | ((uchar) (u&0x3f));
16611 } 16702 }
16612 ch++; 16703 ch++;
16613 } 16704 }
16614 rstr.truncate( cursor - (uchar*)rstr.data() ); 16705 rstr.truncate( cursor - (uchar*)rstr.data() );
16615 return rstr; 16706 return rstr;
16616} 16707}
16617 16708
16618/*! 16709/*!
16619 Returns the Unicode string decoded from the first \a len 16710 Returns the Unicode string decoded from the first \a len
16620 characters of \a utf8, ignoring the rest of \a utf8. If \a len is 16711 characters of \a utf8, ignoring the rest of \a utf8. If \a len is
16621 -1 then the length of \a utf8 is used. If \a len is bigger than 16712 -1 then the length of \a utf8 is used. If \a len is bigger than
16622 the length of \a utf8 then it will use the length of \a utf8. 16713 the length of \a utf8 then it will use the length of \a utf8.
16623 16714
16624 \code 16715 \code
16625 QString str = QString::fromUtf8( "123456789", 5 ); 16716 QString str = QString::fromUtf8( "123456789", 5 );
16626 // str == "12345" 16717 // str == "12345"
16627 \endcode 16718 \endcode
16628 16719
16629 See QTextCodec for more diverse coding/decoding of Unicode strings. 16720 See QTextCodec for more diverse coding/decoding of Unicode strings.
16630*/ 16721*/
16631QString QString::fromUtf8( const char* utf8, int len ) 16722QString QString::fromUtf8( const char* utf8, int len )
16632{ 16723{
16633 if ( !utf8 ) 16724 if ( !utf8 )
16634 return QString::null; 16725 return QString::null;
16635 16726
16636 if ( len < 0 ) len = qstrlen( utf8 ); 16727 if ( len < 0 )
16728 len = strlen( utf8 );
16637 QString result; 16729 QString result;
16638 result.setLength( len ); // worst case 16730 result.setLength( len ); // worst case
16639 QChar *qch = (QChar *)result.unicode(); 16731 QChar *qch = (QChar *)result.unicode();
16640 ushort uc = 0; 16732 ushort uc = 0;
16641 int need = 0; 16733 int need = 0;
16642 for (int i=0; i<len; i++) { 16734 for (int i=0; i<len; i++) {
16643 uchar ch = utf8[i]; 16735 uchar ch = utf8[i];
16644 if (need) { 16736 if (need) {
16645 if ( (ch&0xc0) == 0x80 ) { 16737 if ( (ch&0xc0) == 0x80 ) {
16646 uc = (uc << 6) | (ch & 0x3f); 16738 uc = (uc << 6) | (ch & 0x3f);
16647 need--; 16739 need--;
16648 if ( !need ) { 16740 if ( !need ) {
16649 *qch = uc; 16741 *qch = uc;
16650 qch++; 16742 qch++;
16651 } 16743 }
16652 } else { 16744 } else {
16653 // error 16745 // error
16654 *qch = QChar::replacement; 16746 *qch = QChar::replacement;
16655 qch++; 16747 qch++;
16656 need = 0; 16748 need = 0;
16657 } 16749 }
16658 } else { 16750 } else {
16659 if ( ch < 128 ) { 16751 if ( ch < 128 ) {
16660 *qch = ch; 16752 *qch = ch;
16661 qch++; 16753 qch++;
16662 } else if ( (ch&0xe0) == 0xc0 ) { 16754 } else if ( (ch&0xe0) == 0xc0 ) {
16663 uc = ch &0x1f; 16755 uc = ch &0x1f;
16664 need = 1; 16756 need = 1;
16665 } else if ( (ch&0xf0) == 0xe0 ) { 16757 } else if ( (ch&0xf0) == 0xe0 ) {
16666 uc = ch &0x0f; 16758 uc = ch &0x0f;
16667 need = 2; 16759 need = 2;
16668 } 16760 }
16669 } 16761 }
16670 } 16762 }
16671 result.truncate( qch - result.unicode() ); 16763 result.truncate( qch - result.unicode() );
16672 return result; 16764 return result;
16673} 16765}
16674 16766
16675/*! 16767/*!
16676 Returns the Unicode string decoded from the first \a len 16768 Returns the Unicode string decoded from the first \a len
16677 characters of \a chars, ignoring the rest of \a chars. If \a len 16769 characters of \a ascii, ignoring the rest of \a ascii. If \a len
16678 is -1 then the length of \a chars is used. If \a len is bigger 16770 is -1 then the length of \a ascii is used. If \a len is bigger
16679 than the length of \a chars then it will use the length of \a 16771 than the length of \a ascii then it will use the length of \a
16680 chars. 16772 ascii.
16773
16774 If a codec has been set using QTextCodec::codecForCStrings(),
16775 it is used to convert Unicode to 8-bit char. Otherwise, this function
16776 does the same as fromLatin1().
16681 16777
16682 This is the same as the QString(const char*) constructor, but you 16778 This is the same as the QString(const char*) constructor, but you
16683 can make that constructor invisible if you compile with the define 16779 can make that constructor invisible if you compile with the define
16684 \c QT_NO_CAST_ASCII, in which case you can explicitly create a 16780 \c QT_NO_CAST_ASCII, in which case you can explicitly create a
16685 QString from Latin-1 text using this function. 16781 QString from 8-bit ASCII text using this function.
16686 16782
16687 \code 16783 \code
16688 QString str = QString::fromLatin1( "123456789", 5 ); 16784 QString str = QString::fromAscii( "123456789", 5 );
16689 // str == "12345" 16785 // str == "12345"
16690 \endcode 16786 \endcode
16787 */
16788QString 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*/
16692QString QString::fromLatin1( const char* chars, int len ) 16814QString 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
16722QCString QString::local8Bit() const 16843QCString QString::local8Bit() const
16723{ 16844{
16724#ifdef QT_NO_TEXTCODEC 16845#ifdef QT_NO_TEXTCODEC
16725 return latin1(); 16846 return latin1();
16726#else 16847#else
16727#ifdef Q_WS_X11 16848#ifdef Q_WS_X11
16728 QTextCodec* codec = QTextCodec::codecForLocale(); 16849 QTextCodec* codec = QTextCodec::codecForLocale();
16729 return codec 16850 return codec
16730 ? codec->fromUnicode(*this) 16851 ? codec->fromUnicode(*this)
16731 : QCString(latin1()); 16852 : QCString(latin1());
16732#endif 16853#endif
16733#if defined( Q_WS_MACX ) 16854#if defined( Q_WS_MACX )
16734 return utf8(); 16855 return utf8();
16735#endif 16856#endif
16736#if defined( Q_WS_MAC9 ) 16857#if defined( Q_WS_MAC9 )
16737 return QCString(latin1()); //I'm evil.. 16858 return QCString(latin1()); //I'm evil..
16738#endif 16859#endif
16739#ifdef Q_WS_WIN 16860#ifdef Q_WS_WIN
16740 return qt_winQString2MB( *this ); 16861 return qt_winQString2MB( *this );
16741#endif 16862#endif
16742#ifdef Q_WS_QWS 16863#ifdef Q_WS_QWS
16743 return utf8(); // ##### if there is ANY 8 bit format supported? 16864 return utf8(); // ### if there is any 8 bit format supported?
16744#endif 16865#endif
16745#endif 16866#endif
16746} 16867}
16747 16868
16748/*! 16869/*!
16749 Returns the Unicode string decoded from the first \a len 16870 Returns the Unicode string decoded from the first \a len
16750 characters of \a local8Bit, ignoring the rest of \a local8Bit. If 16871 characters of \a local8Bit, ignoring the rest of \a local8Bit. If
16751 \a len is -1 then the length of \a local8Bit is used. If \a len is 16872 \a len is -1 then the length of \a local8Bit is used. If \a len is
16752 bigger than the length of \a local8Bit then it will use the length 16873 bigger than the length of \a local8Bit then it will use the length
16753 of \a local8Bit. 16874 of \a local8Bit.
16754 16875
16755 \code 16876 \code
16756 QString str = QString::fromLocal8Bit( "123456789", 5 ); 16877 QString str = QString::fromLocal8Bit( "123456789", 5 );
16757 // str == "12345" 16878 // str == "12345"
16758 \endcode 16879 \endcode
16759 16880
16760 \a local8Bit is assumed to be encoded in a locale-specific format. 16881 \a local8Bit is assumed to be encoded in a locale-specific format.
16761 16882
16762 See QTextCodec for more diverse coding/decoding of Unicode strings. 16883 See QTextCodec for more diverse coding/decoding of Unicode strings.
16763*/ 16884*/
16764QString QString::fromLocal8Bit( const char* local8Bit, int len ) 16885QString QString::fromLocal8Bit( const char* local8Bit, int len )
16765{ 16886{
16766#ifdef QT_NO_TEXTCODEC 16887#ifdef QT_NO_TEXTCODEC
16767 return fromLatin1( local8Bit, len ); 16888 return fromLatin1( local8Bit, len );
16768#else 16889#else
16769 16890
16770 if ( !local8Bit ) 16891 if ( !local8Bit )
16771 return QString::null; 16892 return QString::null;
16772#ifdef Q_WS_X11 16893#ifdef Q_WS_X11
16773 QTextCodec* codec = QTextCodec::codecForLocale(); 16894 QTextCodec* codec = QTextCodec::codecForLocale();
16774 if ( len < 0 ) len = qstrlen(local8Bit); 16895 if ( len < 0 )
16896 len = strlen( local8Bit );
16775 return codec 16897 return codec
16776 ? codec->toUnicode( local8Bit, len ) 16898 ? codec->toUnicode( local8Bit, len )
16777 : fromLatin1( local8Bit, len ); 16899 : fromLatin1( local8Bit, len );
16778#endif 16900#endif
16779#if defined( Q_WS_MAC ) 16901#if defined( Q_WS_MAC )
16780 return fromUtf8(local8Bit,len); 16902 return fromUtf8(local8Bit,len);
16781#endif 16903#endif
16782// Should this be OS_WIN32? 16904// Should this be OS_WIN32?
16783#ifdef Q_WS_WIN 16905#ifdef Q_WS_WIN
16784 if ( len >= 0 ) { 16906 if ( len >= 0 ) {
16785 QCString s(local8Bit,len+1); 16907 QCString s(local8Bit,len+1);
16786 return qt_winMB2QString(s); 16908 return qt_winMB2QString(s);
16787 } 16909 }
16788 return qt_winMB2QString( local8Bit ); 16910 return qt_winMB2QString( local8Bit );
16789#endif 16911#endif
16790#ifdef Q_WS_QWS 16912#ifdef Q_WS_QWS
16791 return fromUtf8(local8Bit,len); 16913 return fromUtf8(local8Bit,len);
16792#endif 16914#endif
16793#endif // QT_NO_TEXTCODEC 16915#endif // QT_NO_TEXTCODEC
16794} 16916}
16795 16917
16796/*! 16918/*!
16797 \fn QString::operator const char *() const 16919 \fn QString::operator const char *() const
16798 16920
16799 Returns latin1(). Be sure to see the warnings documented in the 16921 Returns latin1(). Be sure to see the warnings documented in the
16800 latin1() function. Note that for new code which you wish to be 16922 latin1() function. Note that for new code which you wish to be
16801 strictly Unicode-clean, you can define the macro \c 16923 strictly Unicode-clean, you can define the macro \c
16802 QT_NO_ASCII_CAST when compiling your code to hide this function so 16924 QT_NO_ASCII_CAST when compiling your code to hide this function so
16803 that automatic casts are not done. This has the added advantage 16925 that automatic casts are not done. This has the added advantage
16804 that you catch the programming error described in operator!(). 16926 that you catch the programming error described in operator!().
16805*/ 16927*/
16806 16928
16929/*!
16930 \fn QString::operator std::string() const
16931
16932 Returns ascii().
16933*/
16934
16807 16935
16808/*! 16936/*!
16809 Returns the QString as a zero terminated array of unsigned shorts 16937 Returns the QString as a zero terminated array of unsigned shorts
16810 if the string is not null; otherwise returns zero. 16938 if the string is not null; otherwise returns zero.
16811 16939
16812 The result remains valid so long as one unmodified 16940 The result remains valid so long as one unmodified
16813 copy of the source string exists. 16941 copy of the source string exists.
16814*/ 16942*/
16815const unsigned short *QString::ucs2() const 16943const unsigned short *QString::ucs2() const
16816{ 16944{
16817 if ( ! d->unicode ) 16945 if ( ! d->unicode )
16818 return 0; 16946 return 0;
16819 unsigned int len = d->len; 16947 unsigned int len = d->len;
16820 if ( d->maxl < len + 1 ) { 16948 if ( d->maxl < len + 1 ) {
16821 // detach, grow or shrink 16949 // detach, grow or shrink
16822 Q2HELPER(stat_copy_on_write++)
16823 Q2HELPER(stat_copy_on_write_size += len)
16824 uint newMax = computeNewMax( len + 1 ); 16950 uint newMax = computeNewMax( len + 1 );
16825 QChar* nd = QT_ALLOC_QCHAR_VEC( newMax ); 16951 QChar* nd = QT_ALLOC_QCHAR_VEC( newMax );
16826 if ( nd ) { 16952 if ( nd ) {
16827 if ( d->unicode ) 16953 if ( d->unicode )
16828 memcpy( nd, d->unicode, sizeof(QChar)*len ); 16954 memcpy( nd, d->unicode, sizeof(QChar)*len );
16829 ((QString *)this)->deref(); 16955 ((QString *)this)->deref();
16830 ((QString *)this)->d = new QStringData( nd, len, newMax ); 16956 ((QString *)this)->d = new QStringData( nd, len, newMax );
16831 } 16957 }
16832 } 16958 }
16833 d->unicode[len] = 0; 16959 d->unicode[len] = 0;
16834 return (unsigned short *) d->unicode; 16960 return (unsigned short *) d->unicode;
16835} 16961}
16836 16962
16837/*! 16963/*!
16838 Constructs a string that is a deep copy of \a str, interpreted as a 16964 Constructs a string that is a deep copy of \a str, interpreted as a
16839 UCS2 encoded, zero terminated, Unicode string. 16965 UCS2 encoded, zero terminated, Unicode string.
16840 16966
16841 If \a str is 0, then a null string is created. 16967 If \a str is 0, then a null string is created.
16842 16968
16843 \sa isNull() 16969 \sa isNull()
16844*/ 16970*/
16845QString QString::fromUcs2( const unsigned short *str ) 16971QString QString::fromUcs2( const unsigned short *str )
16846{ 16972{
16847 if ( !str ) { 16973 if ( !str ) {
@@ -16962,126 +17088,146 @@ void QString::subat( uint i )
16962 } 17088 }
16963} 17089}
16964 17090
16965 17091
16966/*! 17092/*!
16967 Resizes the string to \a len characters and copies \a unicode into 17093 Resizes the string to \a len characters and copies \a unicode into
16968 the string. If \a unicode is 0, nothing is copied, but the 17094 the string. If \a unicode is 0, nothing is copied, but the
16969 string is still resized to \a len. If \a len is zero, then the 17095 string is still resized to \a len. If \a len is zero, then the
16970 string becomes a \link isNull() null\endlink string. 17096 string becomes a \link isNull() null\endlink string.
16971 17097
16972 \sa setLatin1(), isNull() 17098 \sa setLatin1(), isNull()
16973*/ 17099*/
16974 17100
16975QString& QString::setUnicode( const QChar *unicode, uint len ) 17101QString& QString::setUnicode( const QChar *unicode, uint len )
16976{ 17102{
16977 if ( len == 0 ) { // set to null string 17103 if ( len == 0 ) { // set to null string
16978 if ( d != shared_null ) { // beware of nullstring being set to nullstring 17104 if ( d != shared_null ) { // beware of nullstring being set to nullstring
16979 deref(); 17105 deref();
16980 d = shared_null ? shared_null : makeSharedNull(); 17106 d = shared_null ? shared_null : makeSharedNull();
16981 d->ref(); 17107 d->ref();
16982 } 17108 }
16983 } else if ( d->count != 1 || len > d->maxl || 17109 } else if ( d->count != 1 || len > d->maxl ||
16984 ( len * 4 < d->maxl && d->maxl > 4 ) ) { 17110 ( len * 4 < d->maxl && d->maxl > 4 ) ) {
16985 // detach, grown or shrink 17111 // detach, grown or shrink
16986 Q2HELPER(stat_copy_on_write++)
16987 Q2HELPER(stat_copy_on_write_size+=d->len)
16988 uint newMax = computeNewMax( len ); 17112 uint newMax = computeNewMax( len );
16989 QChar* nd = QT_ALLOC_QCHAR_VEC( newMax ); 17113 QChar* nd = QT_ALLOC_QCHAR_VEC( newMax );
16990 if ( unicode ) 17114 if ( unicode )
16991 memcpy( nd, unicode, sizeof(QChar)*len ); 17115 memcpy( nd, unicode, sizeof(QChar)*len );
16992 deref(); 17116 deref();
16993 d = new QStringData( nd, len, newMax ); 17117 d = new QStringData( nd, len, newMax );
16994 } else { 17118 } else {
16995 d->len = len; 17119 d->len = len;
16996 d->setDirty(); 17120 d->setDirty();
16997 if ( unicode ) 17121 if ( unicode )
16998 memcpy( d->unicode, unicode, sizeof(QChar)*len ); 17122 memcpy( d->unicode, unicode, sizeof(QChar)*len );
16999 } 17123 }
17000 return *this; 17124 return *this;
17001} 17125}
17002 17126
17003/*! 17127/*!
17004 Resizes the string to \a len characters and copies \a 17128 Resizes the string to \a len characters and copies \a
17005 unicode_as_ushorts into the string (on some X11 client platforms 17129 unicode_as_ushorts into the string (on some X11 client platforms
17006 this will involve a byte-swapping pass). 17130 this will involve a byte-swapping pass).
17007 17131
17008 If \a unicode_as_ushorts is 0, nothing is copied, but the string 17132 If \a unicode_as_ushorts is 0, nothing is copied, but the string
17009 is still resized to \a len. If \a len is zero, the string becomes 17133 is still resized to \a len. If \a len is zero, the string becomes
17010 a \link isNull() null\endlink string. 17134 a \link isNull() null\endlink string.
17011 17135
17012 \sa setLatin1(), isNull() 17136 \sa setLatin1(), isNull()
17013*/ 17137*/
17014QString& QString::setUnicodeCodes( const ushort* unicode_as_ushorts, uint len ) 17138QString& 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
17155QString &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
17031QString &QString::setLatin1( const char *str, int len ) 17177QString &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 */
17050void QString::checkSimpleText() const 17196void 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 */
17072bool QString::isRightToLeft() const 17218bool QString::isRightToLeft() const
17073{ 17219{
17074 int len = length(); 17220 int len = length();
17075 QChar *p = d->unicode; 17221 QChar *p = d->unicode;
17076 while( len-- ) { 17222 while( len-- ) {
17077 switch( ::direction( *p ) ) 17223 switch( ::direction( *p ) )
17078 { 17224 {
17079 case QChar::DirL: 17225 case QChar::DirL:
17080 case QChar::DirLRO: 17226 case QChar::DirLRO:
17081 case QChar::DirLRE: 17227 case QChar::DirLRE:
17082 return FALSE; 17228 return FALSE;
17083 case QChar::DirR: 17229 case QChar::DirR:
17084 case QChar::DirAL: 17230 case QChar::DirAL:
17085 case QChar::DirRLO: 17231 case QChar::DirRLO:
17086 case QChar::DirRLE: 17232 case QChar::DirRLE:
17087 return TRUE; 17233 return TRUE;