summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKay Sievers <kay@vrfy.org>2013-08-21 22:16:44 +0200
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2013-08-22 00:52:14 -0400
commitdf89481a35d4d7d58c7563f3082f67c218891375 (patch)
treeac4389bf301f92b00e3b0b42f1b38fdb939f1636
parent2a0e0692565f0435657c93498e09cbb2d3517152 (diff)
Optimize startswith() to macro
I guess it's easier and cleaner anyway to use simple static inline functions instead of defines.
-rw-r--r--src/shared/util.c34
-rw-r--r--src/shared/util.h14
2 files changed, 12 insertions, 36 deletions
diff --git a/src/shared/util.c b/src/shared/util.c
index ca9c2eb9d1..1dde8afcad 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -128,40 +128,6 @@ char* endswith(const char *s, const char *postfix) {
return (char*) s + sl - pl;
}
-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++;
- }
-}
-
-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++;
- }
-}
-
bool first_word(const char *s, const char *word) {
size_t sl, wl;
diff --git a/src/shared/util.h b/src/shared/util.h
index 3be692ec33..63f4e3dff2 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -106,9 +106,19 @@ static inline bool isempty(const char *p) {
return !p || !p[0];
}
+static inline const char *startswith(const char *s, const char *prefix) {
+ if (strncmp(s, prefix, strlen(prefix)) == 0)
+ return s + strlen(prefix);
+ return NULL;
+}
+
+static inline const char *startswith_no_case(const char *s, const char *prefix) {
+ if (strncasecmp(s, prefix, strlen(prefix)) == 0)
+ return s + strlen(prefix);
+ return NULL;
+}
+
char *endswith(const char *s, const char *postfix) _pure_;
-char *startswith(const char *s, const char *prefix) _pure_;
-char *startswith_no_case(const char *s, const char *prefix) _pure_;
bool first_word(const char *s, const char *word) _pure_;