Skip to content

strncmp(str, "foo", sizeof("foo") - 1) is the same as memcmp(str, "foo", sizeof("foo") - 1), but slower #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 24 additions & 24 deletions kirc.c
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,7 @@ static void handle_dcc(param p)
int slot = -1;
int file_fd;

if (!strncmp(message, "SEND", sizeof("SEND") - 1)) {
if (!memcmp(message, "SEND", sizeof("SEND") - 1)) {
while(++slot < CON_MAX && dcc_sessions.slots[slot].file_fd >= 0);

if (slot == CON_MAX) {
Expand Down Expand Up @@ -960,7 +960,7 @@ static void handle_dcc(param p)
return;
}

if (!strncmp(message, "ACCEPT", sizeof("ACCEPT") - 1)) {
if (!memcmp(message, "ACCEPT", sizeof("ACCEPT") - 1)) {
if(parse_dcc_accept_message(message, filename, &port, &file_size)) {
return;
}
Expand All @@ -981,29 +981,29 @@ static void handle_dcc(param p)

static void handle_ctcp(param p)
{
if (p->message[0] != '\001' && strncmp(p->message, "ACTION", sizeof("ACTION") - 1)) {
if (p->message[0] != '\001' && memcmp(p->message, "ACTION", sizeof("ACTION") - 1)) {
return;
}
const char *message = p->message + 1;
if (!strncmp(message, "VERSION", sizeof("VERSION") - 1)) {
if (!memcmp(message, "VERSION", sizeof("VERSION") - 1)) {
raw("NOTICE %s :\001VERSION kirc " VERSION "\001\r\n", p->nickname);
return;
}
if (!strncmp(message, "TIME", sizeof("TIME") - 1)) {
if (!memcmp(message, "TIME", sizeof("TIME") - 1)) {
char buf[26];
if (!ctime_now(buf)) {
raw("NOTICE %s :\001TIME %s\001\r\n", p->nickname, buf);
}
return;
}
if (!strncmp(message, "CLIENTINFO", sizeof("CLIENTINFO") - 1)) {
if (!memcmp(message, "CLIENTINFO", sizeof("CLIENTINFO") - 1)) {
raw("NOTICE %s :\001CLIENTINFO " CTCP_CMDS "\001\r\n", p->nickname);
return;
}if (!strncmp(message, "PING", sizeof("PING") - 1)) {
}if (!memcmp(message, "PING", sizeof("PING") - 1)) {
raw("NOTICE %s :\001%s\r\n", p->nickname, message);
return;
}
if (!strncmp(message, "DCC", sizeof("DCC") - 1)) {
if (!memcmp(message, "DCC", sizeof("DCC") - 1)) {
handle_dcc(p);
return;
}
Expand Down Expand Up @@ -1049,7 +1049,7 @@ static void param_print_private(param p)
} else {
printf("%*s\x1b[33;1m%-.*s\x1b[0m ", s, "", p->nicklen, p->nickname);
}
if (!strncmp(p->message, "\x01" "ACTION", sizeof("ACTION"))) {
if (!memcmp(p->message, "\x01" "ACTION", sizeof("\x01" "ACTION") - 1)) {
p->message += sizeof("ACTION");
p->offset += sizeof("[ACTION] ");
printf("[ACTION] ");
Expand All @@ -1071,7 +1071,7 @@ static void param_print_channel(param p)

static void raw_parser(char *string)
{
if (!strncmp(string, "PING", sizeof("PING") - 1)) {
if (!memcmp(string, "PING", sizeof("PING") - 1)) {
string[1] = 'O';
raw("%s\r\n", string);
return;
Expand Down Expand Up @@ -1105,31 +1105,31 @@ static void raw_parser(char *string)
small_screen = 0;
p.nicklen = WRAP_LEN;
}
if (!strncmp(p.command, "001", sizeof("001") - 1) && *chan != '\0') {
if (!memcmp(p.command, "001", sizeof("001") - 1) && *chan != '\0') {
char *tok;
for (tok = strtok(chan, ",|"); tok != NULL; tok = strtok(NULL, ",|")) {
strcpy(chan, tok);
raw("JOIN #%s\r\n", tok);
}
return;
}
if (!strncmp(p.command, "QUIT", sizeof("QUIT") - 1)) {
if (!memcmp(p.command, "QUIT", sizeof("QUIT") - 1)) {
param_print_quit(&p);
printf("\x1b[0m\r\n");
return;
}if (!strncmp(p.command, "PART", sizeof("PART") - 1)) {
}if (!memcmp(p.command, "PART", sizeof("PART") - 1)) {
param_print_part(&p);
printf("\x1b[0m\r\n");
return;
}if (!strncmp(p.command, "JOIN", sizeof("JOIN") - 1)) {
}if (!memcmp(p.command, "JOIN", sizeof("JOIN") - 1)) {
param_print_join(&p);
printf("\x1b[0m\r\n");
return;
}if (!strncmp(p.command, "NICK", sizeof("NICK") - 1)) {
}if (!memcmp(p.command, "NICK", sizeof("NICK") - 1)) {
param_print_nick(&p);
printf("\x1b[0m\r\n");
return;
}if ((!strncmp(p.command, "PRIVMSG", sizeof("PRIVMSG") - 1)) || (!strncmp(p.command, "NOTICE", sizeof("NOTICE") - 1))) {
}if ((!memcmp(p.command, "PRIVMSG", sizeof("PRIVMSG") - 1)) || (!memcmp(p.command, "NOTICE", sizeof("NOTICE") - 1))) {
param_print_private(&p);
message_wrap(&p);
printf("\x1b[0m\r\n");
Expand Down Expand Up @@ -1231,7 +1231,7 @@ static void msg_command(state l)
offset ++;
}
raw("PRIVMSG %s :%s\r\n", l->buf + sizeof("msg") + offset, tok);
if (strncmp(l->buf + sizeof("msg") + offset, "NickServ", sizeof("NickServ") - 1)) {
if (memcmp(l->buf + sizeof("msg") + offset, "NickServ", sizeof("NickServ") - 1)) {
printf("\x1b[35mprivmsg %s :%s\x1b[0m\r\n", l->buf + sizeof("msg") + offset, tok);
}
}
Expand Down Expand Up @@ -1563,11 +1563,11 @@ static void handle_user_input(state l)
printf("\r\x1b[0K");
switch (l->buf[0]) {
case '/': /* send system command */
if (!strncmp(l->buf + 1, "JOIN", sizeof("JOIN") - 1) || !strncmp(l->buf + 1, "join", sizeof("join") - 1)) {
if (!memcmp(l->buf + 1, "JOIN", sizeof("JOIN") - 1) || !memcmp(l->buf + 1, "join", sizeof("join") - 1)) {
join_command(l);
return;
}
if (!strncmp(l->buf + 1, "PART", sizeof("PART") - 1) || !strncmp(l->buf + 1, "part", sizeof("part") - 1)) {
if (!memcmp(l->buf + 1, "PART", sizeof("PART") - 1) || !memcmp(l->buf + 1, "part", sizeof("part") - 1)) {
part_command(l);
return;
}
Expand All @@ -1578,23 +1578,23 @@ static void handle_user_input(state l)
"\x1b[35mprivmsg #%s :%s\x1b[0m\r\n", chan, l->buf + 2);
return;
}
if (!strncmp(l->buf + 1, "MSG", sizeof("MSG") - 1) || !strncmp(l->buf + 1, "msg", sizeof("msg") - 1)) {
if (!memcmp(l->buf + 1, "MSG", sizeof("MSG") - 1) || !memcmp(l->buf + 1, "msg", sizeof("msg") - 1)) {
msg_command(l);
return;
}
if (!strncmp(l->buf + 1, "NICK", sizeof("NICK") - 1) || !strncmp(l->buf + 1, "nick", sizeof("nick") - 1)) {
if (!memcmp(l->buf + 1, "NICK", sizeof("NICK") - 1) || !memcmp(l->buf + 1, "nick", sizeof("nick") - 1)) {
nick_command(l);
return;
}
if (!strncmp(l->buf + 1, "ACTION", sizeof("ACTION") - 1) || !strncmp(l->buf + 1, "action", sizeof("action") - 1)) {
if (!memcmp(l->buf + 1, "ACTION", sizeof("ACTION") - 1) || !memcmp(l->buf + 1, "action", sizeof("action") - 1)) {
action_command(l);
return;
}
if (!strncmp(l->buf + 1, "QUERY", sizeof("QUERY") - 1) || !strncmp(l->buf + 1, "query", sizeof("query") - 1)) {
if (!memcmp(l->buf + 1, "QUERY", sizeof("QUERY") - 1) || !memcmp(l->buf + 1, "query", sizeof("query") - 1)) {
query_command(l);
return;
}
if (!strncmp(l->buf + 1, "DCC", sizeof("DCC") - 1) || !strncmp(l->buf + 1, "dcc", sizeof("dcc") - 1)) {
if (!memcmp(l->buf + 1, "DCC", sizeof("DCC") - 1) || !memcmp(l->buf + 1, "dcc", sizeof("dcc") - 1)) {
dcc_command(l);
return;
}
Expand Down