chiark / gitweb /
util: return the remaining string in startswith()
authorLennart Poettering <lennart@poettering.net>
Fri, 26 Oct 2012 23:20:01 +0000 (01:20 +0200)
committerLennart Poettering <lennart@poettering.net>
Fri, 26 Oct 2012 23:20:01 +0000 (01:20 +0200)
src/shared/util.c
src/shared/util.h

index a73443d535c960c68d00396d665e278da8ccd2b6..db8e75b6280635356ee69b24ea66253a1617fec0 100644 (file)
@@ -216,45 +216,38 @@ char* endswith(const char *s, const char *postfix) {
         return (char*) s + sl - pl;
 }
 
-bool startswith(const char *s, const char *prefix) {
-        size_t sl, pl;
+char* startswith(const char *s, const char *prefix) {
+        const char *a, *b;
 
         assert(s);
         assert(prefix);
 
-        sl = strlen(s);
-        pl = strlen(prefix);
-
-        if (pl == 0)
-                return true;
-
-        if (sl < pl)
-                return false;
+        a = s, b = prefix;
+        for (;;) {
+                if (*b == 0)
+                        return (char*) a;
+                if (*a != *b)
+                        return NULL;
 
-        return memcmp(s, prefix, pl) == 0;
+                a++, b++;
+        }
 }
 
-bool startswith_no_case(const char *s, const char *prefix) {
-        size_t sl, pl;
-        unsigned i;
+char* startswith_no_case(const char *s, const char *prefix) {
+        const char *a, *b;
 
         assert(s);
         assert(prefix);
 
-        sl = strlen(s);
-        pl = strlen(prefix);
-
-        if (pl == 0)
-                return true;
-
-        if (sl < pl)
-                return false;
-
-        for(i = 0; i < pl; ++i)
-                if (tolower(s[i]) != tolower(prefix[i]))
-                        return false;
+        a = s, b = prefix;
+        for (;;) {
+                if (*b == 0)
+                        return (char*) a;
+                if (tolower(*a) != tolower(*b))
+                        return NULL;
 
-        return true;
+                a++, b++;
+        }
 }
 
 bool first_word(const char *s, const char *word) {
index 8935beea2b6845f4de3836f5d08a01b97c36fc0e..f6a4c1ea6c8c199d53d70a3d9b9999777cd78e76 100644 (file)
@@ -142,8 +142,8 @@ static inline bool isempty(const char *p) {
 }
 
 char *endswith(const char *s, const char *postfix);
-bool startswith(const char *s, const char *prefix);
-bool startswith_no_case(const char *s, const char *prefix);
+char *startswith(const char *s, const char *prefix);
+char *startswith_no_case(const char *s, const char *prefix);
 
 bool first_word(const char *s, const char *word);