1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 20:57:35 +00:00

AK: Add an overload of String::find_byte_offset for StringView

This commit is contained in:
Timothy Flynn 2023-01-27 10:17:34 -05:00 committed by Linus Groh
parent 38b4e938b7
commit c35b1371a3
3 changed files with 55 additions and 2 deletions

View file

@ -320,8 +320,11 @@ TEST_CASE(find_byte_offset)
{
{
String string {};
auto index = string.find_byte_offset(0);
EXPECT(!index.has_value());
auto index1 = string.find_byte_offset(0);
EXPECT(!index1.has_value());
auto index2 = string.find_byte_offset(""sv);
EXPECT(!index2.has_value());
}
{
auto string = MUST(String::from_utf8("foo"sv));
@ -338,6 +341,21 @@ TEST_CASE(find_byte_offset)
auto index4 = string.find_byte_offset('b');
EXPECT(!index4.has_value());
}
{
auto string = MUST(String::from_utf8("foo"sv));
auto index1 = string.find_byte_offset("fo"sv);
EXPECT_EQ(index1, 0u);
auto index2 = string.find_byte_offset("oo"sv);
EXPECT_EQ(index2, 1u);
auto index3 = string.find_byte_offset("o"sv, *index2 + 1);
EXPECT_EQ(index3, 2u);
auto index4 = string.find_byte_offset("fooo"sv);
EXPECT(!index4.has_value());
}
{
auto string = MUST(String::from_utf8("ωΣωΣω"sv));
@ -356,6 +374,24 @@ TEST_CASE(find_byte_offset)
auto index5 = string.find_byte_offset(0x03C9U, 6);
EXPECT_EQ(index5, 8u);
}
{
auto string = MUST(String::from_utf8("ωΣωΣω"sv));
auto index1 = string.find_byte_offset("ω"sv);
EXPECT_EQ(index1, 0u);
auto index2 = string.find_byte_offset("Σ"sv);
EXPECT_EQ(index2, 2u);
auto index3 = string.find_byte_offset("ω"sv, 2);
EXPECT_EQ(index3, 4u);
auto index4 = string.find_byte_offset("Σ"sv, 4);
EXPECT_EQ(index4, 6u);
auto index5 = string.find_byte_offset("ω"sv, 6);
EXPECT_EQ(index5, 8u);
}
}
TEST_CASE(repeated)