Skip to content

Commit

Permalink
Use CompareOrdinalHelper for SpanHelpers.SequenceCompareTo
Browse files Browse the repository at this point in the history
  • Loading branch information
benaadams committed Aug 9, 2020
1 parent aa5fdab commit 1f66e28
Show file tree
Hide file tree
Showing 5 changed files with 330 additions and 221 deletions.
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 @@ public static int IndexOf(this ReadOnlySpan<char> span, ReadOnlySpan<char> 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 @@ internal static bool EndsWithOrdinalIgnoreCase(this ReadOnlySpan<char> span, Rea
/// <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

0 comments on commit 1f66e28

Please sign in to comment.