Document the changed semantics when passing arrays to QByteArray methods

After adding QByteArrayView overloads of QByteArray methods that used
to take char*, the way of determining the length of passed fixed-sized
array data has changed, due to the QByteArraiyView's optimization to deduce
the length at compile time for arrays. Document the behavior and add a
changelog item.

[ChangeLog][QtCore][QByteArray] When passing fixed size C arrays to
QByteArray methods, the length of the data on which the method will operate
is determined by array size, and not by scanning for the first '\0'
terminator, as it was in Qt 5.

Task-number: QTBUG-85815
Change-Id: I11cba17d428791e06c9bd4c7a727b7bd6b6aec3c
Reviewed-by: Paul Wicking <paul.wicking@qt.io>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
This commit is contained in:
Sona Kurazyan 2020-07-31 11:51:23 +02:00
parent 883f2dd81f
commit 8003ae79bf
2 changed files with 20 additions and 2 deletions

View File

@ -504,6 +504,16 @@ qDebug(ba.constData());
QByteArray ba = QByteArrayLiteral("byte array contents");
//! [53]
//! [54]
QByteArray ba("abc");
const char big[10] = "abc";
ba.compare(big); // returns -1, big is longer than ba
// In contrast:
const char arr[] = "abc";
ba.compare(arr); // returns 0, both have the same size and data
const char *str = "abc";
ba.compare(str); // returns 0, the size is determined by scanning for '\0'
//! [54]
}

View File

@ -847,6 +847,14 @@ QByteArray qUncompress(const uchar* data, int nbytes)
such a pointer, without a length, will interpret it as this sequence of
bytes. Such a sequence, by construction, cannot contain a '\\0' byte.
Take care when passing fixed size C arrays to QByteArray methods that accept
a QByteArrayView: the length of the data on which the method will operate is
determined by array size. A \c{char [N]} array will be handled as a view of
size \c{N-1}, on the expectation that the array is a string literal with a '\\0'
at index \c{N-1}. For example:
\snippet code/src_corelib_text_qbytearray.cpp 54
Other overloads accept a start-pointer and a byte-count; these use the given
number of bytes, following the start address, regardless of whether any of
them happen to be '\\0' bytes. In some cases, where there is no overload