Third batch for 2.1
[git/git.git] / strbuf.c
CommitLineData
812666c8 1#include "cache.h"
a2fab531 2#include "refs.h"
d4241f52 3#include "utf8.h"
d1df5743 4
95662315
CC
5int starts_with(const char *str, const char *prefix)
6{
7 for (; ; str++, prefix++)
8 if (!*prefix)
9 return 1;
10 else if (*str != *prefix)
11 return 0;
12}
13
95662315
CC
14int ends_with(const char *str, const char *suffix)
15{
16 int len = strlen(str), suflen = strlen(suffix);
17 if (len < suflen)
18 return 0;
19 else
20 return !strcmp(str + len - suflen, suffix);
21}
22
b315c5c0
PH
23/*
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
26 * initialized strbuf.
27 */
28char strbuf_slopbuf[1];
29
917c9a71
PH
30void strbuf_init(struct strbuf *sb, size_t hint)
31{
b315c5c0
PH
32 sb->alloc = sb->len = 0;
33 sb->buf = strbuf_slopbuf;
8c74ef1e 34 if (hint)
f1696ee3 35 strbuf_grow(sb, hint);
d1df5743
JH
36}
37
917c9a71
PH
38void strbuf_release(struct strbuf *sb)
39{
b315c5c0
PH
40 if (sb->alloc) {
41 free(sb->buf);
42 strbuf_init(sb, 0);
43 }
b449f4cf
PH
44}
45
b315c5c0 46char *strbuf_detach(struct strbuf *sb, size_t *sz)
917c9a71 47{
08ad56f3
JK
48 char *res;
49 strbuf_grow(sb, 0);
50 res = sb->buf;
b315c5c0
PH
51 if (sz)
52 *sz = sb->len;
f1696ee3 53 strbuf_init(sb, 0);
b449f4cf
PH
54 return res;
55}
56
917c9a71
PH
57void strbuf_attach(struct strbuf *sb, void *buf, size_t len, size_t alloc)
58{
59 strbuf_release(sb);
60 sb->buf = buf;
61 sb->len = len;
62 sb->alloc = alloc;
63 strbuf_grow(sb, 0);
64 sb->buf[sb->len] = '\0';
65}
66
67void strbuf_grow(struct strbuf *sb, size_t extra)
68{
8c74ef1e 69 int new_buf = !sb->alloc;
1368f650
JN
70 if (unsigned_add_overflows(extra, 1) ||
71 unsigned_add_overflows(sb->len, extra + 1))
b449f4cf 72 die("you want to use way too much memory");
8c74ef1e 73 if (new_buf)
b315c5c0 74 sb->buf = NULL;
b449f4cf 75 ALLOC_GROW(sb->buf, sb->len + extra + 1, sb->alloc);
8c74ef1e
TR
76 if (new_buf)
77 sb->buf[0] = '\0';
b449f4cf
PH
78}
79
eacd6dc5
LS
80void strbuf_trim(struct strbuf *sb)
81{
3bb55e8a
BG
82 strbuf_rtrim(sb);
83 strbuf_ltrim(sb);
eacd6dc5 84}
f1696ee3
PH
85void strbuf_rtrim(struct strbuf *sb)
86{
87 while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1]))
88 sb->len--;
89 sb->buf[sb->len] = '\0';
90}
91
eacd6dc5
LS
92void strbuf_ltrim(struct strbuf *sb)
93{
94 char *b = sb->buf;
95 while (sb->len > 0 && isspace(*b)) {
96 b++;
97 sb->len--;
98 }
99 memmove(sb->buf, b, sb->len);
100 sb->buf[sb->len] = '\0';
101}
102
d4241f52
JK
103int strbuf_reencode(struct strbuf *sb, const char *from, const char *to)
104{
105 char *out;
106 int len;
107
108 if (same_encoding(from, to))
109 return 0;
110
111 out = reencode_string_len(sb->buf, sb->len, to, from, &len);
112 if (!out)
113 return -1;
114
115 strbuf_attach(sb, out, len, len);
116 return 0;
117}
118
ffb20ce1
JK
119void strbuf_tolower(struct strbuf *sb)
120{
121 char *p = sb->buf, *end = sb->buf + sb->len;
122 for (; p < end; p++)
123 *p = tolower(*p);
124}
125
17b73dc6
MH
126struct strbuf **strbuf_split_buf(const char *str, size_t slen,
127 int terminator, int max)
eacd6dc5 128{
b8c2c1fa
MH
129 struct strbuf **ret = NULL;
130 size_t nr = 0, alloc = 0;
eacd6dc5
LS
131 struct strbuf *t;
132
1173bb33
MH
133 while (slen) {
134 int len = slen;
135 if (max <= 0 || nr + 1 < max) {
17b73dc6 136 const char *end = memchr(str, terminator, slen);
1173bb33
MH
137 if (end)
138 len = end - str + 1;
139 }
eacd6dc5
LS
140 t = xmalloc(sizeof(struct strbuf));
141 strbuf_init(t, len);
1173bb33 142 strbuf_add(t, str, len);
b8c2c1fa
MH
143 ALLOC_GROW(ret, nr + 2, alloc);
144 ret[nr++] = t;
1173bb33
MH
145 str += len;
146 slen -= len;
eacd6dc5 147 }
b8c2c1fa
MH
148 ALLOC_GROW(ret, nr + 1, alloc); /* In case string was empty */
149 ret[nr] = NULL;
eacd6dc5
LS
150 return ret;
151}
152
153void strbuf_list_free(struct strbuf **sbs)
154{
155 struct strbuf **s = sbs;
156
157 while (*s) {
158 strbuf_release(*s);
159 free(*s++);
160 }
161 free(sbs);
162}
163
9b200fd6 164int strbuf_cmp(const struct strbuf *a, const struct strbuf *b)
45f66f64 165{
8f024655
AR
166 int len = a->len < b->len ? a->len: b->len;
167 int cmp = memcmp(a->buf, b->buf, len);
168 if (cmp)
169 return cmp;
170 return a->len < b->len ? -1: a->len != b->len;
45f66f64
PH
171}
172
917c9a71
PH
173void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
174 const void *data, size_t dlen)
175{
1368f650 176 if (unsigned_add_overflows(pos, len))
917c9a71
PH
177 die("you want to use way too much memory");
178 if (pos > sb->len)
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");
182
183 if (dlen >= len)
184 strbuf_grow(sb, dlen - len);
185 memmove(sb->buf + pos + dlen,
186 sb->buf + pos + len,
187 sb->len - pos - len);
188 memcpy(sb->buf + pos, data, dlen);
189 strbuf_setlen(sb, sb->len + dlen - len);
190}
191
c76689df
PH
192void strbuf_insert(struct strbuf *sb, size_t pos, const void *data, size_t len)
193{
194 strbuf_splice(sb, pos, 0, data, len);
195}
196
197void strbuf_remove(struct strbuf *sb, size_t pos, size_t len)
198{
199 strbuf_splice(sb, pos, len, NULL, 0);
200}
201
917c9a71
PH
202void strbuf_add(struct strbuf *sb, const void *data, size_t len)
203{
b449f4cf
PH
204 strbuf_grow(sb, len);
205 memcpy(sb->buf + sb->len, data, len);
206 strbuf_setlen(sb, sb->len + len);
207}
208
91db267e
RS
209void strbuf_adddup(struct strbuf *sb, size_t pos, size_t len)
210{
211 strbuf_grow(sb, len);
212 memcpy(sb->buf + sb->len, sb->buf + pos, len);
213 strbuf_setlen(sb, sb->len + len);
214}
215
917c9a71
PH
216void strbuf_addf(struct strbuf *sb, const char *fmt, ...)
217{
b449f4cf 218 va_list ap;
ebeb6090
JK
219 va_start(ap, fmt);
220 strbuf_vaddf(sb, fmt, ap);
221 va_end(ap);
222}
223
eff80a9f
JH
224static void add_lines(struct strbuf *out,
225 const char *prefix1,
226 const char *prefix2,
227 const char *buf, size_t size)
228{
229 while (size) {
230 const char *prefix;
231 const char *next = memchr(buf, '\n', size);
232 next = next ? (next + 1) : (buf + size);
233
234 prefix = (prefix2 && buf[0] == '\n') ? prefix2 : prefix1;
235 strbuf_addstr(out, prefix);
236 strbuf_add(out, buf, next - buf);
237 size -= next - buf;
238 buf = next;
239 }
240 strbuf_complete_line(out);
241}
242
243void strbuf_add_commented_lines(struct strbuf *out, const char *buf, size_t size)
244{
245 static char prefix1[3];
246 static char prefix2[2];
247
248 if (prefix1[0] != comment_line_char) {
249 sprintf(prefix1, "%c ", comment_line_char);
250 sprintf(prefix2, "%c", comment_line_char);
251 }
252 add_lines(out, prefix1, prefix2, buf, size);
253}
254
255void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...)
256{
257 va_list params;
258 struct strbuf buf = STRBUF_INIT;
259 int incomplete_line = sb->len && sb->buf[sb->len - 1] != '\n';
260
261 va_start(params, fmt);
262 strbuf_vaddf(&buf, fmt, params);
263 va_end(params);
264
265 strbuf_add_commented_lines(sb, buf.buf, buf.len);
266 if (incomplete_line)
267 sb->buf[--sb->len] = '\0';
268
269 strbuf_release(&buf);
270}
271
ebeb6090
JK
272void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap)
273{
274 int len;
275 va_list cp;
b449f4cf 276
f141bd80
SP
277 if (!strbuf_avail(sb))
278 strbuf_grow(sb, 64);
ebeb6090
JK
279 va_copy(cp, ap);
280 len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, cp);
281 va_end(cp);
f141bd80 282 if (len < 0)
ebeb6090 283 die("BUG: your vsnprintf is broken (returned %d)", len);
f1696ee3 284 if (len > strbuf_avail(sb)) {
b449f4cf 285 strbuf_grow(sb, len);
b449f4cf 286 len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
ebeb6090
JK
287 if (len > strbuf_avail(sb))
288 die("BUG: your vsnprintf is broken (insatiable)");
b449f4cf
PH
289 }
290 strbuf_setlen(sb, sb->len + len);
d1df5743
JH
291}
292
c3a670de
MC
293void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn,
294 void *context)
cde75e59
RS
295{
296 for (;;) {
c3a670de
MC
297 const char *percent;
298 size_t consumed;
cde75e59
RS
299
300 percent = strchrnul(format, '%');
301 strbuf_add(sb, format, percent - format);
302 if (!*percent)
303 break;
304 format = percent + 1;
305
0a0416a3
JK
306 if (*format == '%') {
307 strbuf_addch(sb, '%');
308 format++;
309 continue;
310 }
311
c3a670de
MC
312 consumed = fn(sb, format, context);
313 if (consumed)
314 format += consumed;
315 else
cde75e59
RS
316 strbuf_addch(sb, '%');
317 }
318}
319
9b864e73
RS
320size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder,
321 void *context)
322{
323 struct strbuf_expand_dict_entry *e = context;
324 size_t len;
325
326 for (; e->placeholder && (len = strlen(e->placeholder)); e++) {
327 if (!strncmp(placeholder, e->placeholder, len)) {
328 if (e->value)
329 strbuf_addstr(sb, e->value);
330 return len;
331 }
332 }
333 return 0;
334}
335
361df5df
JK
336void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src)
337{
338 int i, len = src->len;
339
340 for (i = 0; i < len; i++) {
341 if (src->buf[i] == '%')
342 strbuf_addch(dst, '%');
343 strbuf_addch(dst, src->buf[i]);
344 }
345}
346
917c9a71
PH
347size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f)
348{
b449f4cf 349 size_t res;
2fc64700 350 size_t oldalloc = sb->alloc;
b449f4cf
PH
351
352 strbuf_grow(sb, size);
353 res = fread(sb->buf + sb->len, 1, size, f);
2fc64700 354 if (res > 0)
b449f4cf 355 strbuf_setlen(sb, sb->len + res);
6651c3f7 356 else if (oldalloc == 0)
2fc64700 357 strbuf_release(sb);
b449f4cf 358 return res;
d1df5743
JH
359}
360
f1696ee3 361ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
b449f4cf
PH
362{
363 size_t oldlen = sb->len;
2fc64700 364 size_t oldalloc = sb->alloc;
b449f4cf 365
f1696ee3 366 strbuf_grow(sb, hint ? hint : 8192);
b449f4cf
PH
367 for (;;) {
368 ssize_t cnt;
369
b449f4cf
PH
370 cnt = xread(fd, sb->buf + sb->len, sb->alloc - sb->len - 1);
371 if (cnt < 0) {
2fc64700
RS
372 if (oldalloc == 0)
373 strbuf_release(sb);
374 else
375 strbuf_setlen(sb, oldlen);
b449f4cf
PH
376 return -1;
377 }
378 if (!cnt)
379 break;
380 sb->len += cnt;
f1696ee3 381 strbuf_grow(sb, 8192);
b449f4cf
PH
382 }
383
384 sb->buf[sb->len] = '\0';
385 return sb->len - oldlen;
d1df5743
JH
386}
387
b11b7e13
LT
388#define STRBUF_MAXLINK (2*PATH_MAX)
389
390int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint)
391{
2fc64700
RS
392 size_t oldalloc = sb->alloc;
393
b11b7e13
LT
394 if (hint < 32)
395 hint = 32;
396
397 while (hint < STRBUF_MAXLINK) {
398 int len;
399
400 strbuf_grow(sb, hint);
401 len = readlink(path, sb->buf, hint);
402 if (len < 0) {
403 if (errno != ERANGE)
404 break;
405 } else if (len < hint) {
406 strbuf_setlen(sb, len);
407 return 0;
408 }
409
410 /* .. the buffer was too small - try again */
411 hint *= 2;
412 }
2fc64700
RS
413 if (oldalloc == 0)
414 strbuf_release(sb);
b11b7e13
LT
415 return -1;
416}
417
c7e4f0d7 418int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
917c9a71 419{
d1df5743 420 int ch;
e6c019d0 421
e6c019d0
PH
422 if (feof(fp))
423 return EOF;
b449f4cf
PH
424
425 strbuf_reset(sb);
d1df5743 426 while ((ch = fgetc(fp)) != EOF) {
b449f4cf
PH
427 strbuf_grow(sb, 1);
428 sb->buf[sb->len++] = ch;
c7e4f0d7
BC
429 if (ch == term)
430 break;
d1df5743 431 }
e6c019d0
PH
432 if (ch == EOF && sb->len == 0)
433 return EOF;
b449f4cf 434
b449f4cf 435 sb->buf[sb->len] = '\0';
e6c019d0 436 return 0;
d1df5743 437}
a9390b9f 438
c7e4f0d7
BC
439int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
440{
441 if (strbuf_getwholeline(sb, fp, term))
442 return EOF;
443 if (sb->buf[sb->len-1] == term)
444 strbuf_setlen(sb, sb->len-1);
445 return 0;
446}
447
5e8617f5
TR
448int strbuf_getwholeline_fd(struct strbuf *sb, int fd, int term)
449{
450 strbuf_reset(sb);
451
452 while (1) {
453 char ch;
454 ssize_t len = xread(fd, &ch, 1);
455 if (len <= 0)
456 return EOF;
457 strbuf_addch(sb, ch);
458 if (ch == term)
459 break;
460 }
461 return 0;
462}
463
387e7e19 464int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)
a9390b9f
KH
465{
466 int fd, len;
467
468 fd = open(path, O_RDONLY);
469 if (fd < 0)
470 return -1;
387e7e19 471 len = strbuf_read(sb, fd, hint);
a9390b9f
KH
472 close(fd);
473 if (len < 0)
474 return -1;
475
476 return len;
477}
895680f0
JH
478
479void strbuf_add_lines(struct strbuf *out, const char *prefix,
480 const char *buf, size_t size)
481{
eff80a9f 482 add_lines(out, prefix, NULL, buf, size);
895680f0 483}
367d20ec 484
5963c036
MH
485void strbuf_addstr_xml_quoted(struct strbuf *buf, const char *s)
486{
487 while (*s) {
488 size_t len = strcspn(s, "\"<>&");
489 strbuf_add(buf, s, len);
490 s += len;
491 switch (*s) {
492 case '"':
493 strbuf_addstr(buf, "&quot;");
494 break;
495 case '<':
496 strbuf_addstr(buf, "&lt;");
497 break;
498 case '>':
499 strbuf_addstr(buf, "&gt;");
500 break;
501 case '&':
502 strbuf_addstr(buf, "&amp;");
503 break;
504 case 0:
505 return;
506 }
507 s++;
508 }
509}
510
c505116b
JK
511static int is_rfc3986_reserved(char ch)
512{
513 switch (ch) {
514 case '!': case '*': case '\'': case '(': case ')': case ';':
515 case ':': case '@': case '&': case '=': case '+': case '$':
516 case ',': case '/': case '?': case '#': case '[': case ']':
517 return 1;
518 }
519 return 0;
520}
521
522static int is_rfc3986_unreserved(char ch)
523{
524 return isalnum(ch) ||
525 ch == '-' || ch == '_' || ch == '.' || ch == '~';
526}
527
ea03a8e1
JH
528static void strbuf_add_urlencode(struct strbuf *sb, const char *s, size_t len,
529 int reserved)
c505116b
JK
530{
531 strbuf_grow(sb, len);
532 while (len--) {
533 char ch = *s++;
534 if (is_rfc3986_unreserved(ch) ||
535 (!reserved && is_rfc3986_reserved(ch)))
536 strbuf_addch(sb, ch);
537 else
538 strbuf_addf(sb, "%%%02x", ch);
539 }
540}
541
542void strbuf_addstr_urlencode(struct strbuf *sb, const char *s,
543 int reserved)
544{
545 strbuf_add_urlencode(sb, s, strlen(s), reserved);
546}
9a0a30aa 547
079b546a
AP
548void strbuf_humanise_bytes(struct strbuf *buf, off_t bytes)
549{
550 if (bytes > 1 << 30) {
551 strbuf_addf(buf, "%u.%2.2u GiB",
552 (int)(bytes >> 30),
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);
562 } else {
563 strbuf_addf(buf, "%u bytes", (int)bytes);
564 }
565}
566
9a0a30aa
NTND
567int printf_ln(const char *fmt, ...)
568{
569 int ret;
570 va_list ap;
571 va_start(ap, fmt);
572 ret = vprintf(fmt, ap);
573 va_end(ap);
574 if (ret < 0 || putchar('\n') == EOF)
575 return -1;
576 return ret + 1;
577}
578
579int fprintf_ln(FILE *fp, const char *fmt, ...)
580{
581 int ret;
582 va_list ap;
583 va_start(ap, fmt);
584 ret = vfprintf(fp, fmt, ap);
585 va_end(ap);
586 if (ret < 0 || putc('\n', fp) == EOF)
587 return -1;
588 return ret + 1;
589}
88d5a6f6
JK
590
591char *xstrdup_tolower(const char *string)
592{
593 char *result;
594 size_t len, i;
595
596 len = strlen(string);
597 result = xmalloc(len + 1);
598 for (i = 0; i < len; i++)
599 result[i] = tolower(string[i]);
600 result[i] = '\0';
601 return result;
602}