Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/libraries/Common/tests/Tests/System/StringTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace System.Tests
public partial class StringTests
{
private const string SoftHyphen = "\u00AD";
private const string ZeroWidthJoiner = "\u200D"; // weightless in both ICU and NLS
private static readonly char[] s_whiteSpaceCharacters = { '\u0009', '\u000a', '\u000b', '\u000c', '\u000d', '\u0020', '\u0085', '\u00a0', '\u1680' };

[Theory]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,11 @@ private unsafe string ReplaceCore(string oldValue, string? newValue, CultureInfo
do
{
index = ci.IndexOf(this, oldValue, startIndex, this.Length - startIndex, options, &matchLength);
if (index >= 0)

// There's the possibility that 'oldValue' has zero collation weight (empty string equivalent).
// If this is the case, we behave as if there are no more substitutions to be made.

if (index >= 0 && matchLength > 0)
{
// append the unmodified portion of string
result.Append(this.AsSpan(startIndex, index - startIndex));
Expand Down
15 changes: 15 additions & 0 deletions src/libraries/System.Runtime/tests/System/StringTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,21 @@ public void Replace_StringComparison_EmptyOldValue_ThrowsArgumentException()
AssertExtensions.Throws<ArgumentException>("oldValue", () => "abc".Replace("", "def", true, CultureInfo.CurrentCulture));
}

[Fact]
public void Replace_StringComparison_WeightlessOldValue_WithOrdinalComparison_Succeeds()
{
Assert.Equal("abcdef", ("abc" + ZeroWidthJoiner).Replace(ZeroWidthJoiner, "def"));
Assert.Equal("abcdef", ("abc" + ZeroWidthJoiner).Replace(ZeroWidthJoiner, "def", StringComparison.Ordinal));
Assert.Equal("abcdef", ("abc" + ZeroWidthJoiner).Replace(ZeroWidthJoiner, "def", StringComparison.OrdinalIgnoreCase));
}

[Fact]
public void Replace_StringComparison_WeightlessOldValue_WithLinguisticComparison_TerminatesReplacement()
{
Assert.Equal("abc" + ZeroWidthJoiner + "def", ("abc" + ZeroWidthJoiner + "def").Replace(ZeroWidthJoiner, "xyz", StringComparison.CurrentCulture));
Assert.Equal("abc" + ZeroWidthJoiner + "def", ("abc" + ZeroWidthJoiner + "def").Replace(ZeroWidthJoiner, "xyz", true, CultureInfo.CurrentCulture));
}

[Theory]
[InlineData(StringComparison.CurrentCulture - 1)]
[InlineData(StringComparison.OrdinalIgnoreCase + 1)]
Expand Down