When I was looking to code a faster strlen
in C (than the one which check byte by byte) I found this macro:
#define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080)
This macro reads 4 bytes and returns (1) when it finds at least one NUL byte. Otherwise it returns (0).
I wonder if it's possible to use the same technique to find any char of the ascii table (I prefer not to use a byte by byte loop).
I tried a lot of combinations and the best I could do is this:
// in this example I wanted to find a '#'
int32_t detectsharp(int32_t c) {
c = ~(c - 0x24242424) & ~c;
return ((c - 0x01010101) & ~c & 0x80808080);
}
But it doesn't work with 0x22222222
(""""
) or things like 0x24212121
($!!!
).