Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace a few Regex static method calls #728

Merged
merged 3 commits into from
Apr 30, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion dotnet/src/SemanticKernel/Planning/Plan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ public ISKFunction SetAIConfiguration(CompleteRequestSettings settings)
internal string ExpandFromVariables(ContextVariables variables, string input)
{
var result = input;
var matches = Regex.Matches(input, @"\$(?<var>\w+)");
var matches = s_variablesRegex.Matches(input);
var orderedMatches = matches.Cast<Match>().Select(m => m.Groups["var"].Value).OrderByDescending(m => m.Length);

foreach (var varName in orderedMatches)
Expand Down Expand Up @@ -489,4 +489,6 @@ private void SetFunction(ISKFunction function)
private ISKFunction? Function { get; set; } = null;

private readonly List<Plan> _steps = new();

private static readonly Regex s_variablesRegex = new(@"\$(?<var>\w+)");
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public FunctionIdBlock(string? text, ILogger? log = null)

public override bool IsValid(out string errorMsg)
{
if (!Regex.IsMatch(this.Content, "^[a-zA-Z0-9_.]*$"))
if (!s_validContentRegex.IsMatch(this.Content))
{
errorMsg = "The function identifier is empty";
return false;
Expand Down Expand Up @@ -66,4 +66,6 @@ private static bool HasMoreThanOneDot(string? value)
int count = 0;
return value.Any(t => t == '.' && ++count > 1);
}

private static readonly Regex s_validContentRegex = new("^[a-zA-Z0-9_.]*$");
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public override bool IsValid(out string errorMsg)
return false;
}

if (!Regex.IsMatch(this.Name, "^[a-zA-Z0-9_]*$"))
if (!s_validNameRegex.IsMatch(this.Name))
{
errorMsg = $"The variable name '{this.Name}' contains invalid characters. " +
"Only alphanumeric chars and underscore are allowed.";
Expand Down Expand Up @@ -78,4 +78,6 @@ public string Render(ContextVariables? variables)

return exists ? value : string.Empty;
}

private static readonly Regex s_validNameRegex = new("^[a-zA-Z0-9_]*$");
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@ public static IReadOnlyList<RestApiOperationParameter> GetParameters(this RestAp
//Create a property alternative name without special symbols that are not supported by SK template language.
foreach (var parameter in parameters)
{
parameter.AlternativeName = Regex.Replace(parameter.Name, @"[^0-9A-Za-z_]+", "_");
parameter.AlternativeName = s_invalidSymbolsRegex.Replace(parameter.Name, "_");
}

return parameters;
}

private const string MediaTypeTextPlain = "text/plain";
private static readonly Regex s_invalidSymbolsRegex = new(@"[^0-9A-Za-z_]+");
}