diff options
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/StringUtil.cxx | 15 | ||||
-rw-r--r-- | src/util/StringUtil.hxx | 8 |
2 files changed, 23 insertions, 0 deletions
diff --git a/src/util/StringUtil.cxx b/src/util/StringUtil.cxx index 4d538989f..107354b6e 100644 --- a/src/util/StringUtil.cxx +++ b/src/util/StringUtil.cxx @@ -96,6 +96,21 @@ StringEndsWith(const char *haystack, const char *needle) needle, needle_length) == 0; } +const char * +FindStringSuffix(const char *p, const char *suffix) +{ + const size_t p_length = strlen(p); + const size_t suffix_length = strlen(suffix); + + if (p_length < suffix_length) + return nullptr; + + const char *q = p + p_length - suffix_length; + return memcmp(q, suffix, suffix_length) == 0 + ? q + : nullptr; +} + char * CopyString(char *gcc_restrict dest, const char *gcc_restrict src, size_t size) { diff --git a/src/util/StringUtil.hxx b/src/util/StringUtil.hxx index 9f2dab935..4f44ab94a 100644 --- a/src/util/StringUtil.hxx +++ b/src/util/StringUtil.hxx @@ -91,6 +91,14 @@ bool StringEndsWith(const char *haystack, const char *needle); /** + * Check if the given string ends with the specified suffix. If yes, + * returns the position of the suffix, and nullptr otherwise. + */ +gcc_pure +const char * +FindStringSuffix(const char *p, const char *suffix); + +/** * Copy a string. If the buffer is too small, then the string is * truncated. This is a safer version of strncpy(). * |