strbuf: make strbuf_pushchars() a little less dumb
authorLucas De Marchi <lucas.demarchi@intel.com>
Fri, 17 Oct 2014 00:39:33 +0000 (21:39 -0300)
committerLucas De Marchi <lucas.demarchi@intel.com>
Fri, 17 Oct 2014 00:39:33 +0000 (21:39 -0300)
Do not push one char at a time. We have strlen() and memcpy() to rely
on.

shared/strbuf.c

index af445d9..e5df35d 100644 (file)
@@ -22,6 +22,7 @@
 #include <assert.h>
 #include <stdbool.h>
 #include <stdlib.h>
+#include <string.h>
 
 #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)