string list functions
Subsystems (2)
v3 cover string list functions
Add some handy string list functions, for general use now, and for eventual use in the cpr/live update patches.
Steve Sistare (4):
util: strList_from_string
qapi: QAPI_LIST_LENGTH
util: strv_from_strList
util: strList unit tests
include/monitor/hmp.h | 1 - include/qapi/util.h | 13 ++++++++ include/qemu/strList.h | 30 ++++++++++++++++++ monitor/hmp-cmds.c | 19 ----------- net/net-hmp-cmds.c | 3 +- stats/stats-hmp-cmds.c | 3 +- tests/unit/meson.build | 1 + tests/unit/test-strList.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++ util/meson.build | 1 + util/strList.c | 38 ++++++++++++++++++++++ 10 files changed, 167 insertions(+), 22 deletions(-) create mode 100644 include/qemu/strList.h create mode 100644 tests/unit/test-strList.c create mode 100644 util/strList.c
Signature · 1 line
1.8.3.1
PATCH v3 1/4 util: strList_from_string
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 <[email protected]>
--- 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
include/monitor/hmp.h +0 -1
@@ -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);
include/qemu/strList.h new file +24 -0
@@ -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
monitor/hmp-cmds.c +0 -19
@@ -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;
net/net-hmp-cmds.c +2 -1
@@ -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 );
stats/stats-hmp-cmds.c +2 -1
@@ -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); }
util/meson.build +1 -0
@@ -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'))
util/strList.c new file +24 -0
@@ -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; +}
Signature · 1 line
1.8.3.1
PATCH v3 2/4 qapi: QAPI_LIST_LENGTH
In reply to Steve SistareSigned-off-by Steve Sistare <[email protected]>
Reviewed-by Marc-André Lureau <[email protected]>
--- include/qapi/util.h | 13 +++++++++++++ 1 file changed, 13 insertions(+)
include/qapi/util.h +13 -0
@@ -56,4 +56,17 @@ int parse_qapi_name(const char *name, bool complete); (tail) = &(*(tail))->next; \ } while (0) +/* +* For any GenericList @list, return its length. + */ +#define QAPI_LIST_LENGTH (list) \ + ({ \ + int len = 0; \ + typeof(list) elem; \ + for (elem = list; elem != NULL; elem = elem->next) { \ + len++; \ + } \ + len; \ + }) +#endif
Signature · 1 line
1.8.3.1
PATCH v3 3/4 util: strv_from_strList
In reply to Steve SistareSigned-off-by Steve Sistare <[email protected]>
Reviewed-by Marc-André Lureau <[email protected]>
--- include/qemu/strList.h | 6 ++++++ util/strList.c | 14 ++++++++++++++ 2 files changed, 20 insertions(+)
include/qemu/strList.h +6 -0
@@ -21,4 +21,10 @@*/ strList *strList_from_string (const char *in,const char *delim); +/* +* Produce and return a NULL-terminated array of strings from @args. +* The result is g_malloc'd and all strings are g_strdup'd. +*/ +GStrv strv_from_strList (const strList *args); +#endif
util/strList.c +14 -0
@@ -22,3 +22,17 @@ strList *strList_from_string(const char *str, const char *delim)return res; } + +GStrv strv_from_strList (const strList *args) +{ +const strList *arg; +int i =0 ; + GStrv argv = g_new(char *, QAPI_LIST_LENGTH(args) + 1); + +for (arg = args; arg !=NULL ; arg = arg->next) { + argv[i++] =g_strdup (arg->value); + } + argv[i] =NULL ; + +return argv; +}
Signature · 1 line
1.8.3.1
PATCH v3 4/4 util: strList unit tests
In reply to Steve SistareSigned-off-by Steve Sistare <[email protected]>
Reviewed-by Marc-André Lureau <[email protected]>
--- tests/unit/meson.build | 1 + tests/unit/test-strList.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 tests/unit/test-strList.c
tests/unit/meson.build +1 -0
@@ -34,6 +34,7 @@ tests = { 'test-rcu-simpleq': [], 'test-rcu-tailq': [], 'test-rcu-slist': [], + 'test-strList': [], 'test-qdist': [], 'test-qht': [], 'test-qtree': [],
tests/unit/test-strList.c new file +80 -0
80 lines not on this page · raw message
Signature · 1 line
1.8.3.1