argv_array: offer to split a string by whitespace
authorJohannes Schindelin <johannes.schindelin@gmx.de>
Wed, 25 Apr 2018 09:53:57 +0000 (11:53 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 26 Apr 2018 03:52:57 +0000 (12:52 +0900)
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>
argv-array.c
argv-array.h
index 5d370fa3366163f8c0c81ca0b6b1a64a7030c696..cb5bcd2c064961919d965cb2cae79de653162fde 100644 (file)
@@ -64,6 +64,26 @@ void argv_array_pop(struct argv_array *array)
        array->argc--;
 }
 
+void argv_array_split(struct argv_array *array, const char *to_split)
+{
+       while (isspace(*to_split))
+               to_split++;
+       for (;;) {
+               const char *p = to_split;
+
+               if (!*p)
+                       break;
+
+               while (*p && !isspace(*p))
+                       p++;
+               argv_array_push_nodup(array, xstrndup(to_split, p - to_split));
+
+               while (isspace(*p))
+                       p++;
+               to_split = p;
+       }
+}
+
 void argv_array_clear(struct argv_array *array)
 {
        if (array->argv != empty_argv) {
index 29056e49a1208b5506d0809c7311e4112dc1f7f3..750c30d2f2cc7c7532ae67778da6396b4744708f 100644 (file)
@@ -19,6 +19,8 @@ LAST_ARG_MUST_BE_NULL
 void argv_array_pushl(struct argv_array *, ...);
 void argv_array_pushv(struct argv_array *, const char **);
 void argv_array_pop(struct argv_array *);
+/* Splits by whitespace; does not handle quoted arguments! */
+void argv_array_split(struct argv_array *, const char *);
 void argv_array_clear(struct argv_array *);
 const char **argv_array_detach(struct argv_array *);