summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/corelib/doc/snippets/code/src_corelib_text_qbytearray.cpp14
-rw-r--r--src/corelib/text/qbytearray.cpp8
2 files changed, 20 insertions, 2 deletions
diff --git a/src/corelib/doc/snippets/code/src_corelib_text_qbytearray.cpp b/src/corelib/doc/snippets/code/src_corelib_text_qbytearray.cpp
index 74f14f2a4b..196c992c8e 100644
--- a/src/corelib/doc/snippets/code/src_corelib_text_qbytearray.cpp
+++ b/src/corelib/doc/snippets/code/src_corelib_text_qbytearray.cpp
@@ -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]
}
-
-
diff --git a/src/corelib/text/qbytearray.cpp b/src/corelib/text/qbytearray.cpp
index aee22edc6f..b5327280f9 100644
--- a/src/corelib/text/qbytearray.cpp
+++ b/src/corelib/text/qbytearray.cpp
@@ -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