mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-08 05:27:14 +09:00
AK: Add SplitBehavior::KeepTrailingSeparator with tests
This commit is contained in:
parent
3e8b5ac920
commit
7c33f8f7df
Notes:
sideshowbarker
2024-07-17 05:08:08 +09:00
Author: https://github.com/demostanis
Commit: 7c33f8f7df
Pull-request: https://github.com/SerenityOS/serenity/pull/15750
Reviewed-by: https://github.com/AtkinsSJ
Reviewed-by: https://github.com/davidot
Reviewed-by: https://github.com/linusg ✅
5 changed files with 26 additions and 6 deletions
|
@ -272,12 +272,13 @@ Vector<StringView> StringView::split_view_if(Function<bool(char)> const& predica
|
|||
Vector<StringView> v;
|
||||
size_t substart = 0;
|
||||
bool keep_empty = has_flag(split_behavior, SplitBehavior::KeepEmpty);
|
||||
bool keep_separator = has_flag(split_behavior, SplitBehavior::KeepTrailingSeparator);
|
||||
for (size_t i = 0; i < length(); ++i) {
|
||||
char ch = characters_without_null_termination()[i];
|
||||
if (predicate(ch)) {
|
||||
size_t sublen = i - substart;
|
||||
if (sublen != 0 || keep_empty)
|
||||
v.append(substring_view(substart, sublen));
|
||||
v.append(substring_view(substart, keep_separator ? sublen + 1 : sublen));
|
||||
substart = i + 1;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue