Skip to content

Commit

Permalink
Fixed error using VirtualTags with functions when there was a comma i…
Browse files Browse the repository at this point in the history
…n a field.
  • Loading branch information
maforget committed Oct 26, 2024
1 parent b6392f3 commit 03be52c
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions cYo.Common/Text/ExtendedStringFormater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private static object GetValue(IDictionary<string, object> values, string key)
return value;
}

private static string Format(string format, Func<string, object> getValue, out bool success)
private static string Format(string format, Func<string, object> getValue, out bool success, bool escapeComma = false)
{
StringBuilder stringBuilder = new StringBuilder();
success = true;
Expand All @@ -58,7 +58,7 @@ private static string Format(string format, Func<string, object> getValue, out b
case '[':
{
bool success2;
string value2 = Format(GetPart(format, ref i, '[', ']'), getValue, out success2);
string value2 = Format(GetPart(format, ref i, '[', ']'), getValue, out success2, escapeComma);
if (success2)
{
stringBuilder.Append(value2);
Expand All @@ -69,7 +69,7 @@ private static string Format(string format, Func<string, object> getValue, out b
case '$':
{
bool success3;
string value3 = Format(GetPart(format, ref i, '$', '>', ['<', '>', '$']), getValue, out success3);
string value3 = Format(GetPart(format, ref i, '$', '>', ['<', '>', '$']), getValue, out success3, true);
string result = ParseFunction(value3, out success3);
stringBuilder.Append(result);
success &= success3;
Expand All @@ -79,6 +79,9 @@ private static string Format(string format, Func<string, object> getValue, out b
{
string value = FormatValue(GetPart(format, ref i, '{', '}'), getValue);
success &= !string.IsNullOrEmpty(value);
if (escapeComma && value.Contains(',')) //Escape commas when using functions, so they aren't considered parameters
value = value.Replace(",", "\\,");

stringBuilder.Append(value);
break;
}
Expand All @@ -102,9 +105,15 @@ private static string ParseFunction(string value, out bool success)
string result = string.Empty;
Regex regex = new Regex(@"(?<function>[^<]+?)<(?<params>.+)$");
name = regex.Match(value).Groups["function"].Value.ToLower();
param = regex.Match(value).Groups["params"].Value?.Split(',').Select(x => x.Trim()).ToArray();
string paramsValue = regex.Match(value).Groups["params"]?.Value;

param = string.IsNullOrEmpty(paramsValue)
? Array.Empty<string>() // Return an empty array if null or empty
: Regex.Split(paramsValue, @"(?<!\\),")
.Select(x => x.Trim().Replace(@"\,", ",")) // Remove escaping backslash
.ToArray();

IFunction func = FunctionFactory.Functions.CreateFunction(name);
IFunction func = FunctionFactory.Functions.CreateFunction(name);
func.SetParameters(param);
result = func.ResultAsText;
success = true;
Expand Down

0 comments on commit 03be52c

Please sign in to comment.