3 int prefixcmp(const char *str
, const char *prefix
)
5 for (; ; str
++, prefix
++)
8 else if (*str
!= *prefix
)
9 return (unsigned char)*prefix
- (unsigned char)*str
;
13 * Used as the default ->buf value, so that people can always assume
14 * buf is non NULL and ->buf is NUL terminated even for a freshly
17 char strbuf_slopbuf
[1];
19 void strbuf_init(struct strbuf
*sb
, size_t hint
)
21 sb
->alloc
= sb
->len
= 0;
22 sb
->buf
= strbuf_slopbuf
;
24 strbuf_grow(sb
, hint
);
27 void strbuf_release(struct strbuf
*sb
)
35 char *strbuf_detach(struct strbuf
*sb
, size_t *sz
)
37 char *res
= sb
->alloc
? sb
->buf
: NULL
;
44 void strbuf_attach(struct strbuf
*sb
, void *buf
, size_t len
, size_t alloc
)
51 sb
->buf
[sb
->len
] = '\0';
54 void strbuf_grow(struct strbuf
*sb
, size_t extra
)
56 if (sb
->len
+ extra
+ 1 <= sb
->len
)
57 die("you want to use way too much memory");
60 ALLOC_GROW(sb
->buf
, sb
->len
+ extra
+ 1, sb
->alloc
);
63 void strbuf_trim(struct strbuf
*sb
)
66 while (sb
->len
> 0 && isspace((unsigned char)sb
->buf
[sb
->len
- 1]))
68 while (sb
->len
> 0 && isspace(*b
)) {
72 memmove(sb
->buf
, b
, sb
->len
);
73 sb
->buf
[sb
->len
] = '\0';
75 void strbuf_rtrim(struct strbuf
*sb
)
77 while (sb
->len
> 0 && isspace((unsigned char)sb
->buf
[sb
->len
- 1]))
79 sb
->buf
[sb
->len
] = '\0';
82 void strbuf_ltrim(struct strbuf
*sb
)
85 while (sb
->len
> 0 && isspace(*b
)) {
89 memmove(sb
->buf
, b
, sb
->len
);
90 sb
->buf
[sb
->len
] = '\0';
93 void strbuf_tolower(struct strbuf
*sb
)
96 for (i
= 0; i
< sb
->len
; i
++)
97 sb
->buf
[i
] = tolower(sb
->buf
[i
]);
100 struct strbuf
**strbuf_split(const struct strbuf
*sb
, int delim
)
102 int alloc
= 2, pos
= 0;
107 ret
= xcalloc(alloc
, sizeof(struct strbuf
*));
109 while (n
< sb
->buf
+ sb
->len
) {
111 n
= memchr(n
, delim
, sb
->len
- (n
- sb
->buf
));
112 if (pos
+ 1 >= alloc
) {
114 ret
= xrealloc(ret
, sizeof(struct strbuf
*) * alloc
);
117 n
= sb
->buf
+ sb
->len
- 1;
119 t
= xmalloc(sizeof(struct strbuf
));
121 strbuf_add(t
, p
, len
);
129 void strbuf_list_free(struct strbuf
**sbs
)
131 struct strbuf
**s
= sbs
;
140 int strbuf_cmp(const struct strbuf
*a
, const struct strbuf
*b
)
142 int len
= a
->len
< b
->len
? a
->len
: b
->len
;
143 int cmp
= memcmp(a
->buf
, b
->buf
, len
);
146 return a
->len
< b
->len
? -1: a
->len
!= b
->len
;
149 void strbuf_splice(struct strbuf
*sb
, size_t pos
, size_t len
,
150 const void *data
, size_t dlen
)
153 die("you want to use way too much memory");
155 die("`pos' is too far after the end of the buffer");
156 if (pos
+ len
> sb
->len
)
157 die("`pos + len' is too far after the end of the buffer");
160 strbuf_grow(sb
, dlen
- len
);
161 memmove(sb
->buf
+ pos
+ dlen
,
163 sb
->len
- pos
- len
);
164 memcpy(sb
->buf
+ pos
, data
, dlen
);
165 strbuf_setlen(sb
, sb
->len
+ dlen
- len
);
168 void strbuf_insert(struct strbuf
*sb
, size_t pos
, const void *data
, size_t len
)
170 strbuf_splice(sb
, pos
, 0, data
, len
);
173 void strbuf_remove(struct strbuf
*sb
, size_t pos
, size_t len
)
175 strbuf_splice(sb
, pos
, len
, NULL
, 0);
178 void strbuf_add(struct strbuf
*sb
, const void *data
, size_t len
)
180 strbuf_grow(sb
, len
);
181 memcpy(sb
->buf
+ sb
->len
, data
, len
);
182 strbuf_setlen(sb
, sb
->len
+ len
);
185 void strbuf_adddup(struct strbuf
*sb
, size_t pos
, size_t len
)
187 strbuf_grow(sb
, len
);
188 memcpy(sb
->buf
+ sb
->len
, sb
->buf
+ pos
, len
);
189 strbuf_setlen(sb
, sb
->len
+ len
);
192 void strbuf_addf(struct strbuf
*sb
, const char *fmt
, ...)
197 if (!strbuf_avail(sb
))
200 len
= vsnprintf(sb
->buf
+ sb
->len
, sb
->alloc
- sb
->len
, fmt
, ap
);
203 die("your vsnprintf is broken");
204 if (len
> strbuf_avail(sb
)) {
205 strbuf_grow(sb
, len
);
207 len
= vsnprintf(sb
->buf
+ sb
->len
, sb
->alloc
- sb
->len
, fmt
, ap
);
209 if (len
> strbuf_avail(sb
)) {
210 die("this should not happen, your snprintf is broken");
213 strbuf_setlen(sb
, sb
->len
+ len
);
216 void strbuf_expand(struct strbuf
*sb
, const char *format
, expand_fn_t fn
,
223 percent
= strchrnul(format
, '%');
224 strbuf_add(sb
, format
, percent
- format
);
227 format
= percent
+ 1;
229 consumed
= fn(sb
, format
, context
);
233 strbuf_addch(sb
, '%');
237 size_t strbuf_expand_dict_cb(struct strbuf
*sb
, const char *placeholder
,
240 struct strbuf_expand_dict_entry
*e
= context
;
243 for (; e
->placeholder
&& (len
= strlen(e
->placeholder
)); e
++) {
244 if (!strncmp(placeholder
, e
->placeholder
, len
)) {
246 strbuf_addstr(sb
, e
->value
);
253 size_t strbuf_fread(struct strbuf
*sb
, size_t size
, FILE *f
)
256 size_t oldalloc
= sb
->alloc
;
258 strbuf_grow(sb
, size
);
259 res
= fread(sb
->buf
+ sb
->len
, 1, size
, f
);
261 strbuf_setlen(sb
, sb
->len
+ res
);
262 else if (res
< 0 && oldalloc
== 0)
267 ssize_t
strbuf_read(struct strbuf
*sb
, int fd
, size_t hint
)
269 size_t oldlen
= sb
->len
;
270 size_t oldalloc
= sb
->alloc
;
272 strbuf_grow(sb
, hint
? hint
: 8192);
276 cnt
= xread(fd
, sb
->buf
+ sb
->len
, sb
->alloc
- sb
->len
- 1);
281 strbuf_setlen(sb
, oldlen
);
287 strbuf_grow(sb
, 8192);
290 sb
->buf
[sb
->len
] = '\0';
291 return sb
->len
- oldlen
;
294 #define STRBUF_MAXLINK (2*PATH_MAX)
296 int strbuf_readlink(struct strbuf
*sb
, const char *path
, size_t hint
)
298 size_t oldalloc
= sb
->alloc
;
303 while (hint
< STRBUF_MAXLINK
) {
306 strbuf_grow(sb
, hint
);
307 len
= readlink(path
, sb
->buf
, hint
);
311 } else if (len
< hint
) {
312 strbuf_setlen(sb
, len
);
316 /* .. the buffer was too small - try again */
324 int strbuf_getline(struct strbuf
*sb
, FILE *fp
, int term
)
333 while ((ch
= fgetc(fp
)) != EOF
) {
337 sb
->buf
[sb
->len
++] = ch
;
339 if (ch
== EOF
&& sb
->len
== 0)
342 sb
->buf
[sb
->len
] = '\0';
346 int strbuf_read_file(struct strbuf
*sb
, const char *path
, size_t hint
)
350 fd
= open(path
, O_RDONLY
);
353 len
= strbuf_read(sb
, fd
, hint
);