@@ -17,6 +17,17 @@ namespace System
17
17
{
18
18
public partial class String
19
19
{
20
+ // Avoid paying the init cost of all the IndexOfAnyValues unless they are actually used.
21
+ private static class IndexOfAnyValuesStorage
22
+ {
23
+ // The Unicode Standard, Sec. 5.8, Recommendation R4 and Table 5-2 state that the CR, LF,
24
+ // CRLF, NEL, LS, FF, and PS sequences are considered newline functions. That section
25
+ // also specifically excludes VT from the list of newline functions, so we do not include
26
+ // it in the needle list.
27
+ public static readonly IndexOfAnyValues < char > NewLineChars =
28
+ IndexOfAnyValues . Create ( "\r \n \f \u0085 \u2028 \u2029 " ) ;
29
+ }
30
+
20
31
private const int StackallocIntBufferSizeLimit = 128 ;
21
32
22
33
private static void FillStringChecked ( string dest , int destPos , string src )
@@ -1233,16 +1244,9 @@ internal static int IndexOfNewlineChar(ReadOnlySpan<char> text, out int stride)
1233
1244
// the haystack; or O(n) if no needle is found. This ensures that in the common case
1234
1245
// of this method being called within a loop, the worst-case runtime is O(n) rather than
1235
1246
// O(n^2), where n is the length of the input text.
1236
- //
1237
- // The Unicode Standard, Sec. 5.8, Recommendation R4 and Table 5-2 state that the CR, LF,
1238
- // CRLF, NEL, LS, FF, and PS sequences are considered newline functions. That section
1239
- // also specifically excludes VT from the list of newline functions, so we do not include
1240
- // it in the needle list.
1241
-
1242
- const string needles = "\r \n \f \u0085 \u2028 \u2029 " ;
1243
1247
1244
1248
stride = default ;
1245
- int idx = text . IndexOfAny ( needles ) ;
1249
+ int idx = text . IndexOfAny ( IndexOfAnyValuesStorage . NewLineChars ) ;
1246
1250
if ( ( uint ) idx < ( uint ) text . Length )
1247
1251
{
1248
1252
stride = 1 ; // needle found
0 commit comments