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
@@ -5,7 +5,7 @@
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**
@@ -47,11 +47,11 @@
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
@@ -59,6 +59,10 @@
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
@@ -11787,6 +11791,7 @@ static inline QChar::Direction direction( const QChar &c )
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}
@@ -11799,6 +11804,7 @@ static inline bool mirrored( const QChar &c )
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}
@@ -11900,7 +11906,7 @@ static uint computeNewMax( uint len )
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;
@@ -12891,7 +12897,7 @@ void QString::compose()
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;
@@ -12910,7 +12916,7 @@ QChar* QString::asciiToUnicode( const QByteArray& ba, uint* len )
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;
@@ -12940,7 +12946,7 @@ static QChar* internalAsciiToUnicode( const QByteArray& ba, uint* len )
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;
@@ -12950,7 +12956,7 @@ QChar* QString::asciiToUnicode( const char *str, uint* len, uint maxlen )
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;
@@ -12963,7 +12969,7 @@ QChar* QString::asciiToUnicode( const char *str, uint* len, uint maxlen )
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;
@@ -12974,7 +12980,7 @@ static QChar* internalAsciiToUnicode( const char *str, uint* len,
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;
@@ -12994,7 +13000,7 @@ static QChar* internalAsciiToUnicode( const char *str, uint* len,
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;
@@ -13157,35 +13163,6 @@ QStringData* QString::makeSharedNull()
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
@@ -13211,7 +13188,6 @@ QString::QString( QChar ch )
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
@@ -13230,13 +13206,10 @@ QString::QString( const QString &s ) :
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 }
@@ -13249,9 +13222,15 @@ QString::QString( int size, bool /*dummy*/ )
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
@@ -13302,12 +13281,39 @@ QString::QString( const QChar* unicode, uint length )
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()
@@ -13333,10 +13339,10 @@ void QString::real_detach()
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
@@ -13352,6 +13358,15 @@ void QStringData::deleteSelf()
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
@@ -13368,7 +13383,6 @@ void QStringData::deleteSelf()
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;
@@ -13383,7 +13397,7 @@ QString &QString::operator=( const QString &s )
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
@@ -13399,7 +13413,7 @@ QString &QString::operator=( const QCString& cs )
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
@@ -13471,8 +13485,7 @@ void QString::truncate( uint newLen )
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.
@@ -13486,12 +13499,12 @@ void QString::truncate( uint newLen )
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
@@ -13505,8 +13518,6 @@ void QString::setLength( uint newLen )
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 ) {
@@ -13530,21 +13541,20 @@ void QString::setLength( uint newLen )
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
@@ -13774,7 +13784,7 @@ QString &QString::sprintf( const char* cformat, ... )
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;
@@ -13913,7 +13923,7 @@ QString &QString::sprintf( const char* cformat, ... )
13913 } 13923 }
13914 } 13924 }
13915 } 13925 }
13916 replacement = fromLatin1( out ); 13926 replacement = fromAscii( out );
13917 } 13927 }
13918 result += replacement; 13928 result += replacement;
13919 } 13929 }
@@ -14124,8 +14134,10 @@ int QString::find( const QString& str, int index, bool cs ) const
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
@@ -14221,6 +14233,9 @@ int QString::find( const QString& str, int index, bool cs ) const
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;
@@ -14237,6 +14252,7 @@ int QString::findRev( QChar c, int index, bool cs ) const
14237 uc--; 14252 uc--;
14238 } 14253 }
14239 return uc - end; 14254 return uc - end;
14255#endif
14240} 14256}
14241 14257
14242/*! 14258/*!
@@ -14273,8 +14289,10 @@ int QString::findRev( const QString& str, int index, bool cs ) const
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;
@@ -14979,19 +14997,24 @@ QString QString::rightJustify( uint width, QChar fill, bool truncate ) const
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/*!
@@ -15007,19 +15030,24 @@ QString QString::lower() const
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
@@ -15137,8 +15165,8 @@ QString &QString::insert( uint index, const QString &s )
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 )
@@ -15251,6 +15279,16 @@ QString &QString::insert( uint index, QChar c ) // insert char
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
@@ -15848,9 +15886,9 @@ static bool ok_in_base( QChar c, int base )
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 )
@@ -15883,21 +15921,19 @@ long QString::toLong( bool *ok, int base ) const
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/*!
@@ -15915,7 +15951,7 @@ ulong QString::toULong( bool *ok, int base ) const
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;
@@ -15940,7 +15976,7 @@ ulong QString::toULong( bool *ok, int base ) const
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++;
@@ -15967,7 +16003,7 @@ bye:
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 }
@@ -15986,7 +16022,7 @@ short QString::toShort( bool *ok, int base ) const
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 }
@@ -16013,7 +16049,12 @@ ushort QString::toUShort( bool *ok, int base ) const
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/*!
@@ -16028,7 +16069,12 @@ int QString::toInt( bool *ok, int base ) const
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/*!
@@ -16052,7 +16098,7 @@ double QString::toDouble( bool *ok ) const
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
@@ -16098,11 +16144,11 @@ QString &QString::setNum( long n, int base )
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 }
@@ -16461,6 +16507,15 @@ void QString::setExpand( uint index, QChar c )
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
@@ -16490,9 +16545,15 @@ QString& QString::operator+=( const QString &str )
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 ) {
@@ -16508,6 +16569,7 @@ QString& QString::operator+=( const char *str )
16508 } 16569 }
16509 return *this; 16570 return *this;
16510} 16571}
16572#endif
16511 16573
16512/*! \overload 16574/*! \overload
16513 16575
@@ -16529,6 +16591,10 @@ QString &QString::operator+=( QChar c )
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;
@@ -16541,18 +16607,25 @@ QString &QString::operator+=( char c )
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.
@@ -16563,31 +16636,49 @@ QString &QString::operator+=( char c )
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{
@@ -16633,7 +16724,8 @@ QString QString::fromUtf8( const char* utf8, int len )
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();
@@ -16674,20 +16766,50 @@ QString QString::fromUtf8( const char* utf8, int len )
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{
@@ -16695,7 +16817,7 @@ QString QString::fromLatin1( const char* chars, int len )
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
@@ -16709,16 +16831,15 @@ QString QString::fromLatin1( const char* chars, int len )
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
@@ -16740,7 +16861,7 @@ QCString QString::local8Bit() const
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}
@@ -16771,7 +16892,8 @@ QString QString::fromLocal8Bit( const char* local8Bit, int len )
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 );
@@ -16804,6 +16926,12 @@ QString QString::fromLocal8Bit( const char* local8Bit, int len )
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
@@ -16819,8 +16947,6 @@ const unsigned short *QString::ucs2() const
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 ) {
@@ -16983,8 +17109,6 @@ QString& QString::setUnicode( const QChar *unicode, uint len )
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 )
@@ -17018,6 +17142,28 @@ QString& QString::setUnicodeCodes( const ushort* unicode_as_ushorts, uint len )
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).
@@ -17033,7 +17179,7 @@ QString &QString::setLatin1( const char *str, int len )
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 {
@@ -17051,16 +17197,16 @@ void QString::checkSimpleText() const
17051{ 17197{
17052 QChar *p = d->unicode; 17198 QChar *p = d->unicode;
17053 QChar *end = p + d->len; 17199 QChar *end = p + d->len;
17054 d->simpletext = 1;
17055 while( p < end ) { 17200 while( p < end ) {
17056 ushort uc = p->unicode(); 17201 ushort uc = p->unicode();
17057 // sort out regions of complex text formatting 17202 // sort out regions of complex text formatting
17058 if ( uc > 0x058f && ( uc < 0x1100 || uc > 0xfb0f ) ) { 17203 if ( uc > 0x058f && ( uc < 0x1100 || uc > 0xfb0f ) ) {
17059 d->simpletext = 0; 17204 d->issimpletext = FALSE;
17060 return; 17205 return;
17061 } 17206 }
17062 p++; 17207 p++;
17063 } 17208 }
17209 d->issimpletext = TRUE;
17064} 17210}
17065 17211
17066/*! \fn bool QString::simpleText() const 17212/*! \fn bool QString::simpleText() const