4 int prefixcmp(const char *str, const char *prefix)
6 for (; ; str++, prefix++)
9 else if (*str != *prefix)
10 return (unsigned char)*prefix - (unsigned char)*str;
13 int suffixcmp(const char *str, const char *suffix)
15 int len = strlen(str), suflen = strlen(suffix);
19 return strcmp(str + len - suflen, suffix);
23 * Used as the default ->buf value, so that people can always assume
24 * buf is non NULL and ->buf is NUL terminated even for a freshly
27 char strbuf_slopbuf[1];
29 void strbuf_init(struct strbuf *sb, size_t hint)
31 sb->alloc = sb->len = 0;
32 sb->buf = strbuf_slopbuf;
34 strbuf_grow(sb, hint);
37 void strbuf_release(struct strbuf *sb)
45 char *strbuf_detach(struct strbuf *sb, size_t *sz)
56 void strbuf_attach(struct strbuf *sb, void *buf, size_t len, size_t alloc)
63 sb->buf[sb->len] = '\0';
66 void strbuf_grow(struct strbuf *sb, size_t extra)
68 int new_buf = !sb->alloc;
69 if (unsigned_add_overflows(extra, 1) ||
70 unsigned_add_overflows(sb->len, extra + 1))
71 die("you want to use way too much memory");
74 ALLOC_GROW(sb->buf, sb->len + extra + 1, sb->alloc);
79 void strbuf_trim(struct strbuf *sb)
82 while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1]))
84 while (sb->len > 0 && isspace(*b)) {
88 memmove(sb->buf, b, sb->len);
89 sb->buf[sb->len] = '\0';
91 void strbuf_rtrim(struct strbuf *sb)
93 while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1]))
95 sb->buf[sb->len] = '\0';
98 void strbuf_ltrim(struct strbuf *sb)
101 while (sb->len > 0 && isspace(*b)) {
105 memmove(sb->buf, b, sb->len);
106 sb->buf[sb->len] = '\0';
109 struct strbuf **strbuf_split_buf(const char *str, size_t slen,
110 int terminator, int max)
112 struct strbuf **ret = NULL;
113 size_t nr = 0, alloc = 0;
118 if (max <= 0 || nr + 1 < max) {
119 const char *end = memchr(str, terminator, slen);
123 t = xmalloc(sizeof(struct strbuf));
125 strbuf_add(t, str, len);
126 ALLOC_GROW(ret, nr + 2, alloc);
131 ALLOC_GROW(ret, nr + 1, alloc); /* In case string was empty */
136 void strbuf_list_free(struct strbuf **sbs)
138 struct strbuf **s = sbs;
147 int strbuf_cmp(const struct strbuf *a, const struct strbuf *b)
149 int len = a->len < b->len ? a->len: b->len;
150 int cmp = memcmp(a->buf, b->buf, len);
153 return a->len < b->len ? -1: a->len != b->len;
156 void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
157 const void *data, size_t dlen)
159 if (unsigned_add_overflows(pos, len))
160 die("you want to use way too much memory");
162 die("`pos' is too far after the end of the buffer");
163 if (pos + len > sb->len)
164 die("`pos + len' is too far after the end of the buffer");
167 strbuf_grow(sb, dlen - len);
168 memmove(sb->buf + pos + dlen,
170 sb->len - pos - len);
171 memcpy(sb->buf + pos, data, dlen);
172 strbuf_setlen(sb, sb->len + dlen - len);
175 void strbuf_insert(struct strbuf *sb, size_t pos, const void *data, size_t len)
177 strbuf_splice(sb, pos, 0, data, len);
180 void strbuf_remove(struct strbuf *sb, size_t pos, size_t len)
182 strbuf_splice(sb, pos, len, NULL, 0);
185 void strbuf_add(struct strbuf *sb, const void *data, size_t len)
187 strbuf_grow(sb, len);
188 memcpy(sb->buf + sb->len, data, len);
189 strbuf_setlen(sb, sb->len + len);
192 void strbuf_adddup(struct strbuf *sb, size_t pos, size_t len)
194 strbuf_grow(sb, len);
195 memcpy(sb->buf + sb->len, sb->buf + pos, len);
196 strbuf_setlen(sb, sb->len + len);
199 void strbuf_addchars(struct strbuf *sb, int c, size_t n)
202 memset(sb->buf + sb->len, c, n);
203 strbuf_setlen(sb, sb->len + n);
206 void strbuf_addf(struct strbuf *sb, const char *fmt, ...)
210 strbuf_vaddf(sb, fmt, ap);
214 static void add_lines(struct strbuf *out,
217 const char *buf, size_t size)
221 const char *next = memchr(buf, '\n', size);
222 next = next ? (next + 1) : (buf + size);
224 prefix = (prefix2 && buf[0] == '\n') ? prefix2 : prefix1;
225 strbuf_addstr(out, prefix);
226 strbuf_add(out, buf, next - buf);
230 strbuf_complete_line(out);
233 void strbuf_add_commented_lines(struct strbuf *out, const char *buf, size_t size)
235 static char prefix1[3];
236 static char prefix2[2];
238 if (prefix1[0] != comment_line_char) {
239 sprintf(prefix1, "%c ", comment_line_char);
240 sprintf(prefix2, "%c", comment_line_char);
242 add_lines(out, prefix1, prefix2, buf, size);
245 void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...)
248 struct strbuf buf = STRBUF_INIT;
249 int incomplete_line = sb->len && sb->buf[sb->len - 1] != '\n';
251 va_start(params, fmt);
252 strbuf_vaddf(&buf, fmt, params);
255 strbuf_add_commented_lines(sb, buf.buf, buf.len);
257 sb->buf[--sb->len] = '\0';
259 strbuf_release(&buf);
262 void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap)
267 if (!strbuf_avail(sb))
270 len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, cp);
273 die("BUG: your vsnprintf is broken (returned %d)", len);
274 if (len > strbuf_avail(sb)) {
275 strbuf_grow(sb, len);
276 len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
277 if (len > strbuf_avail(sb))
278 die("BUG: your vsnprintf is broken (insatiable)");
280 strbuf_setlen(sb, sb->len + len);
283 void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn,
290 percent = strchrnul(format, '%');
291 strbuf_add(sb, format, percent - format);
294 format = percent + 1;
296 if (*format == '%') {
297 strbuf_addch(sb, '%');
302 consumed = fn(sb, format, context);
306 strbuf_addch(sb, '%');
310 size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder,
313 struct strbuf_expand_dict_entry *e = context;
316 for (; e->placeholder && (len = strlen(e->placeholder)); e++) {
317 if (!strncmp(placeholder, e->placeholder, len)) {
319 strbuf_addstr(sb, e->value);
326 void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src)
328 int i, len = src->len;
330 for (i = 0; i < len; i++) {
331 if (src->buf[i] == '%')
332 strbuf_addch(dst, '%');
333 strbuf_addch(dst, src->buf[i]);
337 size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f)
340 size_t oldalloc = sb->alloc;
342 strbuf_grow(sb, size);
343 res = fread(sb->buf + sb->len, 1, size, f);
345 strbuf_setlen(sb, sb->len + res);
346 else if (oldalloc == 0)
351 ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
353 size_t oldlen = sb->len;
354 size_t oldalloc = sb->alloc;
356 strbuf_grow(sb, hint ? hint : 8192);
360 cnt = xread(fd, sb->buf + sb->len, sb->alloc - sb->len - 1);
365 strbuf_setlen(sb, oldlen);
371 strbuf_grow(sb, 8192);
374 sb->buf[sb->len] = '\0';
375 return sb->len - oldlen;
378 #define STRBUF_MAXLINK (2*PATH_MAX)
380 int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint)
382 size_t oldalloc = sb->alloc;
387 while (hint < STRBUF_MAXLINK) {
390 strbuf_grow(sb, hint);
391 len = readlink(path, sb->buf, hint);
395 } else if (len < hint) {
396 strbuf_setlen(sb, len);
400 /* .. the buffer was too small - try again */
408 int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
416 while ((ch = fgetc(fp)) != EOF) {
418 sb->buf[sb->len++] = ch;
422 if (ch == EOF && sb->len == 0)
425 sb->buf[sb->len] = '\0';
429 int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
431 if (strbuf_getwholeline(sb, fp, term))
433 if (sb->buf[sb->len-1] == term)
434 strbuf_setlen(sb, sb->len-1);
438 int strbuf_getwholeline_fd(struct strbuf *sb, int fd, int term)
444 ssize_t len = xread(fd, &ch, 1);
447 strbuf_addch(sb, ch);
454 int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)
458 fd = open(path, O_RDONLY);
461 len = strbuf_read(sb, fd, hint);
469 void strbuf_add_lines(struct strbuf *out, const char *prefix,
470 const char *buf, size_t size)
472 add_lines(out, prefix, NULL, buf, size);
475 void strbuf_addstr_xml_quoted(struct strbuf *buf, const char *s)
478 size_t len = strcspn(s, "\"<>&");
479 strbuf_add(buf, s, len);
483 strbuf_addstr(buf, """);
486 strbuf_addstr(buf, "<");
489 strbuf_addstr(buf, ">");
492 strbuf_addstr(buf, "&");
501 static int is_rfc3986_reserved(char ch)
504 case '!': case '*': case '\'': case '(': case ')': case ';':
505 case ':': case '@': case '&': case '=': case '+': case '$':
506 case ',': case '/': case '?': case '#': case '[': case ']':
512 static int is_rfc3986_unreserved(char ch)
514 return isalnum(ch) ||
515 ch == '-' || ch == '_' || ch == '.' || ch == '~';
518 static void strbuf_add_urlencode(struct strbuf *sb, const char *s, size_t len,
521 strbuf_grow(sb, len);
524 if (is_rfc3986_unreserved(ch) ||
525 (!reserved && is_rfc3986_reserved(ch)))
526 strbuf_addch(sb, ch);
528 strbuf_addf(sb, "%%%02x", ch);
532 void strbuf_addstr_urlencode(struct strbuf *sb, const char *s,
535 strbuf_add_urlencode(sb, s, strlen(s), reserved);
538 void strbuf_humanise_bytes(struct strbuf *buf, off_t bytes)
540 if (bytes > 1 << 30) {
541 strbuf_addf(buf, "%u.%2.2u GiB",
543 (int)(bytes & ((1 << 30) - 1)) / 10737419);
544 } else if (bytes > 1 << 20) {
545 int x = bytes + 5243; /* for rounding */
546 strbuf_addf(buf, "%u.%2.2u MiB",
547 x >> 20, ((x & ((1 << 20) - 1)) * 100) >> 20);
548 } else if (bytes > 1 << 10) {
549 int x = bytes + 5; /* for rounding */
550 strbuf_addf(buf, "%u.%2.2u KiB",
551 x >> 10, ((x & ((1 << 10) - 1)) * 100) >> 10);
553 strbuf_addf(buf, "%u bytes", (int)bytes);
557 int printf_ln(const char *fmt, ...)
562 ret = vprintf(fmt, ap);
564 if (ret < 0 || putchar('\n') == EOF)
569 int fprintf_ln(FILE *fp, const char *fmt, ...)
574 ret = vfprintf(fp, fmt, ap);
576 if (ret < 0 || putc('\n', fp) == EOF)