Merge branch 'sk/msvc-dynlink-crt'
[git/git-svn.git] / strbuf.c
blob4d31567a1a6a67ae9697816d78da442f87ab5803
1 #include "cache.h"
2 #include "refs.h"
4 int starts_with(const char *str, const char *prefix)
6 for (; ; str++, prefix++)
7 if (!*prefix)
8 return 1;
9 else if (*str != *prefix)
10 return 0;
13 int ends_with(const char *str, const char *suffix)
15 int len = strlen(str), suflen = strlen(suffix);
16 if (len < suflen)
17 return 0;
18 else
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
25 * initialized strbuf.
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;
33 if (hint)
34 strbuf_grow(sb, hint);
37 void strbuf_release(struct strbuf *sb)
39 if (sb->alloc) {
40 free(sb->buf);
41 strbuf_init(sb, 0);
45 char *strbuf_detach(struct strbuf *sb, size_t *sz)
47 char *res;
48 strbuf_grow(sb, 0);
49 res = sb->buf;
50 if (sz)
51 *sz = sb->len;
52 strbuf_init(sb, 0);
53 return res;
56 void strbuf_attach(struct strbuf *sb, void *buf, size_t len, size_t alloc)
58 strbuf_release(sb);
59 sb->buf = buf;
60 sb->len = len;
61 sb->alloc = alloc;
62 strbuf_grow(sb, 0);
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");
72 if (new_buf)
73 sb->buf = NULL;
74 ALLOC_GROW(sb->buf, sb->len + extra + 1, sb->alloc);
75 if (new_buf)
76 sb->buf[0] = '\0';
79 void strbuf_trim(struct strbuf *sb)
81 strbuf_rtrim(sb);
82 strbuf_ltrim(sb);
84 void strbuf_rtrim(struct strbuf *sb)
86 while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1]))
87 sb->len--;
88 sb->buf[sb->len] = '\0';
91 void strbuf_ltrim(struct strbuf *sb)
93 char *b = sb->buf;
94 while (sb->len > 0 && isspace(*b)) {
95 b++;
96 sb->len--;
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;
107 struct strbuf *t;
109 while (slen) {
110 int len = slen;
111 if (max <= 0 || nr + 1 < max) {
112 const char *end = memchr(str, terminator, slen);
113 if (end)
114 len = end - str + 1;
116 t = xmalloc(sizeof(struct strbuf));
117 strbuf_init(t, len);
118 strbuf_add(t, str, len);
119 ALLOC_GROW(ret, nr + 2, alloc);
120 ret[nr++] = t;
121 str += len;
122 slen -= len;
124 ALLOC_GROW(ret, nr + 1, alloc); /* In case string was empty */
125 ret[nr] = NULL;
126 return ret;
129 void strbuf_list_free(struct strbuf **sbs)
131 struct strbuf **s = sbs;
133 while (*s) {
134 strbuf_release(*s);
135 free(*s++);
137 free(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);
144 if (cmp)
145 return cmp;
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");
154 if (pos > sb->len)
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");
159 if (dlen >= len)
160 strbuf_grow(sb, dlen - len);
161 memmove(sb->buf + pos + dlen,
162 sb->buf + pos + len,
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, ...)
194 va_list ap;
195 va_start(ap, fmt);
196 strbuf_vaddf(sb, fmt, ap);
197 va_end(ap);
200 static void add_lines(struct strbuf *out,
201 const char *prefix1,
202 const char *prefix2,
203 const char *buf, size_t size)
205 while (size) {
206 const char *prefix;
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);
213 size -= next - buf;
214 buf = next;
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, ...)
233 va_list params;
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);
239 va_end(params);
241 strbuf_add_commented_lines(sb, buf.buf, buf.len);
242 if (incomplete_line)
243 sb->buf[--sb->len] = '\0';
245 strbuf_release(&buf);
248 void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap)
250 int len;
251 va_list cp;
253 if (!strbuf_avail(sb))
254 strbuf_grow(sb, 64);
255 va_copy(cp, ap);
256 len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, cp);
257 va_end(cp);
258 if (len < 0)
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,
270 void *context)
272 for (;;) {
273 const char *percent;
274 size_t consumed;
276 percent = strchrnul(format, '%');
277 strbuf_add(sb, format, percent - format);
278 if (!*percent)
279 break;
280 format = percent + 1;
282 if (*format == '%') {
283 strbuf_addch(sb, '%');
284 format++;
285 continue;
288 consumed = fn(sb, format, context);
289 if (consumed)
290 format += consumed;
291 else
292 strbuf_addch(sb, '%');
296 size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder,
297 void *context)
299 struct strbuf_expand_dict_entry *e = context;
300 size_t len;
302 for (; e->placeholder && (len = strlen(e->placeholder)); e++) {
303 if (!strncmp(placeholder, e->placeholder, len)) {
304 if (e->value)
305 strbuf_addstr(sb, e->value);
306 return len;
309 return 0;
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)
325 size_t res;
326 size_t oldalloc = sb->alloc;
328 strbuf_grow(sb, size);
329 res = fread(sb->buf + sb->len, 1, size, f);
330 if (res > 0)
331 strbuf_setlen(sb, sb->len + res);
332 else if (oldalloc == 0)
333 strbuf_release(sb);
334 return res;
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);
343 for (;;) {
344 ssize_t cnt;
346 cnt = xread(fd, sb->buf + sb->len, sb->alloc - sb->len - 1);
347 if (cnt < 0) {
348 if (oldalloc == 0)
349 strbuf_release(sb);
350 else
351 strbuf_setlen(sb, oldlen);
352 return -1;
354 if (!cnt)
355 break;
356 sb->len += cnt;
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;
370 if (hint < 32)
371 hint = 32;
373 while (hint < STRBUF_MAXLINK) {
374 int len;
376 strbuf_grow(sb, hint);
377 len = readlink(path, sb->buf, hint);
378 if (len < 0) {
379 if (errno != ERANGE)
380 break;
381 } else if (len < hint) {
382 strbuf_setlen(sb, len);
383 return 0;
386 /* .. the buffer was too small - try again */
387 hint *= 2;
389 if (oldalloc == 0)
390 strbuf_release(sb);
391 return -1;
394 int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
396 int ch;
398 if (feof(fp))
399 return EOF;
401 strbuf_reset(sb);
402 while ((ch = fgetc(fp)) != EOF) {
403 strbuf_grow(sb, 1);
404 sb->buf[sb->len++] = ch;
405 if (ch == term)
406 break;
408 if (ch == EOF && sb->len == 0)
409 return EOF;
411 sb->buf[sb->len] = '\0';
412 return 0;
415 int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
417 if (strbuf_getwholeline(sb, fp, term))
418 return EOF;
419 if (sb->buf[sb->len-1] == term)
420 strbuf_setlen(sb, sb->len-1);
421 return 0;
424 int strbuf_getwholeline_fd(struct strbuf *sb, int fd, int term)
426 strbuf_reset(sb);
428 while (1) {
429 char ch;
430 ssize_t len = xread(fd, &ch, 1);
431 if (len <= 0)
432 return EOF;
433 strbuf_addch(sb, ch);
434 if (ch == term)
435 break;
437 return 0;
440 int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)
442 int fd, len;
444 fd = open(path, O_RDONLY);
445 if (fd < 0)
446 return -1;
447 len = strbuf_read(sb, fd, hint);
448 close(fd);
449 if (len < 0)
450 return -1;
452 return len;
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)
463 while (*s) {
464 size_t len = strcspn(s, "\"<>&");
465 strbuf_add(buf, s, len);
466 s += len;
467 switch (*s) {
468 case '"':
469 strbuf_addstr(buf, "&quot;");
470 break;
471 case '<':
472 strbuf_addstr(buf, "&lt;");
473 break;
474 case '>':
475 strbuf_addstr(buf, "&gt;");
476 break;
477 case '&':
478 strbuf_addstr(buf, "&amp;");
479 break;
480 case 0:
481 return;
483 s++;
487 static int is_rfc3986_reserved(char ch)
489 switch (ch) {
490 case '!': case '*': case '\'': case '(': case ')': case ';':
491 case ':': case '@': case '&': case '=': case '+': case '$':
492 case ',': case '/': case '?': case '#': case '[': case ']':
493 return 1;
495 return 0;
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,
505 int reserved)
507 strbuf_grow(sb, len);
508 while (len--) {
509 char ch = *s++;
510 if (is_rfc3986_unreserved(ch) ||
511 (!reserved && is_rfc3986_reserved(ch)))
512 strbuf_addch(sb, ch);
513 else
514 strbuf_addf(sb, "%%%02x", ch);
518 void strbuf_addstr_urlencode(struct strbuf *sb, const char *s,
519 int reserved)
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",
528 (int)(bytes >> 30),
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);
538 } else {
539 strbuf_addf(buf, "%u bytes", (int)bytes);
543 int printf_ln(const char *fmt, ...)
545 int ret;
546 va_list ap;
547 va_start(ap, fmt);
548 ret = vprintf(fmt, ap);
549 va_end(ap);
550 if (ret < 0 || putchar('\n') == EOF)
551 return -1;
552 return ret + 1;
555 int fprintf_ln(FILE *fp, const char *fmt, ...)
557 int ret;
558 va_list ap;
559 va_start(ap, fmt);
560 ret = vfprintf(fp, fmt, ap);
561 va_end(ap);
562 if (ret < 0 || putc('\n', fp) == EOF)
563 return -1;
564 return ret + 1;