summaryrefslogtreecommitdiff
path: root/AK
diff options
context:
space:
mode:
authorIdan Horowitz <idan.horowitz@gmail.com>2021-06-16 14:17:03 +0300
committerLinus Groh <mail@linusgroh.de>2021-06-16 20:05:18 +0100
commitfea6d952a48c02d8cc7350fdfc440d1f3fc94fc9 (patch)
tree1ee7809c3ea572d8444300826bed948082ffbc0e /AK
parent8ba05331387773571cc6d92bd080012c73516550 (diff)
downloadserenity-fea6d952a48c02d8cc7350fdfc440d1f3fc94fc9.zip
AK: Add the Utf8View::{contains, trim} helper methods
Diffstat (limited to 'AK')
-rw-r--r--AK/Utf8View.cpp41
-rw-r--r--AK/Utf8View.h3
2 files changed, 44 insertions, 0 deletions
diff --git a/AK/Utf8View.cpp b/AK/Utf8View.cpp
index 9023cc50da..1481ae9d68 100644
--- a/AK/Utf8View.cpp
+++ b/AK/Utf8View.cpp
@@ -173,6 +173,47 @@ bool Utf8View::starts_with(const Utf8View& start) const
return true;
}
+bool Utf8View::contains(u32 needle) const
+{
+ for (u32 code_point : *this) {
+ if (code_point == needle)
+ return true;
+ }
+ return false;
+}
+
+Utf8View Utf8View::trim(const Utf8View& characters, TrimMode mode) const
+{
+ size_t substring_start = 0;
+ size_t substring_length = length();
+
+ if (mode == TrimMode::Left || mode == TrimMode::Both) {
+ for (auto code_point : *this) {
+ if (substring_length == 0)
+ return {};
+ if (!characters.contains(code_point))
+ break;
+ ++substring_start;
+ --substring_length;
+ }
+ }
+
+ if (mode == TrimMode::Right || mode == TrimMode::Both) {
+ size_t seen_whitespace_length = 0;
+ for (auto code_point : *this) {
+ if (characters.contains(code_point))
+ seen_whitespace_length++;
+ else
+ seen_whitespace_length = 0;
+ }
+ if (seen_whitespace_length >= substring_length)
+ return {};
+ substring_length -= seen_whitespace_length;
+ }
+
+ return substring_view(substring_start, substring_length);
+}
+
Utf8CodePointIterator::Utf8CodePointIterator(const unsigned char* ptr, size_t length)
: m_ptr(ptr)
, m_length(length)
diff --git a/AK/Utf8View.h b/AK/Utf8View.h
index 9e96e1e000..e8afc590a6 100644
--- a/AK/Utf8View.h
+++ b/AK/Utf8View.h
@@ -75,6 +75,9 @@ public:
bool is_empty() const { return m_string.is_empty(); }
bool starts_with(const Utf8View&) const;
+ bool contains(u32) const;
+
+ Utf8View trim(const Utf8View& characters, TrimMode mode = TrimMode::Both) const;
size_t iterator_offset(const Utf8CodePointIterator& it) const
{