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 (sb
->len
+ extra
+ 1 <= sb
->len
)
69 die("you want to use way too much memory");
72 ALLOC_GROW(sb
->buf
, sb
->len
+ extra
+ 1, sb
->alloc
);
75 void strbuf_trim(struct strbuf
*sb
)
78 while (sb
->len
> 0 && isspace((unsigned char)sb
->buf
[sb
->len
- 1]))
80 while (sb
->len
> 0 && isspace(*b
)) {
84 memmove(sb
->buf
, b
, sb
->len
);
85 sb
->buf
[sb
->len
] = '\0';
87 void strbuf_rtrim(struct strbuf
*sb
)
89 while (sb
->len
> 0 && isspace((unsigned char)sb
->buf
[sb
->len
- 1]))
91 sb
->buf
[sb
->len
] = '\0';
94 void strbuf_ltrim(struct strbuf
*sb
)
97 while (sb
->len
> 0 && isspace(*b
)) {
101 memmove(sb
->buf
, b
, sb
->len
);
102 sb
->buf
[sb
->len
] = '\0';
105 struct strbuf
**strbuf_split(const struct strbuf
*sb
, int delim
)
107 int alloc
= 2, pos
= 0;
112 ret
= xcalloc(alloc
, sizeof(struct strbuf
*));
114 while (n
< sb
->buf
+ sb
->len
) {
116 n
= memchr(n
, delim
, sb
->len
- (n
- sb
->buf
));
117 if (pos
+ 1 >= alloc
) {
119 ret
= xrealloc(ret
, sizeof(struct strbuf
*) * alloc
);
122 n
= sb
->buf
+ sb
->len
- 1;
124 t
= xmalloc(sizeof(struct strbuf
));
126 strbuf_add(t
, p
, len
);
134 void strbuf_list_free(struct strbuf
**sbs
)
136 struct strbuf
**s
= sbs
;
145 int strbuf_cmp(const struct strbuf
*a
, const struct strbuf
*b
)
147 int len
= a
->len
< b
->len
? a
->len
: b
->len
;
148 int cmp
= memcmp(a
->buf
, b
->buf
, len
);
151 return a
->len
< b
->len
? -1: a
->len
!= b
->len
;
154 void strbuf_splice(struct strbuf
*sb
, size_t pos
, size_t len
,
155 const void *data
, size_t dlen
)
158 die("you want to use way too much memory");
160 die("`pos' is too far after the end of the buffer");
161 if (pos
+ len
> sb
->len
)
162 die("`pos + len' is too far after the end of the buffer");
165 strbuf_grow(sb
, dlen
- len
);
166 memmove(sb
->buf
+ pos
+ dlen
,
168 sb
->len
- pos
- len
);
169 memcpy(sb
->buf
+ pos
, data
, dlen
);
170 strbuf_setlen(sb
, sb
->len
+ dlen
- len
);
173 void strbuf_insert(struct strbuf
*sb
, size_t pos
, const void *data
, size_t len
)
175 strbuf_splice(sb
, pos
, 0, data
, len
);
178 void strbuf_remove(struct strbuf
*sb
, size_t pos
, size_t len
)
180 strbuf_splice(sb
, pos
, len
, NULL
, 0);
183 void strbuf_add(struct strbuf
*sb
, const void *data
, size_t len
)
185 strbuf_grow(sb
, len
);
186 memcpy(sb
->buf
+ sb
->len
, data
, len
);
187 strbuf_setlen(sb
, sb
->len
+ len
);
190 void strbuf_adddup(struct strbuf
*sb
, size_t pos
, size_t len
)
192 strbuf_grow(sb
, len
);
193 memcpy(sb
->buf
+ sb
->len
, sb
->buf
+ pos
, len
);
194 strbuf_setlen(sb
, sb
->len
+ len
);
197 void strbuf_addf(struct strbuf
*sb
, const char *fmt
, ...)
202 if (!strbuf_avail(sb
))
205 len
= vsnprintf(sb
->buf
+ sb
->len
, sb
->alloc
- sb
->len
, fmt
, ap
);
208 die("your vsnprintf is broken");
209 if (len
> strbuf_avail(sb
)) {
210 strbuf_grow(sb
, len
);
212 len
= vsnprintf(sb
->buf
+ sb
->len
, sb
->alloc
- sb
->len
, fmt
, ap
);
214 if (len
> strbuf_avail(sb
)) {
215 die("this should not happen, your snprintf is broken");
218 strbuf_setlen(sb
, sb
->len
+ len
);
221 void strbuf_expand(struct strbuf
*sb
, const char *format
, expand_fn_t fn
,
228 percent
= strchrnul(format
, '%');
229 strbuf_add(sb
, format
, percent
- format
);
232 format
= percent
+ 1;
234 if (*format
== '%') {
235 strbuf_addch(sb
, '%');
240 consumed
= fn(sb
, format
, context
);
244 strbuf_addch(sb
, '%');
248 size_t strbuf_expand_dict_cb(struct strbuf
*sb
, const char *placeholder
,
251 struct strbuf_expand_dict_entry
*e
= context
;
254 for (; e
->placeholder
&& (len
= strlen(e
->placeholder
)); e
++) {
255 if (!strncmp(placeholder
, e
->placeholder
, len
)) {
257 strbuf_addstr(sb
, e
->value
);
264 void strbuf_addbuf_percentquote(struct strbuf
*dst
, const struct strbuf
*src
)
266 int i
, len
= src
->len
;
268 for (i
= 0; i
< len
; i
++) {
269 if (src
->buf
[i
] == '%')
270 strbuf_addch(dst
, '%');
271 strbuf_addch(dst
, src
->buf
[i
]);
275 size_t strbuf_fread(struct strbuf
*sb
, size_t size
, FILE *f
)
278 size_t oldalloc
= sb
->alloc
;
280 strbuf_grow(sb
, size
);
281 res
= fread(sb
->buf
+ sb
->len
, 1, size
, f
);
283 strbuf_setlen(sb
, sb
->len
+ res
);
284 else if (oldalloc
== 0)
289 ssize_t
strbuf_read(struct strbuf
*sb
, int fd
, size_t hint
)
291 size_t oldlen
= sb
->len
;
292 size_t oldalloc
= sb
->alloc
;
294 strbuf_grow(sb
, hint
? hint
: 8192);
298 cnt
= xread(fd
, sb
->buf
+ sb
->len
, sb
->alloc
- sb
->len
- 1);
303 strbuf_setlen(sb
, oldlen
);
309 strbuf_grow(sb
, 8192);
312 sb
->buf
[sb
->len
] = '\0';
313 return sb
->len
- oldlen
;
316 #define STRBUF_MAXLINK (2*PATH_MAX)
318 int strbuf_readlink(struct strbuf
*sb
, const char *path
, size_t hint
)
320 size_t oldalloc
= sb
->alloc
;
325 while (hint
< STRBUF_MAXLINK
) {
328 strbuf_grow(sb
, hint
);
329 len
= readlink(path
, sb
->buf
, hint
);
333 } else if (len
< hint
) {
334 strbuf_setlen(sb
, len
);
338 /* .. the buffer was too small - try again */
346 int strbuf_getwholeline(struct strbuf
*sb
, FILE *fp
, int term
)
355 while ((ch
= fgetc(fp
)) != EOF
) {
357 sb
->buf
[sb
->len
++] = ch
;
361 if (ch
== EOF
&& sb
->len
== 0)
364 sb
->buf
[sb
->len
] = '\0';
368 int strbuf_getline(struct strbuf
*sb
, FILE *fp
, int term
)
370 if (strbuf_getwholeline(sb
, fp
, term
))
372 if (sb
->buf
[sb
->len
-1] == term
)
373 strbuf_setlen(sb
, sb
->len
-1);
377 int strbuf_read_file(struct strbuf
*sb
, const char *path
, size_t hint
)
381 fd
= open(path
, O_RDONLY
);
384 len
= strbuf_read(sb
, fd
, hint
);
392 int strbuf_branchname(struct strbuf
*sb
, const char *name
)
394 int len
= strlen(name
);
395 if (interpret_branch_name(name
, sb
) == len
)
397 strbuf_add(sb
, name
, len
);
401 int strbuf_check_branch_ref(struct strbuf
*sb
, const char *name
)
403 strbuf_branchname(sb
, name
);
404 strbuf_splice(sb
, 0, 0, "refs/heads/", 11);
405 return check_ref_format(sb
->buf
);