From e62d8c725b73cc03efc01df59bf5cc5a5fa31b5d Mon Sep 17 00:00:00 2001 From: Lucas De Marchi Date: Thu, 16 Oct 2014 21:39:33 -0300 Subject: strbuf: make strbuf_pushchars() a little less dumb Do not push one char at a time. We have strlen() and memcpy() to rely on. --- shared/strbuf.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) (limited to 'shared') diff --git a/shared/strbuf.c b/shared/strbuf.c index af445d9..e5df35d 100644 --- a/shared/strbuf.c +++ b/shared/strbuf.c @@ -22,6 +22,7 @@ #include #include #include +#include #include "util.h" #include "strbuf.h" @@ -93,15 +94,20 @@ bool strbuf_pushchar(struct strbuf *buf, char ch) unsigned strbuf_pushchars(struct strbuf *buf, const char *str) { - unsigned i = 0; - int ch; + unsigned int len; - while ((ch = str[i])) { - strbuf_pushchar(buf, ch); - i++; - } + assert(str != NULL); + assert(buf != NULL); + + len = strlen(str); + + if (!buf_grow(buf, buf->used + len)) + return 0; + + memcpy(buf->bytes + buf->used, str, len); + buf->used += len; - return i; + return len; } void strbuf_popchar(struct strbuf *buf) -- cgit v1.2.3