Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StringSegment behavior #55501

Merged
merged 6 commits into from
Jul 15, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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 (start < 0 || start > Length)
hrrrrustic marked this conversation as resolved.
Show resolved Hide resolved
{
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start);
}

if (count < 0)
{
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count);
}
if (count < 0 || start + count > Length)
hrrrrustic marked this conversation as resolved.
Show resolved Hide resolved
{
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 (startIndex < 0 || startIndex > Length)
hrrrrustic marked this conversation as resolved.
Show resolved Hide resolved
{
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start);
}

if (count < 0 || Offset + startIndex + count > Buffer.Length)
if (count < 0 || startIndex + count > Length)
hrrrrustic marked this conversation as resolved.
Show resolved Hide resolved
{
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