1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 21:57:35 +00:00

AK: Implement String::find_any_of() and StringView::find_any_of()

This implements StringUtils::find_any_of() and uses it in
String::find_any_of() and StringView::find_any_of(). All uses of
find_{first,last}_of have been replaced with find_any_of(), find() or
find_last(). find_{first,last}_of have subsequently been removed.
This commit is contained in:
Max Wipfli 2021-07-01 18:12:21 +02:00 committed by Andreas Kling
parent 17eddf3ac4
commit 9cc35d1ba3
12 changed files with 44 additions and 52 deletions

View file

@ -147,6 +147,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); }
using SearchDirection = StringUtils::SearchDirection;
[[nodiscard]] Optional<size_t> find_any_of(StringView const& needles, SearchDirection direction) const { return StringUtils::find_any_of(*this, needles, direction); }
[[nodiscard]] String substring(size_t start, size_t length) const;
[[nodiscard]] String substring(size_t start) const;

View file

@ -378,6 +378,24 @@ Vector<size_t> find_all(StringView const& haystack, StringView const& needle)
return positions;
}
Optional<size_t> find_any_of(StringView const& haystack, StringView const& needles, SearchDirection direction)
{
if (haystack.is_empty() || needles.is_empty())
return {};
if (direction == SearchDirection::Forward) {
for (size_t i = 0; i < haystack.length(); ++i) {
if (needles.contains(haystack[i]))
return i;
}
} else if (direction == SearchDirection::Backward) {
for (size_t i = haystack.length(); i > 0; --i) {
if (needles.contains(haystack[i - 1]))
return i - 1;
}
}
return {};
}
String to_snakecase(const StringView& str)
{
auto should_insert_underscore = [&](auto i, auto current_char) {

View file

@ -62,6 +62,11 @@ 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);
enum class SearchDirection {
Forward,
Backward
};
Optional<size_t> find_any_of(StringView const& haystack, StringView const& needles, SearchDirection);
String to_snakecase(const StringView&);

View file

@ -238,33 +238,6 @@ bool StringView::operator==(const String& string) const
return !__builtin_memcmp(m_characters, string.characters(), m_length);
}
Optional<size_t> StringView::find_first_of(const StringView& view) const
{
if (const auto location = AK::find_if(begin(), end(),
[&](const auto c) {
return any_of(view.begin(), view.end(),
[&](const auto view_char) {
return c == view_char;
});
});
location != end()) {
return location.index();
}
return {};
}
Optional<size_t> StringView::find_last_of(const StringView& view) const
{
for (size_t pos = m_length; pos != 0; --pos) {
char c = m_characters[pos - 1];
for (char view_char : view) {
if (c == view_char)
return pos - 1;
}
}
return {};
}
String StringView::to_string() const { return String { *this }; }
}

View file

@ -93,8 +93,8 @@ public:
[[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;
using SearchDirection = StringUtils::SearchDirection;
[[nodiscard]] Optional<size_t> find_any_of(StringView const& needles, SearchDirection direction = SearchDirection::Forward) { return StringUtils::find_any_of(*this, needles, direction); }
[[nodiscard]] constexpr StringView substring_view(size_t start, size_t length) const
{