[cfe-dev] CLang and UTF BOM characters

David Chisnall csdavec at swan.ac.uk
Sun Oct 17 12:06:54 PDT 2010


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.

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



More information about the cfe-dev mailing list