From: Christian Couder <chriscool@tuxfamily.org>
To: Junio C Hamano <gitster@pobox.com>
Cc: git@vger.kernel.org, Avery Pennarun <apenwarr@gmail.com>,
Johannes Schindelin <Johannes.Schindelin@gmx.de>,
Jonathan Nieder <jrnieder@gmail.com>, Jeff King <peff@peff.net>,
Max Horn <max@quendi.de>, Andreas Ericsson <ae@op5.se>
Subject: [PATCH v2 01/86] strbuf: add starts_with() to be used instead of prefixcmp()
Date: Sun, 17 Nov 2013 23:05:53 +0100 [thread overview]
Message-ID: <20131117220719.4386.5213.chriscool@tuxfamily.org> (raw)
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
next prev parent reply other threads:[~2013-11-19 21:05 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-11-17 22:05 [PATCH v2 00/86] replace prefixcmp() with starts_with() Christian Couder
2013-11-17 22:05 ` Christian Couder [this message]
2013-11-17 22:05 ` [PATCH v2 02/86] diff: " Christian Couder
2013-11-17 22:06 ` [PATCH v2 08/86] transport*: " Christian Couder
2013-11-17 22:06 ` [PATCH v2 40/86] environment: " Christian Couder
2013-11-17 22:07 ` [PATCH v2 86/86] strbuf: remove prefixcmp() as it has been replaced " Christian Couder
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=20131117220719.4386.5213.chriscool@tuxfamily.org \
--to=chriscool@tuxfamily.org \
--cc=Johannes.Schindelin@gmx.de \
--cc=ae@op5.se \
--cc=apenwarr@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=jrnieder@gmail.com \
--cc=max@quendi.de \
--cc=peff@peff.net \
/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).