1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 04:47:35 +00:00

AK+Everywhere: Remove StringView::find_{first,last}_of(char) methods

This removes StringView::find_first_of(char) and find_last_of(char) and
replaces all its usages with find and find_last respectively. This is
because those two methods are functionally equivalent.
find_{first,last}_of should only be used if searching for multiple
different characters, which is never the case with the char argument.

This also adds the [[nodiscard]] to the remaining find_{first,last}_of
methods.
This commit is contained in:
Max Wipfli 2021-07-01 15:01:29 +02:00 committed by Andreas Kling
parent 56253bf389
commit 3bdaed501e
10 changed files with 41 additions and 68 deletions

View file

@ -29,7 +29,7 @@ LexicalPath::LexicalPath(String path)
m_parts = m_string.split_view('/');
auto last_slash_index = m_string.view().find_last_of('/');
auto last_slash_index = m_string.view().find_last('/');
if (!last_slash_index.has_value()) {
// The path contains a single part and is not absolute. m_dirname = "."sv
m_dirname = { &s_single_dot, 1 };
@ -47,7 +47,7 @@ LexicalPath::LexicalPath(String path)
m_basename = m_parts.last();
}
auto last_dot_index = m_basename.find_last_of('.');
auto last_dot_index = m_basename.find_last('.');
// NOTE: if the dot index is 0, this means we have ".foo", it's not an extension, as the title would then be "".
if (last_dot_index.has_value() && *last_dot_index != 0) {
m_title = m_basename.substring_view(0, *last_dot_index);

View file

@ -238,14 +238,6 @@ bool StringView::operator==(const String& string) const
return !__builtin_memcmp(m_characters, string.characters(), m_length);
}
Optional<size_t> StringView::find_first_of(char c) const
{
if (const auto location = AK::find(begin(), end(), c); location != end()) {
return location.index();
}
return {};
}
Optional<size_t> StringView::find_first_of(const StringView& view) const
{
if (const auto location = AK::find_if(begin(), end(),
@ -261,15 +253,6 @@ Optional<size_t> StringView::find_first_of(const StringView& view) const
return {};
}
Optional<size_t> StringView::find_last_of(char c) const
{
for (size_t pos = m_length; pos != 0; --pos) {
if (m_characters[pos - 1] == c)
return pos - 1;
}
return {};
}
Optional<size_t> StringView::find_last_of(const StringView& view) const
{
for (size_t pos = m_length; pos != 0; --pos) {

View file

@ -86,17 +86,14 @@ public:
[[nodiscard]] String to_lowercase_string() const;
[[nodiscard]] String to_uppercase_string() const;
Optional<size_t> find_first_of(char) const;
Optional<size_t> find_first_of(const StringView&) const;
Optional<size_t> find_last_of(char) const;
Optional<size_t> find_last_of(const StringView&) const;
[[nodiscard]] Optional<size_t> find(char needle, size_t start = 0) const { return StringUtils::find(*this, needle, start); }
[[nodiscard]] Optional<size_t> find(StringView const& needle, size_t start = 0) const { return StringUtils::find(*this, needle, start); }
[[nodiscard]] Optional<size_t> find_last(char needle) const { return StringUtils::find_last(*this, needle); }
// FIXME: Implement find_last(StringView const&) for API symmetry.
[[nodiscard]] Optional<size_t> find_first_of(StringView const&) const;
[[nodiscard]] Optional<size_t> find_last_of(StringView const&) const;
[[nodiscard]] constexpr StringView substring_view(size_t start, size_t length) const
{
if (!is_constant_evaluated())