Do not push one char at a time. We have strlen() and memcpy() to rely
on.
#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
+#include <string.h>
#include "util.h"
#include "strbuf.h"
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)