Skip to content

Use properties for some [GeneratedRegex]s #109118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ public DesignerVerb(string text, EventHandler handler) : this(text, handler, Sta
/// </summary>
public DesignerVerb(string text, EventHandler handler, CommandID startCommandID) : base(handler, startCommandID)
{
Properties["Text"] = text == null ? null : GetParameterReplacementRegex().Replace(text, "");
Properties["Text"] = text == null ? null : ParameterReplacementRegex.Replace(text, "");
}

[GeneratedRegex(@"\(\&.\)")]
private static partial Regex GetParameterReplacementRegex();
private static partial Regex ParameterReplacementRegex { get; }

/// <summary>
/// Gets or sets the description of the menu item for the verb.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2234,7 +2234,7 @@ internal static bool IsAssemblyFriendOfSerialization(Assembly assembly)
string internalsVisibleAttributeAssemblyName = internalsVisibleAttribute.AssemblyName;

if (internalsVisibleAttributeAssemblyName.Trim().Equals("System.Runtime.Serialization") ||
Globals.FullSRSInternalsVisibleRegex().IsMatch(internalsVisibleAttributeAssemblyName))
Globals.FullSRSInternalsVisibleRegex.IsMatch(internalsVisibleAttributeAssemblyName))
{
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ internal static Type TypeOfHashtable
public const string NullObjectId = null;
public const string FullSRSInternalsVisiblePattern = @"^[\s]*System\.Runtime\.Serialization[\s]*,[\s]*PublicKey[\s]*=[\s]*(?i:00240000048000009400000006020000002400005253413100040000010001008d56c76f9e8649383049f383c44be0ec204181822a6c31cf5eb7ef486944d032188ea1d3920763712ccb12d75fb77e9811149e6148e5d32fbaab37611c1878ddc19e20ef135d0cb2cff2bfec3d115810c3d9069638fe4be215dbf795861920e5ab6f7db2e2ceef136ac23d5dd2bf031700aec232f6c6b1c785b4305c123b37ab)[\s]*$";
[GeneratedRegex(FullSRSInternalsVisiblePattern)]
public static partial Regex FullSRSInternalsVisibleRegex();
public static partial Regex FullSRSInternalsVisibleRegex { get; }
public const char SpaceChar = ' ';
public const char OpenBracketChar = '[';
public const char CloseBracketChar = ']';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1342,7 +1342,7 @@ internal sealed partial class StringFacetsChecker : FacetsChecker
{ //All types derived from string & anyURI

[GeneratedRegex("^([a-zA-Z]{1,8})(-[a-zA-Z0-9]{1,8})*$", RegexOptions.ExplicitCapture)]
private static partial Regex LanguageRegex();
private static partial Regex LanguageRegex { get; }

internal override Exception? CheckValueFacets(object value, XmlSchemaDatatype datatype)
{
Expand Down Expand Up @@ -1456,7 +1456,7 @@ private static bool MatchEnumeration(string value, ArrayList enumeration, XmlSch
{
return new XmlSchemaException(SR.Sch_EmptyAttributeValue, string.Empty);
}
if (!LanguageRegex().IsMatch(s))
if (!LanguageRegex.IsMatch(s))
{
return new XmlSchemaException(SR.Sch_InvalidLanguageId, string.Empty);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ internal sealed partial class SourceInfo
//a[ia]
//((global::System.Xml.Serialization.XmlSerializerNamespaces)p[0])
[GeneratedRegex("([(][(](?<t>[^)]+)[)])?(?<a>[^[]+)[[](?<ia>.+)[]][)]?")]
private static partial Regex Regex1();
private static partial Regex Regex1 { get; }

//((global::Microsoft.CFx.Test.Common.TypeLibrary.IXSType_9)o), @"IXSType_9", @"", true, true);
[GeneratedRegex("[(][(](?<cast>[^)]+)[)](?<arg>[^)]+)[)]")]
private static partial Regex Regex2();
private static partial Regex Regex2 { get; }

private static readonly Lazy<MethodInfo> s_iListGetItemMethod = new Lazy<MethodInfo>(
() =>
Expand Down Expand Up @@ -69,7 +69,7 @@ public void Load(Type? elementType)
[RequiresUnreferencedCode("calls LoadMemberAddress")]
private void InternalLoad(Type? elementType, bool asAddress = false)
{
Match match = Regex1().Match(Arg);
Match match = Regex1.Match(Arg);
if (match.Success)
{
object varA = ILG.GetVariable(match.Groups["a"].Value);
Expand Down Expand Up @@ -190,7 +190,7 @@ private void InternalLoad(Type? elementType, bool asAddress = false)
}
else
{
match = Regex2().Match(Source);
match = Regex2.Match(Source);
if (match.Success)
{
Debug.Assert(match.Groups["arg"].Value == Arg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2708,10 +2708,10 @@ private void WriteSourceBeginTyped(string source)
}

[GeneratedRegex("(?<locA1>[^ ]+) = .+EnsureArrayIndex[(](?<locA2>[^,]+), (?<locI1>[^,]+),[^;]+;(?<locA3>[^[]+)[[](?<locI2>[^+]+)[+][+][]]")]
private static partial Regex EnsureArrayIndexRegex();
private static partial Regex EnsureArrayIndexRegex { get; }

[GeneratedRegex("(?<a>[^[]+)[[](?<ia>.+)[]]")]
private static partial Regex P0Regex();
private static partial Regex P0Regex { get; }

private void WriteSourceBegin(string source)
{
Expand All @@ -2733,7 +2733,7 @@ private void WriteSourceBegin(string source)
return;
}
// a_0_0 = (global::System.Object[])EnsureArrayIndex(a_0_0, ca_0_0, typeof(global::System.Object));a_0_0[ca_0_0++]
Match match = EnsureArrayIndexRegex().Match(source);
Match match = EnsureArrayIndexRegex.Match(source);
if (match.Success)
{
Debug.Assert(match.Groups["locA1"].Value == match.Groups["locA2"].Value);
Expand Down Expand Up @@ -2779,7 +2779,7 @@ private void WriteSourceBegin(string source)
}

// p[0]
match = P0Regex().Match(source);
match = P0Regex.Match(source);
if (match.Success)
{
System.Diagnostics.Debug.Assert(CodeGenerator.GetVariableType(ilg.GetVariable(match.Groups["a"].Value)).IsArray);
Expand Down Expand Up @@ -2825,7 +2825,7 @@ private void WriteSourceEnd(string source, Type elementType, Type stackType)
return;
}
// a_0_0 = (global::System.Object[])EnsureArrayIndex(a_0_0, ca_0_0, typeof(global::System.Object));a_0_0[ca_0_0++]
Match match = EnsureArrayIndexRegex().Match(source);
Match match = EnsureArrayIndexRegex.Match(source);
if (match.Success)
{
object oVar = ilg.GetVariable(match.Groups["locA1"].Value);
Expand Down Expand Up @@ -2861,7 +2861,7 @@ private void WriteSourceEnd(string source, Type elementType, Type stackType)
return;
}
// p[0]
match = P0Regex().Match(source);
match = P0Regex.Match(source);
if (match.Success)
{
Type varType = CodeGenerator.GetVariableType(ilg.GetVariable(match.Groups["a"].Value));
Expand Down Expand Up @@ -3528,19 +3528,19 @@ private static void WriteLocalDecl(string variableName, SourceInfo initValue)
}

[GeneratedRegex("UnknownNode[(]null, @[\"](?<qnames>[^\"]*)[\"][)];")]
private static partial Regex UnknownNodeNullAnyTypeRegex();
private static partial Regex UnknownNodeNullAnyTypeRegex { get; }

[GeneratedRegex("UnknownNode[(][(]object[)](?<o>[^,]+), @[\"](?<qnames>[^\"]*)[\"][)];")]
private static partial Regex UnknownNodeObjectEmptyRegex();
private static partial Regex UnknownNodeObjectEmptyRegex { get; }

[GeneratedRegex("UnknownNode[(][(]object[)](?<o>[^,]+), null[)];")]
private static partial Regex UnknownNodeObjectNullRegex();
private static partial Regex UnknownNodeObjectNullRegex { get; }

[GeneratedRegex("UnknownNode[(][(]object[)](?<o>[^)]+)[)];")]
private static partial Regex UnknownNodeObjectRegex();
private static partial Regex UnknownNodeObjectRegex { get; }

[GeneratedRegex("paramsRead\\[(?<index>[0-9]+)\\]")]
private static partial Regex ParamsReadRegex();
private static partial Regex ParamsReadRegex { get; }

private void ILGenElseString(string elseString)
{
Expand All @@ -3555,7 +3555,7 @@ private void ILGenElseString(string elseString)
new Type[] { typeof(object), typeof(string) }
)!;
// UnknownNode(null, @":anyType");
Match match = UnknownNodeNullAnyTypeRegex().Match(elseString);
Match match = UnknownNodeNullAnyTypeRegex.Match(elseString);
if (match.Success)
{
ilg.Ldarg(0);
Expand All @@ -3565,7 +3565,7 @@ private void ILGenElseString(string elseString)
return;
}
// UnknownNode((object)o, @"");
match = UnknownNodeObjectEmptyRegex().Match(elseString);
match = UnknownNodeObjectEmptyRegex.Match(elseString);
if (match.Success)
{
ilg.Ldarg(0);
Expand All @@ -3577,7 +3577,7 @@ private void ILGenElseString(string elseString)
return;
}
// UnknownNode((object)o, null);
match = UnknownNodeObjectNullRegex().Match(elseString);
match = UnknownNodeObjectNullRegex.Match(elseString);
if (match.Success)
{
ilg.Ldarg(0);
Expand All @@ -3589,7 +3589,7 @@ private void ILGenElseString(string elseString)
return;
}
// "UnknownNode((object)o);"
match = UnknownNodeObjectRegex().Match(elseString);
match = UnknownNodeObjectRegex.Match(elseString);
if (match.Success)
{
ilg.Ldarg(0);
Expand All @@ -3603,7 +3603,7 @@ private void ILGenElseString(string elseString)
}
private void ILGenParamsReadSource(string paramsReadSource)
{
Match match = ParamsReadRegex().Match(paramsReadSource);
Match match = ParamsReadRegex.Match(paramsReadSource);
if (match.Success)
{
ilg.Ldloca(ilg.GetLocal("paramsRead"));
Expand All @@ -3616,7 +3616,7 @@ private void ILGenParamsReadSource(string paramsReadSource)
}
private void ILGenParamsReadSource(string paramsReadSource, bool value)
{
Match match = ParamsReadRegex().Match(paramsReadSource);
Match match = ParamsReadRegex.Match(paramsReadSource);
if (match.Success)
{
ilg.Ldloca(ilg.GetLocal("paramsRead"));
Expand Down
8 changes: 4 additions & 4 deletions src/libraries/System.Private.Xml/src/System/Xml/XmlConvert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public partial class XmlConvert
return name;
}

Regex.ValueMatchEnumerator en = DecodeCharRegex().EnumerateMatches(name.AsSpan(underscorePos));
Regex.ValueMatchEnumerator en = DecodeCharRegex.EnumerateMatches(name.AsSpan(underscorePos));
int matchPos = -1;
if (en.MoveNext())
{
Expand Down Expand Up @@ -185,7 +185,7 @@ public partial class XmlConvert
IEnumerator? en = null;
if (underscorePos >= 0)
{
mc = EncodeCharRegex().Matches(name, underscorePos);
mc = EncodeCharRegex.Matches(name, underscorePos);
en = mc.GetEnumerator();
}

Expand Down Expand Up @@ -281,10 +281,10 @@ public partial class XmlConvert
private const int EncodedCharLength = 7; // ("_xFFFF_".Length);

[GeneratedRegex("_[Xx][0-9a-fA-F]{4}(?:_|[0-9a-fA-F]{4}_)")]
private static partial Regex DecodeCharRegex();
private static partial Regex DecodeCharRegex { get; }

[GeneratedRegex("(?<=_)[Xx][0-9a-fA-F]{4}(?:_|[0-9a-fA-F]{4}_)")]
private static partial Regex EncodeCharRegex();
private static partial Regex EncodeCharRegex { get; }

private static int FromHex(char digit)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2434,10 +2434,8 @@ public static IEnumerable<object[]> SystemTimeZonesTestData()
}
}

private const string IanaAbbreviationPattern = @"^(?:[A-Z][A-Za-z]+|[+-]\d{2}|[+-]\d{4})$";

[GeneratedRegex(IanaAbbreviationPattern)]
private static partial Regex IanaAbbreviationRegex();
[GeneratedRegex(@"^(?:[A-Z][A-Za-z]+|[+-]\d{2}|[+-]\d{4})$")]
private static partial Regex IanaAbbreviationRegex { get; }

// UTC aliases per https://github.com/unicode-org/cldr/blob/master/common/bcp47/timezone.xml
// (This list is not likely to change.)
Expand Down Expand Up @@ -2487,10 +2485,10 @@ public static void TimeZoneDisplayNames_Unix(TimeZoneInfo timeZone)
else
{
// For other time zones, match any valid IANA time zone abbreviation, including numeric forms
Assert.True(IanaAbbreviationRegex().IsMatch(timeZone.StandardName),
$"Id: \"{timeZone.Id}\", StandardName should have matched the pattern @\"{IanaAbbreviationPattern}\", Actual StandardName: \"{timeZone.StandardName}\"");
Assert.True(IanaAbbreviationRegex().IsMatch(timeZone.DaylightName),
$"Id: \"{timeZone.Id}\", DaylightName should have matched the pattern @\"{IanaAbbreviationPattern}\", Actual DaylightName: \"{timeZone.DaylightName}\"");
Assert.True(IanaAbbreviationRegex.IsMatch(timeZone.StandardName),
$"Id: \"{timeZone.Id}\", StandardName should have matched the pattern @\"{IanaAbbreviationRegex}\", Actual StandardName: \"{timeZone.StandardName}\"");
Assert.True(IanaAbbreviationRegex.IsMatch(timeZone.DaylightName),
$"Id: \"{timeZone.Id}\", DaylightName should have matched the pattern @\"{IanaAbbreviationRegex}\", Actual DaylightName: \"{timeZone.DaylightName}\"");
}
}
else if (isUtc)
Expand Down
Loading