diff --git a/AK/String.cpp b/AK/String.cpp index e165bf697ab..49aed1d2478 100644 --- a/AK/String.cpp +++ b/AK/String.cpp @@ -438,6 +438,17 @@ ErrorOr String::reverse() const return builder.to_string(); } +ErrorOr 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::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 { return StringUtils::contains(bytes_as_string_view(), needle, case_sensitivity); diff --git a/AK/String.h b/AK/String.h index 46821e34261..38873c614cf 100644 --- a/AK/String.h +++ b/AK/String.h @@ -130,6 +130,9 @@ public: ErrorOr replace(StringView needle, StringView replacement, ReplaceMode replace_mode) const; ErrorOr reverse() const; + ErrorOr trim(Utf8View const& code_points_to_trim, TrimMode mode = TrimMode::Both) const; + ErrorOr trim(StringView code_points_to_trim, TrimMode mode = TrimMode::Both) const; + ErrorOr> split_limit(u32 separator, size_t limit, SplitBehavior = SplitBehavior::Nothing) const; ErrorOr> split(u32 separator, SplitBehavior = SplitBehavior::Nothing) const; diff --git a/Tests/AK/TestString.cpp b/Tests/AK/TestString.cpp index 4f5d87dee09..38799ddb61a 100644 --- a/Tests/AK/TestString.cpp +++ b/Tests/AK/TestString.cpp @@ -495,3 +495,115 @@ TEST_CASE(join) auto string7 = MUST(String::join(" - "sv, Array { 1, 16, 256, 4096 }, "[{:#04x}]"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()); + } +}