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

[PATCH v2 01/86] strbuf: add starts_with() to be used instead of prefixcmp()

From
Christian Couder <chriscool@tuxfamily.org>
Date
Nov 17, 2013, 22:05 UTC
Message-ID
<20131117220719.4386.5213.chriscool@tuxfamily.org>
In-Reply-To
<20131117215732.4386.19345.chriscool@tuxfamily.org>

prefixcmp() cannot be really used as a comparison function as it is not antisymmetric:

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

So it is not suitable as a function for passing to qsort. And in fact it is used nowhere as a comparison function.

So we should replace it with a function that just checks for equality.

As a first step toward this goal, this patch introduces starts_with().

Some popular programming languages have functions or methods called using "start" and "with" that are doing what we want. Therefore it makes sense to use starts_with() as a function name to replace prefixcmp().

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 git-compat-util.h | 1 +
 strbuf.c          | 9 +++++++++
 2 files changed, 10 insertions(+)
diff --git a/git-compat-util.h b/git-compat-util.h
index 37f0ba0..e441a6b 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -351,6 +351,7 @@ 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 prefixcmp(const char *str, const char *prefix);
+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)
diff --git a/strbuf.c b/strbuf.c
index 2a14fdb..933d998 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -10,6 +10,15 @@ int prefixcmp(const char *str, const char *prefix)
 			return (unsigned char)*prefix - (unsigned char)*str;
 }
 
+int starts_with(const char *str, const char *prefix)
+{
+	for (; ; str++, prefix++)
+		if (!*prefix)
+			return 1;
+		else if (*str != *prefix)
+			return 0;
+}
+
 int ends_with(const char *str, const char *suffix)
 {
 	int len = strlen(str), suflen = strlen(suffix);
-- 
1.8.4.1.561.g12affca
Previous: Christian CouderNext: Christian Couder
Message 2 of 6 in “replace prefixcmp() with starts_with()”
  1. 00/86 replace prefixcmp() with starts_with()Christian Couder, Nov 17, 2013
  2. 01/86 strbuf: add starts_with() to be used instead of prefixcmp()Christian Couder, Nov 17, 2013
  3. 02/86 diff: replace prefixcmp() with starts_with()Christian Couder, Nov 17, 2013
  4. 08/86 transport*: replace prefixcmp() with starts_with()Christian Couder, Nov 17, 2013
  5. 40/86 environment: replace prefixcmp() with starts_with()Christian Couder, Nov 17, 2013
  6. 86/86 strbuf: remove prefixcmp() as it has been replaced with starts_with()Christian Couder, Nov 17, 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.