strbuf: introduce starts_with() and ends_with()
authorChristian Couder <chriscool@tuxfamily.org>
Sun, 1 Dec 2013 07:49:16 +0000 (08:49 +0100)
committerJunio C Hamano <gitster@pobox.com>
Thu, 5 Dec 2013 22:12:52 +0000 (14:12 -0800)
prefixcmp() and suffixcmp() share the common "cmp" suffix that
typically are used to name functions that can be used for ordering,
but they can't, because they are not antisymmetric:

prefixcmp("foo", "foobar") < 0
prefixcmp("foobar", "foo") == 0

We in fact do not use these functions for ordering. Replace them
with functions that just check for equality.

Add starts_with() and end_with() that will be used to replace
prefixcmp() and suffixcmp(), respectively, as the first step. These
are named after corresponding functions/methods in programming
languages, like Java, Python and Ruby.

In vcs-svn/fast_export.c, there was already an ends_with() function
that did the same thing. Let's use the new one instead while at it.

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
git-compat-util.h
strbuf.c
vcs-svn/fast_export.c
index 7776f126d3bd4facfe987bd844a339c8a17b102a..b73916bbc64b3d2b926c4f5aa50311c7a867c711 100644 (file)
@@ -350,7 +350,9 @@ extern void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_lis
 extern void set_error_routine(void (*routine)(const char *err, va_list params));
 extern void set_die_is_recursing_routine(int (*routine)(void));
 
+extern int starts_with(const char *str, const char *prefix);
 extern int prefixcmp(const char *str, const char *prefix);
+extern int ends_with(const char *str, const char *suffix);
 extern int suffixcmp(const char *str, const char *suffix);
 
 static inline const char *skip_prefix(const char *str, const char *prefix)
index 1170d01c4322b494cd900853279766edd030de32..83caf4a9147e6788107fb5fd23e6d9e1d6f6267f 100644 (file)
--- a/strbuf.c
+++ b/strbuf.c
@@ -1,6 +1,15 @@
 #include "cache.h"
 #include "refs.h"
 
+int starts_with(const char *str, const char *prefix)
+{
+       for (; ; str++, prefix++)
+               if (!*prefix)
+                       return 1;
+               else if (*str != *prefix)
+                       return 0;
+}
+
 int prefixcmp(const char *str, const char *prefix)
 {
        for (; ; str++, prefix++)
@@ -10,6 +19,15 @@ int prefixcmp(const char *str, const char *prefix)
                        return (unsigned char)*prefix - (unsigned char)*str;
 }
 
+int ends_with(const char *str, const char *suffix)
+{
+       int len = strlen(str), suflen = strlen(suffix);
+       if (len < suflen)
+               return 0;
+       else
+               return !strcmp(str + len - suflen, suffix);
+}
+
 int suffixcmp(const char *str, const char *suffix)
 {
        int len = strlen(str), suflen = strlen(suffix);
index f2b23c81de11a63c2c1c2ac925be9081ad5bea5c..bd0f2c2b86847708e5ebe668773f0fd08b9efc5d 100644 (file)
@@ -162,22 +162,13 @@ static void die_short_read(struct line_buffer *input)
        die("invalid dump: unexpected end of file");
 }
 
-static int ends_with(const char *s, size_t len, const char *suffix)
-{
-       const size_t suffixlen = strlen(suffix);
-       if (len < suffixlen)
-               return 0;
-       return !memcmp(s + len - suffixlen, suffix, suffixlen);
-}
-
 static int parse_cat_response_line(const char *header, off_t *len)
 {
-       size_t headerlen = strlen(header);
        uintmax_t n;
        const char *type;
        const char *end;
 
-       if (ends_with(header, headerlen, " missing"))
+       if (ends_with(header, " missing"))
                return error("cat-blob reports missing blob: %s", header);
        type = strstr(header, " blob ");
        if (!type)