Skip to content

Commit 9566231

Browse files
chriscoolgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3fb5aea commit 9566231

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

git-compat-util.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,9 @@ extern void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_lis
350350
extern void set_error_routine(void (*routine)(const char *err, va_list params));
351351
extern void set_die_is_recursing_routine(int (*routine)(void));
352352

353+
extern int starts_with(const char *str, const char *prefix);
353354
extern int prefixcmp(const char *str, const char *prefix);
355+
extern int ends_with(const char *str, const char *suffix);
354356
extern int suffixcmp(const char *str, const char *suffix);
355357

356358
static inline const char *skip_prefix(const char *str, const char *prefix)

strbuf.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
#include "cache.h"
22
#include "refs.h"
33

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+
413
int prefixcmp(const char *str, const char *prefix)
514
{
615
for (; ; str++, prefix++)
@@ -10,6 +19,15 @@ int prefixcmp(const char *str, const char *prefix)
1019
return (unsigned char)*prefix - (unsigned char)*str;
1120
}
1221

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+
1331
int suffixcmp(const char *str, const char *suffix)
1432
{
1533
int len = strlen(str), suflen = strlen(suffix);

vcs-svn/fast_export.c

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -162,22 +162,13 @@ static void die_short_read(struct line_buffer *input)
162162
die("invalid dump: unexpected end of file");
163163
}
164164

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-
173165
static int parse_cat_response_line(const char *header, off_t *len)
174166
{
175-
size_t headerlen = strlen(header);
176167
uintmax_t n;
177168
const char *type;
178169
const char *end;
179170

180-
if (ends_with(header, headerlen, " missing"))
171+
if (ends_with(header, " missing"))
181172
return error("cat-blob reports missing blob: %s", header);
182173
type = strstr(header, " blob ");
183174
if (!type)

0 commit comments

Comments
 (0)