Skip to content

Commit 11c5c82

Browse files
Added IndexOf and refined Contains methods.
1 parent f80ecb4 commit 11c5c82

4 files changed

Lines changed: 61 additions & 32 deletions

File tree

Source/Extensions.IndexOf.cs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,24 @@ public static int IndexOf(this ReadOnlySpan<char> source, char search, StringCom
4040
}
4141

4242
/// <inheritdoc cref="string.IndexOf(char, int)"/>
43-
public static int IndexOf(this ReadOnlySpan<char> source, char search, int startIndex, StringComparison comparisonType)
43+
public static int IndexOf(this ReadOnlySpan<char> source, char value, int startIndex, StringComparison comparisonType)
4444
{
4545
if (startIndex >= source.Length)
4646
throw new ArgumentOutOfRangeException(nameof(startIndex), startIndex, "Must be less than the length of the source.");
47-
var i = source.Slice(startIndex).IndexOf(search, comparisonType);
47+
var i = source.Slice(startIndex).IndexOf(value, comparisonType);
4848
return i == -1 ? -1 : i + startIndex;
4949
}
5050

5151
/// <inheritdoc cref="string.LastIndexOf(char)"/>
52-
public static int LastIndexOf(this ReadOnlySpan<char> source, char search, StringComparison comparisonType)
52+
public static int LastIndexOf(this ReadOnlySpan<char> source, char value, StringComparison comparisonType)
5353
{
5454
Func<char, char> toUpper;
5555
switch (comparisonType)
5656
{
5757
case StringComparison.Ordinal:
5858
case StringComparison.CurrentCulture:
5959
case StringComparison.InvariantCulture:
60-
return source.LastIndexOf(search);
60+
return source.LastIndexOf(value);
6161
case StringComparison.CurrentCultureIgnoreCase:
6262
toUpper = static c => char.ToUpper(c, CultureInfo.CurrentCulture);
6363
break;
@@ -71,14 +71,14 @@ public static int LastIndexOf(this ReadOnlySpan<char> source, char search, Strin
7171
throw new ArgumentException("Invalid comparison type.", nameof(comparisonType));
7272
}
7373

74-
var searchUpper = toUpper(search);
75-
if (searchUpper == search)
76-
return source.LastIndexOf(search);
74+
var searchUpper = toUpper(value);
75+
if (searchUpper == value)
76+
return source.LastIndexOf(value);
7777

7878
for (var i = source.Length - 1; i >= 0; i--)
7979
{
8080
var c = source[i];
81-
if (c == search) return i;
81+
if (c == value) return i;
8282
if (toUpper(c) == searchUpper)
8383
return i;
8484
}
@@ -87,29 +87,29 @@ public static int LastIndexOf(this ReadOnlySpan<char> source, char search, Strin
8787
}
8888

8989
/// <inheritdoc cref="string.LastIndexOf(char)"/>
90-
public static int LastIndexOf(this string source, char search, StringComparison comparisonType)
91-
=> source.AsSpan().LastIndexOf(search, comparisonType);
90+
public static int LastIndexOf(this string source, char value, StringComparison comparisonType)
91+
=> source.AsSpan().LastIndexOf(value, comparisonType);
9292

9393
/// <inheritdoc cref="string.LastIndexOf(char)"/>
94-
public static int LastIndexOf(this StringSegment source, char search, StringComparison comparisonType)
95-
=> source.AsSpan().LastIndexOf(search, comparisonType);
94+
public static int LastIndexOf(this StringSegment source, char value, StringComparison comparisonType)
95+
=> source.AsSpan().LastIndexOf(value, comparisonType);
9696

9797
/// <inheritdoc cref="StringSegment.IndexOf(char)"/>
98-
public static int IndexOf(this StringSegment source, char search, StringComparison comparisonType)
99-
=> source.HasValue ? source.AsSpan().IndexOf(search, comparisonType) : -1;
98+
public static int IndexOf(this StringSegment source, char value, StringComparison comparisonType)
99+
=> source.HasValue ? source.AsSpan().IndexOf(value, comparisonType) : -1;
100100

101101
/// <inheritdoc cref="StringSegment.IndexOf(char)"/>
102-
public static int IndexOf(this StringSegment source, char search, int startIndex, StringComparison comparisonType)
103-
=> source.HasValue ? source.AsSpan().IndexOf(search, startIndex, comparisonType) : -1;
102+
public static int IndexOf(this StringSegment source, char value, int startIndex, StringComparison comparisonType)
103+
=> source.HasValue ? source.AsSpan().IndexOf(value, startIndex, comparisonType) : -1;
104104

105105
#if NETSTANDARD2_0
106106
/// <inheritdoc cref="StringSegment.IndexOf(char)"/>
107-
public static int IndexOf(this string source, char search, StringComparison comparisonType)
108-
=> source.AsSpan().IndexOf(search, comparisonType);
107+
public static int IndexOf(this string source, char value, StringComparison comparisonType)
108+
=> source.AsSpan().IndexOf(value, comparisonType);
109109

110110
/// <inheritdoc cref="StringSegment.IndexOf(char)"/>
111-
public static int LastIndexOf(this string source, char search)
112-
=> source.AsSpan().LastIndexOf(search);
111+
public static int LastIndexOf(this string source, char value)
112+
=> source.AsSpan().LastIndexOf(value);
113113
#endif
114114

115115
/// <summary>

Source/Open.Text.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<RepositoryUrl>https://github.com/Open-NET-Libraries/Open.Text</RepositoryUrl>
2121
<RepositoryType>git</RepositoryType>
2222
<PackageTags>string, span, enum, readonlyspan, text, format, split, trim, equals, trimmed equals, first, last, preceding, following, stringbuilder, extensions, stringcomparable, spancomparable, stringsegment, splitassegment</PackageTags>
23-
<Version>8.0.0</Version>
23+
<Version>8.1.0</Version>
2424
<PackageReleaseNotes></PackageReleaseNotes>
2525
<PackageLicenseExpression>MIT</PackageLicenseExpression>
2626
<PublishRepositoryUrl>true</PublishRepositoryUrl>

Source/SpanComparable.cs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,34 @@ public bool Equals(SpanComparable other)
4747
=> Source.Equals(other.Source, Type)
4848
|| Type != other.Type && other.Equals(Source);
4949

50-
/// <inheritdoc cref="Equals(string?)"/>
51-
public bool Equals(StringSegment other)
52-
=> Equals(other.AsSpan());
50+
/// <summary>
51+
/// Reports the zero-based index of the first occurrence
52+
/// of the specified <paramref name="value"/>
53+
/// starting from the <paramref name="startIndex"/>.
54+
/// </summary>
55+
public int IndexOf(StringSegment value, int startIndex = 0)
56+
=> Source.IndexOf(value, startIndex, Type);
57+
58+
/// <inheritdoc cref="IndexOf(StringSegment, int)"/>
59+
public int IndexOf(char value, int startIndex = 0)
60+
=> Source.IndexOf(value, startIndex, Type);
61+
62+
/// <inheritdoc cref="IndexOf(StringSegment, int)"/>
63+
public int IndexOf(ReadOnlySpan<char> value, int startIndex = 0)
64+
=> Source.IndexOf(value, startIndex, Type);
5365

5466
/// <summary>
5567
/// Checks if <paramref name="value"/> value is contained in the sequence using the comparison type.
5668
/// </summary>
5769
/// <returns>true if the value of <paramref name="value"/> is contained (using the comparison type); otherwise false. </returns>
58-
public bool Contains(string value)
70+
public bool Contains(StringSegment value)
5971
=> Source.Contains(value, Type);
6072

61-
/// <inheritdoc cref="Contains(string)"/>
62-
public bool Contains(StringSegment value)
73+
/// <inheritdoc cref="Contains(StringSegment)"/>
74+
public bool Contains(char value)
6375
=> Source.Contains(value, Type);
6476

65-
/// <inheritdoc cref="Contains(string)"/>
77+
/// <inheritdoc cref="Contains(StringSegment)"/>
6678
public bool Contains(ReadOnlySpan<char> value)
6779
=> Source.Contains(value, Type);
6880

Source/StringComparable.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public StringComparable(string? value, StringComparison type)
2323
Segment = value;
2424
Type = type;
2525
}
26+
2627
/// <summary>
2728
/// The string to use for comparison.
2829
/// </summary>
@@ -77,18 +78,34 @@ public bool Equals(StringSegment other)
7778
_ => false
7879
};
7980

