Bug 1880008 - Add char8_t string overloads to MFBT string functions when compiling with -std=c++20. r=xpcom-reviewers,emilio

When compiled with -std=c++17, u8"" string literals have type `const char[]`. When compiled with -std=c++20, u8"" string literals have type `const char8_t[]`. This patch adds the minimum char8_t overloads needed to compile Firefox with -std=c++20. If we want to use char8_t in more code or replace `Utf8Unit` with char8_t, that will require an extensive redesign of MFBT's and xpcom's string classes after we update from -std=c++17 to c++20.

Differential Revision: https://phabricator.services.mozilla.com/D201672
This commit is contained in:
Chris Peterson 2024-02-14 01:33:31 +00:00
Родитель 88ecbe13e5
Коммит 515689e933
3 изменённых файлов: 24 добавлений и 0 удалений

Просмотреть файл

@ -43,6 +43,13 @@ inline constexpr bool IsAscii(char aChar) {
return IsAscii(static_cast<unsigned char>(aChar));
}
#ifdef __cpp_char8_t
/** Returns true iff |aChar| is ASCII, i.e. in the range [0, 0x80). */
inline constexpr bool IsAscii(char8_t aChar) {
return IsAscii(static_cast<unsigned char>(aChar));
}
#endif
/** Returns true iff |aChar| is ASCII, i.e. in the range [0, 0x80). */
inline constexpr bool IsAscii(char16_t aChar) { return aChar < 0x80; }

Просмотреть файл

@ -176,6 +176,11 @@ union Utf8Unit {
// assume the conversion does what we want it to.
}
#ifdef __cpp_char8_t
explicit constexpr Utf8Unit(char8_t aUnit)
: mValue(static_cast<char>(aUnit)) {}
#endif
constexpr bool operator==(const Utf8Unit& aOther) const {
return mValue == aOther.mValue;
}

Просмотреть файл

@ -240,6 +240,18 @@ class nsTStringRepr {
*/
bool EqualsIgnoreCase(const std::string_view& aString) const;
#ifdef __cpp_char8_t
template <typename Q = T, typename EnableIfChar = mozilla::CharOnlyT<Q>>
bool NS_FASTCALL Equals(const char8_t* aData) const {
return Equals(reinterpret_cast<const char*>(aData));
}
template <typename Q = T, typename EnableIfChar = mozilla::CharOnlyT<Q>>
bool NS_FASTCALL Equals(const char8_t* aData, comparator_type aComp) const {
return Equals(reinterpret_cast<const char*>(aData), aComp);
}
#endif
#if defined(MOZ_USE_CHAR16_WRAPPER)
template <typename Q = T, typename EnableIfChar16 = Char16OnlyT<Q>>
bool NS_FASTCALL Equals(char16ptr_t aData) const {