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
)
56 void strbuf_attach(struct strbuf
*sb
, void *buf
, size_t len
, size_t alloc
)
63 sb
->buf
[sb
->len
] = '\0';
66 void strbuf_grow(struct strbuf
*sb
, size_t extra
)
68 int new_buf
= !sb
->alloc
;
69 if (unsigned_add_overflows(extra
, 1) ||
70 unsigned_add_overflows(sb
->len
, extra
+ 1))
71 die("you want to use way too much memory");
74 ALLOC_GROW(sb
->buf
, sb
->len
+ extra
+ 1, sb
->alloc
);
79 void strbuf_trim(struct strbuf
*sb
)
82 while (sb
->len
> 0 && isspace((unsigned char)sb
->buf
[sb
->len
- 1]))
84 while (sb
->len
> 0 && isspace(*b
)) {
88 memmove(sb
->buf
, b
, sb
->len
);
89 sb
->buf
[sb
->len
] = '\0';
91 void strbuf_rtrim(struct strbuf
*sb
)
93 while (sb
->len
> 0 && isspace((unsigned char)sb
->buf
[sb
->len
- 1]))
95 sb
->buf
[sb
->len
] = '\0';
98 void strbuf_ltrim(struct strbuf
*sb
)
101 while (sb
->len
> 0 && isspace(*b
)) {
105 memmove(sb
->buf
, b
, sb
->len
);
106 sb
->buf
[sb
->len
] = '\0';
109 struct strbuf
**strbuf_split_buf(const char *str
, size_t slen
, int delim
, int max
)
111 int alloc
= 2, pos
= 0;
116 ret
= xcalloc(alloc
, sizeof(struct strbuf
*));
118 while (n
< str
+ slen
) {
120 if (max
<= 0 || pos
+ 1 < max
)
121 n
= memchr(n
, delim
, slen
- (n
- str
));
124 if (pos
+ 1 >= alloc
) {
126 ret
= xrealloc(ret
, sizeof(struct strbuf
*) * alloc
);
131 t
= xmalloc(sizeof(struct strbuf
));
133 strbuf_add(t
, p
, len
);
141 void strbuf_list_free(struct strbuf
**sbs
)
143 struct strbuf
**s
= sbs
;
152 int strbuf_cmp(const struct strbuf
*a
, const struct strbuf
*b
)
154 int len
= a
->len
< b
->len
? a
->len
: b
->len
;
155 int cmp
= memcmp(a
->buf
, b
->buf
, len
);
158 return a
->len
< b
->len
? -1: a
->len
!= b
->len
;
161 void strbuf_splice(struct strbuf
*sb
, size_t pos
, size_t len
,
162 const void *data
, size_t dlen
)
164 if (unsigned_add_overflows(pos
, len
))
165 die("you want to use way too much memory");
167 die("`pos' is too far after the end of the buffer");
168 if (pos
+ len
> sb
->len
)
169 die("`pos + len' is too far after the end of the buffer");
172 strbuf_grow(sb
, dlen
- len
);
173 memmove(sb
->buf
+ pos
+ dlen
,
175 sb
->len
- pos
- len
);
176 memcpy(sb
->buf
+ pos
, data
, dlen
);
177 strbuf_setlen(sb
, sb
->len
+ dlen
- len
);
180 void strbuf_insert(struct strbuf
*sb
, size_t pos
, const void *data
, size_t len
)
182 strbuf_splice(sb
, pos
, 0, data
, len
);
185 void strbuf_remove(struct strbuf
*sb
, size_t pos
, size_t len
)
187 strbuf_splice(sb
, pos
, len
, NULL
, 0);
190 void strbuf_add(struct strbuf
*sb
, const void *data
, size_t len
)
192 strbuf_grow(sb
, len
);
193 memcpy(sb
->buf
+ sb
->len
, data
, len
);
194 strbuf_setlen(sb
, sb
->len
+ len
);
197 void strbuf_adddup(struct strbuf
*sb
, size_t pos
, size_t len
)
199 strbuf_grow(sb
, len
);
200 memcpy(sb
->buf
+ sb
->len
, sb
->buf
+ pos
, len
);
201 strbuf_setlen(sb
, sb
->len
+ len
);
204 void strbuf_addf(struct strbuf
*sb
, const char *fmt
, ...)
208 strbuf_vaddf(sb
, fmt
, ap
);
212 void strbuf_vaddf(struct strbuf
*sb
, const char *fmt
, va_list ap
)
217 if (!strbuf_avail(sb
))
220 len
= vsnprintf(sb
->buf
+ sb
->len
, sb
->alloc
- sb
->len
, fmt
, cp
);
223 die("BUG: your vsnprintf is broken (returned %d)", len
);
224 if (len
> strbuf_avail(sb
)) {
225 strbuf_grow(sb
, len
);
226 len
= vsnprintf(sb
->buf
+ sb
->len
, sb
->alloc
- sb
->len
, fmt
, ap
);
227 if (len
> strbuf_avail(sb
))
228 die("BUG: your vsnprintf is broken (insatiable)");
230 strbuf_setlen(sb
, sb
->len
+ len
);
233 void strbuf_expand(struct strbuf
*sb
, const char *format
, expand_fn_t fn
,
240 percent
= strchrnul(format
, '%');
241 strbuf_add(sb
, format
, percent
- format
);
244 format
= percent
+ 1;
246 if (*format
== '%') {
247 strbuf_addch(sb
, '%');
252 consumed
= fn(sb
, format
, context
);
256 strbuf_addch(sb
, '%');
260 size_t strbuf_expand_dict_cb(struct strbuf
*sb
, const char *placeholder
,
263 struct strbuf_expand_dict_entry
*e
= context
;
266 for (; e
->placeholder
&& (len
= strlen(e
->placeholder
)); e
++) {
267 if (!strncmp(placeholder
, e
->placeholder
, len
)) {
269 strbuf_addstr(sb
, e
->value
);
276 void strbuf_addbuf_percentquote(struct strbuf
*dst
, const struct strbuf
*src
)
278 int i
, len
= src
->len
;
280 for (i
= 0; i
< len
; i
++) {
281 if (src
->buf
[i
] == '%')
282 strbuf_addch(dst
, '%');
283 strbuf_addch(dst
, src
->buf
[i
]);
287 size_t strbuf_fread(struct strbuf
*sb
, size_t size
, FILE *f
)
290 size_t oldalloc
= sb
->alloc
;
292 strbuf_grow(sb
, size
);
293 res
= fread(sb
->buf
+ sb
->len
, 1, size
, f
);
295 strbuf_setlen(sb
, sb
->len
+ res
);
296 else if (oldalloc
== 0)
301 ssize_t
strbuf_read(struct strbuf
*sb
, int fd
, size_t hint
)
303 size_t oldlen
= sb
->len
;
304 size_t oldalloc
= sb
->alloc
;
306 strbuf_grow(sb
, hint
? hint
: 8192);
310 cnt
= xread(fd
, sb
->buf
+ sb
->len
, sb
->alloc
- sb
->len
- 1);
315 strbuf_setlen(sb
, oldlen
);
321 strbuf_grow(sb
, 8192);
324 sb
->buf
[sb
->len
] = '\0';
325 return sb
->len
- oldlen
;
328 #define STRBUF_MAXLINK (2*PATH_MAX)
330 int strbuf_readlink(struct strbuf
*sb
, const char *path
, size_t hint
)
332 size_t oldalloc
= sb
->alloc
;
337 while (hint
< STRBUF_MAXLINK
) {
340 strbuf_grow(sb
, hint
);
341 len
= readlink(path
, sb
->buf
, hint
);
345 } else if (len
< hint
) {
346 strbuf_setlen(sb
, len
);
350 /* .. the buffer was too small - try again */
358 int strbuf_getwholeline(struct strbuf
*sb
, FILE *fp
, int term
)
366 while ((ch
= fgetc(fp
)) != EOF
) {
368 sb
->buf
[sb
->len
++] = ch
;
372 if (ch
== EOF
&& sb
->len
== 0)
375 sb
->buf
[sb
->len
] = '\0';
379 int strbuf_getline(struct strbuf
*sb
, FILE *fp
, int term
)
381 if (strbuf_getwholeline(sb
, fp
, term
))
383 if (sb
->buf
[sb
->len
-1] == term
)
384 strbuf_setlen(sb
, sb
->len
-1);
388 int strbuf_getwholeline_fd(struct strbuf
*sb
, int fd
, int term
)
394 ssize_t len
= xread(fd
, &ch
, 1);
397 strbuf_addch(sb
, ch
);
404 int strbuf_read_file(struct strbuf
*sb
, const char *path
, size_t hint
)
408 fd
= open(path
, O_RDONLY
);
411 len
= strbuf_read(sb
, fd
, hint
);
419 void strbuf_add_lines(struct strbuf
*out
, const char *prefix
,
420 const char *buf
, size_t size
)
423 const char *next
= memchr(buf
, '\n', size
);
424 next
= next
? (next
+ 1) : (buf
+ size
);
425 strbuf_addstr(out
, prefix
);
426 strbuf_add(out
, buf
, next
- buf
);
430 strbuf_complete_line(out
);
433 static int is_rfc3986_reserved(char ch
)
436 case '!': case '*': case '\'': case '(': case ')': case ';':
437 case ':': case '@': case '&': case '=': case '+': case '$':
438 case ',': case '/': case '?': case '#': case '[': case ']':
444 static int is_rfc3986_unreserved(char ch
)
446 return isalnum(ch
) ||
447 ch
== '-' || ch
== '_' || ch
== '.' || ch
== '~';
450 static void strbuf_add_urlencode(struct strbuf
*sb
, const char *s
, size_t len
,
453 strbuf_grow(sb
, len
);
456 if (is_rfc3986_unreserved(ch
) ||
457 (!reserved
&& is_rfc3986_reserved(ch
)))
458 strbuf_addch(sb
, ch
);
460 strbuf_addf(sb
, "%%%02x", ch
);
464 void strbuf_addstr_urlencode(struct strbuf
*sb
, const char *s
,
467 strbuf_add_urlencode(sb
, s
, strlen(s
), reserved
);
470 int printf_ln(const char *fmt
, ...)
475 ret
= vprintf(fmt
, ap
);
477 if (ret
< 0 || putchar('\n') == EOF
)
482 int fprintf_ln(FILE *fp
, const char *fmt
, ...)
487 ret
= vfprintf(fp
, fmt
, ap
);
489 if (ret
< 0 || putc('\n', fp
) == EOF
)