Skip to content

Commit c5aa6db

Browse files
dschogitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1f1cddd commit c5aa6db

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

argv-array.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,26 @@ void argv_array_pop(struct argv_array *array)
6464
array->argc--;
6565
}
6666

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+
6787
void argv_array_clear(struct argv_array *array)
6888
{
6989
if (array->argv != empty_argv) {

argv-array.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ LAST_ARG_MUST_BE_NULL
1919
void argv_array_pushl(struct argv_array *, ...);
2020
void argv_array_pushv(struct argv_array *, const char **);
2121
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 *);
2224
void argv_array_clear(struct argv_array *);
2325
const char **argv_array_detach(struct argv_array *);
2426

0 commit comments

Comments
 (0)