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>

Jeff King committed Jan 7, 2023 at 08:26 UTC 20869d1a1d30e9a64c66953a0f4c7245089009cf
4 files changed +6 -6
builtin/remote-fd.c
+1 -1
@@ -40,7 +40,7 @@ static void command_loop(int input_fd, int output_fd)
40 if (!strcmp(buffer, "capabilities")) {
41 printf("*connect\n\n");
42 fflush(stdout);
43 - } else if (!strncmp(buffer, "connect ", 8)) {
43 + } else if (starts_with(buffer, "connect ")) {
44 printf("\n");
45 fflush(stdout);
46 if (bidirectional_transfer_loop(input_fd,
bundle-uri.c
+1 -1
@@ -620,7 +620,7 @@ static int config_to_packet_line(const char *key, const char *value, void *data)
620 {
621 struct packet_reader *writer = data;
622
623 - if (!strncmp(key, "bundle.", 7))
623 + if (starts_with(key, "bundle."))
624 packet_write_fmt(writer->fd, "%s=%s", key, value);
625
626 return 0;
ref-filter.c
+1 -1
@@ -1209,7 +1209,7 @@ static const char *copy_name(const char *buf)
1209 {
1210 const char *cp;
1211 for (cp = buf; *cp && *cp != '\n'; cp++) {
1212 - if (!strncmp(cp, " <", 2))
1212 + if (starts_with(cp, " <"))
1213 return xmemdupz(buf, cp - buf);
1214 }
1215 return xstrdup("");
urlmatch.c
+3 -3
@@ -209,7 +209,7 @@ static char *url_normalize_1(const char *url, struct url_info *out_info, char al
209 */
210 if (!url_len || strchr(":/?#", *url)) {
211 /* Missing host invalid for all URL schemes except file */
212 - if (strncmp(norm.buf, "file:", 5)) {
212 + if (!starts_with(norm.buf, "file:")) {
213 if (out_info) {
214 out_info->url = NULL;
215 out_info->err = _("missing host and scheme is not 'file:'");
@@ -268,11 +268,11 @@ static char *url_normalize_1(const char *url, struct url_info *out_info, char al
268 if (url == slash_ptr) {
269 /* Skip ":" port with no number, it's same as default */
270 } else if (slash_ptr - url == 2 &&
271 - !strncmp(norm.buf, "http:", 5) &&
271 + starts_with(norm.buf, "http:") &&
272 !strncmp(url, "80", 2)) {
273 /* Skip http :80 as it's the default */
274 } else if (slash_ptr - url == 3 &&
275 - !strncmp(norm.buf, "https:", 6) &&
275 + starts_with(norm.buf, "https:") &&
276 !strncmp(url, "443", 3)) {
277 /* Skip https :443 as it's the default */
278 } else {