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

AK+Everywhere: Consolidate String::index_of() and String::find()

We had two functions for doing mostly the same thing. Combine both
of them into String::find() and use that everywhere.

Also add some tests to cover basic behavior.
This commit is contained in:
Andreas Kling 2021-05-24 11:50:46 +02:00
parent 875a2cbb71
commit de395a3df2
12 changed files with 36 additions and 30 deletions

View file

@ -286,18 +286,6 @@ bool String::contains(const StringView& needle, CaseSensitivity case_sensitivity
return StringUtils::contains(*this, needle, case_sensitivity);
}
Optional<size_t> String::index_of(const String& needle, size_t start) const
{
if (is_null() || needle.is_null())
return {};
const char* self_characters = characters();
const char* result = strstr(self_characters + start, needle.characters());
if (!result)
return {};
return Optional<size_t> { result - self_characters };
}
bool String::equals_ignoring_case(const StringView& other) const
{
return StringUtils::equals_ignoring_case(view(), other);
@ -491,14 +479,17 @@ String String::vformatted(StringView fmtstr, TypeErasedFormatParams params)
return builder.to_string();
}
Optional<size_t> String::find(char c) const
Optional<size_t> String::find(char c, size_t start) const
{
return find(StringView { &c, 1 });
return find(StringView { &c, 1 }, start);
}
Optional<size_t> String::find(const StringView& view) const
Optional<size_t> String::find(StringView const& view, size_t start) const
{
return StringUtils::find(*this, view);
auto index = StringUtils::find(substring_view(start), view);
if (!index.has_value())
return {};
return index.value() + start;
}
}