From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38159) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fGNf0-00006a-0P for qemu-devel@nongnu.org; Wed, 09 May 2018 07:53:31 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fGNez-0006K2-3r for qemu-devel@nongnu.org; Wed, 09 May 2018 07:53:30 -0400 From: Markus Armbruster References: <20180509055802.28423-1-famz@redhat.com> <20180509055802.28423-2-famz@redhat.com> Date: Wed, 09 May 2018 13:53:23 +0200 In-Reply-To: <20180509055802.28423-2-famz@redhat.com> (Fam Zheng's message of "Wed, 9 May 2018 13:57:58 +0800") Message-ID: <87wowd9hak.fsf@dusky.pond.sub.org> MIME-Version: 1.0 Content-Type: text/plain Subject: Re: [Qemu-devel] [PATCH 1/5] qapi: Add qapi_enum_parse_full List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Fam Zheng Cc: qemu-devel@nongnu.org, Kevin Wolf , Michael Roth , qemu-block@nongnu.org, Max Reitz Fam Zheng writes: > This variant of qapi_enum_parse can do case insensitive compare. I'm curious why we need that. We'll see when we get to the new function's uses. > Signed-off-by: Fam Zheng > --- > include/qapi/util.h | 2 ++ > qapi/qapi-util.c | 20 ++++++++++++++++---- > 2 files changed, 18 insertions(+), 4 deletions(-) > > diff --git a/include/qapi/util.h b/include/qapi/util.h > index a7c3c64148..2cec231919 100644 > --- a/include/qapi/util.h > +++ b/include/qapi/util.h > @@ -19,6 +19,8 @@ typedef struct QEnumLookup { > const char *qapi_enum_lookup(const QEnumLookup *lookup, int val); > int qapi_enum_parse(const QEnumLookup *lookup, const char *buf, > int def, Error **errp); > +int qapi_enum_parse_full(const QEnumLookup *lookup, const char *buf, > + int def, bool ignore_case, Error **errp); > > int parse_qapi_name(const char *name, bool complete); > > diff --git a/qapi/qapi-util.c b/qapi/qapi-util.c > index e9b266bb70..6180957edb 100644 > --- a/qapi/qapi-util.c > +++ b/qapi/qapi-util.c > @@ -21,8 +21,8 @@ const char *qapi_enum_lookup(const QEnumLookup *lookup, int val) > return lookup->array[val]; > } > > -int qapi_enum_parse(const QEnumLookup *lookup, const char *buf, > - int def, Error **errp) > +int qapi_enum_parse_full(const QEnumLookup *lookup, const char *buf, > + int def, bool ignore_case, Error **errp) > { > int i; > > @@ -31,8 +31,14 @@ int qapi_enum_parse(const QEnumLookup *lookup, const char *buf, > } > > for (i = 0; i < lookup->size; i++) { > - if (!strcmp(buf, lookup->array[i])) { > - return i; > + if (ignore_case) { > + if (!strcasecmp(buf, lookup->array[i])) { > + return i; > + } > + } else { > + if (!strcmp(buf, lookup->array[i])) { > + return i; > + } > } > } > > @@ -40,6 +46,12 @@ int qapi_enum_parse(const QEnumLookup *lookup, const char *buf, > return def; > } > > +int qapi_enum_parse(const QEnumLookup *lookup, const char *buf, > + int def, Error **errp) > +{ > + return qapi_enum_parse_full(lookup, buf, def, false, errp); > +} > + > /* > * Parse a valid QAPI name from @str. > * A valid name consists of letters, digits, hyphen and underscore. What's "full" about qapi_enum_parse_full()? Existing parse_unit_full() differs from parse_uint() in that it always consumes the full string. Hmm. Do we expect to pass anything but literal false or true to @ignore_case? If not, then a more convenient interface is two functions, just like we have strcmp() and strcasecmp(). They could still call a common internal helper to avoid code duplication. Consider passing strcmp, strcasecmp instead of false, true to it then.