1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 11:38:11 +00:00

AK: Add a StringView method to count the number of lines in a string

We already have a helper to split a StringView by line while considering
"\n", "\r", and "\r\n". Add an analagous method to just count the number
of lines in the same manner.
This commit is contained in:
Timothy Flynn 2024-03-08 09:57:35 -05:00 committed by Tim Flynn
parent 07a27b2ec0
commit 82ea53cf10
3 changed files with 79 additions and 30 deletions

View file

@ -105,6 +105,24 @@ TEST_CASE(lines)
EXPECT_EQ(test_string_vector.at(2).is_empty(), true);
}
TEST_CASE(count_lines)
{
EXPECT_EQ(""sv.count_lines(), 1u);
EXPECT_EQ("foo"sv.count_lines(), 1u);
EXPECT_EQ("foo\nbar"sv.count_lines(), 2u);
EXPECT_EQ("foo\rbar"sv.count_lines(), 2u);
EXPECT_EQ("foo\rbar"sv.count_lines(StringView::ConsiderCarriageReturn::No), 1u);
EXPECT_EQ("foo\r\nbar"sv.count_lines(), 2u);
EXPECT_EQ("foo\r\nbar"sv.count_lines(StringView::ConsiderCarriageReturn::No), 2u);
EXPECT_EQ("foo\nbar\nbax"sv.count_lines(), 3u);
EXPECT_EQ("foo\rbar\rbaz"sv.count_lines(), 3u);
EXPECT_EQ("foo\rbar\rbaz"sv.count_lines(StringView::ConsiderCarriageReturn::No), 1u);
EXPECT_EQ("foo\r\nbar\r\nbaz"sv.count_lines(), 3u);
EXPECT_EQ("foo\r\nbar\r\nbaz"sv.count_lines(StringView::ConsiderCarriageReturn::No), 3u);
}
TEST_CASE(find)
{
auto test_string_view = "aabbcc_xy_ccbbaa"sv;