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 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");
72 ALLOC_GROW(sb->buf, sb->len + extra + 1, sb->alloc);
77 void strbuf_trim(struct strbuf *sb)
80 while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1]))
82 while (sb->len > 0 && isspace(*b)) {
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]))
93 sb->buf[sb->len] = '\0';
96 void strbuf_ltrim(struct strbuf *sb)
99 while (sb->len > 0 && isspace(*b)) {
103 memmove(sb->buf, b, sb->len);
104 sb->buf[sb->len] = '\0';
107 struct strbuf **strbuf_split(const struct strbuf *sb, int delim)
109 int alloc = 2, pos = 0;
114 ret = xcalloc(alloc, sizeof(struct strbuf *));
116 while (n < sb->buf + sb->len) {
118 n = memchr(n, delim, sb->len - (n - sb->buf));
119 if (pos + 1 >= alloc) {
121 ret = xrealloc(ret, sizeof(struct strbuf *) * alloc);
124 n = sb->buf + sb->len - 1;
126 t = xmalloc(sizeof(struct strbuf));
128 strbuf_add(t, p, len);
136 void strbuf_list_free(struct strbuf **sbs)
138 struct strbuf **s = sbs;
147 int strbuf_cmp(const struct strbuf *a, const struct strbuf *b)
149 int len = a->len < b->len ? a->len: b->len;
150 int cmp = memcmp(a->buf, b->buf, len);
153 return a->len < b->len ? -1: a->len != b->len;
156 void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
157 const void *data, size_t dlen)
159 if (unsigned_add_overflows(pos, len))
160 die("you want to use way too much memory");
162 die("`pos' is too far after the end of the buffer");
163 if (pos + len > sb->len)
164 die("`pos + len' is too far after the end of the buffer");
167 strbuf_grow(sb, dlen - len);
168 memmove(sb->buf + pos + dlen,
170 sb->len - pos - len);
171 memcpy(sb->buf + pos, data, dlen);
172 strbuf_setlen(sb, sb->len + dlen - len);
175 void strbuf_insert(struct strbuf *sb, size_t pos, const void *data, size_t len)
177 strbuf_splice(sb, pos, 0, data, len);
180 void strbuf_remove(struct strbuf *sb, size_t pos, size_t len)
182 strbuf_splice(sb, pos, len, NULL, 0);
185 void strbuf_add(struct strbuf *sb, const void *data, size_t len)
187 strbuf_grow(sb, len);
188 memcpy(sb->buf + sb->len, data, len);
189 strbuf_setlen(sb, sb->len + len);
192 void strbuf_adddup(struct strbuf *sb, size_t pos, size_t len)
194 strbuf_grow(sb, len);
195 memcpy(sb->buf + sb->len, sb->buf + pos, len);
196 strbuf_setlen(sb, sb->len + len);
199 void strbuf_addf(struct strbuf *sb, const char *fmt, ...)
203 strbuf_vaddf(sb, fmt, ap);
207 void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap)
212 if (!strbuf_avail(sb))
215 len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, cp);
218 die("BUG: your vsnprintf is broken (returned %d)", len);
219 if (len > strbuf_avail(sb)) {
220 strbuf_grow(sb, len);
221 len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
222 if (len > strbuf_avail(sb))
223 die("BUG: your vsnprintf is broken (insatiable)");
225 strbuf_setlen(sb, sb->len + len);
228 void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn,
235 percent = strchrnul(format, '%');
236 strbuf_add(sb, format, percent - format);
239 format = percent + 1;
241 if (*format == '%') {
242 strbuf_addch(sb, '%');
247 consumed = fn(sb, format, context);
251 strbuf_addch(sb, '%');
255 size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder,
258 struct strbuf_expand_dict_entry *e = context;
261 for (; e->placeholder && (len = strlen(e->placeholder)); e++) {
262 if (!strncmp(placeholder, e->placeholder, len)) {
264 strbuf_addstr(sb, e->value);
271 void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src)
273 int i, len = src->len;
275 for (i = 0; i < len; i++) {
276 if (src->buf[i] == '%')
277 strbuf_addch(dst, '%');
278 strbuf_addch(dst, src->buf[i]);
282 size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f)
285 size_t oldalloc = sb->alloc;
287 strbuf_grow(sb, size);
288 res = fread(sb->buf + sb->len, 1, size, f);
290 strbuf_setlen(sb, sb->len + res);
291 else if (oldalloc == 0)
296 ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
298 size_t oldlen = sb->len;
299 size_t oldalloc = sb->alloc;
301 strbuf_grow(sb, hint ? hint : 8192);
305 cnt = xread(fd, sb->buf + sb->len, sb->alloc - sb->len - 1);
310 strbuf_setlen(sb, oldlen);
316 strbuf_grow(sb, 8192);
319 sb->buf[sb->len] = '\0';
320 return sb->len - oldlen;
323 #define STRBUF_MAXLINK (2*PATH_MAX)
325 int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint)
327 size_t oldalloc = sb->alloc;
332 while (hint < STRBUF_MAXLINK) {
335 strbuf_grow(sb, hint);
336 len = readlink(path, sb->buf, hint);
340 } else if (len < hint) {
341 strbuf_setlen(sb, len);
345 /* .. the buffer was too small - try again */
353 int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
362 while ((ch = fgetc(fp)) != EOF) {
364 sb->buf[sb->len++] = ch;
368 if (ch == EOF && sb->len == 0)
371 sb->buf[sb->len] = '\0';
375 int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
377 if (strbuf_getwholeline(sb, fp, term))
379 if (sb->buf[sb->len-1] == term)
380 strbuf_setlen(sb, sb->len-1);
384 int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)
388 fd = open(path, O_RDONLY);
391 len = strbuf_read(sb, fd, hint);