Subject: Re: [PATCH 01/12] Make starts_with() a wrapper of skip_prefix()

From: Junio C Hamano <[email-masked]>
Date: Wed, 18 Dec 2013 10:16:17 -0800

Junio C Hamano <[email-masked]> writes:

> Nguyễn Thái Ngọc Duy  <[email-masked]> writes:
>
>> starts_with() started out as a copy of prefixcmp(). But if we don't
>> care about the sorting order, the logic looks closer to
>> skip_prefix(). This looks like a good thing to do.
>>
>> Signed-off-by: Nguyễn Thái Ngọc Duy <[email-masked]>
>> ---
>
> Sure, but the implementation of skip_prefix() scans the prefix
> string twice, while prefixcmp() aka starts_with() does it only once.
>
> I'd expect a later step in this series would rectify this micro
> regression in the performance, though ;-)

... and I did not see one, but it would be trivial on top.

 git-compat-util.h | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/git-compat-util.h b/git-compat-util.h
index b72a80d..59265af 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -356,8 +356,11 @@ extern int suffixcmp(const char *str, const char *suffix);
 
 static inline const char *skip_prefix_defval(const char *str, const char *prefix, const char *defval)
 {
-	size_t len = strlen(prefix);
-	return strncmp(str, prefix, len) ? defval : str + len;
+	for ( ; ; str++, prefix++)
+		if (!*prefix)
+			return str;
+		else if (*str != *prefix)
+			return defval;
 }
 
 static inline const char *skip_prefix(const char *str, const char *prefix)