From 03be52cf10c34b3537cc2be0696bea0e730ff82d Mon Sep 17 00:00:00 2001 From: maforget <11904426+maforget@users.noreply.github.com> Date: Sat, 26 Oct 2024 17:22:47 -0400 Subject: [PATCH] Fixed error using VirtualTags with functions when there was a comma in a field. --- cYo.Common/Text/ExtendedStringFormater.cs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/cYo.Common/Text/ExtendedStringFormater.cs b/cYo.Common/Text/ExtendedStringFormater.cs index 1f94bed..11c644f 100644 --- a/cYo.Common/Text/ExtendedStringFormater.cs +++ b/cYo.Common/Text/ExtendedStringFormater.cs @@ -46,7 +46,7 @@ private static object GetValue(IDictionary values, string key) return value; } - private static string Format(string format, Func getValue, out bool success) + private static string Format(string format, Func getValue, out bool success, bool escapeComma = false) { StringBuilder stringBuilder = new StringBuilder(); success = true; @@ -58,7 +58,7 @@ private static string Format(string format, Func 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); @@ -69,7 +69,7 @@ private static string Format(string format, Func 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; @@ -79,6 +79,9 @@ private static string Format(string format, Func 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; } @@ -102,9 +105,15 @@ private static string ParseFunction(string value, out bool success) string result = string.Empty; Regex regex = new Regex(@"(?[^<]+?)<(?.+)$"); 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() // Return an empty array if null or empty + : Regex.Split(paramsValue, @"(? 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;