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

[PATCH 3/5] strbuf: introduce starts_with() and ends_with()

From
Christian Couder <chriscool@tuxfamily.org>
Date
Dec 1, 2013, 07:49 UTC
Message-ID
<20131201074919.3042.28757.chriscool@tuxfamily.org>
In-Reply-To
<20131201074818.3042.57357.chriscool@tuxfamily.org>

prefixcmp() and suffixcmp() cannot be really used as comparison functions as they are not antisymmetric:

        prefixcmp("foo", "foobar") < 0
        prefixcmp("foobar", "foo") == 0

So they are not suitable as functions for passing to qsort. And in fact they are used nowhere as comparison functions.

Therefore we should replace them with functions that just check for equality.

As a first step toward this goal, this patch introduces starts_with() and end_with() that will be used to replace respectively prefixcmp() and suffixcmp().

Some popular programming languages, like Java, Python and Ruby have functions or methods called like starts_with() and ends_with() that are doing what we want. Therefore it makes sense to use such names.

In vcs-svn/fast_export.c, there was already an ends_with() function that did the same thing. Let's use the new one instead while at it.

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 git-compat-util.h     |  2 ++
 strbuf.c              | 18 ++++++++++++++++++
 vcs-svn/fast_export.c | 11 +----------
 3 files changed, 21 insertions(+), 10 deletions(-)
diff --git a/git-compat-util.h b/git-compat-util.h
index 7776f12..b73916b 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -350,7 +350,9 @@ extern void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_lis
 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 prefixcmp(const char *str, const char *prefix);
+extern int ends_with(const char *str, const char *suffix);
 extern int suffixcmp(const char *str, const char *suffix);
 
 static inline const char *skip_prefix(const char *str, const char *prefix)
diff --git a/strbuf.c b/strbuf.c
index 1170d01..83caf4a 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -1,6 +1,15 @@
 #include "cache.h"
 #include "refs.h"
 
+int starts_with(const char *str, const char *prefix)
+{
+	for (; ; str++, prefix++)
+		if (!*prefix)
+			return 1;
+		else if (*str != *prefix)
+			return 0;
+}
+
 int prefixcmp(const char *str, const char *prefix)
 {
 	for (; ; str++, prefix++)
@@ -10,6 +19,15 @@ int prefixcmp(const char *str, const char *prefix)
 			return (unsigned char)*prefix - (unsigned char)*str;
 }
 
+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);
+}
+
 int suffixcmp(const char *str, const char *suffix)
 {
 	int len = strlen(str), suflen = strlen(suffix);
diff --git a/vcs-svn/fast_export.c b/vcs-svn/fast_export.c
index f2b23c8..bd0f2c2 100644
--- a/vcs-svn/fast_export.c
+++ b/vcs-svn/fast_export.c
@@ -162,22 +162,13 @@ static void die_short_read(struct line_buffer *input)
 	die("invalid dump: unexpected end of file");
 }
 
-static int ends_with(const char *s, size_t len, const char *suffix)
-{
-	const size_t suffixlen = strlen(suffix);
-	if (len < suffixlen)
-		return 0;
-	return !memcmp(s + len - suffixlen, suffix, suffixlen);
-}
-
 static int parse_cat_response_line(const char *header, off_t *len)
 {
-	size_t headerlen = strlen(header);
 	uintmax_t n;
 	const char *type;
 	const char *end;
 
-	if (ends_with(header, headerlen, " missing"))
+	if (ends_with(header, " missing"))
 		return error("cat-blob reports missing blob: %s", header);
 	type = strstr(header, " blob ");
 	if (!type)
-- 
1.8.4.1.561.g12affca
Previous: Christian CouderNext: Christian Couder
Message 4 of 17 in “use starts_with() and ends_with()”
  1. 0/5 use starts_with() and ends_with()Christian Couder, Dec 1, 2013
  2. 1/5 environment: normalize use of prefixcmp() by removing " != 0"Christian Couder, Dec 1, 2013
  3. 2/5 builtin/remote: remove postfixcmp() and use suffixcmp() insteadChristian Couder, Dec 1, 2013
  4. 3/5 strbuf: introduce starts_with() and ends_with()Christian Couder, Dec 1, 2013
  5. 5/5 strbuf: remove prefixcmp() and suffixcmp()Christian Couder, Dec 1, 2013
  6. Jeff KingDec 2, 2013
  7. Junio C HamanoDec 2, 2013
  8. Christian CouderDec 2, 2013
  9. Antoine PelisseDec 2, 2013
  10. Christian CouderDec 3, 2013
  11. Jeff KingDec 3, 2013
  12. Christian CouderDec 3, 2013
  13. Jeff KingDec 4, 2013
  14. Junio C HamanoDec 4, 2013
  15. Junio C HamanoDec 4, 2013
  16. Christian CouderDec 5, 2013
  17. Junio C HamanoDec 5, 2013

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.