Skip to content

Commit

Permalink
implement ends_with via strip_suffix
Browse files Browse the repository at this point in the history
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 <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Jeff King authored and Junio C Hamano committed Jun 30, 2014
1 parent 35480f0 commit f52a35f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
7 changes: 6 additions & 1 deletion git-compat-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,6 @@ 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 ends_with(const char *str, const char *suffix);

static inline const char *skip_prefix(const char *str, const char *prefix)
{
Expand Down Expand Up @@ -377,6 +376,12 @@ static inline int strip_suffix(const char *str, const char *suffix, size_t *len)
return strip_suffix_mem(str, len, suffix);
}

static inline int ends_with(const char *str, const char *suffix)
{
size_t len;
return strip_suffix(str, suffix, &len);
}

#if defined(NO_MMAP) || defined(USE_WIN32_MMAP)

#ifndef PROT_READ
Expand Down
9 changes: 0 additions & 9 deletions strbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,6 @@ int starts_with(const char *str, const char *prefix)
return 0;
}

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);
}

/*
* Used as the default ->buf value, so that people can always assume
* buf is non NULL and ->buf is NUL terminated even for a freshly
Expand Down

0 comments on commit f52a35f

Please sign in to comment.