Bruno Haible <[EMAIL PROTECTED]> writes:

> No; ISO C 99 section 7.21.4 says that when byte strings are compared the
> elements are considered as 'unsigned char' values. Why risk bugs when the
> approach is very simple: after fetching any 'char' from any of the strings,
> cast it to 'unsigned char'.

Unfortunately that simple approach isn't portable according to the C99
rules.  The 'char' type can have padding bits, which means that code
like this:

     char const *needle = (char const *) needle_start;
     char const *haystack = (char const *) haystack_start;
     if ((unsigned char) *needle == (unsigned char) *haystack) ...

might ignore some of the bits in the storage addressed by A and B.
To get a proper memcmp-style comparison one must do something like this:

     unsigned char const *needle = (unsigned char const *) needle_start;
     unsigned char const *haystack = (unsigned char const *) haystack_start;
     if (*needle == *haystack) ...

(In the latter version, the casts are needed only because this code is
intended to be portable to C++.)


Reply via email to