Skip to content

Commit 6fa9cfc

Browse files
authored
Use properties for some [GeneratedRegex]s (#109118)
* Use properties for some [GeneratedRegex]s * Address PR feedback
1 parent 86d2eaa commit 6fa9cfc

File tree

8 files changed

+37
-39
lines changed

8 files changed

+37
-39
lines changed

src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/Design/DesignerVerb.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ public DesignerVerb(string text, EventHandler handler) : this(text, handler, Sta
2222
/// </summary>
2323
public DesignerVerb(string text, EventHandler handler, CommandID startCommandID) : base(handler, startCommandID)
2424
{
25-
Properties["Text"] = text == null ? null : GetParameterReplacementRegex().Replace(text, "");
25+
Properties["Text"] = text == null ? null : ParameterReplacementRegex.Replace(text, "");
2626
}
2727

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

3131
/// <summary>
3232
/// Gets or sets the description of the menu item for the verb.

src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/DataContract.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2234,7 +2234,7 @@ internal static bool IsAssemblyFriendOfSerialization(Assembly assembly)
22342234
string internalsVisibleAttributeAssemblyName = internalsVisibleAttribute.AssemblyName;
22352235

22362236
if (internalsVisibleAttributeAssemblyName.Trim().Equals("System.Runtime.Serialization") ||
2237-
Globals.FullSRSInternalsVisibleRegex().IsMatch(internalsVisibleAttributeAssemblyName))
2237+
Globals.FullSRSInternalsVisibleRegex.IsMatch(internalsVisibleAttributeAssemblyName))
22382238
{
22392239
return true;
22402240
}

src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Globals.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ internal static Type TypeOfHashtable
347347
public const string NullObjectId = null;
348348
public const string FullSRSInternalsVisiblePattern = @"^[\s]*System\.Runtime\.Serialization[\s]*,[\s]*PublicKey[\s]*=[\s]*(?i:00240000048000009400000006020000002400005253413100040000010001008d56c76f9e8649383049f383c44be0ec204181822a6c31cf5eb7ef486944d032188ea1d3920763712ccb12d75fb77e9811149e6148e5d32fbaab37611c1878ddc19e20ef135d0cb2cff2bfec3d115810c3d9069638fe4be215dbf795861920e5ab6f7db2e2ceef136ac23d5dd2bf031700aec232f6c6b1c785b4305c123b37ab)[\s]*$";
349349
[GeneratedRegex(FullSRSInternalsVisiblePattern)]
350-
public static partial Regex FullSRSInternalsVisibleRegex();
350+
public static partial Regex FullSRSInternalsVisibleRegex { get; }
351351
public const char SpaceChar = ' ';
352352
public const char OpenBracketChar = '[';
353353
public const char CloseBracketChar = ']';

src/libraries/System.Private.Xml/src/System/Xml/Schema/FacetChecker.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,7 +1342,7 @@ internal sealed partial class StringFacetsChecker : FacetsChecker
13421342
{ //All types derived from string & anyURI
13431343

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

13471347
internal override Exception? CheckValueFacets(object value, XmlSchemaDatatype datatype)
13481348
{
@@ -1456,7 +1456,7 @@ private static bool MatchEnumeration(string value, ArrayList enumeration, XmlSch
14561456
{
14571457
return new XmlSchemaException(SR.Sch_EmptyAttributeValue, string.Empty);
14581458
}
1459-
if (!LanguageRegex().IsMatch(s))
1459+
if (!LanguageRegex.IsMatch(s))
14601460
{
14611461
return new XmlSchemaException(SR.Sch_InvalidLanguageId, string.Empty);
14621462
}

src/libraries/System.Private.Xml/src/System/Xml/Serialization/SourceInfo.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ internal sealed partial class SourceInfo
1616
//a[ia]
1717
//((global::System.Xml.Serialization.XmlSerializerNamespaces)p[0])
1818
[GeneratedRegex("([(][(](?<t>[^)]+)[)])?(?<a>[^[]+)[[](?<ia>.+)[]][)]?")]
19-
private static partial Regex Regex1();
19+
private static partial Regex Regex1 { get; }
2020

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

2525
private static readonly Lazy<MethodInfo> s_iListGetItemMethod = new Lazy<MethodInfo>(
2626
() =>
@@ -69,7 +69,7 @@ public void Load(Type? elementType)
6969
[RequiresUnreferencedCode("calls LoadMemberAddress")]
7070
private void InternalLoad(Type? elementType, bool asAddress = false)
7171
{
72-
Match match = Regex1().Match(Arg);
72+
Match match = Regex1.Match(Arg);
7373
if (match.Success)
7474
{
7575
object varA = ILG.GetVariable(match.Groups["a"].Value);
@@ -190,7 +190,7 @@ private void InternalLoad(Type? elementType, bool asAddress = false)
190190
}
191191
else
192192
{
193-
match = Regex2().Match(Source);
193+
match = Regex2.Match(Source);
194194
if (match.Success)
195195
{
196196
Debug.Assert(match.Groups["arg"].Value == Arg);

src/libraries/System.Private.Xml/src/System/Xml/Serialization/XmlSerializationReaderILGen.cs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2708,10 +2708,10 @@ private void WriteSourceBeginTyped(string source)
27082708
}
27092709

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

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

27162716
private void WriteSourceBegin(string source)
27172717
{
@@ -2733,7 +2733,7 @@ private void WriteSourceBegin(string source)
27332733
return;
27342734
}
27352735
// a_0_0 = (global::System.Object[])EnsureArrayIndex(a_0_0, ca_0_0, typeof(global::System.Object));a_0_0[ca_0_0++]
2736-
Match match = EnsureArrayIndexRegex().Match(source);
2736+
Match match = EnsureArrayIndexRegex.Match(source);
27372737
if (match.Success)
27382738
{
27392739
Debug.Assert(match.Groups["locA1"].Value == match.Groups["locA2"].Value);
@@ -2779,7 +2779,7 @@ private void WriteSourceBegin(string source)
27792779
}
27802780

27812781
// p[0]
2782-
match = P0Regex().Match(source);
2782+
match = P0Regex.Match(source);
27832783
if (match.Success)
27842784
{
27852785
System.Diagnostics.Debug.Assert(CodeGenerator.GetVariableType(ilg.GetVariable(match.Groups["a"].Value)).IsArray);
@@ -2825,7 +2825,7 @@ private void WriteSourceEnd(string source, Type elementType, Type stackType)
28252825
return;
28262826
}
28272827
// a_0_0 = (global::System.Object[])EnsureArrayIndex(a_0_0, ca_0_0, typeof(global::System.Object));a_0_0[ca_0_0++]
2828-
Match match = EnsureArrayIndexRegex().Match(source);
2828+
Match match = EnsureArrayIndexRegex.Match(source);
28292829
if (match.Success)
28302830
{
28312831
object oVar = ilg.GetVariable(match.Groups["locA1"].Value);
@@ -2861,7 +2861,7 @@ private void WriteSourceEnd(string source, Type elementType, Type stackType)
28612861
return;
28622862
}
28632863
// p[0]
2864-
match = P0Regex().Match(source);
2864+
match = P0Regex.Match(source);
28652865
if (match.Success)
28662866
{
28672867
Type varType = CodeGenerator.GetVariableType(ilg.GetVariable(match.Groups["a"].Value));
@@ -3528,19 +3528,19 @@ private static void WriteLocalDecl(string variableName, SourceInfo initValue)
35283528
}
35293529

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

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

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

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

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

35453545
private void ILGenElseString(string elseString)
35463546
{
@@ -3555,7 +3555,7 @@ private void ILGenElseString(string elseString)
35553555
new Type[] { typeof(object), typeof(string) }
35563556
)!;
35573557
// UnknownNode(null, @":anyType");
3558-
Match match = UnknownNodeNullAnyTypeRegex().Match(elseString);
3558+
Match match = UnknownNodeNullAnyTypeRegex.Match(elseString);
35593559
if (match.Success)
35603560
{
35613561
ilg.Ldarg(0);
@@ -3565,7 +3565,7 @@ private void ILGenElseString(string elseString)
35653565
return;
35663566
}
35673567
// UnknownNode((object)o, @"");
3568-
match = UnknownNodeObjectEmptyRegex().Match(elseString);
3568+
match = UnknownNodeObjectEmptyRegex.Match(elseString);
35693569
if (match.Success)
35703570
{
35713571
ilg.Ldarg(0);
@@ -3577,7 +3577,7 @@ private void ILGenElseString(string elseString)
35773577
return;
35783578
}
35793579
// UnknownNode((object)o, null);
3580-
match = UnknownNodeObjectNullRegex().Match(elseString);
3580+
match = UnknownNodeObjectNullRegex.Match(elseString);
35813581
if (match.Success)
35823582
{
35833583
ilg.Ldarg(0);
@@ -3589,7 +3589,7 @@ private void ILGenElseString(string elseString)
35893589
return;
35903590
}
35913591
// "UnknownNode((object)o);"
3592-
match = UnknownNodeObjectRegex().Match(elseString);
3592+
match = UnknownNodeObjectRegex.Match(elseString);
35933593
if (match.Success)
35943594
{
35953595
ilg.Ldarg(0);
@@ -3603,7 +3603,7 @@ private void ILGenElseString(string elseString)
36033603
}
36043604
private void ILGenParamsReadSource(string paramsReadSource)
36053605
{
3606-
Match match = ParamsReadRegex().Match(paramsReadSource);
3606+
Match match = ParamsReadRegex.Match(paramsReadSource);
36073607
if (match.Success)
36083608
{
36093609
ilg.Ldloca(ilg.GetLocal("paramsRead"));
@@ -3616,7 +3616,7 @@ private void ILGenParamsReadSource(string paramsReadSource)
36163616
}
36173617
private void ILGenParamsReadSource(string paramsReadSource, bool value)
36183618
{
3619-
Match match = ParamsReadRegex().Match(paramsReadSource);
3619+
Match match = ParamsReadRegex.Match(paramsReadSource);
36203620
if (match.Success)
36213621
{
36223622
ilg.Ldloca(ilg.GetLocal("paramsRead"));

src/libraries/System.Private.Xml/src/System/Xml/XmlConvert.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public partial class XmlConvert
8686
return name;
8787
}
8888

89-
Regex.ValueMatchEnumerator en = DecodeCharRegex().EnumerateMatches(name.AsSpan(underscorePos));
89+
Regex.ValueMatchEnumerator en = DecodeCharRegex.EnumerateMatches(name.AsSpan(underscorePos));
9090
int matchPos = -1;
9191
if (en.MoveNext())
9292
{
@@ -185,7 +185,7 @@ public partial class XmlConvert
185185
IEnumerator? en = null;
186186
if (underscorePos >= 0)
187187
{
188-
mc = EncodeCharRegex().Matches(name, underscorePos);
188+
mc = EncodeCharRegex.Matches(name, underscorePos);
189189
en = mc.GetEnumerator();
190190
}
191191

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

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

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

289289
private static int FromHex(char digit)
290290
{

src/libraries/System.Runtime/tests/System.Runtime.Tests/System/TimeZoneInfoTests.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2434,10 +2434,8 @@ public static IEnumerable<object[]> SystemTimeZonesTestData()
24342434
}
24352435
}
24362436

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

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

0 commit comments

Comments
 (0)