Create string_split_noalloc

This commit is contained in:
twinaphex 2020-08-26 03:20:42 +02:00
parent a32d027f76
commit 6df62bbccc
2 changed files with 38 additions and 1 deletions

View File

@ -88,6 +88,9 @@ bool string_list_find_elem_prefix(const struct string_list *list,
*/
struct string_list *string_split(const char *str, const char *delim);
bool string_split_noalloc(struct string_list *list,
const char *str, const char *delim);
/**
* string_separate:
* @str : string to turn into a string list

View File

@ -286,7 +286,7 @@ struct string_list *string_split(const char *str, const char *delim)
struct string_list *list = string_list_new();
if (!list)
goto error;
return NULL;
copy = strdup(str);
if (!copy)
@ -314,6 +314,40 @@ error:
return NULL;
}
bool string_split_noalloc(struct string_list *list,
const char *str, const char *delim)
{
char *save = NULL;
char *copy = NULL;
const char *tmp = NULL;
if (!list)
return false;
copy = strdup(str);
if (!copy)
return false;
tmp = strtok_r(copy, delim, &save);
while (tmp)
{
union string_list_elem_attr attr;
attr.i = 0;
if (!string_list_append(list, tmp, attr))
{
free(copy);
return false;
}
tmp = strtok_r(NULL, delim, &save);
}
free(copy);
return true;
}
/**
* string_separate:
* @str : string to turn into a string list