QUuid: port to QAnyStringView

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 <marten.nordheim@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Marc Mutz 2020-06-09 12:02:57 +02:00
parent 09fba5cf36
commit 615a9cf991
4 changed files with 56 additions and 42 deletions

View File

@ -59,6 +59,31 @@ QByteArray QCryptographicHash::hash(const QByteArray &data, Algorithm method)
#include "quuid.h" #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) QUuid QUuid::fromRfc4122(const QByteArray &bytes)
{ {
return fromRfc4122(qToByteArrayViewIgnoringNull(bytes)); return fromRfc4122(qToByteArrayViewIgnoringNull(bytes));

View File

@ -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 Creates a QUuid object from the string \a text, which must be
formatted as five hex fields separated by '-', e.g., formatted as five hex fields separated by '-', e.g.,
"{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}" where each 'x' is a hex "{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 toString() for an explanation of how the five hex fields map to the
public data members in QUuid. 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() \sa toString(), QUuid()
*/ */
QUuid::QUuid(const QString &text)
: QUuid(fromString(text))
{
}
/*! /*!
\fn static QUuid::fromString(QAnyStringView text)
\since 5.10 \since 5.10
Creates a QUuid object from the string \a text, which must be 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 toString() for an explanation of how the five hex fields map to the
public data members in QUuid. 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() \sa toString(), QUuid()
*/ */
QUuid QUuid::fromString(QStringView text) noexcept static QUuid uuidFromString(QStringView text) noexcept
{ {
if (text.size() > MaxStringUuidLength) if (text.size() > MaxStringUuidLength)
text = text.left(MaxStringUuidLength); // text.truncate(MaxStringUuidLength); text.truncate(MaxStringUuidLength);
char latin1[MaxStringUuidLength + 1]; char latin1[MaxStringUuidLength + 1];
char *dst = latin1; char *dst = latin1;
@ -409,21 +416,7 @@ QUuid QUuid::fromString(QStringView text) noexcept
return _q_uuidFromHex(latin1); return _q_uuidFromHex(latin1);
} }
/*! static QUuid uuidFromString(QLatin1String text) noexcept
\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
{ {
if (Q_UNLIKELY(text.size() < MaxStringUuidLength - 2 if (Q_UNLIKELY(text.size() < MaxStringUuidLength - 2
|| (text.front() == QLatin1Char('{') && text.size() < MaxStringUuidLength - 1))) { || (text.front() == QLatin1Char('{') && text.size() < MaxStringUuidLength - 1))) {
@ -434,30 +427,16 @@ QUuid QUuid::fromString(QLatin1String text) noexcept
return _q_uuidFromHex(text.data()); return _q_uuidFromHex(text.data());
} }
/*! Q_ALWAYS_INLINE
\internal // can treat UTF-8 the same as Latin-1:
*/ static QUuid uuidFromString(QUtf8StringView text) noexcept
QUuid::QUuid(const char *text)
: QUuid(_q_uuidFromHex(text))
{ {
return uuidFromString(QLatin1String(text.data(), text.size()));
} }
/*! QUuid QUuid::fromString(QAnyStringView text) noexcept
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())))
{ {
return text.visit([] (auto text) { return uuidFromString(text); });
} }
/*! /*!

View File

@ -97,12 +97,17 @@ public:
uchar b4, uchar b5, uchar b6, uchar b7, uchar b8) noexcept 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} {} : 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 &); explicit QUuid(const QString &);
static QUuid fromString(QStringView string) noexcept; static QUuid fromString(QStringView string) noexcept;
static QUuid fromString(QLatin1String string) noexcept; static QUuid fromString(QLatin1String string) noexcept;
explicit QUuid(const char *); explicit QUuid(const char *);
QString toString(StringFormat mode = WithBraces) const;
explicit QUuid(const QByteArray &); explicit QUuid(const QByteArray &);
#endif
QString toString(StringFormat mode = WithBraces) const;
QByteArray toByteArray(StringFormat mode = WithBraces) const; QByteArray toByteArray(StringFormat mode = WithBraces) const;
QByteArray toRfc4122() const; QByteArray toRfc4122() const;
#if QT_REMOVED_SINCE(6, 3) #if QT_REMOVED_SINCE(6, 3)

View File

@ -193,6 +193,11 @@ void tst_QUuid::fromString()
const auto longerInputL1 = inputL1 + '5'; // the '5' makes the premature end check incorrectly succeed const auto longerInputL1 = inputL1 + '5'; // the '5' makes the premature end check incorrectly succeed
const auto inputL1S = QLatin1String(longerInputL1.data(), inputL1.size()); const auto inputL1S = QLatin1String(longerInputL1.data(), inputL1.size());
QCOMPARE(expected, QUuid::fromString(inputL1S)); 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() void tst_QUuid::toByteArray()