From: João Abecasis Date: Tue, 31 Jan 2012 17:31:58 +0000 (+0100) Subject: Remove constructors taking implicit string sizes X-Git-Tag: 071012110112~1235^2^2~294^2^2~34 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=5d593da3d31a0c6adffb449db3ceb65328b87cd6;p=profile%2Fivi%2Fqtbase.git Remove constructors taking implicit string sizes Constructors taking explicit sizes got a default -1 size argument that triggers length calculation from nul-terminated strings. This imposes a slight change in behavior: negative size arguments would previously be ignored and generate an empty string whereas with this patch we expect to see a nul-terminated string. On the other hand, keeping the previous behavior could effectively hide errors in user code and I can't find a good reason to support it. Documentation for the constructors was updated and made more consistent between the classes. Change-Id: I738ac3298cffe3221c8a56e85ba2102623e7b67d Reviewed-by: Lars Knoll --- diff --git a/dist/changes-5.0.0 b/dist/changes-5.0.0 index fc5bebd..e73ecc5 100644 --- a/dist/changes-5.0.0 +++ b/dist/changes-5.0.0 @@ -36,6 +36,13 @@ information about a particular change. - QCoreApplication::translate() will no longer return the source text when the translation is empty. Use lrelease -removeidentical for optimization. +- QString and QByteArray constructors that take a size argument will now treat + negative sizes to indicate nul-terminated strings (a nul-terminated array of + QChar, in the case of QString). In Qt 4, negative sizes were ignored and + result in empty QString and QByteArray, respectively. The size argument to + those constructors now has a default value of -1, thus replacing the separate + constructors that did the same. + - Qt::escape() is deprecated (but can be enabled via QT_DISABLE_DEPRECATED_BEFORE), use QString::toHtmlEscaped() instead. diff --git a/src/corelib/tools/qbytearray.cpp b/src/corelib/tools/qbytearray.cpp index aa22413..90069b1 100644 --- a/src/corelib/tools/qbytearray.cpp +++ b/src/corelib/tools/qbytearray.cpp @@ -1287,38 +1287,16 @@ void QByteArray::chop(int n) \sa isEmpty() */ -/*! \fn QByteArray::QByteArray(const char *str) - - Constructs a byte array initialized with the string \a str. - - QByteArray makes a deep copy of the string data. -*/ - -QByteArray::QByteArray(const char *str) -{ - if (!str) { - d = const_cast(&shared_null.ba); - } else if (!*str) { - d = const_cast(&shared_empty.ba); - } else { - int len = qstrlen(str); - d = static_cast(malloc(sizeof(Data) + len + 1)); - Q_CHECK_PTR(d); - d->ref.initializeOwned(); - d->size = len; - d->alloc = len; - d->capacityReserved = false; - d->offset = 0; - memcpy(d->data(), str, len+1); // include null terminator - } -} - /*! Constructs a byte array containing the first \a size bytes of array \a data. If \a data is 0, a null byte array is constructed. + If \a size is negative, \a data is assumed to point to a nul-terminated + string and its length is determined dynamically. The terminating + nul-character is not considered part of the byte array. + QByteArray makes a deep copy of the string data. \sa fromRawData() @@ -1328,18 +1306,22 @@ QByteArray::QByteArray(const char *data, int size) { if (!data) { d = const_cast(&shared_null.ba); - } else if (size <= 0) { - d = const_cast(&shared_empty.ba); } else { - d = static_cast(malloc(sizeof(Data) + size + 1)); - Q_CHECK_PTR(d); - d->ref.initializeOwned(); - d->size = size; - d->alloc = size; - d->capacityReserved = false; - d->offset = 0; - memcpy(d->data(), data, size); - d->data()[size] = '\0'; + if (size < 0) + size = strlen(data); + if (!size) { + d = const_cast(&shared_empty.ba); + } else { + d = static_cast(malloc(sizeof(Data) + size + 1)); + Q_CHECK_PTR(d); + d->ref.initializeOwned(); + d->size = size; + d->alloc = size; + d->capacityReserved = false; + d->offset = 0; + memcpy(d->data(), data, size); + d->data()[size] = '\0'; + } } } diff --git a/src/corelib/tools/qbytearray.h b/src/corelib/tools/qbytearray.h index 5efeb60..c4feb63 100644 --- a/src/corelib/tools/qbytearray.h +++ b/src/corelib/tools/qbytearray.h @@ -180,8 +180,7 @@ private: public: inline QByteArray(); - QByteArray(const char *); - QByteArray(const char *, int size); + QByteArray(const char *, int size = -1); QByteArray(int size, char c); QByteArray(int size, Qt::Initialization); inline QByteArray(const QByteArray &); diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp index e12cf1f..0f7d398 100644 --- a/src/corelib/tools/qstring.cpp +++ b/src/corelib/tools/qstring.cpp @@ -1022,63 +1022,44 @@ int QString::toUcs4_helper(const ushort *uc, int length, uint *out) Constructs a string initialized with the first \a size characters of the QChar array \a unicode. + If \a unicode is 0, a null string is constructed. + + If \a size is negative, \a unicode is assumed to point to a nul-terminated + array and its length is determined dynamically. The terminating + nul-character is not considered part of the string. + QString makes a deep copy of the string data. The unicode data is copied as is and the Byte Order Mark is preserved if present. + + \sa fromRawData() */ QString::QString(const QChar *unicode, int size) { if (!unicode) { d = const_cast(&shared_null.str); - } else if (size <= 0) { - d = const_cast(&shared_empty.str); } else { - d = (Data*) ::malloc(sizeof(Data)+(size+1)*sizeof(QChar)); - Q_CHECK_PTR(d); - d->ref.initializeOwned(); - d->size = size; - d->alloc = (uint) size; - d->capacityReserved = false; - d->offset = 0; - memcpy(d->data(), unicode, size * sizeof(QChar)); - d->data()[size] = '\0'; + if (size < 0) { + size = 0; + while (unicode[size] != 0) + ++size; + } + if (!size) { + d = const_cast(&shared_empty.str); + } else { + d = (Data*) ::malloc(sizeof(Data)+(size+1)*sizeof(QChar)); + Q_CHECK_PTR(d); + d->ref.initializeOwned(); + d->size = size; + d->alloc = (uint) size; + d->capacityReserved = false; + d->offset = 0; + memcpy(d->data(), unicode, size * sizeof(QChar)); + d->data()[size] = '\0'; + } } } /*! - \since 4.7 - - Constructs a string initialized with the characters of the QChar array - \a unicode, which must be terminated with a 0. - - QString makes a deep copy of the string data. The unicode data is copied as - is and the Byte Order Mark is preserved if present. -*/ -QString::QString(const QChar *unicode) -{ - if (!unicode) { - d = const_cast(&shared_null.str); - } else { - int size = 0; - while (unicode[size] != 0) - ++size; - if (!size) { - d = const_cast(&shared_empty.str); - } else { - d = (Data*) ::malloc(sizeof(Data)+(size+1)*sizeof(QChar)); - Q_CHECK_PTR(d); - d->ref.initializeOwned(); - d->size = size; - d->alloc = (uint) size; - d->capacityReserved = false; - d->offset = 0; - memcpy(d->data(), unicode, size * sizeof(QChar)); - d->data()[size] = '\0'; - } - } -} - - -/*! Constructs a string of the given \a size with every character set to \a ch. diff --git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h index a857d2d..df0ce73 100644 --- a/src/corelib/tools/qstring.h +++ b/src/corelib/tools/qstring.h @@ -158,8 +158,7 @@ public: typedef QStringData Data; inline QString(); - QString(const QChar *unicode, int size); // Qt5: don't cap size < 0 - explicit QString(const QChar *unicode); // Qt5: merge with the above + explicit QString(const QChar *unicode, int size = -1); QString(QChar c); QString(int size, QChar c); inline QString(const QLatin1String &latin1);