argv_array: offer to split a string by whitespace
This is a simple function that will interpret a string as a whitespace delimited list of values, and add those values into the array. Note: this function does not (yet) offer to split by arbitrary delimiters, or keep empty values in case of runs of whitespace, or de-quote Unix shell style. All fo this functionality can be added later, when and if needed. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Johannes Schindelin committed
Apr 25, 2018 at 11:53 UTC
c5aa6db64f6f43621568bb1fec9360c25dbd2749
2 files changed
+22
argv-array.c
+20
@@ -64,6 +64,26 @@ void argv_array_pop(struct argv_array *array)
64
array->argc--;
65
}
66
67
+void argv_array_split(struct argv_array *array, const char *to_split)
68
+{
69
+ while (isspace(*to_split))
70
+ to_split++;
71
+ for (;;) {
72
+ const char *p = to_split;
73
+
74
+ if (!*p)
75
+ break;
76
+
77
+ while (*p && !isspace(*p))
78
+ p++;
79
+ argv_array_push_nodup(array, xstrndup(to_split, p - to_split));
80
+
81
+ while (isspace(*p))
82
+ p++;
83
+ to_split = p;
84
+ }
85
+}
86
+
87
void argv_array_clear(struct argv_array *array)
88
{
89
if (array->argv != empty_argv) {
argv-array.h
+2
@@ -19,6 +19,8 @@ LAST_ARG_MUST_BE_NULL
19
void argv_array_pushl(struct argv_array *, ...);
20
void argv_array_pushv(struct argv_array *, const char **);
21
void argv_array_pop(struct argv_array *);
22
+/* Splits by whitespace; does not handle quoted arguments! */
23
+void argv_array_split(struct argv_array *, const char *);
24
void argv_array_clear(struct argv_array *);
25
const char **argv_array_detach(struct argv_array *);
26