diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a9468a..be7992e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,9 @@ All notable changes to **ValueStringBuilder** will be documented in this file. T ### Added - Added `Reverse` function +### Changed +- Fixed a bug where two empty strings return the wrong value in (Last)IndexOf + ## [1.12.2] - 2023-02-21 ### Changed diff --git a/src/LinkDotNet.StringBuilder/NaiveSearch.cs b/src/LinkDotNet.StringBuilder/NaiveSearch.cs index 1ca0667..84d308d 100644 --- a/src/LinkDotNet.StringBuilder/NaiveSearch.cs +++ b/src/LinkDotNet.StringBuilder/NaiveSearch.cs @@ -49,6 +49,11 @@ public static ReadOnlySpan FindAll(ReadOnlySpan text, ReadOnlySpanThe index of the found in or -1 if not found. public static int FindFirst(ReadOnlySpan text, ReadOnlySpan word) { + if (text.IsEmpty && word.IsEmpty) + { + return 0; + } + if (text.IsEmpty || word.IsEmpty) { return -1; @@ -86,6 +91,11 @@ public static int FindFirst(ReadOnlySpan text, ReadOnlySpan word) /// The index of the found in or -1 if not found. public static int FindLast(ReadOnlySpan text, ReadOnlySpan word) { + if (text.IsEmpty && word.IsEmpty) + { + return 0; + } + if (text.IsEmpty || word.IsEmpty) { return -1; diff --git a/tests/LinkDotNet.StringBuilder.UnitTests/ValueStringBuilderTests.cs b/tests/LinkDotNet.StringBuilder.UnitTests/ValueStringBuilderTests.cs index b26e86a..6e4d59f 100644 --- a/tests/LinkDotNet.StringBuilder.UnitTests/ValueStringBuilderTests.cs +++ b/tests/LinkDotNet.StringBuilder.UnitTests/ValueStringBuilderTests.cs @@ -206,6 +206,16 @@ public void ShouldReturnMinusOneIfNotFound() index.Should().Be(-1); } + [Fact] + public void ShouldReturnZeroIfBothEmpty() + { + using var stringBuilder = new ValueStringBuilder(); + + var index = stringBuilder.IndexOf(string.Empty); + + index.Should().Be(0); + } + [Fact] public void ShouldReturnMinusOneWordIsLongerThanString() { @@ -307,6 +317,16 @@ public void ShouldReturnZeroWhenEmptyStringInIndexOf() index.Should().Be(0); } + [Fact] + public void ShouldReturnZeroIfBothEmptyLastIndexOf() + { + using var stringBuilder = new ValueStringBuilder(); + + var index = stringBuilder.LastIndexOf(string.Empty); + + index.Should().Be(0); + } + [Fact] public void ShouldReturnZeroWhenEmptyStringInLastIndexOf() {