4 int prefixcmp(const char *str, const char *prefix)
6 for (; ; str++, prefix++)
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);
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
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;
34 strbuf_grow(sb, hint);
37 void strbuf_release(struct strbuf *sb)
45 char *strbuf_detach(struct strbuf *sb, size_t *sz)
47 char *res = sb->alloc ? sb->buf : NULL;
54 void strbuf_attach(struct strbuf *sb, void *buf, size_t len, size_t alloc)
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");
70 ALLOC_GROW(sb->buf, sb->len + extra + 1, sb->alloc);
73 void strbuf_trim(struct strbuf *sb)
76 while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1]))
78 while (sb->len > 0 && isspace(*b)) {
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]))
89 sb->buf[sb->len] = '\0';
92 void strbuf_ltrim(struct strbuf *sb)
95 while (sb->len > 0 && isspace(*b)) {
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;
110 ret = xcalloc(alloc, sizeof(struct strbuf *));
112 while (n < sb->buf + sb->len) {
114 n = memchr(n, delim, sb->len - (n - sb->buf));
115 if (pos + 1 >= alloc) {
117 ret = xrealloc(ret, sizeof(struct strbuf *) * alloc);
120 n = sb->buf + sb->len - 1;
122 t = xmalloc(sizeof(struct strbuf));
124 strbuf_add(t, p, len);
132 void strbuf_list_free(struct strbuf **sbs)
134 struct strbuf **s = 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);
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)
156 die("you want to use way too much memory");
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");
163 strbuf_grow(sb, dlen - len);
164 memmove(sb->buf + pos + dlen,
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 void strbuf_addf(struct strbuf *sb, const char *fmt, ...)
200 if (!strbuf_avail(sb))
203 len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
206 die("your vsnprintf is broken");
207 if (len > strbuf_avail(sb)) {
208 strbuf_grow(sb, len);
210 len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
212 if (len > strbuf_avail(sb)) {
213 die("this should not happen, your snprintf is broken");
216 strbuf_setlen(sb, sb->len + len);
219 void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn,
226 percent = strchrnul(format, '%');
227 strbuf_add(sb, format, percent - format);
230 format = percent + 1;
232 if (*format == '%') {
233 strbuf_addch(sb, '%');
238 consumed = fn(sb, format, context);
242 strbuf_addch(sb, '%');
246 size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder,
249 struct strbuf_expand_dict_entry *e = context;
252 for (; e->placeholder && (len = strlen(e->placeholder)); e++) {
253 if (!strncmp(placeholder, e->placeholder, len)) {
255 strbuf_addstr(sb, e->value);
262 void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src)
264 int i, len = src->len;
266 for (i = 0; i < len; i++) {
267 if (src->buf[i] == '%')
268 strbuf_addch(dst, '%');
269 strbuf_addch(dst, src->buf[i]);
273 size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f)
276 size_t oldalloc = sb->alloc;
278 strbuf_grow(sb, size);
279 res = fread(sb->buf + sb->len, 1, size, f);
281 strbuf_setlen(sb, sb->len + res);
282 else if (oldalloc == 0)
287 ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
289 size_t oldlen = sb->len;
290 size_t oldalloc = sb->alloc;
292 strbuf_grow(sb, hint ? hint : 8192);
296 cnt = xread(fd, sb->buf + sb->len, sb->alloc - sb->len - 1);
301 strbuf_setlen(sb, oldlen);
307 strbuf_grow(sb, 8192);
310 sb->buf[sb->len] = '\0';
311 return sb->len - oldlen;
314 #define STRBUF_MAXLINK (2*PATH_MAX)
316 int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint)
318 size_t oldalloc = sb->alloc;
323 while (hint < STRBUF_MAXLINK) {
326 strbuf_grow(sb, hint);
327 len = readlink(path, sb->buf, hint);
331 } else if (len < hint) {
332 strbuf_setlen(sb, len);
336 /* .. the buffer was too small - try again */
344 int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
353 while ((ch = fgetc(fp)) != EOF) {
355 sb->buf[sb->len++] = ch;
359 if (ch == EOF && sb->len == 0)
362 sb->buf[sb->len] = '\0';
366 int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
368 if (strbuf_getwholeline(sb, fp, term))
370 if (sb->buf[sb->len-1] == term)
371 strbuf_setlen(sb, sb->len-1);
375 int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)
379 fd = open(path, O_RDONLY);
382 len = strbuf_read(sb, fd, hint);
390 int strbuf_branchname(struct strbuf *sb, const char *name)
392 int len = strlen(name);
393 if (interpret_branch_name(name, sb) == len)
395 strbuf_add(sb, name, len);
399 int strbuf_check_branch_ref(struct strbuf *sb, const char *name)
401 strbuf_branchname(sb, name);
402 strbuf_splice(sb, 0, 0, "refs/heads/", 11);
403 return check_ref_format(sb->buf);