AK: Reimplement StringView::find methods in StringUtils

This patch reimplements the StringView::find methods in StringUtils, so
they can also be used by String. The methods now also take an optional
start parameter, which moves their API in line with String's respective
methods.

This also implements a StringView::find_ast(char) method, which is
currently functionally equivalent to find_last_of(char). This is because
find_last_of(char) will be removed in a further commit.
This commit is contained in:
Max Wipfli 2021-07-01 14:58:37 +02:00 committed by Andreas Kling
parent 3ea65200d8
commit 56253bf389
Notes: sideshowbarker 2024-07-18 11:06:21 +09:00
5 changed files with 37 additions and 20 deletions

View file

@ -483,10 +483,7 @@ Optional<size_t> String::find(char c, size_t start) const
Optional<size_t> String::find(StringView const& view, size_t start) const
{
auto index = StringUtils::find(substring_view(start), view);
if (!index.has_value())
return {};
return index.value() + start;
return StringUtils::find(*this, view, start);
}
}

View file

@ -332,11 +332,34 @@ StringView trim_whitespace(const StringView& str, TrimMode mode)
return trim(str, " \n\t\v\f\r", mode);
}
Optional<size_t> find(const StringView& haystack, const StringView& needle)
Optional<size_t> find(StringView const& haystack, char needle, size_t start)
{
return AK::memmem_optional(
haystack.characters_without_null_termination(), haystack.length(),
if (start >= haystack.length())
return {};
for (size_t i = start; i < haystack.length(); ++i) {
if (haystack[i] == needle)
return i;
}
return {};
}
Optional<size_t> find(StringView const& haystack, StringView const& needle, size_t start)
{
if (start > haystack.length())
return {};
auto index = AK::memmem_optional(
haystack.characters_without_null_termination() + start, haystack.length() - start,
needle.characters_without_null_termination(), needle.length());
return index.has_value() ? (*index + start) : index;
}
Optional<size_t> find_last(StringView const& haystack, char needle)
{
for (size_t i = haystack.length(); i > 0; --i) {
if (haystack[i - 1] == needle)
return i - 1;
}
return {};
}
String to_snakecase(const StringView& str)

View file

@ -57,7 +57,12 @@ bool contains(const StringView&, const StringView&, CaseSensitivity);
bool is_whitespace(const StringView&);
StringView trim(const StringView& string, const StringView& characters, TrimMode mode);
StringView trim_whitespace(const StringView& string, TrimMode mode);
Optional<size_t> find(const StringView& haystack, const StringView& needle);
Optional<size_t> find(StringView const& haystack, char needle, size_t start = 0);
Optional<size_t> find(StringView const& haystack, StringView const& needle, size_t start = 0);
Optional<size_t> find_last(StringView const& haystack, char needle);
Vector<size_t> find_all(StringView const& haystack, StringView const& needle);
String to_snakecase(const StringView&);
}

View file

@ -282,16 +282,6 @@ Optional<size_t> StringView::find_last_of(const StringView& view) const
return {};
}
Optional<size_t> StringView::find(char c) const
{
return find(StringView { &c, 1 });
}
Optional<size_t> StringView::find(const StringView& view) const
{
return StringUtils::find(*this, view);
}
String StringView::to_string() const { return String { *this }; }
}

View file

@ -92,8 +92,10 @@ public:
Optional<size_t> find_last_of(char) const;
Optional<size_t> find_last_of(const StringView&) const;
Optional<size_t> find(const StringView&) const;
Optional<size_t> find(char c) const;
[[nodiscard]] Optional<size_t> find(char needle, size_t start = 0) const { return StringUtils::find(*this, needle, start); }
[[nodiscard]] Optional<size_t> find(StringView const& needle, size_t start = 0) const { return StringUtils::find(*this, needle, start); }
[[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]] constexpr StringView substring_view(size_t start, size_t length) const
{