mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 06:57:45 +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:
parent
875a2cbb71
commit
de395a3df2
12 changed files with 36 additions and 30 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -127,13 +127,12 @@ public:
|
|||
[[nodiscard]] bool equals_ignoring_case(const StringView&) const;
|
||||
|
||||
[[nodiscard]] bool contains(const StringView&, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
|
||||
[[nodiscard]] Optional<size_t> index_of(const String&, size_t start = 0) const;
|
||||
|
||||
[[nodiscard]] Vector<String> split_limit(char separator, size_t limit, bool keep_empty = false) const;
|
||||
[[nodiscard]] Vector<String> split(char separator, bool keep_empty = false) const;
|
||||
|
||||
[[nodiscard]] Optional<size_t> find(char) const;
|
||||
[[nodiscard]] Optional<size_t> find(const StringView&) const;
|
||||
[[nodiscard]] Optional<size_t> find(char, size_t start = 0) const;
|
||||
[[nodiscard]] Optional<size_t> find(StringView const&, size_t start = 0) const;
|
||||
|
||||
[[nodiscard]] String substring(size_t start) const;
|
||||
[[nodiscard]] String substring(size_t start, size_t length) const;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue