From fce7a45268afde8a8ffcc6d79d068f3536a4c999 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Mon, 10 Jun 2024 21:30:29 -0700 Subject: [PATCH] Strings: get rid of the last std::char_traits use MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit LLVM's libc++ became pedantic and insists on only accepting instantiations for one of the standard Char types, so remove all uses of it with a template parameter. Complements dc2ae08e02730ab795445bc047221aa56914f723. Pick-to: 6.7 Fixes: QTBUG-126214 Task-number: QTBUG-122753 Change-Id: I8c5c4dba62924541bfb0fffd17d7d8ddc78338bb Reviewed-by: MÃ¥rten Nordheim Reviewed-by: Marc Mutz (cherry picked from commit ab05e05f60253b9be615c09aa340ee75f2e5bcaf) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/text/qanystringview.h | 2 +- src/corelib/text/qbytearrayview.h | 10 +++++++++- src/corelib/text/qstringview.h | 2 +- src/corelib/text/qutf8stringview.h | 9 +++------ 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/corelib/text/qanystringview.h b/src/corelib/text/qanystringview.h index 843fa7e5c9f..075d64679c1 100644 --- a/src/corelib/text/qanystringview.h +++ b/src/corelib/text/qanystringview.h @@ -137,7 +137,7 @@ private: static constexpr qsizetype lengthHelperPointer(const Char *str) noexcept { if (q20::is_constant_evaluated()) - return qsizetype(std::char_traits::length(str)); + return QtPrivate::lengthHelperPointer(str); if constexpr (sizeof(Char) == sizeof(char16_t)) return QtPrivate::qustrlen(reinterpret_cast(str)); else diff --git a/src/corelib/text/qbytearrayview.h b/src/corelib/text/qbytearrayview.h index 358c9e62ef1..dea0e6cbe8d 100644 --- a/src/corelib/text/qbytearrayview.h +++ b/src/corelib/text/qbytearrayview.h @@ -68,7 +68,15 @@ struct IsContainerCompatibleWithQByteArrayView static constexpr qsizetype lengthHelperPointer(const Char *data) noexcept { - return qsizetype(std::char_traits::length(data)); + // std::char_traits can only be used with one of the regular char types + // (char, char16_t, wchar_t, but not uchar or QChar), so we roll the loop + // out by ourselves. + qsizetype i = 0; + if (!data) + return i; + while (data[i] != Char(0)) + ++i; + return i; } } // namespace QtPrivate diff --git a/src/corelib/text/qstringview.h b/src/corelib/text/qstringview.h index ab97d834d38..2ad245caa38 100644 --- a/src/corelib/text/qstringview.h +++ b/src/corelib/text/qstringview.h @@ -108,7 +108,7 @@ private: static constexpr qsizetype lengthHelperPointer(const Char *str) noexcept { if (q20::is_constant_evaluated()) - return std::char_traits::length(str); + return QtPrivate::lengthHelperPointer(str); return QtPrivate::qustrlen(reinterpret_cast(str)); } static qsizetype lengthHelperPointer(const QChar *str) noexcept diff --git a/src/corelib/text/qutf8stringview.h b/src/corelib/text/qutf8stringview.h index cc9ce504554..7ae06c0ec40 100644 --- a/src/corelib/text/qutf8stringview.h +++ b/src/corelib/text/qutf8stringview.h @@ -134,14 +134,12 @@ private: return qsizetype(std::size(c)); } - // Note: Do not replace with std::size(const Char (&)[N]), cause the result + // Note: Do not replace with std::size(const Char (&)[N]), because the result // will be of by one. template static constexpr qsizetype lengthHelperContainer(const Char (&str)[N]) noexcept { - const auto it = std::char_traits::find(str, N, Char(0)); - const auto end = it ? it : std::next(str, N); - return qsizetype(std::distance(str, end)); + return QtPrivate::lengthHelperContainer(str); } template @@ -174,8 +172,7 @@ public: #else template = true> constexpr QBasicUtf8StringView(const Pointer &str) noexcept - : QBasicUtf8StringView(str, - str ? std::char_traits>>::length(str) : 0) {} + : QBasicUtf8StringView(str, QtPrivate::lengthHelperPointer(str)) {} #endif #ifdef Q_QDOC