mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 23:20:20 +00:00
AK: Add the Utf8View::{contains, trim} helper methods
This commit is contained in:
parent
8ba0533138
commit
fea6d952a4
Notes:
sideshowbarker
2024-07-18 12:10:44 +09:00
Author: https://github.com/IdanHo Commit: https://github.com/SerenityOS/serenity/commit/fea6d952a48 Pull-request: https://github.com/SerenityOS/serenity/pull/8095 Reviewed-by: https://github.com/linusg
2 changed files with 44 additions and 0 deletions
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue