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