Use IndexOfAnyValues in another place in System.Text.Json (#81976)
authorStephen Toub <stoub@microsoft.com>
Sat, 11 Feb 2023 17:46:38 +0000 (12:46 -0500)
committerGitHub <noreply@github.com>
Sat, 11 Feb 2023 17:46:38 +0000 (12:46 -0500)
src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonObject.cs
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/EnumConverter.cs
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ReadStack.cs
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/WriteStack.cs

index b1d7112..0a5a41f 100644 (file)
@@ -128,7 +128,7 @@ namespace System.Text.Json.Nodes
                 InitializeIfRequired();
                 Debug.Assert(_dictionary != null);
                 string propertyName = _dictionary.FindValue(child)!.Value.Key;
-                if (propertyName.IndexOfAny(ReadStack.SpecialCharacters) != -1)
+                if (propertyName.AsSpan().IndexOfAny(ReadStack.s_specialCharacters) >= 0)
                 {
                     path.Add($"['{propertyName}']");
                 }
index 9f57170..fe4779f 100644 (file)
@@ -15,8 +15,6 @@ namespace System.Text.Json.Serialization.Converters
     {
         private static readonly TypeCode s_enumTypeCode = Type.GetTypeCode(typeof(T));
 
-        private static readonly char[] s_specialChars = new[] { ',', ' ' };
-
         // Odd type codes are conveniently signed types (for enum backing types).
         private static readonly bool s_isSignedEnum = ((int)s_enumTypeCode % 2) == 1;
 
@@ -89,7 +87,7 @@ namespace System.Text.Json.Serialization.Converters
                 _nameCacheForReading?.TryAdd(jsonName, value);
 
                 // If enum contains special char, make it failed to serialize or deserialize.
-                if (name.IndexOfAny(s_specialChars) != -1)
+                if (name.AsSpan().IndexOfAny(',', ' ') >= 0)
                 {
                     ThrowHelper.ThrowInvalidOperationException_InvalidEnumTypeWithSpecialChar(typeof(T), name);
                 }
index 0d6df0a..9c4c472 100644 (file)
@@ -1,8 +1,8 @@
 // Licensed to the .NET Foundation under one or more agreements.
 // The .NET Foundation licenses this file to you under the MIT license.
 
+using System.Buffers;
 using System.Collections;
-using System.Collections.Generic;
 using System.Diagnostics;
 using System.Runtime.CompilerServices;
 using System.Runtime.InteropServices;
@@ -15,7 +15,12 @@ namespace System.Text.Json
     [DebuggerDisplay("{DebuggerDisplay,nq}")]
     internal struct ReadStack
     {
-        internal static readonly char[] SpecialCharacters = { '.', ' ', '\'', '/', '"', '[', ']', '(', ')', '\t', '\n', '\r', '\f', '\b', '\\', '\u0085', '\u2028', '\u2029' };
+        private const string SpecialCharacters = ". '/\"[]()\t\n\r\f\b\\\u0085\u2028\u2029";
+#if NET8_0_OR_GREATER
+        internal static readonly IndexOfAnyValues<char> s_specialCharacters = IndexOfAnyValues.Create(SpecialCharacters);
+#else
+        internal static ReadOnlySpan<char> s_specialCharacters => SpecialCharacters.AsSpan();
+#endif
 
         /// <summary>
         /// Exposes the stackframe that is currently active.
@@ -320,7 +325,7 @@ namespace System.Text.Json
             {
                 if (propertyName != null)
                 {
-                    if (propertyName.IndexOfAny(SpecialCharacters) != -1)
+                    if (propertyName.AsSpan().IndexOfAny(s_specialCharacters) >= 0)
                     {
                         sb.Append(@"['");
                         sb.Append(propertyName);
index 1369c99..d741029 100644 (file)
@@ -427,7 +427,7 @@ namespace System.Text.Json
             {
                 if (propertyName != null)
                 {
-                    if (propertyName.IndexOfAny(ReadStack.SpecialCharacters) != -1)
+                    if (propertyName.AsSpan().IndexOfAny(ReadStack.s_specialCharacters) >= 0)
                     {
                         sb.Append(@"['");
                         sb.Append(propertyName);