File tree Expand file tree Collapse file tree 2 files changed +67
-0
lines changed Expand file tree Collapse file tree 2 files changed +67
-0
lines changed Original file line number Diff line number Diff line change @@ -47,5 +47,48 @@ public static int Count<T>(this ReadOnlySpan<T> source, Predicate<T> predicate)
47
47
48
48
return count ;
49
49
}
50
+
51
+ #if ! NET8_0_OR_GREATER
52
+ /// <summary>Counts the number of times the specified <paramref name="value"/> occurs in the <paramref name="source"/>.</summary>
53
+ /// <typeparam name="T">The element type of the span.</typeparam>
54
+ /// <param name="source">A <see cref="ReadOnlySpan{T}"/> whose elements are to be counted.</param>
55
+ /// <param name="value">The value for which to search.</param>
56
+ /// <returns>The number of elements eqaul to <paramref name="value"/> in <paramref name="source"/>.</returns>
57
+ /// <exception cref="OverflowException">The number of elements in <paramref name="source"/> is larger than <see cref="int.MaxValue"/>.</exception>
58
+ public static int Count < T > ( this ReadOnlySpan < T > source , T value ) where T : IEquatable < T >
59
+ {
60
+ int count = 0 ;
61
+
62
+ foreach ( var item in source )
63
+ {
64
+ if ( item . Equals ( value ) )
65
+ {
66
+ count ++ ;
67
+ }
68
+ }
69
+
70
+ return count ;
71
+ }
72
+
73
+ /// <summary>Counts the number of times the specified <paramref name="value"/> occurs in the <paramref name="source"/>.</summary>
74
+ /// <typeparam name="T">The element type of the span.</typeparam>
75
+ /// <param name="source">A <see cref="ReadOnlySpan{T}"/> whose elements are to be counted.</param>
76
+ /// <param name="value">The value for which to search.</param>
77
+ /// <returns>The number of elements eqaul to <paramref name="value"/> in <paramref name="source"/>.</returns>
78
+ /// <exception cref="OverflowException">The number of elements in <paramref name="source"/> is larger than <see cref="int.MaxValue"/>.</exception>
79
+ public static int Count < T > ( this ReadOnlySpan < T > source , ReadOnlySpan < T > value ) where T : IEquatable < T >
80
+ {
81
+ int count = 0 ;
82
+ int current = 0 ;
83
+
84
+ while ( ( current = source . IndexOf ( value ) ) != - 1 )
85
+ {
86
+ source = source . Slice ( current + value . Length ) ;
87
+ count ++ ;
88
+ }
89
+
90
+ return count ;
91
+ }
92
+ #endif
50
93
}
51
94
}
Original file line number Diff line number Diff line change @@ -29,5 +29,29 @@ public static int Count<T>(this Span<T> source, Predicate<T> predicate)
29
29
{
30
30
return ReadOnlySpanExtensions . Count ( source , predicate ) ;
31
31
}
32
+
33
+ #if ! NET8_0_OR_GREATER
34
+ /// <summary>Counts the number of times the specified <paramref name="value"/> occurs in the <paramref name="source"/>.</summary>
35
+ /// <typeparam name="T">The element type of the span.</typeparam>
36
+ /// <param name="source">A <see cref="Span{T}"/> whose elements are to be counted.</param>
37
+ /// <param name="value">The value for which to search.</param>
38
+ /// <returns>The number of elements eqaul to <paramref name="value"/> in <paramref name="source"/>.</returns>
39
+ /// <exception cref="OverflowException">The number of elements in <paramref name="source"/> is larger than <see cref="int.MaxValue"/>.</exception>
40
+ public static int Count < T > ( this Span < T > source , T value ) where T : IEquatable < T >
41
+ {
42
+ return ReadOnlySpanExtensions . Count ( source , value ) ;
43
+ }
44
+
45
+ /// <summary>Counts the number of times the specified <paramref name="value"/> occurs in the <paramref name="source"/>.</summary>
46
+ /// <typeparam name="T">The element type of the span.</typeparam>
47
+ /// <param name="source">A <see cref="Span{T}"/> whose elements are to be counted.</param>
48
+ /// <param name="value">The value for which to search.</param>
49
+ /// <returns>The number of elements eqaul to <paramref name="value"/> in <paramref name="source"/>.</returns>
50
+ /// <exception cref="OverflowException">The number of elements in <paramref name="source"/> is larger than <see cref="int.MaxValue"/>.</exception>
51
+ public static int Count < T > ( this Span < T > source , ReadOnlySpan < T > value ) where T : IEquatable < T >
52
+ {
53
+ return ReadOnlySpanExtensions . Count ( source , value ) ;
54
+ }
55
+ #endif
32
56
}
33
57
}
You can’t perform that action at this time.
0 commit comments