From 3600bce6a6bc505675fef6a1f52fac6234bd193c Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Fri, 10 Feb 2023 16:17:11 -0500 Subject: [PATCH] Use IndexOfAnyValues in another place in System.Text.Json --- .../src/System/Text/Json/Nodes/JsonObject.cs | 2 +- .../Serialization/Converters/Value/EnumConverter.cs | 4 +--- .../src/System/Text/Json/Serialization/ReadStack.cs | 11 ++++++++--- .../src/System/Text/Json/Serialization/WriteStack.cs | 2 +- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonObject.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonObject.cs index b1d711242773d4..0a5a41fa6d98a4 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonObject.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonObject.cs @@ -128,7 +128,7 @@ internal override void GetPath(List path, JsonNode? child) 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}']"); } diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/EnumConverter.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/EnumConverter.cs index 9f571702c6d8ae..fe4779f87190f0 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/EnumConverter.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/EnumConverter.cs @@ -15,8 +15,6 @@ internal sealed class EnumConverter : JsonPrimitiveConverter { 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 @@ public EnumConverter(EnumConverterOptions converterOptions, JsonNamingPolicy? na _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); } diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ReadStack.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ReadStack.cs index 0d6df0a8d3862e..9c4c4720e7c2b0 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ReadStack.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ReadStack.cs @@ -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 s_specialCharacters = IndexOfAnyValues.Create(SpecialCharacters); +#else + internal static ReadOnlySpan s_specialCharacters => SpecialCharacters.AsSpan(); +#endif /// /// Exposes the stackframe that is currently active. @@ -320,7 +325,7 @@ static void AppendPropertyName(StringBuilder sb, string? propertyName) { if (propertyName != null) { - if (propertyName.IndexOfAny(SpecialCharacters) != -1) + if (propertyName.AsSpan().IndexOfAny(s_specialCharacters) >= 0) { sb.Append(@"['"); sb.Append(propertyName); diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/WriteStack.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/WriteStack.cs index 1369c99d05898e..d741029bf6e47b 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/WriteStack.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/WriteStack.cs @@ -427,7 +427,7 @@ static void AppendPropertyName(StringBuilder sb, string? propertyName) { if (propertyName != null) { - if (propertyName.IndexOfAny(ReadStack.SpecialCharacters) != -1) + if (propertyName.AsSpan().IndexOfAny(ReadStack.s_specialCharacters) >= 0) { sb.Append(@"['"); sb.Append(propertyName);