mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 06:58:11 +00:00
AK: Add StringView::find_first/last_of
These methods search from the beginning or end of a string for the first character in the input StringView and returns the position in the string of the first match. Note that this is not a substring match. Each comes with single char overloads for efficiency.
This commit is contained in:
parent
94c28552c6
commit
3eb3c2477f
3 changed files with 91 additions and 0 deletions
|
@ -111,4 +111,47 @@ TEST_CASE(lines)
|
|||
EXPECT_EQ(test_string_vector.at(2).is_empty(), true);
|
||||
}
|
||||
|
||||
TEST_CASE(find_first_of)
|
||||
{
|
||||
String test_string = "aabbcc_xy_ccbbaa";
|
||||
StringView test_string_view = test_string.view();
|
||||
|
||||
EXPECT_EQ(test_string_view.find_first_of('b').has_value(), true);
|
||||
EXPECT_EQ(test_string_view.find_first_of('b').value(), 2U);
|
||||
|
||||
EXPECT_EQ(test_string_view.find_first_of('_').has_value(), true);
|
||||
EXPECT_EQ(test_string_view.find_first_of('_').value(), 6U);
|
||||
|
||||
EXPECT_EQ(test_string_view.find_first_of("bc").has_value(), true);
|
||||
EXPECT_EQ(test_string_view.find_first_of("bc").value(), 2U);
|
||||
|
||||
EXPECT_EQ(test_string_view.find_first_of("yx").has_value(), true);
|
||||
EXPECT_EQ(test_string_view.find_first_of("yx").value(), 7U);
|
||||
|
||||
EXPECT_EQ(test_string_view.find_first_of('n').has_value(), false);
|
||||
EXPECT_EQ(test_string_view.find_first_of("defg").has_value(), false);
|
||||
}
|
||||
|
||||
TEST_CASE(find_last_of)
|
||||
{
|
||||
String test_string = "aabbcc_xy_ccbbaa";
|
||||
StringView test_string_view = test_string.view();
|
||||
|
||||
EXPECT_EQ(test_string_view.find_last_of('b').has_value(), true);
|
||||
EXPECT_EQ(test_string_view.find_last_of('b').value(), 13U);
|
||||
|
||||
EXPECT_EQ(test_string_view.find_last_of('_').has_value(), true);
|
||||
EXPECT_EQ(test_string_view.find_last_of('_').value(), 9U);
|
||||
|
||||
EXPECT_EQ(test_string_view.find_last_of("bc").has_value(), true);
|
||||
EXPECT_EQ(test_string_view.find_last_of("bc").value(), 13U);
|
||||
|
||||
EXPECT_EQ(test_string_view.find_last_of("yx").has_value(), true);
|
||||
EXPECT_EQ(test_string_view.find_last_of("yx").value(), 8U);
|
||||
|
||||
EXPECT_EQ(test_string_view.find_last_of('3').has_value(), false);
|
||||
EXPECT_EQ(test_string_view.find_last_of("fghi").has_value(), false);
|
||||
}
|
||||
|
||||
|
||||
TEST_MAIN(StringView)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue