Skip to content

Commit

Permalink
Fix bug with IndexOf
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdotnet committed Mar 4, 2023
1 parent 19e1633 commit 757c26d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions src/LinkDotNet.StringBuilder/NaiveSearch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public static ReadOnlySpan<int> FindAll(ReadOnlySpan<char> text, ReadOnlySpan<ch
/// <returns>The index of the found <paramref name="word"/> in <paramref name="text"/> or -1 if not found.</returns>
public static int FindFirst(ReadOnlySpan<char> text, ReadOnlySpan<char> word)
{
if (text.IsEmpty && word.IsEmpty)
{
return 0;
}

if (text.IsEmpty || word.IsEmpty)
{
return -1;
Expand Down Expand Up @@ -86,6 +91,11 @@ public static int FindFirst(ReadOnlySpan<char> text, ReadOnlySpan<char> word)
/// <returns>The index of the found <paramref name="word"/> in <paramref name="text"/> or -1 if not found.</returns>
public static int FindLast(ReadOnlySpan<char> text, ReadOnlySpan<char> word)
{
if (text.IsEmpty && word.IsEmpty)
{
return 0;
}

if (text.IsEmpty || word.IsEmpty)
{
return -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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()
{
Expand Down

0 comments on commit 757c26d

Please sign in to comment.