mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 23:20:20 +00:00
AK: Add invert_case() and invert_case(StringView)
In the given String, invert_case() swaps lowercase characters with uppercase ones and vice versa.
This commit is contained in:
parent
b1709c368f
commit
8ffa860bc3
Notes:
sideshowbarker
2024-07-17 10:37:41 +09:00
Author: https://github.com/huttongrabiel Commit: https://github.com/SerenityOS/serenity/commit/8ffa860bc3 Pull-request: https://github.com/SerenityOS/serenity/pull/14105 Reviewed-by: https://github.com/2xsaiko
4 changed files with 21 additions and 0 deletions
|
@ -388,6 +388,11 @@ String String::to_titlecase() const
|
|||
return StringUtils::to_titlecase(*this);
|
||||
}
|
||||
|
||||
String String::invert_case() const
|
||||
{
|
||||
return StringUtils::invert_case(*this);
|
||||
}
|
||||
|
||||
bool operator<(char const* characters, String const& string)
|
||||
{
|
||||
return string.view() > characters;
|
||||
|
|
|
@ -121,6 +121,7 @@ public:
|
|||
[[nodiscard]] String to_uppercase() const;
|
||||
[[nodiscard]] String to_snakecase() const;
|
||||
[[nodiscard]] String to_titlecase() const;
|
||||
[[nodiscard]] String invert_case() const;
|
||||
|
||||
[[nodiscard]] bool is_whitespace() const { return StringUtils::is_whitespace(*this); }
|
||||
|
||||
|
|
|
@ -462,6 +462,20 @@ String to_titlecase(StringView str)
|
|||
return builder.to_string();
|
||||
}
|
||||
|
||||
String invert_case(StringView str)
|
||||
{
|
||||
StringBuilder builder(str.length());
|
||||
|
||||
for (auto ch : str) {
|
||||
if (is_ascii_lower_alpha(ch))
|
||||
builder.append(to_ascii_uppercase(ch));
|
||||
else
|
||||
builder.append(to_ascii_lowercase(ch));
|
||||
}
|
||||
|
||||
return builder.to_string();
|
||||
}
|
||||
|
||||
String replace(StringView str, StringView needle, StringView replacement, bool all_occurrences)
|
||||
{
|
||||
if (str.is_empty())
|
||||
|
|
|
@ -78,6 +78,7 @@ Optional<size_t> find_any_of(StringView haystack, StringView needles, SearchDire
|
|||
|
||||
String to_snakecase(StringView);
|
||||
String to_titlecase(StringView);
|
||||
String invert_case(StringView);
|
||||
|
||||
String replace(StringView, StringView needle, StringView replacement, bool all_occurrences = false);
|
||||
size_t count(StringView, StringView needle);
|
||||
|
|
Loading…
Reference in a new issue