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
31 changes: 17 additions & 14 deletions src/libraries/Microsoft.Extensions.Primitives/src/StringSegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -452,22 +452,25 @@ public StringSegment Subsegment(int offset, int length)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int IndexOf(char c, int start, int count)
{
int offset = Offset + start;
int index = -1;

if (!HasValue || start < 0 || (uint)offset > (uint)Buffer.Length)
if (HasValue)
{
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start);
}
if ((uint)start > (uint)Length)
{
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start);
}

if (count < 0)
{
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count);
}
if ((uint)count > (uint)(Length - start))
{
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count);
}

int index = AsSpan().Slice(start, count).IndexOf(c);
if (index >= 0)
{
index += start;
index = AsSpan(start, count).IndexOf(c);
if (index >= 0)
{
index += start;
}
}

return index;
Expand Down Expand Up @@ -516,12 +519,12 @@ public int IndexOfAny(char[] anyOf, int startIndex, int count)

if (HasValue)
{
if (startIndex < 0 || Offset + startIndex > Buffer.Length)
if ((uint)startIndex > (uint)Length)
{
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start);
}

if (count < 0 || Offset + startIndex + count > Buffer.Length)
if ((uint)count > (uint)(Length - startIndex))
{
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1134,6 +1134,19 @@ public void IndexOf_ReturnsMinusOne_IfElementNotInSegment()
Assert.Equal(-1, result);
}

[Fact]
public void IndexOf_ReturnsMinusOne_OnDefaultStringSegment()
{
// Arrange
StringSegment segment = default;

// Act
int result = segment.IndexOf(',');

// Assert
Assert.Equal(-1, result);
}

[Fact]
public void IndexOf_SkipsANumberOfCaracters_IfStartIsProvided()
{
Expand Down Expand Up @@ -1211,6 +1224,19 @@ public void IndexOfAny_ReturnsMinusOne_IfElementNotInSegment()
Assert.Equal(-1, result);
}

[Fact]
public void IndexOfAny_ReturnsMinusOne_OnDefaultStringSegment()
{
// Arrange
StringSegment segment = default;

// Act
int result = segment.IndexOfAny(new[] { ',' });

// Assert
Assert.Equal(-1, result);
}

[Fact]
public void IndexOfAny_SkipsANumberOfCaracters_IfStartIsProvided()
{
Expand Down Expand Up @@ -1239,6 +1265,17 @@ public void IndexOfAny_SearchOnlyInsideTheRange_IfStartAndCountAreProvided()
Assert.Equal(-1, result);
}

[Fact]
public void IndexOfAny_StartOverflowsWithOffset_OutOfRangeThrows()
{
// Arrange
StringSegment segment = new StringSegment("12345", 0, 1);

// Act & Assert
ArgumentOutOfRangeException exception = Assert.Throws<ArgumentOutOfRangeException>(() => segment.IndexOfAny(new []{ '5' }, 2, 3));
Assert.Equal("start", exception.ParamName);
}

[Fact]
public void LastIndexOf_ComputesIndex_RelativeToTheCurrentSegment()
{
Expand All @@ -1265,6 +1302,19 @@ public void LastIndexOf_ReturnsMinusOne_IfElementNotInSegment()
Assert.Equal(-1, result);
}

[Fact]
public void LastIndexOf_ReturnsMinusOne_OnDefaultStringSegment()
{
// Arrange
StringSegment segment = default;

// Act
int result = segment.LastIndexOf(',');

// Assert
Assert.Equal(-1, result);
}

[Fact]
public void Value_DoesNotAllocateANewString_IfTheSegmentContainsTheWholeBuffer()
{
Expand Down