summaryrefslogtreecommitdiff
path: root/qmake/tools/qstring.cpp
Side-by-side diff
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
@@ -2,13 +2,13 @@
** $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.
@@ -44,24 +44,28 @@
#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
*
@@ -11784,24 +11788,26 @@ 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[] = {
@@ -11897,13 +11903,13 @@ static int ucstrnicmp( const QChar *a, const QChar *b, int l )
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;
}
/*!
@@ -12888,13 +12894,13 @@ void QString::compose()
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;
@@ -12907,13 +12913,13 @@ QChar* QString::asciiToUnicode( const QByteArray& ba, uint* 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;
@@ -12937,47 +12943,47 @@ static QChar* internalAsciiToUnicode( const QByteArray& ba, uint* len )
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++;
@@ -12991,13 +12997,13 @@ 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;
@@ -13154,41 +13160,12 @@ QStringData* QString::makeSharedNull()
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.
@@ -13208,13 +13185,12 @@ 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
@@ -13227,34 +13203,37 @@ QString::QString( const QString &s ) :
\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.
@@ -13299,18 +13278,45 @@ QString::QString( const QChar* unicode, uint length )
\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.
@@ -13330,16 +13336,16 @@ 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;
@@ -13349,12 +13355,21 @@ void QStringData::deleteSelf()
\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.
*/
@@ -13365,13 +13380,12 @@ void QStringData::deleteSelf()
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;
}
@@ -13380,13 +13394,13 @@ QString &QString::operator=( const QString &s )
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
@@ -13396,13 +13410,13 @@ QString &QString::operator=( const QCString& cs )
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
@@ -13468,14 +13482,13 @@ void QString::truncate( uint 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.
@@ -13483,18 +13496,18 @@ void QString::truncate( uint newLen )
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()
@@ -13502,14 +13515,12 @@ void QString::truncate( uint newLen )
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 );
@@ -13527,27 +13538,26 @@ void QString::setLength( uint newLen )
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.
*/
@@ -13771,13 +13781,13 @@ QString &QString::sprintf( const char* 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;
@@ -13910,13 +13920,13 @@ QString &QString::sprintf( const char* cformat, ... )
break;
case 2:
::sprintf( out, in, width, decimals, value );
}
}
}
- replacement = fromLatin1( out );
+ replacement = fromAscii( out );
}
result += replacement;
}
*this = result;
va_end( ap );
@@ -14121,14 +14131,16 @@ int QString::find( const QString& str, int index, bool cs ) const
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];
@@ -14218,12 +14230,15 @@ int QString::find( const QString& str, int index, bool cs ) const
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();
@@ -14234,12 +14249,13 @@ int QString::findRev( QChar c, int index, bool cs ) const
} 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
@@ -14270,14 +14286,16 @@ int QString::findRev( const QString& str, int index, bool cs ) const
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;
@@ -14976,25 +14994,30 @@ QString QString::rightJustify( uint width, QChar fill, bool truncate ) const
\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
@@ -15004,25 +15027,30 @@ QString QString::lower() const
\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.
@@ -15134,14 +15162,14 @@ QString &QString::insert( uint index, const QString &s )
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;
@@ -15248,12 +15276,22 @@ QString &QString::insert( uint index, QChar c ) // insert char
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).
@@ -15845,15 +15883,15 @@ static bool ok_in_base( QChar c, int base )
\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++;
@@ -15880,27 +15918,25 @@ long QString::toLong( bool *ok, int base ) const
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.
@@ -15912,13 +15948,13 @@ bye:
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 )
@@ -15937,13 +15973,13 @@ ulong QString::toULong( bool *ok, int base ) const
} 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
@@ -15964,13 +16000,13 @@ bye:
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;
}
@@ -15983,13 +16019,13 @@ short QString::toShort( bool *ok, int base ) const
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;
}
@@ -16010,13 +16046,18 @@ ushort QString::toUShort( bool *ok, int base ) const
\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.
@@ -16025,13 +16066,18 @@ int QString::toInt( bool *ok, int base ) const
\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
@@ -16049,13 +16095,13 @@ 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.
@@ -16095,17 +16141,17 @@ QString &QString::setNum( long n, int base )
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;
@@ -16458,12 +16504,21 @@ void QString::setExpand( uint index, QChar c )
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+=().
@@ -16487,15 +16542,21 @@ QString& QString::operator+=( const QString &str )
/*!
\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 ) {
@@ -16505,12 +16566,13 @@ QString& QString::operator+=( const char *str )
} 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.
*/
@@ -16526,71 +16588,100 @@ QString &QString::operator+=( QChar c )
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);
@@ -16630,13 +16721,14 @@ QCString QString::utf8() const
*/
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++) {
@@ -16671,34 +16763,64 @@ 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
@@ -16706,22 +16828,21 @@ QString QString::fromLatin1( const char* chars, int len )
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
@@ -16737,13 +16858,13 @@ QCString QString::local8Bit() const
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
@@ -16768,13 +16889,14 @@ QString QString::fromLocal8Bit( const char* local8Bit, int 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);
@@ -16801,12 +16923,18 @@ QString QString::fromLocal8Bit( const char* local8Bit, int len )
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
@@ -16816,14 +16944,12 @@ 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();
@@ -16980,14 +17106,12 @@ QString& QString::setUnicode( const QChar *unicode, uint len )
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 );
@@ -17015,12 +17139,34 @@ 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.
@@ -17030,13 +17176,13 @@ QString& QString::setUnicodeCodes( const ushort* unicode_as_ushorts, uint len )
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-- )
@@ -17048,22 +17194,22 @@ 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
*/