Use the Regex source generator in the AlphaRouteConstraint. by eerhardt · Pull Request #44770 · dotnet/aspnetcore (original) (raw)

I switched the pattern from a-z with IgnoreCase to A-Za-z because the source generator generates better code with that pattern.

Is there a regex issue for that? @stephentoub (This is invariant culture)

There's no "issue"... it's by design: [a-z] (IgnoreCase) and [A-Za-z] are functionally different (ever so slightly)... the former recognizes the Kelvin sign as being case-equivalent to k/K. So the difference in the code gen is that when using [A-Za-z], we generate:

char.IsAsciiLetter(slice[iteration])

whereas with [a-z] under IgnoreCase, we generate:

((ch = slice[iteration]) < 128 ? ("\0\0\0\0\ufffe\u07ff\ufffe\u07ff"[ch >> 4] & (1 << (ch & 0xF))) != 0 : RegexRunner.CharInClass((char)ch, "\0\u0006\0A[a{KÅ"))

With more work and analysis we could optimize the latter to instead be:

char.IsAsciiLetter(ch = slice[iteration]) || ch == '\u212A'

or something like that, but it still has the additional check.

(I think it's fine to use [A-Za-z] here, though. Technically it's a breaking change, but I struggle to imagine anyone relying on Kelvin being equal to k or K in routes :)