mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-14 01:04:38 +03:00
AK: Implement method to convert a String/StringView to title case
This implementation preserves consecutive spaces in the orginal string.
This commit is contained in:
parent
d2af27d2d0
commit
262e412634
Notes:
sideshowbarker
2024-07-18 05:16:02 +09:00
Author: https://github.com/trflynn89 Commit: https://github.com/SerenityOS/serenity/commit/262e4126346 Pull-request: https://github.com/SerenityOS/serenity/pull/9599 Reviewed-by: https://github.com/alimpfard Reviewed-by: https://github.com/davidot Reviewed-by: https://github.com/linusg ✅
@ -446,6 +446,11 @@ String String::to_snakecase() const
|
||||
return StringUtils::to_snakecase(*this);
|
||||
}
|
||||
|
||||
String String::to_titlecase() const
|
||||
{
|
||||
return StringUtils::to_titlecase(*this);
|
||||
}
|
||||
|
||||
bool operator<(const char* characters, const String& string)
|
||||
{
|
||||
if (!characters)
|
||||
|
@ -120,6 +120,7 @@ public:
|
||||
[[nodiscard]] String to_lowercase() const;
|
||||
[[nodiscard]] String to_uppercase() const;
|
||||
[[nodiscard]] String to_snakecase() const;
|
||||
[[nodiscard]] String to_titlecase() const;
|
||||
|
||||
[[nodiscard]] bool is_whitespace() const { return StringUtils::is_whitespace(*this); }
|
||||
|
||||
|
@ -411,6 +411,22 @@ String to_snakecase(const StringView& str)
|
||||
return builder.to_string();
|
||||
}
|
||||
|
||||
String to_titlecase(StringView const& str)
|
||||
{
|
||||
StringBuilder builder;
|
||||
bool next_is_upper = true;
|
||||
|
||||
for (auto ch : str) {
|
||||
if (next_is_upper)
|
||||
builder.append_code_point(to_ascii_uppercase(ch));
|
||||
else
|
||||
builder.append_code_point(to_ascii_lowercase(ch));
|
||||
next_is_upper = ch == ' ';
|
||||
}
|
||||
|
||||
return builder.to_string();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -69,6 +69,7 @@ enum class SearchDirection {
|
||||
Optional<size_t> find_any_of(StringView const& haystack, StringView const& needles, SearchDirection);
|
||||
|
||||
String to_snakecase(const StringView&);
|
||||
String to_titlecase(StringView const&);
|
||||
|
||||
}
|
||||
|
||||
|
@ -183,6 +183,11 @@ String StringView::to_uppercase_string() const
|
||||
return StringImpl::create_uppercased(characters_without_null_termination(), length());
|
||||
}
|
||||
|
||||
String StringView::to_titlecase_string() const
|
||||
{
|
||||
return StringUtils::to_titlecase(*this);
|
||||
}
|
||||
|
||||
StringView StringView::substring_view_starting_from_substring(const StringView& substring) const
|
||||
{
|
||||
const char* remaining_characters = substring.characters_without_null_termination();
|
||||
|
@ -85,6 +85,7 @@ public:
|
||||
|
||||
[[nodiscard]] String to_lowercase_string() const;
|
||||
[[nodiscard]] String to_uppercase_string() const;
|
||||
[[nodiscard]] String to_titlecase_string() const;
|
||||
|
||||
[[nodiscard]] Optional<size_t> find(char needle, size_t start = 0) const { return StringUtils::find(*this, needle, start); }
|
||||
[[nodiscard]] Optional<size_t> find(StringView const& needle, size_t start = 0) const { return StringUtils::find(*this, needle, start); }
|
||||
|
@ -304,3 +304,17 @@ TEST_CASE(to_snakecase)
|
||||
EXPECT_EQ(AK::StringUtils::to_snakecase("FBar"), "f_bar");
|
||||
EXPECT_EQ(AK::StringUtils::to_snakecase("FooB"), "foo_b");
|
||||
}
|
||||
|
||||
TEST_CASE(to_titlecase)
|
||||
{
|
||||
EXPECT_EQ(AK::StringUtils::to_titlecase(""sv), ""sv);
|
||||
EXPECT_EQ(AK::StringUtils::to_titlecase("f"sv), "F"sv);
|
||||
EXPECT_EQ(AK::StringUtils::to_titlecase("foobar"sv), "Foobar"sv);
|
||||
EXPECT_EQ(AK::StringUtils::to_titlecase("Foobar"sv), "Foobar"sv);
|
||||
EXPECT_EQ(AK::StringUtils::to_titlecase("FOOBAR"sv), "Foobar"sv);
|
||||
EXPECT_EQ(AK::StringUtils::to_titlecase("foo bar"sv), "Foo Bar"sv);
|
||||
EXPECT_EQ(AK::StringUtils::to_titlecase("foo bAR"sv), "Foo Bar"sv);
|
||||
EXPECT_EQ(AK::StringUtils::to_titlecase("foo bar"sv), "Foo Bar"sv);
|
||||
EXPECT_EQ(AK::StringUtils::to_titlecase("foo bar"sv), "Foo Bar"sv);
|
||||
EXPECT_EQ(AK::StringUtils::to_titlecase(" foo bar "sv), " Foo Bar "sv);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user