summaryrefslogtreecommitdiff
path: root/AK
diff options
context:
space:
mode:
authorhuttongrabiel <huttonthomas@icloud.com>2022-05-18 22:23:45 -0700
committerLinus Groh <mail@linusgroh.de>2022-05-26 21:51:23 +0100
commit8ffa860bc3cb45673c3cb7fb617259df60c987da (patch)
tree4b600aca663f1167fd119410fbd72ddd0f6cfc30 /AK
parentb1709c368f93c76ccbec7ceca5106948a974db6e (diff)
downloadserenity-8ffa860bc3cb45673c3cb7fb617259df60c987da.zip
AK: Add invert_case() and invert_case(StringView)
In the given String, invert_case() swaps lowercase characters with uppercase ones and vice versa.
Diffstat (limited to 'AK')
-rw-r--r--AK/String.cpp5
-rw-r--r--AK/String.h1
-rw-r--r--AK/StringUtils.cpp14
-rw-r--r--AK/StringUtils.h1
4 files changed, 21 insertions, 0 deletions
diff --git a/AK/String.cpp b/AK/String.cpp
index c5e6f7965c..a74dec2c41 100644
--- a/AK/String.cpp
+++ b/AK/String.cpp
@@ -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;
diff --git a/AK/String.h b/AK/String.h
index 674cfdb6cb..2c73bdc838 100644
--- a/AK/String.h
+++ b/AK/String.h
@@ -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); }
diff --git a/AK/StringUtils.cpp b/AK/StringUtils.cpp
index 2c66ec68d5..8e7a4bc646 100644
--- a/AK/StringUtils.cpp
+++ b/AK/StringUtils.cpp
@@ -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())
diff --git a/AK/StringUtils.h b/AK/StringUtils.h
index 9ba0012041..0e8470b4d5 100644
--- a/AK/StringUtils.h
+++ b/AK/StringUtils.h
@@ -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);