This repository was archived by the owner on Dec 24, 2022. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +16
-13
lines changed
Expand file tree Collapse file tree 2 files changed +16
-13
lines changed Original file line number Diff line number Diff line change @@ -1220,18 +1220,8 @@ public static IEnumerable<string> ReadLines(this string text)
12201220 }
12211221 }
12221222
1223- public static int CountOccurrencesOf ( this string text , char needle )
1224- {
1225- var chars = text . ToCharArray ( ) ;
1226- var count = 0 ;
1227- var length = chars . Length ;
1228- for ( var n = length - 1 ; n >= 0 ; n -- )
1229- {
1230- if ( chars [ n ] == needle )
1231- count ++ ;
1232- }
1233- return count ;
1234- }
1223+ public static int CountOccurrencesOf ( this string text , char needle ) =>
1224+ text . AsSpan ( ) . CountOccurrencesOf ( needle ) ;
12351225
12361226 public static string NormalizeNewLines ( this string text )
12371227 {
Original file line number Diff line number Diff line change @@ -643,6 +643,19 @@ public static List<string> ToStringList(this IEnumerable<ReadOnlyMemory<char>> f
643643 }
644644 }
645645 return to ;
646- }
646+ }
647+
648+ public static int CountOccurrencesOf ( this ReadOnlySpan < char > value , char needle )
649+ {
650+ var count = 0 ;
651+ var length = value . Length ;
652+ for ( var n = length - 1 ; n >= 0 ; n -- )
653+ {
654+ if ( value [ n ] == needle )
655+ count ++ ;
656+ }
657+ return count ;
658+ }
659+
647660 }
648661}
You can’t perform that action at this time.
0 commit comments