mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-02-01 17:35:17 +00:00
util: virstring: Remove virStringListAdd and virStringListRemove
virStringListAdd hides the fact that a O(n) count of elements is performed every time it's called which makes it inefficient. Stop supporting such semantics and remove the helpers. Users have a choice of using GSList or an array with a counter variable rather than repeated lookups. Signed-off-by: Peter Krempa <pkrempa@redhat.com> Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
parent
67c345cb97
commit
2b3e390674
@ -3240,14 +3240,12 @@ virStringHasControlChars;
|
||||
virStringHasSuffix;
|
||||
virStringIsEmpty;
|
||||
virStringIsPrintable;
|
||||
virStringListAdd;
|
||||
virStringListFreeCount;
|
||||
virStringListGetFirstWithPrefix;
|
||||
virStringListHasString;
|
||||
virStringListJoin;
|
||||
virStringListLength;
|
||||
virStringListMerge;
|
||||
virStringListRemove;
|
||||
virStringMatch;
|
||||
virStringMatchesNameSuffix;
|
||||
virStringParsePort;
|
||||
|
@ -162,67 +162,6 @@ char *virStringListJoin(const char **strings,
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* virStringListAdd:
|
||||
* @strings: a NULL-terminated array of strings
|
||||
* @item: string to add
|
||||
*
|
||||
* Appends @item into string list @strings. If *@strings is not
|
||||
* allocated yet new string list is created.
|
||||
*
|
||||
* Returns: 0 on success,
|
||||
* -1 otherwise
|
||||
*/
|
||||
int
|
||||
virStringListAdd(char ***strings,
|
||||
const char *item)
|
||||
{
|
||||
size_t i = virStringListLength((const char **) *strings);
|
||||
|
||||
if (VIR_EXPAND_N(*strings, i, 2) < 0)
|
||||
return -1;
|
||||
|
||||
(*strings)[i - 2] = g_strdup(item);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* virStringListRemove:
|
||||
* @strings: a NULL-terminated array of strings
|
||||
* @item: string to remove
|
||||
*
|
||||
* Remove every occurrence of @item in list of @strings.
|
||||
*/
|
||||
void
|
||||
virStringListRemove(char ***strings,
|
||||
const char *item)
|
||||
{
|
||||
size_t r, w = 0;
|
||||
|
||||
if (!strings || !*strings)
|
||||
return;
|
||||
|
||||
for (r = 0; (*strings)[r]; r++) {
|
||||
if (STREQ((*strings)[r], item)) {
|
||||
VIR_FREE((*strings)[r]);
|
||||
continue;
|
||||
}
|
||||
if (r != w)
|
||||
(*strings)[w] = (*strings)[r];
|
||||
w++;
|
||||
}
|
||||
|
||||
if (w == 0) {
|
||||
VIR_FREE(*strings);
|
||||
} else {
|
||||
(*strings)[w] = NULL;
|
||||
ignore_value(VIR_REALLOC_N(*strings, w + 1));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* virStringListMerge:
|
||||
* @dst: a NULL-terminated array of strings to expand
|
||||
|
@ -37,11 +37,6 @@ char *virStringListJoin(const char **strings,
|
||||
const char *delim)
|
||||
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
|
||||
|
||||
int virStringListAdd(char ***strings,
|
||||
const char *item);
|
||||
void virStringListRemove(char ***strings,
|
||||
const char *item);
|
||||
|
||||
int virStringListMerge(char ***dst,
|
||||
char ***src);
|
||||
|
||||
|
@ -163,74 +163,6 @@ static int testJoin(const void *args)
|
||||
}
|
||||
|
||||
|
||||
static int testAdd(const void *args)
|
||||
{
|
||||
const struct testJoinData *data = args;
|
||||
char **list = NULL;
|
||||
char *got = NULL;
|
||||
int ret = -1;
|
||||
size_t i;
|
||||
|
||||
for (i = 0; data->tokens[i]; i++) {
|
||||
if (virStringListAdd(&list, data->tokens[i]) < 0)
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
if (!list)
|
||||
list = g_new0(char *, 1);
|
||||
|
||||
if (!(got = virStringListJoin((const char **)list, data->delim))) {
|
||||
VIR_DEBUG("Got no result");
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
if (STRNEQ(got, data->string)) {
|
||||
fprintf(stderr, "Mismatch '%s' vs '%s'\n", got, data->string);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
ret = 0;
|
||||
cleanup:
|
||||
g_strfreev(list);
|
||||
VIR_FREE(got);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
static int testRemove(const void *args)
|
||||
{
|
||||
const struct testSplitData *data = args;
|
||||
char **list = NULL;
|
||||
size_t ntokens;
|
||||
size_t i;
|
||||
int ret = -1;
|
||||
|
||||
if (!(list = virStringSplitCount(data->string, data->delim,
|
||||
data->max_tokens, &ntokens))) {
|
||||
VIR_DEBUG("Got no tokens at all");
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (i = 0; data->tokens[i]; i++) {
|
||||
virStringListRemove(&list, data->tokens[i]);
|
||||
if (virStringListHasString((const char **) list, data->tokens[i])) {
|
||||
fprintf(stderr, "Not removed %s", data->tokens[i]);
|
||||
goto cleanup;
|
||||
}
|
||||
}
|
||||
|
||||
if (list && list[0]) {
|
||||
fprintf(stderr, "Not removed all tokens: %s", list[0]);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
ret = 0;
|
||||
cleanup:
|
||||
g_strfreev(list);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
static int
|
||||
testStringSortCompare(const void *opaque G_GNUC_UNUSED)
|
||||
{
|
||||
@ -683,10 +615,6 @@ mymain(void)
|
||||
ret = -1; \
|
||||
if (virTestRun("Join " #str, testJoin, &joinData) < 0) \
|
||||
ret = -1; \
|
||||
if (virTestRun("Add " #str, testAdd, &joinData) < 0) \
|
||||
ret = -1; \
|
||||
if (virTestRun("Remove " #str, testRemove, &splitData) < 0) \
|
||||
ret = -1; \
|
||||
} while (0)
|
||||
|
||||
VIR_WARNINGS_NO_DECLARATION_AFTER_STATEMENT
|
||||
|
Loading…
x
Reference in New Issue
Block a user