Skip to content

Commit d43b993

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 20869d1 commit d43b993

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

builtin/remote-ext.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ static int command_loop(const char *child)
169169

170170
while (1) {
171171
size_t i;
172+
const char *arg;
173+
172174
if (!fgets(buffer, MAXCOMMAND - 1, stdin)) {
173175
if (ferror(stdin))
174176
die("Command input error");
@@ -182,10 +184,10 @@ static int command_loop(const char *child)
182184
if (!strcmp(buffer, "capabilities")) {
183185
printf("*connect\n\n");
184186
fflush(stdout);
185-
} else if (!strncmp(buffer, "connect ", 8)) {
187+
} else if (skip_prefix(buffer, "connect ", &arg)) {
186188
printf("\n");
187189
fflush(stdout);
188-
return run_child(child, buffer + 8);
190+
return run_child(child, arg);
189191
} else {
190192
fprintf(stderr, "Bad command");
191193
return 1;

ws.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ unsigned parse_whitespace_rule(const char *string)
2929
int i;
3030
size_t len;
3131
const char *ep;
32+
const char *arg;
3233
int negated = 0;
3334

3435
string = string + strspn(string, ", \t\n\r");
@@ -52,15 +53,15 @@ unsigned parse_whitespace_rule(const char *string)
5253
rule |= whitespace_rule_names[i].rule_bits;
5354
break;
5455
}
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);
5758
if (0 < tabwidth && tabwidth < 0100) {
5859
rule &= ~WS_TAB_WIDTH_MASK;
5960
rule |= tabwidth;
6061
}
6162
else
6263
warning("tabwidth %.*s out of range",
63-
(int)(len - 9), string + 9);
64+
(int)(ep - arg), arg);
6465
}
6566
string = ep;
6667
}

0 commit comments

Comments
 (0)