Skip to content

Commit 7d01e97

Browse files
Improved testing.
1 parent 15c6721 commit 7d01e97

102 files changed

Lines changed: 3727 additions & 342 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Source/Extensions.Equals.cs

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
using System;
2+
3+
namespace Open.Text
4+
{
5+
public static partial class Extensions
6+
{
7+
8+
/// <summary>
9+
/// Optimized equals for comparing as span vs a string.
10+
/// </summary>
11+
/// <param name="source">The source span.</param>
12+
/// <param name="other">The string to compare to.</param>
13+
/// <param name="stringComparison">The string comparison type.</param>
14+
/// <returns>True if the are contents equal.</returns>
15+
public static bool Equals(this in ReadOnlySpan<char> source, string? other, StringComparison stringComparison)
16+
{
17+
if (other is null) return false;
18+
var len = source.Length;
19+
if (len != other.Length) return false;
20+
return len switch
21+
{
22+
0 => true,
23+
1 when stringComparison == StringComparison.Ordinal => source[0] == other[0],
24+
_ => source.Equals(other.AsSpan(), stringComparison),
25+
};
26+
}
27+
28+
/// <summary>
29+
/// Optimized equals for comparing as span vs a string.
30+
/// </summary>
31+
/// <param name="source">The source span.</param>
32+
/// <param name="other">The string to compare to.</param>
33+
/// <param name="stringComparison">The string comparison type.</param>
34+
/// <returns>True if the are contents equal.</returns>
35+
public static bool Equals(this in Span<char> source, string? other, StringComparison stringComparison)
36+
{
37+
if (other is null) return false;
38+
var len = source.Length;
39+
if (len != other.Length) return false;
40+
return len switch
41+
{
42+
0 => true,
43+
1 when stringComparison == StringComparison.Ordinal => source[0] == other[0],
44+
_ => MemoryExtensions.Equals(source, other.AsSpan(), stringComparison)
45+
};
46+
}
47+
48+
/// <summary>
49+
/// Optimized equals for comparing spans.
50+
/// </summary>
51+
/// <param name="source">The source span.</param>
52+
/// <param name="other">The span to compare to.</param>
53+
/// <param name="stringComparison">The string comparison type.</param>
54+
/// <returns>True if the are contents equal.</returns>
55+
public static bool Equals(this in Span<char> source, in Span<char> other, StringComparison stringComparison)
56+
{
57+
var len = source.Length;
58+
if (len != other.Length) return false;
59+
return len switch
60+
{
61+
0 => true,
62+
1 when stringComparison == StringComparison.Ordinal => source[0] == other[0],
63+
_ => MemoryExtensions.Equals(source, other, stringComparison)
64+
};
65+
}
66+
67+
/// <summary>
68+
/// Optimized equals for comparing spans.
69+
/// </summary>
70+
/// <param name="source">The source span.</param>
71+
/// <param name="other">The span to compare to.</param>
72+
/// <param name="stringComparison">The string comparison type.</param>
73+
/// <returns>True if the are contents equal.</returns>
74+
public static bool Equals(this in Span<char> source, in ReadOnlySpan<char> other, StringComparison stringComparison)
75+
{
76+
var len = source.Length;
77+
if (len != other.Length) return false;
78+
return len switch
79+
{
80+
0 => true,
81+
1 when stringComparison == StringComparison.Ordinal => source[0] == other[0],
82+
_ => MemoryExtensions.Equals(source, other, stringComparison)
83+
};
84+
}
85+
86+
/// <summary>
87+
/// Optimized equals for comparing as string to a span.
88+
/// </summary>
89+
/// <param name="source">The source string.</param>
90+
/// <param name="other">The span to compare to.</param>
91+
/// <param name="stringComparison">The string comparison type.</param>
92+
/// <returns>True if the are contents equal.</returns>
93+
public static bool Equals(this string source, in ReadOnlySpan<char> other, StringComparison stringComparison)
94+
{
95+
if (source is null)
96+
throw new ArgumentNullException(nameof(source));
97+
var len = source.Length;
98+
if (len != other.Length) return false;
99+
return len switch
100+
{
101+
0 => true,
102+
1 when stringComparison == StringComparison.Ordinal => source[0] == other[0],
103+
_ => source.AsSpan().Equals(other, stringComparison),
104+
};
105+
}
106+
107+
/// <summary>
108+
/// Optimized equals for comparing as string to a span.
109+
/// </summary>
110+
/// <param name="source">The source string.</param>
111+
/// <param name="other">The span to compare to.</param>
112+
/// <param name="stringComparison">The string comparison type.</param>
113+
/// <returns>True if the are contents equal.</returns>
114+
public static bool Equals(this string source, in Span<char> other, StringComparison stringComparison)
115+
{
116+
if (source is null) throw new ArgumentNullException(nameof(source));
117+
var len = source.Length;
118+
if (len != other.Length) return false;
119+
return len switch
120+
{
121+
0 => true,
122+
1 when stringComparison == StringComparison.Ordinal => source[0] == other[0],
123+
_ => source.AsSpan().Equals(other, stringComparison),
124+
};
125+
}
126+
}
127+
}

0 commit comments

Comments
 (0)