Skip to content

Commit ca5057d

Browse files
committed
initialize field in static ctor
1 parent afd010b commit ca5057d

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

src/System.CommandLine.Rendering/TextSpanFormatter.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,23 @@ public class TextSpanFormatter :
1111
ICustomFormatter,
1212
IFormatProvider
1313
{
14-
private static readonly Regex _formattableStringParser =
15-
new Regex(@"
16-
(\s*{{\s*)
14+
private static readonly Regex _formattableStringParser;
15+
16+
private readonly Dictionary<Type, Func<object, TextSpan>> _formatters = new Dictionary<Type, Func<object, TextSpan>>();
17+
18+
static TextSpanFormatter()
19+
{
20+
_formattableStringParser = new Regex(
21+
@"(\s*{{\s*)
1722
|
1823
(\s*}}\s*)
1924
|
2025
(?<token> \{ [0-9]+ [^\}]* \} )
2126
|
2227
(?<text> [^\{\}]* )",
23-
RegexOptions.Compiled |
24-
RegexOptions.IgnorePatternWhitespace);
25-
26-
private readonly Dictionary<Type, Func<object, TextSpan>> _formatters = new Dictionary<Type, Func<object, TextSpan>>();
28+
RegexOptions.Compiled |
29+
RegexOptions.IgnorePatternWhitespace);
30+
}
2731

2832
public void AddFormatter<T>(Func<T, TextSpan> format)
2933
{
@@ -108,11 +112,12 @@ public TextSpan ParseToSpan(FormattableString formattableString)
108112
{
109113
return new ContainerSpan(DestructureIntoSpans().ToArray());
110114
}
111-
115+
112116
IEnumerable<TextSpan> DestructureIntoSpans()
113117
{
114118
var partIndex = 0;
115119

120+
116121
foreach (Match match in _formattableStringParser.Matches(formattableString.Format))
117122
{
118123
if (match.Value != "")

0 commit comments

Comments
 (0)