From 35bfb16ff976a36707c9d9c258d9f6c4af6b5de3 Mon Sep 17 00:00:00 2001 From: kaetemi Date: Fri, 6 Dec 2019 16:24:25 +0800 Subject: [PATCH] Add super handy endsWith string utility function --- code/nel/include/nel/misc/string_common.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/code/nel/include/nel/misc/string_common.h b/code/nel/include/nel/misc/string_common.h index bc506cf82..3fd46b835 100644 --- a/code/nel/include/nel/misc/string_common.h +++ b/code/nel/include/nel/misc/string_common.h @@ -275,6 +275,21 @@ inline bool startsWith(const char *str, const char *prefix) inline bool startsWith(const std::string &str, const char *prefix) { return startsWith(str.c_str(), prefix); } inline bool startsWith(const std::string &str, const std::string &prefix) { return startsWith(str.c_str(), prefix.c_str()); } +inline bool endsWith(const char *str, size_t strLen, const char *suffix, size_t suffixLen) +{ + if (strLen < suffixLen) + return false; + int minLen = strLen < suffixLen ? strLen : suffixLen; + for (int i = 1; i <= minLen; ++i) + if (str[strLen - i] != suffix[suffixLen - i]) + return false; + return true; +} + +inline bool endsWith(const char *str, const char *suffix) { return endsWith(str, strlen(str), suffix, strlen(suffix)); } +inline bool endsWith(const std::string &str, const char *suffix) { return endsWith(str.c_str(), str.size(), suffix, strlen(suffix)); } +inline bool endsWith(const std::string &str, const std::string &suffix) { return endsWith(str.c_str(), str.size(), suffix.c_str(), suffix.size()); } + // Convert local codepage to UTF-8 // On Windows, the local codepage is undetermined // On Linux, the local codepage is always UTF-8 (no-op)