Merge branch 'jc/maint-diff-patch-header' into maint
[git/debian.git] / strbuf.c
blob5135d5950d9a5ea3ce8064e5491e53da17645da9
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 int new_buf = !sb->alloc;
67 if (unsigned_add_overflows(extra, 1) ||
68 unsigned_add_overflows(sb->len, extra + 1))
69 die("you want to use way too much memory");
70 if (new_buf)
71 sb->buf = NULL;
72 ALLOC_GROW(sb->buf, sb->len + extra + 1, sb->alloc);
73 if (new_buf)
74 sb->buf[0] = '\0';
77 void strbuf_trim(struct strbuf *sb)
79 char *b = sb->buf;
80 while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1]))
81 sb->len--;
82 while (sb->len > 0 && isspace(*b)) {
83 b++;
84 sb->len--;
86 memmove(sb->buf, b, sb->len);
87 sb->buf[sb->len] = '\0';
89 void strbuf_rtrim(struct strbuf *sb)
91 while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1]))
92 sb->len--;
93 sb->buf[sb->len] = '\0';
96 void strbuf_ltrim(struct strbuf *sb)
98 char *b = sb->buf;
99 while (sb->len > 0 && isspace(*b)) {
100 b++;
101 sb->len--;
103 memmove(sb->buf, b, sb->len);
104 sb->buf[sb->len] = '\0';
107 struct strbuf **strbuf_split_buf(const char *str, size_t slen, int delim, int max)
109 int alloc = 2, pos = 0;
110 const char *n, *p;
111 struct strbuf **ret;
112 struct strbuf *t;
114 ret = xcalloc(alloc, sizeof(struct strbuf *));
115 p = n = str;
116 while (n < str + slen) {
117 int len;
118 if (max <= 0 || pos + 1 < max)
119 n = memchr(n, delim, slen - (n - str));
120 else
121 n = NULL;
122 if (pos + 1 >= alloc) {
123 alloc = alloc * 2;
124 ret = xrealloc(ret, sizeof(struct strbuf *) * alloc);
126 if (!n)
127 n = str + slen - 1;
128 len = n - p + 1;
129 t = xmalloc(sizeof(struct strbuf));
130 strbuf_init(t, len);
131 strbuf_add(t, p, len);
132 ret[pos] = t;
133 ret[++pos] = NULL;
134 p = ++n;
136 return ret;
139 void strbuf_list_free(struct strbuf **sbs)
141 struct strbuf **s = sbs;
143 while (*s) {
144 strbuf_release(*s);
145 free(*s++);
147 free(sbs);
150 int strbuf_cmp(const struct strbuf *a, const struct strbuf *b)
152 int len = a->len < b->len ? a->len: b->len;
153 int cmp = memcmp(a->buf, b->buf, len);
154 if (cmp)
155 return cmp;
156 return a->len < b->len ? -1: a->len != b->len;
159 void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
160 const void *data, size_t dlen)
162 if (unsigned_add_overflows(pos, len))
163 die("you want to use way too much memory");
164 if (pos > sb->len)
165 die("`pos' is too far after the end of the buffer");
166 if (pos + len > sb->len)
167 die("`pos + len' is too far after the end of the buffer");
169 if (dlen >= len)
170 strbuf_grow(sb, dlen - len);
171 memmove(sb->buf + pos + dlen,
172 sb->buf + pos + len,
173 sb->len - pos - len);
174 memcpy(sb->buf + pos, data, dlen);
175 strbuf_setlen(sb, sb->len + dlen - len);
178 void strbuf_insert(struct strbuf *sb, size_t pos, const void *data, size_t len)
180 strbuf_splice(sb, pos, 0, data, len);
183 void strbuf_remove(struct strbuf *sb, size_t pos, size_t len)
185 strbuf_splice(sb, pos, len, NULL, 0);
188 void strbuf_add(struct strbuf *sb, const void *data, size_t len)
190 strbuf_grow(sb, len);
191 memcpy(sb->buf + sb->len, data, len);
192 strbuf_setlen(sb, sb->len + len);
195 void strbuf_adddup(struct strbuf *sb, size_t pos, size_t len)
197 strbuf_grow(sb, len);
198 memcpy(sb->buf + sb->len, sb->buf + pos, len);
199 strbuf_setlen(sb, sb->len + len);
202 void strbuf_addf(struct strbuf *sb, const char *fmt, ...)
204 va_list ap;
205 va_start(ap, fmt);
206 strbuf_vaddf(sb, fmt, ap);
207 va_end(ap);
210 void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap)
212 int len;
213 va_list cp;
215 if (!strbuf_avail(sb))
216 strbuf_grow(sb, 64);
217 va_copy(cp, ap);
218 len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, cp);
219 va_end(cp);
220 if (len < 0)
221 die("BUG: your vsnprintf is broken (returned %d)", len);
222 if (len > strbuf_avail(sb)) {
223 strbuf_grow(sb, len);
224 len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
225 if (len > strbuf_avail(sb))
226 die("BUG: your vsnprintf is broken (insatiable)");
228 strbuf_setlen(sb, sb->len + len);
231 void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn,
232 void *context)
234 for (;;) {
235 const char *percent;
236 size_t consumed;
238 percent = strchrnul(format, '%');
239 strbuf_add(sb, format, percent - format);
240 if (!*percent)
241 break;
242 format = percent + 1;
244 if (*format == '%') {
245 strbuf_addch(sb, '%');
246 format++;
247 continue;
250 consumed = fn(sb, format, context);
251 if (consumed)
252 format += consumed;
253 else
254 strbuf_addch(sb, '%');
258 size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder,
259 void *context)
261 struct strbuf_expand_dict_entry *e = context;
262 size_t len;
264 for (; e->placeholder && (len = strlen(e->placeholder)); e++) {
265 if (!strncmp(placeholder, e->placeholder, len)) {
266 if (e->value)
267 strbuf_addstr(sb, e->value);
268 return len;
271 return 0;
274 void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src)
276 int i, len = src->len;
278 for (i = 0; i < len; i++) {
279 if (src->buf[i] == '%')
280 strbuf_addch(dst, '%');
281 strbuf_addch(dst, src->buf[i]);
285 size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f)
287 size_t res;
288 size_t oldalloc = sb->alloc;
290 strbuf_grow(sb, size);
291 res = fread(sb->buf + sb->len, 1, size, f);
292 if (res > 0)
293 strbuf_setlen(sb, sb->len + res);
294 else if (oldalloc == 0)
295 strbuf_release(sb);
296 return res;
299 ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
301 size_t oldlen = sb->len;
302 size_t oldalloc = sb->alloc;
304 strbuf_grow(sb, hint ? hint : 8192);
305 for (;;) {
306 ssize_t cnt;
308 cnt = xread(fd, sb->buf + sb->len, sb->alloc - sb->len - 1);
309 if (cnt < 0) {
310 if (oldalloc == 0)
311 strbuf_release(sb);
312 else
313 strbuf_setlen(sb, oldlen);
314 return -1;
316 if (!cnt)
317 break;
318 sb->len += cnt;
319 strbuf_grow(sb, 8192);
322 sb->buf[sb->len] = '\0';
323 return sb->len - oldlen;
326 #define STRBUF_MAXLINK (2*PATH_MAX)
328 int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint)
330 size_t oldalloc = sb->alloc;
332 if (hint < 32)
333 hint = 32;
335 while (hint < STRBUF_MAXLINK) {
336 int len;
338 strbuf_grow(sb, hint);
339 len = readlink(path, sb->buf, hint);
340 if (len < 0) {
341 if (errno != ERANGE)
342 break;
343 } else if (len < hint) {
344 strbuf_setlen(sb, len);
345 return 0;
348 /* .. the buffer was too small - try again */
349 hint *= 2;
351 if (oldalloc == 0)
352 strbuf_release(sb);
353 return -1;
356 int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
358 int ch;
360 if (feof(fp))
361 return EOF;
363 strbuf_reset(sb);
364 while ((ch = fgetc(fp)) != EOF) {
365 strbuf_grow(sb, 1);
366 sb->buf[sb->len++] = ch;
367 if (ch == term)
368 break;
370 if (ch == EOF && sb->len == 0)
371 return EOF;
373 sb->buf[sb->len] = '\0';
374 return 0;
377 int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
379 if (strbuf_getwholeline(sb, fp, term))
380 return EOF;
381 if (sb->buf[sb->len-1] == term)
382 strbuf_setlen(sb, sb->len-1);
383 return 0;
386 int strbuf_getwholeline_fd(struct strbuf *sb, int fd, int term)
388 strbuf_reset(sb);
390 while (1) {
391 char ch;
392 ssize_t len = xread(fd, &ch, 1);
393 if (len <= 0)
394 return EOF;
395 strbuf_addch(sb, ch);
396 if (ch == term)
397 break;
399 return 0;
402 int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)
404 int fd, len;
406 fd = open(path, O_RDONLY);
407 if (fd < 0)
408 return -1;
409 len = strbuf_read(sb, fd, hint);
410 close(fd);
411 if (len < 0)
412 return -1;
414 return len;
417 void strbuf_add_lines(struct strbuf *out, const char *prefix,
418 const char *buf, size_t size)
420 while (size) {
421 const char *next = memchr(buf, '\n', size);
422 next = next ? (next + 1) : (buf + size);
423 strbuf_addstr(out, prefix);
424 strbuf_add(out, buf, next - buf);
425 size -= next - buf;
426 buf = next;
428 strbuf_complete_line(out);
431 static int is_rfc3986_reserved(char ch)
433 switch (ch) {
434 case '!': case '*': case '\'': case '(': case ')': case ';':
435 case ':': case '@': case '&': case '=': case '+': case '$':
436 case ',': case '/': case '?': case '#': case '[': case ']':
437 return 1;
439 return 0;
442 static int is_rfc3986_unreserved(char ch)
444 return isalnum(ch) ||
445 ch == '-' || ch == '_' || ch == '.' || ch == '~';
448 void strbuf_add_urlencode(struct strbuf *sb, const char *s, size_t len,
449 int reserved)
451 strbuf_grow(sb, len);
452 while (len--) {
453 char ch = *s++;
454 if (is_rfc3986_unreserved(ch) ||
455 (!reserved && is_rfc3986_reserved(ch)))
456 strbuf_addch(sb, ch);
457 else
458 strbuf_addf(sb, "%%%02x", ch);
462 void strbuf_addstr_urlencode(struct strbuf *sb, const char *s,
463 int reserved)
465 strbuf_add_urlencode(sb, s, strlen(s), reserved);