summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2023-01-17 11:30:10 -0500
committerLinus Groh <mail@linusgroh.de>2023-01-18 14:43:40 +0000
commit537fcaf59ee05f8dbdf22716a4b3c5879a6ef25e (patch)
treecb294fbc7a943f47e34772691a4e3cd423103d5f /Userland
parent8f2589b3b07470ebe5773c6e719ede169d894a93 (diff)
downloadserenity-537fcaf59ee05f8dbdf22716a4b3c5879a6ef25e.zip
AK+LibUnicode: Provide Unicode-aware caseless String matching
The Unicode spec defines much more complicated caseless matching algorithms in its Collation spec. This implements the "basic" case folding comparison.
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibUnicode/String.cpp15
1 files changed, 15 insertions, 0 deletions
diff --git a/Userland/Libraries/LibUnicode/String.cpp b/Userland/Libraries/LibUnicode/String.cpp
index 5251d310bd..e198058a70 100644
--- a/Userland/Libraries/LibUnicode/String.cpp
+++ b/Userland/Libraries/LibUnicode/String.cpp
@@ -33,4 +33,19 @@ ErrorOr<String> String::to_titlecase(Optional<StringView> const& locale) const
return builder.to_string();
}
+ErrorOr<String> String::to_casefold() const
+{
+ StringBuilder builder;
+ TRY(Unicode::Detail::build_casefold_string(code_points(), builder));
+ return builder.to_string();
+}
+
+// https://www.unicode.org/versions/Unicode15.0.0/ch03.pdf#G34145
+ErrorOr<bool> String::equals_ignoring_case(String const& other) const
+{
+ // A string X is a caseless match for a string Y if and only if:
+ // toCasefold(X) = toCasefold(Y)
+ return TRY(to_casefold()) == TRY(other.to_casefold());
+}
+
}