diff options
author | Agustin Gianni <agustingianni@gmail.com> | 2022-12-15 21:20:14 +0000 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-12-20 11:24:05 +0100 |
commit | 9a2ee5a9dd792359440429832ac564f5dde2b315 (patch) | |
tree | 67c9dd0da0dd670a860978c3abf2db38ffe6af6a | |
parent | 03107d402808045bdadc0edf3d66e960dd37b967 (diff) | |
download | serenity-9a2ee5a9dd792359440429832ac564f5dde2b315.zip |
AK: Add DeprecatedString::find_last(StringView)
This adds the the method DeprecatedString::find_last() as wrapper for
StringUtils::find_last for the StringView type.
-rw-r--r-- | AK/DeprecatedString.h | 2 | ||||
-rw-r--r-- | AK/StringUtils.cpp | 11 | ||||
-rw-r--r-- | AK/StringUtils.h | 1 | ||||
-rw-r--r-- | 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<size_t> find(char needle, size_t start = 0) const { return StringUtils::find(*this, needle, start); } [[nodiscard]] Optional<size_t> find(StringView 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) for API symmetry. + [[nodiscard]] Optional<size_t> find_last(StringView needle) const { return StringUtils::find_last(*this, needle); } Vector<size_t> find_all(StringView needle) const; using SearchDirection = StringUtils::SearchDirection; [[nodiscard]] Optional<size_t> 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<size_t> find_last(StringView haystack, char needle) return {}; } +Optional<size_t> 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<size_t> 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<size_t> find(StringView haystack, char needle, size_t start = 0); Optional<size_t> find(StringView haystack, StringView needle, size_t start = 0); Optional<size_t> find_last(StringView haystack, char needle); +Optional<size_t> find_last(StringView haystack, StringView needle); Optional<size_t> find_last_not(StringView haystack, char needle); Vector<size_t> 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<size_t> find(StringView 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); } + [[nodiscard]] Optional<size_t> find_last(StringView needle) const { return StringUtils::find_last(*this, needle); } [[nodiscard]] Optional<size_t> find_last_not(char needle) const { return StringUtils::find_last_not(*this, needle); } - // FIXME: Implement find_last(StringView) for API symmetry. [[nodiscard]] Vector<size_t> find_all(StringView needle) const; |