git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 1/1] general style: replaces memcmp() with starts_with()

From
Jeff King <peff@peff.net>
Date
Mar 17, 2014, 23:01 UTC
Message-ID
<20140317230139.GB19578@sigill.intra.peff.net>
In-Reply-To
<xmqqob14a14s.fsf@gitster.dls.corp.google.com>
On Mon, Mar 17, 2014 at 03:52:51PM -0700, Junio C Hamano wrote:
Show 15 quoted lines
> > 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?
-Peff
Previous: Junio C HamanoNext: Junio C Hamano
Message 4 of 12 in “general style: replaces memcmp() with starts_with()”
  1. 0/1 general style: replaces memcmp() with starts_with()Quint Guvernator, Mar 17, 2014
  2. 1/1 general style: replaces memcmp() with starts_with()Quint Guvernator, Mar 17, 2014
  3. Junio C HamanoMar 17, 2014
  4. Jeff KingMar 17, 2014
  5. Junio C HamanoMar 17, 2014
  6. Jeff KingMar 18, 2014
  7. Quint GuvernatorMar 17, 2014
  8. Eric SunshineMar 18, 2014
  9. Quint GuvernatorMar 18, 2014
  10. Eric SunshineMar 18, 2014
  11. Junio C HamanoMar 18, 2014
  12. Quint GuvernatorMar 17, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.