Skip to content
Open
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
@@ -1,6 +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.Runtime.CompilerServices;

namespace System.Text.Json
{
internal static partial class JsonConstants
Expand Down Expand Up @@ -48,9 +50,25 @@ internal static partial class JsonConstants
// Used to search for the end of a number
public static ReadOnlySpan<byte> Delimiters => ",}] \n\r\t/"u8;

/// <summary>
/// Fast inline check for JSON number delimiters, avoiding the overhead of
/// ReadOnlySpan.Contains() linear search on every digit.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsDelimiter(byte b) =>
b is (byte)',' or (byte)'}' or (byte)']' or (byte)'/' or (byte)' ' or (byte)'\n' or (byte)'\r' or (byte)'\t';

// Explicitly skipping ReverseSolidus since that is handled separately
public static ReadOnlySpan<byte> EscapableChars => "\"nrt/ubf"u8;

/// <summary>
/// Fast inline check for valid JSON escape characters, avoiding
/// ReadOnlySpan.IndexOf() linear search on every escape sequence.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsEscapableChar(byte b) =>
b is (byte)'"' or (byte)'n' or (byte)'r' or (byte)'t' or (byte)'/' or (byte)'u' or (byte)'b' or (byte)'f';

public const int RemoveFlagsBitMask = 0x7FFFFFFF;

// In the worst case, an ASCII character represented as a single utf-8 byte could expand 6x when escaped.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -855,8 +855,7 @@ private bool ConsumeStringNextSegment()
}
else if (nextCharEscaped)
{
int index = JsonConstants.EscapableChars.IndexOf(currentByte);
if (index == -1)
if (!JsonConstants.IsEscapableChar(currentByte))
{
RollBackState(rollBackState, isError: true);
ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.InvalidCharacterAfterEscapeWithinString, currentByte);
Expand Down Expand Up @@ -992,8 +991,7 @@ private bool ConsumeStringAndValidateMultiSegment(ReadOnlySpan<byte> data, int i
}
else if (nextCharEscaped)
{
int index = JsonConstants.EscapableChars.IndexOf(currentByte);
if (index == -1)
if (!JsonConstants.IsEscapableChar(currentByte))
{
RollBackState(rollBackState, isError: true);
ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.InvalidCharacterAfterEscapeWithinString, currentByte);
Expand Down Expand Up @@ -1306,7 +1304,7 @@ private ConsumeNumberResult ConsumeZeroMultiSegment(ref ReadOnlySpan<byte> data,
if (i < data.Length)
{
nextByte = data[i];
if (JsonConstants.Delimiters.Contains(nextByte))
if (JsonConstants.IsDelimiter(nextByte))
{
return ConsumeNumberResult.Success;
}
Expand Down Expand Up @@ -1335,7 +1333,7 @@ private ConsumeNumberResult ConsumeZeroMultiSegment(ref ReadOnlySpan<byte> data,
i = 0;
data = _buffer;
nextByte = data[i];
if (JsonConstants.Delimiters.Contains(nextByte))
if (JsonConstants.IsDelimiter(nextByte))
{
return ConsumeNumberResult.Success;
}
Expand Down Expand Up @@ -1422,7 +1420,7 @@ private ConsumeNumberResult ConsumeIntegerDigitsMultiSegment(ref ReadOnlySpan<by
_bytePositionInLine += counter;
}

if (JsonConstants.Delimiters.Contains(nextByte))
if (JsonConstants.IsDelimiter(nextByte))
{
return ConsumeNumberResult.Success;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1350,8 +1350,7 @@ private bool ConsumeStringAndValidate(ReadOnlySpan<byte> data, int idx)
}
else if (nextCharEscaped)
{
int index = JsonConstants.EscapableChars.IndexOf(currentByte);
if (index == -1)
if (!JsonConstants.IsEscapableChar(currentByte))
{
ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.InvalidCharacterAfterEscapeWithinString, currentByte);
}
Expand Down Expand Up @@ -1570,7 +1569,7 @@ private ConsumeNumberResult ConsumeZero(ref ReadOnlySpan<byte> data, scoped ref
if (i < data.Length)
{
nextByte = data[i];
if (JsonConstants.Delimiters.Contains(nextByte))
if (JsonConstants.IsDelimiter(nextByte))
{
return ConsumeNumberResult.Success;
}
Expand Down Expand Up @@ -1626,7 +1625,7 @@ private ConsumeNumberResult ConsumeIntegerDigits(ref ReadOnlySpan<byte> data, sc
return ConsumeNumberResult.NeedMoreData;
}
}
if (JsonConstants.Delimiters.Contains(nextByte))
if (JsonConstants.IsDelimiter(nextByte))
{
return ConsumeNumberResult.Success;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ private bool TryParseEnumFromString(ref Utf8JsonReader reader, out T result)
goto End;
}

if (JsonHelpers.IntegerRegex.IsMatch(source))
if (IsIntegerLike(source))
{
// We found an integer that is not an enum field name.
if ((_converterOptions & EnumConverterOptions.AllowNumbers) != 0)
Expand Down Expand Up @@ -390,6 +390,43 @@ private static T ConvertFromUInt64(ulong value)
};
}

/// <summary>
/// Determines whether the string looks like an integer value (optional leading/trailing whitespace,
/// optional sign, followed by digits only) as a fast replacement for regex-based integer detection.
/// Matches the behavior of the regex pattern: ^\s*(?:\+|\-)?[0-9]+\s*$
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool IsIntegerLike(ReadOnlySpan<char> source)
{
ReadOnlySpan<char> trimmed = source.Trim();
if (trimmed.IsEmpty)
{
return false;
}

int i = 0;
char first = trimmed[0];
if (first is '-' or '+')
{
i = 1;
if (i >= trimmed.Length)
{
return false;
}
}

for (; i < trimmed.Length; i++)
{
char c = trimmed[i];
if (c < '0' || c > '9')
{
return false;
}
}

return true;
}

/// <summary>
/// Attempt to format the enum value as a comma-separated string of flag values, or returns false if not a valid flag combination.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public sealed partial class Utf8JsonWriter

private void ClearPartialStringData() => _partialStringDataLength = 0;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void ValidateWritingValue()
{
if (!CanWriteValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1223,6 +1223,7 @@ private void FirstCallToGetMemory(int requiredSize)
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void SetFlagToAddListSeparatorBeforeNextItem()
{
_currentDepth |= 1 << 31;
Expand Down