]> diplodocus.org Git - nmh/commitdiff
sbr/utils.c: Add HasSuffix(s, suffix).
authorRalph Corderoy <ralph@inputplus.co.uk>
Sun, 23 Oct 2016 23:11:42 +0000 (00:11 +0100)
committerRalph Corderoy <ralph@inputplus.co.uk>
Mon, 24 Oct 2016 09:18:56 +0000 (10:18 +0100)
HasSuffix returns true if non-NULL s ends with non-NULL suffix.

h/utils.h
sbr/utils.c

index c8384b3..fe05493 100644 (file)
--- 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);
index 2e61df8..d6a96d8 100644 (file)
@@ -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)