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 <lars.knoll@nokia.com>
This commit is contained in:
João Abecasis 2012-01-31 18:31:58 +01:00 committed by Qt by Nokia
parent 75286739de
commit 5d593da3d3
5 changed files with 54 additions and 86 deletions

7
dist/changes-5.0.0 vendored
View File

@ -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.

View File

@ -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<Data *>(&shared_null.ba);
} else if (!*str) {
d = const_cast<Data *>(&shared_empty.ba);
} else {
int len = qstrlen(str);
d = static_cast<Data *>(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<Data *>(&shared_null.ba);
} else if (size <= 0) {
d = const_cast<Data *>(&shared_empty.ba);
} else {
d = static_cast<Data *>(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<Data *>(&shared_empty.ba);
} else {
d = static_cast<Data *>(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';
}
}
}

View File

@ -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 &);

View File

@ -1022,62 +1022,43 @@ 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<Data *>(&shared_null.str);
} else if (size <= 0) {
d = const_cast<Data *>(&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<Data *>(&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<Data *>(&shared_null.str);
} else {
int size = 0;
while (unicode[size] != 0)
++size;
if (!size) {
d = const_cast<Data *>(&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.

View File

@ -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);