From 6e8a74712fd28b466ea99bf205c9447cb53c0d2b Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Thu, 15 Apr 2021 14:31:46 +0200 Subject: QString: add missing char8_t* constructor / fromUtf8 overloads Currently we support: QString s = QString::fromUtf(u8"foo", 3); But we don't support QString s = QString::fromUtf8(u8"foo"); QString s(u8"foo"); There's no reason not to have these two functions. Guess what, we've actually got code _in Qt_ that tries to build a QString out of a char8_t; that code stops compiling under C++20 (which is supported, but not CI-tested at the moment, it seems). Re-add the missing constructor and fromUtf8 overloads. [ChangeLog][QtCore][QString] Added a constructor and a fromUtf8() overload taking a `const char8_t *` argument. Task-number: QTQAINFRA-4117 Task-number: QTQAINFRA-4242 Change-Id: I1f0ae658b3490b9e092941cabcc7fb8fc4c51aa3 Reviewed-by: Lars Knoll Reviewed-by: Volker Hilsheimer Reviewed-by: Edward Welbourne (cherry picked from commit b322bfcc14845a4b6a6eef85ef359b1e4591a5ca) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/text/qstring.cpp | 18 ++++++++++++++++++ src/corelib/text/qstring.h | 9 +++++++++ 2 files changed, 27 insertions(+) (limited to 'src/corelib') diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index fcdf687b99..745b963260 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -2190,6 +2190,16 @@ inline char qToLower(char ch) \sa fromLatin1(), fromLocal8Bit(), fromUtf8() */ +/*! \fn QString::QString(const char8_t *str) + + Constructs a string initialized with the UTF-8 string \a str. The + given const char8_t pointer is converted to Unicode using the + fromUtf8() function. + + \since 6.1 + \sa fromLatin1(), fromLocal8Bit(), fromUtf8() +*/ + /*! \fn QString QString::fromStdString(const std::string &str) Returns a copy of the \a str string. The given string is converted @@ -5326,6 +5336,14 @@ QString QString::fromLocal8Bit(QByteArrayView ba) \sa toUtf8(), fromLatin1(), fromLocal8Bit() */ +/*! + \fn QString QString::fromUtf8(const char8_t *str) + \overload + \since 6.1 + + This overload is only available when compiling in C++20 mode. +*/ + /*! \fn QString QString::fromUtf8(const char8_t *str, qsizetype size) \overload diff --git a/src/corelib/text/qstring.h b/src/corelib/text/qstring.h index 8926f6f3af..153a9e0475 100644 --- a/src/corelib/text/qstring.h +++ b/src/corelib/text/qstring.h @@ -386,6 +386,12 @@ public: QString(QChar c); QString(qsizetype size, QChar c); inline QString(QLatin1String latin1); +#if defined(__cpp_char8_t) || defined(Q_CLANG_QDOC) + Q_WEAK_OVERLOAD + inline QString(const char8_t *str) + : QString(fromUtf8(str)) + {} +#endif inline QString(const QString &) noexcept; inline ~QString(); QString &operator=(QChar c); @@ -746,6 +752,9 @@ public: return fromUtf8(QByteArrayView(utf8, !utf8 || size < 0 ? qstrlen(utf8) : size)); } #if defined(__cpp_char8_t) || defined(Q_CLANG_QDOC) + Q_WEAK_OVERLOAD + static inline QString fromUtf8(const char8_t *str) + { return fromUtf8(reinterpret_cast(str)); } Q_WEAK_OVERLOAD static inline QString fromUtf8(const char8_t *str, qsizetype size) { return fromUtf8(reinterpret_cast(str), size); } -- cgit v1.2.3