convert trivial uses of strncmp() to skip_prefix()
As with the previous patch, using skip_prefix() is more readable and less error-prone than a raw strncmp(), because it avoids a manually-computed length. These cases differ from the previous patch that uses starts_with() because they care about the value after the matched prefix. We can convert these to use skip_prefix() by introducing an extra variable to hold the out-pointer. Note in the case in ws.c that to get rid of the magic number "9" completely, we also switch out "len" for recomputing the pointer difference. These are equivalent because "len" is always "ep - string". 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
d43b99322bd0ca4a371901bcf6705f1282143a2b
2 files changed
+8
-5
builtin/remote-ext.c
+4
-2
@@ -169,6 +169,8 @@ static int command_loop(const char *child)
169
170
while (1) {
171
size_t i;
172
+ const char *arg;
173
+
174
if (!fgets(buffer, MAXCOMMAND - 1, stdin)) {
175
if (ferror(stdin))
176
die("Command input error");
@@ -182,10 +184,10 @@ static int command_loop(const char *child)
184
if (!strcmp(buffer, "capabilities")) {
185
printf("*connect\n\n");
186
fflush(stdout);
185
- } else if (!strncmp(buffer, "connect ", 8)) {
187
+ } else if (skip_prefix(buffer, "connect ", &arg)) {
188
printf("\n");
189
fflush(stdout);
188
- return run_child(child, buffer + 8);
190
+ return run_child(child, arg);
191
} else {
192
fprintf(stderr, "Bad command");
193
return 1;
ws.c
+4
-3
@@ -29,6 +29,7 @@ unsigned parse_whitespace_rule(const char *string)
29
int i;
30
size_t len;
31
const char *ep;
32
+ const char *arg;
33
int negated = 0;
34
35
string = string + strspn(string, ", \t\n\r");
@@ -52,15 +53,15 @@ unsigned parse_whitespace_rule(const char *string)
53
rule |= whitespace_rule_names[i].rule_bits;
54
break;
55
}
55
- if (strncmp(string, "tabwidth=", 9) == 0) {
56
- unsigned tabwidth = atoi(string + 9);
56
+ if (skip_prefix(string, "tabwidth=", &arg)) {
57
+ unsigned tabwidth = atoi(arg);
58
if (0 < tabwidth && tabwidth < 0100) {
59
rule &= ~WS_TAB_WIDTH_MASK;
60
rule |= tabwidth;
61
}
62
else
63
warning("tabwidth %.*s out of range",
63
- (int)(len - 9), string + 9);
64
+ (int)(ep - arg), arg);
65
}
66
string = ep;
67
}