Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit fdabc62

Browse files
authored
Add ROSpan Equals/CompareTo/IndexOf/Contains string-like APIs with StringComparison (#27319)
* Add ROSpan Equals/CompareTo/IndexOf/Contains string-like APIs with StringComparison * Respond to recent change AsReadOnlySpan -> AsSpan * Remove the out parameter from IndexOf * Add tests and avoid allocations for StringComparison.Ordinal * Remove duplicate definition of SoftHyphen
1 parent 28e3939 commit fdabc62

File tree

9 files changed

+1204
-0
lines changed

9 files changed

+1204
-0
lines changed

src/System.Memory/System.Memory.sln

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ref", "ref", "{2E666815-2ED
2727
EndProject
2828
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{3F794AE4-319F-4498-A9DA-C49A4EB11D9E}"
2929
ProjectSection(SolutionItems) = preProject
30+
..\Common\src\CoreLib\System\Runtime\InteropServices\MemoryMarshal.Fast.cs = ..\Common\src\CoreLib\System\Runtime\InteropServices\MemoryMarshal.Fast.cs
3031
..\Common\src\CoreLib\System\ReadOnlySpan.Fast.cs = ..\Common\src\CoreLib\System\ReadOnlySpan.Fast.cs
3132
..\Common\src\CoreLib\System\Span.Fast.cs = ..\Common\src\CoreLib\System\Span.Fast.cs
3233
..\Common\src\CoreLib\System\Span.NonGeneric.cs = ..\Common\src\CoreLib\System\Span.NonGeneric.cs

src/System.Memory/ref/System.Memory.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,15 @@ public static partial class MemoryExtensions
2929
public static int BinarySearch<T, TComparable>(this System.ReadOnlySpan<T> span, TComparable comparable) where TComparable : System.IComparable<T> { throw null; }
3030
public static int BinarySearch<T, TComparer>(this System.Span<T> span, T value, TComparer comparer) where TComparer : System.Collections.Generic.IComparer<T> { throw null; }
3131
public static int BinarySearch<T, TComparable>(this System.Span<T> span, TComparable comparable) where TComparable : System.IComparable<T> { throw null; }
32+
public static int CompareTo(this System.ReadOnlySpan<char> span, System.ReadOnlySpan<char> value, System.StringComparison comparisonType) { throw null; }
33+
public static bool Contains(this System.ReadOnlySpan<char> span, System.ReadOnlySpan<char> value, System.StringComparison comparisonType) { throw null; }
3234
public static void CopyTo<T>(this T[] array, System.Memory<T> destination) { }
3335
public static void CopyTo<T>(this T[] array, System.Span<T> destination) { }
3436
public static bool EndsWith(this System.ReadOnlySpan<char> span, System.ReadOnlySpan<char> value, System.StringComparison comparisonType) { throw null; }
3537
public static bool EndsWith<T>(this System.ReadOnlySpan<T> span, System.ReadOnlySpan<T> value) where T : System.IEquatable<T> { throw null; }
3638
public static bool EndsWith<T>(this System.Span<T> span, System.ReadOnlySpan<T> value) where T : System.IEquatable<T> { throw null; }
39+
public static bool Equals(this System.ReadOnlySpan<char> span, System.ReadOnlySpan<char> value, System.StringComparison comparisonType) { throw null; }
40+
public static int IndexOf(this System.ReadOnlySpan<char> span, System.ReadOnlySpan<char> value, System.StringComparison comparisonType) { throw null; }
3741
public static int IndexOfAny<T>(this System.ReadOnlySpan<T> span, System.ReadOnlySpan<T> values) where T : System.IEquatable<T> { throw null; }
3842
public static int IndexOfAny<T>(this System.ReadOnlySpan<T> span, T value0, T value1) where T : System.IEquatable<T> { throw null; }
3943
public static int IndexOfAny<T>(this System.ReadOnlySpan<T> span, T value0, T value1, T value2) where T : System.IEquatable<T> { throw null; }

src/System.Memory/src/System/MemoryExtensions.Portable.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,58 @@ namespace System
1313
/// </summary>
1414
public static partial class MemoryExtensions
1515
{
16+
/// <summary>
17+
/// Returns a value indicating whether the specified <paramref name="value"/> occurs within the <paramref name="span"/>.
18+
/// <param name="span">The source span.</param>
19+
/// <param name="value">The value to seek within the source span.</param>
20+
/// <param name="comparisonType">One of the enumeration values that determines how the <paramref name="span"/> and <paramref name="value"/> are compared.</param>
21+
/// </summary>
22+
public static bool Contains(this ReadOnlySpan<char> span, ReadOnlySpan<char> value, StringComparison comparisonType)
23+
=> (IndexOf(span, value, comparisonType) >= 0);
24+
25+
/// <summary>
26+
/// Determines whether this <paramref name="span"/> and the specified <paramref name="value"/> span have the same characters
27+
/// when compared using the specified <paramref name="comparisonType"/> option.
28+
/// <param name="span">The source span.</param>
29+
/// <param name="value">The value to compare with the source span.</param>
30+
/// <param name="comparisonType">One of the enumeration values that determines how the <paramref name="span"/> and <paramref name="value"/> are compared.</param>
31+
/// </summary>
32+
public static bool Equals(this ReadOnlySpan<char> span, ReadOnlySpan<char> value, StringComparison comparisonType)
33+
{
34+
if (comparisonType == StringComparison.Ordinal)
35+
{
36+
return span.SequenceEqual<char>(value);
37+
}
38+
39+
return span.ToString().Equals(value.ToString(), comparisonType);
40+
}
41+
42+
/// <summary>
43+
/// Compares the specified <paramref name="span"/> and <paramref name="value"/> using the specified <paramref name="comparisonType"/>,
44+
/// and returns an integer that indicates their relative position in the sort order.
45+
/// <param name="span">The source span.</param>
46+
/// <param name="value">The value to compare with the source span.</param>
47+
/// <param name="comparisonType">One of the enumeration values that determines how the <paramref name="span"/> and <paramref name="value"/> are compared.</param>
48+
/// </summary>
49+
public static int CompareTo(this ReadOnlySpan<char> span, ReadOnlySpan<char> value, StringComparison comparisonType)
50+
=> string.Compare(span.ToString(), value.ToString(), comparisonType);
51+
52+
/// <summary>
53+
/// Reports the zero-based index of the first occurrence of the specified <paramref name="value"/> in the current <paramref name="span"/>.
54+
/// <param name="span">The source span.</param>
55+
/// <param name="value">The value to seek within the source span.</param>
56+
/// <param name="comparisonType">One of the enumeration values that determines how the <paramref name="span"/> and <paramref name="value"/> are compared.</param>
57+
/// </summary>
58+
public static int IndexOf(this ReadOnlySpan<char> span, ReadOnlySpan<char> value, StringComparison comparisonType)
59+
{
60+
if (comparisonType == StringComparison.Ordinal)
61+
{
62+
return span.IndexOf<char>(value);
63+
}
64+
65+
return span.ToString().IndexOf(value.ToString(), comparisonType);
66+
}
67+
1668
/// <summary>
1769
/// Copies the characters from the source span into the destination, converting each character to lowercase,
1870
/// using the casing rules of the specified culture.

0 commit comments

Comments
 (0)