mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 07:30:19 +00:00
AK: Implement StringView::find_all()
This implements the StringView::find_all() method by re-implemeting the current method existing for String in StringUtils, and using that implementation for both String and StringView. The rewrite uses memmem() instead of strstr(), so the String::find_all() argument type has been changed from String to StringView, as the null byte is no longer required.
This commit is contained in:
parent
3bdaed501e
commit
d7a104c27c
Notes:
sideshowbarker
2024-07-18 11:06:14 +09:00
Author: https://github.com/MaxWipfli Commit: https://github.com/SerenityOS/serenity/commit/d7a104c27c0 Pull-request: https://github.com/SerenityOS/serenity/pull/8368 Reviewed-by: https://github.com/alimpfard Reviewed-by: https://github.com/trflynn89
4 changed files with 19 additions and 18 deletions
|
@ -293,23 +293,6 @@ bool String::equals_ignoring_case(const StringView& other) const
|
|||
return StringUtils::equals_ignoring_case(view(), other);
|
||||
}
|
||||
|
||||
Vector<size_t> String::find_all(const String& needle) const
|
||||
{
|
||||
Vector<size_t> positions;
|
||||
size_t start = 0, pos;
|
||||
for (;;) {
|
||||
const char* ptr = strstr(characters() + start, needle.characters());
|
||||
if (!ptr)
|
||||
break;
|
||||
|
||||
pos = ptr - characters();
|
||||
positions.append(pos);
|
||||
|
||||
start = pos + 1;
|
||||
}
|
||||
return positions;
|
||||
}
|
||||
|
||||
int String::replace(const String& needle, const String& replacement, bool all_occurrences)
|
||||
{
|
||||
if (is_empty())
|
||||
|
|
|
@ -143,6 +143,7 @@ public:
|
|||
|
||||
[[nodiscard]] Optional<size_t> find(char, size_t start = 0) const;
|
||||
[[nodiscard]] Optional<size_t> find(StringView const&, size_t start = 0) const;
|
||||
[[nodiscard]] Vector<size_t> find_all(StringView const& needle) const { return StringUtils::find_all(*this, needle); }
|
||||
|
||||
[[nodiscard]] String substring(size_t start) const;
|
||||
[[nodiscard]] String substring(size_t start, size_t length) const;
|
||||
|
@ -279,7 +280,6 @@ public:
|
|||
|
||||
int replace(const String& needle, const String& replacement, bool all_occurrences = false);
|
||||
size_t count(const String& needle) const;
|
||||
Vector<size_t> find_all(const String& needle) const;
|
||||
[[nodiscard]] String reverse() const;
|
||||
|
||||
template<typename... Ts>
|
||||
|
|
|
@ -362,6 +362,22 @@ Optional<size_t> find_last(StringView const& haystack, char needle)
|
|||
return {};
|
||||
}
|
||||
|
||||
Vector<size_t> find_all(StringView const& haystack, StringView const& needle)
|
||||
{
|
||||
Vector<size_t> positions;
|
||||
size_t current_position = 0;
|
||||
while (current_position <= haystack.length()) {
|
||||
auto maybe_position = AK::memmem_optional(
|
||||
haystack.characters_without_null_termination() + current_position, haystack.length() - current_position,
|
||||
needle.characters_without_null_termination(), needle.length());
|
||||
if (!maybe_position.has_value())
|
||||
break;
|
||||
positions.append(current_position + *maybe_position);
|
||||
current_position += *maybe_position + 1;
|
||||
}
|
||||
return positions;
|
||||
}
|
||||
|
||||
String to_snakecase(const StringView& str)
|
||||
{
|
||||
auto should_insert_underscore = [&](auto i, auto current_char) {
|
||||
|
|
|
@ -91,6 +91,8 @@ public:
|
|||
[[nodiscard]] Optional<size_t> find_last(char needle) const { return StringUtils::find_last(*this, needle); }
|
||||
// FIXME: Implement find_last(StringView const&) for API symmetry.
|
||||
|
||||
[[nodiscard]] Vector<size_t> find_all(StringView const& needle) const { return StringUtils::find_all(*this, needle); }
|
||||
|
||||
[[nodiscard]] Optional<size_t> find_first_of(StringView const&) const;
|
||||
[[nodiscard]] Optional<size_t> find_last_of(StringView const&) const;
|
||||
|
||||
|
|
Loading…
Reference in a new issue