[PATCH 21/40] util: virstring: Remove virStringListAdd and virStringListRemove

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxx>
---
 src/libvirt_private.syms |  2 --
 src/util/virstring.c     | 61 ----------------------------------
 src/util/virstring.h     |  5 ---
 tests/virstringtest.c    | 72 ----------------------------------------
 4 files changed, 140 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index c2270208e4..86380d0853 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -3240,14 +3240,12 @@ virStringHasControlChars;
 virStringHasSuffix;
 virStringIsEmpty;
 virStringIsPrintable;
-virStringListAdd;
 virStringListFreeCount;
 virStringListGetFirstWithPrefix;
 virStringListHasString;
 virStringListJoin;
 virStringListLength;
 virStringListMerge;
-virStringListRemove;
 virStringMatch;
 virStringMatchesNameSuffix;
 virStringParsePort;
diff --git a/src/util/virstring.c b/src/util/virstring.c
index 521f2de3f6..bf861d4847 100644
--- a/src/util/virstring.c
+++ b/src/util/virstring.c
@@ -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
diff --git a/src/util/virstring.h b/src/util/virstring.h
index cfd24f0b74..00c669b43b 100644
--- a/src/util/virstring.h
+++ b/src/util/virstring.h
@@ -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);

diff --git a/tests/virstringtest.c b/tests/virstringtest.c
index 7305190691..9eabb5b299 100644
--- a/tests/virstringtest.c
+++ b/tests/virstringtest.c
@@ -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
-- 
2.29.2




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux