Bug 1602452 - Make member functions of nsTStringRepr defined in header constexpr. r=froydnj

Differential Revision: https://phabricator.services.mozilla.com/D56369

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Simon Giesecke 2019-12-10 08:40:48 +00:00
Родитель 8f4c6468bb
Коммит fec8c64d2a
3 изменённых файлов: 48 добавлений и 20 удалений

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

@ -39,14 +39,14 @@ class char16ptr_t {
"char16_t and wchar_t sizes differ");
public:
MOZ_IMPLICIT char16ptr_t(const char16_t* aPtr) : mPtr(aPtr) {}
constexpr MOZ_IMPLICIT char16ptr_t(const char16_t* aPtr) : mPtr(aPtr) {}
MOZ_IMPLICIT char16ptr_t(const wchar_t* aPtr)
: mPtr(reinterpret_cast<const char16_t*>(aPtr)) {}
/* Without this, nullptr assignment would be ambiguous. */
constexpr MOZ_IMPLICIT char16ptr_t(decltype(nullptr)) : mPtr(nullptr) {}
operator const char16_t*() const { return mPtr; }
constexpr operator const char16_t*() const { return mPtr; }
operator const wchar_t*() const {
return reinterpret_cast<const wchar_t*>(mPtr);
}
@ -55,11 +55,13 @@ class char16ptr_t {
return const_cast<wchar_t*>(reinterpret_cast<const wchar_t*>(mPtr));
}
operator const void*() const { return mPtr; }
explicit operator bool() const { return mPtr != nullptr; }
constexpr operator const void*() const { return mPtr; }
constexpr explicit operator bool() const { return mPtr != nullptr; }
/* Explicit cast operators to allow things like (char16_t*)str. */
explicit operator char16_t*() const { return const_cast<char16_t*>(mPtr); }
constexpr explicit operator char16_t*() const {
return const_cast<char16_t*>(mPtr);
}
explicit operator wchar_t*() const {
return const_cast<wchar_t*>(static_cast<const wchar_t*>(*this));
}

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

@ -124,8 +124,8 @@ class nsTStringRepr {
typedef StringClassFlags ClassFlags;
// Reading iterators.
const_char_iterator BeginReading() const { return mData; }
const_char_iterator EndReading() const { return mData + mLength; }
constexpr const_char_iterator BeginReading() const { return mData; }
constexpr const_char_iterator EndReading() const { return mData + mLength; }
// Deprecated reading iterators.
const_iterator& BeginReading(const_iterator& aIter) const {
@ -163,26 +163,32 @@ class nsTStringRepr {
#endif
// Returns pointer to string data (not necessarily null-terminated)
const typename raw_type<T, int>::type Data() const { return mData; }
constexpr const typename raw_type<T, int>::type Data() const { return mData; }
size_type Length() const { return mLength; }
constexpr size_type Length() const { return mLength; }
DataFlags GetDataFlags() const { return mDataFlags; }
constexpr DataFlags GetDataFlags() const { return mDataFlags; }
bool IsEmpty() const { return mLength == 0; }
constexpr bool IsEmpty() const { return mLength == 0; }
bool IsLiteral() const { return !!(mDataFlags & DataFlags::LITERAL); }
constexpr bool IsLiteral() const {
return !!(mDataFlags & DataFlags::LITERAL);
}
bool IsVoid() const { return !!(mDataFlags & DataFlags::VOIDED); }
constexpr bool IsVoid() const { return !!(mDataFlags & DataFlags::VOIDED); }
bool IsTerminated() const { return !!(mDataFlags & DataFlags::TERMINATED); }
constexpr bool IsTerminated() const {
return !!(mDataFlags & DataFlags::TERMINATED);
}
char_type CharAt(index_type aIndex) const {
constexpr char_type CharAt(index_type aIndex) const {
NS_ASSERTION(aIndex < mLength, "index exceeds allowable range");
return mData[aIndex];
}
char_type operator[](index_type aIndex) const { return CharAt(aIndex); }
constexpr char_type operator[](index_type aIndex) const {
return CharAt(aIndex);
}
char_type First() const;
@ -347,13 +353,14 @@ Compare(const mozilla::detail::nsTStringRepr<T>& aLhs,
const nsTStringComparator<T>& = nsTDefaultStringComparator<T>());
template <typename T>
inline bool operator!=(const mozilla::detail::nsTStringRepr<T>& aLhs,
inline constexpr bool operator!=(
const mozilla::detail::nsTStringRepr<T>& aLhs,
const mozilla::detail::nsTStringRepr<T>& aRhs) {
return !aLhs.Equals(aRhs);
}
template <typename T>
inline bool operator!=(const mozilla::detail::nsTStringRepr<T>& aLhs,
inline constexpr bool operator!=(const mozilla::detail::nsTStringRepr<T>& aLhs,
const T* aRhs) {
return !aLhs.Equals(aRhs);
}

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

@ -2446,6 +2446,25 @@ CONVERSION_BENCH(PerfUTF8toUTF16VIHundred, CopyUTF8toUTF16, mViHundredUtf8,
CONVERSION_BENCH(PerfUTF8toUTF16VIThousand, CopyUTF8toUTF16, mViThousandUtf8,
nsAutoString);
// Tests for usability of nsTLiteralString in constant expressions.
static_assert(NS_LITERAL_STRING("").IsEmpty());
constexpr auto testStringA = NS_LITERAL_STRING("a");
static_assert(!testStringA.IsEmpty());
static_assert(!testStringA.IsVoid());
static_assert(testStringA.IsLiteral());
static_assert(testStringA.IsTerminated());
static_assert(testStringA.GetDataFlags() ==
(nsLiteralString::DataFlags::LITERAL |
nsLiteralString::DataFlags::TERMINATED));
static_assert(*static_cast<const char16_t*>(testStringA.Data()) == 'a');
static_assert(1 == testStringA.Length());
static_assert(testStringA.CharAt(0) == 'a');
static_assert(testStringA[0] == 'a');
static_assert(*testStringA.BeginReading() == 'a');
static_assert(*testStringA.EndReading() == 0);
static_assert(testStringA.EndReading() - testStringA.BeginReading() == 1);
} // namespace TestStrings
#if defined(__clang__) && (__clang_major__ >= 6)