[cfe-dev] CLang and UTF BOM characters

Daniel Dunbar daniel at zuster.org
Mon Oct 18 16:14:51 PDT 2010


On Sun, Oct 17, 2010 at 12:06 PM, David Chisnall <csdavec at swan.ac.uk> wrote:
> On 17 Oct 2010, at 19:53, Ruben Van Boxem wrote:
>
>> Why not? I'm not big on C here, just use it here out of brevity. There should be no difference between the two in this use case IMHO?
>
>
> strncmp() will compare up to n characters, stopping when a null byte is encountered.  memcmp() will compare exactly n characters.  Both cases will stop if there is a difference between the two inputs.  A null bytes in the input stream is a) not a terminator, and b) not valid.  Therefore, testing for it is less efficient.

Mostly pointless pedanticism, but memcmp() is also allowed to read
ahead more than strncmp() is, which makes it slightly more
optimizable.

 - Daniel

> Of course, when testing three bytes performance is not likely to be an issue and (hopefully) the lower standard library calls pass will turn it into a trivial comparison, avoiding the function call.  Avoiding the test for null will have almost no impact on performance.  Using strncmp(), however, does impact code readability because it implies to the reader that at least one of the arguments is a null-terminated C string.
>
> David
>
> -- Sent from my Cray X1
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>




More information about the cfe-dev mailing list