strbuf: avoid calling strbuf_grow() twice in strbuf_addbuf()
[git.git] / strbuf.c
blob600e27ea4a04ad3404641cf6faa772df64beb583
1 #include "cache.h"
2 #include "refs.h"
3 #include "utf8.h"
5 int starts_with(const char *str, const char *prefix)
7 for (; ; str++, prefix++)
8 if (!*prefix)
9 return 1;
10 else if (*str != *prefix)
11 return 0;
15 * Used as the default ->buf value, so that people can always assume
16 * buf is non NULL and ->buf is NUL terminated even for a freshly
17 * initialized strbuf.
19 char strbuf_slopbuf[1];
21 void strbuf_init(struct strbuf *sb, size_t hint)
23 sb->alloc = sb->len = 0;
24 sb->buf = strbuf_slopbuf;
25 if (hint)
26 strbuf_grow(sb, hint);
29 void strbuf_release(struct strbuf *sb)
31 if (sb->alloc) {
32 free(sb->buf);
33 strbuf_init(sb, 0);
37 char *strbuf_detach(struct strbuf *sb, size_t *sz)
39 char *res;
40 strbuf_grow(sb, 0);
41 res = sb->buf;
42 if (sz)
43 *sz = sb->len;
44 strbuf_init(sb, 0);
45 return res;
48 void strbuf_attach(struct strbuf *sb, void *buf, size_t len, size_t alloc)
50 strbuf_release(sb);
51 sb->buf = buf;
52 sb->len = len;
53 sb->alloc = alloc;
54 strbuf_grow(sb, 0);
55 sb->buf[sb->len] = '\0';
58 void strbuf_grow(struct strbuf *sb, size_t extra)
60 int new_buf = !sb->alloc;
61 if (unsigned_add_overflows(extra, 1) ||
62 unsigned_add_overflows(sb->len, extra + 1))
63 die("you want to use way too much memory");
64 if (new_buf)
65 sb->buf = NULL;
66 ALLOC_GROW(sb->buf, sb->len + extra + 1, sb->alloc);
67 if (new_buf)
68 sb->buf[0] = '\0';
71 void strbuf_trim(struct strbuf *sb)
73 strbuf_rtrim(sb);
74 strbuf_ltrim(sb);
76 void strbuf_rtrim(struct strbuf *sb)
78 while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1]))
79 sb->len--;
80 sb->buf[sb->len] = '\0';
83 void strbuf_ltrim(struct strbuf *sb)
85 char *b = sb->buf;
86 while (sb->len > 0 && isspace(*b)) {
87 b++;
88 sb->len--;
90 memmove(sb->buf, b, sb->len);
91 sb->buf[sb->len] = '\0';
94 int strbuf_reencode(struct strbuf *sb, const char *from, const char *to)
96 char *out;
97 int len;
99 if (same_encoding(from, to))
100 return 0;
102 out = reencode_string_len(sb->buf, sb->len, to, from, &len);
103 if (!out)
104 return -1;
106 strbuf_attach(sb, out, len, len);
107 return 0;
110 void strbuf_tolower(struct strbuf *sb)
112 char *p = sb->buf, *end = sb->buf + sb->len;
113 for (; p < end; p++)
114 *p = tolower(*p);
117 struct strbuf **strbuf_split_buf(const char *str, size_t slen,
118 int terminator, int max)
120 struct strbuf **ret = NULL;
121 size_t nr = 0, alloc = 0;
122 struct strbuf *t;
124 while (slen) {
125 int len = slen;
126 if (max <= 0 || nr + 1 < max) {
127 const char *end = memchr(str, terminator, slen);
128 if (end)
129 len = end - str + 1;
131 t = xmalloc(sizeof(struct strbuf));
132 strbuf_init(t, len);
133 strbuf_add(t, str, len);
134 ALLOC_GROW(ret, nr + 2, alloc);
135 ret[nr++] = t;
136 str += len;
137 slen -= len;
139 ALLOC_GROW(ret, nr + 1, alloc); /* In case string was empty */
140 ret[nr] = NULL;
141 return ret;
144 void strbuf_list_free(struct strbuf **sbs)
146 struct strbuf **s = sbs;
148 while (*s) {
149 strbuf_release(*s);
150 free(*s++);
152 free(sbs);
155 int strbuf_cmp(const struct strbuf *a, const struct strbuf *b)
157 int len = a->len < b->len ? a->len: b->len;
158 int cmp = memcmp(a->buf, b->buf, len);
159 if (cmp)
160 return cmp;
161 return a->len < b->len ? -1: a->len != b->len;
164 void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
165 const void *data, size_t dlen)
167 if (unsigned_add_overflows(pos, len))
168 die("you want to use way too much memory");
169 if (pos > sb->len)
170 die("`pos' is too far after the end of the buffer");
171 if (pos + len > sb->len)
172 die("`pos + len' is too far after the end of the buffer");
174 if (dlen >= len)
175 strbuf_grow(sb, dlen - len);
176 memmove(sb->buf + pos + dlen,
177 sb->buf + pos + len,
178 sb->len - pos - len);
179 memcpy(sb->buf + pos, data, dlen);
180 strbuf_setlen(sb, sb->len + dlen - len);
183 void strbuf_insert(struct strbuf *sb, size_t pos, const void *data, size_t len)
185 strbuf_splice(sb, pos, 0, data, len);
188 void strbuf_remove(struct strbuf *sb, size_t pos, size_t len)
190 strbuf_splice(sb, pos, len, NULL, 0);
193 void strbuf_add(struct strbuf *sb, const void *data, size_t len)
195 strbuf_grow(sb, len);
196 memcpy(sb->buf + sb->len, data, len);
197 strbuf_setlen(sb, sb->len + len);
200 void strbuf_addbuf(struct strbuf *sb, const struct strbuf *sb2)
202 strbuf_grow(sb, sb2->len);
203 memcpy(sb->buf + sb->len, sb2->buf, sb2->len);
204 strbuf_setlen(sb, sb->len + sb2->len);
207 void strbuf_adddup(struct strbuf *sb, size_t pos, size_t len)
209 strbuf_grow(sb, len);
210 memcpy(sb->buf + sb->len, sb->buf + pos, len);
211 strbuf_setlen(sb, sb->len + len);
214 void strbuf_addchars(struct strbuf *sb, int c, size_t n)
216 strbuf_grow(sb, n);
217 memset(sb->buf + sb->len, c, n);
218 strbuf_setlen(sb, sb->len + n);
221 void strbuf_addf(struct strbuf *sb, const char *fmt, ...)
223 va_list ap;
224 va_start(ap, fmt);
225 strbuf_vaddf(sb, fmt, ap);
226 va_end(ap);
229 static void add_lines(struct strbuf *out,
230 const char *prefix1,
231 const char *prefix2,
232 const char *buf, size_t size)
234 while (size) {
235 const char *prefix;
236 const char *next = memchr(buf, '\n', size);
237 next = next ? (next + 1) : (buf + size);
239 prefix = ((prefix2 && (buf[0] == '\n' || buf[0] == '\t'))
240 ? prefix2 : prefix1);
241 strbuf_addstr(out, prefix);
242 strbuf_add(out, buf, next - buf);
243 size -= next - buf;
244 buf = next;
246 strbuf_complete_line(out);
249 void strbuf_add_commented_lines(struct strbuf *out, const char *buf, size_t size)
251 static char prefix1[3];
252 static char prefix2[2];
254 if (prefix1[0] != comment_line_char) {
255 sprintf(prefix1, "%c ", comment_line_char);
256 sprintf(prefix2, "%c", comment_line_char);
258 add_lines(out, prefix1, prefix2, buf, size);
261 void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...)
263 va_list params;
264 struct strbuf buf = STRBUF_INIT;
265 int incomplete_line = sb->len && sb->buf[sb->len - 1] != '\n';
267 va_start(params, fmt);
268 strbuf_vaddf(&buf, fmt, params);
269 va_end(params);
271 strbuf_add_commented_lines(sb, buf.buf, buf.len);
272 if (incomplete_line)
273 sb->buf[--sb->len] = '\0';
275 strbuf_release(&buf);
278 void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap)
280 int len;
281 va_list cp;
283 if (!strbuf_avail(sb))
284 strbuf_grow(sb, 64);
285 va_copy(cp, ap);
286 len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, cp);
287 va_end(cp);
288 if (len < 0)
289 die("BUG: your vsnprintf is broken (returned %d)", len);
290 if (len > strbuf_avail(sb)) {
291 strbuf_grow(sb, len);
292 len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
293 if (len > strbuf_avail(sb))
294 die("BUG: your vsnprintf is broken (insatiable)");
296 strbuf_setlen(sb, sb->len + len);
299 void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn,
300 void *context)
302 for (;;) {
303 const char *percent;
304 size_t consumed;
306 percent = strchrnul(format, '%');
307 strbuf_add(sb, format, percent - format);
308 if (!*percent)
309 break;
310 format = percent + 1;
312 if (*format == '%') {
313 strbuf_addch(sb, '%');
314 format++;
315 continue;
318 consumed = fn(sb, format, context);
319 if (consumed)
320 format += consumed;
321 else
322 strbuf_addch(sb, '%');
326 size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder,
327 void *context)
329 struct strbuf_expand_dict_entry *e = context;
330 size_t len;
332 for (; e->placeholder && (len = strlen(e->placeholder)); e++) {
333 if (!strncmp(placeholder, e->placeholder, len)) {
334 if (e->value)
335 strbuf_addstr(sb, e->value);
336 return len;
339 return 0;
342 void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src)
344 int i, len = src->len;
346 for (i = 0; i < len; i++) {
347 if (src->buf[i] == '%')
348 strbuf_addch(dst, '%');
349 strbuf_addch(dst, src->buf[i]);
353 size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f)
355 size_t res;
356 size_t oldalloc = sb->alloc;
358 strbuf_grow(sb, size);
359 res = fread(sb->buf + sb->len, 1, size, f);
360 if (res > 0)
361 strbuf_setlen(sb, sb->len + res);
362 else if (oldalloc == 0)
363 strbuf_release(sb);
364 return res;
367 ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
369 size_t oldlen = sb->len;
370 size_t oldalloc = sb->alloc;
372 strbuf_grow(sb, hint ? hint : 8192);
373 for (;;) {
374 ssize_t want = sb->alloc - sb->len - 1;
375 ssize_t got = read_in_full(fd, sb->buf + sb->len, want);
377 if (got < 0) {
378 if (oldalloc == 0)
379 strbuf_release(sb);
380 else
381 strbuf_setlen(sb, oldlen);
382 return -1;
384 sb->len += got;
385 if (got < want)
386 break;
387 strbuf_grow(sb, 8192);
390 sb->buf[sb->len] = '\0';
391 return sb->len - oldlen;
394 #define STRBUF_MAXLINK (2*PATH_MAX)
396 int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint)
398 size_t oldalloc = sb->alloc;
400 if (hint < 32)
401 hint = 32;
403 while (hint < STRBUF_MAXLINK) {
404 int len;
406 strbuf_grow(sb, hint);
407 len = readlink(path, sb->buf, hint);
408 if (len < 0) {
409 if (errno != ERANGE)
410 break;
411 } else if (len < hint) {
412 strbuf_setlen(sb, len);
413 return 0;
416 /* .. the buffer was too small - try again */
417 hint *= 2;
419 if (oldalloc == 0)
420 strbuf_release(sb);
421 return -1;
424 int strbuf_getcwd(struct strbuf *sb)
426 size_t oldalloc = sb->alloc;
427 size_t guessed_len = 128;
429 for (;; guessed_len *= 2) {
430 strbuf_grow(sb, guessed_len);
431 if (getcwd(sb->buf, sb->alloc)) {
432 strbuf_setlen(sb, strlen(sb->buf));
433 return 0;
435 if (errno != ERANGE)
436 break;
438 if (oldalloc == 0)
439 strbuf_release(sb);
440 else
441 strbuf_reset(sb);
442 return -1;
445 #ifdef HAVE_GETDELIM
446 int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
448 ssize_t r;
450 if (feof(fp))
451 return EOF;
453 strbuf_reset(sb);
455 /* Translate slopbuf to NULL, as we cannot call realloc on it */
456 if (!sb->alloc)
457 sb->buf = NULL;
458 r = getdelim(&sb->buf, &sb->alloc, term, fp);
460 if (r > 0) {
461 sb->len = r;
462 return 0;
464 assert(r == -1);
467 * Normally we would have called xrealloc, which will try to free
468 * memory and recover. But we have no way to tell getdelim() to do so.
469 * Worse, we cannot try to recover ENOMEM ourselves, because we have
470 * no idea how many bytes were read by getdelim.
472 * Dying here is reasonable. It mirrors what xrealloc would do on
473 * catastrophic memory failure. We skip the opportunity to free pack
474 * memory and retry, but that's unlikely to help for a malloc small
475 * enough to hold a single line of input, anyway.
477 if (errno == ENOMEM)
478 die("Out of memory, getdelim failed");
480 /* Restore slopbuf that we moved out of the way before */
481 if (!sb->buf)
482 strbuf_init(sb, 0);
483 return EOF;
485 #else
486 int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
488 int ch;
490 if (feof(fp))
491 return EOF;
493 strbuf_reset(sb);
494 flockfile(fp);
495 while ((ch = getc_unlocked(fp)) != EOF) {
496 if (!strbuf_avail(sb))
497 strbuf_grow(sb, 1);
498 sb->buf[sb->len++] = ch;
499 if (ch == term)
500 break;
502 funlockfile(fp);
503 if (ch == EOF && sb->len == 0)
504 return EOF;
506 sb->buf[sb->len] = '\0';
507 return 0;
509 #endif
511 int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
513 if (strbuf_getwholeline(sb, fp, term))
514 return EOF;
515 if (sb->buf[sb->len-1] == term)
516 strbuf_setlen(sb, sb->len-1);
517 return 0;
520 int strbuf_getwholeline_fd(struct strbuf *sb, int fd, int term)
522 strbuf_reset(sb);
524 while (1) {
525 char ch;
526 ssize_t len = xread(fd, &ch, 1);
527 if (len <= 0)
528 return EOF;
529 strbuf_addch(sb, ch);
530 if (ch == term)
531 break;
533 return 0;
536 ssize_t strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)
538 int fd;
539 ssize_t len;
541 fd = open(path, O_RDONLY);
542 if (fd < 0)
543 return -1;
544 len = strbuf_read(sb, fd, hint);
545 close(fd);
546 if (len < 0)
547 return -1;
549 return len;
552 void strbuf_add_lines(struct strbuf *out, const char *prefix,
553 const char *buf, size_t size)
555 add_lines(out, prefix, NULL, buf, size);
558 void strbuf_addstr_xml_quoted(struct strbuf *buf, const char *s)
560 while (*s) {
561 size_t len = strcspn(s, "\"<>&");
562 strbuf_add(buf, s, len);
563 s += len;
564 switch (*s) {
565 case '"':
566 strbuf_addstr(buf, "&quot;");
567 break;
568 case '<':
569 strbuf_addstr(buf, "&lt;");
570 break;
571 case '>':
572 strbuf_addstr(buf, "&gt;");
573 break;
574 case '&':
575 strbuf_addstr(buf, "&amp;");
576 break;
577 case 0:
578 return;
580 s++;
584 static int is_rfc3986_reserved(char ch)
586 switch (ch) {
587 case '!': case '*': case '\'': case '(': case ')': case ';':
588 case ':': case '@': case '&': case '=': case '+': case '$':
589 case ',': case '/': case '?': case '#': case '[': case ']':
590 return 1;
592 return 0;
595 static int is_rfc3986_unreserved(char ch)
597 return isalnum(ch) ||
598 ch == '-' || ch == '_' || ch == '.' || ch == '~';
601 static void strbuf_add_urlencode(struct strbuf *sb, const char *s, size_t len,
602 int reserved)
604 strbuf_grow(sb, len);
605 while (len--) {
606 char ch = *s++;
607 if (is_rfc3986_unreserved(ch) ||
608 (!reserved && is_rfc3986_reserved(ch)))
609 strbuf_addch(sb, ch);
610 else
611 strbuf_addf(sb, "%%%02x", ch);
615 void strbuf_addstr_urlencode(struct strbuf *sb, const char *s,
616 int reserved)
618 strbuf_add_urlencode(sb, s, strlen(s), reserved);
621 void strbuf_humanise_bytes(struct strbuf *buf, off_t bytes)
623 if (bytes > 1 << 30) {
624 strbuf_addf(buf, "%u.%2.2u GiB",
625 (int)(bytes >> 30),
626 (int)(bytes & ((1 << 30) - 1)) / 10737419);
627 } else if (bytes > 1 << 20) {
628 int x = bytes + 5243; /* for rounding */
629 strbuf_addf(buf, "%u.%2.2u MiB",
630 x >> 20, ((x & ((1 << 20) - 1)) * 100) >> 20);
631 } else if (bytes > 1 << 10) {
632 int x = bytes + 5; /* for rounding */
633 strbuf_addf(buf, "%u.%2.2u KiB",
634 x >> 10, ((x & ((1 << 10) - 1)) * 100) >> 10);
635 } else {
636 strbuf_addf(buf, "%u bytes", (int)bytes);
640 void strbuf_add_absolute_path(struct strbuf *sb, const char *path)
642 if (!*path)
643 die("The empty string is not a valid path");
644 if (!is_absolute_path(path)) {
645 struct stat cwd_stat, pwd_stat;
646 size_t orig_len = sb->len;
647 char *cwd = xgetcwd();
648 char *pwd = getenv("PWD");
649 if (pwd && strcmp(pwd, cwd) &&
650 !stat(cwd, &cwd_stat) &&
651 (cwd_stat.st_dev || cwd_stat.st_ino) &&
652 !stat(pwd, &pwd_stat) &&
653 pwd_stat.st_dev == cwd_stat.st_dev &&
654 pwd_stat.st_ino == cwd_stat.st_ino)
655 strbuf_addstr(sb, pwd);
656 else
657 strbuf_addstr(sb, cwd);
658 if (sb->len > orig_len && !is_dir_sep(sb->buf[sb->len - 1]))
659 strbuf_addch(sb, '/');
660 free(cwd);
662 strbuf_addstr(sb, path);
665 int printf_ln(const char *fmt, ...)
667 int ret;
668 va_list ap;
669 va_start(ap, fmt);
670 ret = vprintf(fmt, ap);
671 va_end(ap);
672 if (ret < 0 || putchar('\n') == EOF)
673 return -1;
674 return ret + 1;
677 int fprintf_ln(FILE *fp, const char *fmt, ...)
679 int ret;
680 va_list ap;
681 va_start(ap, fmt);
682 ret = vfprintf(fp, fmt, ap);
683 va_end(ap);
684 if (ret < 0 || putc('\n', fp) == EOF)
685 return -1;
686 return ret + 1;
689 char *xstrdup_tolower(const char *string)
691 char *result;
692 size_t len, i;
694 len = strlen(string);
695 result = xmalloc(len + 1);
696 for (i = 0; i < len; i++)
697 result[i] = tolower(string[i]);
698 result[i] = '\0';
699 return result;
702 char *xstrvfmt(const char *fmt, va_list ap)
704 struct strbuf buf = STRBUF_INIT;
705 strbuf_vaddf(&buf, fmt, ap);
706 return strbuf_detach(&buf, NULL);
709 char *xstrfmt(const char *fmt, ...)
711 va_list ap;
712 char *ret;
714 va_start(ap, fmt);
715 ret = xstrvfmt(fmt, ap);
716 va_end(ap);
718 return ret;
721 void strbuf_addftime(struct strbuf *sb, const char *fmt, const struct tm *tm)
723 size_t hint = 128;
724 size_t len;
726 if (!*fmt)
727 return;
729 strbuf_grow(sb, hint);
730 len = strftime(sb->buf + sb->len, sb->alloc - sb->len, fmt, tm);
732 if (!len) {
734 * strftime reports "0" if it could not fit the result in the buffer.
735 * Unfortunately, it also reports "0" if the requested time string
736 * takes 0 bytes. So our strategy is to munge the format so that the
737 * output contains at least one character, and then drop the extra
738 * character before returning.
740 struct strbuf munged_fmt = STRBUF_INIT;
741 strbuf_addf(&munged_fmt, "%s ", fmt);
742 while (!len) {
743 hint *= 2;
744 strbuf_grow(sb, hint);
745 len = strftime(sb->buf + sb->len, sb->alloc - sb->len,
746 munged_fmt.buf, tm);
748 strbuf_release(&munged_fmt);
749 len--; /* drop munged space */
751 strbuf_setlen(sb, sb->len + len);
755 * Returns the length of a line, without trailing spaces.
757 * If the line ends with newline, it will be removed too.
759 static size_t cleanup(char *line, size_t len)
761 while (len) {
762 unsigned char c = line[len - 1];
763 if (!isspace(c))
764 break;
765 len--;
768 return len;
772 * Remove empty lines from the beginning and end
773 * and also trailing spaces from every line.
775 * Turn multiple consecutive empty lines between paragraphs
776 * into just one empty line.
778 * If the input has only empty lines and spaces,
779 * no output will be produced.
781 * If last line does not have a newline at the end, one is added.
783 * Enable skip_comments to skip every line starting with comment
784 * character.
786 void strbuf_stripspace(struct strbuf *sb, int skip_comments)
788 int empties = 0;
789 size_t i, j, len, newlen;
790 char *eol;
792 /* We may have to add a newline. */
793 strbuf_grow(sb, 1);
795 for (i = j = 0; i < sb->len; i += len, j += newlen) {
796 eol = memchr(sb->buf + i, '\n', sb->len - i);
797 len = eol ? eol - (sb->buf + i) + 1 : sb->len - i;
799 if (skip_comments && len && sb->buf[i] == comment_line_char) {
800 newlen = 0;
801 continue;
803 newlen = cleanup(sb->buf + i, len);
805 /* Not just an empty line? */
806 if (newlen) {
807 if (empties > 0 && j > 0)
808 sb->buf[j++] = '\n';
809 empties = 0;
810 memmove(sb->buf + j, sb->buf + i, newlen);
811 sb->buf[newlen + j++] = '\n';
812 } else {
813 empties++;
817 strbuf_setlen(sb, j);