summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--dist/changes-5.0.07
-rw-r--r--src/corelib/tools/qbytearray.cpp56
-rw-r--r--src/corelib/tools/qbytearray.h3
-rw-r--r--src/corelib/tools/qstring.cpp71
-rw-r--r--src/corelib/tools/qstring.h3
5 files changed, 54 insertions, 86 deletions
diff --git a/dist/changes-5.0.0 b/dist/changes-5.0.0
index fc5bebd11d..e73ecc50ee 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 aa22413d94..90069b112d 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<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';
+ }
}
}
diff --git a/src/corelib/tools/qbytearray.h b/src/corelib/tools/qbytearray.h
index 5efeb6025e..c4feb63814 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 e12cf1f910..0f7d398972 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<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.
diff --git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h
index a857d2d4e5..df0ce73b3a 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);