]> git.ipfire.org Git - thirdparty/git.git/commit - ref-filter.c
convert trivial uses of strncmp() to starts_with()
authorJeff King <peff@peff.net>
Sat, 7 Jan 2023 13:26:18 +0000 (08:26 -0500)
committerJunio C Hamano <gitster@pobox.com>
Sun, 8 Jan 2023 01:34:35 +0000 (10:34 +0900)
commit20869d1a1d30e9a64c66953a0f4c7245089009cf
treef532412f1835adf97c7c0c9a823494cd90b206c3
parent4dbebc36b0893f5094668ddea077d0e235560b16
convert trivial uses of strncmp() to starts_with()

It's more readable to use starts_with() instead of strncmp() to match a
prefix, as the latter requires a manually-computed length, and has the
funny "matching is zero" return value common to cmp functions.  This
patch converts several cases which were found with:

  git grep 'strncmp(.*, [0-9]*)'

But note that it doesn't convert all such cases. There are several where
the magic length number is repeated elsewhere in the code, like:

  /* handle "buf" which isn't NUL-terminated and might be too small */
  if (len >= 3 && !strncmp(buf, "foo", 3))

or:

  /* exact match for "foo", but within a larger string */
  if (end - buf == 3 && !strncmp(buf, "foo", 3))

While it would not produce the wrong outcome to use starts_with() in
these cases, we'd still be left with one instance of "3". We're better
to leave them for now, as the repeated "3" makes it clear that the two
are linked (there may be other refactorings that handle both, but
they're out of scope for this patch).

A few things to note while reading the patch:

  - all cases but one are trying to match, and so lose the extra "!".
    The case in the first hunk of urlmatch.c is not-matching, and hence
    gains a "!".

  - the case in remote-fd.c is matching the beginning of "connect foo",
    but we never look at str+8 to parse the "foo" part (which would make
    this a candidate for skip_prefix(), not starts_with()). This seems
    at first glance like a bug, but is a limitation of how remote-fd
    works.

  - the second hunk in urlmatch.c shows some cases adjacent to other
    strncmp() calls that are left. These are of the "exact match within
    a larger string" type, as described above.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/remote-fd.c
bundle-uri.c
ref-filter.c
urlmatch.c