msgbuf: don't append a ';' unless there are existing tags

When both account-tag and server-time are present but the client
doesn't have the first (i == 0) of these enabled. They will get
an erroneous ';' after the '@'.

Track whether or not there are tags present, and use this to
determine whether to add the ';' or not. Also remove the extra
function that loops over all of the tags by using this flag to
handle the case where there are no tags being written.
This commit is contained in:
Simon Arlott 2016-11-25 16:02:11 +00:00
parent 6396c5da07
commit 57dd2c6a89
No known key found for this signature in database
GPG key ID: C8975F2043CA5D24

View file

@ -115,33 +115,22 @@ msgbuf_parse(struct MsgBuf *msgbuf, char *line)
return 0;
}
static int
msgbuf_has_matching_tags(struct MsgBuf *msgbuf, unsigned int capmask)
{
for (size_t i = 0; i < msgbuf->n_tags; i++)
{
if ((msgbuf->tags[i].capmask & capmask) != 0)
return 1;
}
return 0;
}
static void
msgbuf_unparse_tags(char *buf, size_t buflen, struct MsgBuf *msgbuf, unsigned int capmask)
{
if (!msgbuf_has_matching_tags(msgbuf, capmask))
return;
*buf = '@';
bool has_tags = false;
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);
@ -153,6 +142,7 @@ msgbuf_unparse_tags(char *buf, size_t buflen, struct MsgBuf *msgbuf, unsigned in
}
}
if (has_tags)
rb_strlcat(buf, " ", buflen);
}