qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Anthony Liguori <aliguori@us.ibm.com>
To: Peter Maydell <peter.maydell@linaro.org>
Cc: Luiz Capitulino <lcapitulino@redhat.com>,
	qemu-devel@nongnu.org, Markus Armbruster <armbru@redhat.com>
Subject: Re: [Qemu-devel] [PATCH 1/2] qemu-opts: introduce a function to compare option names
Date: Wed, 25 Jul 2012 12:33:44 -0500	[thread overview]
Message-ID: <87y5m721af.fsf@codemonkey.ws> (raw)
In-Reply-To: <CAFEAcA-idFZcwrNZBXmsiG9w=oc3CiU7ud8QyXBFeoKCqja-=Q@mail.gmail.com>

Peter Maydell <peter.maydell@linaro.org> writes:

> On 25 July 2012 17:25, Anthony Liguori <aliguori@us.ibm.com> wrote:
>> We don't use the standard C functions for conversion because we don't want to
>> depend on the user's locale.  All option names in QEMU are en_US in plain ASCII.
>>
>> Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
>> ---
>>  qemu-option.c |   53 +++++++++++++++++++++++++++++++++++++++++++++--------
>>  qemu-option.h |    2 ++
>>  2 files changed, 47 insertions(+), 8 deletions(-)
>>
>> diff --git a/qemu-option.c b/qemu-option.c
>> index 8334190..6494c99 100644
>> --- a/qemu-option.c
>> +++ b/qemu-option.c
>> @@ -89,6 +89,43 @@ const char *get_opt_value(char *buf, int buf_size, const char *p)
>>      return p;
>>  }
>>
>> +static int opt_tolower(int ch)
>
> This isn't actually doing a pure tolower() operation.
> opt_canonicalize() is a bit long though, perhaps.
> I guess it's static so not a big deal.

Yeah.

>
>  +{
>> +    if (ch >= 'A' && ch <= 'Z') {
>> +        return 'a' + (ch - 'A');
>> +    } else if (ch == '_') {
>> +        return '-';
>> +    }
>> +
>> +    return ch;
>> +}
>> +
>> +int qemu_opt_name_cmp(const char *lhs, const char *rhs)
>> +{
>> +    int i;
>> +
>> +    g_assert(lhs && rhs);
>> +
>> +    for (i = 0; lhs[i] && rhs[i]; i++) {
>> +        int ret;
>> +
>> +        ret = opt_tolower(lhs[i]) - opt_tolower(rhs[i]);
>
> This is not in line with the return value that the C library
> strcmp() would return. C99 7.21.4 says "The sign of a nonzero
> value returned by the comparison functions memcmp, strcmp,
> and strncmp is determined by the sign of the difference between
> the values of the first pair of characters (both interpreted
> as unsigned char) that differ in the objects being compared."
> This code will use whatever the signed/unsignedess of plain
> 'char' is.
>
> (None of the callers use the sign of the return value so this
> is something of a nitpick.)

Sorry, how is this wrong?

This returns:

strcmp("a", "b") -> -1
qemu_opt_name_cmp("a", "b") -> -1

>> +        if (ret < 0) {
>> +            return -1;
>> +        } else if (ret > 0) {
>> +            return 1;
>> +        }
>> +    }
>> +
>> +    if (!lhs[i] && rhs[i]) {
>> +        return -1;
>> +    } else if (lhs[i] && !rhs[i]) {
>> +        return 1;
>> +    }
>
> If you made the for() loop into a do..while so that we
> execute the loop body for the 'final NUL' case you could
> avoid having this pair of checks, I think (and you can
> make the loop termination case just '!lhs[i]' since if
> we get past the 'mismatch' exits we've definitely got
> to the end of both strings and can return true).

I can poke around but not convinced it will result in better code.  I
must admit that I prefer explicit handling of edge cases like this.

>
>> +
>> +    return 0;
>> +}
>
>> --- a/qemu-option.h
>> +++ b/qemu-option.h
>> @@ -141,4 +141,6 @@ int qemu_opts_print(QemuOpts *opts, void *dummy);
>>  int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func, void *opaque,
>>                        int abort_on_failure);
>>
>> +int qemu_opt_name_cmp(const char *lhs, const char *rhs);
>
> No documentation comment?

Good point.

Regards,

Anthony Liguori

>
> -- PMM

  reply	other threads:[~2012-07-25 19:21 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-07-25 16:25 [Qemu-devel] [PATCH 0/2] Allow '-' or '_' for all QemuOpts Anthony Liguori
2012-07-25 16:25 ` [Qemu-devel] [PATCH 1/2] qemu-opts: introduce a function to compare option names Anthony Liguori
2012-07-25 16:45   ` Eric Blake
2012-07-25 16:46     ` Eric Blake
2012-07-25 17:34       ` Anthony Liguori
2012-07-25 16:53   ` Peter Maydell
2012-07-25 17:33     ` Anthony Liguori [this message]
2012-07-25 18:31       ` Peter Maydell
2012-07-27  7:44   ` Markus Armbruster
2012-07-27 11:14     ` Peter Maydell
2012-07-25 16:25 ` [Qemu-devel] [PATCH 2/2] qemu-config: convert all QemuOpts to use dashes instead of underscores Anthony Liguori
2012-07-25 17:32   ` Luiz Capitulino
2012-07-25 18:20     ` Anthony Liguori
2012-07-25 18:58       ` Peter Maydell
2012-07-25 19:02         ` Luiz Capitulino
2012-07-25 19:02           ` Peter Maydell
2012-07-25 19:09             ` [Qemu-devel] Changing qemu's -help output Luiz Capitulino
2012-07-25 21:21               ` [Qemu-devel] [libvirt] " Eric Blake
2012-07-27  7:45       ` [Qemu-devel] [PATCH 2/2] qemu-config: convert all QemuOpts to use dashes instead of underscores Markus Armbruster
2012-07-27 11:10   ` Andreas Färber
2012-07-25 17:34 ` [Qemu-devel] [PATCH 0/2] Allow '-' or '_' for all QemuOpts Luiz Capitulino
2012-07-27  8:39 ` Markus Armbruster

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87y5m721af.fsf@codemonkey.ws \
    --to=aliguori@us.ibm.com \
    --cc=armbru@redhat.com \
    --cc=lcapitulino@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).