Skip to content
This repository has been archived by the owner on Dec 24, 2022. It is now read-only.

Commit

Permalink
Fix ReplaceAll
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Aug 28, 2019
1 parent fee0f67 commit 923e666
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/ServiceStack.Text/StringExtensions.cs
Expand Up @@ -913,8 +913,9 @@ public static string ReplaceAll(this string haystack, string needle, string repl
{ {
int pos; int pos;
// Avoid a possible infinite loop // Avoid a possible infinite loop
if (needle == replacement) return haystack; if (needle == replacement)
while ((pos = haystack.IndexOf(needle, StringComparison.Ordinal)) > 0) return haystack;
while ((pos = haystack.IndexOf(needle, StringComparison.Ordinal)) >= 0)
{ {
haystack = haystack.Substring(0, pos) haystack = haystack.Substring(0, pos)
+ replacement + replacement
Expand Down
7 changes: 7 additions & 0 deletions tests/ServiceStack.Text.Tests/StringExtensionsTests.cs
Expand Up @@ -355,5 +355,12 @@ public void Does_ContainsAny_Return_CaseInsensitive_Matches()


Assert.That(input.ContainsAny(testMatches, StringComparison.OrdinalIgnoreCase)); Assert.That(input.ContainsAny(testMatches, StringComparison.OrdinalIgnoreCase));
} }

[Test]
public void Does_ReplaceAll_from_Start()
{
Assert.That("/images".ReplaceAll("/",""), Is.EqualTo("images"));
}

} }
} }

0 comments on commit 923e666

Please sign in to comment.