mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 15:10:19 +00:00
AK: Add Utf8View::for_each_split_view() method
Returns one Utf8View at a time, using a callback function to identify code points to split on.
This commit is contained in:
parent
ec5101a1d3
commit
3f10a5701d
Notes:
github-actions[bot]
2024-11-15 22:27:09 +00:00
Author: https://github.com/AtkinsSJ Commit: https://github.com/LadybirdBrowser/ladybird/commit/3f10a5701d9 Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2368
2 changed files with 70 additions and 0 deletions
|
@ -10,6 +10,7 @@
|
||||||
#include <AK/ByteString.h>
|
#include <AK/ByteString.h>
|
||||||
#include <AK/Debug.h>
|
#include <AK/Debug.h>
|
||||||
#include <AK/Format.h>
|
#include <AK/Format.h>
|
||||||
|
#include <AK/Function.h>
|
||||||
#include <AK/StringView.h>
|
#include <AK/StringView.h>
|
||||||
#include <AK/Types.h>
|
#include <AK/Types.h>
|
||||||
|
|
||||||
|
@ -139,6 +140,52 @@ public:
|
||||||
|
|
||||||
bool validate(size_t& valid_bytes, AllowSurrogates allow_surrogates = AllowSurrogates::Yes) const;
|
bool validate(size_t& valid_bytes, AllowSurrogates allow_surrogates = AllowSurrogates::Yes) const;
|
||||||
|
|
||||||
|
template<typename Callback>
|
||||||
|
auto for_each_split_view(Function<bool(u32)> splitter, SplitBehavior split_behavior, Callback callback) const
|
||||||
|
{
|
||||||
|
bool keep_empty = has_flag(split_behavior, SplitBehavior::KeepEmpty);
|
||||||
|
bool keep_trailing_separator = has_flag(split_behavior, SplitBehavior::KeepTrailingSeparator);
|
||||||
|
|
||||||
|
auto start_offset = 0u;
|
||||||
|
auto offset = 0u;
|
||||||
|
|
||||||
|
auto run_callback = [&]() {
|
||||||
|
auto length = offset - start_offset;
|
||||||
|
|
||||||
|
if (length == 0 && !keep_empty)
|
||||||
|
return;
|
||||||
|
|
||||||
|
auto substring = unicode_substring_view(start_offset, length);
|
||||||
|
|
||||||
|
// Reject splitter-only entries if we're not keeping empty results
|
||||||
|
if (keep_trailing_separator && !keep_empty && length == 1 && splitter(*substring.begin()))
|
||||||
|
return;
|
||||||
|
|
||||||
|
callback(substring);
|
||||||
|
};
|
||||||
|
|
||||||
|
auto iterator = begin();
|
||||||
|
while (iterator != end()) {
|
||||||
|
if (splitter(*iterator)) {
|
||||||
|
if (keep_trailing_separator)
|
||||||
|
++offset;
|
||||||
|
|
||||||
|
run_callback();
|
||||||
|
|
||||||
|
if (!keep_trailing_separator)
|
||||||
|
++offset;
|
||||||
|
|
||||||
|
start_offset = offset;
|
||||||
|
++iterator;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
++offset;
|
||||||
|
++iterator;
|
||||||
|
}
|
||||||
|
run_callback();
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class Utf8CodePointIterator;
|
friend class Utf8CodePointIterator;
|
||||||
|
|
||||||
|
|
|
@ -312,3 +312,26 @@ TEST_CASE(trim)
|
||||||
EXPECT_EQ(view.trim(whitespace, TrimMode::Right).as_string(), "\u180E");
|
EXPECT_EQ(view.trim(whitespace, TrimMode::Right).as_string(), "\u180E");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool is_period(u32 code_point) { return code_point == '.'; }
|
||||||
|
|
||||||
|
TEST_CASE(for_each_split_view)
|
||||||
|
{
|
||||||
|
Utf8View view { "...Well..hello.friends!..."sv };
|
||||||
|
auto gather = [&](auto split_behavior) {
|
||||||
|
Vector<StringView> results;
|
||||||
|
view.for_each_split_view(is_period, split_behavior, [&](auto part) {
|
||||||
|
results.append(part.as_string());
|
||||||
|
});
|
||||||
|
return results;
|
||||||
|
};
|
||||||
|
|
||||||
|
EXPECT_EQ(gather(SplitBehavior::Nothing),
|
||||||
|
Vector({ "Well"sv, "hello"sv, "friends!"sv }));
|
||||||
|
EXPECT_EQ(gather(SplitBehavior::KeepEmpty),
|
||||||
|
Vector({ ""sv, ""sv, ""sv, "Well"sv, ""sv, "hello"sv, "friends!"sv, ""sv, ""sv, ""sv }));
|
||||||
|
EXPECT_EQ(gather(SplitBehavior::KeepTrailingSeparator),
|
||||||
|
Vector({ "Well."sv, "hello."sv, "friends!."sv }));
|
||||||
|
EXPECT_EQ(gather(SplitBehavior::KeepEmpty | SplitBehavior::KeepTrailingSeparator),
|
||||||
|
Vector({ "."sv, "."sv, "."sv, "Well."sv, "."sv, "hello."sv, "friends!."sv, "."sv, "."sv, ""sv }));
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue