* [PATCH V3 1/4] util: strList_from_string
2023-06-20 17:10 [PATCH V3 0/4] string list functions Steve Sistare
@ 2023-06-20 17:10 ` Steve Sistare
2023-06-20 17:10 ` [PATCH V3 2/4] qapi: QAPI_LIST_LENGTH Steve Sistare
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Steve Sistare @ 2023-06-20 17:10 UTC (permalink / raw)
To: qemu-devel; +Cc: Marc-André Lureau, Markus Armbruster, Steve Sistare
Generalize hmp_split_at_comma() to take any delimiter string, rename
as strList_from_string(), and move it to util/strList.c.
No functional change.
Signed-off-by: Steve Sistare <steven.sistare@oracle.com>
---
include/monitor/hmp.h | 1 -
include/qemu/strList.h | 24 ++++++++++++++++++++++++
monitor/hmp-cmds.c | 19 -------------------
net/net-hmp-cmds.c | 3 ++-
stats/stats-hmp-cmds.c | 3 ++-
util/meson.build | 1 +
util/strList.c | 24 ++++++++++++++++++++++++
7 files changed, 53 insertions(+), 22 deletions(-)
create mode 100644 include/qemu/strList.h
create mode 100644 util/strList.c
diff --git a/include/monitor/hmp.h b/include/monitor/hmp.h
index 13f9a2d..2df661e 100644
--- a/include/monitor/hmp.h
+++ b/include/monitor/hmp.h
@@ -19,7 +19,6 @@
bool hmp_handle_error(Monitor *mon, Error *err);
void hmp_help_cmd(Monitor *mon, const char *name);
-strList *hmp_split_at_comma(const char *str);
void hmp_info_name(Monitor *mon, const QDict *qdict);
void hmp_info_version(Monitor *mon, const QDict *qdict);
diff --git a/include/qemu/strList.h b/include/qemu/strList.h
new file mode 100644
index 0000000..1f4c11d
--- /dev/null
+++ b/include/qemu/strList.h
@@ -0,0 +1,24 @@
+/*
+ * Copyright (c) 2022, 2023 Oracle and/or its affiliates.
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#ifndef QEMU_STR_LIST_H
+#define QEMU_STR_LIST_H
+
+#include "qapi/qapi-builtin-types.h"
+
+/*
+ * Break @in into a strList using the delimiter string @delim.
+ * The delimiter is not included in the result.
+ * Return NULL if @in is NULL or an empty string.
+ * A leading, trailing, or consecutive delimiter produces an
+ * empty string at that position in the output.
+ * All strings are g_strdup'd, and the result can be freed
+ * using qapi_free_strList.
+ */
+strList *strList_from_string(const char *in, const char *delim);
+
+#endif
diff --git a/monitor/hmp-cmds.c b/monitor/hmp-cmds.c
index 6c559b4..1e833f9 100644
--- a/monitor/hmp-cmds.c
+++ b/monitor/hmp-cmds.c
@@ -39,25 +39,6 @@ bool hmp_handle_error(Monitor *mon, Error *err)
return false;
}
-/*
- * Split @str at comma.
- * A null @str defaults to "".
- */
-strList *hmp_split_at_comma(const char *str)
-{
- char **split = g_strsplit(str ?: "", ",", -1);
- strList *res = NULL;
- strList **tail = &res;
- int i;
-
- for (i = 0; split[i]; i++) {
- QAPI_LIST_APPEND(tail, split[i]);
- }
-
- g_free(split);
- return res;
-}
-
void hmp_info_name(Monitor *mon, const QDict *qdict)
{
NameInfo *info;
diff --git a/net/net-hmp-cmds.c b/net/net-hmp-cmds.c
index 41d326b..e893801 100644
--- a/net/net-hmp-cmds.c
+++ b/net/net-hmp-cmds.c
@@ -26,6 +26,7 @@
#include "qemu/config-file.h"
#include "qemu/help_option.h"
#include "qemu/option.h"
+#include "qemu/strList.h"
void hmp_info_network(Monitor *mon, const QDict *qdict)
{
@@ -72,7 +73,7 @@ void hmp_announce_self(Monitor *mon, const QDict *qdict)
migrate_announce_params());
qapi_free_strList(params->interfaces);
- params->interfaces = hmp_split_at_comma(interfaces_str);
+ params->interfaces = strList_from_string(interfaces_str, ",");
params->has_interfaces = params->interfaces != NULL;
params->id = g_strdup(id);
qmp_announce_self(params, NULL);
diff --git a/stats/stats-hmp-cmds.c b/stats/stats-hmp-cmds.c
index 1f91bf8..428c0e6 100644
--- a/stats/stats-hmp-cmds.c
+++ b/stats/stats-hmp-cmds.c
@@ -10,6 +10,7 @@
#include "monitor/hmp.h"
#include "monitor/monitor.h"
#include "qemu/cutils.h"
+#include "qemu/strList.h"
#include "hw/core/cpu.h"
#include "qapi/qmp/qdict.h"
#include "qapi/error.h"
@@ -176,7 +177,7 @@ static StatsFilter *stats_filter(StatsTarget target, const char *names,
request->provider = provider_idx;
if (names && !g_str_equal(names, "*")) {
request->has_names = true;
- request->names = hmp_split_at_comma(names);
+ request->names = strList_from_string(names, ",");
}
QAPI_LIST_PREPEND(request_list, request);
}
diff --git a/util/meson.build b/util/meson.build
index 3a93071..960f233 100644
--- a/util/meson.build
+++ b/util/meson.build
@@ -1,4 +1,5 @@
util_ss.add(files('osdep.c', 'cutils.c', 'unicode.c', 'qemu-timer-common.c'))
+util_ss.add(files('strList.c'))
util_ss.add(files('thread-context.c'), numa)
if not config_host_data.get('CONFIG_ATOMIC64')
util_ss.add(files('atomic64.c'))
diff --git a/util/strList.c b/util/strList.c
new file mode 100644
index 0000000..217746e
--- /dev/null
+++ b/util/strList.c
@@ -0,0 +1,24 @@
+/*
+ * Copyright (c) 2023 Red Hat, Inc.
+ * Copyright (c) 2022, 2023 Oracle and/or its affiliates.
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#include "qemu/osdep.h"
+#include "qemu/strList.h"
+
+strList *strList_from_string(const char *str, const char *delim)
+{
+ g_autofree char **split = g_strsplit(str ?: "", delim, -1);
+ strList *res = NULL;
+ strList **tail = &res;
+ int i;
+
+ for (i = 0; split[i]; i++) {
+ QAPI_LIST_APPEND(tail, split[i]);
+ }
+
+ return res;
+}
--
1.8.3.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH V3 4/4] util: strList unit tests
2023-06-20 17:10 [PATCH V3 0/4] string list functions Steve Sistare
` (2 preceding siblings ...)
2023-06-20 17:10 ` [PATCH V3 3/4] util: strv_from_strList Steve Sistare
@ 2023-06-20 17:10 ` Steve Sistare
3 siblings, 0 replies; 5+ messages in thread
From: Steve Sistare @ 2023-06-20 17:10 UTC (permalink / raw)
To: qemu-devel; +Cc: Marc-André Lureau, Markus Armbruster, Steve Sistare
Signed-off-by: Steve Sistare <steven.sistare@oracle.com>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
tests/unit/meson.build | 1 +
tests/unit/test-strList.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 81 insertions(+)
create mode 100644 tests/unit/test-strList.c
diff --git a/tests/unit/meson.build b/tests/unit/meson.build
index 93977cc..972f2e6 100644
--- a/tests/unit/meson.build
+++ b/tests/unit/meson.build
@@ -34,6 +34,7 @@ tests = {
'test-rcu-simpleq': [],
'test-rcu-tailq': [],
'test-rcu-slist': [],
+ 'test-strList': [],
'test-qdist': [],
'test-qht': [],
'test-qtree': [],
diff --git a/tests/unit/test-strList.c b/tests/unit/test-strList.c
new file mode 100644
index 0000000..56df52b
--- /dev/null
+++ b/tests/unit/test-strList.c
@@ -0,0 +1,80 @@
+/*
+ * Copyright (c) 2022, 2023 Oracle and/or its affiliates.
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#include "qemu/osdep.h"
+#include "qemu/strList.h"
+
+static strList *make_list(int length)
+{
+ strList *head = 0, *list, **prev = &head;
+
+ while (length--) {
+ list = *prev = g_new0(strList, 1);
+ list->value = g_strdup("aaa");
+ prev = &list->next;
+ }
+ return head;
+}
+
+static void test_length(void)
+{
+ strList *list;
+ int i;
+
+ for (i = 0; i < 5; i++) {
+ list = make_list(i);
+ g_assert_cmpint(i, ==, QAPI_LIST_LENGTH(list));
+ qapi_free_strList(list);
+ }
+}
+
+struct {
+ const char *string;
+ const char *delim;
+ const char *args[5];
+} list_data[] = {
+ { 0, ",", { 0 } },
+ { "", ",", { 0 } },
+ { "a", ",", { "a", 0 } },
+ { "a,b", ",", { "a", "b", 0 } },
+ { "a,b,c", ",", { "a", "b", "c", 0 } },
+ { "first last", " ", { "first", "last", 0 } },
+ { "a:", ":", { "a", "", 0 } },
+ { "a::b", ":", { "a", "", "b", 0 } },
+ { ":", ":", { "", "", 0 } },
+ { ":a", ":", { "", "a", 0 } },
+ { "::a", ":", { "", "", "a", 0 } },
+};
+
+static void test_strv(void)
+{
+ int i, j;
+ const char **expect;
+ strList *list;
+ GStrv args;
+
+ for (i = 0; i < ARRAY_SIZE(list_data); i++) {
+ expect = list_data[i].args;
+ list = strList_from_string(list_data[i].string, list_data[i].delim);
+ args = strv_from_strList(list);
+ qapi_free_strList(list);
+ for (j = 0; expect[j] && args[j]; j++) {
+ g_assert_cmpstr(expect[j], ==, args[j]);
+ }
+ g_assert_null(expect[j]);
+ g_assert_null(args[j]);
+ g_strfreev(args);
+ }
+}
+
+int main(int argc, char **argv)
+{
+ g_test_init(&argc, &argv, NULL);
+ g_test_add_func("/test-string/length", test_length);
+ g_test_add_func("/test-string/strv", test_strv);
+ return g_test_run();
+}
--
1.8.3.1
^ permalink raw reply related [flat|nested] 5+ messages in thread