Skip to content
  • Jeff King's avatar
    use skip_prefix to avoid magic numbers · ae021d87
    Jeff King authored and Junio C Hamano's avatar Junio C Hamano committed
    
    
    It's a common idiom to match a prefix and then skip past it
    with a magic number, like:
    
      if (starts_with(foo, "bar"))
    	  foo += 3;
    
    This is easy to get wrong, since you have to count the
    prefix string yourself, and there's no compiler check if the
    string changes.  We can use skip_prefix to avoid the magic
    numbers here.
    
    Note that some of these conversions could be much shorter.
    For example:
    
      if (starts_with(arg, "--foo=")) {
    	  bar = arg + 6;
    	  continue;
      }
    
    could become:
    
      if (skip_prefix(arg, "--foo=", &bar))
    	  continue;
    
    However, I have left it as:
    
      if (skip_prefix(arg, "--foo=", &v)) {
    	  bar = v;
    	  continue;
      }
    
    to visually match nearby cases which need to actually
    process the string. Like:
    
      if (skip_prefix(arg, "--foo=", &v)) {
    	  bar = atoi(v);
    	  continue;
      }
    
    Signed-off-by: default avatarJeff King <peff@peff.net>
    Signed-off-by: default avatarJunio C Hamano <gitster@pobox.com>
    ae021d87