File tree Expand file tree Collapse file tree 1 file changed +36
-5
lines changed
Expand file tree Collapse file tree 1 file changed +36
-5
lines changed Original file line number Diff line number Diff line change @@ -15,11 +15,12 @@ namespace SimdUnicodeBenchmarks
1515 public class Checker
1616 {
1717 List < char [ ] > names ;
18- List < bool > results ;
19- public static bool RuntimeIsAsciiApproach ( ReadOnlySpan < char > s )
20- {
21- // The runtime as of NET 8.0 has a dedicated method for this, but
22- // it is not available prior to that, so let us branch.
18+ List < bool > results ;
19+
20+ public static bool RuntimeIsAsciiApproach ( ReadOnlySpan < char > s )
21+ {
22+ // The runtime as of NET 8.0 has a dedicated method for this, but
23+ // it is not available prior to that, so let us branch.
2324#if NET8_0_OR_GREATER
2425 return Ascii . IsValid ( s ) ;
2526#else
@@ -99,6 +100,36 @@ public void RuntimeIsAscii()
99100 }
100101 }
101102
103+
104+ [ Benchmark ]
105+ public void TestErrorGetIndexOfFirstNonAsciiByteBenchmark ( )
106+ {
107+ foreach ( char [ ] chars in names )
108+ {
109+ byte [ ] ascii = Encoding . UTF8 . GetBytes ( chars ) ;
110+
111+ for ( int i = 0 ; i < ascii . Length ; i ++ )
112+ {
113+ ascii [ i ] += 0b10000000 ;
114+
115+ unsafe
116+ {
117+ fixed ( byte * pAscii = ascii )
118+ {
119+ nuint result = Ascii . GetIndexOfFirstNonAsciiByte ( pAscii , ( nuint ) ascii . Length ) ;
120+ if ( result != ( nuint ) i )
121+ {
122+ throw new Exception ( $ "Expected non-ASCII character at index { i } , but found at index { result } ") ;
123+ }
124+ }
125+ }
126+
127+ ascii [ i ] -= 0b10000000 ;
128+ }
129+ }
130+ }
131+
132+
102133 }
103134
104135 public class Program
You can’t perform that action at this time.
0 commit comments