diff options
| author | Ahmad Samir <a.samirh78@gmail.com> | 2024-10-25 03:08:38 +0300 |
|---|---|---|
| committer | Ahmad Samir <a.samirh78@gmail.com> | 2025-01-22 19:11:10 +0200 |
| commit | 3b95bfe7c7b918bfececd9734780aa9e2f91ec02 (patch) | |
| tree | 4895601647673f940ce2bfd27a52655cd3c2f545 /src/corelib/text/qstring.cpp | |
| parent | 09fa335fb22122c42f057380e3d6f4d8944256d0 (diff) | |
QString/QByteArray: add nullTerminate{,d}()
As requested in code review.
utf16() guarantees that the string will always be \0-terminated which
could mean allocating behind the scenes. On the other hand
nullTerminate{,d}() makes it obvious that the data may be deep-copied.
For QByteArray, this partially reverts
e0206fe9d47bfbf18a0d4c2e5e780504305f72e7. Adding the same method to
QByteArray was requested in code review for API symmetry.
[ChangeLog][QtCore][QString] Added nullTerminate() and nullTerminated()
methods (like chop() and chopped()), which are useful for strings
constructed with fromRawData() when calling methods that expect
\0-terminated strings.
[ChangeLog][QtCore][QByteArray] Added nullTerminate() and
nullTerminated() methods, which are useful for byte arrays constructed
with fromRawData() when calling methods that expect \0-terminated data.
Change-Id: Iec33b889a9ab62460b7a0df8f9b2189f3f794a54
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/text/qstring.cpp')
| -rw-r--r-- | src/corelib/text/qstring.cpp | 30 |
1 files changed, 28 insertions, 2 deletions
diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index 1d1485d20f6..e225852b0f3 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -7064,6 +7064,31 @@ const ushort *QString::utf16() const } /*! + \fn QString nullTerminate() const + \since 6.9 + + If this string data isn't null-terminated, this method will make a deep + copy of the data and make it null-terminated(). + + A QString is null-terminated by default, however in some cases (e.g. + when using fromRawData()), the string data doesn't necessarily end + with a \c {\0} character, which could be a problem when calling methods + that expect a null-terminated string. + + \sa nullTerminated(), fromRawData(), setRawData() +*/ + +/*! + \fn QString nullTerminated() const + \since 6.9 + + Returns a copy of this string that is always null-terminated. + See nullTerminate(). + + \sa nullTerminated(), fromRawData(), setRawData() +*/ + +/*! Returns a string of size \a width that contains this string padded by the \a fill character. @@ -9375,7 +9400,8 @@ QString::iterator QString::erase(QString::const_iterator first, QString::const_i '\\0'-terminated string (although utf16() does, at the cost of copying the raw data). - \sa fromUtf16(), setRawData() + \sa fromUtf16(), setRawData(), data(), constData(), + nullTerminate(), nullTerminated() */ QString QString::fromRawData(const QChar *unicode, qsizetype size) { @@ -9394,7 +9420,7 @@ QString QString::fromRawData(const QChar *unicode, qsizetype size) This function can be used instead of fromRawData() to re-use existings QString objects to save memory re-allocations. - \sa fromRawData() + \sa fromRawData(), nullTerminate(), nullTerminated() */ QString &QString::setRawData(const QChar *unicode, qsizetype size) { |
