Skip to content

Commit 48cb8cb

Browse files
committed
Reuse filename template regex
1 parent 73dc3e4 commit 48cb8cb

2 files changed

Lines changed: 5 additions & 4 deletions

File tree

StabilityMatrix.Avalonia/Models/Inference/FileNameFormatProvider.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public ValidationResult Validate(string format)
144144

145145
public IEnumerable<string> GetVariableTexts(string template)
146146
{
147-
return BracketRegex().Matches(template).Select(m => m.Groups[1].Value);
147+
return VariableRegex().Matches(template).Select(m => m.Groups[1].Value);
148148
}
149149

150150
public string GetVariableName(string variableText)
@@ -185,7 +185,7 @@ public bool TryResolveVariable(string variableText, out string? value, out strin
185185

186186
public IEnumerable<FileNameFormatPart> GetParts(string template)
187187
{
188-
var regex = BracketRegex();
188+
var regex = VariableRegex();
189189
var matches = regex.Matches(template);
190190

191191
var parts = new List<FileNameFormatPart>();
@@ -338,6 +338,8 @@ private static (string Variable, Slice? Slice) ExtractVariableAndSlice(string co
338338
[GeneratedRegex(@"\{([a-z_:\d\[\]]+)\}")]
339339
private static partial Regex BracketRegex();
340340

341+
internal static Regex VariableRegex() => BracketRegex();
342+
341343
/// <summary>
342344
/// Regex for matching a Python-like array index.
343345
/// </summary>

StabilityMatrix.Avalonia/Services/ModelOrganizationService.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ public class ModelOrganizationService
2020
Path.DirectorySeparatorChar,
2121
Path.AltDirectorySeparatorChar,
2222
];
23-
private static readonly Regex TemplateRegex = new(@"\{([a-z_:\d\[\]]+)\}", RegexOptions.Compiled);
2423
private static readonly HashSet<char> InvalidSubstitutionChars =
2524
[
2625
.. Path.GetInvalidFileNameChars(),
@@ -325,7 +324,7 @@ out string? error
325324
var builder = new StringBuilder();
326325
var currentIndex = 0;
327326

328-
foreach (var match in TemplateRegex.Matches(template).Cast<Match>())
327+
foreach (var match in FileNameFormatProvider.VariableRegex().Matches(template).Cast<Match>())
329328
{
330329
if (match.Index > currentIndex)
331330
{

0 commit comments

Comments
 (0)