1#include "cache.h"
2#include "refs.h"
3
4int prefixcmp(const char *str, const char *prefix)
5{
6 for (; ; str++, prefix++)
7 if (!*prefix)
8 return 0;
9 else if (*str != *prefix)
10 return (unsigned char)*prefix - (unsigned char)*str;
11}
12
13int suffixcmp(const char *str, const char *suffix)
14{
15 int len = strlen(str), suflen = strlen(suffix);
16 if (len < suflen)
17 return -1;
18 else
19 return strcmp(str + len - suflen, suffix);
20}
21
22/*
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
25 * initialized strbuf.
26 */
27char strbuf_slopbuf[1];
28
29void strbuf_init(struct strbuf *sb, size_t hint)
30{
31 sb->alloc = sb->len = 0;
32 sb->buf = strbuf_slopbuf;
33 if (hint)
34 strbuf_grow(sb, hint);
35}
36
37void strbuf_release(struct strbuf *sb)
38{
39 if (sb->alloc) {
40 free(sb->buf);
41 strbuf_init(sb, 0);
42 }
43}
44
45char *strbuf_detach(struct strbuf *sb, size_t *sz)
46{
47 char *res = sb->alloc ? sb->buf : NULL;
48 if (sz)
49 *sz = sb->len;
50 strbuf_init(sb, 0);
51 return res;
52}
53
54void strbuf_attach(struct strbuf *sb, void *buf, size_t len, size_t alloc)
55{
56 strbuf_release(sb);
57 sb->buf = buf;
58 sb->len = len;
59 sb->alloc = alloc;
60 strbuf_grow(sb, 0);
61 sb->buf[sb->len] = '\0';
62}
63
64void strbuf_grow(struct strbuf *sb, size_t extra)
65{
66 if (sb->len + extra + 1 <= sb->len)
67 die("you want to use way too much memory");
68 if (!sb->alloc)
69 sb->buf = NULL;
70 ALLOC_GROW(sb->buf, sb->len + extra + 1, sb->alloc);
71}
72
73void strbuf_trim(struct strbuf *sb)
74{
75 char *b = sb->buf;
76 while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1]))
77 sb->len--;
78 while (sb->len > 0 && isspace(*b)) {
79 b++;
80 sb->len--;
81 }
82 memmove(sb->buf, b, sb->len);
83 sb->buf[sb->len] = '\0';
84}
85void strbuf_rtrim(struct strbuf *sb)
86{
87 while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1]))
88 sb->len--;
89 sb->buf[sb->len] = '\0';
90}
91
92void strbuf_ltrim(struct strbuf *sb)
93{
94 char *b = sb->buf;
95 while (sb->len > 0 && isspace(*b)) {
96 b++;
97 sb->len--;
98 }
99 memmove(sb->buf, b, sb->len);
100 sb->buf[sb->len] = '\0';
101}
102
103void strbuf_tolower(struct strbuf *sb)
104{
105 int i;
106 for (i = 0; i < sb->len; i++)
107 sb->buf[i] = tolower(sb->buf[i]);
108}
109
110struct strbuf **strbuf_split(const struct strbuf *sb, int delim)
111{
112 int alloc = 2, pos = 0;
113 char *n, *p;
114 struct strbuf **ret;
115 struct strbuf *t;
116
117 ret = xcalloc(alloc, sizeof(struct strbuf *));
118 p = n = sb->buf;
119 while (n < sb->buf + sb->len) {
120 int len;
121 n = memchr(n, delim, sb->len - (n - sb->buf));
122 if (pos + 1 >= alloc) {
123 alloc = alloc * 2;
124 ret = xrealloc(ret, sizeof(struct strbuf *) * alloc);
125 }
126 if (!n)
127 n = sb->buf + sb->len - 1;
128 len = n - p + 1;
129 t = xmalloc(sizeof(struct strbuf));
130 strbuf_init(t, len);
131 strbuf_add(t, p, len);
132 ret[pos] = t;
133 ret[++pos] = NULL;
134 p = ++n;
135 }
136 return ret;
137}
138
139void strbuf_list_free(struct strbuf **sbs)
140{
141 struct strbuf **s = sbs;
142
143 while (*s) {
144 strbuf_release(*s);
145 free(*s++);
146 }
147 free(sbs);
148}
149
150int strbuf_cmp(const struct strbuf *a, const struct strbuf *b)
151{
152 int len = a->len < b->len ? a->len: b->len;
153 int cmp = memcmp(a->buf, b->buf, len);
154 if (cmp)
155 return cmp;
156 return a->len < b->len ? -1: a->len != b->len;
157}
158
159void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
160 const void *data, size_t dlen)
161{
162 if (pos + len < pos)
163 die("you want to use way too much memory");
164 if (pos > sb->len)
165 die("`pos' is too far after the end of the buffer");
166 if (pos + len > sb->len)
167 die("`pos + len' is too far after the end of the buffer");
168
169 if (dlen >= len)
170 strbuf_grow(sb, dlen - len);
171 memmove(sb->buf + pos + dlen,
172 sb->buf + pos + len,
173 sb->len - pos - len);
174 memcpy(sb->buf + pos, data, dlen);
175 strbuf_setlen(sb, sb->len + dlen - len);
176}
177
178void strbuf_insert(struct strbuf *sb, size_t pos, const void *data, size_t len)
179{
180 strbuf_splice(sb, pos, 0, data, len);
181}
182
183void strbuf_remove(struct strbuf *sb, size_t pos, size_t len)
184{
185 strbuf_splice(sb, pos, len, NULL, 0);
186}
187
188void strbuf_add(struct strbuf *sb, const void *data, size_t len)
189{
190 strbuf_grow(sb, len);
191 memcpy(sb->buf + sb->len, data, len);
192 strbuf_setlen(sb, sb->len + len);
193}
194
195void strbuf_adddup(struct strbuf *sb, size_t pos, size_t len)
196{
197 strbuf_grow(sb, len);
198 memcpy(sb->buf + sb->len, sb->buf + pos, len);
199 strbuf_setlen(sb, sb->len + len);
200}
201
202void strbuf_addf(struct strbuf *sb, const char *fmt, ...)
203{
204 int len;
205 va_list ap;
206
207 if (!strbuf_avail(sb))
208 strbuf_grow(sb, 64);
209 va_start(ap, fmt);
210 len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
211 va_end(ap);
212 if (len < 0)
213 die("your vsnprintf is broken");
214 if (len > strbuf_avail(sb)) {
215 strbuf_grow(sb, len);
216 va_start(ap, fmt);
217 len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
218 va_end(ap);
219 if (len > strbuf_avail(sb)) {
220 die("this should not happen, your snprintf is broken");
221 }
222 }
223 strbuf_setlen(sb, sb->len + len);
224}
225
226void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn,
227 void *context)
228{
229 for (;;) {
230 const char *percent;
231 size_t consumed;
232
233 percent = strchrnul(format, '%');
234 strbuf_add(sb, format, percent - format);
235 if (!*percent)
236 break;
237 format = percent + 1;
238
239 consumed = fn(sb, format, context);
240 if (consumed)
241 format += consumed;
242 else
243 strbuf_addch(sb, '%');
244 }
245}
246
247size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder,
248 void *context)
249{
250 struct strbuf_expand_dict_entry *e = context;
251 size_t len;
252
253 for (; e->placeholder && (len = strlen(e->placeholder)); e++) {
254 if (!strncmp(placeholder, e->placeholder, len)) {
255 if (e->value)
256 strbuf_addstr(sb, e->value);
257 return len;
258 }
259 }
260 return 0;
261}
262
263size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f)
264{
265 size_t res;
266 size_t oldalloc = sb->alloc;
267
268 strbuf_grow(sb, size);
269 res = fread(sb->buf + sb->len, 1, size, f);
270 if (res > 0)
271 strbuf_setlen(sb, sb->len + res);
272 else if (oldalloc == 0)
273 strbuf_release(sb);
274 return res;
275}
276
277ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
278{
279 size_t oldlen = sb->len;
280 size_t oldalloc = sb->alloc;
281
282 strbuf_grow(sb, hint ? hint : 8192);
283 for (;;) {
284 ssize_t cnt;
285
286 cnt = xread(fd, sb->buf + sb->len, sb->alloc - sb->len - 1);
287 if (cnt < 0) {
288 if (oldalloc == 0)
289 strbuf_release(sb);
290 else
291 strbuf_setlen(sb, oldlen);
292 return -1;
293 }
294 if (!cnt)
295 break;
296 sb->len += cnt;
297 strbuf_grow(sb, 8192);
298 }
299
300 sb->buf[sb->len] = '\0';
301 return sb->len - oldlen;
302}
303
304#define STRBUF_MAXLINK (2*PATH_MAX)
305
306int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint)
307{
308 size_t oldalloc = sb->alloc;
309
310 if (hint < 32)
311 hint = 32;
312
313 while (hint < STRBUF_MAXLINK) {
314 int len;
315
316 strbuf_grow(sb, hint);
317 len = readlink(path, sb->buf, hint);
318 if (len < 0) {
319 if (errno != ERANGE)
320 break;
321 } else if (len < hint) {
322 strbuf_setlen(sb, len);
323 return 0;
324 }
325
326 /* .. the buffer was too small - try again */
327 hint *= 2;
328 }
329 if (oldalloc == 0)
330 strbuf_release(sb);
331 return -1;
332}
333
334int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
335{
336 int ch;
337
338 strbuf_grow(sb, 0);
339 if (feof(fp))
340 return EOF;
341
342 strbuf_reset(sb);
343 while ((ch = fgetc(fp)) != EOF) {
344 strbuf_grow(sb, 1);
345 sb->buf[sb->len++] = ch;
346 if (ch == term)
347 break;
348 }
349 if (ch == EOF && sb->len == 0)
350 return EOF;
351
352 sb->buf[sb->len] = '\0';
353 return 0;
354}
355
356int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
357{
358 if (strbuf_getwholeline(sb, fp, term))
359 return EOF;
360 if (sb->buf[sb->len-1] == term)
361 strbuf_setlen(sb, sb->len-1);
362 return 0;
363}
364
365int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)
366{
367 int fd, len;
368
369 fd = open(path, O_RDONLY);
370 if (fd < 0)
371 return -1;
372 len = strbuf_read(sb, fd, hint);
373 close(fd);
374 if (len < 0)
375 return -1;
376
377 return len;
378}
379
380int strbuf_branchname(struct strbuf *sb, const char *name)
381{
382 int len = strlen(name);
383 if (interpret_branch_name(name, sb) == len)
384 return 0;
385 strbuf_add(sb, name, len);
386 return len;
387}
388
389int strbuf_check_branch_ref(struct strbuf *sb, const char *name)
390{
391 strbuf_branchname(sb, name);
392 strbuf_splice(sb, 0, 0, "refs/heads/", 11);
393 return check_ref_format(sb->buf);
394}