describe: fix --no-exact-match
[git/debian.git] / strvec.c
blob17d54b6c3bc500f7c8f927a702e1ec26ba66b7a1
1 #include "git-compat-util.h"
2 #include "strvec.h"
3 #include "alloc.h"
4 #include "hex.h"
5 #include "strbuf.h"
7 const char *empty_strvec[] = { NULL };
9 void strvec_init(struct strvec *array)
11 struct strvec blank = STRVEC_INIT;
12 memcpy(array, &blank, sizeof(*array));
15 static void strvec_push_nodup(struct strvec *array, const char *value)
17 if (array->v == empty_strvec)
18 array->v = NULL;
20 ALLOC_GROW(array->v, array->nr + 2, array->alloc);
21 array->v[array->nr++] = value;
22 array->v[array->nr] = NULL;
25 const char *strvec_push(struct strvec *array, const char *value)
27 strvec_push_nodup(array, xstrdup(value));
28 return array->v[array->nr - 1];
31 const char *strvec_pushf(struct strvec *array, const char *fmt, ...)
33 va_list ap;
34 struct strbuf v = STRBUF_INIT;
36 va_start(ap, fmt);
37 strbuf_vaddf(&v, fmt, ap);
38 va_end(ap);
40 strvec_push_nodup(array, strbuf_detach(&v, NULL));
41 return array->v[array->nr - 1];
44 void strvec_pushl(struct strvec *array, ...)
46 va_list ap;
47 const char *arg;
49 va_start(ap, array);
50 while ((arg = va_arg(ap, const char *)))
51 strvec_push(array, arg);
52 va_end(ap);
55 void strvec_pushv(struct strvec *array, const char **items)
57 for (; *items; items++)
58 strvec_push(array, *items);
61 void strvec_pop(struct strvec *array)
63 if (!array->nr)
64 return;
65 free((char *)array->v[array->nr - 1]);
66 array->v[array->nr - 1] = NULL;
67 array->nr--;
70 void strvec_split(struct strvec *array, const char *to_split)
72 while (isspace(*to_split))
73 to_split++;
74 for (;;) {
75 const char *p = to_split;
77 if (!*p)
78 break;
80 while (*p && !isspace(*p))
81 p++;
82 strvec_push_nodup(array, xstrndup(to_split, p - to_split));
84 while (isspace(*p))
85 p++;
86 to_split = p;
90 void strvec_clear(struct strvec *array)
92 if (array->v != empty_strvec) {
93 int i;
94 for (i = 0; i < array->nr; i++)
95 free((char *)array->v[i]);
96 free(array->v);
98 strvec_init(array);
101 const char **strvec_detach(struct strvec *array)
103 if (array->v == empty_strvec)
104 return xcalloc(1, sizeof(const char *));
105 else {
106 const char **ret = array->v;
107 strvec_init(array);
108 return ret;