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
);
39 void strbuf_release(struct strbuf
*sb
)
47 char *strbuf_detach(struct strbuf
*sb
, size_t *sz
)
49 char *res
= sb
->alloc
? sb
->buf
: NULL
;
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 if (unsigned_add_overflows(extra
, 1) ||
69 unsigned_add_overflows(sb
->len
, extra
+ 1))
70 die("you want to use way too much memory");
73 ALLOC_GROW(sb
->buf
, sb
->len
+ extra
+ 1, sb
->alloc
);
76 void strbuf_trim(struct strbuf
*sb
)
79 while (sb
->len
> 0 && isspace((unsigned char)sb
->buf
[sb
->len
- 1]))
81 while (sb
->len
> 0 && isspace(*b
)) {
85 memmove(sb
->buf
, b
, sb
->len
);
86 sb
->buf
[sb
->len
] = '\0';
88 void strbuf_rtrim(struct strbuf
*sb
)
90 while (sb
->len
> 0 && isspace((unsigned char)sb
->buf
[sb
->len
- 1]))
92 sb
->buf
[sb
->len
] = '\0';
95 void strbuf_ltrim(struct strbuf
*sb
)
98 while (sb
->len
> 0 && isspace(*b
)) {
102 memmove(sb
->buf
, b
, sb
->len
);
103 sb
->buf
[sb
->len
] = '\0';
106 struct strbuf
**strbuf_split(const struct strbuf
*sb
, int delim
)
108 int alloc
= 2, pos
= 0;
113 ret
= xcalloc(alloc
, sizeof(struct strbuf
*));
115 while (n
< sb
->buf
+ sb
->len
) {
117 n
= memchr(n
, delim
, sb
->len
- (n
- sb
->buf
));
118 if (pos
+ 1 >= alloc
) {
120 ret
= xrealloc(ret
, sizeof(struct strbuf
*) * alloc
);
123 n
= sb
->buf
+ sb
->len
- 1;
125 t
= xmalloc(sizeof(struct strbuf
));
127 strbuf_add(t
, p
, len
);
135 void strbuf_list_free(struct strbuf
**sbs
)
137 struct strbuf
**s
= sbs
;
146 int strbuf_cmp(const struct strbuf
*a
, const struct strbuf
*b
)
148 int len
= a
->len
< b
->len
? a
->len
: b
->len
;
149 int cmp
= memcmp(a
->buf
, b
->buf
, len
);
152 return a
->len
< b
->len
? -1: a
->len
!= b
->len
;
155 void strbuf_splice(struct strbuf
*sb
, size_t pos
, size_t len
,
156 const void *data
, size_t dlen
)
158 if (unsigned_add_overflows(pos
, len
))
159 die("you want to use way too much memory");
161 die("`pos' is too far after the end of the buffer");
162 if (pos
+ len
> sb
->len
)
163 die("`pos + len' is too far after the end of the buffer");
166 strbuf_grow(sb
, dlen
- len
);
167 memmove(sb
->buf
+ pos
+ dlen
,
169 sb
->len
- pos
- len
);
170 memcpy(sb
->buf
+ pos
, data
, dlen
);
171 strbuf_setlen(sb
, sb
->len
+ dlen
- len
);
174 void strbuf_insert(struct strbuf
*sb
, size_t pos
, const void *data
, size_t len
)
176 strbuf_splice(sb
, pos
, 0, data
, len
);
179 void strbuf_remove(struct strbuf
*sb
, size_t pos
, size_t len
)
181 strbuf_splice(sb
, pos
, len
, NULL
, 0);
184 void strbuf_add(struct strbuf
*sb
, const void *data
, size_t len
)
186 strbuf_grow(sb
, len
);
187 memcpy(sb
->buf
+ sb
->len
, data
, len
);
188 strbuf_setlen(sb
, sb
->len
+ len
);
191 void strbuf_adddup(struct strbuf
*sb
, size_t pos
, size_t len
)
193 strbuf_grow(sb
, len
);
194 memcpy(sb
->buf
+ sb
->len
, sb
->buf
+ pos
, len
);
195 strbuf_setlen(sb
, sb
->len
+ len
);
198 void strbuf_addf(struct strbuf
*sb
, const char *fmt
, ...)
202 strbuf_vaddf(sb
, fmt
, ap
);
206 void strbuf_vaddf(struct strbuf
*sb
, const char *fmt
, va_list ap
)
211 if (!strbuf_avail(sb
))
214 len
= vsnprintf(sb
->buf
+ sb
->len
, sb
->alloc
- sb
->len
, fmt
, cp
);
217 die("BUG: your vsnprintf is broken (returned %d)", len
);
218 if (len
> strbuf_avail(sb
)) {
219 strbuf_grow(sb
, len
);
220 len
= vsnprintf(sb
->buf
+ sb
->len
, sb
->alloc
- sb
->len
, fmt
, ap
);
221 if (len
> strbuf_avail(sb
))
222 die("BUG: your vsnprintf is broken (insatiable)");
224 strbuf_setlen(sb
, sb
->len
+ len
);
227 void strbuf_expand(struct strbuf
*sb
, const char *format
, expand_fn_t fn
,
234 percent
= strchrnul(format
, '%');
235 strbuf_add(sb
, format
, percent
- format
);
238 format
= percent
+ 1;
240 if (*format
== '%') {
241 strbuf_addch(sb
, '%');
246 consumed
= fn(sb
, format
, context
);
250 strbuf_addch(sb
, '%');
254 size_t strbuf_expand_dict_cb(struct strbuf
*sb
, const char *placeholder
,
257 struct strbuf_expand_dict_entry
*e
= context
;
260 for (; e
->placeholder
&& (len
= strlen(e
->placeholder
)); e
++) {
261 if (!strncmp(placeholder
, e
->placeholder
, len
)) {
263 strbuf_addstr(sb
, e
->value
);
270 void strbuf_addbuf_percentquote(struct strbuf
*dst
, const struct strbuf
*src
)
272 int i
, len
= src
->len
;
274 for (i
= 0; i
< len
; i
++) {
275 if (src
->buf
[i
] == '%')
276 strbuf_addch(dst
, '%');
277 strbuf_addch(dst
, src
->buf
[i
]);
281 size_t strbuf_fread(struct strbuf
*sb
, size_t size
, FILE *f
)
284 size_t oldalloc
= sb
->alloc
;
286 strbuf_grow(sb
, size
);
287 res
= fread(sb
->buf
+ sb
->len
, 1, size
, f
);
289 strbuf_setlen(sb
, sb
->len
+ res
);
290 else if (oldalloc
== 0)
295 ssize_t
strbuf_read(struct strbuf
*sb
, int fd
, size_t hint
)
297 size_t oldlen
= sb
->len
;
298 size_t oldalloc
= sb
->alloc
;
300 strbuf_grow(sb
, hint
? hint
: 8192);
304 cnt
= xread(fd
, sb
->buf
+ sb
->len
, sb
->alloc
- sb
->len
- 1);
309 strbuf_setlen(sb
, oldlen
);
315 strbuf_grow(sb
, 8192);
318 sb
->buf
[sb
->len
] = '\0';
319 return sb
->len
- oldlen
;
322 #define STRBUF_MAXLINK (2*PATH_MAX)
324 int strbuf_readlink(struct strbuf
*sb
, const char *path
, size_t hint
)
326 size_t oldalloc
= sb
->alloc
;
331 while (hint
< STRBUF_MAXLINK
) {
334 strbuf_grow(sb
, hint
);
335 len
= readlink(path
, sb
->buf
, hint
);
339 } else if (len
< hint
) {
340 strbuf_setlen(sb
, len
);
344 /* .. the buffer was too small - try again */
352 int strbuf_getwholeline(struct strbuf
*sb
, FILE *fp
, int term
)
361 while ((ch
= fgetc(fp
)) != EOF
) {
363 sb
->buf
[sb
->len
++] = ch
;
367 if (ch
== EOF
&& sb
->len
== 0)
370 sb
->buf
[sb
->len
] = '\0';
374 int strbuf_getline(struct strbuf
*sb
, FILE *fp
, int term
)
376 if (strbuf_getwholeline(sb
, fp
, term
))
378 if (sb
->buf
[sb
->len
-1] == term
)
379 strbuf_setlen(sb
, sb
->len
-1);
383 int strbuf_read_file(struct strbuf
*sb
, const char *path
, size_t hint
)
387 fd
= open(path
, O_RDONLY
);
390 len
= strbuf_read(sb
, fd
, hint
);