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