6 ms·
> Casting a u_int32_t over a 4-byte string (like an SMTP verb) to get a value you can switch() on. This is awesome beyond words. If I stumbled across this in
by brl 16y ago
> Casting a u_int32_t over a 4-byte string (like an SMTP verb) to get a value you can switch() on.
This is awesome beyond words. If I stumbled across this in the wild I would flip flop between awe and disgust until my head exploded.
Let me guess, "SMTP verb" is not a hypothetical example?
- jallmann 16y agoThis is also the basis for the fourcc's used in multimedia. Checking them only requires an integer compare.
- defen 16y agoNginx does this too under certain circumstances...check out the ngx_strN_cmp macros in ngx_http_parse.c. (where N is an integer from 3..9)
- mansr 16y agoThere are at least three common mistakes involving tricks like this: 1. Alignment. Many CPUs will trap (or worse) if accessing a 32-bit quantity at a non-aligned address. 2. Endianness. Needless to say, the 32-bit value read for a given string depends on the machine endianness. 3. Aliasing. Casting between different pointer types can result in a violation of the C aliasing rules and, with a little bad luck, incorrect results.
- jallmann 16y agoGood to see you on hn.