path: root/argv-array.c
diff options
authorJeff King <>2020-07-28 20:23:25 (GMT)
committerJunio C Hamano <>2020-07-28 22:02:17 (GMT)
commit873cd28a8b17ff21908c78c7929a7615f8c94992 (patch)
treef9daec6b9851a6f4373f0a029903fb3ad04ec23b /argv-array.c
parent819f0e76b110458488c41741b0dcd538483848c0 (diff)
argv-array: rename to strvec
The name "argv-array" isn't very good, because it describes what the data type can be used for (program argument arrays), not what it actually is (a dynamically-growing string array that maintains a NULL-terminator invariant). This leads to people being hesitant to use it for other cases where it would actually be a good fit. The existing name is also clunky to use. It's overly long, and the name often leads to saying things like "argv.argv" (i.e., the field names overlap with variable names, since they're describing the use, not the type). Let's give it a more neutral name. I settled on "strvec" because "vector" is the name for a dynamic array type in many programming languages. "strarray" would work, too, but it's longer and a bit more awkward to say (and don't we all say these things in our mind as we type them?). A more extreme direction would be a generic data structure which stores a NULL-terminated of _any_ type. That would be easy to do with void pointers, but we'd lose some type safety for the existing cases. Plus it raises questions about memory allocation and ownership. So I limited myself here to changing names only, and not semantics. If we do find a use for that more generic data type, we could perhaps implement it at a lower level and then provide type-safe wrappers around it for strings. But that can come later. This patch does the minimum to convert the struct and function names in the header and implementation, leaving a few things for follow-on patches: - files retain their original names for now - struct field names are retained for now - there's a preprocessor compat layer that lets most users remain the same for now. The exception is headers which made a manual forward declaration of the struct. I've converted them (and their dependent function declarations) here. Signed-off-by: Jeff King <> Signed-off-by: Junio C Hamano <>
Diffstat (limited to 'argv-array.c')
1 files changed, 22 insertions, 22 deletions
diff --git a/argv-array.c b/argv-array.c
index 61ef8c0..b7461c4 100644
--- a/argv-array.c
+++ b/argv-array.c
@@ -2,18 +2,18 @@
#include "argv-array.h"
#include "strbuf.h"
-const char *empty_argv[] = { NULL };
+const char *empty_strvec[] = { NULL };
-void argv_array_init(struct argv_array *array)
+void strvec_init(struct strvec *array)
- array->argv = empty_argv;
+ array->argv = empty_strvec;
array->argc = 0;
array->alloc = 0;
-static void argv_array_push_nodup(struct argv_array *array, const char *value)
+static void strvec_push_nodup(struct strvec *array, const char *value)
- if (array->argv == empty_argv)
+ if (array->argv == empty_strvec)
array->argv = NULL;
ALLOC_GROW(array->argv, array->argc + 2, array->alloc);
@@ -21,13 +21,13 @@ static void argv_array_push_nodup(struct argv_array *array, const char *value)
array->argv[array->argc] = NULL;
-const char *argv_array_push(struct argv_array *array, const char *value)
+const char *strvec_push(struct strvec *array, const char *value)
- argv_array_push_nodup(array, xstrdup(value));
+ strvec_push_nodup(array, xstrdup(value));
return array->argv[array->argc - 1];
-const char *argv_array_pushf(struct argv_array *array, const char *fmt, ...)
+const char *strvec_pushf(struct strvec *array, const char *fmt, ...)
va_list ap;
struct strbuf v = STRBUF_INIT;
@@ -36,28 +36,28 @@ const char *argv_array_pushf(struct argv_array *array, const char *fmt, ...)
strbuf_vaddf(&v, fmt, ap);
- argv_array_push_nodup(array, strbuf_detach(&v, NULL));
+ strvec_push_nodup(array, strbuf_detach(&v, NULL));
return array->argv[array->argc - 1];
-void argv_array_pushl(struct argv_array *array, ...)
+void strvec_pushl(struct strvec *array, ...)
va_list ap;
const char *arg;
va_start(ap, array);
while ((arg = va_arg(ap, const char *)))
- argv_array_push(array, arg);
+ strvec_push(array, arg);
-void argv_array_pushv(struct argv_array *array, const char **argv)
+void strvec_pushv(struct strvec *array, const char **argv)
for (; *argv; argv++)
- argv_array_push(array, *argv);
+ strvec_push(array, *argv);
-void argv_array_pop(struct argv_array *array)
+void strvec_pop(struct strvec *array)
if (!array->argc)
@@ -66,7 +66,7 @@ void argv_array_pop(struct argv_array *array)
-void argv_array_split(struct argv_array *array, const char *to_split)
+void strvec_split(struct strvec *array, const char *to_split)
while (isspace(*to_split))
@@ -78,7 +78,7 @@ void argv_array_split(struct argv_array *array, const char *to_split)
while (*p && !isspace(*p))
- argv_array_push_nodup(array, xstrndup(to_split, p - to_split));
+ strvec_push_nodup(array, xstrndup(to_split, p - to_split));
while (isspace(*p))
@@ -86,24 +86,24 @@ void argv_array_split(struct argv_array *array, const char *to_split)
-void argv_array_clear(struct argv_array *array)
+void strvec_clear(struct strvec *array)
- if (array->argv != empty_argv) {
+ if (array->argv != empty_strvec) {
int i;
for (i = 0; i < array->argc; i++)
free((char *)array->argv[i]);
- argv_array_init(array);
+ strvec_init(array);
-const char **argv_array_detach(struct argv_array *array)
+const char **strvec_detach(struct strvec *array)
- if (array->argv == empty_argv)
+ if (array->argv == empty_strvec)
return xcalloc(1, sizeof(const char *));
else {
const char **ret = array->argv;
- argv_array_init(array);
+ strvec_init(array);
return ret;