diff --git a/AK/DeprecatedString.h b/AK/DeprecatedString.h index e5a4ed853dd..c126f2c21ac 100644 --- a/AK/DeprecatedString.h +++ b/AK/DeprecatedString.h @@ -158,7 +158,7 @@ public: [[nodiscard]] Optional find(char needle, size_t start = 0) const { return StringUtils::find(*this, needle, start); } [[nodiscard]] Optional find(StringView needle, size_t start = 0) const { return StringUtils::find(*this, needle, start); } [[nodiscard]] Optional find_last(char needle) const { return StringUtils::find_last(*this, needle); } - // FIXME: Implement find_last(StringView) for API symmetry. + [[nodiscard]] Optional find_last(StringView needle) const { return StringUtils::find_last(*this, needle); } Vector find_all(StringView needle) const; using SearchDirection = StringUtils::SearchDirection; [[nodiscard]] Optional find_any_of(StringView needles, SearchDirection direction) const { return StringUtils::find_any_of(*this, needles, direction); } diff --git a/AK/StringUtils.cpp b/AK/StringUtils.cpp index eba74f8ee09..d660b95f0f5 100644 --- a/AK/StringUtils.cpp +++ b/AK/StringUtils.cpp @@ -405,6 +405,17 @@ Optional find_last(StringView haystack, char needle) return {}; } +Optional find_last(StringView haystack, StringView needle) +{ + for (size_t i = haystack.length(); i > 0; --i) { + auto value = StringUtils::find(haystack, needle, i - 1); + if (value.has_value()) + return value; + } + + return {}; +} + Optional find_last_not(StringView haystack, char needle) { for (size_t i = haystack.length(); i > 0; --i) { diff --git a/AK/StringUtils.h b/AK/StringUtils.h index 173d8c941bf..8f7fb2f4905 100644 --- a/AK/StringUtils.h +++ b/AK/StringUtils.h @@ -90,6 +90,7 @@ StringView trim_whitespace(StringView string, TrimMode mode); Optional find(StringView haystack, char needle, size_t start = 0); Optional find(StringView haystack, StringView needle, size_t start = 0); Optional find_last(StringView haystack, char needle); +Optional find_last(StringView haystack, StringView needle); Optional find_last_not(StringView haystack, char needle); Vector find_all(StringView haystack, StringView needle); enum class SearchDirection { diff --git a/AK/StringView.h b/AK/StringView.h index 0f1b1dbefe4..7cbc760af9c 100644 --- a/AK/StringView.h +++ b/AK/StringView.h @@ -116,8 +116,8 @@ public: } [[nodiscard]] Optional find(StringView needle, size_t start = 0) const { return StringUtils::find(*this, needle, start); } [[nodiscard]] Optional find_last(char needle) const { return StringUtils::find_last(*this, needle); } + [[nodiscard]] Optional find_last(StringView needle) const { return StringUtils::find_last(*this, needle); } [[nodiscard]] Optional find_last_not(char needle) const { return StringUtils::find_last_not(*this, needle); } - // FIXME: Implement find_last(StringView) for API symmetry. [[nodiscard]] Vector find_all(StringView needle) const;