5 int starts_with(const char *str, const char *prefix)
7 for (; ; str++, prefix++)
10 else if (*str != *prefix)
14 int ends_with(const char *str, const char *suffix)
16 int len = strlen(str), suflen = strlen(suffix);
20 return !strcmp(str + len - suflen, suffix);
24 * Used as the default ->buf value, so that people can always assume
25 * buf is non NULL and ->buf is NUL terminated even for a freshly
28 char strbuf_slopbuf[1];
30 void strbuf_init(struct strbuf *sb, size_t hint)
32 sb->alloc = sb->len = 0;
33 sb->buf = strbuf_slopbuf;
35 strbuf_grow(sb, hint);
38 void strbuf_release(struct strbuf *sb)
46 char *strbuf_detach(struct strbuf *sb, size_t *sz)
57 void strbuf_attach(struct strbuf *sb, void *buf, size_t len, size_t alloc)
64 sb->buf[sb->len] = '\0';
67 void strbuf_grow(struct strbuf *sb, size_t extra)
69 int new_buf = !sb->alloc;
70 if (unsigned_add_overflows(extra, 1) ||
71 unsigned_add_overflows(sb->len, extra + 1))
72 die("you want to use way too much memory");
75 ALLOC_GROW(sb->buf, sb->len + extra + 1, sb->alloc);
80 void strbuf_trim(struct strbuf *sb)
85 void strbuf_rtrim(struct strbuf *sb)
87 while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1]))
89 sb->buf[sb->len] = '\0';
92 void strbuf_ltrim(struct strbuf *sb)
95 while (sb->len > 0 && isspace(*b)) {
99 memmove(sb->buf, b, sb->len);
100 sb->buf[sb->len] = '\0';
103 int strbuf_reencode(struct strbuf *sb, const char *from, const char *to)
108 if (same_encoding(from, to))
111 out = reencode_string_len(sb->buf, sb->len, to, from, &len);
115 strbuf_attach(sb, out, len, len);
119 void strbuf_tolower(struct strbuf *sb)
121 char *p = sb->buf, *end = sb->buf + sb->len;
126 struct strbuf **strbuf_split_buf(const char *str, size_t slen,
127 int terminator, int max)
129 struct strbuf **ret = NULL;
130 size_t nr = 0, alloc = 0;
135 if (max <= 0 || nr + 1 < max) {
136 const char *end = memchr(str, terminator, slen);
140 t = xmalloc(sizeof(struct strbuf));
142 strbuf_add(t, str, len);
143 ALLOC_GROW(ret, nr + 2, alloc);
148 ALLOC_GROW(ret, nr + 1, alloc); /* In case string was empty */
153 void strbuf_list_free(struct strbuf **sbs)
155 struct strbuf **s = sbs;
164 int strbuf_cmp(const struct strbuf *a, const struct strbuf *b)
166 int len = a->len < b->len ? a->len: b->len;
167 int cmp = memcmp(a->buf, b->buf, len);
170 return a->len < b->len ? -1: a->len != b->len;
173 void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
174 const void *data, size_t dlen)
176 if (unsigned_add_overflows(pos, len))
177 die("you want to use way too much memory");
179 die("`pos' is too far after the end of the buffer");
180 if (pos + len > sb->len)
181 die("`pos + len' is too far after the end of the buffer");
184 strbuf_grow(sb, dlen - len);
185 memmove(sb->buf + pos + dlen,
187 sb->len - pos - len);
188 memcpy(sb->buf + pos, data, dlen);
189 strbuf_setlen(sb, sb->len + dlen - len);
192 void strbuf_insert(struct strbuf *sb, size_t pos, const void *data, size_t len)
194 strbuf_splice(sb, pos, 0, data, len);
197 void strbuf_remove(struct strbuf *sb, size_t pos, size_t len)
199 strbuf_splice(sb, pos, len, NULL, 0);
202 void strbuf_add(struct strbuf *sb, const void *data, size_t len)
204 strbuf_grow(sb, len);
205 memcpy(sb->buf + sb->len, data, len);
206 strbuf_setlen(sb, sb->len + len);
209 void strbuf_adddup(struct strbuf *sb, size_t pos, size_t len)
211 strbuf_grow(sb, len);
212 memcpy(sb->buf + sb->len, sb->buf + pos, len);
213 strbuf_setlen(sb, sb->len + len);
216 void strbuf_addf(struct strbuf *sb, const char *fmt, ...)
220 strbuf_vaddf(sb, fmt, ap);
224 static void add_lines(struct strbuf *out,
227 const char *buf, size_t size)
231 const char *next = memchr(buf, '\n', size);
232 next = next ? (next + 1) : (buf + size);
234 prefix = (prefix2 && buf[0] == '\n') ? prefix2 : prefix1;
235 strbuf_addstr(out, prefix);
236 strbuf_add(out, buf, next - buf);
240 strbuf_complete_line(out);
243 void strbuf_add_commented_lines(struct strbuf *out, const char *buf, size_t size)
245 static char prefix1[3];
246 static char prefix2[2];
248 if (prefix1[0] != comment_line_char) {
249 sprintf(prefix1, "%c ", comment_line_char);
250 sprintf(prefix2, "%c", comment_line_char);
252 add_lines(out, prefix1, prefix2, buf, size);
255 void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...)
258 struct strbuf buf = STRBUF_INIT;
259 int incomplete_line = sb->len && sb->buf[sb->len - 1] != '\n';
261 va_start(params, fmt);
262 strbuf_vaddf(&buf, fmt, params);
265 strbuf_add_commented_lines(sb, buf.buf, buf.len);
267 sb->buf[--sb->len] = '\0';
269 strbuf_release(&buf);
272 void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap)
277 if (!strbuf_avail(sb))
280 len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, cp);
283 die("BUG: your vsnprintf is broken (returned %d)", len);
284 if (len > strbuf_avail(sb)) {
285 strbuf_grow(sb, len);
286 len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
287 if (len > strbuf_avail(sb))
288 die("BUG: your vsnprintf is broken (insatiable)");
290 strbuf_setlen(sb, sb->len + len);
293 void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn,
300 percent = strchrnul(format, '%');
301 strbuf_add(sb, format, percent - format);
304 format = percent + 1;
306 if (*format == '%') {
307 strbuf_addch(sb, '%');
312 consumed = fn(sb, format, context);
316 strbuf_addch(sb, '%');
320 size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder,
323 struct strbuf_expand_dict_entry *e = context;
326 for (; e->placeholder && (len = strlen(e->placeholder)); e++) {
327 if (!strncmp(placeholder, e->placeholder, len)) {
329 strbuf_addstr(sb, e->value);
336 void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src)
338 int i, len = src->len;
340 for (i = 0; i < len; i++) {
341 if (src->buf[i] == '%')
342 strbuf_addch(dst, '%');
343 strbuf_addch(dst, src->buf[i]);
347 size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f)
350 size_t oldalloc = sb->alloc;
352 strbuf_grow(sb, size);
353 res = fread(sb->buf + sb->len, 1, size, f);
355 strbuf_setlen(sb, sb->len + res);
356 else if (oldalloc == 0)
361 ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
363 size_t oldlen = sb->len;
364 size_t oldalloc = sb->alloc;
366 strbuf_grow(sb, hint ? hint : 8192);
370 cnt = xread(fd, sb->buf + sb->len, sb->alloc - sb->len - 1);
375 strbuf_setlen(sb, oldlen);
381 strbuf_grow(sb, 8192);
384 sb->buf[sb->len] = '\0';
385 return sb->len - oldlen;
388 #define STRBUF_MAXLINK (2*PATH_MAX)
390 int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint)
392 size_t oldalloc = sb->alloc;
397 while (hint < STRBUF_MAXLINK) {
400 strbuf_grow(sb, hint);
401 len = readlink(path, sb->buf, hint);
405 } else if (len < hint) {
406 strbuf_setlen(sb, len);
410 /* .. the buffer was too small - try again */
418 int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
426 while ((ch = fgetc(fp)) != EOF) {
428 sb->buf[sb->len++] = ch;
432 if (ch == EOF && sb->len == 0)
435 sb->buf[sb->len] = '\0';
439 int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
441 if (strbuf_getwholeline(sb, fp, term))
443 if (sb->buf[sb->len-1] == term)
444 strbuf_setlen(sb, sb->len-1);
448 int strbuf_getwholeline_fd(struct strbuf *sb, int fd, int term)
454 ssize_t len = xread(fd, &ch, 1);
457 strbuf_addch(sb, ch);
464 int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)
468 fd = open(path, O_RDONLY);
471 len = strbuf_read(sb, fd, hint);
479 void strbuf_add_lines(struct strbuf *out, const char *prefix,
480 const char *buf, size_t size)
482 add_lines(out, prefix, NULL, buf, size);
485 void strbuf_addstr_xml_quoted(struct strbuf *buf, const char *s)
488 size_t len = strcspn(s, "\"<>&");
489 strbuf_add(buf, s, len);
493 strbuf_addstr(buf, """);
496 strbuf_addstr(buf, "<");
499 strbuf_addstr(buf, ">");
502 strbuf_addstr(buf, "&");
511 static int is_rfc3986_reserved(char ch)
514 case '!': case '*': case '\'': case '(': case ')': case ';':
515 case ':': case '@': case '&': case '=': case '+': case '$':
516 case ',': case '/': case '?': case '#': case '[': case ']':
522 static int is_rfc3986_unreserved(char ch)
524 return isalnum(ch) ||
525 ch == '-' || ch == '_' || ch == '.' || ch == '~';
528 static void strbuf_add_urlencode(struct strbuf *sb, const char *s, size_t len,
531 strbuf_grow(sb, len);
534 if (is_rfc3986_unreserved(ch) ||
535 (!reserved && is_rfc3986_reserved(ch)))
536 strbuf_addch(sb, ch);
538 strbuf_addf(sb, "%%%02x", ch);
542 void strbuf_addstr_urlencode(struct strbuf *sb, const char *s,
545 strbuf_add_urlencode(sb, s, strlen(s), reserved);
548 void strbuf_humanise_bytes(struct strbuf *buf, off_t bytes)
550 if (bytes > 1 << 30) {
551 strbuf_addf(buf, "%u.%2.2u GiB",
553 (int)(bytes & ((1 << 30) - 1)) / 10737419);
554 } else if (bytes > 1 << 20) {
555 int x = bytes + 5243; /* for rounding */
556 strbuf_addf(buf, "%u.%2.2u MiB",
557 x >> 20, ((x & ((1 << 20) - 1)) * 100) >> 20);
558 } else if (bytes > 1 << 10) {
559 int x = bytes + 5; /* for rounding */
560 strbuf_addf(buf, "%u.%2.2u KiB",
561 x >> 10, ((x & ((1 << 10) - 1)) * 100) >> 10);
563 strbuf_addf(buf, "%u bytes", (int)bytes);
567 int printf_ln(const char *fmt, ...)
572 ret = vprintf(fmt, ap);
574 if (ret < 0 || putchar('\n') == EOF)
579 int fprintf_ln(FILE *fp, const char *fmt, ...)
584 ret = vfprintf(fp, fmt, ap);
586 if (ret < 0 || putc('\n', fp) == EOF)
591 char *xstrdup_tolower(const char *string)
596 len = strlen(string);
597 result = xmalloc(len + 1);
598 for (i = 0; i < len; i++)
599 result[i] = tolower(string[i]);