summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff King <peff@peff.net>2014-06-30 12:58:08 -0400
committerJunio C Hamano <gitster@pobox.com>2014-06-30 13:43:16 -0700
commitf52a35fd63cc6d570083cedc15576d01c0968d98 (patch)
tree39795365e748d106789760a7ef084dc625bd9361
parent35480f0b23f2c1824109ddae24392a70d19c6b9c (diff)
downloadgit-f52a35fd63cc6d570083cedc15576d01c0968d98.tar.gz
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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
-rw-r--r--git-compat-util.h7
-rw-r--r--strbuf.c9
2 files changed, 6 insertions, 10 deletions
diff --git a/git-compat-util.h b/git-compat-util.h
index 4c451b575d..1c9c68cb89 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -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)
{
@@ -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
diff --git a/strbuf.c b/strbuf.c
index ee96dcfb81..63356ccd1b 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -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