81+
/// <summary>
82+
/// Reports the zero-based index of the first occurrence
83+
/// of the specified <paramref name="value"/>
84+
/// starting from the <paramref name="startIndex"/>.
85+
/// </summary>
86+
public int IndexOf(StringSegment value, int startIndex = 0)
87+
=> Segment.IndexOf(value, startIndex, Type);
88+
89+
/// <inheritdoc cref="IndexOf(StringSegment, int)"/>
90+
public int IndexOf(char value, int startIndex = 0)
91+
=> Segment.IndexOf(value, startIndex, Type);
92+
93+
/// <inheritdoc cref="IndexOf(StringSegment, int)"/>
94+
public int IndexOf(ReadOnlySpan<char> value, int startIndex = 0)
95+
=> Segment.IndexOf(value, startIndex, Type);
96+
8097
/// <summary>
8198
/// Checks if <paramref name="value"/> value is contained in the sequence using the comparison type.
8299
/// </summary>
83100
/// <returns>true if the value of <paramref name="value"/> is contained (using the comparison type); otherwise false. </returns>
84-
public bool Contains(string value)
101+
public bool Contains(StringSegment value)
85102
=> Segment.Contains(value, Type);
86103

87-
/// <inheritdoc cref="Contains(string)"/>
88-
public bool Contains(StringSegment value)
104+
/// <inheritdoc cref="Contains(StringSegment)"/>
105+
public bool Contains(char value)
89106
=> Segment.Contains(value, Type);
90107

91-
/// <inheritdoc cref="Contains(string)"/>
108+
/// <inheritdoc cref="Contains(StringSegment)"/>
92109
public bool Contains(ReadOnlySpan<char> value)
93110
=> Segment.Contains(value, Type);
94111

0 commit comments

Comments
 (0)