mirror of
https://github.com/git/git.git
synced 2025-12-12 20:36:24 +01:00
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>
This commit is contained in:
committed by
Junio C Hamano
parent
20869d1a1d
commit
d43b99322b
@@ -169,6 +169,8 @@ static int command_loop(const char *child)
|
|||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
size_t i;
|
size_t i;
|
||||||
|
const char *arg;
|
||||||
|
|
||||||
if (!fgets(buffer, MAXCOMMAND - 1, stdin)) {
|
if (!fgets(buffer, MAXCOMMAND - 1, stdin)) {
|
||||||
if (ferror(stdin))
|
if (ferror(stdin))
|
||||||
die("Command input error");
|
die("Command input error");
|
||||||
@@ -182,10 +184,10 @@ static int command_loop(const char *child)
|
|||||||
if (!strcmp(buffer, "capabilities")) {
|
if (!strcmp(buffer, "capabilities")) {
|
||||||
printf("*connect\n\n");
|
printf("*connect\n\n");
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
} else if (!strncmp(buffer, "connect ", 8)) {
|
} else if (skip_prefix(buffer, "connect ", &arg)) {
|
||||||
printf("\n");
|
printf("\n");
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
return run_child(child, buffer + 8);
|
return run_child(child, arg);
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "Bad command");
|
fprintf(stderr, "Bad command");
|
||||||
return 1;
|
return 1;
|
||||||
|
|||||||
7
ws.c
7
ws.c
@@ -29,6 +29,7 @@ unsigned parse_whitespace_rule(const char *string)
|
|||||||
int i;
|
int i;
|
||||||
size_t len;
|
size_t len;
|
||||||
const char *ep;
|
const char *ep;
|
||||||
|
const char *arg;
|
||||||
int negated = 0;
|
int negated = 0;
|
||||||
|
|
||||||
string = string + strspn(string, ", \t\n\r");
|
string = string + strspn(string, ", \t\n\r");
|
||||||
@@ -52,15 +53,15 @@ unsigned parse_whitespace_rule(const char *string)
|
|||||||
rule |= whitespace_rule_names[i].rule_bits;
|
rule |= whitespace_rule_names[i].rule_bits;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (strncmp(string, "tabwidth=", 9) == 0) {
|
if (skip_prefix(string, "tabwidth=", &arg)) {
|
||||||
unsigned tabwidth = atoi(string + 9);
|
unsigned tabwidth = atoi(arg);
|
||||||
if (0 < tabwidth && tabwidth < 0100) {
|
if (0 < tabwidth && tabwidth < 0100) {
|
||||||
rule &= ~WS_TAB_WIDTH_MASK;
|
rule &= ~WS_TAB_WIDTH_MASK;
|
||||||
rule |= tabwidth;
|
rule |= tabwidth;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
warning("tabwidth %.*s out of range",
|
warning("tabwidth %.*s out of range",
|
||||||
(int)(len - 9), string + 9);
|
(int)(ep - arg), arg);
|
||||||
}
|
}
|
||||||
string = ep;
|
string = ep;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user