Merge branch 'jk/repack-pack-writebitmaps-config'
[git/mingw/4msysgit.git] / strbuf.c
blobac62982e672c22457468f601b35716dc9a52a81c
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;
14 int ends_with(const char *str, const char *suffix)
16 int len = strlen(str), suflen = strlen(suffix);
17 if (len < suflen)
18 return 0;
19 else
20 return !strcmp(str + len - suflen, suffix);
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.
28 char strbuf_slopbuf[1];
30 void strbuf_init(struct strbuf *sb, size_t hint)
32 sb->alloc = sb->len = 0;
33 sb->buf = strbuf_slopbuf;
34 if (hint)
35 strbuf_grow(sb, hint);
38 void strbuf_release(struct strbuf *sb)
40 if (sb->alloc) {
41 free(sb->buf);
42 strbuf_init(sb, 0);
46 char *strbuf_detach(struct strbuf *sb, size_t *sz)
48 char *res;
49 strbuf_grow(sb, 0);
50 res = sb->buf;
51 if (sz)
52 *sz = sb->len;
53 strbuf_init(sb, 0);
54 return res;
57 void strbuf_attach(struct strbuf *sb, void *buf, size_t len, size_t alloc)
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';
67 void strbuf_grow(struct strbuf *sb, size_t extra)
69 int new_buf = !sb->alloc;
70 if (unsigned_add_overflows(extra, 1) ||
71 unsigned_add_overflows(sb->len, extra + 1))
72 die("you want to use way too much memory");
73 if (new_buf)
74 sb->buf = NULL;
75 ALLOC_GROW(sb->buf, sb->len + extra + 1, sb->alloc);
76 if (new_buf)
77 sb->buf[0] = '\0';
80 void strbuf_trim(struct strbuf *sb)
82 strbuf_rtrim(sb);
83 strbuf_ltrim(sb);
85 void strbuf_rtrim(struct strbuf *sb)
87 while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1]))
88 sb->len--;
89 sb->buf[sb->len] = '\0';
92 void strbuf_ltrim(struct strbuf *sb)
94 char *b = sb->buf;
95 while (sb->len > 0 && isspace(*b)) {
96 b++;
97 sb->len--;
99 memmove(sb->buf, b, sb->len);
100 sb->buf[sb->len] = '\0';
103 int strbuf_reencode(struct strbuf *sb, const char *from, const char *to)
105 char *out;
106 int len;
108 if (same_encoding(from, to))
109 return 0;
111 out = reencode_string_len(sb->buf, sb->len, to, from, &len);
112 if (!out)
113 return -1;
115 strbuf_attach(sb, out, len, len);
116 return 0;
119 void strbuf_tolower(struct strbuf *sb)
121 char *p = sb->buf, *end = sb->buf + sb->len;
122 for (; p < end; p++)
123 *p = tolower(*p);
126 struct strbuf **strbuf_split_buf(const char *str, size_t slen,
127 int terminator, int max)
129 struct strbuf **ret = NULL;
130 size_t nr = 0, alloc = 0;
131 struct strbuf *t;
133 while (slen) {
134 int len = slen;
135 if (max <= 0 || nr + 1 < max) {
136 const char *end = memchr(str, terminator, slen);
137 if (end)
138 len = end - str + 1;
140 t = xmalloc(sizeof(struct strbuf));
141 strbuf_init(t, len);
142 strbuf_add(t, str, len);
143 ALLOC_GROW(ret, nr + 2, alloc);
144 ret[nr++] = t;
145 str += len;
146 slen -= len;
148 ALLOC_GROW(ret, nr + 1, alloc); /* In case string was empty */
149 ret[nr] = NULL;
150 return ret;
153 void strbuf_list_free(struct strbuf **sbs)
155 struct strbuf **s = sbs;
157 while (*s) {
158 strbuf_release(*s);
159 free(*s++);
161 free(sbs);
164 int strbuf_cmp(const struct strbuf *a, const struct strbuf *b)
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;
173 void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
174 const void *data, size_t dlen)
176 if (unsigned_add_overflows(pos, len))
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");
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);
192 void strbuf_insert(struct strbuf *sb, size_t pos, const void *data, size_t len)
194 strbuf_splice(sb, pos, 0, data, len);
197 void strbuf_remove(struct strbuf *sb, size_t pos, size_t len)
199 strbuf_splice(sb, pos, len, NULL, 0);
202 void strbuf_add(struct strbuf *sb, const void *data, size_t len)
204 strbuf_grow(sb, len);
205 memcpy(sb->buf + sb->len, data, len);
206 strbuf_setlen(sb, sb->len + len);
209 void strbuf_adddup(struct strbuf *sb, size_t pos, size_t len)
211 strbuf_grow(sb, len);
212 memcpy(sb->buf + sb->len, sb->buf + pos, len);
213 strbuf_setlen(sb, sb->len + len);
216 void strbuf_addf(struct strbuf *sb, const char *fmt, ...)
218 va_list ap;
219 va_start(ap, fmt);
220 strbuf_vaddf(sb, fmt, ap);
221 va_end(ap);
224 static void add_lines(struct strbuf *out,
225 const char *prefix1,
226 const char *prefix2,
227 const char *buf, size_t size)
229 while (size) {
230 const char *prefix;
231 const char *next = memchr(buf, '\n', size);
232 next = next ? (next + 1) : (buf + size);
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;
240 strbuf_complete_line(out);
243 void strbuf_add_commented_lines(struct strbuf *out, const char *buf, size_t size)
245 static char prefix1[3];
246 static char prefix2[2];
248 if (prefix1[0] != comment_line_char) {
249 sprintf(prefix1, "%c ", comment_line_char);
250 sprintf(prefix2, "%c", comment_line_char);
252 add_lines(out, prefix1, prefix2, buf, size);
255 void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...)
257 va_list params;
258 struct strbuf buf = STRBUF_INIT;
259 int incomplete_line = sb->len && sb->buf[sb->len - 1] != '\n';
261 va_start(params, fmt);
262 strbuf_vaddf(&buf, fmt, params);
263 va_end(params);
265 strbuf_add_commented_lines(sb, buf.buf, buf.len);
266 if (incomplete_line)
267 sb->buf[--sb->len] = '\0';
269 strbuf_release(&buf);
272 void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap)
274 int len;
275 va_list cp;
277 if (!strbuf_avail(sb))
278 strbuf_grow(sb, 64);
279 va_copy(cp, ap);
280 len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, cp);
281 va_end(cp);
282 if (len < 0)
283 die("BUG: your vsnprintf is broken (returned %d)", len);
284 if (len > strbuf_avail(sb)) {
285 strbuf_grow(sb, len);
286 len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
287 if (len > strbuf_avail(sb))
288 die("BUG: your vsnprintf is broken (insatiable)");
290 strbuf_setlen(sb, sb->len + len);
293 void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn,
294 void *context)
296 for (;;) {
297 const char *percent;
298 size_t consumed;
300 percent = strchrnul(format, '%');
301 strbuf_add(sb, format, percent - format);
302 if (!*percent)
303 break;
304 format = percent + 1;
306 if (*format == '%') {
307 strbuf_addch(sb, '%');
308 format++;
309 continue;
312 consumed = fn(sb, format, context);
313 if (consumed)
314 format += consumed;
315 else
316 strbuf_addch(sb, '%');
320 size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder,
321 void *context)
323 struct strbuf_expand_dict_entry *e = context;
324 size_t len;
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;
333 return 0;
336 void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src)
338 int i, len = src->len;
340 for (i = 0; i < len; i++) {
341 if (src->buf[i] == '%')
342 strbuf_addch(dst, '%');
343 strbuf_addch(dst, src->buf[i]);
347 size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f)
349 size_t res;
350 size_t oldalloc = sb->alloc;
352 strbuf_grow(sb, size);
353 res = fread(sb->buf + sb->len, 1, size, f);
354 if (res > 0)
355 strbuf_setlen(sb, sb->len + res);
356 else if (oldalloc == 0)
357 strbuf_release(sb);
358 return res;
361 ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
363 size_t oldlen = sb->len;
364 size_t oldalloc = sb->alloc;
366 strbuf_grow(sb, hint ? hint : 8192);
367 for (;;) {
368 ssize_t cnt;
370 cnt = xread(fd, sb->buf + sb->len, sb->alloc - sb->len - 1);
371 if (cnt < 0) {
372 if (oldalloc == 0)
373 strbuf_release(sb);
374 else
375 strbuf_setlen(sb, oldlen);
376 return -1;
378 if (!cnt)
379 break;
380 sb->len += cnt;
381 strbuf_grow(sb, 8192);
384 sb->buf[sb->len] = '\0';
385 return sb->len - oldlen;
388 #define STRBUF_MAXLINK (2*PATH_MAX)
390 int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint)
392 size_t oldalloc = sb->alloc;
394 if (hint < 32)
395 hint = 32;
397 while (hint < STRBUF_MAXLINK) {
398 int len;
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;
410 /* .. the buffer was too small - try again */
411 hint *= 2;
413 if (oldalloc == 0)
414 strbuf_release(sb);
415 return -1;
418 int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
420 int ch;
422 if (feof(fp))
423 return EOF;
425 strbuf_reset(sb);
426 while ((ch = fgetc(fp)) != EOF) {
427 strbuf_grow(sb, 1);
428 sb->buf[sb->len++] = ch;
429 if (ch == term)
430 break;
432 if (ch == EOF && sb->len == 0)
433 return EOF;
435 sb->buf[sb->len] = '\0';
436 return 0;
439 int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
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;
448 int strbuf_getwholeline_fd(struct strbuf *sb, int fd, int term)
450 strbuf_reset(sb);
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;
461 return 0;
464 int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)
466 int fd, len;
468 fd = open(path, O_RDONLY);
469 if (fd < 0)
470 return -1;
471 len = strbuf_read(sb, fd, hint);
472 close(fd);
473 if (len < 0)
474 return -1;
476 return len;
479 void strbuf_add_lines(struct strbuf *out, const char *prefix,
480 const char *buf, size_t size)
482 add_lines(out, prefix, NULL, buf, size);
485 void strbuf_addstr_xml_quoted(struct strbuf *buf, const char *s)
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;
507 s++;
511 static int is_rfc3986_reserved(char ch)
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;
519 return 0;
522 static int is_rfc3986_unreserved(char ch)
524 return isalnum(ch) ||
525 ch == '-' || ch == '_' || ch == '.' || ch == '~';
528 static void strbuf_add_urlencode(struct strbuf *sb, const char *s, size_t len,
529 int reserved)
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);
542 void strbuf_addstr_urlencode(struct strbuf *sb, const char *s,
543 int reserved)
545 strbuf_add_urlencode(sb, s, strlen(s), reserved);
548 void strbuf_humanise_bytes(struct strbuf *buf, off_t bytes)
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);
567 int printf_ln(const char *fmt, ...)
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;
579 int fprintf_ln(FILE *fp, const char *fmt, ...)
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;
591 char *xstrdup_tolower(const char *string)
593 char *result;
594 size_t len, i;
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;