Fix major performance problem due to haywire pattern matching on very
large strings