]> jfr.im git - solanum.git/blobdiff - ircd/msgbuf.c
librb: remove unnecessary NULL from the end of rb_string_to_array output
[solanum.git] / ircd / msgbuf.c
index 3aae2157d38c935aa89f81e6a818d2c94bff3bc7..1336c096bfb76502f12f7ed796971d7531f86747 100644 (file)
@@ -34,8 +34,6 @@ msgbuf_parse(struct MsgBuf *msgbuf, char *line)
 {
        char *ch;
        char *parv[MAXPARA];
-       size_t n_para;
-       int i;
 
        /* skip any leading spaces */
        for (ch = line; *ch && *ch == ' '; ch++)
@@ -79,6 +77,8 @@ msgbuf_parse(struct MsgBuf *msgbuf, char *line)
 
                        *ch++ = '\0';
                }
+               else
+                       ch = t;
        }
 
        /* skip any whitespace between tags and origin */
@@ -103,48 +103,30 @@ msgbuf_parse(struct MsgBuf *msgbuf, char *line)
        if (*ch == '\0')
                return 1;
 
-       n_para = rb_string_to_array(ch, parv, MAXPARA);
-       if (n_para == 0)
+       msgbuf->n_para = rb_string_to_array(ch, (char **)msgbuf->para, MAXPARA);
+       if (msgbuf->n_para == 0)
                return 1;
 
-       msgbuf->cmd = parv[0];
-       for (i = 0; i < n_para; i++)
-               msgbuf_append_para(msgbuf, parv[i]);
-
-       return 0;
-}
-
-static int
-msgbuf_has_matching_tags(struct MsgBuf *msgbuf, unsigned int capmask)
-{
-       int i;
-
-       for (i = 0; i < msgbuf->n_tags; i++)
-       {
-               if ((msgbuf->tags[i].capmask & capmask) != 0)
-                       return 1;
-       }
-
+       msgbuf->cmd = msgbuf->para[0];
        return 0;
 }
 
 static void
 msgbuf_unparse_tags(char *buf, size_t buflen, struct MsgBuf *msgbuf, unsigned int capmask)
 {
-       int i;
-
-       if (!msgbuf_has_matching_tags(msgbuf, capmask))
-               return;
+       bool has_tags = false;
 
-       *buf = '@';
-
-       for (i = 0; i < msgbuf->n_tags; i++)
+       for (size_t i = 0; i < msgbuf->n_tags; i++)
        {
                if ((msgbuf->tags[i].capmask & capmask) == 0)
                        continue;
 
-               if (i != 0)
+               if (has_tags) {
                        rb_strlcat(buf, ";", buflen);
+               } else {
+                       *buf = '@';
+                       has_tags = true;
+               }
 
                rb_strlcat(buf, msgbuf->tags[i].key, buflen);
 
@@ -156,14 +138,13 @@ msgbuf_unparse_tags(char *buf, size_t buflen, struct MsgBuf *msgbuf, unsigned in
                }
        }
 
-       rb_strlcat(buf, " ", buflen);
+       if (has_tags)
+               rb_strlcat(buf, " ", buflen);
 }
 
 void
 msgbuf_unparse_prefix(char *buf, size_t buflen, struct MsgBuf *msgbuf, unsigned int capmask)
 {
-       int i;
-
        memset(buf, 0, buflen);
 
        if (msgbuf->n_tags > 0)
@@ -187,11 +168,9 @@ msgbuf_unparse_prefix(char *buf, size_t buflen, struct MsgBuf *msgbuf, unsigned
 int
 msgbuf_unparse(char *buf, size_t buflen, struct MsgBuf *msgbuf, unsigned int capmask)
 {
-       int i;
-
        msgbuf_unparse_prefix(buf, buflen, msgbuf, capmask);
 
-       for (i = msgbuf->cmd != NULL ? 0 : 1; i < msgbuf->n_para; i++)
+       for (size_t i = msgbuf->cmd != NULL ? 0 : 1; i < msgbuf->n_para; i++)
        {
                if (i == (msgbuf->n_para - 1))
                {