diff options
author | João Abecasis <joao.abecasis@nokia.com> | 2012-01-31 18:31:58 +0100 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-02-14 15:25:37 +0100 |
commit | 5d593da3d31a0c6adffb449db3ceb65328b87cd6 (patch) | |
tree | 9ceedd7af91e7fcdf60d91e9d3af8f0ad8fa74fd /src/corelib/tools/qbytearray.cpp | |
parent | 75286739de854d761bbfcababa50d1bc6dfda12a (diff) |
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>
Diffstat (limited to 'src/corelib/tools/qbytearray.cpp')
-rw-r--r-- | src/corelib/tools/qbytearray.cpp | 56 |
1 files changed, 19 insertions, 37 deletions
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'; + } } } |