git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jeff King <peff@peff.net>
To: "René Scharfe" <l.s.r@web.de>
Cc: Duy Nguyen <pclouds@gmail.com>,
	Git Mailing List <git@vger.kernel.org>,
	Junio C Hamano <gitster@pobox.com>
Subject: [PATCH 3/9] implement ends_with via strip_suffix
Date: Mon, 30 Jun 2014 12:58:08 -0400	[thread overview]
Message-ID: <20140630165808.GC16637@sigill.intra.peff.net> (raw)
In-Reply-To: <20140630165526.GA15690@sigill.intra.peff.net>

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>
---
 git-compat-util.h | 7 ++++++-
 strbuf.c          | 9 ---------
 2 files changed, 6 insertions(+), 10 deletions(-)

diff --git a/git-compat-util.h b/git-compat-util.h
index d044c42..4cfde49 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -347,7 +347,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)
 {
@@ -385,6 +384,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 ac62982..99dbeba 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -11,15 +11,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
-- 
2.0.0.566.gfe3e6b2

  parent reply	other threads:[~2014-06-30 16:58 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-06-28 14:47 [PATCH 1/2] sha1_file: replace PATH_MAX buffer wirh strbuf in prepare_packed_git_one() René Scharfe
2014-06-28 15:01 ` [PATCH 2/4] sha1_file: use strncmp for string comparison René Scharfe
2014-06-29  1:21 ` [PATCH 1/2] sha1_file: replace PATH_MAX buffer wirh strbuf in prepare_packed_git_one() Duy Nguyen
2014-06-29  5:43   ` [PATCH v2 1/2] sha1_file: replace PATH_MAX buffer wirh strbuf in, prepare_packed_git_one() René Scharfe
2014-06-29  5:56     ` [PATCH v2 2/2] sha1_file: use strncmp for string comparison René Scharfe
2014-06-30 13:43       ` Jeff King
2014-06-30 13:59         ` Duy Nguyen
2014-06-30 14:22           ` Jeff King
2014-06-30 16:43             ` René Scharfe
2014-06-30 16:45               ` Jeff King
2014-06-30 16:35         ` René Scharfe
2014-06-30 16:46           ` Jeff King
2014-06-30 16:55         ` [PATCH 0/9] add strip_suffix as an alternative to ends_with Jeff King
2014-06-30 16:55           ` [PATCH 1/9] sha1_file: replace PATH_MAX buffer with strbuf in prepare_packed_git_one() Jeff King
2014-06-30 16:57           ` [PATCH 2/9] add strip_suffix function Jeff King
2014-07-02 15:54             ` Junio C Hamano
2014-07-02 16:38               ` Jeff King
2014-07-02 17:41                 ` Junio C Hamano
2014-06-30 16:58           ` Jeff King [this message]
2014-06-30 16:58           ` [PATCH 4/9] replace has_extension with ends_with Jeff King
2014-06-30 16:58           ` [PATCH 5/9] use strip_suffix instead of ends_with in simple cases Jeff King
2014-06-30 16:59           ` [PATCH 6/9] index-pack: use strip_suffix to avoid magic numbers Jeff King
2014-06-30 17:01           ` [PATCH 7/9] strbuf: implement strbuf_strip_suffix Jeff King
2014-06-30 17:02           ` [PATCH 8/9] verify-pack: use strbuf_strip_suffix Jeff King
2014-06-30 17:04           ` [PATCH 9/9] prepare_packed_git_one: refactor duplicate-pack check Jeff King
2014-07-01  1:00           ` [PATCH 0/9] add strip_suffix as an alternative to ends_with Junio C Hamano
2014-06-30 13:24     ` [PATCH v2 1/2] sha1_file: replace PATH_MAX buffer wirh strbuf in, prepare_packed_git_one() Jeff King

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20140630165808.GC16637@sigill.intra.peff.net \
    --to=peff@peff.net \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=l.s.r@web.de \
    --cc=pclouds@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).