Browse Source
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>maint


7 changed files with 213 additions and 1 deletions
@ -0,0 +1,63 @@
@@ -0,0 +1,63 @@
|
||||
#!/bin/sh |
||||
# |
||||
# Copyright (c) 2012 Michael Haggerty |
||||
# |
||||
|
||||
test_description='Test string list functionality' |
||||
|
||||
. ./test-lib.sh |
||||
|
||||
test_split () { |
||||
cat >expected && |
||||
test_expect_success "split $1 at $2, max $3" " |
||||
test-string-list split '$1' '$2' '$3' >actual && |
||||
test_cmp expected actual && |
||||
test-string-list split_in_place '$1' '$2' '$3' >actual && |
||||
test_cmp expected actual |
||||
" |
||||
} |
||||
|
||||
test_split "foo:bar:baz" ":" "-1" <<EOF |
||||
3 |
||||
[0]: "foo" |
||||
[1]: "bar" |
||||
[2]: "baz" |
||||
EOF |
||||
|
||||
test_split "foo:bar:baz" ":" "0" <<EOF |
||||
1 |
||||
[0]: "foo:bar:baz" |
||||
EOF |
||||
|
||||
test_split "foo:bar:baz" ":" "1" <<EOF |
||||
2 |
||||
[0]: "foo" |
||||
[1]: "bar:baz" |
||||
EOF |
||||
|
||||
test_split "foo:bar:baz" ":" "2" <<EOF |
||||
3 |
||||
[0]: "foo" |
||||
[1]: "bar" |
||||
[2]: "baz" |
||||
EOF |
||||
|
||||
test_split "foo:bar:" ":" "-1" <<EOF |
||||
3 |
||||
[0]: "foo" |
||||
[1]: "bar" |
||||
[2]: "" |
||||
EOF |
||||
|
||||
test_split "" ":" "-1" <<EOF |
||||
1 |
||||
[0]: "" |
||||
EOF |
||||
|
||||
test_split ":" ":" "-1" <<EOF |
||||
2 |
||||
[0]: "" |
||||
[1]: "" |
||||
EOF |
||||
|
||||
test_done |
@ -0,0 +1,45 @@
@@ -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; |
||||
} |
Loading…
Reference in new issue