Merge branch 'jk/strncmp-to-api-funcs'

Code clean-up.

* jk/strncmp-to-api-funcs:
  convert trivial uses of strncmp() to skip_prefix()
  convert trivial uses of strncmp() to starts_with()
This commit is contained in:
Junio C Hamano
2023-01-16 12:07:47 -08:00
6 changed files with 14 additions and 11 deletions

View File

@ -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;

View File

@ -40,7 +40,7 @@ static void command_loop(int input_fd, int output_fd)
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 (starts_with(buffer, "connect ")) {
printf("\n"); printf("\n");
fflush(stdout); fflush(stdout);
if (bidirectional_transfer_loop(input_fd, if (bidirectional_transfer_loop(input_fd,

View File

@ -620,7 +620,7 @@ static int config_to_packet_line(const char *key, const char *value, void *data)
{ {
struct packet_reader *writer = data; struct packet_reader *writer = data;
if (!strncmp(key, "bundle.", 7)) if (starts_with(key, "bundle."))
packet_write_fmt(writer->fd, "%s=%s", key, value); packet_write_fmt(writer->fd, "%s=%s", key, value);
return 0; return 0;

View File

@ -1209,7 +1209,7 @@ static const char *copy_name(const char *buf)
{ {
const char *cp; const char *cp;
for (cp = buf; *cp && *cp != '\n'; cp++) { for (cp = buf; *cp && *cp != '\n'; cp++) {
if (!strncmp(cp, " <", 2)) if (starts_with(cp, " <"))
return xmemdupz(buf, cp - buf); return xmemdupz(buf, cp - buf);
} }
return xstrdup(""); return xstrdup("");

View File

@ -209,7 +209,7 @@ static char *url_normalize_1(const char *url, struct url_info *out_info, char al
*/ */
if (!url_len || strchr(":/?#", *url)) { if (!url_len || strchr(":/?#", *url)) {
/* Missing host invalid for all URL schemes except file */ /* Missing host invalid for all URL schemes except file */
if (strncmp(norm.buf, "file:", 5)) { if (!starts_with(norm.buf, "file:")) {
if (out_info) { if (out_info) {
out_info->url = NULL; out_info->url = NULL;
out_info->err = _("missing host and scheme is not 'file:'"); 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
if (url == slash_ptr) { if (url == slash_ptr) {
/* Skip ":" port with no number, it's same as default */ /* Skip ":" port with no number, it's same as default */
} else if (slash_ptr - url == 2 && } else if (slash_ptr - url == 2 &&
!strncmp(norm.buf, "http:", 5) && starts_with(norm.buf, "http:") &&
!strncmp(url, "80", 2)) { !strncmp(url, "80", 2)) {
/* Skip http :80 as it's the default */ /* Skip http :80 as it's the default */
} else if (slash_ptr - url == 3 && } else if (slash_ptr - url == 3 &&
!strncmp(norm.buf, "https:", 6) && starts_with(norm.buf, "https:") &&
!strncmp(url, "443", 3)) { !strncmp(url, "443", 3)) {
/* Skip https :443 as it's the default */ /* Skip https :443 as it's the default */
} else { } else {

7
ws.c
View File

@ -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;
} }