mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 07:30:19 +00:00
AK: Add String::trim
This commit is contained in:
parent
cccaa94767
commit
c59268d15b
Notes:
sideshowbarker
2024-07-17 02:35:27 +09:00
Author: https://github.com/trflynn89 Commit: https://github.com/SerenityOS/serenity/commit/c59268d15b Pull-request: https://github.com/SerenityOS/serenity/pull/17206 Reviewed-by: https://github.com/linusg ✅
3 changed files with 126 additions and 0 deletions
|
@ -438,6 +438,17 @@ ErrorOr<String> String::reverse() const
|
|||
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
|
||||
{
|
||||
return StringUtils::contains(bytes_as_string_view(), needle, case_sensitivity);
|
||||
|
|
|
@ -130,6 +130,9 @@ public:
|
|||
ErrorOr<String> replace(StringView needle, StringView replacement, ReplaceMode replace_mode) 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(u32 separator, SplitBehavior = SplitBehavior::Nothing) const;
|
||||
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue