-rw-r--r-- | qmake/tools/qstring.cpp | 434 |
1 files changed, 290 insertions, 144 deletions
diff --git a/qmake/tools/qstring.cpp b/qmake/tools/qstring.cpp index 56df62b..7f1fac3 100644 --- a/qmake/tools/qstring.cpp +++ b/qmake/tools/qstring.cpp @@ -1,16 +1,16 @@ /**************************************************************************** ** $Id$ ** ** Implementation of the QString class and related Unicode functions ** ** Created : 920722 ** -** Copyright (C) 1992-2000 Trolltech AS. All rights reserved. +** Copyright (C) 1992-2002 Trolltech AS. All rights reserved. ** ** This file is part of the tools module of the Qt GUI Toolkit. ** ** This file may be distributed under the terms of the Q Public License ** as defined by Trolltech AS of Norway and appearing in the file ** LICENSE.QPL included in the packaging of this file. ** ** This file may be distributed and/or modified under the terms of the @@ -42,28 +42,32 @@ #endif #include "qstring.h" #include "qregexp.h" #include "qdatastream.h" #ifndef QT_NO_TEXTCODEC #include "qtextcodec.h" #endif -#include <ctype.h> #include <limits.h> #include <stdarg.h> #include <stdio.h> #include <stdlib.h> +#include <string.h> #if defined(Q_WS_WIN) #include "qt_windows.h" #endif #if !defined( QT_NO_COMPONENT ) && !defined( QT_LITE_COMPONENT ) #include "qcleanuphandler.h" #endif +#ifdef QT_NO_UNICODETABLES +# include <ctype.h> +#endif + /* ------------------------------------------------------------------------- * unicode information * these tables are generated from the unicode reference file * ftp://ftp.unicode.org/Public/3.2-Update/UnicodeData.txt * * Lars * ------------------------------------------------------------------------- @@ -11782,28 +11786,30 @@ static inline QChar upper( const QChar &c ) static inline QChar::Direction direction( const QChar &c ) { #ifndef QT_NO_UNICODETABLES const Q_UINT8 *rowp = direction_info[c.row()]; if(!rowp) return QChar::DirL; return (QChar::Direction) ( *(rowp+c.cell()) & 0x1f ); #else + Q_UNUSED(c); return QChar::DirL; #endif } static inline bool mirrored( const QChar &c ) { #ifndef QT_NO_UNICODETABLES const Q_UINT8 *rowp = direction_info[c.row()]; if ( !rowp ) return FALSE; return *(rowp+c.cell())>128; #else + Q_UNUSED(c); return FALSE; #endif } #ifndef QT_NO_UNICODETABLES static const Q_UINT16 symmetricPairs[] = { 0x0028, 0x0029, 0x003C, 0x003E, 0x005B, 0x005D, 0x007B, 0x007D, 0x00AB, 0x00BB, 0x2039, 0x203A, 0x2045, 0x2046, 0x207D, 0x207E, @@ -11895,17 +11901,17 @@ static int ucstrnicmp( const QChar *a, const QChar *b, int l ) return ::lower( *a ).unicode() - ::lower( *b ).unicode(); } static uint computeNewMax( uint len ) { uint newMax = 4; while ( newMax < len ) newMax *= 2; - // try to spare some memory + // try to save some memory if ( newMax >= 1024 * 1024 && len <= newMax - (newMax >> 2) ) newMax -= newMax >> 2; return newMax; } /*! \class QCharRef qstring.h \reentrant @@ -12886,17 +12892,17 @@ void QString::compose() /*! This utility function converts the 8-bit string \a ba to Unicode, returning the result. The caller is responsible for deleting the return value with delete[]. */ -QChar* QString::asciiToUnicode( const QByteArray& ba, uint* len ) +QChar* QString::latin1ToUnicode( const QByteArray& ba, uint* len ) { if ( ba.isNull() ) { *len = 0; return 0; } int l = 0; while ( l < (int)ba.size() && ba[l] ) l++; @@ -12905,17 +12911,17 @@ QChar* QString::asciiToUnicode( const QByteArray& ba, uint* len ) QChar *result = uc; if ( len ) *len = l; while (l--) *uc++ = *str++; return result; } -static QChar* internalAsciiToUnicode( const QByteArray& ba, uint* len ) +static QChar* internalLatin1ToUnicode( const QByteArray& ba, uint* len ) { if ( ba.isNull() ) { *len = 0; return 0; } int l = 0; while ( l < (int)ba.size() && ba[l] ) l++; @@ -12935,51 +12941,51 @@ static QChar* internalAsciiToUnicode( const QByteArray& ba, uint* len ) This utility function converts the '\0'-terminated 8-bit string \a str to Unicode, returning the result and setting \a *len to the length of the Unicode string. The caller is responsible for deleting the return value with delete[]. */ -QChar* QString::asciiToUnicode( const char *str, uint* len, uint maxlen ) +QChar* QString::latin1ToUnicode( const char *str, uint* len, uint maxlen ) { QChar* result = 0; uint l = 0; if ( str ) { if ( maxlen != (uint)-1 ) { while ( l < maxlen && str[l] ) l++; } else { // Faster? - l = qstrlen(str); + l = strlen( str ); } QChar *uc = new QChar[ l ]; // Can't use macro since function is public result = uc; uint i = l; while ( i-- ) *uc++ = *str++; } if ( len ) *len = l; return result; } -static QChar* internalAsciiToUnicode( const char *str, uint* len, +static QChar* internalLatin1ToUnicode( const char *str, uint* len, uint maxlen = (uint)-1 ) { QChar* result = 0; uint l = 0; if ( str ) { if ( maxlen != (uint)-1 ) { while ( l < maxlen && str[l] ) l++; } else { // Faster? - l = qstrlen(str); + l = strlen( str ); } QChar *uc = QT_ALLOC_QCHAR_VEC( l ); result = uc; uint i = l; while ( i-- ) *uc++ = *str++; } if ( len ) @@ -12989,17 +12995,17 @@ static QChar* internalAsciiToUnicode( const char *str, uint* len, /*! This utility function converts \a l 16-bit characters from \a uc to ASCII, returning a '\0'-terminated string. The caller is responsible for deleting the resultant string with delete[]. */ -char* QString::unicodeToAscii(const QChar *uc, uint l) +char* QString::unicodeToLatin1(const QChar *uc, uint l) { if (!uc) { return 0; } char *a = new char[l+1]; char *result = a; while (l--) { *a++ = (uc->unicode() > 0xff) ? '?' : (char)uc->unicode(); @@ -13152,45 +13158,16 @@ QStringData* QString::makeSharedNull() QString::shared_null = new QStringData; #if defined( Q_OS_MAC ) QString *that = const_cast<QString *>(&QString::null); that->d = QString::shared_null; #endif return QString::shared_null; } -// Uncomment this to get some useful statistics. -// #define Q2HELPER(x) x - -#ifdef Q2HELPER -static int stat_construct_charstar=0; -static int stat_construct_charstar_size=0; -static int stat_construct_null=0; -static int stat_construct_int=0; -static int stat_construct_int_size=0; -static int stat_construct_ba=0; -static int stat_get_ascii=0; -static int stat_get_ascii_size=0; -static int stat_copy_on_write=0; -static int stat_copy_on_write_size=0; -static int stat_fast_copy=0; -Q_EXPORT void qt_qstring_stats() -{ - qDebug("construct_charstar = %d (%d chars)", stat_construct_charstar, stat_construct_charstar_size); - qDebug("construct_null = %d", stat_construct_null); - qDebug("construct_int = %d (%d chars)", stat_construct_int, stat_construct_int_size); - qDebug("construct_ba = %d", stat_construct_ba); - qDebug("get_ascii = %d (%d chars)", stat_get_ascii, stat_get_ascii_size); - qDebug("copy_on_write = %d (%d chars)", stat_copy_on_write, stat_copy_on_write_size); - qDebug("fast_copy = %d", stat_fast_copy); -} -#else -#define Q2HELPER(x) -#endif - /*! \fn QString::QString() Constructs a null string, i.e. both the length and data pointer are 0. \sa isNull() */ @@ -13206,17 +13183,16 @@ QString::QString( QChar ch ) /*! Constructs an implicitly shared copy of \a s. This is very fast since it only involves incrementing a reference count. */ QString::QString( const QString &s ) : d(s.d) { - Q2HELPER(stat_fast_copy++) d->ref(); } /*! \internal Private function. @@ -13225,38 +13201,41 @@ QString::QString( const QString &s ) : The string is empty. \sa isNull() */ QString::QString( int size, bool /*dummy*/ ) { if ( size ) { - Q2HELPER(stat_construct_int++) int l = size; - Q2HELPER(stat_construct_int_size+=l) QChar* uc = QT_ALLOC_QCHAR_VEC( l ); d = new QStringData( uc, 0, l ); } else { - Q2HELPER(stat_construct_null++) d = shared_null ? shared_null : (shared_null=new QStringData); d->ref(); } } /*! Constructs a string that is a deep copy of \a ba interpreted as a classic C string. */ QString::QString( const QByteArray& ba ) { - Q2HELPER(stat_construct_ba++) +#ifndef QT_NO_TEXTCODEC + if ( QTextCodec::codecForCStrings() ) { + d = 0; + *this = fromAscii( ba.data(), ba.size() ); + return; + } +#endif uint l; - QChar *uc = internalAsciiToUnicode(ba,&l); + QChar *uc = internalLatin1ToUnicode(ba,&l); d = new QStringData(uc,l,l); } /*! Constructs a string that is a deep copy of the first \a length characters in the QChar array. If \a unicode and \a length are 0, then a null string is created. @@ -13297,22 +13276,49 @@ QString::QString( const QChar* unicode, uint length ) fromUtf8(). Or whatever encoding is appropriate for the 8-bit data you have. \sa isNull() */ QString::QString( const char *str ) { - Q2HELPER(stat_construct_charstar++) +#ifndef QT_NO_TEXTCODEC + if ( QTextCodec::codecForCStrings() ) { + d = 0; + *this = fromAscii( str ); + return; + } +#endif + uint l; + QChar *uc = internalLatin1ToUnicode(str,&l); + d = new QStringData(uc,l,l); +} + +#ifndef QT_NO_STL +/*! + Constructs a string that is a deep copy of \a str. + + This is the same as fromAscii(\a str). +*/ + +QString::QString( const std::string &str ) +{ +#ifndef QT_NO_TEXTCODEC + if ( QTextCodec::codecForCStrings() ) { + d = 0; + *this = fromAscii( str.c_str() ); + return; + } +#endif uint l; - QChar *uc = internalAsciiToUnicode(str,&l); - Q2HELPER(stat_construct_charstar_size+=l) + QChar *uc = internalLatin1ToUnicode(str.c_str(),&l); d = new QStringData(uc,l,l); } +#endif /*! \fn QString::~QString() Destroys the string and frees the string's data if this is the last reference to the string. */ @@ -13328,83 +13334,91 @@ QString::QString( const char *str ) void QString::real_detach() { setLength( length() ); } void QString::deref() { - if ( d->deref() ) { + if ( d && d->deref() ) { if ( d != shared_null ) delete d; - d = 0; // helps debugging + d = 0; } } void QStringData::deleteSelf() { delete this; } /*! \fn QString& QString::operator=( QChar c ) Sets the string to contain just the single character \a c. */ /*! + \fn QString& QString::operator=( const std::string& s ) + + \overload + + Makes a deep copy of \a s and returns a reference to the deep + copy. +*/ + +/*! \fn QString& QString::operator=( char c ) \overload Sets the string to contain just the single character \a c. */ /*! \overload Assigns a shallow copy of \a s to this string and returns a reference to this string. This is very fast because the string isn't actually copied. */ QString &QString::operator=( const QString &s ) { - Q2HELPER(stat_fast_copy++) s.d->ref(); deref(); d = s.d; return *this; } /*! \overload Assigns a deep copy of \a cs, interpreted as a classic C string, to this string and returns a reference to this string. */ QString &QString::operator=( const QCString& cs ) { - return setLatin1(cs); + return setAscii(cs); } /*! \overload Assigns a deep copy of \a str, interpreted as a classic C string to this string and returns a reference to this string. If \a str is 0, then a null string is created. \sa isNull() */ QString &QString::operator=( const char *str ) { - return setLatin1(str); + return setAscii(str); } /*! \fn bool QString::isNull() const Returns TRUE if the string is null; otherwise returns FALSE. A null string is always empty. @@ -13466,52 +13480,49 @@ void QString::truncate( uint newLen ) setLength( newLen ); } /*! Ensures that at least \a newLen characters are allocated to the string, and sets the length of the string to \a newLen. Any new space allocated contains arbitrary data. - If \a newLen is 0, then the string becomes empty, unless the - string is null, in which case it remains null. + If \a newLen is 0, then the string becomes empty (non-null). If it is not possible to allocate enough memory, the string remains unchanged. This function always detaches the string from other references to the same data. This function is useful for code that needs to build up a long string and wants to avoid repeated reallocation. In this example, we want to add to the string until some condition is true, and we're fairly sure that size is big enough: \code QString result; - int resultLength = 0; - result.setLength( newLen ) // allocate some space + int len = 0; + result.setLength( maxLen ); // allocate some space while ( ... ) { - result[resultLength++] = ... // fill (part of) the space with data + result[len++] = ... // fill part of the space } - result.truncate[resultLength]; // and get rid of the undefined junk + result.truncate( len ); // and get rid of the rest \endcode If \a newLen is an underestimate, the worst that will happen is that the loop will slow down. \sa truncate(), isNull(), isEmpty(), length() */ void QString::setLength( uint newLen ) { if ( d->count != 1 || newLen > d->maxl || ( newLen * 4 < d->maxl && d->maxl > 4 ) ) { // detach, grow or shrink - Q2HELPER(stat_copy_on_write++) - Q2HELPER(stat_copy_on_write_size+=d->len) uint newMax = computeNewMax( newLen ); QChar* nd = QT_ALLOC_QCHAR_VEC( newMax ); if ( nd ) { uint len = QMIN( d->len, newLen ); if ( d->unicode ) memcpy( nd, d->unicode, sizeof(QChar)*len ); deref(); d = new QStringData( nd, newLen, newMax ); @@ -13525,31 +13536,30 @@ void QString::setLength( uint newLen ) /*! This function will return a string that replaces the lowest numbered occurrence of \c %1, \c %2, ..., \c %9 with \a a. The \a fieldwidth value specifies the minimum amount of space that \a a is padded to. A positive value will produce right-aligned text, whereas a negative value will produce left-aligned text. + The following example shows how we could create a 'status' string + when processing a list of files: \code - QString firstName( "Joe" ); - QString lastName( "Bloggs" ); - QString fullName; - fullName = QString( "First name is '%1', last name is '%2'" ) - .arg( firstName ) - .arg( lastName ); - - // fullName == First name is 'Joe', last name is 'Bloggs' + QString status = QString( "Processing file %1 of %2: %3" ) + .arg( i ) // current file's number + .arg( total ) // number of files to process + .arg( fileName ); // current file's name \endcode - Note that using arg() to construct sentences as we've done in the - example above does not usually translate well into other languages - because sentence structure and word order often differ between - languages. + It is generally fine to use filenames and numbers as we have done + in the example above. But note that using arg() to construct + natural language sentences does not usually translate well into + other languages because sentence structure and word order often + differ between languages. If there is no place marker (\c %1 or \c %2, etc.), a warning message (qWarning()) is output and the text is appended at the end of the string. We recommend that the correct number of place markers is always used in production code. */ QString QString::arg( const QString& a, int fieldwidth ) const { @@ -13769,17 +13779,17 @@ QString &QString::sprintf( const char* cformat, ... ) va_list ap; va_start( ap, cformat ); if ( !cformat || !*cformat ) { // Qt 1.x compat *this = fromLatin1( "" ); return *this; } - QString format = fromLatin1( cformat ); + QString format = fromAscii( cformat ); QRegExp escape( "%#?0?-? ?\\+?'?[0-9*]*\\.?[0-9*]*h?l?L?q?Z?" ); QString result; uint last = 0; int pos; int len = 0; for (;;) { @@ -13908,17 +13918,17 @@ QString &QString::sprintf( const char* cformat, ... ) case 1: ::sprintf( out, in, width, value ); break; case 2: ::sprintf( out, in, width, decimals, value ); } } } - replacement = fromLatin1( out ); + replacement = fromAscii( out ); } result += replacement; } *this = result; va_end( ap ); return *this; } @@ -14119,18 +14129,20 @@ int QString::find( const QString& str, int index, bool cs ) const const uint sl = str.length(); if ( index < 0 ) index += l; if ( sl + index > l ) return -1; if ( !sl ) return index; +#ifndef MACOSX_101 if ( sl == 1 ) return find( *str.unicode(), index, cs ); +#endif // we use the Boyer-Moore algorithm in cases where the overhead // for the hash table should pay off, otherwise we use a simple // hash function if ( l > 500 && sl > 5 ) { uint skiptable[0x100]; bm_init_skiptable( str, skiptable, cs ); return bm_find( *this, index, str, skiptable, cs ); @@ -14216,32 +14228,36 @@ int QString::find( const QString& str, int index, bool cs ) const \code QString string( "bananas" ); int i = string.findRev( 'a' ); // i == 5 \endcode */ int QString::findRev( QChar c, int index, bool cs ) const { +#ifdef MACOSX_101 + return findRev( QString( c ), index, cs ); +#else const uint l = length(); if ( index < 0 ) index += l; if ( (uint)index >= l ) return -1; const QChar *end = unicode(); register const QChar *uc = end + index; if ( cs ) { while ( uc >= end && *uc != c ) uc--; } else { c = ::lower( c ); while ( uc >= end && ::lower( *uc ) != c ) uc--; } return uc - end; +#endif } /*! \overload Finds the first occurrence of the string \a str, starting at position \a index and searching backwards. If the index is -1, the search starts at the last character, if it is -2, at the next to @@ -14268,18 +14284,20 @@ int QString::findRev( const QString& str, int index, bool cs ) const index += l; const uint sl = str.length(); int delta = l-sl; if ( index < 0 || index > (int)l || delta < 0 ) return -1; if ( index > delta ) index = delta; +#ifndef MACOSX_101 if ( sl == 1 ) return findRev( *str.unicode(), index, cs ); +#endif const QChar* needle = str.unicode(); const QChar* haystack = unicode() + index; const QChar* end = unicode(); const uint sl_minus_1 = sl-1; const QChar* n = needle+sl_minus_1; const QChar* h = haystack+sl_minus_1; uint hashNeedle = 0, hashHaystack = 0, i; @@ -14974,57 +14992,67 @@ QString QString::rightJustify( uint width, QChar fill, bool truncate ) const str = string.lower(); // str == "trolltech" \endcode \sa upper() */ QString QString::lower() const { - QString s(*this); - int l=length(); - if ( l ) { - s.real_detach(); // could do this only when we find a change - register QChar *p=s.d->unicode; - if ( p ) { - while ( l-- ) { + int l = length(); + register QChar *p = d->unicode; + while ( l ) { + if ( *p != ::lower(*p) ) { + QString s( *this ); + s.real_detach(); + p = s.d->unicode + ( p - d->unicode ); + while ( l ) { *p = ::lower( *p ); + l--; p++; } + return s; } + l--; + p++; } - return s; + return *this; } /*! Returns an uppercase copy of the string. \code QString string( "TeXt" ); str = string.upper(); // t == "TEXT" \endcode \sa lower() */ QString QString::upper() const { - QString s(*this); - int l=length(); - if ( l ) { - s.real_detach(); // could do this only when we find a change - register QChar *p=s.d->unicode; - if ( p ) { - while ( l-- ) { + int l = length(); + register QChar *p = d->unicode; + while ( l ) { + if ( *p != ::upper(*p) ) { + QString s( *this ); + s.real_detach(); + p = s.d->unicode + ( p - d->unicode ); + while ( l ) { *p = ::upper( *p ); + l--; p++; } + return s; } + l--; + p++; } - return s; + return *this; } /*! Returns a string that has whitespace removed from the start and the end. Whitespace means any character for which QChar::isSpace() returns @@ -15132,18 +15160,18 @@ QString &QString::insert( uint index, const QString &s ) { // the sub function takes care of &s == this case. return insert( index, s.unicode(), s.length() ); } /*! \overload - Inserts the character in \a s into the string at position \a index - \a len number of times and returns a reference to the string. + Inserts the first \a len characters in \a s into the string at + position \a index and returns a reference to the string. */ QString &QString::insert( uint index, const QChar* s, uint len ) { if ( len == 0 ) return *this; uint olen = length(); int nlen = olen + len; @@ -15246,16 +15274,26 @@ QString &QString::insert( uint index, QChar c ) // insert char Inserts \a s at the beginning of the string and returns a reference to the string. Equivalent to insert(0, \a s). \sa insert() */ +/*! \fn QString& QString::prepend( const std::string &s ) + \overload + + Inserts \a s at the beginning of the string and returns a reference to the string. + + Equivalent to insert(0, \a s). + + \sa insert() + */ + /*! \overload Inserts \a s at the beginning of the string and returns a reference to the string. Equivalent to insert(0, \a s). \sa insert() @@ -15843,19 +15881,19 @@ static bool ok_in_base( QChar c, int base ) FALSE; otherwise \a *ok is set to TRUE. \sa number() */ long QString::toLong( bool *ok, int base ) const { const QChar *p = unicode(); - long val = 0; + ulong val = 0; int l = length(); - const long max_mult = INT_MAX / base; + const ulong max_mult = LONG_MAX / base; bool is_ok = FALSE; int neg = 0; if ( !p ) goto bye; while ( l && p->isSpace() ) // skip leading space l--,p++; if ( !l ) goto bye; @@ -15878,31 +15916,29 @@ long QString::toLong( bool *ok, int base ) const dv = p->digitValue(); } else { if ( *p >= 'a' && *p <= 'z' ) dv = *p - 'a' + 10; else dv = *p - 'A' + 10; } if ( val > max_mult || - (val == max_mult && dv > (INT_MAX % base) + neg) ) + (val == max_mult && dv > (LONG_MAX % base) + neg) ) goto bye; val = base * val + dv; p++; } - if ( neg ) - val = -val; while ( l && p->isSpace() ) // skip trailing space - l--,p++; + l--, p++; if ( !l ) is_ok = TRUE; bye: if ( ok ) *ok = is_ok; - return is_ok ? val : 0; + return is_ok ? ( neg ? -( (long) val ) : (long) val ) : 0L; } /*! Returns the string converted to an \c {unsigned long} value to the base \a base, which is 10 by default and must be between 2 and 36. If \a ok is not 0: if a conversion error occurs, \a *ok is set to FALSE; otherwise \a *ok is set to TRUE. @@ -15910,17 +15946,17 @@ bye: \sa number() */ ulong QString::toULong( bool *ok, int base ) const { const QChar *p = unicode(); ulong val = 0; int l = length(); - const ulong max_mult = UINT_MAX / base; + const ulong max_mult = ULONG_MAX / base; bool is_ok = FALSE; if ( !p ) goto bye; while ( l && p->isSpace() ) // skip leading space l--,p++; if ( !l ) goto bye; if ( *p == '+' ) @@ -15935,17 +15971,17 @@ ulong QString::toULong( bool *ok, int base ) const if ( p->isDigit() ) { dv = p->digitValue(); } else { if ( *p >= 'a' && *p <= 'z' ) dv = *p - 'a' + 10; else dv = *p - 'A' + 10; } - if ( val > max_mult || (val == max_mult && dv > UINT_MAX % base) ) + if ( val > max_mult || (val == max_mult && dv > ULONG_MAX % base) ) goto bye; val = base * val + dv; p++; } while ( l && p->isSpace() ) // skip trailing space l--,p++; if ( !l ) @@ -15962,17 +15998,17 @@ bye: If \a ok is not 0: if a conversion error occurs, \a *ok is set to FALSE; otherwise \a *ok is set to TRUE. */ short QString::toShort( bool *ok, int base ) const { long v = toLong( ok, base ); - if ( ok && *ok && (v < -32768 || v > 32767) ) { + if ( ok && *ok && (v < SHRT_MIN || v > SHRT_MAX) ) { *ok = FALSE; v = 0; } return (short)v; } /*! Returns the string converted to an \c {unsigned short} value to @@ -15981,17 +16017,17 @@ short QString::toShort( bool *ok, int base ) const If \a ok is not 0: if a conversion error occurs, \a *ok is set to FALSE; otherwise \a *ok is set to TRUE. */ ushort QString::toUShort( bool *ok, int base ) const { ulong v = toULong( ok, base ); - if ( ok && *ok && (v > 65535) ) { + if ( ok && *ok && (v > USHRT_MAX) ) { *ok = FALSE; v = 0; } return (ushort)v; } /*! @@ -16008,32 +16044,42 @@ ushort QString::toUShort( bool *ok, int base ) const int dec = str.toInt( &ok, 10 ); // dec == 0, ok == FALSE \endcode \sa number() */ int QString::toInt( bool *ok, int base ) const { - return (int)toLong( ok, base ); + long v = toLong( ok, base ); + if ( ok && *ok && (v < INT_MIN || v > INT_MAX) ) { + *ok = FALSE; + v = 0; + } + return (int)v; } /*! Returns the string converted to an \c{unsigned int} value to the base \a base, which is 10 by default and must be between 2 and 36. If \a ok is not 0: if a conversion error occurs, \a *ok is set to FALSE; otherwise \a *ok is set to TRUE. \sa number() */ uint QString::toUInt( bool *ok, int base ) const { - return (uint)toULong( ok, base ); + ulong v = toULong( ok, base ); + if ( ok && *ok && (v > UINT_MAX) ) { + *ok = FALSE; + v = 0; + } + return (uint)v; } /*! Returns the string converted to a \c double value. If \a ok is not 0: if a conversion error occurs, \a *ok is set to FALSE; otherwise \a *ok is set to TRUE. @@ -16047,17 +16093,17 @@ uint QString::toUInt( bool *ok, int base ) const double QString::toDouble( bool *ok ) const { char *end; const char *a = latin1(); double val = strtod( a ? a : "", &end ); if ( ok ) - *ok = ( a && *a && (end == 0 || (end - a) == (int)length()) ); + *ok = ( a && *a && (end == 0 || *end == '\0') ); return val; } /*! Returns the string converted to a \c float value. If \a ok is not 0: if a conversion error occurs, \a *ok is set to FALSE; otherwise \a *ok is set to TRUE. @@ -16093,21 +16139,21 @@ QString &QString::setNum( long n, int base ) #endif char charbuf[65*sizeof(QChar)]; QChar *buf = (QChar*)charbuf; QChar *p = &buf[64]; int len = 0; bool neg; if ( n < 0 ) { neg = TRUE; - if ( n == INT_MIN ) { + if ( n == LONG_MIN ) { // Cannot always negate this special case QString s1, s2; - s1.setNum(n/base); - s2.setNum((-(n+base))%base); + s1.setNum(n/base, base ); + s2.setNum((-(n+base))%base, base ); *this = s1 + s2; return *this; } n = -n; } else { neg = FALSE; } do { @@ -16456,16 +16502,25 @@ void QString::setExpand( uint index, QChar c ) /*! \fn QString& QString::append( const QByteArray &str ) \overload Appends \a str to the string and returns a reference to the result. Equivalent to operator+=(). */ +/*! \fn QString& QString::append( const std::string &str ) + \overload + + Appends \a str to the string and returns a reference to the result. + + Equivalent to operator+=(). + */ + + /*! \fn QString& QString::append( const char *str ) \overload Appends \a str to the string and returns a reference to the result. Equivalent to operator+=(). */ @@ -16485,34 +16540,41 @@ QString& QString::operator+=( const QString &str ) return *this; } /*! \overload Appends \a str to the string and returns a reference to the string. */ +#ifndef QT_NO_CAST_ASCII QString& QString::operator+=( const char *str ) { if ( str ) { +#ifndef QT_NO_TEXTCODEC + if ( QTextCodec::codecForCStrings() ) + return operator+=( fromAscii( str ) ); +#endif + uint len1 = length(); uint len2 = strlen( str ); if ( len2 ) { setLength(len1+len2); uint i = 0; while( i < len2 ) { d->unicode[len1+i] = str[i]; i++; } } else if ( isNull() ) { // ## just for 1.x compat: *this = fromLatin1( "" ); } } return *this; } +#endif /*! \overload Appends \a c to the string and returns a reference to the string. */ QString &QString::operator+=( QChar c ) { @@ -16524,75 +16586,104 @@ QString &QString::operator+=( QChar c ) /*! \overload Appends \a c to the string and returns a reference to the string. */ QString &QString::operator+=( char c ) { +#ifndef QT_NO_TEXTCODEC + if ( QTextCodec::codecForCStrings() ) + return operator+=( fromAscii( &c, 1 ) ); +#endif setLength(length()+1); d->unicode[length()-1] = c; return *this; } /*! \fn QString &QString::operator+=( const QByteArray &str ) \overload Appends \a str to the string and returns a reference to the string. */ +/*! + \fn QString &QString::operator+=( const std::string &str ) + \overload + + Appends \a str to the string and returns a reference to the string. +*/ + /*! \fn char QChar::latin1() const - Returns a latin-1 copy of this character, if this character is in - the latin-1 character set. If not, this function returns 0. + Returns the Latin-1 value of this character, or 0 if it + cannot be represented in Latin-1. */ /*! - Returns a Latin-1 representation of the string. Note that the + Returns a Latin-1 representation of the string. The returned value is undefined if the string contains non-Latin-1 characters. If you want to convert strings into formats other than Unicode, see the QTextCodec classes. This function is mainly useful for boot-strapping legacy code to use Unicode. The result remains valid so long as one unmodified copy of the source string exists. - \sa utf8(), local8Bit() + \sa fromLatin1(), ascii(), utf8(), local8Bit() */ const char* QString::latin1() const { - if ( !d->ascii ) { - Q2HELPER(stat_get_ascii++) - Q2HELPER(stat_get_ascii_size+=d->len) - d->ascii = unicodeToAscii( d->unicode, d->len ); + if ( !d->ascii || !d->islatin1 ) { + d->ascii = unicodeToLatin1( d->unicode, d->len ); + d->islatin1 = TRUE; } return d->ascii; } /*! - \fn const char* QString::ascii() const - \obsolete + Returns an 8-bit ASCII representation of the string. - This function simply calls latin1() and returns the result. + If a codec has been set using QTextCodec::codecForCStrings(), + it is used to convert Unicode to 8-bit char. Otherwise, this function + does the same as latin1(). + + \sa fromAscii(), latin1(), utf8(), local8Bit() */ +const char* QString::ascii() const +{ +#ifndef QT_NO_TEXTCODEC + if ( QTextCodec::codecForCStrings() ) { + if ( !d->ascii || d->islatin1 ) { + QCString s = QTextCodec::codecForCStrings()->fromUnicode( *this ); + s.detach(); + d->ascii = s.data(); + d->islatin1 = FALSE; + s.resetRawData( s.data(), s.size() ); // we have stolen the data + } + return d->ascii; + } +#endif // QT_NO_TEXTCODEC + return latin1(); +} /*! - Returns the string encoded in UTF8 format. + Returns the string encoded in UTF-8 format. See QTextCodec for more diverse coding/decoding of Unicode strings. - \sa QString::fromUtf8(), local8Bit(), latin1() + \sa fromUtf8(), ascii(), latin1(), local8Bit() */ QCString QString::utf8() const { int l = length(); int rlen = l*3+1; QCString rstr(rlen); uchar* cursor = (uchar*)rstr.data(); const QChar *ch = d->unicode; @@ -16628,17 +16719,18 @@ QCString QString::utf8() const See QTextCodec for more diverse coding/decoding of Unicode strings. */ QString QString::fromUtf8( const char* utf8, int len ) { if ( !utf8 ) return QString::null; - if ( len < 0 ) len = qstrlen( utf8 ); + if ( len < 0 ) + len = strlen( utf8 ); QString result; result.setLength( len ); // worst case QChar *qch = (QChar *)result.unicode(); ushort uc = 0; int need = 0; for (int i=0; i<len; i++) { uchar ch = utf8[i]; if (need) { @@ -16669,61 +16761,90 @@ QString QString::fromUtf8( const char* utf8, int len ) } } result.truncate( qch - result.unicode() ); return result; } /*! Returns the Unicode string decoded from the first \a len - characters of \a chars, ignoring the rest of \a chars. If \a len - is -1 then the length of \a chars is used. If \a len is bigger - than the length of \a chars then it will use the length of \a - chars. + characters of \a ascii, ignoring the rest of \a ascii. If \a len + is -1 then the length of \a ascii is used. If \a len is bigger + than the length of \a ascii then it will use the length of \a + ascii. + + If a codec has been set using QTextCodec::codecForCStrings(), + it is used to convert Unicode to 8-bit char. Otherwise, this function + does the same as fromLatin1(). This is the same as the QString(const char*) constructor, but you can make that constructor invisible if you compile with the define \c QT_NO_CAST_ASCII, in which case you can explicitly create a - QString from Latin-1 text using this function. + QString from 8-bit ASCII text using this function. \code - QString str = QString::fromLatin1( "123456789", 5 ); + QString str = QString::fromAscii( "123456789", 5 ); // str == "12345" \endcode + */ +QString QString::fromAscii( const char* ascii, int len ) +{ +#ifndef QT_NO_TEXTCODEC + if ( QTextCodec::codecForCStrings() ) { + if ( !ascii ) + return QString::null; + if ( len < 0 ) + len = strlen( ascii ); + if ( len == 0 || *ascii == '\0' ) + return QString::fromLatin1( "" ); + return QTextCodec::codecForCStrings()->toUnicode( ascii, len ); + } +#endif + return fromLatin1( ascii, len ); +} + + +/*! + Returns the Unicode string decoded from the first \a len + characters of \a chars, ignoring the rest of \a chars. If \a len + is -1 then the length of \a chars is used. If \a len is bigger + than the length of \a chars then it will use the length of \a + chars. + + \sa fromAscii() */ QString QString::fromLatin1( const char* chars, int len ) { uint l; QChar *uc; if ( len < 0 ) len = -1; - uc = internalAsciiToUnicode( chars, &l, len ); + uc = internalLatin1ToUnicode( chars, &l, len ); return QString( new QStringData(uc, l, l), TRUE ); } /*! \fn const QChar* QString::unicode() const Returns the Unicode representation of the string. The result remains valid until the string is modified. */ /*! Returns the string encoded in a locale-specific format. On X11, this is the QTextCodec::codecForLocale(). On Windows, it is a - system-defined encoding. On Mac OS X, this always uses utf8 as the - encoding. + system-defined encoding. On Mac OS X, this always uses UTF-8 as + the encoding. See QTextCodec for more diverse coding/decoding of Unicode strings. - \sa QString::fromLocal8Bit(), latin1(), utf8() + \sa fromLocal8Bit(), ascii(), latin1(), utf8() */ - QCString QString::local8Bit() const { #ifdef QT_NO_TEXTCODEC return latin1(); #else #ifdef Q_WS_X11 QTextCodec* codec = QTextCodec::codecForLocale(); return codec @@ -16735,17 +16856,17 @@ QCString QString::local8Bit() const #endif #if defined( Q_WS_MAC9 ) return QCString(latin1()); //I'm evil.. #endif #ifdef Q_WS_WIN return qt_winQString2MB( *this ); #endif #ifdef Q_WS_QWS - return utf8(); // ##### if there is ANY 8 bit format supported? + return utf8(); // ### if there is any 8 bit format supported? #endif #endif } /*! Returns the Unicode string decoded from the first \a len characters of \a local8Bit, ignoring the rest of \a local8Bit. If \a len is -1 then the length of \a local8Bit is used. If \a len is @@ -16766,17 +16887,18 @@ QString QString::fromLocal8Bit( const char* local8Bit, int len ) #ifdef QT_NO_TEXTCODEC return fromLatin1( local8Bit, len ); #else if ( !local8Bit ) return QString::null; #ifdef Q_WS_X11 QTextCodec* codec = QTextCodec::codecForLocale(); - if ( len < 0 ) len = qstrlen(local8Bit); + if ( len < 0 ) + len = strlen( local8Bit ); return codec ? codec->toUnicode( local8Bit, len ) : fromLatin1( local8Bit, len ); #endif #if defined( Q_WS_MAC ) return fromUtf8(local8Bit,len); #endif // Should this be OS_WIN32? @@ -16799,33 +16921,37 @@ QString QString::fromLocal8Bit( const char* local8Bit, int len ) Returns latin1(). Be sure to see the warnings documented in the latin1() function. Note that for new code which you wish to be strictly Unicode-clean, you can define the macro \c QT_NO_ASCII_CAST when compiling your code to hide this function so that automatic casts are not done. This has the added advantage that you catch the programming error described in operator!(). */ +/*! + \fn QString::operator std::string() const + + Returns ascii(). +*/ + /*! Returns the QString as a zero terminated array of unsigned shorts if the string is not null; otherwise returns zero. The result remains valid so long as one unmodified copy of the source string exists. */ const unsigned short *QString::ucs2() const { if ( ! d->unicode ) return 0; unsigned int len = d->len; if ( d->maxl < len + 1 ) { // detach, grow or shrink - Q2HELPER(stat_copy_on_write++) - Q2HELPER(stat_copy_on_write_size += len) uint newMax = computeNewMax( len + 1 ); QChar* nd = QT_ALLOC_QCHAR_VEC( newMax ); if ( nd ) { if ( d->unicode ) memcpy( nd, d->unicode, sizeof(QChar)*len ); ((QString *)this)->deref(); ((QString *)this)->d = new QStringData( nd, len, newMax ); } @@ -16978,18 +17104,16 @@ QString& QString::setUnicode( const QChar *unicode, uint len ) if ( d != shared_null ) { // beware of nullstring being set to nullstring deref(); d = shared_null ? shared_null : makeSharedNull(); d->ref(); } } else if ( d->count != 1 || len > d->maxl || ( len * 4 < d->maxl && d->maxl > 4 ) ) { // detach, grown or shrink - Q2HELPER(stat_copy_on_write++) - Q2HELPER(stat_copy_on_write_size+=d->len) uint newMax = computeNewMax( len ); QChar* nd = QT_ALLOC_QCHAR_VEC( newMax ); if ( unicode ) memcpy( nd, unicode, sizeof(QChar)*len ); deref(); d = new QStringData( nd, len, newMax ); } else { d->len = len; @@ -17013,32 +17137,54 @@ QString& QString::setUnicode( const QChar *unicode, uint len ) */ QString& QString::setUnicodeCodes( const ushort* unicode_as_ushorts, uint len ) { return setUnicode((const QChar*)unicode_as_ushorts, len); } /*! + Sets this string to \a str, interpreted as a classic 8-bit ASCII C + string. If \a len is -1 (the default), then it is set to + strlen(str). + + If \a str is 0 a null string is created. If \a str is "", an empty + string is created. + + \sa isNull(), isEmpty() +*/ + +QString &QString::setAscii( const char *str, int len ) +{ +#ifndef QT_NO_TEXTCODEC + if ( QTextCodec::codecForCStrings() ) { + *this = QString::fromAscii( str, len ); + return *this; + } +#endif // QT_NO_TEXTCODEC + return setLatin1( str, len ); +} + +/*! Sets this string to \a str, interpreted as a classic Latin1 C string. If \a len is -1 (the default), then it is set to strlen(str). If \a str is 0 a null string is created. If \a str is "", an empty string is created. \sa isNull(), isEmpty() */ QString &QString::setLatin1( const char *str, int len ) { if ( str == 0 ) return setUnicode(0,0); if ( len < 0 ) - len = qstrlen(str); + len = strlen( str ); if ( len == 0 ) { // won't make a null string *this = QString::fromLatin1( "" ); } else { setUnicode( 0, len ); // resize but not copy QChar *p = d->unicode; while ( len-- ) *p++ = *str++; } @@ -17046,26 +17192,26 @@ QString &QString::setLatin1( const char *str, int len ) } /*! \internal */ void QString::checkSimpleText() const { QChar *p = d->unicode; QChar *end = p + d->len; - d->simpletext = 1; while( p < end ) { ushort uc = p->unicode(); // sort out regions of complex text formatting if ( uc > 0x058f && ( uc < 0x1100 || uc > 0xfb0f ) ) { - d->simpletext = 0; + d->issimpletext = FALSE; return; } p++; } + d->issimpletext = TRUE; } /*! \fn bool QString::simpleText() const \internal */ /*! \internal */ |