4 int starts_with(const char *str, const char *prefix)
6 for (; ; str++, prefix++)
9 else if (*str != *prefix)
13 int ends_with(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)
84 void strbuf_rtrim(struct strbuf *sb)
86 while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1]))
88 sb->buf[sb->len] = '\0';
91 void strbuf_ltrim(struct strbuf *sb)
94 while (sb->len > 0 && isspace(*b)) {
98 memmove(sb->buf, b, sb->len);
99 sb->buf[sb->len] = '\0';
102 struct strbuf **strbuf_split_buf(const char *str, size_t slen,
103 int terminator, int max)
105 struct strbuf **ret = NULL;
106 size_t nr = 0, alloc = 0;
111 if (max <= 0 || nr + 1 < max) {
112 const char *end = memchr(str, terminator, slen);
116 t = xmalloc(sizeof(struct strbuf));
118 strbuf_add(t, str, len);
119 ALLOC_GROW(ret, nr + 2, alloc);
124 ALLOC_GROW(ret, nr + 1, alloc); /* In case string was empty */
129 void strbuf_list_free(struct strbuf **sbs)
131 struct strbuf **s = sbs;
140 int strbuf_cmp(const struct strbuf *a, const struct strbuf *b)
142 int len = a->len < b->len ? a->len: b->len;
143 int cmp = memcmp(a->buf, b->buf, len);
146 return a->len < b->len ? -1: a->len != b->len;
149 void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
150 const void *data, size_t dlen)
152 if (unsigned_add_overflows(pos, len))
153 die("you want to use way too much memory");
155 die("`pos' is too far after the end of the buffer");
156 if (pos + len > sb->len)
157 die("`pos + len' is too far after the end of the buffer");
160 strbuf_grow(sb, dlen - len);
161 memmove(sb->buf + pos + dlen,
163 sb->len - pos - len);
164 memcpy(sb->buf + pos, data, dlen);
165 strbuf_setlen(sb, sb->len + dlen - len);
168 void strbuf_insert(struct strbuf *sb, size_t pos, const void *data, size_t len)
170 strbuf_splice(sb, pos, 0, data, len);
173 void strbuf_remove(struct strbuf *sb, size_t pos, size_t len)
175 strbuf_splice(sb, pos, len, NULL, 0);
178 void strbuf_add(struct strbuf *sb, const void *data, size_t len)
180 strbuf_grow(sb, len);
181 memcpy(sb->buf + sb->len, data, len);
182 strbuf_setlen(sb, sb->len + len);
185 void strbuf_adddup(struct strbuf *sb, size_t pos, size_t len)
187 strbuf_grow(sb, len);
188 memcpy(sb->buf + sb->len, sb->buf + pos, len);
189 strbuf_setlen(sb, sb->len + len);
192 void strbuf_addf(struct strbuf *sb, const char *fmt, ...)
196 strbuf_vaddf(sb, fmt, ap);
200 static void add_lines(struct strbuf *out,
203 const char *buf, size_t size)
207 const char *next = memchr(buf, '\n', size);
208 next = next ? (next + 1) : (buf + size);
210 prefix = (prefix2 && buf[0] == '\n') ? prefix2 : prefix1;
211 strbuf_addstr(out, prefix);
212 strbuf_add(out, buf, next - buf);
216 strbuf_complete_line(out);
219 void strbuf_add_commented_lines(struct strbuf *out, const char *buf, size_t size)
221 static char prefix1[3];
222 static char prefix2[2];
224 if (prefix1[0] != comment_line_char) {
225 sprintf(prefix1, "%c ", comment_line_char);
226 sprintf(prefix2, "%c", comment_line_char);
228 add_lines(out, prefix1, prefix2, buf, size);
231 void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...)
234 struct strbuf buf = STRBUF_INIT;
235 int incomplete_line = sb->len && sb->buf[sb->len - 1] != '\n';
237 va_start(params, fmt);
238 strbuf_vaddf(&buf, fmt, params);
241 strbuf_add_commented_lines(sb, buf.buf, buf.len);
243 sb->buf[--sb->len] = '\0';
245 strbuf_release(&buf);
248 void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap)
253 if (!strbuf_avail(sb))
256 len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, cp);
259 die("BUG: your vsnprintf is broken (returned %d)", len);
260 if (len > strbuf_avail(sb)) {
261 strbuf_grow(sb, len);
262 len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
263 if (len > strbuf_avail(sb))
264 die("BUG: your vsnprintf is broken (insatiable)");
266 strbuf_setlen(sb, sb->len + len);
269 void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn,
276 percent = strchrnul(format, '%');
277 strbuf_add(sb, format, percent - format);
280 format = percent + 1;
282 if (*format == '%') {
283 strbuf_addch(sb, '%');
288 consumed = fn(sb, format, context);
292 strbuf_addch(sb, '%');
296 size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder,
299 struct strbuf_expand_dict_entry *e = context;
302 for (; e->placeholder && (len = strlen(e->placeholder)); e++) {
303 if (!strncmp(placeholder, e->placeholder, len)) {
305 strbuf_addstr(sb, e->value);
312 void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src)
314 int i, len = src->len;
316 for (i = 0; i < len; i++) {
317 if (src->buf[i] == '%')
318 strbuf_addch(dst, '%');
319 strbuf_addch(dst, src->buf[i]);
323 size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f)
326 size_t oldalloc = sb->alloc;
328 strbuf_grow(sb, size);
329 res = fread(sb->buf + sb->len, 1, size, f);
331 strbuf_setlen(sb, sb->len + res);
332 else if (oldalloc == 0)
337 ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
339 size_t oldlen = sb->len;
340 size_t oldalloc = sb->alloc;
342 strbuf_grow(sb, hint ? hint : 8192);
346 cnt = xread(fd, sb->buf + sb->len, sb->alloc - sb->len - 1);
351 strbuf_setlen(sb, oldlen);
357 strbuf_grow(sb, 8192);
360 sb->buf[sb->len] = '\0';
361 return sb->len - oldlen;
364 #define STRBUF_MAXLINK (2*PATH_MAX)
366 int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint)
368 size_t oldalloc = sb->alloc;
373 while (hint < STRBUF_MAXLINK) {
376 strbuf_grow(sb, hint);
377 len = readlink(path, sb->buf, hint);
381 } else if (len < hint) {
382 strbuf_setlen(sb, len);
386 /* .. the buffer was too small - try again */
394 int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
402 while ((ch = fgetc(fp)) != EOF) {
404 sb->buf[sb->len++] = ch;
408 if (ch == EOF && sb->len == 0)
411 sb->buf[sb->len] = '\0';
415 int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
417 if (strbuf_getwholeline(sb, fp, term))
419 if (sb->buf[sb->len-1] == term)
420 strbuf_setlen(sb, sb->len-1);
424 int strbuf_getwholeline_fd(struct strbuf *sb, int fd, int term)
430 ssize_t len = xread(fd, &ch, 1);
433 strbuf_addch(sb, ch);
440 int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)
444 fd = open(path, O_RDONLY);
447 len = strbuf_read(sb, fd, hint);
455 void strbuf_add_lines(struct strbuf *out, const char *prefix,
456 const char *buf, size_t size)
458 add_lines(out, prefix, NULL, buf, size);
461 void strbuf_addstr_xml_quoted(struct strbuf *buf, const char *s)
464 size_t len = strcspn(s, "\"<>&");
465 strbuf_add(buf, s, len);
469 strbuf_addstr(buf, """);
472 strbuf_addstr(buf, "<");
475 strbuf_addstr(buf, ">");
478 strbuf_addstr(buf, "&");
487 static int is_rfc3986_reserved(char ch)
490 case '!': case '*': case '\'': case '(': case ')': case ';':
491 case ':': case '@': case '&': case '=': case '+': case '$':
492 case ',': case '/': case '?': case '#': case '[': case ']':
498 static int is_rfc3986_unreserved(char ch)
500 return isalnum(ch) ||
501 ch == '-' || ch == '_' || ch == '.' || ch == '~';
504 static void strbuf_add_urlencode(struct strbuf *sb, const char *s, size_t len,
507 strbuf_grow(sb, len);
510 if (is_rfc3986_unreserved(ch) ||
511 (!reserved && is_rfc3986_reserved(ch)))
512 strbuf_addch(sb, ch);
514 strbuf_addf(sb, "%%%02x", ch);
518 void strbuf_addstr_urlencode(struct strbuf *sb, const char *s,
521 strbuf_add_urlencode(sb, s, strlen(s), reserved);
524 void strbuf_humanise_bytes(struct strbuf *buf, off_t bytes)
526 if (bytes > 1 << 30) {
527 strbuf_addf(buf, "%u.%2.2u GiB",
529 (int)(bytes & ((1 << 30) - 1)) / 10737419);
530 } else if (bytes > 1 << 20) {
531 int x = bytes + 5243; /* for rounding */
532 strbuf_addf(buf, "%u.%2.2u MiB",
533 x >> 20, ((x & ((1 << 20) - 1)) * 100) >> 20);
534 } else if (bytes > 1 << 10) {
535 int x = bytes + 5; /* for rounding */
536 strbuf_addf(buf, "%u.%2.2u KiB",
537 x >> 10, ((x & ((1 << 10) - 1)) * 100) >> 10);
539 strbuf_addf(buf, "%u bytes", (int)bytes);
543 int printf_ln(const char *fmt, ...)
548 ret = vprintf(fmt, ap);
550 if (ret < 0 || putchar('\n') == EOF)
555 int fprintf_ln(FILE *fp, const char *fmt, ...)
560 ret = vfprintf(fp, fmt, ap);
562 if (ret < 0 || putc('\n', fp) == EOF)