mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 07:30:19 +00:00
AK: Allow propagating errors from StringView::for_each_split_view()
This commit is contained in:
parent
b16ec1880c
commit
d81a72ed78
Notes:
sideshowbarker
2024-07-17 11:30:54 +09:00
Author: https://github.com/MacDue Commit: https://github.com/SerenityOS/serenity/commit/d81a72ed78 Pull-request: https://github.com/SerenityOS/serenity/pull/17309 Reviewed-by: https://github.com/timschumi Reviewed-by: https://github.com/trflynn89
1 changed files with 39 additions and 26 deletions
|
@ -164,40 +164,53 @@ public:
|
||||||
return substring_view(0, needle_begin.release_value());
|
return substring_view(0, needle_begin.release_value());
|
||||||
}
|
}
|
||||||
|
|
||||||
template<VoidFunction<StringView> Callback>
|
template<typename Callback>
|
||||||
void for_each_split_view(char separator, SplitBehavior split_behavior, Callback callback) const
|
auto for_each_split_view(char separator, SplitBehavior split_behavior, Callback callback) const
|
||||||
{
|
{
|
||||||
StringView seperator_view { &separator, 1 };
|
StringView seperator_view { &separator, 1 };
|
||||||
for_each_split_view(seperator_view, split_behavior, callback);
|
return for_each_split_view(seperator_view, split_behavior, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<VoidFunction<StringView> Callback>
|
template<typename Callback>
|
||||||
void for_each_split_view(StringView separator, SplitBehavior split_behavior, Callback callback) const
|
auto for_each_split_view(StringView separator, SplitBehavior split_behavior, Callback callback) const
|
||||||
{
|
{
|
||||||
VERIFY(!separator.is_empty());
|
VERIFY(!separator.is_empty());
|
||||||
|
// FIXME: This can't go in the template header since declval won't allow the incomplete StringView type.
|
||||||
|
using CallbackReturn = decltype(declval<Callback>()(StringView {}));
|
||||||
|
constexpr auto ReturnsErrorOr = IsSpecializationOf<CallbackReturn, ErrorOr>;
|
||||||
|
using ReturnType = Conditional<ReturnsErrorOr, ErrorOr<void>, void>;
|
||||||
|
return [&]() -> ReturnType {
|
||||||
|
if (is_empty())
|
||||||
|
return ReturnType();
|
||||||
|
|
||||||
if (is_empty())
|
StringView view { *this };
|
||||||
return;
|
auto maybe_separator_index = find(separator);
|
||||||
|
bool keep_empty = has_flag(split_behavior, SplitBehavior::KeepEmpty);
|
||||||
StringView view { *this };
|
bool keep_separator = has_flag(split_behavior, SplitBehavior::KeepTrailingSeparator);
|
||||||
|
while (maybe_separator_index.has_value()) {
|
||||||
auto maybe_separator_index = find(separator);
|
auto separator_index = maybe_separator_index.value();
|
||||||
bool keep_empty = has_flag(split_behavior, SplitBehavior::KeepEmpty);
|
auto part_with_separator = view.substring_view(0, separator_index + separator.length());
|
||||||
bool keep_separator = has_flag(split_behavior, SplitBehavior::KeepTrailingSeparator);
|
if (keep_empty || separator_index > 0) {
|
||||||
while (maybe_separator_index.has_value()) {
|
auto part = part_with_separator;
|
||||||
auto separator_index = maybe_separator_index.value();
|
if (!keep_separator)
|
||||||
auto part_with_separator = view.substring_view(0, separator_index + separator.length());
|
part = part_with_separator.substring_view(0, separator_index);
|
||||||
if (keep_empty || separator_index > 0) {
|
if constexpr (ReturnsErrorOr)
|
||||||
if (keep_separator)
|
TRY(callback(part));
|
||||||
callback(part_with_separator);
|
else
|
||||||
else
|
callback(part);
|
||||||
callback(part_with_separator.substring_view(0, separator_index));
|
}
|
||||||
|
view = view.substring_view_starting_after_substring(part_with_separator);
|
||||||
|
maybe_separator_index = view.find(separator);
|
||||||
}
|
}
|
||||||
view = view.substring_view_starting_after_substring(part_with_separator);
|
if (keep_empty || !view.is_empty()) {
|
||||||
maybe_separator_index = view.find(separator);
|
if constexpr (ReturnsErrorOr)
|
||||||
}
|
TRY(callback(view));
|
||||||
if (keep_empty || !view.is_empty())
|
else
|
||||||
callback(view);
|
callback(view);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ReturnType();
|
||||||
|
}();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a Vector of StringViews split by line endings. As of CommonMark
|
// Create a Vector of StringViews split by line endings. As of CommonMark
|
||||||
|
|
Loading…
Reference in a new issue