git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 3/9] implement ends_with via strip_suffix

From
Jeff King <peff@peff.net>
Date
Jun 30, 2014, 16:58 UTC
Message-ID
<20140630165808.GC16637@sigill.intra.peff.net>
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
Previous: Junio C HamanoNext: Jeff King
Message 19 of 27 in “sha1_file: replace PATH_MAX buffer wirh strbuf in prepare_packed_git_one()”
  1. 1/2 sha1_file: replace PATH_MAX buffer wirh strbuf in prepare_packed_git_one()René Scharfe, Jun 28, 2014
  2. 2/4 sha1_file: use strncmp for string comparisonRené Scharfe, Jun 28, 2014
  3. Duy NguyenJun 29, 2014
  4. 1/2 sha1_file: replace PATH_MAX buffer wirh strbuf in, prepare_packed_git_one()René Scharfe, Jun 29, 2014
  5. 2/2 sha1_file: use strncmp for string comparisonRené Scharfe, Jun 29, 2014
  6. Jeff KingJun 30, 2014
  7. Duy NguyenJun 30, 2014
  8. Jeff KingJun 30, 2014
  9. René ScharfeJun 30, 2014
  10. Jeff KingJun 30, 2014
  11. René ScharfeJun 30, 2014
  12. Jeff KingJun 30, 2014
  13. 0/9 add strip_suffix as an alternative to ends_withJeff King, Jun 30, 2014
  14. 1/9 sha1_file: replace PATH_MAX buffer with strbuf in prepare_packed_git_one()Jeff King, Jun 30, 2014
  15. 2/9 add strip_suffix functionJeff King, Jun 30, 2014
  16. Junio C HamanoJul 2, 2014
  17. Jeff KingJul 2, 2014
  18. Junio C HamanoJul 2, 2014
  19. 3/9 implement ends_with via strip_suffixJeff King, Jun 30, 2014
  20. 4/9 replace has_extension with ends_withJeff King, Jun 30, 2014
  21. 5/9 use strip_suffix instead of ends_with in simple casesJeff King, Jun 30, 2014
  22. 6/9 index-pack: use strip_suffix to avoid magic numbersJeff King, Jun 30, 2014
  23. 7/9 strbuf: implement strbuf_strip_suffixJeff King, Jun 30, 2014
  24. 8/9 verify-pack: use strbuf_strip_suffixJeff King, Jun 30, 2014
  25. 9/9 prepare_packed_git_one: refactor duplicate-pack checkJeff King, Jun 30, 2014
  26. Junio C HamanoJul 1, 2014
  27. Jeff KingJun 30, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.