diff options
author | Andreas Kling <kling@serenityos.org> | 2021-11-10 11:05:21 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-11-10 21:58:58 +0100 |
commit | 5f7d008791f9e358638283dc2f0d709a601344ff (patch) | |
tree | 4453407e376e23e649857a0d37564c4f61ab40de /AK/StringView.cpp | |
parent | e52f987020be996f49c09dfdd53306366f2cbab9 (diff) | |
download | serenity-5f7d008791f9e358638283dc2f0d709a601344ff.zip |
AK+Everywhere: Stop including Vector.h from StringView.h
Preparation for using Error.h from Vector.h. This required moving some
things out of line.
Diffstat (limited to 'AK/StringView.cpp')
-rw-r--r-- | AK/StringView.cpp | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/AK/StringView.cpp b/AK/StringView.cpp index 205f282d70..3bd40ac06b 100644 --- a/AK/StringView.cpp +++ b/AK/StringView.cpp @@ -8,9 +8,11 @@ #include <AK/ByteBuffer.h> #include <AK/Find.h> #include <AK/FlyString.h> +#include <AK/Function.h> #include <AK/Memory.h> #include <AK/String.h> #include <AK/StringView.h> +#include <AK/Vector.h> namespace AK { @@ -252,4 +254,31 @@ String StringView::replace(const StringView& needle, const StringView& replaceme return StringUtils::replace(*this, needle, replacement, all_occurrences); } +Vector<size_t> StringView::find_all(StringView needle) const +{ + return StringUtils::find_all(*this, needle); +} + +Vector<StringView> StringView::split_view_if(Function<bool(char)> const& predicate, bool keep_empty) const +{ + if (is_empty()) + return {}; + + Vector<StringView> v; + size_t substart = 0; + 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)); + substart = i + 1; + } + } + size_t taillen = length() - substart; + if (taillen != 0 || keep_empty) + v.append(substring_view(substart, taillen)); + return v; +} + } |