Skip to content

Commit f52a35f

Browse files
peffgitster
authored andcommitted
implement ends_with via strip_suffix
The ends_with function is essentially a simplified version of strip_suffix, in which we throw away the stripped length. Implementing it as an inline on top of strip_suffix has two advantages: 1. We save a bit of duplicated code. 2. The suffix is typically a string literal, and we call strlen on it. By making the function inline, many compilers can replace the strlen call with a constant. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 35480f0 commit f52a35f

File tree

2 files changed

+6
-10
lines changed

2 files changed

+6
-10
lines changed

git-compat-util.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,6 @@ extern void set_error_routine(void (*routine)(const char *err, va_list params));
339339
extern void set_die_is_recursing_routine(int (*routine)(void));
340340

341341
extern int starts_with(const char *str, const char *prefix);
342-
extern int ends_with(const char *str, const char *suffix);
343342

344343
static inline const char *skip_prefix(const char *str, const char *prefix)
345344
{
@@ -377,6 +376,12 @@ static inline int strip_suffix(const char *str, const char *suffix, size_t *len)
377376
return strip_suffix_mem(str, len, suffix);
378377
}
379378

379+
static inline int ends_with(const char *str, const char *suffix)
380+
{
381+
size_t len;
382+
return strip_suffix(str, suffix, &len);
383+
}
384+
380385
#if defined(NO_MMAP) || defined(USE_WIN32_MMAP)
381386

382387
#ifndef PROT_READ

strbuf.c

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,6 @@ int starts_with(const char *str, const char *prefix)
1010
return 0;
1111
}
1212

13-
int ends_with(const char *str, const char *suffix)
14-
{
15-
int len = strlen(str), suflen = strlen(suffix);
16-
if (len < suflen)
17-
return 0;
18-
else
19-
return !strcmp(str + len - suflen, suffix);
20-
}
21-
2213
/*
2314
* Used as the default ->buf value, so that people can always assume
2415
* buf is non NULL and ->buf is NUL terminated even for a freshly

0 commit comments

Comments
 (0)