Re: [PATCH v3] skip_prefix: rewrite so that prefix is scanned once
- From
David Kastrup <dak@gnu.org>
- Date
- Mar 3, 2014, 23:22 UTC
- Message-ID
- <87bnxmkguw.fsf@fencepost.gnu.org>
- In-Reply-To
- <xmqq61nuoqd5.fsf@gitster.dls.corp.google.com>
Junio C Hamano <gitster@pobox.com> writes:
Show 17 quoted lines
> --- a/git-compat-util.h
> +++ b/git-compat-util.h
> @@ -357,8 +357,14 @@ extern int suffixcmp(const char *str, const char *suffix);
>
> static inline const char *skip_prefix(const char *str, const char *prefix)
> {
> - size_t len = strlen(prefix);
> - return strncmp(str, prefix, len) ? NULL : str + len;
> + while (1) {
> + if (!*prefix)
> + return str;
> + if (*str != *prefix)
> + return NULL;
> + prefix++;
> + str++;
> + }
> }How about a function body of
do {
if (!*prefix)
return str;
} while (*str++ == *prefix++);
return NULL;I'm not too fond of while (1) and tend to use for (;;) instead, but that may again partly be due to some incredibly non-optimizing compiler back in the days of my youth. At any rate, the do-while loop seems a bit brisker.
-- David Kastrup