mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 23:20:20 +00:00
AK: Make String::count not use strstr and take a StringView
This was needlessly copying StringView arguments, and was also using strstr internally, which meant it was doing a bunch of unnecessary strlen calls on it. This also moves the implementation to StringUtils to allow API consistency between String and StringView.
This commit is contained in:
parent
4e40eaf34c
commit
6d2b003b6e
Notes:
sideshowbarker
2024-07-18 04:15:09 +09:00
Author: https://github.com/IdanHo Commit: https://github.com/SerenityOS/serenity/commit/6d2b003b6e6 Pull-request: https://github.com/SerenityOS/serenity/pull/9948 Reviewed-by: https://github.com/alimpfard Reviewed-by: https://github.com/bgianfo ✅
5 changed files with 19 additions and 17 deletions
|
@ -382,22 +382,6 @@ int String::replace(const String& needle, const String& replacement, bool all_oc
|
|||
return positions.size();
|
||||
}
|
||||
|
||||
size_t String::count(const String& needle) const
|
||||
{
|
||||
size_t count = 0;
|
||||
size_t start = 0, pos;
|
||||
for (;;) {
|
||||
const char* ptr = strstr(characters() + start, needle.characters());
|
||||
if (!ptr)
|
||||
break;
|
||||
|
||||
pos = ptr - characters();
|
||||
count++;
|
||||
start = pos + 1;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
String String::reverse() const
|
||||
{
|
||||
StringBuilder reversed_string(length());
|
||||
|
|
|
@ -286,7 +286,7 @@ public:
|
|||
}
|
||||
|
||||
int replace(const String& needle, const String& replacement, bool all_occurrences = false);
|
||||
[[nodiscard]] size_t count(const String& needle) const;
|
||||
[[nodiscard]] size_t count(StringView const& needle) const { return StringUtils::count(*this, needle); }
|
||||
[[nodiscard]] String reverse() const;
|
||||
|
||||
template<typename... Ts>
|
||||
|
|
|
@ -427,6 +427,20 @@ String to_titlecase(StringView const& str)
|
|||
return builder.to_string();
|
||||
}
|
||||
|
||||
// TODO: Benchmark against KMP (AK/MemMem.h) and switch over if it's faster for short strings too
|
||||
size_t count(StringView const& str, StringView const& needle)
|
||||
{
|
||||
if (needle.is_empty())
|
||||
return str.length();
|
||||
|
||||
size_t count = 0;
|
||||
for (size_t i = 0; i < str.length() - needle.length() + 1; ++i) {
|
||||
if (str.substring_view(i).starts_with(needle))
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -71,6 +71,8 @@ Optional<size_t> find_any_of(StringView const& haystack, StringView const& needl
|
|||
String to_snakecase(const StringView&);
|
||||
String to_titlecase(StringView const&);
|
||||
|
||||
size_t count(StringView const&, StringView const& needle);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -220,6 +220,8 @@ public:
|
|||
|
||||
[[nodiscard]] bool is_whitespace() const { return StringUtils::is_whitespace(*this); }
|
||||
|
||||
[[nodiscard]] size_t count(StringView const& needle) const { return StringUtils::count(*this, needle); }
|
||||
|
||||
template<typename... Ts>
|
||||
[[nodiscard]] ALWAYS_INLINE constexpr bool is_one_of(Ts&&... strings) const
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue