Strings: get rid of the last std::char_traits use

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 <marten.nordheim@qt.io>
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
(cherry picked from commit ab05e05f60253b9be615c09aa340ee75f2e5bcaf)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Thiago Macieira 2024-06-10 21:30:29 -07:00 committed by Qt Cherry-pick Bot
parent 28b9a653ea
commit fce7a45268
4 changed files with 14 additions and 9 deletions

View File

@ -137,7 +137,7 @@ private:
static constexpr qsizetype lengthHelperPointer(const Char *str) noexcept static constexpr qsizetype lengthHelperPointer(const Char *str) noexcept
{ {
if (q20::is_constant_evaluated()) if (q20::is_constant_evaluated())
return qsizetype(std::char_traits<Char>::length(str)); return QtPrivate::lengthHelperPointer(str);
if constexpr (sizeof(Char) == sizeof(char16_t)) if constexpr (sizeof(Char) == sizeof(char16_t))
return QtPrivate::qustrlen(reinterpret_cast<const char16_t*>(str)); return QtPrivate::qustrlen(reinterpret_cast<const char16_t*>(str));
else else

View File

@ -68,7 +68,15 @@ struct IsContainerCompatibleWithQByteArrayView<T, std::enable_if_t<
template <typename Char> template <typename Char>
static constexpr qsizetype lengthHelperPointer(const Char *data) noexcept static constexpr qsizetype lengthHelperPointer(const Char *data) noexcept
{ {
return qsizetype(std::char_traits<Char>::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 } // namespace QtPrivate

View File

@ -108,7 +108,7 @@ private:
static constexpr qsizetype lengthHelperPointer(const Char *str) noexcept static constexpr qsizetype lengthHelperPointer(const Char *str) noexcept
{ {
if (q20::is_constant_evaluated()) if (q20::is_constant_evaluated())
return std::char_traits<Char>::length(str); return QtPrivate::lengthHelperPointer(str);
return QtPrivate::qustrlen(reinterpret_cast<const char16_t *>(str)); return QtPrivate::qustrlen(reinterpret_cast<const char16_t *>(str));
} }
static qsizetype lengthHelperPointer(const QChar *str) noexcept static qsizetype lengthHelperPointer(const QChar *str) noexcept

View File

@ -134,14 +134,12 @@ private:
return qsizetype(std::size(c)); 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. // will be of by one.
template <typename Char, size_t N> template <typename Char, size_t N>
static constexpr qsizetype lengthHelperContainer(const Char (&str)[N]) noexcept static constexpr qsizetype lengthHelperContainer(const Char (&str)[N]) noexcept
{ {
const auto it = std::char_traits<Char>::find(str, N, Char(0)); return QtPrivate::lengthHelperContainer(str);
const auto end = it ? it : std::next(str, N);
return qsizetype(std::distance(str, end));
} }
template <typename Char> template <typename Char>
@ -174,8 +172,7 @@ public:
#else #else
template <typename Pointer, if_compatible_pointer<Pointer> = true> template <typename Pointer, if_compatible_pointer<Pointer> = true>
constexpr QBasicUtf8StringView(const Pointer &str) noexcept constexpr QBasicUtf8StringView(const Pointer &str) noexcept
: QBasicUtf8StringView(str, : QBasicUtf8StringView(str, QtPrivate::lengthHelperPointer(str)) {}
str ? std::char_traits<std::remove_cv_t<std::remove_pointer_t<Pointer>>>::length(str) : 0) {}
#endif #endif
#ifdef Q_QDOC #ifdef Q_QDOC