mirror of
https://github.com/libvirt/libvirt.git
synced 2025-02-25 18:55:26 -06:00
virStringSplitCount: Reimplement using g_strsplit and g_strv_length
Signed-off-by: Peter Krempa <pkrempa@redhat.com> Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
parent
50cc5d7035
commit
dc7ac81d37
@ -43,28 +43,9 @@ VIR_LOG_INIT("util.string");
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* virStringSplitCount:
|
* virStringSplitCount:
|
||||||
* @string: a string to split
|
|
||||||
* @delim: a string which specifies the places at which to split
|
|
||||||
* the string. The delimiter is not included in any of the resulting
|
|
||||||
* strings, unless @max_tokens is reached.
|
|
||||||
* @max_tokens: the maximum number of pieces to split @string into.
|
|
||||||
* If this is 0, the string is split completely.
|
|
||||||
* @tokcount: If provided, the value is set to the count of pieces the string
|
|
||||||
* was split to excluding the terminating NULL element.
|
|
||||||
*
|
*
|
||||||
* Splits a string into a maximum of @max_tokens pieces, using the given
|
* A wrapper for g_strsplit which provides number of elements of the split
|
||||||
* @delim. If @max_tokens is reached, the remainder of @string is
|
* string.
|
||||||
* appended to the last token.
|
|
||||||
*
|
|
||||||
* As a special case, the result of splitting the empty string "" is an empty
|
|
||||||
* vector, not a vector containing a single string. The reason for this
|
|
||||||
* special case is that being able to represent an empty vector is typically
|
|
||||||
* more useful than consistent handling of empty elements. If you do need
|
|
||||||
* to represent empty elements, you'll need to check for the empty string
|
|
||||||
* before calling virStringSplit().
|
|
||||||
*
|
|
||||||
* Return value: a newly-allocated NULL-terminated array of strings. Use
|
|
||||||
* g_strfreev() to free it.
|
|
||||||
*/
|
*/
|
||||||
char **
|
char **
|
||||||
virStringSplitCount(const char *string,
|
virStringSplitCount(const char *string,
|
||||||
@ -72,54 +53,11 @@ virStringSplitCount(const char *string,
|
|||||||
size_t max_tokens,
|
size_t max_tokens,
|
||||||
size_t *tokcount)
|
size_t *tokcount)
|
||||||
{
|
{
|
||||||
char **tokens = NULL;
|
GStrv ret = g_strsplit(string, delim, max_tokens);
|
||||||
size_t ntokens = 0;
|
|
||||||
size_t maxtokens = 0;
|
|
||||||
const char *remainder = string;
|
|
||||||
char *tmp;
|
|
||||||
size_t i;
|
|
||||||
|
|
||||||
if (max_tokens == 0)
|
*tokcount = g_strv_length(ret);
|
||||||
max_tokens = INT_MAX;
|
|
||||||
|
|
||||||
tmp = strstr(remainder, delim);
|
return ret;
|
||||||
if (tmp) {
|
|
||||||
size_t delimlen = strlen(delim);
|
|
||||||
|
|
||||||
while (--max_tokens && tmp) {
|
|
||||||
size_t len = tmp - remainder;
|
|
||||||
|
|
||||||
if (VIR_RESIZE_N(tokens, maxtokens, ntokens, 1) < 0)
|
|
||||||
goto error;
|
|
||||||
|
|
||||||
tokens[ntokens] = g_strndup(remainder, len);
|
|
||||||
ntokens++;
|
|
||||||
remainder = tmp + delimlen;
|
|
||||||
tmp = strstr(remainder, delim);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (*string) {
|
|
||||||
if (VIR_RESIZE_N(tokens, maxtokens, ntokens, 1) < 0)
|
|
||||||
goto error;
|
|
||||||
|
|
||||||
tokens[ntokens] = g_strdup(remainder);
|
|
||||||
ntokens++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (VIR_RESIZE_N(tokens, maxtokens, ntokens, 1) < 0)
|
|
||||||
goto error;
|
|
||||||
tokens[ntokens++] = NULL;
|
|
||||||
|
|
||||||
if (tokcount)
|
|
||||||
*tokcount = ntokens - 1;
|
|
||||||
|
|
||||||
return tokens;
|
|
||||||
|
|
||||||
error:
|
|
||||||
for (i = 0; i < ntokens; i++)
|
|
||||||
VIR_FREE(tokens[i]);
|
|
||||||
VIR_FREE(tokens);
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -26,7 +26,7 @@ char **virStringSplitCount(const char *string,
|
|||||||
const char *delim,
|
const char *delim,
|
||||||
size_t max_tokens,
|
size_t max_tokens,
|
||||||
size_t *tokcount)
|
size_t *tokcount)
|
||||||
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
|
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(4);
|
||||||
|
|
||||||
char *virStringListJoin(const char **strings,
|
char *virStringListJoin(const char **strings,
|
||||||
const char *delim)
|
const char *delim)
|
||||||
|
Loading…
Reference in New Issue
Block a user