In OPENJDK's StringSupport class, there are following codes checking if there could be a '\0' byte in a long value:
private static final long HIMAGIC_FOR_BYTES = 0x8080_8080_8080_8080L;
private static final long LOMAGIC_FOR_BYTES = 0x0101_0101_0101_0101L;
static boolean mightContainZeroByte(long l) {
return ((l - LOMAGIC_FOR_BYTES) & (~l) & HIMAGIC_FOR_BYTES) != 0;
}
This method is excellent; it can improve the speed of finding string terminators by 8 times compared to the method of matching byte by byte.
I'm wondering whether this kind of magic can also be applied to other byte searches rather than '\0', even for matching multiple bytes like CR LF?