diff --git a/AK/StringUtils.cpp b/AK/StringUtils.cpp index 70c09fe0080..b9aed9c9aad 100644 --- a/AK/StringUtils.cpp +++ b/AK/StringUtils.cpp @@ -388,6 +388,15 @@ Optional find_last(StringView haystack, char needle) return {}; } +Optional find_last_not(StringView haystack, char needle) +{ + for (size_t i = haystack.length(); i > 0; --i) { + if (haystack[i - 1] != needle) + return i - 1; + } + return {}; +} + Vector find_all(StringView haystack, StringView needle) { Vector positions; diff --git a/AK/StringUtils.h b/AK/StringUtils.h index 0e5f143c370..aa9b2af73d3 100644 --- a/AK/StringUtils.h +++ b/AK/StringUtils.h @@ -74,6 +74,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_not(StringView haystack, char needle); Vector find_all(StringView haystack, StringView needle); enum class SearchDirection { Forward, diff --git a/AK/StringView.h b/AK/StringView.h index 9eb09a10e25..4b8b974aa45 100644 --- a/AK/StringView.h +++ b/AK/StringView.h @@ -107,6 +107,7 @@ 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_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;