3 ms·
Go's regexp package even exposes a routine for this: https://pkg.go.dev/regexp#Regexp.LiteralPrefix https://pkg.go.dev/regexp#Regexp.LiteralPrefix It's been a
by burntsushi 5y ago
Go's regexp package even exposes a routine for this: https://pkg.go.dev/regexp#Regexp.LiteralPrefix https://pkg.go.dev/regexp#Regexp.LiteralPrefix
It's been a while since I've looked at the source code, but it is almost certainly already doing basic prefix literal optimizations.
The more advanced literal optimizations come into play when every match must start with one of a few possible characters or strings. Then you get into things like Aho-Corasick or fancy SIMD algorithms (Teddy).
- tedunangst 5y agoOh, regarding rure go, the bugs note about using int is inaccurate. Go spec says max length of any object can be stored in int. You can't build a too big haystack.
- burntsushi 5y agoAh that's right! Nice catch, thanks.