On 2 September 2014 21:04, Eric Blake <ebl...@redhat.com> wrote:
> On 09/02/2014 04:24 AM, Peter Maydell wrote:
>> +
>> +    if (host == NULL || strlen(host) == 0) {
>> +        host = "localhost";
>> +    }
>> +    if (port == NULL || strlen(port) == 0) {
>> +        error_setg(errp, "chardev: udp: remote port not specified");
>
> In the common case of these strings being non-empty, you end up having
> to hunt for the end of the string only to then throw that information
> away.  Rather than 'strlen(foo) == 0)', it's slightly faster to check
> '*foo' for being a non-NUL byte.

I think that's a lot less clear to read, and in fact the
compiler is entirely capable of turning "strlen(x) == 0"
into "check whether *x is 0":

mnementh$ cat /tmp/zz9.c
#include <string.h>

int is_empty_string(const char *s)
{
    return (strlen(s) == 0);
}
mnementh$ gcc -g -Wall -O2 -o /tmp/zz9.o -c /tmp/zz9.c
mnementh$ objdump --disassemble /tmp/zz9.o

/tmp/zz9.o:     file format elf32-i386


Disassembly of section .text:

00000000 <is_empty_string>:
   0:    8b 44 24 04              mov    0x4(%esp),%eax
   4:    80 38 00                 cmpb   $0x0,(%eax)
   7:    0f 94 c0                 sete   %al
   a:    0f b6 c0                 movzbl %al,%eax
   d:    c3                       ret

thanks
-- PMM

Reply via email to