diff mbox series

[V5,1/5] util: str_split

Message ID 1708638470-114846-2-git-send-email-steven.sistare@oracle.com (mailing list archive)
State New, archived
Headers show
Series string list functions | expand

Commit Message

Steven Sistare Feb. 22, 2024, 9:47 p.m. UTC
Generalize hmp_split_at_comma() to take any delimiter string, rename
as str_split(), 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

Comments

Philippe Mathieu-Daudé Feb. 23, 2024, 5:51 a.m. UTC | #1
On 22/2/24 22:47, Steve Sistare wrote:
> Generalize hmp_split_at_comma() to take any delimiter string, rename
> as str_split(), 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

Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Philippe Mathieu-Daudé Feb. 23, 2024, 6:01 a.m. UTC | #2
On 22/2/24 22:47, Steve Sistare wrote:
> Generalize hmp_split_at_comma() to take any delimiter string, rename
> as str_split(), 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


> +#include "qapi/qapi-builtin-types.h"
> +
> +/*
> + * Split @str into a strList using the delimiter string @delim.
> + * The delimiter is not included in the result.
> + * Return NULL if @str 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.

Note "qapi/qapi-builtin-types.h" defines:

   G_DEFINE_AUTOPTR_CLEANUP_FUNC(strList, qapi_free_strList)

Maybe mention we can also use:

   g_autoptr(strList)

?

> + */
> +strList *str_split(const char *str, const char *delim);
> +
> +#endif
Steven Sistare Feb. 23, 2024, 2:01 p.m. UTC | #3
On 2/23/2024 1:01 AM, Philippe Mathieu-Daudé wrote:
> On 22/2/24 22:47, Steve Sistare wrote:
>> Generalize hmp_split_at_comma() to take any delimiter string, rename
>> as str_split(), 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
> 
> 
>> +#include "qapi/qapi-builtin-types.h"
>> +
>> +/*
>> + * Split @str into a strList using the delimiter string @delim.
>> + * The delimiter is not included in the result.
>> + * Return NULL if @str 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.
> 
> Note "qapi/qapi-builtin-types.h" defines:
> 
>   G_DEFINE_AUTOPTR_CLEANUP_FUNC(strList, qapi_free_strList)
> 
> Maybe mention we can also use:
> 
>   g_autoptr(strList)

Thanks Philippe.  If we get to V6 for this series, I will mention this,
and also mention g_autoptr(GStrv) in the header comment for strv_from_strlist.

- Steve

>> + */
>> +strList *str_split(const char *str, const char *delim);
>> +
>> +#endif
>
Philippe Mathieu-Daudé Feb. 23, 2024, 5:41 p.m. UTC | #4
On 23/2/24 15:01, Steven Sistare wrote:
> On 2/23/2024 1:01 AM, Philippe Mathieu-Daudé wrote:
>> On 22/2/24 22:47, Steve Sistare wrote:
>>> Generalize hmp_split_at_comma() to take any delimiter string, rename
>>> as str_split(), 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
>>
>>
>>> +#include "qapi/qapi-builtin-types.h"
>>> +
>>> +/*
>>> + * Split @str into a strList using the delimiter string @delim.
>>> + * The delimiter is not included in the result.
>>> + * Return NULL if @str 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.
>>
>> Note "qapi/qapi-builtin-types.h" defines:
>>
>>    G_DEFINE_AUTOPTR_CLEANUP_FUNC(strList, qapi_free_strList)
>>
>> Maybe mention we can also use:
>>
>>    g_autoptr(strList)
> 
> Thanks Philippe.  If we get to V6 for this series, I will mention this,
> and also mention g_autoptr(GStrv) in the header comment for strv_from_strlist.

If there is no need for v6, do you mind sharing here what would be
the resulting comment? Maybe Markus can update it directly...
(assuming he takes your series).
Steven Sistare Feb. 23, 2024, 6:04 p.m. UTC | #5
On 2/23/2024 12:41 PM, Philippe Mathieu-Daudé wrote:
> On 23/2/24 15:01, Steven Sistare wrote:
>> On 2/23/2024 1:01 AM, Philippe Mathieu-Daudé wrote:
>>> On 22/2/24 22:47, Steve Sistare wrote:
>>>> Generalize hmp_split_at_comma() to take any delimiter string, rename
>>>> as str_split(), 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
>>>
>>>
>>>> +#include "qapi/qapi-builtin-types.h"
>>>> +
>>>> +/*
>>>> + * Split @str into a strList using the delimiter string @delim.
>>>> + * The delimiter is not included in the result.
>>>> + * Return NULL if @str 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.
>>>
>>> Note "qapi/qapi-builtin-types.h" defines:
>>>
>>>    G_DEFINE_AUTOPTR_CLEANUP_FUNC(strList, qapi_free_strList)
>>>
>>> Maybe mention we can also use:
>>>
>>>    g_autoptr(strList)
>>
>> Thanks Philippe.  If we get to V6 for this series, I will mention this,
>> and also mention g_autoptr(GStrv) in the header comment for strv_from_strlist.
> 
> If there is no need for v6, do you mind sharing here what would be
> the resulting comment? Maybe Markus can update it directly...
> (assuming he takes your series).

Sure - steve

--------------------
diff --git a/include/qemu/strList.h b/include/qemu/strList.h
index c1eb1dd..b13bd53 100644
--- a/include/qemu/strList.h
+++ b/include/qemu/strList.h
@@ -17,13 +17,16 @@
  * 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.
+ * using qapi_free_strList, or by declaring a local variable
+ * with g_autoptr(strList).
  */
 strList *str_split(const char *str, const char *delim);

 /*
  * Produce and return a NULL-terminated array of strings from @list.
- * The result is g_malloc'd and all strings are g_strdup'd.
+ * The result is g_malloc'd and all strings are g_strdup'd.  The result
+ * can be freed using g_strfreev, or by declaring a local variable with
+ * g_auto(GStrv).
  */
 char **strv_from_strList(const strList *list);

