strbuf: introduce starts_with() and ends_with()

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>

Christian Couder committed Dec 1, 2013 at 08:49 UTC 956623157f828b2b4fd91a9bc5e78ba8e42437d9
3 files changed +21 -10
git-compat-util.h
+2
@@ -350,7 +350,9 @@ extern void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_lis
350 extern void set_error_routine(void (*routine)(const char *err, va_list params));
351 extern void set_die_is_recursing_routine(int (*routine)(void));
352
353 +extern int starts_with(const char *str, const char *prefix);
354 extern int prefixcmp(const char *str, const char *prefix);
355 +extern int ends_with(const char *str, const char *suffix);
356 extern int suffixcmp(const char *str, const char *suffix);
357
358 static inline const char *skip_prefix(const char *str, const char *prefix)
strbuf.c
+18
@@ -1,6 +1,15 @@
1 #include "cache.h"
2 #include "refs.h"
3
4 +int starts_with(const char *str, const char *prefix)
5 +{
6 + for (; ; str++, prefix++)
7 + if (!*prefix)
8 + return 1;
9 + else if (*str != *prefix)
10 + return 0;
11 +}
12 +
13 int prefixcmp(const char *str, const char *prefix)
14 {
15 for (; ; str++, prefix++)
@@ -10,6 +19,15 @@ int prefixcmp(const char *str, const char *prefix)
19 return (unsigned char)*prefix - (unsigned char)*str;
20 }
21
22 +int ends_with(const char *str, const char *suffix)
23 +{
24 + int len = strlen(str), suflen = strlen(suffix);
25 + if (len < suflen)
26 + return 0;
27 + else
28 + return !strcmp(str + len - suflen, suffix);
29 +}
30 +
31 int suffixcmp(const char *str, const char *suffix)
32 {
33 int len = strlen(str), suflen = strlen(suffix);
vcs-svn/fast_export.c
+1 -10
@@ -162,22 +162,13 @@ static void die_short_read(struct line_buffer *input)
162 die("invalid dump: unexpected end of file");
163 }
164
165 -static int ends_with(const char *s, size_t len, const char *suffix)
166 -{
167 - const size_t suffixlen = strlen(suffix);
168 - if (len < suffixlen)
169 - return 0;
170 - return !memcmp(s + len - suffixlen, suffix, suffixlen);
171 -}
172 -
165 static int parse_cat_response_line(const char *header, off_t *len)
166 {
175 - size_t headerlen = strlen(header);
167 uintmax_t n;
168 const char *type;
169 const char *end;
170
180 - if (ends_with(header, headerlen, " missing"))
171 + if (ends_with(header, " missing"))
172 return error("cat-blob reports missing blob: %s", header);
173 type = strstr(header, " blob ");
174 if (!type)