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

Re: [PATCH 02/12] Convert starts_with() to skip_prefix() for option parsing

From
Jeff King <peff@peff.net>
Date
Dec 20, 2013, 07:04 UTC
Message-ID
<20131220070449.GA29717@sigill.intra.peff.net>
In-Reply-To
<52B3E8D4.1030805@viscovery.net>
On Fri, Dec 20, 2013 at 07:51:00AM +0100, Johannes Sixt wrote:
Show 15 quoted lines
> >  	for (i = 1; i < argc && *argv[i] == '-'; i++) {
> >  		const char *arg = argv[i];
> > +		const char *optarg;
> >  
> > -		if (starts_with(arg, "--upload-pack=")) {
> > -			args.uploadpack = arg + 14;
> > +		if ((optarg = skip_prefix(arg, "--upload-pack=")) != NULL) {
> > +			args.uploadpack = optarg;
> 
> Quite frankly, I do not think this is an improvement. The old code is
> *MUCH* easier to understand because "starts_with" is clearly a predicate
> that is either true or false, but the code with "skip_prefix" is much
> heavier on the eye with its extra level of parenthesis. That it removes a
> hard-coded constant does not count much IMHO because it is very clear
> where the value comes from.

Yeah, I agree that is unfortunate. Maybe we could have the best of both worlds, like:

  if (starts_with(arg, "--upload-pack=", &optarg))
          ... use optarg ...

Probably we do not want to call it just "starts_with", as quite a few callers to do not care about what comes next, and would just pass NULL.

I cannot seem to think of a good name, though, as the "with" means that obvious things like "starts_with_value" naturally parse as a single (nonsensical) sentence. Something like "parse_prefix" would work, but it is not as clearly a predicate as "starts_with" (but we have at least gotten rid of the extra parentheses).

Elsewhere in the thread, the concept was discussed of returning the full string to mean "did not match", which makes some other idioms simpler (but IMHO makes the simple cases like this even harder to read). My proposal splits the "start of string" out parameter from the boolean return, so it handles both cases naturally:

  /* here we care if we saw the prefix, as above */
  if (parse_prefix(foo, prefix, &the_rest))
      ...
  /*
   * and here we do not care, and just want to optionally strip the
   * prefix, and take the full value otherwise; we just have to ignore
   * the return value in this case.
   */
  parse_prefix(foo, prefix, &foo);
-Peff
Previous: Johannes SixtNext: Christian Couder
Message 7 of 31 in “Hard coded string length cleanup”
  1. 00/12 Hard coded string length cleanupNguyễn Thái Ngọc Duy, Dec 18, 2013
  2. 01/12 Make starts_with() a wrapper of skip_prefix()Nguyễn Thái Ngọc Duy, Dec 18, 2013
  3. Junio C HamanoDec 18, 2013
  4. Junio C HamanoDec 18, 2013
  5. 02/12 Convert starts_with() to skip_prefix() for option parsingNguyễn Thái Ngọc Duy, Dec 18, 2013
  6. Johannes SixtDec 20, 2013
  7. Jeff KingDec 20, 2013
  8. Christian CouderDec 20, 2013
  9. René ScharfeDec 20, 2013
  10. Junio C HamanoDec 20, 2013
  11. Duy NguyenDec 21, 2013
  12. Junio C HamanoDec 26, 2013
  13. Jeff KingDec 28, 2013
  14. 03/12 Add and use skip_prefix_defval()Nguyễn Thái Ngọc Duy, Dec 18, 2013
  15. Kent R. SpillnerDec 18, 2013
  16. Junio C HamanoDec 18, 2013
  17. 04/12 Replace some use of starts_with() with skip_prefix()Nguyễn Thái Ngọc Duy, Dec 18, 2013
  18. 05/12 Convert a lot of starts_with() to skip_prefix()Nguyễn Thái Ngọc Duy, Dec 18, 2013
  19. 06/12 fetch.c: replace some use of starts_with() with skip_prefix()Nguyễn Thái Ngọc Duy, Dec 18, 2013
  20. 07/12 connect.c: replace some use of starts_with() with skip_prefix()Nguyễn Thái Ngọc Duy, Dec 18, 2013
  21. 08/12 refs.c: replace some use of starts_with() with skip_prefix()Nguyễn Thái Ngọc Duy, Dec 18, 2013
  22. 09/12 diff.c: reduce code duplication in --stat-xxx parsingNguyễn Thái Ngọc Duy, Dec 18, 2013
  23. 10/12 environment.c: replace starts_with() in strip_namespace() with skip_prefix()Nguyễn Thái Ngọc Duy, Dec 18, 2013
  24. 11/12 diff.c: convert diff_scoreopt_parse to use skip_prefix()Nguyễn Thái Ngọc Duy, Dec 18, 2013
  25. 12/12 refs.c: use skip_prefix() in prune_ref()Nguyễn Thái Ngọc Duy, Dec 18, 2013
  26. Junio C HamanoDec 18, 2013
  27. René ScharfeDec 19, 2013
  28. Duy NguyenDec 19, 2013
  29. René ScharfeDec 20, 2013
  30. Duy NguyenDec 20, 2013
  31. Junio C HamanoDec 20, 2013

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.