AK+LibWeb: Add {Fly,}String::to_ascii_{upper,lower}_case()

These don't have to worry about the input not being valid UTF-8 and
so can be infallible (and can even return self if no changes needed.)

We use this instead of Infra::to_ascii_{upper,lower}_case in LibWeb.
This commit is contained in:
Andreas Kling 2024-10-14 10:51:15 +02:00 committed by Andreas Kling
commit 073bcfd386
Notes: github-actions[bot] 2024-10-14 18:49:00 +00:00
16 changed files with 147 additions and 13 deletions

View file

@ -1416,3 +1416,27 @@ TEST_CASE(ends_with)
EXPECT(emoji.ends_with(0x1F643));
EXPECT(!emoji.ends_with(0x1F600));
}
TEST_CASE(to_ascii_lowercase)
{
EXPECT_EQ("foobar"_string.to_ascii_lowercase(), "foobar"_string);
EXPECT_EQ("FooBar"_string.to_ascii_lowercase(), "foobar"_string);
EXPECT_EQ("FOOBAR"_string.to_ascii_lowercase(), "foobar"_string);
// NOTE: We expect to_ascii_lowercase() to return the same underlying string if no changes are needed.
auto long_string = "this is a long string that cannot use the short string optimization"_string;
auto lowercased = long_string.to_ascii_lowercase();
EXPECT_EQ(long_string.bytes().data(), lowercased.bytes().data());
}
TEST_CASE(to_ascii_uppercase)
{
EXPECT_EQ("foobar"_string.to_ascii_uppercase(), "FOOBAR"_string);
EXPECT_EQ("FooBar"_string.to_ascii_uppercase(), "FOOBAR"_string);
EXPECT_EQ("FOOBAR"_string.to_ascii_uppercase(), "FOOBAR"_string);
// NOTE: We expect to_ascii_uppercase() to return the same underlying string if no changes are needed.
auto long_string = "THIS IS A LONG STRING THAT CANNOT USE THE SHORT STRING OPTIMIZATION"_string;
auto uppercased = long_string.to_ascii_uppercase();
EXPECT_EQ(long_string.bytes().data(), uppercased.bytes().data());
}