Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 47158cc

Browse files
authored
Use IndexOfAnyValues in CoreLib (#78678)
* Use IndexOfAnyValues in CoreLib * Avoid the init overhead of IndexOfAnyValues in string
1 parent e0aff76 commit 47158cc

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

src/coreclr/System.Private.CoreLib/src/System/TypeNameParser.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Buffers;
45
using System.Diagnostics;
56
using System.Diagnostics.CodeAnalysis;
67
using System.IO;
@@ -88,7 +89,8 @@ internal sealed partial class TypeNameParser : IDisposable
8889

8990
#region Private Data Members
9091
private readonly SafeTypeNameParserHandle m_NativeParser;
91-
private static readonly char[] SPECIAL_CHARS = { ',', '[', ']', '&', '*', '+', '\\' }; /* see typeparse.h */
92+
private const string SpecialChars = ",[]&*+\\"; // see typeparse.h
93+
private static readonly IndexOfAnyValues<char> s_specialChars = IndexOfAnyValues.Create(SpecialChars);
9294
#endregion
9395

9496
#region Constructor and Disposer
@@ -276,13 +278,18 @@ public void Dispose()
276278

277279
private static string EscapeTypeName(string name)
278280
{
279-
if (name.IndexOfAny(SPECIAL_CHARS) < 0)
281+
int specialCharIndex = name.AsSpan().IndexOfAny(s_specialChars);
282+
if (specialCharIndex < 0)
283+
{
280284
return name;
285+
}
281286

282287
var sb = new ValueStringBuilder(stackalloc char[64]);
283-
foreach (char c in name)
288+
sb.Append(name.AsSpan(0, specialCharIndex));
289+
290+
foreach (char c in name.AsSpan(specialCharIndex))
284291
{
285-
if (Array.IndexOf<char>(SPECIAL_CHARS, c) >= 0)
292+
if (SpecialChars.Contains(c))
286293
sb.Append('\\');
287294

288295
sb.Append(c);

src/libraries/System.Private.CoreLib/src/System/String.Manipulation.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@ namespace System
1717
{
1818
public partial class String
1919
{
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+
2031
private const int StackallocIntBufferSizeLimit = 128;
2132

2233
private static void FillStringChecked(string dest, int destPos, string src)
@@ -1233,16 +1244,9 @@ internal static int IndexOfNewlineChar(ReadOnlySpan<char> text, out int stride)
12331244
// the haystack; or O(n) if no needle is found. This ensures that in the common case
12341245
// of this method being called within a loop, the worst-case runtime is O(n) rather than
12351246
// 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";
12431247

12441248
stride = default;
1245-
int idx = text.IndexOfAny(needles);
1249+
int idx = text.IndexOfAny(IndexOfAnyValuesStorage.NewLineChars);
12461250
if ((uint)idx < (uint)text.Length)
12471251
{
12481252
stride = 1; // needle found

0 commit comments

Comments
 (0)