1 #include "git-compat-util.h"
5 const char *empty_strvec
[] = { NULL
};
7 void strvec_init(struct strvec
*array
)
9 struct strvec blank
= STRVEC_INIT
;
10 memcpy(array
, &blank
, sizeof(*array
));
13 static void strvec_push_nodup(struct strvec
*array
, const char *value
)
15 if (array
->v
== empty_strvec
)
18 ALLOC_GROW(array
->v
, array
->nr
+ 2, array
->alloc
);
19 array
->v
[array
->nr
++] = value
;
20 array
->v
[array
->nr
] = NULL
;
23 const char *strvec_push(struct strvec
*array
, const char *value
)
25 strvec_push_nodup(array
, xstrdup(value
));
26 return array
->v
[array
->nr
- 1];
29 const char *strvec_pushf(struct strvec
*array
, const char *fmt
, ...)
32 struct strbuf v
= STRBUF_INIT
;
35 strbuf_vaddf(&v
, fmt
, ap
);
38 strvec_push_nodup(array
, strbuf_detach(&v
, NULL
));
39 return array
->v
[array
->nr
- 1];
42 void strvec_pushl(struct strvec
*array
, ...)
48 while ((arg
= va_arg(ap
, const char *)))
49 strvec_push(array
, arg
);
53 void strvec_pushv(struct strvec
*array
, const char **items
)
55 for (; *items
; items
++)
56 strvec_push(array
, *items
);
59 const char *strvec_replace(struct strvec
*array
, size_t idx
, const char *replacement
)
63 BUG("index outside of array boundary");
64 to_free
= (char *) array
->v
[idx
];
65 array
->v
[idx
] = xstrdup(replacement
);
70 void strvec_remove(struct strvec
*array
, size_t idx
)
73 BUG("index outside of array boundary");
74 free((char *)array
->v
[idx
]);
75 memmove(array
->v
+ idx
, array
->v
+ idx
+ 1, (array
->nr
- idx
) * sizeof(char *));
79 void strvec_pop(struct strvec
*array
)
83 free((char *)array
->v
[array
->nr
- 1]);
84 array
->v
[array
->nr
- 1] = NULL
;
88 void strvec_split(struct strvec
*array
, const char *to_split
)
90 while (isspace(*to_split
))
93 const char *p
= to_split
;
98 while (*p
&& !isspace(*p
))
100 strvec_push_nodup(array
, xstrndup(to_split
, p
- to_split
));
108 void strvec_clear(struct strvec
*array
)
110 if (array
->v
!= empty_strvec
) {
112 for (i
= 0; i
< array
->nr
; i
++)
113 free((char *)array
->v
[i
]);
119 const char **strvec_detach(struct strvec
*array
)
121 if (array
->v
== empty_strvec
)
122 return xcalloc(1, sizeof(const char *));
124 const char **ret
= array
->v
;