7 ms·
Regular expressions are virtually always easier to read because they're so much terser. I unroll a fair bit regular expressions because they are typically an or
by marginalia_nu 4y ago
Regular expressions are virtually always easier to read because they're so much terser. I unroll a fair bit regular expressions because they are typically an order of magnitude slower, and it's almost never an upgrade in readability.
Compare for example something trivial like
[a-f0-9]{32}
with its unrolled form
public boolean hashTest(String path) {
int runLength = 0;
int minLength = 32;
if (path.length() <= minLength + 2)
return false;
for (int i = 0; i < path.length(); i++) {
int c = path.charAt(i);
if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f')) {
runLength++;
}
else if (runLength >= minLength) {
return true;
}
else {
runLength = 0;
}
}
return runLength >= minLength;
}