* [PATCH 1/5] environment: normalize use of prefixcmp() by removing " != 0"
2013-12-01 7:49 [PATCH 0/5] use starts_with() and ends_with() Christian Couder
@ 2013-12-01 7:49 ` Christian Couder
2013-12-01 7:49 ` [PATCH 2/5] builtin/remote: remove postfixcmp() and use suffixcmp() instead Christian Couder
` (4 subsequent siblings)
5 siblings, 0 replies; 17+ messages in thread
From: Christian Couder @ 2013-12-01 7:49 UTC (permalink / raw)
To: Junio C Hamano
Cc: git, Jeff King, Antoine Pelisse, Avery Pennarun,
Johannes Schindelin, Jonathan Nieder, Max Horn, Andreas Ericsson
To be able to automatically convert prefixcmp() to starts_with()
we need first to make sure that prefixcmp() is always used in
the same way.
So let's remove " != 0" after prefixcmp().
Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
environment.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/environment.c b/environment.c
index 0a15349..cd2b068 100644
--- a/environment.c
+++ b/environment.c
@@ -171,7 +171,7 @@ const char *get_git_namespace(void)
const char *strip_namespace(const char *namespaced_ref)
{
- if (prefixcmp(namespaced_ref, get_git_namespace()) != 0)
+ if (prefixcmp(namespaced_ref, get_git_namespace()))
return NULL;
return namespaced_ref + namespace_len;
}
--
1.8.4.1.561.g12affca
^ permalink raw reply related [flat|nested] 17+ messages in thread
* [PATCH 2/5] builtin/remote: remove postfixcmp() and use suffixcmp() instead
2013-12-01 7:49 [PATCH 0/5] use starts_with() and ends_with() Christian Couder
2013-12-01 7:49 ` [PATCH 1/5] environment: normalize use of prefixcmp() by removing " != 0" Christian Couder
@ 2013-12-01 7:49 ` Christian Couder
2013-12-01 7:49 ` [PATCH 3/5] strbuf: introduce starts_with() and ends_with() Christian Couder
` (3 subsequent siblings)
5 siblings, 0 replies; 17+ messages in thread
From: Christian Couder @ 2013-12-01 7:49 UTC (permalink / raw)
To: Junio C Hamano
Cc: git, Jeff King, Antoine Pelisse, Avery Pennarun,
Johannes Schindelin, Jonathan Nieder, Max Horn, Andreas Ericsson
Commit 8cc5b290 (git merge -X<option>, 25 Nov 2009) introduced
suffixcmp() with nearly the same implementation as postfixcmp()
that already existed since commit 211c8968 (Make git-remote a
builtin, 29 Feb 2008).
The only difference between the two implementations is that,
when the string is smaller than the suffix, one implementation
returns 1 while the other one returns -1.
But, as postfixcmp() is only used to compare for equality, the
distinction does not matter and does not affect the correctness of
this patch.
As postfixcmp() has always been static in builtin/remote.c
and is used nowhere else, it makes more sense to remove it
and use suffixcmp() instead in builtin/remote.c, rather than
to remove suffixcmp().
Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
builtin/remote.c | 14 +++-----------
1 file changed, 3 insertions(+), 11 deletions(-)
diff --git a/builtin/remote.c b/builtin/remote.c
index 4e14891..9b3a98e 100644
--- a/builtin/remote.c
+++ b/builtin/remote.c
@@ -80,14 +80,6 @@ static int verbose;
static int show_all(void);
static int prune_remote(const char *remote, int dry_run);
-static inline int postfixcmp(const char *string, const char *postfix)
-{
- int len1 = strlen(string), len2 = strlen(postfix);
- if (len1 < len2)
- return 1;
- return strcmp(string + len1 - len2, postfix);
-}
-
static int fetch_remote(const char *name)
{
const char *argv[] = { "fetch", name, NULL, NULL };
@@ -277,13 +269,13 @@ static int config_read_branches(const char *key, const char *value, void *cb)
enum { REMOTE, MERGE, REBASE } type;
key += 7;
- if (!postfixcmp(key, ".remote")) {
+ if (!suffixcmp(key, ".remote")) {
name = xstrndup(key, strlen(key) - 7);
type = REMOTE;
- } else if (!postfixcmp(key, ".merge")) {
+ } else if (!suffixcmp(key, ".merge")) {
name = xstrndup(key, strlen(key) - 6);
type = MERGE;
- } else if (!postfixcmp(key, ".rebase")) {
+ } else if (!suffixcmp(key, ".rebase")) {
name = xstrndup(key, strlen(key) - 7);
type = REBASE;
} else
--
1.8.4.1.561.g12affca
^ permalink raw reply related [flat|nested] 17+ messages in thread
* [PATCH 3/5] strbuf: introduce starts_with() and ends_with()
2013-12-01 7:49 [PATCH 0/5] use starts_with() and ends_with() Christian Couder
2013-12-01 7:49 ` [PATCH 1/5] environment: normalize use of prefixcmp() by removing " != 0" Christian Couder
2013-12-01 7:49 ` [PATCH 2/5] builtin/remote: remove postfixcmp() and use suffixcmp() instead Christian Couder
@ 2013-12-01 7:49 ` Christian Couder
2013-12-01 7:49 ` [PATCH 5/5] strbuf: remove prefixcmp() and suffixcmp() Christian Couder
` (2 subsequent siblings)
5 siblings, 0 replies; 17+ messages in thread
From: Christian Couder @ 2013-12-01 7:49 UTC (permalink / raw)
To: Junio C Hamano
Cc: git, Jeff King, Antoine Pelisse, Avery Pennarun,
Johannes Schindelin, Jonathan Nieder, Max Horn, Andreas Ericsson
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
^ permalink raw reply related [flat|nested] 17+ messages in thread
* [PATCH 5/5] strbuf: remove prefixcmp() and suffixcmp()
2013-12-01 7:49 [PATCH 0/5] use starts_with() and ends_with() Christian Couder
` (2 preceding siblings ...)
2013-12-01 7:49 ` [PATCH 3/5] strbuf: introduce starts_with() and ends_with() Christian Couder
@ 2013-12-01 7:49 ` Christian Couder
2013-12-02 15:09 ` [PATCH 0/5] use starts_with() and ends_with() Jeff King
[not found] ` <20131201074919.3042.92026.chriscool@tuxfamily.org>
5 siblings, 0 replies; 17+ messages in thread
From: Christian Couder @ 2013-12-01 7:49 UTC (permalink / raw)
To: Junio C Hamano
Cc: git, Jeff King, Antoine Pelisse, Avery Pennarun,
Johannes Schindelin, Jonathan Nieder, Max Horn, Andreas Ericsson
As starts_with() and ends_with() have been used to
replace prefixcmp() and suffixcmp() respectively,
we can now remove them.
Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
git-compat-util.h | 2 --
strbuf.c | 18 ------------------
2 files changed, 20 deletions(-)
diff --git a/git-compat-util.h b/git-compat-util.h
index b73916b..c4c01e7 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -351,9 +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 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 83caf4a..ee96dcf 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -10,15 +10,6 @@ int starts_with(const char *str, const char *prefix)
return 0;
}
-int prefixcmp(const char *str, const char *prefix)
-{
- for (; ; str++, prefix++)
- if (!*prefix)
- return 0;
- else if (*str != *prefix)
- return (unsigned char)*prefix - (unsigned char)*str;
-}
-
int ends_with(const char *str, const char *suffix)
{
int len = strlen(str), suflen = strlen(suffix);
@@ -28,15 +19,6 @@ int ends_with(const char *str, const char *suffix)
return !strcmp(str + len - suflen, suffix);
}
-int suffixcmp(const char *str, const char *suffix)
-{
- int len = strlen(str), suflen = strlen(suffix);
- if (len < suflen)
- return -1;
- 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
--
1.8.4.1.561.g12affca
^ permalink raw reply related [flat|nested] 17+ messages in thread
* Re: [PATCH 0/5] use starts_with() and ends_with()
2013-12-01 7:49 [PATCH 0/5] use starts_with() and ends_with() Christian Couder
` (3 preceding siblings ...)
2013-12-01 7:49 ` [PATCH 5/5] strbuf: remove prefixcmp() and suffixcmp() Christian Couder
@ 2013-12-02 15:09 ` Jeff King
2013-12-02 18:32 ` Junio C Hamano
[not found] ` <20131201074919.3042.92026.chriscool@tuxfamily.org>
5 siblings, 1 reply; 17+ messages in thread
From: Jeff King @ 2013-12-02 15:09 UTC (permalink / raw)
To: Christian Couder
Cc: Junio C Hamano, git, Antoine Pelisse, Avery Pennarun,
Johannes Schindelin, Jonathan Nieder, Max Horn, Andreas Ericsson
On Sun, Dec 01, 2013 at 08:49:13AM +0100, Christian Couder wrote:
> This is a new patch series along the lines Junio suggested in this
> thread:
>
> http://thread.gmane.org/gmane.comp.version-control.git/238054/
>
> I send it now because I saw a 1.8.5 tag.
This looks sane to me. Your 4/5 did not make it to the list (nor
directly to me), though. Perhaps because it is huge?
-Peff
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH 0/5] use starts_with() and ends_with()
2013-12-02 15:09 ` [PATCH 0/5] use starts_with() and ends_with() Jeff King
@ 2013-12-02 18:32 ` Junio C Hamano
2013-12-02 19:29 ` Christian Couder
0 siblings, 1 reply; 17+ messages in thread
From: Junio C Hamano @ 2013-12-02 18:32 UTC (permalink / raw)
To: Jeff King
Cc: Christian Couder, git, Antoine Pelisse, Avery Pennarun,
Johannes Schindelin, Jonathan Nieder, Max Horn, Andreas Ericsson
Jeff King <peff@peff.net> writes:
> On Sun, Dec 01, 2013 at 08:49:13AM +0100, Christian Couder wrote:
>
>> This is a new patch series along the lines Junio suggested in this
>> thread:
>>
>> http://thread.gmane.org/gmane.comp.version-control.git/238054/
>>
>> I send it now because I saw a 1.8.5 tag.
>
> This looks sane to me. Your 4/5 did not make it to the list (nor
> directly to me), though. Perhaps because it is huge?
Nor to me, either.
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH 0/5] use starts_with() and ends_with()
2013-12-02 18:32 ` Junio C Hamano
@ 2013-12-02 19:29 ` Christian Couder
2013-12-02 19:32 ` Antoine Pelisse
0 siblings, 1 reply; 17+ messages in thread
From: Christian Couder @ 2013-12-02 19:29 UTC (permalink / raw)
To: gitster
Cc: peff, git, apelisse, apenwarr, Johannes.Schindelin, jrnieder, max,
ae
From: Junio C Hamano <gitster@pobox.com>
>
> Jeff King <peff@peff.net> writes:
>
>> On Sun, Dec 01, 2013 at 08:49:13AM +0100, Christian Couder wrote:
>>
>>> This is a new patch series along the lines Junio suggested in this
>>> thread:
>>>
>>> http://thread.gmane.org/gmane.comp.version-control.git/238054/
>>>
>>> I send it now because I saw a 1.8.5 tag.
>>
>> This looks sane to me. Your 4/5 did not make it to the list (nor
>> directly to me), though. Perhaps because it is huge?
>
> Nor to me, either.
Sorry about that I don't know what happened. Maybe my ISP has blocked
it because it is big.
I just tried to send it again.
Best,
Christian.
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH 0/5] use starts_with() and ends_with()
2013-12-02 19:29 ` Christian Couder
@ 2013-12-02 19:32 ` Antoine Pelisse
2013-12-03 6:06 ` Christian Couder
0 siblings, 1 reply; 17+ messages in thread
From: Antoine Pelisse @ 2013-12-02 19:32 UTC (permalink / raw)
To: Christian Couder
Cc: Junio C Hamano, Jeff King, git, Avery Pennarun,
Johannes Schindelin, Jonathan Nieder, Max Horn, Andreas Ericsson
On Mon, Dec 2, 2013 at 8:29 PM, Christian Couder
<chriscool@tuxfamily.org> wrote:
> From: Junio C Hamano <gitster@pobox.com>
>>
>> Jeff King <peff@peff.net> writes:
>>
>>> On Sun, Dec 01, 2013 at 08:49:13AM +0100, Christian Couder wrote:
>>>
>>>> This is a new patch series along the lines Junio suggested in this
>>>> thread:
>>>>
>>>> http://thread.gmane.org/gmane.comp.version-control.git/238054/
>>>>
>>>> I send it now because I saw a 1.8.5 tag.
>>>
>>> This looks sane to me. Your 4/5 did not make it to the list (nor
>>> directly to me), though. Perhaps because it is huge?
>>
>> Nor to me, either.
>
> Sorry about that I don't know what happened. Maybe my ISP has blocked
> it because it is big.
I don't know, I received it.
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH 0/5] use starts_with() and ends_with()
2013-12-02 19:32 ` Antoine Pelisse
@ 2013-12-03 6:06 ` Christian Couder
0 siblings, 0 replies; 17+ messages in thread
From: Christian Couder @ 2013-12-03 6:06 UTC (permalink / raw)
To: Antoine Pelisse
Cc: Christian Couder, Junio C Hamano, Jeff King, git, Avery Pennarun,
Johannes Schindelin, Jonathan Nieder, Max Horn, Andreas Ericsson
On Mon, Dec 2, 2013 at 8:32 PM, Antoine Pelisse <apelisse@gmail.com> wrote:
> On Mon, Dec 2, 2013 at 8:29 PM, Christian Couder
> <chriscool@tuxfamily.org> wrote:
>> From: Junio C Hamano <gitster@pobox.com>
>>>
>>> Jeff King <peff@peff.net> writes:
>>>
>>>> This looks sane to me. Your 4/5 did not make it to the list (nor
>>>> directly to me), though. Perhaps because it is huge?
>>>
>>> Nor to me, either.
>>
>> Sorry about that I don't know what happened. Maybe my ISP has blocked
>> it because it is big.
>
> I don't know, I received it.
When I send it to my gmail account, I receive it too.
^ permalink raw reply [flat|nested] 17+ messages in thread
[parent not found: <20131201074919.3042.92026.chriscool@tuxfamily.org>]