1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 12:38:12 +00:00

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.
This commit is contained in:
Lenny Maiorani 2021-01-14 15:48:01 -07:00 committed by Andreas Kling
parent d11727ff28
commit 53afdc0106

View file

@ -24,7 +24,9 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#include <AK/AnyOf.h>
#include <AK/ByteBuffer.h> #include <AK/ByteBuffer.h>
#include <AK/Find.h>
#include <AK/FlyString.h> #include <AK/FlyString.h>
#include <AK/Memory.h> #include <AK/Memory.h>
#include <AK/String.h> #include <AK/String.h>
@ -270,21 +272,23 @@ bool StringView::operator==(const String& string) const
Optional<size_t> StringView::find_first_of(char c) const Optional<size_t> StringView::find_first_of(char c) const
{ {
for (size_t pos = 0; pos < m_length; ++pos) { if (const auto location = AK::find(begin(), end(), c); location != end()) {
if (m_characters[pos] == c) return location.index();
return pos;
} }
return {}; return {};
} }
Optional<size_t> StringView::find_first_of(const StringView& view) const Optional<size_t> StringView::find_first_of(const StringView& view) const
{ {
for (size_t pos = 0; pos < m_length; ++pos) { if (const auto location = AK::find_if(begin(), end(),
char c = m_characters[pos]; [&](const auto c) {
for (char view_char : view) { return AK::any_of(view.begin(), view.end(),
if (c == view_char) [&](const auto view_char) {
return pos; return c == view_char;
} });
});
location != end()) {
return location.index();
} }
return {}; return {};
} }