Repository navigation
Description
This issue has now been turned into an API proposal for the missing GeneratedRegex constructors that take in a cultureName. The previous description of the issue has been moved bellow
Background and motivation
Regex engines are able to do case insensitive searches when you use RegexOptions.IgnoreCase or when you use a pattern that signals that it is case-insensitive, like: (?i)abc. For better performance, our engines translate the passed in patterns into their case-sensitive equivalence so that we can benefit of some optimizations like using vectorized search, for example, a pattern (?i)abc will be translated into the equivalent [Aa][Bb][Cc].
Case conversions are not standard across all cultures, which means that when making that translation, it matters a lot which culture is used in order to produce the translation. For most of our engines (interpreted, Compiled, nonBacktracking) this translation happens at runtime, and will use the CultureInfo.CurrentCulture at runtime in order to perform this translation. The remaining engine, however, is different. Because it is the source generated engine, it means that this translation will happen at build-time of the application, which means that the culture won't necessarily be the same as the one you have at runtime. Today, the source generated engine will use the Culture at build-time to perform the translation, and there is no way to select a different culture, which is problematic, since even if you are able to change the culture of your machine if you are building the application, this won't be possible if the code gets built in a build server. This proposal is to add an extra string parameter to the GeneratedRegex attribute so that you can optionally select which culture should be used to perform the translation.
API Proposal
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
public sealed class GeneratedRegexAttribute : System.Attribute
{
public GeneratedRegexAttribute([StringSyntax(StringSyntaxAttribute.Regex)] string pattern) { }
public GeneratedRegexAttribute([StringSyntax(StringSyntaxAttribute.Regex, "options")] string pattern, RegexOptions options) { }
+ public GeneratedRegexAttribute([StringSyntax(StringSyntaxAttribute.Regex, "options")] string pattern, RegexOptions options, string cultureName) { }
public GeneratedRegexAttribute([StringSyntax(StringSyntaxAttribute.Regex, "options")] string pattern, RegexOptions options, int matchTimeoutMilliseconds) { }
+ public GeneratedRegexAttribute([StringSyntax(StringSyntaxAttribute.Regex, "options")] string pattern, RegexOptions options, int matchTimeoutMilliseconds, string cultureName) { }
+ public string CultureName { get; }
}API Usage
public partial class Foo
{
[GeneratedRegex(@"(?i)abc", RegexOptions.None, "en-US")]
public static partial Regex ThreeParameterRegex();
[GeneratedRegex(@"abc", RegexOptions.IgnoreCase, /*matchTimeoutInMilliseconds*/ 5000, "en-US")]
public static partial Regex FourParameterRegex();
}Draft PR with the implementation
Original Issue Description
If a developer specifies RegexOptions.IgnoreCase to a regular expression (or uses the inline ignore casing option in the expression), the implementation(s) then need to do casing operations to determine whether things match. That's implemented today in two parts:
- At construction time, values are lowercased in the pattern.
- At match time, the text being compared is lowercased.
If the developer specifies RegexOptions.CultureInvariant, that's fine, as both (1) and (2) will be done with CultureInfo.InvariantCulture. But if CultureInvariant isn't specified, it's possible that the CultureInfo.CurrentCulture at the time of (1) will differ from the time of (2), in which case the lowercasing can be performed with different cultures, leading to problems. Note that this really only affects the instance methods on Regex. The static methods on Regex access a cache of Regex instances, and the culture used to create the Regex instances is included in the key to that cache. Also note that the docs talk about the current culture being used, but they don't specify current "when".
We have a few options to fix this:
- Keep the current discrepancy. Using a different culture to match from the culture when the Regex was created is buggy.
- Use the culture that was present at the time of construction. This would mean the Regex would cache that CultureInfo instance (or TextInfo) instance, and use it for match operations rather than having those match operations each consult CultureInfo.CurrentCulture. (This could even have a small positive impact on perf, as it means we'd be accessing a field rather than a thread static on each match operation.)
- Use the culture that's present at the time of match. This means we couldn't do at construction time many of the optimizations that are currently done, and we'd need to come up with some caching scheme where we lazily compute the necessary data and find the right set of data to use at match time. In the extreme this could include maintaining a cache per culture of all the regex's state and then finding the right state to use as part of the match, essentially a limited version of what we do in the static methods. There is very likely to be a negative perf impact from this.
We should investigate whether the discrepancy actually matters, e.g. in practice, do all regexes where this could matter:
- not use IgnoreCase
- use InvariantCulture
- only ever access it from the same culture in which it was constructed or where casing rules are the same as the culture in which it was constructed
- have a pattern that's not susceptible to issues (e.g.
is)
My gut is we should do (2), primarily as it's explainable and simple. We're already doing some such computation at construction time, so it's unlikely a robust dependency could have been taken on the use of the culture only at match time.
If we do (2), there are then additional complications for the regex source generator. All the computation that normally happens in the Regex constructor happens at build time with the regex source generator, which means CultureInfo.CurrentCulture is determined at the time of build, whatever the culture is for the process doing the build / generating the source code. We have a few options there:
- Keep that design, using whatever the current culture is at build. That's probably not ideal.
- Always use invariant culture in the source generator, regardless of whether RegexOptions.CultureInvariant is specified.
- Fail to generate code if casing might be required (either RegexOptions.IgnoreCase or the inline
ioption is specified somewhere in the expression) and RegexOptions.CultureInvariant wasn't specified - Add overloads to the RegexGenerator attribute constructor that accept a
string cultureName, which is then used. Either get rid of the other overloads, or have them do one of the other options.
I'm tempted to say we should do (4), and if you use a ctor that doesn't take a culture, fall back to (3).
Activity
danmoseley commentedon Sep 22, 2021
cc @veanes @olsaarik as this is of course relevant to all engines.
joperezr commentedon Jul 18, 2022
With all of the IgnoreCase work that happened during this release, some of the behavior here changed, so some of the statements in the original description of the issue are no longer accurate. Here is the current state of things:
abcto a case-sensitive[Aa][Bb][Cc].RegexOptions.CultureInvariantwas passed.RegexOptions.CultureInvariantwas passed.I think the only action item here we may want to consider (and for which it is probably already late in case of 7.0) would be to add an option to the RegexGenerator attribute so it could optionally also take a particular culture to be used for computing the case equivalences.
I'm punting this to Future for now, as having this extra option would be considered new API, and we are currently not taking any more new-APIs for 7.0.
stephentoub commentedon Jul 18, 2022
I think we need to decide now, in .NET 7, whether the source generator defaults to invariant. Additional API could be used to override that.
Having the generated code be based on whatever ambient culture the roslyn process is running as is unfortunate.
stephentoub commentedon Jul 18, 2022
(And if we need to get in an additional ctor to the attribute in 7 in order to ensure a good story, we can do that.)
joperezr commentedon Jul 18, 2022
TBH, the case mappings we use are basically the same across all cultures except for a handful of characters (all related to
iand turkish i's). For that reason, I think it is fine if we want to default to current culture, and if someone really want it to be invariant they can control it with the surface we have today as they can pass inRegexOptions.CultureInvariant.17 remaining items
jaredpar commentedon Aug 3, 2022
@stephentoub
100% agree, this is my main concern as well. It's a very subtle, but significant break for determinism to do this. Enough so that I've added this API, and a few others, to the list of "banned APIs" we'll be checking generators against with an analyzer we have in the works. Essentially APIs we know to be dangerous in generators / analyzers that we want to push customers to stop using because it impacts these type of scenarios.
This seems very reasonable to me.
joperezr commentedon Aug 3, 2022
Makes sense, I will send out a PR soon that addresses all @stephentoub's points above. @jaredpar that means there is no need on adding this API to the "banned APIs" list 😄
jaredpar commentedon Aug 3, 2022
Oh the culture APIs are still being added to the ban list for reference of future customers. 😄
There are a lot of APIs that are effectively banned in the compiler and an analyzer is the best way to communicate that to generator / analyzer authors. Some of the banned APIs are more obvious, like don't do file IO, but others like culture are more subtle. I hadn't even thought of culture as a specific problem until reading this thread.
jzabroski commentedon Aug 3, 2022
[-]Determine if/how to handle CultureInfo better in Regex (and RegexGenerator)[/-][+][API Proposal] Add cultureName constructors to GeneratedRegex[/+]bartonjs commentedon Aug 9, 2022
Video
namespace System.Text.RegularExpressions; [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)] public sealed class GeneratedRegexAttribute : System.Attribute { public GeneratedRegexAttribute([StringSyntax(StringSyntaxAttribute.Regex)] string pattern) { } public GeneratedRegexAttribute([StringSyntax(StringSyntaxAttribute.Regex, "options")] string pattern, RegexOptions options) { } + public GeneratedRegexAttribute([StringSyntax(StringSyntaxAttribute.Regex, "options")] string pattern, RegexOptions options, string cultureName) { } public GeneratedRegexAttribute([StringSyntax(StringSyntaxAttribute.Regex, "options")] string pattern, RegexOptions options, int matchTimeoutMilliseconds) { } + public GeneratedRegexAttribute([StringSyntax(StringSyntaxAttribute.Regex, "options")] string pattern, RegexOptions options, int matchTimeoutMilliseconds, string cultureName) { } + public string CultureName { get; } }