diff --git a/PanoramicData.NCalcExtensions.Test/ReplaceTests.cs b/PanoramicData.NCalcExtensions.Test/ReplaceTests.cs index 340734f..10577f0 100644 --- a/PanoramicData.NCalcExtensions.Test/ReplaceTests.cs +++ b/PanoramicData.NCalcExtensions.Test/ReplaceTests.cs @@ -15,4 +15,11 @@ public void Replace_Example2_Succeeds() var expression = new ExtendedExpression("replace('abcdefg', 'cde', '')"); (expression.Evaluate() as string).Should().Be("abfg"); } + + [Fact] + public void Replace_Example3_Succeeds() + { + var expression = new ExtendedExpression("replace('abcdefg', 'a', '1', 'bc', '23')"); + (expression.Evaluate() as string).Should().Be("123defg"); + } } diff --git a/PanoramicData.NCalcExtensions/Extensions/Replace.cs b/PanoramicData.NCalcExtensions/Extensions/Replace.cs index 239faea..fa3f62c 100644 --- a/PanoramicData.NCalcExtensions/Extensions/Replace.cs +++ b/PanoramicData.NCalcExtensions/Extensions/Replace.cs @@ -13,9 +13,13 @@ string Replace( [Description("The input string to be searched.")] string haystack, [Description("The smaller string being searched for.")] - string needle, - [Description("The new replacement text to substituted for each macth.")] - string newNeedle + string needle1, + [Description("The new replacement text to substituted for each match.")] + string newNeedle1, + [Description("The next smaller string being searched for.")] + string needle2, + [Description("The next new replacement text to substituted for each match.")] + string newNeedle2 ); } @@ -23,16 +27,37 @@ internal static class Replace { internal static void Evaluate(FunctionArgs functionArgs) { - try + // Input checks + switch (functionArgs.Parameters.Length) { - var haystack = (string)functionArgs.Parameters[0].Evaluate(); - var needle = (string)functionArgs.Parameters[1].Evaluate(); - var newNeedle = (string)functionArgs.Parameters[2].Evaluate(); - functionArgs.Result = haystack.Replace(needle, newNeedle); + case 0: + case 1: + case 2: + throw new FormatException($"{ExtensionFunction.Replace}() requires at least three string parameters."); + default: + if (functionArgs.Parameters.Length % 2 == 0) + { + throw new FormatException($"{ExtensionFunction.Replace}() requires an odd number of string parameters."); + } + // All good + break; } - catch (Exception e) when (e is not NCalcExtensionsException or FormatException) + + var haystack = (string)functionArgs.Parameters[0].Evaluate(); + + if (haystack is not string haystackString) + { + throw new NCalcExtensionsException($"{ExtensionFunction.Replace}() requires a string parameter."); + } + + var needleIndex = 1; + while (needleIndex < functionArgs.Parameters.Length) { - throw new FormatException($"{ExtensionFunction.Replace}() requires three string parameters."); + var needle = (string)functionArgs.Parameters[needleIndex++].Evaluate(); + var replacementNeedle = (string)functionArgs.Parameters[needleIndex++].Evaluate(); + haystackString = haystackString.Replace(needle, replacementNeedle); } + + functionArgs.Result = haystackString; } } diff --git a/PanoramicData.NCalcExtensions/PanoramicData.NCalcExtensions.csproj b/PanoramicData.NCalcExtensions/PanoramicData.NCalcExtensions.csproj index d909261..bbbe3de 100644 --- a/PanoramicData.NCalcExtensions/PanoramicData.NCalcExtensions.csproj +++ b/PanoramicData.NCalcExtensions/PanoramicData.NCalcExtensions.csproj @@ -25,7 +25,7 @@ portable - Added trim(). Added formatting constructor options. + replace() can now replace more than one string at a time. snupkg diff --git a/README.md b/README.md index df1145b..f88954d 100644 --- a/README.md +++ b/README.md @@ -1162,10 +1162,12 @@ Emits a List\. * haystackString * needleString * betterNeedleString + * ... (optional) more needle/betterNeedle pairs #### Examples * replace('abcdefg', 'cde', 'CDE') : 'abCDEfg' * replace('abcdefg', 'cde', '') : 'abfg' + * replace('abcdefg', 'a', '1', 'bc', '23') : '123defg' ---