Skip to content

Commit

Permalink
Improve IndexOfStartOf, add more tests for it
Browse files Browse the repository at this point in the history
  • Loading branch information
aannenko committed Jan 3, 2025
1 parent fc2ab3f commit 7108065
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,42 @@ namespace TransmissionManager.TorrentWebPages.Tests;
internal sealed class ReadOnlySpanExtensionsTests
{
[Test]
public void IndexOfStartOf_WhenGivenNormalSpanAndFullyExistingValue_ReturnsIndexOfValue() =>
public void IndexOfStartOf_GivenNormalSpanAndFullyExistingValue_ReturnsIndexOfValue() =>
Assert.That("testing"u8.IndexOfStartOf("ing"u8), Is.EqualTo(4));

[Test]
public void IndexOfStartOf_WhenGivenNormalSpanAndValueEqualToSpan_ReturnsIndexOfValue() =>
public void IndexOfStartOf_GivenNormalSpanAndValueEqualToSpan_ReturnsIndexOfValue() =>
Assert.That("testing"u8.IndexOfStartOf("testing"u8), Is.EqualTo(0));

[Test]
public void IndexOfStartOf_WhenGivenNormalSpanAndPartiallyExistingValue_ReturnsIndexOfValue() =>
public void IndexOfStartOf_GivenNormalSpanAndPartiallyExistingValue_ReturnsIndexOfValue() =>
Assert.That("testin"u8.IndexOfStartOf("ing"u8), Is.EqualTo(4));

[Test]
public void IndexOfStartOf_WhenGivenShorterSpanAndPartiallyExistingValue_ReturnsIndexOfValue() =>
public void IndexOfStartOf_GivenNormalSpanWithDuplicatesAndPartiallyExistingValue_ReturnsIndexOfValue() =>
Assert.That("aaaab"u8.IndexOfStartOf("aabc"u8), Is.EqualTo(2));

[Test]
public void IndexOfStartOf_GivenNormalSpanWithRepeatingPatternAndPartiallyExistingValue_ReturnsIndexOfValue() =>
Assert.That("ababa"u8.IndexOfStartOf("abac"u8), Is.EqualTo(2));

[Test]
public void IndexOfStartOf_GivenShorterSpanAndPartiallyExistingValue_ReturnsIndexOfValue() =>
Assert.That("in"u8.IndexOfStartOf("ing"u8), Is.EqualTo(0));

[Test]
public void IndexOfStartOf_WhenGivenNormalSpanAndEmptyValue_ReturnsIndexOfValue() =>
public void IndexOfStartOf_GivenNormalSpanAndEmptyValue_ReturnsIndexOfValue() =>
Assert.That("testing"u8.IndexOfStartOf(""u8), Is.EqualTo(0));

[Test]
public void IndexOfStartOf_WhenGivenEmptySpanAndEmptyValue_ReturnsIndexOfValue() =>
public void IndexOfStartOf_GivenEmptySpanAndEmptyValue_ReturnsIndexOfValue() =>
Assert.That(""u8.IndexOfStartOf(""u8), Is.EqualTo(0));

[Test]
public void IndexOfStartOf_WhenGivenNormalSpanAndNonExistingValue_ReturnsMinusOne() =>
public void IndexOfStartOf_GivenNormalSpanAndNonExistingValue_ReturnsMinusOne() =>
Assert.That("testing"u8.IndexOfStartOf("asdfg"u8), Is.EqualTo(-1));

[Test]
public void IndexOfStartOf_WhenGivenEmptySpanAndNonEmptyValue_ReturnsMinusOne() =>
public void IndexOfStartOf_GivenEmptySpanAndNonEmptyValue_ReturnsMinusOne() =>
Assert.That(""u8.IndexOfStartOf("asdf"u8), Is.EqualTo(-1));
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@ public static int IndexOfStartOf(this ReadOnlySpan<byte> span, ReadOnlySpan<byte
if (index is not -1)
return index;

for (var valueLength = Math.Min(span.Length, value.Length - 1); valueLength > 0; valueLength--)
for (var end = span[^Math.Min(span.Length, value.Length - 1)..]; end.Length > 0; end = end[1..])
{
if (span.EndsWith(value[..valueLength]))
return span.Length - valueLength;
index = end.IndexOf(value[0]);
if (index is -1)
return -1;

end = end[index..];
if (end.SequenceEqual(value[..end.Length]))
return span.Length - end.Length;
}

return -1;
Expand Down

0 comments on commit 7108065

Please sign in to comment.