util/StringUtil: add FindStringSuffix()

This commit is contained in:
Max Kellermann
2015-03-02 22:26:46 +01:00
parent c19292c036
commit 820debf45a
3 changed files with 27 additions and 8 deletions

View File

@@ -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)
{

View File

@@ -90,6 +90,14 @@ gcc_pure
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().