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

Use SpanHelpers.SequenceCompareTo instead of CompareOrdinalHelper #402

Closed
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
32 changes: 30 additions & 2 deletions src/libraries/System.Memory/tests/Span/SequenceCompareTo.char.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,38 @@ public static void LengthMismatchSequenceCompareTo_Char()
Assert.True(result > 0);
}

[Fact]
public static void SequenceCompareToEqual_Char()
{
for (int length = 0; length < 128; length++)
{
var first = new char[length + 1];
var second = new char[length + 1];
// Add mismatch just outside of range to ensure no over read
first[length] = '\0';
second[length] = 'z';

for (int i = 0; i < length; i++)
{
first[i] = second[i] = (char)(i + 1);
}

// Trim off the mismatch
var firstSpan = new Span<char>(first)[..^1];
var secondSpan = new Span<char>(second)[..^1];

Assert.Equal(length, firstSpan.Length);
Assert.Equal(length, secondSpan.Length);

Assert.Equal(0, firstSpan.SequenceCompareTo<char>(secondSpan));
Assert.Equal(0, secondSpan.SequenceCompareTo<char>(firstSpan));
}
}

[Fact]
public static void SequenceCompareToWithSingleMismatch_Char()
{
for (int length = 1; length < 32; length++)
for (int length = 1; length < 128; length++)
{
for (int mismatchIndex = 0; mismatchIndex < length; mismatchIndex++)
{
Expand All @@ -100,7 +128,7 @@ public static void SequenceCompareToWithSingleMismatch_Char()
[Fact]
public static void SequenceCompareToNoMatch_Char()
{
for (int length = 1; length < 32; length++)
for (int length = 1; length < 128; length++)
{
var first = new char[length];
var second = new char[length];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static bool Contains(this ReadOnlySpan<char> span, ReadOnlySpan<char> val
/// </summary>
public static bool Equals(this ReadOnlySpan<char> span, ReadOnlySpan<char> other, StringComparison comparisonType)
{
string.CheckStringComparison(comparisonType);
string.ThrowIfStringComparisonInvalid(comparisonType);

switch (comparisonType)
{
Expand Down Expand Up @@ -95,7 +95,7 @@ internal static bool EqualsOrdinalIgnoreCase(this ReadOnlySpan<char> span, ReadO
/// </summary>
public static int CompareTo(this ReadOnlySpan<char> span, ReadOnlySpan<char> other, StringComparison comparisonType)
{
string.CheckStringComparison(comparisonType);
string.ThrowIfStringComparisonInvalid(comparisonType);

switch (comparisonType)
{
Expand Down Expand Up @@ -126,7 +126,7 @@ public static int CompareTo(this ReadOnlySpan<char> span, ReadOnlySpan<char> oth
/// </summary>
public static int IndexOf(this ReadOnlySpan<char> span, ReadOnlySpan<char> value, StringComparison comparisonType)
{
string.CheckStringComparison(comparisonType);
string.ThrowIfStringComparisonInvalid(comparisonType);

if (comparisonType == StringComparison.Ordinal)
{
Expand Down Expand Up @@ -161,7 +161,7 @@ ref MemoryMarshal.GetReference(value),
/// </summary>
public static int LastIndexOf(this ReadOnlySpan<char> span, ReadOnlySpan<char> value, StringComparison comparisonType)
{
string.CheckStringComparison(comparisonType);
string.ThrowIfStringComparisonInvalid(comparisonType);

if (comparisonType == StringComparison.Ordinal)
{
Expand Down Expand Up @@ -300,7 +300,7 @@ public static int ToUpperInvariant(this ReadOnlySpan<char> source, Span<char> de
/// <param name="comparisonType">One of the enumeration values that determines how the <paramref name="span"/> and <paramref name="value"/> are compared.</param>
public static bool EndsWith(this ReadOnlySpan<char> span, ReadOnlySpan<char> value, StringComparison comparisonType)
{
string.CheckStringComparison(comparisonType);
string.ThrowIfStringComparisonInvalid(comparisonType);

switch (comparisonType)
{
Expand Down Expand Up @@ -337,7 +337,7 @@ ref MemoryMarshal.GetReference(value),
/// <param name="comparisonType">One of the enumeration values that determines how the <paramref name="span"/> and <paramref name="value"/> are compared.</param>
public static bool StartsWith(this ReadOnlySpan<char> span, ReadOnlySpan<char> value, StringComparison comparisonType)
{
string.CheckStringComparison(comparisonType);
string.ThrowIfStringComparisonInvalid(comparisonType);

switch (comparisonType)
{
Expand Down
Loading