]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/strutils: optimalize {starts,ends}with()
authorKarel Zak <kzak@redhat.com>
Tue, 10 Sep 2013 10:18:20 +0000 (12:18 +0200)
committerKarel Zak <kzak@redhat.com>
Tue, 10 Sep 2013 10:18:20 +0000 (12:18 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
include/strutils.h
lib/strutils.c

index 8d8a6e46cfd84c3897c5649132fe73b7457267bb..c7fe42a6326756148fc6855df048d3318e53fb18 100644 (file)
@@ -102,8 +102,45 @@ extern int parse_range(const char *str, int *lower, int *upper, int def);
 
 extern int streq_except_trailing_slash(const char *s1, const char *s2);
 
-extern char *startswith(const char *s, const char *prefix);
-extern char *startswith_no_case(const char *s, const char *prefix);
-extern char *endswith(const char *s, const char *postfix);
+/*
+ * Match string beginning.
+ */
+static inline const char *startswith(const char *s, const char *prefix)
+{
+       size_t sz = prefix ? strlen(prefix) : 0;
+
+        if (s && sz && strncmp(s, prefix, sz) == 0)
+                return s + sz;
+       return NULL;
+}
+
+/*
+ * Case insensitive match string beginning.
+ */
+static inline const char *startswith_no_case(const char *s, const char *prefix)
+{
+       size_t sz = prefix ? strlen(prefix) : 0;
+
+        if (s && sz && strncasecmp(s, prefix, sz) == 0)
+                return s + sz;
+       return NULL;
+}
+
+/*
+ * Match string ending.
+ */
+static inline const char *endswith(const char *s, const char *postfix)
+{
+       size_t sl = s ? strlen(s) : 0;
+       size_t pl = postfix ? strlen(postfix) : 0;
+
+       if (pl == 0)
+               return (char *)s + sl;
+       if (sl < pl)
+               return NULL;
+       if (memcmp(s + sl - pl, postfix, pl) != 0)
+               return NULL;
+       return (char *)s + sl - pl;
+}
 
 #endif
index 95ab5a87eef59a8d008ba64a71d0173b26009b77..69b7ba23faec0a238ab63f75535973071db86a85 100644 (file)
@@ -686,72 +686,6 @@ int streq_except_trailing_slash(const char *s1, const char *s2)
        return equal;
 }
 
-/*
- * Match string beginning.
- */
-char *startswith(const char *s, const char *prefix)
-{
-       const char *a, *b;
-
-       assert(s);
-       assert(prefix);
-
-       a = s, b = prefix;
-       for (;;) {
-               if (*b == 0)
-                       return (char *)a;
-               if (*a != *b)
-                       return NULL;
-
-               a++, b++;
-       }
-}
-
-/*
- * Case insensitive match string beginning.
- */
-char *startswith_no_case(const char *s, const char *prefix)
-{
-       const char *a, *b;
-
-       assert(s);
-       assert(prefix);
-
-       a = s, b = prefix;
-       for (;;) {
-               if (*b == 0)
-                       return (char *)a;
-               if (tolower(*a) != tolower(*b))
-                       return NULL;
-
-               a++, b++;
-       }
-}
-
-/*
- * Match string ending.
- */
-char *endswith(const char *s, const char *postfix)
-{
-       size_t sl, pl;
-
-       assert(s);
-       assert(postfix);
-
-       sl = strlen(s);
-       pl = strlen(postfix);
-
-       if (pl == 0)
-               return (char *)s + sl;
-
-       if (sl < pl)
-               return NULL;
-
-       if (memcmp(s + sl - pl, postfix, pl) != 0)
-               return NULL;
-
-       return (char *)s + sl - pl;
-}
 
 #ifdef TEST_PROGRAM