diffcore-rename: replace basename_same() heuristics by levenshtein
[git/dscho.git] / strbuf.c
blobc95dd54c75301761b85828088f88bdfbb4ccad8a
1 #include "cache.h"
2 #include "refs.h"
4 int prefixcmp(const char *str, const char *prefix)
6 for (; ; str++, prefix++)
7 if (!*prefix)
8 return 0;
9 else if (*str != *prefix)
10 return (unsigned char)*prefix - (unsigned char)*str;
13 int suffixcmp(const char *str, const char *suffix)
15 int len = strlen(str), suflen = strlen(suffix);
16 if (len < suflen)
17 return -1;
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 = sb->alloc ? sb->buf : NULL;
48 if (sz)
49 *sz = sb->len;
50 strbuf_init(sb, 0);
51 return res;
54 void strbuf_attach(struct strbuf *sb, void *buf, size_t len, size_t alloc)
56 strbuf_release(sb);
57 sb->buf = buf;
58 sb->len = len;
59 sb->alloc = alloc;
60 strbuf_grow(sb, 0);
61 sb->buf[sb->len] = '\0';
64 void strbuf_grow(struct strbuf *sb, size_t extra)
66 if (sb->len + extra + 1 <= sb->len)
67 die("you want to use way too much memory");
68 if (!sb->alloc)
69 sb->buf = NULL;
70 ALLOC_GROW(sb->buf, sb->len + extra + 1, sb->alloc);
73 void strbuf_trim(struct strbuf *sb)
75 char *b = sb->buf;
76 while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1]))
77 sb->len--;
78 while (sb->len > 0 && isspace(*b)) {
79 b++;
80 sb->len--;
82 memmove(sb->buf, b, sb->len);
83 sb->buf[sb->len] = '\0';
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 struct strbuf **strbuf_split(const struct strbuf *sb, int delim)
105 int alloc = 2, pos = 0;
106 char *n, *p;
107 struct strbuf **ret;
108 struct strbuf *t;
110 ret = xcalloc(alloc, sizeof(struct strbuf *));
111 p = n = sb->buf;
112 while (n < sb->buf + sb->len) {
113 int len;
114 n = memchr(n, delim, sb->len - (n - sb->buf));
115 if (pos + 1 >= alloc) {
116 alloc = alloc * 2;
117 ret = xrealloc(ret, sizeof(struct strbuf *) * alloc);
119 if (!n)
120 n = sb->buf + sb->len - 1;
121 len = n - p + 1;
122 t = xmalloc(sizeof(struct strbuf));
123 strbuf_init(t, len);
124 strbuf_add(t, p, len);
125 ret[pos] = t;
126 ret[++pos] = NULL;
127 p = ++n;
129 return ret;
132 void strbuf_list_free(struct strbuf **sbs)
134 struct strbuf **s = sbs;
136 while (*s) {
137 strbuf_release(*s);
138 free(*s++);
140 free(sbs);
143 int strbuf_cmp(const struct strbuf *a, const struct strbuf *b)
145 int len = a->len < b->len ? a->len: b->len;
146 int cmp = memcmp(a->buf, b->buf, len);
147 if (cmp)
148 return cmp;
149 return a->len < b->len ? -1: a->len != b->len;
152 void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
153 const void *data, size_t dlen)
155 if (pos + len < pos)
156 die("you want to use way too much memory");
157 if (pos > sb->len)
158 die("`pos' is too far after the end of the buffer");
159 if (pos + len > sb->len)
160 die("`pos + len' is too far after the end of the buffer");
162 if (dlen >= len)
163 strbuf_grow(sb, dlen - len);
164 memmove(sb->buf + pos + dlen,
165 sb->buf + pos + len,
166 sb->len - pos - len);
167 memcpy(sb->buf + pos, data, dlen);
168 strbuf_setlen(sb, sb->len + dlen - len);
171 void strbuf_insert(struct strbuf *sb, size_t pos, const void *data, size_t len)
173 strbuf_splice(sb, pos, 0, data, len);
176 void strbuf_remove(struct strbuf *sb, size_t pos, size_t len)
178 strbuf_splice(sb, pos, len, NULL, 0);
181 void strbuf_add(struct strbuf *sb, const void *data, size_t len)
183 strbuf_grow(sb, len);
184 memcpy(sb->buf + sb->len, data, len);
185 strbuf_setlen(sb, sb->len + len);
188 void strbuf_adddup(struct strbuf *sb, size_t pos, size_t len)
190 strbuf_grow(sb, len);
191 memcpy(sb->buf + sb->len, sb->buf + pos, len);
192 strbuf_setlen(sb, sb->len + len);
195 static int number_length(unsigned long number, long base)
197 int length = 1;
198 while (number >= base) {
199 number /= base;
200 length++;
202 return length;
206 * Only supports %u, %i, %d, %l, %o, %x and %X with size indicators
207 * '<number>', '0<number>', '-<number>' and ' <number>',
208 * as well as %c,
209 * and %s with size indicators '<number>', ' <number>' and '.*'.
211 void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap)
213 while (*fmt) {
214 char fill = '\0';
215 int size = -1, max_size = -1, left_align = 0;
216 char *p = (char *)fmt;
218 if (*p != '%') {
219 p = strchrnul(fmt, '%');
220 strbuf_add(sb, fmt, p - fmt);
221 fmt = p;
222 continue;
225 if (*(++p) == '%') {
226 strbuf_addch(sb, *p++);
227 fmt = p;
228 continue;
230 if (*p == '-') {
231 p++;
232 left_align = 1;
234 if (*p == ' ' || *p == '0')
235 fill = *p++;
236 if (isdigit(*p))
237 size = (int)strtol(p, &p, 10);
238 else if (*p == '.' && p[1] == '*') {
239 max_size = va_arg(ap, int);
240 p += 2;
242 else if (*p == '*') {
243 if (!fill)
244 fill = ' ';
245 size = va_arg(ap, int);
246 p++;
249 switch (*p) {
250 case 's': {
251 const char *s = va_arg(ap, const char *);
252 if (fill && !left_align) {
253 int len = size - strlen(s);
254 while (len-- > 0)
255 strbuf_addch(sb, fill);
257 while (*s && max_size--)
258 strbuf_addch(sb, *s++);
259 if (fill && left_align) {
260 int len = size - strlen(s);
261 while (len-- > 0)
262 strbuf_addch(sb, ' ');
264 break;
266 case 'c':
267 strbuf_addch(sb, va_arg(ap, int));
268 break;
269 case 'u':
270 case 'i':
271 case 'l':
272 case 'd':
273 case 'o':
274 case 'x':
275 case 'X': {
276 int base = *p == 'x' || *p == 'X' ? 16 :
277 *p == 'o' ? 8 : 10;
278 int negative = 0, len;
279 unsigned long number, power;
281 if (*p == 'u')
282 number = va_arg(ap, unsigned int);
283 else {
284 long signed_number;
285 if (*p == 'l')
286 signed_number = va_arg(ap, long);
287 else
288 signed_number = va_arg(ap, int);
289 if (signed_number < 0) {
290 negative = 1;
291 number = -signed_number;
292 } else
293 number = signed_number;
296 /* pad */
297 len = number_length(number, base);
298 while (size-- > len + negative)
299 strbuf_addch(sb, fill ? fill : ' ');
300 if (negative)
301 strbuf_addch(sb, '-');
303 /* output number */
304 power = 1;
305 while (len-- > 1)
306 power *= base;
307 while (power) {
308 int digit = number / power;
309 strbuf_addch(sb, digit < 10 ? '0' + digit
310 : *p + 'A' - 'X' + digit - 10);
311 number -= digit * power;
312 power /= base;
315 break;
317 case '\0':
318 break;
319 default:
320 /* unknown / invalid format: copy verbatim */
321 strbuf_insert(sb, sb->len, fmt, p - fmt + 1);
323 fmt = p + (*p != '\0');
327 void strbuf_addf(struct strbuf *sb, const char *fmt, ...)
329 va_list ap;
331 va_start(ap, fmt);
332 strbuf_vaddf(sb, fmt, ap);
333 va_end(ap);
336 void strbuf_initf(struct strbuf *sb, const char *fmt, ...)
338 va_list ap;
340 strbuf_init(sb, strlen(fmt) + 64);
341 va_start(ap, fmt);
342 strbuf_vaddf(sb, fmt, ap);
343 va_end(ap);
346 void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn,
347 void *context)
349 for (;;) {
350 const char *percent;
351 size_t consumed;
353 percent = strchrnul(format, '%');
354 strbuf_add(sb, format, percent - format);
355 if (!*percent)
356 break;
357 format = percent + 1;
359 if (*format == '%') {
360 strbuf_addch(sb, '%');
361 format++;
362 continue;
365 consumed = fn(sb, format, context);
366 if (consumed)
367 format += consumed;
368 else
369 strbuf_addch(sb, '%');
373 size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder,
374 void *context)
376 struct strbuf_expand_dict_entry *e = context;
377 size_t len;
379 for (; e->placeholder && (len = strlen(e->placeholder)); e++) {
380 if (!strncmp(placeholder, e->placeholder, len)) {
381 if (e->value)
382 strbuf_addstr(sb, e->value);
383 return len;
386 return 0;
389 void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src)
391 int i, len = src->len;
393 for (i = 0; i < len; i++) {
394 if (src->buf[i] == '%')
395 strbuf_addch(dst, '%');
396 strbuf_addch(dst, src->buf[i]);
400 size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f)
402 size_t res;
403 size_t oldalloc = sb->alloc;
405 strbuf_grow(sb, size);
406 res = fread(sb->buf + sb->len, 1, size, f);
407 if (res > 0)
408 strbuf_setlen(sb, sb->len + res);
409 else if (oldalloc == 0)
410 strbuf_release(sb);
411 return res;
414 ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
416 size_t oldlen = sb->len;
417 size_t oldalloc = sb->alloc;
419 strbuf_grow(sb, hint ? hint : 8192);
420 for (;;) {
421 ssize_t cnt;
423 cnt = xread(fd, sb->buf + sb->len, sb->alloc - sb->len - 1);
424 if (cnt < 0) {
425 if (oldalloc == 0)
426 strbuf_release(sb);
427 else
428 strbuf_setlen(sb, oldlen);
429 return -1;
431 if (!cnt)
432 break;
433 sb->len += cnt;
434 strbuf_grow(sb, 8192);
437 sb->buf[sb->len] = '\0';
438 return sb->len - oldlen;
441 #define STRBUF_MAXLINK (2*PATH_MAX)
443 int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint)
445 size_t oldalloc = sb->alloc;
447 if (hint < 32)
448 hint = 32;
450 while (hint < STRBUF_MAXLINK) {
451 int len;
453 strbuf_grow(sb, hint);
454 len = readlink(path, sb->buf, hint);
455 if (len < 0) {
456 if (errno != ERANGE)
457 break;
458 } else if (len < hint) {
459 strbuf_setlen(sb, len);
460 return 0;
463 /* .. the buffer was too small - try again */
464 hint *= 2;
466 if (oldalloc == 0)
467 strbuf_release(sb);
468 return -1;
471 int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
473 int ch;
475 strbuf_grow(sb, 0);
476 if (feof(fp))
477 return EOF;
479 strbuf_reset(sb);
480 while ((ch = fgetc(fp)) != EOF) {
481 strbuf_grow(sb, 1);
482 sb->buf[sb->len++] = ch;
483 if (ch == term)
484 break;
486 if (ch == EOF && sb->len == 0)
487 return EOF;
489 sb->buf[sb->len] = '\0';
490 return 0;
493 int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
495 if (strbuf_getwholeline(sb, fp, term))
496 return EOF;
497 if (sb->buf[sb->len-1] == term)
498 strbuf_setlen(sb, sb->len-1);
499 return 0;
502 int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)
504 int fd, len;
506 fd = open(path, O_RDONLY);
507 if (fd < 0)
508 return -1;
509 len = strbuf_read(sb, fd, hint);
510 close(fd);
511 if (len < 0)
512 return -1;
514 return len;
517 int strbuf_branchname(struct strbuf *sb, const char *name)
519 int len = strlen(name);
520 if (interpret_branch_name(name, sb) == len)
521 return 0;
522 strbuf_add(sb, name, len);
523 return len;
526 int strbuf_check_branch_ref(struct strbuf *sb, const char *name)
528 strbuf_branchname(sb, name);
529 strbuf_splice(sb, 0, 0, "refs/heads/", 11);
530 return check_ref_format(sb->buf);