4 int starts_with(const char *str
, const char *prefix
)
6 for (; ; str
++, prefix
++)
9 else if (*str
!= *prefix
)
13 int prefixcmp(const char *str
, const char *prefix
)
15 for (; ; str
++, prefix
++)
18 else if (*str
!= *prefix
)
19 return (unsigned char)*prefix
- (unsigned char)*str
;
22 int ends_with(const char *str
, const char *suffix
)
24 int len
= strlen(str
), suflen
= strlen(suffix
);
28 return !strcmp(str
+ len
- suflen
, suffix
);
31 int suffixcmp(const char *str
, const char *suffix
)
33 int len
= strlen(str
), suflen
= strlen(suffix
);
37 return strcmp(str
+ len
- suflen
, suffix
);
41 * Used as the default ->buf value, so that people can always assume
42 * buf is non NULL and ->buf is NUL terminated even for a freshly
45 char strbuf_slopbuf
[1];
47 void strbuf_init(struct strbuf
*sb
, size_t hint
)
49 sb
->alloc
= sb
->len
= 0;
50 sb
->buf
= strbuf_slopbuf
;
52 strbuf_grow(sb
, hint
);
55 void strbuf_release(struct strbuf
*sb
)
63 char *strbuf_detach(struct strbuf
*sb
, size_t *sz
)
74 void strbuf_attach(struct strbuf
*sb
, void *buf
, size_t len
, size_t alloc
)
81 sb
->buf
[sb
->len
] = '\0';
84 void strbuf_grow(struct strbuf
*sb
, size_t extra
)
86 int new_buf
= !sb
->alloc
;
87 if (unsigned_add_overflows(extra
, 1) ||
88 unsigned_add_overflows(sb
->len
, extra
+ 1))
89 die("you want to use way too much memory");
92 ALLOC_GROW(sb
->buf
, sb
->len
+ extra
+ 1, sb
->alloc
);
97 void strbuf_trim(struct strbuf
*sb
)
100 while (sb
->len
> 0 && isspace((unsigned char)sb
->buf
[sb
->len
- 1]))
102 while (sb
->len
> 0 && isspace(*b
)) {
106 memmove(sb
->buf
, b
, sb
->len
);
107 sb
->buf
[sb
->len
] = '\0';
109 void strbuf_rtrim(struct strbuf
*sb
)
111 while (sb
->len
> 0 && isspace((unsigned char)sb
->buf
[sb
->len
- 1]))
113 sb
->buf
[sb
->len
] = '\0';
116 void strbuf_ltrim(struct strbuf
*sb
)
119 while (sb
->len
> 0 && isspace(*b
)) {
123 memmove(sb
->buf
, b
, sb
->len
);
124 sb
->buf
[sb
->len
] = '\0';
127 struct strbuf
**strbuf_split_buf(const char *str
, size_t slen
,
128 int terminator
, int max
)
130 struct strbuf
**ret
= NULL
;
131 size_t nr
= 0, alloc
= 0;
136 if (max
<= 0 || nr
+ 1 < max
) {
137 const char *end
= memchr(str
, terminator
, slen
);
141 t
= xmalloc(sizeof(struct strbuf
));
143 strbuf_add(t
, str
, len
);
144 ALLOC_GROW(ret
, nr
+ 2, alloc
);
149 ALLOC_GROW(ret
, nr
+ 1, alloc
); /* In case string was empty */
154 void strbuf_list_free(struct strbuf
**sbs
)
156 struct strbuf
**s
= sbs
;
165 int strbuf_cmp(const struct strbuf
*a
, const struct strbuf
*b
)
167 int len
= a
->len
< b
->len
? a
->len
: b
->len
;
168 int cmp
= memcmp(a
->buf
, b
->buf
, len
);
171 return a
->len
< b
->len
? -1: a
->len
!= b
->len
;
174 void strbuf_splice(struct strbuf
*sb
, size_t pos
, size_t len
,
175 const void *data
, size_t dlen
)
177 if (unsigned_add_overflows(pos
, len
))
178 die("you want to use way too much memory");
180 die("`pos' is too far after the end of the buffer");
181 if (pos
+ len
> sb
->len
)
182 die("`pos + len' is too far after the end of the buffer");
185 strbuf_grow(sb
, dlen
- len
);
186 memmove(sb
->buf
+ pos
+ dlen
,
188 sb
->len
- pos
- len
);
189 memcpy(sb
->buf
+ pos
, data
, dlen
);
190 strbuf_setlen(sb
, sb
->len
+ dlen
- len
);
193 void strbuf_insert(struct strbuf
*sb
, size_t pos
, const void *data
, size_t len
)
195 strbuf_splice(sb
, pos
, 0, data
, len
);
198 void strbuf_remove(struct strbuf
*sb
, size_t pos
, size_t len
)
200 strbuf_splice(sb
, pos
, len
, NULL
, 0);
203 void strbuf_add(struct strbuf
*sb
, const void *data
, size_t len
)
205 strbuf_grow(sb
, len
);
206 memcpy(sb
->buf
+ sb
->len
, data
, len
);
207 strbuf_setlen(sb
, sb
->len
+ len
);
210 void strbuf_adddup(struct strbuf
*sb
, size_t pos
, size_t len
)
212 strbuf_grow(sb
, len
);
213 memcpy(sb
->buf
+ sb
->len
, sb
->buf
+ pos
, len
);
214 strbuf_setlen(sb
, sb
->len
+ len
);
217 void strbuf_addf(struct strbuf
*sb
, const char *fmt
, ...)
221 strbuf_vaddf(sb
, fmt
, ap
);
225 static void add_lines(struct strbuf
*out
,
228 const char *buf
, size_t size
)
232 const char *next
= memchr(buf
, '\n', size
);
233 next
= next
? (next
+ 1) : (buf
+ size
);
235 prefix
= (prefix2
&& buf
[0] == '\n') ? prefix2
: prefix1
;
236 strbuf_addstr(out
, prefix
);
237 strbuf_add(out
, buf
, next
- buf
);
241 strbuf_complete_line(out
);
244 void strbuf_add_commented_lines(struct strbuf
*out
, const char *buf
, size_t size
)
246 static char prefix1
[3];
247 static char prefix2
[2];
249 if (prefix1
[0] != comment_line_char
) {
250 sprintf(prefix1
, "%c ", comment_line_char
);
251 sprintf(prefix2
, "%c", comment_line_char
);
253 add_lines(out
, prefix1
, prefix2
, buf
, size
);
256 void strbuf_commented_addf(struct strbuf
*sb
, const char *fmt
, ...)
259 struct strbuf buf
= STRBUF_INIT
;
260 int incomplete_line
= sb
->len
&& sb
->buf
[sb
->len
- 1] != '\n';
262 va_start(params
, fmt
);
263 strbuf_vaddf(&buf
, fmt
, params
);
266 strbuf_add_commented_lines(sb
, buf
.buf
, buf
.len
);
268 sb
->buf
[--sb
->len
] = '\0';
270 strbuf_release(&buf
);
273 void strbuf_vaddf(struct strbuf
*sb
, const char *fmt
, va_list ap
)
278 if (!strbuf_avail(sb
))
281 len
= vsnprintf(sb
->buf
+ sb
->len
, sb
->alloc
- sb
->len
, fmt
, cp
);
284 die("BUG: your vsnprintf is broken (returned %d)", len
);
285 if (len
> strbuf_avail(sb
)) {
286 strbuf_grow(sb
, len
);
287 len
= vsnprintf(sb
->buf
+ sb
->len
, sb
->alloc
- sb
->len
, fmt
, ap
);
288 if (len
> strbuf_avail(sb
))
289 die("BUG: your vsnprintf is broken (insatiable)");
291 strbuf_setlen(sb
, sb
->len
+ len
);
294 void strbuf_expand(struct strbuf
*sb
, const char *format
, expand_fn_t fn
,
301 percent
= strchrnul(format
, '%');
302 strbuf_add(sb
, format
, percent
- format
);
305 format
= percent
+ 1;
307 if (*format
== '%') {
308 strbuf_addch(sb
, '%');
313 consumed
= fn(sb
, format
, context
);
317 strbuf_addch(sb
, '%');
321 size_t strbuf_expand_dict_cb(struct strbuf
*sb
, const char *placeholder
,
324 struct strbuf_expand_dict_entry
*e
= context
;
327 for (; e
->placeholder
&& (len
= strlen(e
->placeholder
)); e
++) {
328 if (!strncmp(placeholder
, e
->placeholder
, len
)) {
330 strbuf_addstr(sb
, e
->value
);
337 void strbuf_addbuf_percentquote(struct strbuf
*dst
, const struct strbuf
*src
)
339 int i
, len
= src
->len
;
341 for (i
= 0; i
< len
; i
++) {
342 if (src
->buf
[i
] == '%')
343 strbuf_addch(dst
, '%');
344 strbuf_addch(dst
, src
->buf
[i
]);
348 size_t strbuf_fread(struct strbuf
*sb
, size_t size
, FILE *f
)
351 size_t oldalloc
= sb
->alloc
;
353 strbuf_grow(sb
, size
);
354 res
= fread(sb
->buf
+ sb
->len
, 1, size
, f
);
356 strbuf_setlen(sb
, sb
->len
+ res
);
357 else if (oldalloc
== 0)
362 ssize_t
strbuf_read(struct strbuf
*sb
, int fd
, size_t hint
)
364 size_t oldlen
= sb
->len
;
365 size_t oldalloc
= sb
->alloc
;
367 strbuf_grow(sb
, hint
? hint
: 8192);
371 cnt
= xread(fd
, sb
->buf
+ sb
->len
, sb
->alloc
- sb
->len
- 1);
376 strbuf_setlen(sb
, oldlen
);
382 strbuf_grow(sb
, 8192);
385 sb
->buf
[sb
->len
] = '\0';
386 return sb
->len
- oldlen
;
389 #define STRBUF_MAXLINK (2*PATH_MAX)
391 int strbuf_readlink(struct strbuf
*sb
, const char *path
, size_t hint
)
393 size_t oldalloc
= sb
->alloc
;
398 while (hint
< STRBUF_MAXLINK
) {
401 strbuf_grow(sb
, hint
);
402 len
= readlink(path
, sb
->buf
, hint
);
406 } else if (len
< hint
) {
407 strbuf_setlen(sb
, len
);
411 /* .. the buffer was too small - try again */
419 int strbuf_getwholeline(struct strbuf
*sb
, FILE *fp
, int term
)
427 while ((ch
= fgetc(fp
)) != EOF
) {
429 sb
->buf
[sb
->len
++] = ch
;
433 if (ch
== EOF
&& sb
->len
== 0)
436 sb
->buf
[sb
->len
] = '\0';
440 int strbuf_getline(struct strbuf
*sb
, FILE *fp
, int term
)
442 if (strbuf_getwholeline(sb
, fp
, term
))
444 if (sb
->buf
[sb
->len
-1] == term
)
445 strbuf_setlen(sb
, sb
->len
-1);
449 int strbuf_getwholeline_fd(struct strbuf
*sb
, int fd
, int term
)
455 ssize_t len
= xread(fd
, &ch
, 1);
458 strbuf_addch(sb
, ch
);
465 int strbuf_read_file(struct strbuf
*sb
, const char *path
, size_t hint
)
469 fd
= open(path
, O_RDONLY
);
472 len
= strbuf_read(sb
, fd
, hint
);
480 void strbuf_add_lines(struct strbuf
*out
, const char *prefix
,
481 const char *buf
, size_t size
)
483 add_lines(out
, prefix
, NULL
, buf
, size
);
486 void strbuf_addstr_xml_quoted(struct strbuf
*buf
, const char *s
)
489 size_t len
= strcspn(s
, "\"<>&");
490 strbuf_add(buf
, s
, len
);
494 strbuf_addstr(buf
, """);
497 strbuf_addstr(buf
, "<");
500 strbuf_addstr(buf
, ">");
503 strbuf_addstr(buf
, "&");
512 static int is_rfc3986_reserved(char ch
)
515 case '!': case '*': case '\'': case '(': case ')': case ';':
516 case ':': case '@': case '&': case '=': case '+': case '$':
517 case ',': case '/': case '?': case '#': case '[': case ']':
523 static int is_rfc3986_unreserved(char ch
)
525 return isalnum(ch
) ||
526 ch
== '-' || ch
== '_' || ch
== '.' || ch
== '~';
529 static void strbuf_add_urlencode(struct strbuf
*sb
, const char *s
, size_t len
,
532 strbuf_grow(sb
, len
);
535 if (is_rfc3986_unreserved(ch
) ||
536 (!reserved
&& is_rfc3986_reserved(ch
)))
537 strbuf_addch(sb
, ch
);
539 strbuf_addf(sb
, "%%%02x", ch
);
543 void strbuf_addstr_urlencode(struct strbuf
*sb
, const char *s
,
546 strbuf_add_urlencode(sb
, s
, strlen(s
), reserved
);
549 void strbuf_humanise_bytes(struct strbuf
*buf
, off_t bytes
)
551 if (bytes
> 1 << 30) {
552 strbuf_addf(buf
, "%u.%2.2u GiB",
554 (int)(bytes
& ((1 << 30) - 1)) / 10737419);
555 } else if (bytes
> 1 << 20) {
556 int x
= bytes
+ 5243; /* for rounding */
557 strbuf_addf(buf
, "%u.%2.2u MiB",
558 x
>> 20, ((x
& ((1 << 20) - 1)) * 100) >> 20);
559 } else if (bytes
> 1 << 10) {
560 int x
= bytes
+ 5; /* for rounding */
561 strbuf_addf(buf
, "%u.%2.2u KiB",
562 x
>> 10, ((x
& ((1 << 10) - 1)) * 100) >> 10);
564 strbuf_addf(buf
, "%u bytes", (int)bytes
);
568 int printf_ln(const char *fmt
, ...)
573 ret
= vprintf(fmt
, ap
);
575 if (ret
< 0 || putchar('\n') == EOF
)
580 int fprintf_ln(FILE *fp
, const char *fmt
, ...)
585 ret
= vfprintf(fp
, fmt
, ap
);
587 if (ret
< 0 || putc('\n', fp
) == EOF
)