From 9a2ee5a9dd792359440429832ac564f5dde2b315 Mon Sep 17 00:00:00 2001 From: Agustin Gianni Date: Thu, 15 Dec 2022 21:20:14 +0000 Subject: [PATCH] AK: Add DeprecatedString::find_last(StringView) This adds the the method DeprecatedString::find_last() as wrapper for StringUtils::find_last for the StringView type. --- AK/DeprecatedString.h | 2 +- AK/StringUtils.cpp | 11 +++++++++++ AK/StringUtils.h | 1 + AK/StringView.h | 2 +- 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/AK/DeprecatedString.h b/AK/DeprecatedString.h index e5a4ed853d..c126f2c21a 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 eba74f8ee0..d660b95f0f 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 173d8c941b..8f7fb2f490 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 0f1b1dbefe..7cbc760af9 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;