From: Ralph Corderoy Date: Sun, 23 Oct 2016 23:11:42 +0000 (+0100) Subject: sbr/utils.c: Add HasSuffix(s, suffix). X-Git-Url: https://diplodocus.org/git/nmh/commitdiff_plain/c525020c77134b8832587755b7c05764a0516d80?hp=dcf4ca6104c1085214bc110f0bb636fed0f64019 sbr/utils.c: Add HasSuffix(s, suffix). HasSuffix returns true if non-NULL s ends with non-NULL suffix. --- diff --git a/h/utils.h b/h/utils.h index c8384b39..fe054931 100644 --- a/h/utils.h +++ b/h/utils.h @@ -64,6 +64,7 @@ char *rfind_str (const char [], size_t, const char *); char *nmh_strcasestr (const char *, const char *); bool HasPrefix(const char *s, const char *prefix); +bool HasSuffix(const char *s, const char *suffix); bool HasSuffixC(const char *s, int c); void TrimSuffixC(char *s, int c); void ToLower(char *s); diff --git a/sbr/utils.c b/sbr/utils.c index 2e61df89..d6a96d84 100644 --- a/sbr/utils.c +++ b/sbr/utils.c @@ -370,6 +370,18 @@ bool HasPrefix(const char *s, const char *prefix) } +/* HasSuffix returns true if non-NULL s ends with non-NULL suffix. */ +bool HasSuffix(const char *s, const char *suffix) +{ + size_t ls, lsuf; + + ls = strlen(s); + lsuf = strlen(suffix); + + return lsuf <= ls && !strcmp(s + ls - lsuf, suffix); +} + + /* HasSuffixC returns true if non-NULL string s ends with a c before the * terminating NUL. */ bool HasSuffixC(const char *s, int c)