mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 01:57:45 +00:00
AK: Make the constexpr StringView methods actually constexpr
Also add some tests to ensure that they _remain_ constexpr. In general, any runtime assertions, weirdo C casts, pointer aliasing, and such shenanigans should be gated behind the (helpfully newly added) AK::is_constant_evaluated() function when the intention is to write constexpr-capable code. a.k.a. deliver promises of constexpr-ness :P
This commit is contained in:
parent
67d1b28b97
commit
37b0f55104
3 changed files with 41 additions and 4 deletions
|
@ -24,7 +24,8 @@ public:
|
|||
: m_characters(characters)
|
||||
, m_length(length)
|
||||
{
|
||||
VERIFY(!Checked<uintptr_t>::addition_would_overflow((uintptr_t)characters, length));
|
||||
if (!is_constant_evaluated())
|
||||
VERIFY(!Checked<uintptr_t>::addition_would_overflow((uintptr_t)characters, length));
|
||||
}
|
||||
ALWAYS_INLINE StringView(const unsigned char* characters, size_t length)
|
||||
: m_characters((const char*)characters)
|
||||
|
@ -93,7 +94,8 @@ public:
|
|||
|
||||
[[nodiscard]] constexpr StringView substring_view(size_t start, size_t length) const
|
||||
{
|
||||
VERIFY(start + length <= m_length);
|
||||
if (!is_constant_evaluated())
|
||||
VERIFY(start + length <= m_length);
|
||||
return { m_characters + start, length };
|
||||
}
|
||||
|
||||
|
@ -158,7 +160,7 @@ public:
|
|||
[[nodiscard]] StringView substring_view_starting_from_substring(const StringView& substring) const;
|
||||
[[nodiscard]] StringView substring_view_starting_after_substring(const StringView& substring) const;
|
||||
|
||||
bool operator==(const char* cstring) const
|
||||
constexpr bool operator==(const char* cstring) const
|
||||
{
|
||||
if (is_null())
|
||||
return !cstring;
|
||||
|
@ -175,7 +177,7 @@ public:
|
|||
return !*cp;
|
||||
}
|
||||
|
||||
bool operator!=(const char* cstring) const
|
||||
constexpr bool operator!=(const char* cstring) const
|
||||
{
|
||||
return !(*this == cstring);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue