string_list: add two new functions for splitting strings
[git/jnareb-git.git] / test-string-list.c
blobcdc3cf3a7fd92a659c5fe503ef0aef0b6e006f3a
1 #include "cache.h"
2 #include "string-list.h"
4 void write_list(const struct string_list *list)
6 int i;
7 for (i = 0; i < list->nr; i++)
8 printf("[%d]: \"%s\"\n", i, list->items[i].string);
11 int main(int argc, char **argv)
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]);
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;
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]);
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;
42 fprintf(stderr, "%s: unknown function name: %s\n", argv[0],
43 argv[1] ? argv[1] : "(there was none)");
44 return 1;