AK: Add String::trim

This commit is contained in:
Timothy Flynn 2023-01-27 14:37:40 -05:00 committed by Linus Groh
parent cccaa94767
commit c59268d15b
Notes: sideshowbarker 2024-07-17 02:35:27 +09:00
3 changed files with 126 additions and 0 deletions

View file

@ -438,6 +438,17 @@ ErrorOr<String> String::reverse() const
return builder.to_string(); return builder.to_string();
} }
ErrorOr<String> String::trim(Utf8View const& code_points_to_trim, TrimMode mode) const
{
auto trimmed = code_points().trim(code_points_to_trim, mode);
return String::from_utf8(trimmed.as_string());
}
ErrorOr<String> String::trim(StringView code_points_to_trim, TrimMode mode) const
{
return trim(Utf8View { code_points_to_trim }, mode);
}
bool String::contains(StringView needle, CaseSensitivity case_sensitivity) const bool String::contains(StringView needle, CaseSensitivity case_sensitivity) const
{ {
return StringUtils::contains(bytes_as_string_view(), needle, case_sensitivity); return StringUtils::contains(bytes_as_string_view(), needle, case_sensitivity);

View file

@ -130,6 +130,9 @@ public:
ErrorOr<String> replace(StringView needle, StringView replacement, ReplaceMode replace_mode) const; ErrorOr<String> replace(StringView needle, StringView replacement, ReplaceMode replace_mode) const;
ErrorOr<String> reverse() const; ErrorOr<String> reverse() const;
ErrorOr<String> trim(Utf8View const& code_points_to_trim, TrimMode mode = TrimMode::Both) const;
ErrorOr<String> trim(StringView code_points_to_trim, TrimMode mode = TrimMode::Both) const;
ErrorOr<Vector<String>> split_limit(u32 separator, size_t limit, SplitBehavior = SplitBehavior::Nothing) const; ErrorOr<Vector<String>> split_limit(u32 separator, size_t limit, SplitBehavior = SplitBehavior::Nothing) const;
ErrorOr<Vector<String>> split(u32 separator, SplitBehavior = SplitBehavior::Nothing) const; ErrorOr<Vector<String>> split(u32 separator, SplitBehavior = SplitBehavior::Nothing) const;

View file

@ -495,3 +495,115 @@ TEST_CASE(join)
auto string7 = MUST(String::join(" - "sv, Array { 1, 16, 256, 4096 }, "[{:#04x}]"sv)); auto string7 = MUST(String::join(" - "sv, Array { 1, 16, 256, 4096 }, "[{:#04x}]"sv));
EXPECT_EQ(string7, "[0x0001] - [0x0010] - [0x0100] - [0x1000]"sv); EXPECT_EQ(string7, "[0x0001] - [0x0010] - [0x0100] - [0x1000]"sv);
} }
TEST_CASE(trim)
{
{
String string {};
auto result = MUST(string.trim(" "sv, TrimMode::Both));
EXPECT(result.is_empty());
result = MUST(string.trim(" "sv, TrimMode::Left));
EXPECT(result.is_empty());
result = MUST(string.trim(" "sv, TrimMode::Right));
EXPECT(result.is_empty());
}
{
auto string = MUST(String::from_utf8("word"sv));
auto result = MUST(string.trim(" "sv, TrimMode::Both));
EXPECT_EQ(result, "word"sv);
result = MUST(string.trim(" "sv, TrimMode::Left));
EXPECT_EQ(result, "word"sv);
result = MUST(string.trim(" "sv, TrimMode::Right));
EXPECT_EQ(result, "word"sv);
}
{
auto string = MUST(String::from_utf8(" word"sv));
auto result = MUST(string.trim(" "sv, TrimMode::Both));
EXPECT_EQ(result, "word"sv);
result = MUST(string.trim(" "sv, TrimMode::Left));
EXPECT_EQ(result, "word"sv);
result = MUST(string.trim(" "sv, TrimMode::Right));
EXPECT_EQ(result, " word"sv);
}
{
auto string = MUST(String::from_utf8("word "sv));
auto result = MUST(string.trim(" "sv, TrimMode::Both));
EXPECT_EQ(result, "word"sv);
result = MUST(string.trim(" "sv, TrimMode::Left));
EXPECT_EQ(result, "word "sv);
result = MUST(string.trim(" "sv, TrimMode::Right));
EXPECT_EQ(result, "word"sv);
}
{
auto string = MUST(String::from_utf8(" word "sv));
auto result = MUST(string.trim(" "sv, TrimMode::Both));
EXPECT_EQ(result, "word"sv);
result = MUST(string.trim(" "sv, TrimMode::Left));
EXPECT_EQ(result, "word "sv);
result = MUST(string.trim(" "sv, TrimMode::Right));
EXPECT_EQ(result, " word"sv);
}
{
auto string = MUST(String::from_utf8(" word "sv));
auto result = MUST(string.trim("\t"sv, TrimMode::Both));
EXPECT_EQ(result, " word "sv);
result = MUST(string.trim("\t"sv, TrimMode::Left));
EXPECT_EQ(result, " word "sv);
result = MUST(string.trim("\t"sv, TrimMode::Right));
EXPECT_EQ(result, " word "sv);
}
{
auto string = MUST(String::from_utf8("ωΣωΣω"sv));
auto result = MUST(string.trim("ω"sv, TrimMode::Both));
EXPECT_EQ(result, "ΣωΣ"sv);
result = MUST(string.trim("ω"sv, TrimMode::Left));
EXPECT_EQ(result, "ΣωΣω"sv);
result = MUST(string.trim("ω"sv, TrimMode::Right));
EXPECT_EQ(result, "ωΣωΣ"sv);
}
{
auto string = MUST(String::from_utf8("ωΣωΣω"sv));
auto result = MUST(string.trim("ωΣ"sv, TrimMode::Both));
EXPECT(result.is_empty());
result = MUST(string.trim("ωΣ"sv, TrimMode::Left));
EXPECT(result.is_empty());
result = MUST(string.trim("ωΣ"sv, TrimMode::Right));
EXPECT(result.is_empty());
}
{
auto string = MUST(String::from_utf8("ωΣωΣω"sv));
auto result = MUST(string.trim("Σω"sv, TrimMode::Both));
EXPECT(result.is_empty());
result = MUST(string.trim("Σω"sv, TrimMode::Left));
EXPECT(result.is_empty());
result = MUST(string.trim("Σω"sv, TrimMode::Right));
EXPECT(result.is_empty());
}
}