--------------------
Philippe Mathieu-Daudé Feb. 26, 2024, 12:21 p.m. UTC | #6
On 22/2/24 22:47, Steve Sistare wrote:
> Generalize hmp_split_at_comma() to take any delimiter string, rename
> as str_split(), 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

Markus, is that OK to include these two in your QAPI
section in MAINTAINERS? Squashing:

-- >8 --
diff --git a/MAINTAINERS b/MAINTAINERS
index 992799171f..7970d34cdd 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -3196,6 +3196,8 @@ X: qapi/*.json
  F: include/qapi/
  X: include/qapi/qmp/
  F: include/qapi/qmp/dispatch.h
+F: include/qemu/strList.h
+F: util/strList.c
  F: tests/qapi-schema/
  F: tests/unit/test-*-visitor.c
  F: tests/unit/test-qapi-*.c
---
diff mbox series

Patch

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..0f26116
--- /dev/null
+++ b/include/qemu/strList.h
@@ -0,0 +1,24 @@ 
+/*
+ * Copyright (c) 2022 - 2024 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"
+
+/*
+ * Split @str into a strList using the delimiter string @delim.
+ * The delimiter is not included in the result.
+ * Return NULL if @str 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 *str_split(const char *str, const char *delim);
+
+#endif
diff --git a/monitor/hmp-cmds.c b/monitor/hmp-cmds.c
index 871898a..66b68a0 100644
--- a/monitor/hmp-cmds.c
+++ b/monitor/hmp-cmds.c
@@ -38,25 +38,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..969cdd1 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 = str_split(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..62db8c6 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 = str_split(names, ",");
             }
             QAPI_LIST_PREPEND(request_list, request);
         }
diff --git a/util/meson.build b/util/meson.build
index 0ef9886..bd125a4 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..7588c7c
--- /dev/null
+++ b/util/strList.c
@@ -0,0 +1,24 @@ 
+/*
+ * Copyright (c) 2023 Red Hat, Inc.
+ * Copyright (c) 2022 - 2024 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 *str_split(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;
+}