[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 01/28] qemu-option: simplify search for end of key
From: |
Paolo Bonzini |
Subject: |
[PATCH 01/28] qemu-option: simplify search for end of key |
Date: |
Wed, 2 Dec 2020 04:02:38 -0500 |
Use strcspn to find an equal or comma value, and pass the result directly
to get_opt_name to avoid another strchr.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
util/qemu-option.c | 35 +++++++++++++----------------------
1 file changed, 13 insertions(+), 22 deletions(-)
diff --git a/util/qemu-option.c b/util/qemu-option.c
index 25792159ba..6bd654a473 100644
--- a/util/qemu-option.c
+++ b/util/qemu-option.c
@@ -38,27 +38,19 @@
#include "qemu/help_option.h"
/*
- * Extracts the name of an option from the parameter string (p points at the
+ * Extracts the name of an option from the parameter string (@p points at the
* first byte of the option name)
*
- * The option name is delimited by delim (usually , or =) or the string end
- * and is copied into option. The caller is responsible for free'ing option
- * when no longer required.
+ * The option name is @len characters long and is copied into @option. The
+ * caller is responsible for free'ing @option when no longer required.
*
* The return value is the position of the delimiter/zero byte after the option
- * name in p.
+ * name in @p.
*/
-static const char *get_opt_name(const char *p, char **option, char delim)
+static const char *get_opt_name(const char *p, char **option, size_t len)
{
- char *offset = strchr(p, delim);
-
- if (offset) {
- *option = g_strndup(p, offset - p);
- return offset;
- } else {
- *option = g_strdup(p);
- return p + strlen(p);
- }
+ *option = g_strndup(p, len);
+ return p + len;
}
/*
@@ -766,12 +758,11 @@ static const char *get_opt_name_value(const char *params,
const char *firstname,
char **name, char **value)
{
- const char *p, *pe, *pc;
-
- pe = strchr(params, '=');
- pc = strchr(params, ',');
+ const char *p;
+ size_t len;
- if (!pe || (pc && pc < pe)) {
+ len = strcspn(params, "=,");
+ if (params[len] != '=') {
/* found "foo,more" */
if (firstname) {
/* implicitly named first option */
@@ -779,7 +770,7 @@ static const char *get_opt_name_value(const char *params,
p = get_opt_value(params, value);
} else {
/* option without value, must be a flag */
- p = get_opt_name(params, name, ',');
+ p = get_opt_name(params, name, len);
if (strncmp(*name, "no", 2) == 0) {
memmove(*name, *name + 2, strlen(*name + 2) + 1);
*value = g_strdup("off");
@@ -789,7 +780,7 @@ static const char *get_opt_name_value(const char *params,
}
} else {
/* found "foo=bar,more" */
- p = get_opt_name(params, name, '=');
+ p = get_opt_name(params, name, len);
assert(*p == '=');
p++;
p = get_opt_value(p, value);
--
2.26.2
- [PATCH 00/28] qemu-option, keyval, vl: switch -object/-M/-accel to keyval parsing, Paolo Bonzini, 2020/12/02
- [PATCH 03/28] qemu-option: clean up id vs. list->merge_lists, Paolo Bonzini, 2020/12/02
- [PATCH 04/28] qemu-option: move help handling to get_opt_name_value, Paolo Bonzini, 2020/12/02
- [PATCH 02/28] qemu-option: pass QemuOptsList to opts_accepts_any, Paolo Bonzini, 2020/12/02
- [PATCH 01/28] qemu-option: simplify search for end of key,
Paolo Bonzini <=
- [PATCH 05/28] qemu-option: warn for short-form boolean options, Paolo Bonzini, 2020/12/02
- [PATCH 06/28] keyval: accept escaped commas in implied option, Paolo Bonzini, 2020/12/02
- [PATCH 11/28] qom: use qemu_printf to print help for user-creatable objects, Paolo Bonzini, 2020/12/02
- [PATCH 07/28] keyval: simplify keyval_parse_one, Paolo Bonzini, 2020/12/02
- [PATCH 08/28] tests: convert check-qom-proplist to keyval, Paolo Bonzini, 2020/12/02
- [PATCH 09/28] keyval: introduce keyval_parse_into, Paolo Bonzini, 2020/12/02
- [PATCH 19/28] qemu-io: use keyval for -object parsing, Paolo Bonzini, 2020/12/02
- [PATCH 25/28] vl: rename local variable in configure_accelerators, Paolo Bonzini, 2020/12/02
- [PATCH 24/28] qom: export more functions for use with non-UserCreatable objects, Paolo Bonzini, 2020/12/02
- [PATCH 10/28] hmp: replace "O" parser with keyval, Paolo Bonzini, 2020/12/02