From 53afdc0106f1ebe0de424c8f468241e592d716b8 Mon Sep 17 00:00:00 2001 From: Lenny Maiorani Date: Thu, 14 Jan 2021 15:48:01 -0700 Subject: [PATCH] StringView: Implement `find_first_of` in terms of `AK::find` Problem: - The implementation of `find_first_of` is coupled to the implementation of `StringView`. Solution: - Decouple the implementation of `find_first_of` from the class by using a generic `find` algorithm. --- AK/StringView.cpp | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/AK/StringView.cpp b/AK/StringView.cpp index 0c7679d42f..a27771a19d 100644 --- a/AK/StringView.cpp +++ b/AK/StringView.cpp @@ -24,7 +24,9 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include #include +#include #include #include #include @@ -270,21 +272,23 @@ bool StringView::operator==(const String& string) const Optional StringView::find_first_of(char c) const { - for (size_t pos = 0; pos < m_length; ++pos) { - if (m_characters[pos] == c) - return pos; + if (const auto location = AK::find(begin(), end(), c); location != end()) { + return location.index(); } return {}; } Optional StringView::find_first_of(const StringView& view) const { - for (size_t pos = 0; pos < m_length; ++pos) { - char c = m_characters[pos]; - for (char view_char : view) { - if (c == view_char) - return pos; - } + if (const auto location = AK::find_if(begin(), end(), + [&](const auto c) { + return AK::any_of(view.begin(), view.end(), + [&](const auto view_char) { + return c == view_char; + }); + }); + location != end()) { + return location.index(); } return {}; }