diff options
author | Michael Haggerty <mhagger@alum.mit.edu> | 2012-09-12 16:04:43 +0200 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2012-09-12 11:43:24 -0700 |
commit | ff919f965d20d003e3882c70de667f41a86349ac (patch) | |
tree | b63a1df5b8aeebaee410c4d39bf838ea65e16786 /test-string-list.c | |
parent | e448fed8e6ba9cd9237b7c8045c11cc40afc8595 (diff) | |
download | git-ff919f965d20d003e3882c70de667f41a86349ac.tar.gz |
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>
Diffstat (limited to 'test-string-list.c')
-rw-r--r-- | test-string-list.c | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/test-string-list.c b/test-string-list.c new file mode 100644 index 0000000000..cdc3cf3a7f --- /dev/null +++ b/test-string-list.c @@ -0,0 +1,45 @@ +#include "cache.h" +#include "string-list.h" + +void write_list(const struct string_list *list) +{ + int i; + for (i = 0; i < list->nr; i++) + printf("[%d]: \"%s\"\n", i, list->items[i].string); +} + +int main(int argc, char **argv) +{ + if (argc == 5 && !strcmp(argv[1], "split")) { + struct string_list list = STRING_LIST_INIT_DUP; + int i; + const char *s = argv[2]; + int delim = *argv[3]; + int maxsplit = atoi(argv[4]); + + i = string_list_split(&list, s, delim, maxsplit); + printf("%d\n", i); + write_list(&list); + string_list_clear(&list, 0); + return 0; + } + + if (argc == 5 && !strcmp(argv[1], "split_in_place")) { + struct string_list list = STRING_LIST_INIT_NODUP; + int i; + char *s = xstrdup(argv[2]); + int delim = *argv[3]; + int maxsplit = atoi(argv[4]); + + i = string_list_split_in_place(&list, s, delim, maxsplit); + printf("%d\n", i); + write_list(&list); + string_list_clear(&list, 0); + free(s); + return 0; + } + + fprintf(stderr, "%s: unknown function name: %s\n", argv[0], + argv[1] ? argv[1] : "(there was none)"); + return 1; +} |