Skip to content
  • Jeff King's avatar
    use strip_suffix instead of ends_with in simple cases · 26936bfd
    Jeff King authored and Junio C Hamano's avatar Junio C Hamano committed
    
    
    When stripping a suffix like:
    
      if (ends_with(str, "foo"))
    	buf = xmemdupz(str, strlen(str) - 3);
    
    we can instead use strip_suffix to avoid the constant 3,
    which must match the literal "foo" (we sometimes use
    strlen("foo") instead, but that means we are repeating
    ourselves). The example above becomes:
    
      if (strip_suffix(str, "foo", &len))
    	buf = xmemdupz(str, len);
    
    This also saves a strlen(), since we calculate the string
    length when detecting the suffix.
    
    Note that in some cases we also switch from xstrndup to
    xmemdupz, which saves a further strlen call.
    
    Signed-off-by: default avatarJeff King <peff@peff.net>
    Signed-off-by: default avatarJunio C Hamano <gitster@pobox.com>
    26936bfd