From fe46cd59ce3c961d714b303d7d5484cca8864247 Mon Sep 17 00:00:00 2001 From: Ievgenii Meshcheriakov Date: Thu, 26 Aug 2021 13:04:58 +0200 Subject: Add isValidUtf8() methods to QUtf8StringView and QByteArray{,View} The new methods return true if the string contains valid UTF-8 encoded data, or false otherwise. [ChangeLog][QtCore][QByteArray] Added isValidUtf8() method. [ChangeLog][QtCore][QByteArrayView] Added isValidUtf8() method. [ChangeLog][QtCore][QUtf8StringView] Added isValidUtf8() method. Task-number: QTBUG-92021 Change-Id: I5d0cb613265d98b1f189c5f5cc09c1f7db302272 Reviewed-by: Edward Welbourne --- src/corelib/text/qbytearray.cpp | 18 ++++++++++++++++++ src/corelib/text/qbytearray.h | 5 +++++ src/corelib/text/qbytearrayalgorithms.h | 2 ++ src/corelib/text/qbytearrayview.h | 2 ++ src/corelib/text/qbytearrayview.qdoc | 9 +++++++++ src/corelib/text/qutf8stringview.h | 5 +++++ src/corelib/text/qutf8stringview.qdoc | 9 +++++++++ 7 files changed, 50 insertions(+) (limited to 'src/corelib/text') diff --git a/src/corelib/text/qbytearray.cpp b/src/corelib/text/qbytearray.cpp index a1b0f30d01..8a7db6ebe4 100644 --- a/src/corelib/text/qbytearray.cpp +++ b/src/corelib/text/qbytearray.cpp @@ -51,6 +51,7 @@ #include "qstringalgorithms_p.h" #include "qscopedpointer.h" #include "qbytearray_p.h" +#include "qstringconverter_p.h" #include #include @@ -426,6 +427,14 @@ int QtPrivate::compareMemory(QByteArrayView lhs, QByteArrayView rhs) return lhs.size() == rhs.size() ? 0 : lhs.size() > rhs.size() ? 1 : -1; } +/*! + \internal +*/ +bool QtPrivate::isValidUtf8(QByteArrayView s) noexcept +{ + return QUtf8::isValidUtf8(s).isValidUtf8; +} + // the CRC table below is created by the following piece of code #if 0 static void createCRC16Table() // build CRC16 lookup table @@ -2783,6 +2792,15 @@ bool QByteArray::isLower() const return true; } +/*! + \fn QByteArray::isValidUtf8() const + + Returns \c true if this byte array contains valid UTF-8 encoded data, + or \c false otherwise. + + \since 6.3 +*/ + /*! Returns a byte array that contains the first \a len bytes of this byte array. diff --git a/src/corelib/text/qbytearray.h b/src/corelib/text/qbytearray.h index 4f29018f32..0a604f1594 100644 --- a/src/corelib/text/qbytearray.h +++ b/src/corelib/text/qbytearray.h @@ -200,6 +200,11 @@ public: bool isUpper() const; bool isLower() const; + [[nodiscard]] bool isValidUtf8() const noexcept + { + return QtPrivate::isValidUtf8(qToByteArrayViewIgnoringNull(*this)); + } + void truncate(qsizetype pos); void chop(qsizetype n); diff --git a/src/corelib/text/qbytearrayalgorithms.h b/src/corelib/text/qbytearrayalgorithms.h index a78e6e1709..b669f065b9 100644 --- a/src/corelib/text/qbytearrayalgorithms.h +++ b/src/corelib/text/qbytearrayalgorithms.h @@ -74,6 +74,8 @@ qsizetype count(QByteArrayView haystack, QByteArrayView needle) noexcept; [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION QByteArrayView trimmed(QByteArrayView s) noexcept; +[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isValidUtf8(QByteArrayView s) noexcept; + } // namespace QtPrivate /***************************************************************************** diff --git a/src/corelib/text/qbytearrayview.h b/src/corelib/text/qbytearrayview.h index 1cd5b0333e..11db03c62f 100644 --- a/src/corelib/text/qbytearrayview.h +++ b/src/corelib/text/qbytearrayview.h @@ -288,6 +288,8 @@ public: inline int compare(QByteArrayView a, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; + [[nodiscard]] inline bool isValidUtf8() const noexcept { return QtPrivate::isValidUtf8(*this); } + // // STL compatibility API: // diff --git a/src/corelib/text/qbytearrayview.qdoc b/src/corelib/text/qbytearrayview.qdoc index 624b056744..96bb1ccb56 100644 --- a/src/corelib/text/qbytearrayview.qdoc +++ b/src/corelib/text/qbytearrayview.qdoc @@ -376,6 +376,15 @@ \sa operator==() */ +/*! + \fn QByteArrayView::isValidUtf8() const + + Returns \c true if this byte array view contains valid UTF-8 encoded data, + or \c false otherwise. + + \since 6.3 +*/ + /*! \fn QByteArrayView::const_iterator QByteArrayView::begin() const diff --git a/src/corelib/text/qutf8stringview.h b/src/corelib/text/qutf8stringview.h index eeab604fa8..a6930c2e0f 100644 --- a/src/corelib/text/qutf8stringview.h +++ b/src/corelib/text/qutf8stringview.h @@ -285,6 +285,11 @@ public: constexpr void chop(qsizetype n) { verify(n); m_size -= n; } + [[nodiscard]] inline bool isValidUtf8() const noexcept + { + return QByteArrayView(reinterpret_cast(data()), size()).isValidUtf8(); + } + // // STL compatibility API: // diff --git a/src/corelib/text/qutf8stringview.qdoc b/src/corelib/text/qutf8stringview.qdoc index 683af4e423..deac0882fe 100644 --- a/src/corelib/text/qutf8stringview.qdoc +++ b/src/corelib/text/qutf8stringview.qdoc @@ -678,6 +678,15 @@ \sa sliced(), first(), last(), chopped(), truncate() */ +/*! + \fn QUtf8StringView::isValidUtf8() const + + Returns \c true if this string contains valid UTF-8 encoded data, + or \c false otherwise. + + \since 6.3 +*/ + /*! \fn template qToUtf8StringViewIgnoringNull(const QStringLike &s); \relates QUtf8StringView -- cgit v1.2.3