From 615a9cf99150cfee34c14cfed0aa9583f5993934 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Tue, 9 Jun 2020 12:02:57 +0200 Subject: QUuid: port to QAnyStringView MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Remove the QString/QStringView/QLatin1String/const char* overloads from the API, but not the ABI. As a drive-by, replace a use of QStringView::left() by truncate(), as suggested by a comment. [ChangeLog][QtCore][QUuid] The from-string constructor and the fromString() function now take QAnyStringView (was: overload set with a subset of QString, QByteArray, const char*, QLatin1String, QStringView each). Change-Id: If7fa26cfbef9280480c78b669d9f5f14118995ed Reviewed-by: MÃ¥rten Nordheim Reviewed-by: Thiago Macieira --- src/corelib/compat/removed_api.cpp | 25 +++++++++++ src/corelib/plugin/quuid.cpp | 61 +++++++++------------------ src/corelib/plugin/quuid.h | 7 ++- tests/auto/corelib/plugin/quuid/tst_quuid.cpp | 5 +++ 4 files changed, 56 insertions(+), 42 deletions(-) diff --git a/src/corelib/compat/removed_api.cpp b/src/corelib/compat/removed_api.cpp index d5d71ce75a..b2e758d2ca 100644 --- a/src/corelib/compat/removed_api.cpp +++ b/src/corelib/compat/removed_api.cpp @@ -59,6 +59,31 @@ QByteArray QCryptographicHash::hash(const QByteArray &data, Algorithm method) #include "quuid.h" +QUuid::QUuid(const QString &text) + : QUuid{qToAnyStringViewIgnoringNull(text)} +{ +} + +QUuid::QUuid(const char *text) + : QUuid{QAnyStringView(text)} +{ +} + +QUuid::QUuid(const QByteArray &text) + : QUuid{qToAnyStringViewIgnoringNull(text)} +{ +} + +QUuid QUuid::fromString(QStringView string) noexcept +{ + return fromString(QAnyStringView{string}); +} + +QUuid QUuid::fromString(QLatin1String string) noexcept +{ + return fromString(QAnyStringView{string}); +} + QUuid QUuid::fromRfc4122(const QByteArray &bytes) { return fromRfc4122(qToByteArrayViewIgnoringNull(bytes)); diff --git a/src/corelib/plugin/quuid.cpp b/src/corelib/plugin/quuid.cpp index ea78fff5b3..33fac4b892 100644 --- a/src/corelib/plugin/quuid.cpp +++ b/src/corelib/plugin/quuid.cpp @@ -365,6 +365,8 @@ static QUuid createFromName(const QUuid &ns, const QByteArray &baseData, QCrypto */ /*! + \fn QUuid::QUuid(QAnyStringView text) + Creates a QUuid object from the string \a text, which must be formatted as five hex fields separated by '-', e.g., "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}" where each 'x' is a hex @@ -373,14 +375,15 @@ static QUuid createFromName(const QUuid &ns, const QByteArray &baseData, QCrypto toString() for an explanation of how the five hex fields map to the public data members in QUuid. + \note In Qt versions prior to 6.3, this constructor was an overload + set consisting of QString, QByteArray and \c{const char*} + instead of one constructor taking QAnyStringView. + \sa toString(), QUuid() */ -QUuid::QUuid(const QString &text) - : QUuid(fromString(text)) -{ -} /*! + \fn static QUuid::fromString(QAnyStringView text) \since 5.10 Creates a QUuid object from the string \a text, which must be @@ -391,12 +394,16 @@ QUuid::QUuid(const QString &text) toString() for an explanation of how the five hex fields map to the public data members in QUuid. + \note In Qt versions prior to 6.3, this function was an overload + set consisting of QStringView and QLatin1String instead of + one function taking QAnyStringView. + \sa toString(), QUuid() */ -QUuid QUuid::fromString(QStringView text) noexcept +static QUuid uuidFromString(QStringView text) noexcept { if (text.size() > MaxStringUuidLength) - text = text.left(MaxStringUuidLength); // text.truncate(MaxStringUuidLength); + text.truncate(MaxStringUuidLength); char latin1[MaxStringUuidLength + 1]; char *dst = latin1; @@ -409,21 +416,7 @@ QUuid QUuid::fromString(QStringView text) noexcept return _q_uuidFromHex(latin1); } -/*! - \since 5.10 - \overload - - Creates a QUuid object from the string \a text, which must be - formatted as five hex fields separated by '-', e.g., - "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}" where each 'x' is a hex - digit. The curly braces shown here are optional, but it is normal to - include them. If the conversion fails, a null UUID is returned. See - toString() for an explanation of how the five hex fields map to the - public data members in QUuid. - - \sa toString(), QUuid() -*/ -QUuid QUuid::fromString(QLatin1String text) noexcept +static QUuid uuidFromString(QLatin1String text) noexcept { if (Q_UNLIKELY(text.size() < MaxStringUuidLength - 2 || (text.front() == QLatin1Char('{') && text.size() < MaxStringUuidLength - 1))) { @@ -434,30 +427,16 @@ QUuid QUuid::fromString(QLatin1String text) noexcept return _q_uuidFromHex(text.data()); } -/*! - \internal -*/ -QUuid::QUuid(const char *text) - : QUuid(_q_uuidFromHex(text)) +Q_ALWAYS_INLINE +// can treat UTF-8 the same as Latin-1: +static QUuid uuidFromString(QUtf8StringView text) noexcept { + return uuidFromString(QLatin1String(text.data(), text.size())); } -/*! - Creates a QUuid object from the QByteArray \a text, which must be - formatted as five hex fields separated by '-', e.g., - "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}" where each 'x' is a hex - digit. The curly braces shown here are optional, but it is normal to - include them. If the conversion fails, a null UUID is created. See - toByteArray() for an explanation of how the five hex fields map to the - public data members in QUuid. - - \since 4.8 - - \sa toByteArray(), QUuid() -*/ -QUuid::QUuid(const QByteArray &text) - : QUuid(fromString(QLatin1String(text.data(), text.size()))) +QUuid QUuid::fromString(QAnyStringView text) noexcept { + return text.visit([] (auto text) { return uuidFromString(text); }); } /*! diff --git a/src/corelib/plugin/quuid.h b/src/corelib/plugin/quuid.h index 9192542684..9229d5b694 100644 --- a/src/corelib/plugin/quuid.h +++ b/src/corelib/plugin/quuid.h @@ -97,12 +97,17 @@ public: uchar b4, uchar b5, uchar b6, uchar b7, uchar b8) noexcept : data1(l), data2(w1), data3(w2), data4{b1, b2, b3, b4, b5, b6, b7, b8} {} + explicit QUuid(QAnyStringView string) noexcept + : QUuid{fromString(string)} {} + static QUuid fromString(QAnyStringView string) noexcept; +#if QT_REMOVED_SINCE(6, 3) explicit QUuid(const QString &); static QUuid fromString(QStringView string) noexcept; static QUuid fromString(QLatin1String string) noexcept; explicit QUuid(const char *); - QString toString(StringFormat mode = WithBraces) const; explicit QUuid(const QByteArray &); +#endif + QString toString(StringFormat mode = WithBraces) const; QByteArray toByteArray(StringFormat mode = WithBraces) const; QByteArray toRfc4122() const; #if QT_REMOVED_SINCE(6, 3) diff --git a/tests/auto/corelib/plugin/quuid/tst_quuid.cpp b/tests/auto/corelib/plugin/quuid/tst_quuid.cpp index 8240f2452a..1ff273e665 100644 --- a/tests/auto/corelib/plugin/quuid/tst_quuid.cpp +++ b/tests/auto/corelib/plugin/quuid/tst_quuid.cpp @@ -193,6 +193,11 @@ void tst_QUuid::fromString() const auto longerInputL1 = inputL1 + '5'; // the '5' makes the premature end check incorrectly succeed const auto inputL1S = QLatin1String(longerInputL1.data(), inputL1.size()); QCOMPARE(expected, QUuid::fromString(inputL1S)); + + // for QUtf8StringView, too: + const auto longerInputU8 = inputU8 + '5'; // the '5' makes the premature end check incorrectly succeed + const auto inputU8S = QUtf8StringView(longerInputU8.data(), inputU8.size()); + QCOMPARE(expected, QUuid::fromString(inputU8S)); } void tst_QUuid::toByteArray() -- cgit v1.2.3