Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use IndexOfAnyValues in another place in System.Text.Json #81976

Merged
merged 1 commit into from
Feb 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ internal override void GetPath(List<string> 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}']");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ internal sealed class EnumConverter<T> : JsonPrimitiveConverter<T>
{
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;

Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: this could probably be moved to JsonReaderHelpers.cs with all the other relevant helpers are located. Not in scope for this PR though, feel free to ignore :-)

#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
stephentoub marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Exposes the stackframe that is currently active.
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down