string_list: add two new functions for splitting strings

Add two new functions, string_list_split() and string_list_split_in_place(). These split a string into a string_list on a separator character. The first makes copies of the substrings (leaving the input string untouched) and the second splits the original string in place, overwriting the separator characters with NULs and referring to the original string's memory. These functions are similar to the strbuf_split_*() functions except that they work with the more powerful string_list interface. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Sep 12, 2012 at 16:04 UTC ff919f965d20d003e3882c70de667f41a86349ac
7 files changed +213 -1
.gitignore
+1
@@ -193,6 +193,7 @@
193 /test-run-command
194 /test-sha1
195 /test-sigchain
196 +/test-string-list
197 /test-subprocess
198 /test-svn-fe
199 /common-cmds.h
Documentation/technical/api-string-list.txt
+21 -1
@@ -21,7 +21,8 @@ member (you need this if you add things later) and you should set the
21 `nr` and `alloc` members in that case, too.
22
23 . Adds new items to the list, using `string_list_append`,
24 - `string_list_append_nodup`, or `string_list_insert`.
24 + `string_list_append_nodup`, `string_list_insert`,
25 + `string_list_split`, and/or `string_list_split_in_place`.
26
27 . Can check if a string is in the list using `string_list_has_string` or
28 `unsorted_string_list_has_string` and get it from the list using
@@ -135,6 +136,25 @@ counterpart for sorted lists, which performs a binary search.
136 is set. The third parameter controls if the `util` pointer of the
137 items should be freed or not.
138
139 +`string_list_split`::
140 +`string_list_split_in_place`::
141 +
142 + Split a string into substrings on a delimiter character and
143 + append the substrings to a `string_list`. If `maxsplit` is
144 + non-negative, then split at most `maxsplit` times. Return the
145 + number of substrings appended to the list.
146 ++
147 +`string_list_split` requires a `string_list` that has `strdup_strings`
148 +set to true; it leaves the input string untouched and makes copies of
149 +the substrings in newly-allocated memory.
150 +`string_list_split_in_place` requires a `string_list` that has
151 +`strdup_strings` set to false; it splits the input string in place,
152 +overwriting the delimiter characters with NULs and creating new
153 +string_list_items that point into the original string (the original
154 +string must therefore not be modified or freed while the `string_list`
155 +is in use).
156 +
157 +
158 Data structures
159 ---------------
160
Makefile
+1
@@ -501,6 +501,7 @@ TEST_PROGRAMS_NEED_X += test-run-command
501 TEST_PROGRAMS_NEED_X += test-scrap-cache-tree
502 TEST_PROGRAMS_NEED_X += test-sha1
503 TEST_PROGRAMS_NEED_X += test-sigchain
504 +TEST_PROGRAMS_NEED_X += test-string-list
505 TEST_PROGRAMS_NEED_X += test-subprocess
506 TEST_PROGRAMS_NEED_X += test-svn-fe
507
string-list.c
+53
@@ -204,3 +204,56 @@ void unsorted_string_list_delete_item(struct string_list *list, int i, int free_
204 list->items[i] = list->items[list->nr-1];
205 list->nr--;
206 }
207 +
208 +int string_list_split(struct string_list *list, const char *string,
209 + int delim, int maxsplit)
210 +{
211 + int count = 0;
212 + const char *p = string, *end;
213 +
214 + if (!list->strdup_strings)
215 + die("internal error in string_list_split(): "
216 + "list->strdup_strings must be set");
217 + for (;;) {
218 + count++;
219 + if (maxsplit >= 0 && count > maxsplit) {
220 + string_list_append(list, p);
221 + return count;
222 + }
223 + end = strchr(p, delim);
224 + if (end) {
225 + string_list_append_nodup(list, xmemdupz(p, end - p));
226 + p = end + 1;
227 + } else {
228 + string_list_append(list, p);
229 + return count;
230 + }
231 + }
232 +}
233 +
234 +int string_list_split_in_place(struct string_list *list, char *string,
235 + int delim, int maxsplit)
236 +{
237 + int count = 0;
238 + char *p = string, *end;
239 +
240 + if (list->strdup_strings)
241 + die("internal error in string_list_split_in_place(): "
242 + "list->strdup_strings must not be set");
243 + for (;;) {
244 + count++;
245 + if (maxsplit >= 0 && count > maxsplit) {
246 + string_list_append(list, p);
247 + return count;
248 + }
249 + end = strchr(p, delim);
250 + if (end) {
251 + *end = '\0';
252 + string_list_append(list, p);
253 + p = end + 1;
254 + } else {
255 + string_list_append(list, p);
256 + return count;
257 + }
258 + }
259 +}
string-list.h
+29
@@ -63,4 +63,33 @@ struct string_list_item *unsorted_string_list_lookup(struct string_list *list,
63 const char *string);
64
65 void unsorted_string_list_delete_item(struct string_list *list, int i, int free_util);
66 +
67 +/*
68 + * Split string into substrings on character delim and append the
69 + * substrings to list. The input string is not modified.
70 + * list->strdup_strings must be set, as new memory needs to be
71 + * allocated to hold the substrings. If maxsplit is non-negative,
72 + * then split at most maxsplit times. Return the number of substrings
73 + * appended to list.
74 + *
75 + * Examples:
76 + * string_list_split(l, "foo:bar:baz", ':', -1) -> ["foo", "bar", "baz"]
77 + * string_list_split(l, "foo:bar:baz", ':', 0) -> ["foo:bar:baz"]
78 + * string_list_split(l, "foo:bar:baz", ':', 1) -> ["foo", "bar:baz"]
79 + * string_list_split(l, "foo:bar:", ':', -1) -> ["foo", "bar", ""]
80 + * string_list_split(l, "", ':', -1) -> [""]
81 + * string_list_split(l, ":", ':', -1) -> ["", ""]
82 + */
83 +int string_list_split(struct string_list *list, const char *string,
84 + int delim, int maxsplit);
85 +
86 +/*
87 + * Like string_list_split(), except that string is split in-place: the
88 + * delimiter characters in string are overwritten with NULs, and the
89 + * new string_list_items point into string (which therefore must not
90 + * be modified or freed while the string_list is in use).
91 + * list->strdup_strings must *not* be set.
92 + */
93 +int string_list_split_in_place(struct string_list *list, char *string,
94 + int delim, int maxsplit);
95 #endif /* STRING_LIST_H */
t/t0063-string-list.sh new
+63
@@ -0,0 +1,63 @@
1 +#!/bin/sh
2 +#
3 +# Copyright (c) 2012 Michael Haggerty
4 +#
5 +
6 +test_description='Test string list functionality'
7 +
8 +. ./test-lib.sh
9 +
10 +test_split () {
11 + cat >expected &&
12 + test_expect_success "split $1 at $2, max $3" "
13 + test-string-list split '$1' '$2' '$3' >actual &&
14 + test_cmp expected actual &&
15 + test-string-list split_in_place '$1' '$2' '$3' >actual &&
16 + test_cmp expected actual
17 + "
18 +}
19 +
20 +test_split "foo:bar:baz" ":" "-1" <<EOF
21 +3
22 +[0]: "foo"
23 +[1]: "bar"
24 +[2]: "baz"
25 +EOF
26 +
27 +test_split "foo:bar:baz" ":" "0" <<EOF
28 +1
29 +[0]: "foo:bar:baz"
30 +EOF
31 +
32 +test_split "foo:bar:baz" ":" "1" <<EOF
33 +2
34 +[0]: "foo"
35 +[1]: "bar:baz"
36 +EOF
37 +
38 +test_split "foo:bar:baz" ":" "2" <<EOF
39 +3
40 +[0]: "foo"
41 +[1]: "bar"
42 +[2]: "baz"
43 +EOF
44 +
45 +test_split "foo:bar:" ":" "-1" <<EOF
46 +3
47 +[0]: "foo"
48 +[1]: "bar"
49 +[2]: ""
50 +EOF
51 +
52 +test_split "" ":" "-1" <<EOF
53 +1
54 +[0]: ""
55 +EOF
56 +
57 +test_split ":" ":" "-1" <<EOF
58 +2
59 +[0]: ""
60 +[1]: ""
61 +EOF
62 +
63 +test_done
test-string-list.c new
+45
@@ -0,0 +1,45 @@
1 +#include "cache.h"
2 +#include "string-list.h"
3 +
4 +void write_list(const struct string_list *list)
5 +{
6 + int i;
7 + for (i = 0; i < list->nr; i++)
8 + printf("[%d]: \"%s\"\n", i, list->items[i].string);
9 +}
10 +
11 +int main(int argc, char **argv)
12 +{
13 + if (argc == 5 && !strcmp(argv[1], "split")) {
14 + struct string_list list = STRING_LIST_INIT_DUP;
15 + int i;
16 + const char *s = argv[2];
17 + int delim = *argv[3];
18 + int maxsplit = atoi(argv[4]);
19 +
20 + i = string_list_split(&list, s, delim, maxsplit);
21 + printf("%d\n", i);
22 + write_list(&list);
23 + string_list_clear(&list, 0);
24 + return 0;
25 + }
26 +
27 + if (argc == 5 && !strcmp(argv[1], "split_in_place")) {
28 + struct string_list list = STRING_LIST_INIT_NODUP;
29 + int i;
30 + char *s = xstrdup(argv[2]);
31 + int delim = *argv[3];
32 + int maxsplit = atoi(argv[4]);
33 +
34 + i = string_list_split_in_place(&list, s, delim, maxsplit);
35 + printf("%d\n", i);
36 + write_list(&list);
37 + string_list_clear(&list, 0);
38 + free(s);
39 + return 0;
40 + }
41 +
42 + fprintf(stderr, "%s: unknown function name: %s\n", argv[0],
43 + argv[1] ? argv[1] : "(there was none)");
44 + return 1;
45 +}