]> diplodocus.org Git - nmh/commitdiff
utils.c: Add HasPrefix(s, prefix).
authorRalph Corderoy <ralph@inputplus.co.uk>
Sun, 23 Oct 2016 11:02:55 +0000 (12:02 +0100)
committerRalph Corderoy <ralph@inputplus.co.uk>
Mon, 24 Oct 2016 09:18:55 +0000 (10:18 +0100)
HasPrefix returns true if non-NULL s starts with non-NULL prefix.

h/utils.h
sbr/utils.c

index 0ce68a7..47ae59f 100644 (file)
--- a/h/utils.h
+++ b/h/utils.h
@@ -63,6 +63,7 @@ char *find_str (const char [], size_t, const char *);
 char *rfind_str (const char [], size_t, const char *);
 char *nmh_strcasestr (const char *, const char *);
 
 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 HasSuffixC(char *s, int c);
 void TrimSuffixC(char *s, int c);
 void ToLower(char *s);
 bool HasSuffixC(char *s, int c);
 void TrimSuffixC(char *s, int c);
 void ToLower(char *s);
index d8a43c1..a6f0e11 100644 (file)
@@ -358,6 +358,18 @@ nmh_strcasestr (const char *s1, const char *s2) {
 }
 
 
 }
 
 
+/* HasPrefix returns true if non-NULL s starts with non-NULL prefix. */
+bool HasPrefix(const char *s, const char *prefix)
+{
+    while (*s && *s == *prefix) {
+        s++;
+        prefix++;
+    }
+
+    return *prefix == '\0';
+}
+
+
 /* HasSuffixC returns true if non-NULL string s ends with a c before the
  * terminating NUL. */
 bool HasSuffixC(char *s, int c)
 /* HasSuffixC returns true if non-NULL string s ends with a c before the
  * terminating NUL. */
 bool HasSuffixC(char *s, int c)