1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-05-12 14:06:08 +02:00
git/test-string-list.c
Michael Haggerty ff919f965d 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>
2012-09-12 11:43:24 -07:00

46 lines
1.0 KiB
C

#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;
}