]> PatchyGit - patchyircd.git/commitdiff
irc: use string_view for command matching
authorEgor <egor@patchysicp.xyz>
Sun, 15 Jun 2025 08:26:44 +0000 (11:26 +0300)
committerEgor <egor@patchysicp.xyz>
Sun, 15 Jun 2025 08:26:44 +0000 (11:26 +0300)
src/common.hpp
src/irc.cpp
src/irc.hpp
src/ircfmt.cpp
src/ircfmt.hpp
src/util.cpp

index 4e4d541..90d73c4 100644 (file)
 #include <algorithm>
 #include <deque>
 #include <iterator>
+#include <string_view>
 #include <utility>
 #include <vector>
+using namespace std::string_view_literals;
 
 /* libraries */
 #ifdef HAVE_LIBTLS
index 24dfaad..d723f71 100644 (file)
@@ -170,14 +170,15 @@ int irc_parse_line(Client *client, char *p, char *end) {
                args[argc] = NULL;
        }
        log_client(client, "parsed: %s", command);
-       if (strcmp(command, "PASS") != 0 && strcmp(command, "OPER") != 0 && strcmp(command, "NEWACC") != 0
-                       && strcmp(command, "AUTHENTICATE") != 0 && strcmp(command, "WEBIRC") != 0) {
+       std::string_view commandsv = command;
+       if (commandsv != "PASS"sv != 0 && commandsv != "OPER"sv && commandsv != "NEWACC"sv &&
+                       commandsv != "AUTHENTICATE"sv && commandsv != "WEBIRC"sv) {
                for (int i = 0; i < argc; i++){
                        fprintf(stderr, " %d:%s", i, args[i]);
                }
        }
        fprintf(stderr, "\n");
-       return irc_process_line(client, source, command, argc, args);
+       return irc_process_line(client, source, commandsv, argc, args);
 }
 static void cont_webirc(Client *client, int res) {
        if (!(client->umode & UMODE_SECURE) || res != 0) {
@@ -235,11 +236,11 @@ static void cont_pass(Client *client, int res) {
                newUser(client);
        }
 }
-#define MATCH_COMMAND(s) else if(strcmp(command, s) == 0)
-int irc_process_line(Client *client, char *source, char *command, int argc, char *args[]) {
+#define MATCH_COMMAND(s) else if (command == s##sv)
+int irc_process_line(Client *client, char *source, std::string_view command, int argc, char *args[]) {
        (void)source;
        databuf.clear();
-       if (command[0] == '\0') {
+       if (command.empty()) {
                return 0;
        } MATCH_COMMAND("PING") {
                if (!args[0])
@@ -1624,7 +1625,7 @@ void channel_mode(Client *client, Channel *chan, char *args[]) {
                case '-': is_plus = 0; break;
                default:
                        if (client)
-                               err_unknowncommand(client, "MODE"), sendbuf(client);
+                               err_unknowncommand(client, "MODE"sv), sendbuf(client);
                        return;
        }
        stringset *list;
index a2a9230..f4cf610 100644 (file)
@@ -10,7 +10,7 @@
 void sendbuf_monitor(const char *nick, int cookie, int positive, int negative);
 const char *format_umodes(Client *cl);
 int irc_parse_line(Client *client, char *p, char *end);
-int irc_process_line(Client *client, char *source, char *command, int argc, char *args[]);
+int irc_process_line(Client *client, char *source, std::string_view command, int argc, char *args[]);
 void newUser(Client *client);
 void sendLUSERS(Client *client);
 void sendMOTD(Client *client);
index 7e945e7..cc3c280 100644 (file)
@@ -579,9 +579,9 @@ void err_inputtoolong(Client *client) // 417
        copy_strf(&databuf, ":%s 417 %s :Input line was too long\r\n", hostname, client->nick);
 }
 #endif
-void err_unknowncommand(Client *client, const char *command) // 421
+void err_unknowncommand(Client *client, std::string_view command) // 421
 {
-       copy_strf(&databuf, ":%s 421 %s %s :Unknown command\r\n", hostname, client->nick ? client->nick : "*", command);
+       copy_strf(&databuf, ":%s 421 %s %.*s :Unknown command\r\n", hostname, client->nick ? client->nick : "*", (int)command.size(), command.data());
 }
 void err_nomotd(Client *client) // 422
 {
@@ -612,9 +612,9 @@ void err_notregistered(Client *client) // 451
        (void)client;
        copy_strf(&databuf, ":%s 451 * :You have not registered\r\n", hostname);
 }
-void err_needmoreparams(Client *client, const char *command) // 461
+void err_needmoreparams(Client *client, std::string_view command) // 461
 {
-       copy_strf(&databuf, ":%s 461 %s %s :Not enough parameters\r\n", hostname, client->nick ? client->nick : "*", command);
+       copy_strf(&databuf, ":%s 461 %s %.*s :Not enough parameters\r\n", hostname, client->nick ? client->nick : "*", (int)command.size(), command.data());
 }
 void err_alreadyregistered(Client *client) // 462
 {
index 1a9068a..c1bb2a0 100644 (file)
@@ -107,7 +107,7 @@ void err_toomanychannels(Client *client, const char *channel); // 405
 void err_wasnosuchnick(Client *client); // 406
 void err_noorigin(Client *client); // 409
 void err_inputtoolong(Client *client); // 417
-void err_unknowncommand(Client *client, const char *command); // 421
+void err_unknowncommand(Client *client, std::string_view command); // 421
 void err_nomotd(Client *client); // 422
 void err_erroneusnickname(Client *client, const char *nick); // 432
 void err_nicknameinuse(Client *client, const char *nick); // 433
@@ -115,7 +115,7 @@ void err_usernotinchannel(Client *client, const char *nick, Channel *chan); // 4
 void err_notonchannel(Client *client, Channel *chan); // 442
 void err_useronchannel(Client *client, const char *nick, Channel *chan); // 443
 void err_notregistered(Client *client); // 451
-void err_needmoreparams(Client *client, const char *command); // 461
+void err_needmoreparams(Client *client, std::string_view command); // 461
 void err_alreadyregistered(Client *client); // 462
 void err_passwdmismatch(Client *client); // 464
 void err_yourebannedcreep(Client *client); // 465
index 4266fbd..abc2701 100644 (file)
@@ -27,7 +27,13 @@ void append_strf(std::vector<char> *v, const char *s, ...) {
                        }
                        break;
                }
-               if (end[1] == 's') { // %s
+               if (end[1] == '.' && end[2] == '*' && end[3] == 's') { // %.*s
+                       append_strn(v, s, end-s);
+                       int n = va_arg(ap, int);
+                       const char *c = va_arg(ap, const char *);
+                       append_strn(v, c, n);
+                       s = end + 4;
+               } else if (end[1] == 's') { // %s
                        append_strn(v, s, end-s);
                        const char *c = va_arg(ap, const char *);
                        append_str(v, c ? c : "(null)");