Skip to content

Commit

Permalink
replace() can now replace more than one string at a time.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidnmbond committed Oct 8, 2024
1 parent f4289a4 commit f20f890
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 11 deletions.
7 changes: 7 additions & 0 deletions PanoramicData.NCalcExtensions.Test/ReplaceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
45 changes: 35 additions & 10 deletions PanoramicData.NCalcExtensions/Extensions/Replace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,51 @@ 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
);
}

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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<DebugType>portable</DebugType>

<!-- Release Notes -->
<PackageReleaseNotes>Added trim(). Added formatting constructor options.</PackageReleaseNotes>
<PackageReleaseNotes>replace() can now replace more than one string at a time.</PackageReleaseNotes>

<SymbolPackageFormat>snupkg</SymbolPackageFormat>

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1162,10 +1162,12 @@ Emits a List\<T\>.
* 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'

---

Expand Down

0 comments on commit f20f890

Please sign in to comment.