From: Junio C Hamano Date: Mon, 17 Mar 2014 23:07:00 GMT Subject: Re: [PATCH 1/1] general style: replaces memcmp() with starts_with() Message-ID: In-Reply-To: <20140317230139.GB19578@sigill.intra.peff.net> Jeff King writes: > On Mon, Mar 17, 2014 at 03:52:51PM -0700, Junio C Hamano wrote: > >> > diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c >> > index 3e1d5c3..4135980 100644 >> > --- a/builtin/for-each-ref.c >> > +++ b/builtin/for-each-ref.c >> > @@ -193,7 +193,7 @@ static int verify_format(const char *format) >> > at = parse_atom(sp + 2, ep); >> > cp = ep + 1; >> > >> > - if (!memcmp(used_atom[at], "color:", 6)) >> > + if (starts_with(used_atom[at], "color:")) >> > need_color_reset_at_eol = !!strcmp(used_atom[at], color_reset); >> > } >> > return 0; >> >> Good. > > Actually, I found this one confusing. We are looking for "color:", but > if we find it, we _don't_ skip past and look at what comes after. > Instead, we compare the whole string. Which works because color_reset > actually contains "color:reset", and we end up just re-comparing the > first bit of the string. So the memcmp here is redundant, and this can > simply become: > > need_color_reset_at_eol = !!strcmp(used_atom[at], color_reset); > > Or am I missing something? What if used_atom[at] is not related to color at all? We do not want to touch the variable.