qemu-option: simplify search for end of key
Use strcspn to find an equal or comma value, and pass the result directly to get_opt_name to avoid another strchr. Reviewed-by: Markus Armbruster <armbru@redhat.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
parent
4054adbdd2
commit
924e9b0da9
|
@ -38,27 +38,19 @@
|
||||||
#include "qemu/help_option.h"
|
#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)
|
* first byte of the option name)
|
||||||
*
|
*
|
||||||
* The option name is delimited by delim (usually , or =) or the string end
|
* The option name is @len characters long and is copied into @option. The
|
||||||
* and is copied into option. The caller is responsible for free'ing option
|
* caller is responsible for free'ing @option when no longer required.
|
||||||
* when no longer required.
|
|
||||||
*
|
*
|
||||||
* The return value is the position of the delimiter/zero byte after the option
|
* 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);
|
*option = g_strndup(p, len);
|
||||||
|
return p + len;
|
||||||
if (offset) {
|
|
||||||
*option = g_strndup(p, offset - p);
|
|
||||||
return offset;
|
|
||||||
} else {
|
|
||||||
*option = g_strdup(p);
|
|
||||||
return p + strlen(p);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -766,12 +758,11 @@ static const char *get_opt_name_value(const char *params,
|
||||||
const char *firstname,
|
const char *firstname,
|
||||||
char **name, char **value)
|
char **name, char **value)
|
||||||
{
|
{
|
||||||
const char *p, *pe, *pc;
|
const char *p;
|
||||||
|
size_t len;
|
||||||
|
|
||||||
pe = strchr(params, '=');
|
len = strcspn(params, "=,");
|
||||||
pc = strchr(params, ',');
|
if (params[len] != '=') {
|
||||||
|
|
||||||
if (!pe || (pc && pc < pe)) {
|
|
||||||
/* found "foo,more" */
|
/* found "foo,more" */
|
||||||
if (firstname) {
|
if (firstname) {
|
||||||
/* implicitly named first option */
|
/* implicitly named first option */
|
||||||
|
@ -779,7 +770,7 @@ static const char *get_opt_name_value(const char *params,
|
||||||
p = get_opt_value(params, value);
|
p = get_opt_value(params, value);
|
||||||
} else {
|
} else {
|
||||||
/* option without value, must be a flag */
|
/* 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) {
|
if (strncmp(*name, "no", 2) == 0) {
|
||||||
memmove(*name, *name + 2, strlen(*name + 2) + 1);
|
memmove(*name, *name + 2, strlen(*name + 2) + 1);
|
||||||
*value = g_strdup("off");
|
*value = g_strdup("off");
|
||||||
|
@ -789,7 +780,7 @@ static const char *get_opt_name_value(const char *params,
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
/* found "foo=bar,more" */
|
/* found "foo=bar,more" */
|
||||||
p = get_opt_name(params, name, '=');
|
p = get_opt_name(params, name, len);
|
||||||
assert(*p == '=');
|
assert(*p == '=');
|
||||||
p++;
|
p++;
|
||||||
p = get_opt_value(p, value);
|
p = get_opt_value(p, value);
|
||||||
|
|
Loading…
Reference in New Issue