From 8e6972e024842c14d4fa6d9a7fdf60edff723447 Mon Sep 17 00:00:00 2001 From: tkp1n Date: Thu, 10 Jan 2019 18:36:39 +0100 Subject: [PATCH 1/2] Remove unnecessary checks in ConsumeNumber (#33294) --- .../Json/Reader/Utf8JsonReader.MultiSegment.cs | 17 +++++++---------- .../System/Text/Json/Reader/Utf8JsonReader.cs | 18 +++++++----------- 2 files changed, 14 insertions(+), 21 deletions(-) diff --git a/src/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.MultiSegment.cs b/src/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.MultiSegment.cs index 51f8c786e6ff..df09f205fecd 100644 --- a/src/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.MultiSegment.cs +++ b/src/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.MultiSegment.cs @@ -647,21 +647,18 @@ private bool ConsumeNumberMultiSegment() if (_consumed >= (uint)_buffer.Length) { + Debug.Assert(IsLastSpan); + if (!_isNotPrimitive) { return true; } - if (IsLastSpan || !GetNextSpan()) - { - ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.ExpectedEndOfDigitNotFound, _buffer[_consumed - 1]); - } - } - // TODO: https://github.com/dotnet/corefx/issues/33294 - if (JsonConstants.Delimiters.IndexOf(_buffer[_consumed]) < 0) - { - ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.ExpectedEndOfDigitNotFound, _buffer[_consumed]); + ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.ExpectedEndOfDigitNotFound, _buffer[_consumed - 1]); } + + Debug.Assert(JsonConstants.Delimiters.IndexOf(_buffer[_consumed]) >= 0); + return true; } @@ -1306,7 +1303,7 @@ private ConsumeNumberResult ConsumeZeroMultiSegment(ref ReadOnlySpan data, if (IsLastSpan) { // A payload containing a single value: "0" is valid - // If we are v with multi-value JSON, + // If we are dealing with multi-value JSON, // ConsumeNumber will validate that we have a delimiter following the "0". return ConsumeNumberResult.Success; } diff --git a/src/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.cs b/src/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.cs index 0b60c3f902ac..8965f511d890 100644 --- a/src/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.cs +++ b/src/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.cs @@ -685,22 +685,18 @@ private bool ConsumeNumber() if (_consumed >= (uint)_buffer.Length) { + Debug.Assert(IsLastSpan); + if (!_isNotPrimitive) { return true; } - if (IsLastSpan) - { - ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.ExpectedEndOfDigitNotFound, _buffer[_consumed - 1]); - } - return false; - } - // TODO: https://github.com/dotnet/corefx/issues/33294 - if (JsonConstants.Delimiters.IndexOf(_buffer[_consumed]) < 0) - { - ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.ExpectedEndOfDigitNotFound, _buffer[_consumed]); + ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.ExpectedEndOfDigitNotFound, _buffer[_consumed - 1]); } + + Debug.Assert(JsonConstants.Delimiters.IndexOf(_buffer[_consumed]) >= 0); + return true; } @@ -1058,7 +1054,7 @@ private ConsumeNumberResult ConsumeZero(ref ReadOnlySpan data, ref int i) if (IsLastSpan) { // A payload containing a single value: "0" is valid - // If we are v with multi-value JSON, + // If we are dealing with multi-value JSON, // ConsumeNumber will validate that we have a delimiter following the "0". return ConsumeNumberResult.Success; } From ecffe5046608086201c8716ae132ddacbd7182d8 Mon Sep 17 00:00:00 2001 From: tkp1n Date: Fri, 18 Jan 2019 03:20:18 +0100 Subject: [PATCH 2/2] Remove unnecessary _isNotPrimitive check --- .../System/Text/Json/Reader/Utf8JsonReader.MultiSegment.cs | 5 ----- .../src/System/Text/Json/Reader/Utf8JsonReader.cs | 5 ----- 2 files changed, 10 deletions(-) diff --git a/src/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.MultiSegment.cs b/src/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.MultiSegment.cs index df09f205fecd..8c268864775e 100644 --- a/src/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.MultiSegment.cs +++ b/src/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.MultiSegment.cs @@ -649,11 +649,6 @@ private bool ConsumeNumberMultiSegment() { Debug.Assert(IsLastSpan); - if (!_isNotPrimitive) - { - return true; - } - ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.ExpectedEndOfDigitNotFound, _buffer[_consumed - 1]); } diff --git a/src/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.cs b/src/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.cs index 8965f511d890..11ace1505428 100644 --- a/src/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.cs +++ b/src/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.cs @@ -687,11 +687,6 @@ private bool ConsumeNumber() { Debug.Assert(IsLastSpan); - if (!_isNotPrimitive) - { - return true; - } - ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.ExpectedEndOfDigitNotFound, _buffer[_consumed - 1]); }