From 117f6b335cfc618ffb6004acb084587d89ebc1a9 Mon Sep 17 00:00:00 2001 From: lifegpc Date: Fri, 5 Jan 2024 22:54:34 +0800 Subject: [PATCH] Add new function --- str_util.cpp | 8 ++++++++ str_util.h | 7 +++++++ 2 files changed, 15 insertions(+) diff --git a/str_util.cpp b/str_util.cpp index a66b6e7..9f9cff2 100644 --- a/str_util.cpp +++ b/str_util.cpp @@ -57,3 +57,11 @@ std::string str_util::str_hex(std::string input) { } return output; } + +bool str_util::str_endswith(std::string input, std::string pattern) { + auto ilen = input.length(); + auto plen = pattern.length(); + if (ilen < plen) return false; + auto i = input.rfind(pattern); + return i == ilen - plen; +} diff --git a/str_util.h b/str_util.h index e3ac7c6..f8d4895 100644 --- a/str_util.h +++ b/str_util.h @@ -33,5 +33,12 @@ namespace str_util { * @return Output */ std::string str_hex(std::string input); + /** + * @brief Check if a string ends with a pattern + * @param input Input data + * @param pattern Pattern + * @return true if input ends with pattern + */ + bool str_endswith(std::string input, std::string pattern); } #endif