1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 14:27:35 +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:
Ali Mohammad Pur 2021-06-27 22:41:38 +04:30 committed by Linus Groh
parent 67d1b28b97
commit 37b0f55104
3 changed files with 41 additions and 4 deletions

View file

@ -177,3 +177,28 @@ TEST_CASE(split_view)
EXPECT_EQ(test_string_view.split_view_if(predicate), Vector<StringView>({ "a", "b", "c", "d" }));
EXPECT_EQ(test_string_view.split_view_if(predicate, true), Vector<StringView>({ "a", "", "b", "c", "d" }));
}
TEST_CASE(constexpr_stuff)
{
#define do_test() \
static_assert(test_constexpr.length() == 3); \
static_assert(!test_constexpr.is_empty()); \
static_assert(test_constexpr.is_one_of("foo", "bar", "baz")); \
static_assert(test_constexpr.is_one_of("foo"sv, "bar"sv, "baz"sv)); \
static_assert(test_constexpr != "fob"sv); \
static_assert(test_constexpr != "fob"); \
static_assert(test_constexpr.substring_view(1).is_one_of("oo"sv));
{
// Can initialize from ""sv.
constexpr StringView test_constexpr { "foo"sv };
do_test();
}
{
// Can initialize from char const*.
constexpr StringView test_constexpr { "foo" };
do_test();
}
#undef do_test
}