summaryrefslogtreecommitdiffstats
path: root/include/strutils.h
diff options
context:
space:
mode:
authorKarel Zak2013-09-10 12:18:20 +0200
committerKarel Zak2013-09-10 12:18:20 +0200
commit646e159aa980063cde55704450db0dbd903ea814 (patch)
tree285e5ea48e7a64a0de41d243f9badb1960c6f2e2 /include/strutils.h
parentsu: fix lastlog and btmp logging (diff)
downloadkernel-qcow2-util-linux-646e159aa980063cde55704450db0dbd903ea814.tar.gz
kernel-qcow2-util-linux-646e159aa980063cde55704450db0dbd903ea814.tar.xz
kernel-qcow2-util-linux-646e159aa980063cde55704450db0dbd903ea814.zip
lib/strutils: optimalize {starts,ends}with()
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'include/strutils.h')
-rw-r--r--include/strutils.h43
1 files changed, 40 insertions, 3 deletions
diff --git a/include/strutils.h b/include/strutils.h
index 8d8a6e46c..c7fe42a63 100644
--- a/include/strutils.h
+++ b/include/strutils.h
@@ -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