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_buf(const char *str
, size_t slen
, int delim
, int max
)
109 int alloc
= 2, pos
= 0;
114 ret
= xcalloc(alloc
, sizeof(struct strbuf
*));
116 while (n
< str
+ slen
) {
118 if (max
<= 0 || pos
+ 1 < max
)
119 n
= memchr(n
, delim
, slen
- (n
- str
));
122 if (pos
+ 1 >= alloc
) {
124 ret
= xrealloc(ret
, sizeof(struct strbuf
*) * alloc
);
129 t
= xmalloc(sizeof(struct strbuf
));
131 strbuf_add(t
, p
, len
);
139 void strbuf_list_free(struct strbuf
**sbs
)
141 struct strbuf
**s
= 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
);
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");
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");
170 strbuf_grow(sb
, dlen
- len
);
171 memmove(sb
->buf
+ pos
+ dlen
,
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
, ...)
206 strbuf_vaddf(sb
, fmt
, ap
);
210 void strbuf_vaddf(struct strbuf
*sb
, const char *fmt
, va_list ap
)
215 if (!strbuf_avail(sb
))
218 len
= vsnprintf(sb
->buf
+ sb
->len
, sb
->alloc
- sb
->len
, fmt
, cp
);
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
,
238 percent
= strchrnul(format
, '%');
239 strbuf_add(sb
, format
, percent
- format
);
242 format
= percent
+ 1;
244 if (*format
== '%') {
245 strbuf_addch(sb
, '%');
250 consumed
= fn(sb
, format
, context
);
254 strbuf_addch(sb
, '%');
258 size_t strbuf_expand_dict_cb(struct strbuf
*sb
, const char *placeholder
,
261 struct strbuf_expand_dict_entry
*e
= context
;
264 for (; e
->placeholder
&& (len
= strlen(e
->placeholder
)); e
++) {
265 if (!strncmp(placeholder
, e
->placeholder
, len
)) {
267 strbuf_addstr(sb
, e
->value
);
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
)
288 size_t oldalloc
= sb
->alloc
;
290 strbuf_grow(sb
, size
);
291 res
= fread(sb
->buf
+ sb
->len
, 1, size
, f
);
293 strbuf_setlen(sb
, sb
->len
+ res
);
294 else if (oldalloc
== 0)
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);
308 cnt
= xread(fd
, sb
->buf
+ sb
->len
, sb
->alloc
- sb
->len
- 1);
313 strbuf_setlen(sb
, oldlen
);
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
;
335 while (hint
< STRBUF_MAXLINK
) {
338 strbuf_grow(sb
, hint
);
339 len
= readlink(path
, sb
->buf
, hint
);
343 } else if (len
< hint
) {
344 strbuf_setlen(sb
, len
);
348 /* .. the buffer was too small - try again */
356 int strbuf_getwholeline(struct strbuf
*sb
, FILE *fp
, int term
)
364 while ((ch
= fgetc(fp
)) != EOF
) {
366 sb
->buf
[sb
->len
++] = ch
;
370 if (ch
== EOF
&& sb
->len
== 0)
373 sb
->buf
[sb
->len
] = '\0';
377 int strbuf_getline(struct strbuf
*sb
, FILE *fp
, int term
)
379 if (strbuf_getwholeline(sb
, fp
, term
))
381 if (sb
->buf
[sb
->len
-1] == term
)
382 strbuf_setlen(sb
, sb
->len
-1);
386 int strbuf_getwholeline_fd(struct strbuf
*sb
, int fd
, int term
)
392 ssize_t len
= xread(fd
, &ch
, 1);
395 strbuf_addch(sb
, ch
);
402 int strbuf_read_file(struct strbuf
*sb
, const char *path
, size_t hint
)
406 fd
= open(path
, O_RDONLY
);
409 len
= strbuf_read(sb
, fd
, hint
);
417 void strbuf_add_lines(struct strbuf
*out
, const char *prefix
,
418 const char *buf
, size_t 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
);
428 strbuf_complete_line(out
);
431 static int is_rfc3986_reserved(char ch
)
434 case '!': case '*': case '\'': case '(': case ')': case ';':
435 case ':': case '@': case '&': case '=': case '+': case '$':
436 case ',': case '/': case '?': case '#': case '[': case ']':
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
,
451 strbuf_grow(sb
, len
);
454 if (is_rfc3986_unreserved(ch
) ||
455 (!reserved
&& is_rfc3986_reserved(ch
)))
456 strbuf_addch(sb
, ch
);
458 strbuf_addf(sb
, "%%%02x", ch
);
462 void strbuf_addstr_urlencode(struct strbuf
*sb
, const char *s
,
465 strbuf_add_urlencode(sb
, s
, strlen(s
), reserved
);
468 void strbuf_addf_ln(struct strbuf
*sb
, const char *fmt
, ...)
472 strbuf_vaddf(sb
, fmt
, ap
);
474 strbuf_addch(sb
, '\n');
477 int printf_ln(const char *fmt
, ...)
482 ret
= vprintf(fmt
, ap
);
484 if (ret
< 0 || putchar('\n') == EOF
)
489 int fprintf_ln(FILE *fp
, const char *fmt
, ...)
494 ret
= vfprintf(fp
, fmt
, ap
);
496 if (ret
< 0 || putc('\n', fp
) == EOF
)