From: David CantĂș Date: Thu, 17 Sep 2020 22:45:20 +0000 (-0700) Subject: Remove unnecessary logic to determine E format when parsing floating point values... X-Git-Tag: submit/tizen/20210909.063632~5439 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=527f9ae88a0ee216b44d556f9bdc84037fe0ebda;p=platform%2Fupstream%2Fdotnet%2Fruntime.git Remove unnecessary logic to determine E format when parsing floating point values on Utf8JsonReader and JsonDocument (#42298) --- diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.DbRow.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.DbRow.cs index d11c5a8..1a391e3 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.DbRow.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.DbRow.cs @@ -38,7 +38,6 @@ namespace System.Text.Json internal bool IsUnknownSize => _sizeOrLengthUnion == UnknownSize; /// - /// Number: Use scientific format. /// String/PropertyName: Unescaping is required. /// Array: At least one element is an object/array. /// Otherwise; false diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.cs index 633bfc7..5a472f5 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.cs @@ -586,9 +586,7 @@ namespace System.Text.Json ReadOnlySpan data = _utf8Json.Span; ReadOnlySpan segment = data.Slice(row.Location, row.SizeOrLength); - char standardFormat = row.HasComplexChildren ? JsonConstants.ScientificNotationFormat : default; - - if (Utf8Parser.TryParse(segment, out double tmp, out int bytesConsumed, standardFormat) && + if (Utf8Parser.TryParse(segment, out double tmp, out int bytesConsumed) && segment.Length == bytesConsumed) { value = tmp; @@ -610,9 +608,7 @@ namespace System.Text.Json ReadOnlySpan data = _utf8Json.Span; ReadOnlySpan segment = data.Slice(row.Location, row.SizeOrLength); - char standardFormat = row.HasComplexChildren ? JsonConstants.ScientificNotationFormat : default; - - if (Utf8Parser.TryParse(segment, out float tmp, out int bytesConsumed, standardFormat) && + if (Utf8Parser.TryParse(segment, out float tmp, out int bytesConsumed) && segment.Length == bytesConsumed) { value = tmp; @@ -634,9 +630,7 @@ namespace System.Text.Json ReadOnlySpan data = _utf8Json.Span; ReadOnlySpan segment = data.Slice(row.Location, row.SizeOrLength); - char standardFormat = row.HasComplexChildren ? JsonConstants.ScientificNotationFormat : default; - - if (Utf8Parser.TryParse(segment, out decimal tmp, out int bytesConsumed, standardFormat) && + if (Utf8Parser.TryParse(segment, out decimal tmp, out int bytesConsumed) && segment.Length == bytesConsumed) { value = tmp; @@ -1067,21 +1061,6 @@ namespace System.Text.Json else { database.Append(tokenType, tokenStart, reader.ValueSpan.Length); - - if (tokenType == JsonTokenType.Number) - { - switch (reader._numberFormat) - { - case JsonConstants.ScientificNotationFormat: - database.SetHasComplexChildren(database.Length - DbRow.Size); - break; - default: - Debug.Assert( - reader._numberFormat == default, - $"Unhandled numeric format {reader._numberFormat}"); - break; - } - } } } diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/JsonConstants.cs b/src/libraries/System.Text.Json/src/System/Text/Json/JsonConstants.cs index 2e1bb4e..e828113 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/JsonConstants.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/JsonConstants.cs @@ -88,8 +88,6 @@ namespace System.Text.Json public const int MinimumDateTimeParseLength = 10; // YYYY-MM-DD public const int MaximumEscapedDateTimeOffsetParseLength = MaxExpansionFactorWhileEscaping * MaximumDateTimeOffsetParseLength; - internal const char ScientificNotationFormat = 'e'; - // Encoding Helpers public const char HighSurrogateStart = '\ud800'; public const char HighSurrogateEnd = '\udbff'; diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Reader/JsonReaderHelper.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Reader/JsonReaderHelper.cs index dbea5c0..4ba1bff 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Reader/JsonReaderHelper.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Reader/JsonReaderHelper.cs @@ -322,21 +322,6 @@ namespace System.Text.Json return false; } - public static char GetFloatingPointStandardParseFormat(ReadOnlySpan span) - { - // Assume that 'e/E' is closer to the end. - int startIndex = span.Length - 1; - for (int i = startIndex; i >= 0; i--) - { - byte token = span[i]; - if (token == 'E' || token == 'e') - { - return JsonConstants.ScientificNotationFormat; - } - } - return default; - } - public static bool TryGetFloatingPointConstant(ReadOnlySpan span, out float value) { if (span.Length == 3) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Reader/JsonReaderState.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Reader/JsonReaderState.cs index 92f3913..5d215e8 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Reader/JsonReaderState.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Reader/JsonReaderState.cs @@ -17,7 +17,6 @@ namespace System.Text.Json internal long _bytePositionInLine; internal bool _inObject; internal bool _isNotPrimitive; - internal char _numberFormat; internal bool _stringHasEscaping; internal bool _trailingCommaBeforeComment; internal JsonTokenType _tokenType; @@ -43,7 +42,6 @@ namespace System.Text.Json _bytePositionInLine = default; _inObject = default; _isNotPrimitive = default; - _numberFormat = default; _stringHasEscaping = default; _trailingCommaBeforeComment = default; _tokenType = default; diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.MultiSegment.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.MultiSegment.cs index 33e3e78..e639610 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.MultiSegment.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.MultiSegment.cs @@ -32,7 +32,6 @@ namespace System.Text.Json _bytePositionInLine = state._bytePositionInLine; _inObject = state._inObject; _isNotPrimitive = state._isNotPrimitive; - _numberFormat = state._numberFormat; _stringHasEscaping = state._stringHasEscaping; _trailingCommaBeforeComment = state._trailingCommaBeforeComment; _tokenType = state._tokenType; @@ -1122,8 +1121,6 @@ namespace System.Text.Json // TODO: https://github.com/dotnet/runtime/issues/27837 Debug.Assert(data.Length > 0); - _numberFormat = default; - PartialStateForRollback rollBackState = CaptureState(); consumed = 0; @@ -1207,7 +1204,6 @@ namespace System.Text.Json Debug.Assert(nextByte == 'E' || nextByte == 'e'); i++; - _numberFormat = JsonConstants.ScientificNotationFormat; _bytePositionInLine++; signResult = ConsumeSignMultiSegment(ref data, ref i, rollBackState); diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.TryGet.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.TryGet.cs index b888bf3..faa0dc2 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.TryGet.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.TryGet.cs @@ -422,8 +422,7 @@ namespace System.Text.Json return value; } - char numberFormat = JsonReaderHelper.GetFloatingPointStandardParseFormat(span); - if (Utf8Parser.TryParse(span, out value, out int bytesConsumed, numberFormat) + if (Utf8Parser.TryParse(span, out value, out int bytesConsumed) && span.Length == bytesConsumed) { // NETCOREAPP implementation of the TryParse method above permits case-insenstive variants of the @@ -482,11 +481,10 @@ namespace System.Text.Json return value; } - char numberFormat = JsonReaderHelper.GetFloatingPointStandardParseFormat(span); - if (Utf8Parser.TryParse(span, out value, out int bytesConsumed, numberFormat) + if (Utf8Parser.TryParse(span, out value, out int bytesConsumed) && span.Length == bytesConsumed) { - // NETCOREAPP implmentation of the TryParse method above permits case-insenstive variants of the + // NETCOREAPP implementation of the TryParse method above permits case-insenstive variants of the // float constants "NaN", "Infinity", "-Infinity". This differs from the NETFRAMEWORK implementation. // The following logic reconciles the two implementations to enforce consistent behavior. if (!double.IsNaN(value) && !double.IsPositiveInfinity(value) && !double.IsNegativeInfinity(value)) @@ -536,15 +534,11 @@ namespace System.Text.Json internal decimal GetDecimalWithQuotes() { ReadOnlySpan span = GetUnescapedSpan(); - - char numberFormat = JsonReaderHelper.GetFloatingPointStandardParseFormat(span); - if (Utf8Parser.TryParse(span, out decimal value, out int bytesConsumed, numberFormat) - && span.Length == bytesConsumed) + if (!TryGetDecimalCore(out decimal value, span)) { - return value; + throw ThrowHelper.GetFormatException(NumericType.Decimal); } - - throw ThrowHelper.GetFormatException(NumericType.Decimal); + return value; } /// @@ -979,7 +973,7 @@ namespace System.Text.Json ReadOnlySpan span = HasValueSequence ? ValueSequence.ToArray() : ValueSpan; - if (Utf8Parser.TryParse(span, out float tmp, out int bytesConsumed, _numberFormat) + if (Utf8Parser.TryParse(span, out float tmp, out int bytesConsumed) && span.Length == bytesConsumed) { value = tmp; @@ -1009,7 +1003,7 @@ namespace System.Text.Json ReadOnlySpan span = HasValueSequence ? ValueSequence.ToArray() : ValueSpan; - if (Utf8Parser.TryParse(span, out double tmp, out int bytesConsumed, _numberFormat) + if (Utf8Parser.TryParse(span, out double tmp, out int bytesConsumed) && span.Length == bytesConsumed) { value = tmp; @@ -1038,8 +1032,13 @@ namespace System.Text.Json } ReadOnlySpan span = HasValueSequence ? ValueSequence.ToArray() : ValueSpan; + return TryGetDecimalCore(out value, span); + } - if (Utf8Parser.TryParse(span, out decimal tmp, out int bytesConsumed, _numberFormat) + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal bool TryGetDecimalCore(out decimal value, ReadOnlySpan span) + { + if (Utf8Parser.TryParse(span, out decimal tmp, out int bytesConsumed) && span.Length == bytesConsumed) { value = tmp; diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.cs index c6224ee..a25c3d5 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.cs @@ -33,7 +33,6 @@ namespace System.Text.Json private int _consumed; private bool _inObject; private bool _isNotPrimitive; - internal char _numberFormat; private JsonTokenType _tokenType; private JsonTokenType _previousTokenType; private JsonReaderOptions _readerOptions; @@ -183,7 +182,6 @@ namespace System.Text.Json _bytePositionInLine = _bytePositionInLine, _inObject = _inObject, _isNotPrimitive = _isNotPrimitive, - _numberFormat = _numberFormat, _stringHasEscaping = _stringHasEscaping, _trailingCommaBeforeComment = _trailingCommaBeforeComment, _tokenType = _tokenType, @@ -215,7 +213,6 @@ namespace System.Text.Json _bytePositionInLine = state._bytePositionInLine; _inObject = state._inObject; _isNotPrimitive = state._isNotPrimitive; - _numberFormat = state._numberFormat; _stringHasEscaping = state._stringHasEscaping; _trailingCommaBeforeComment = state._trailingCommaBeforeComment; _tokenType = state._tokenType; @@ -1415,7 +1412,6 @@ namespace System.Text.Json // TODO: https://github.com/dotnet/runtime/issues/27837 Debug.Assert(data.Length > 0); - _numberFormat = default; consumed = 0; int i = 0; @@ -1493,7 +1489,6 @@ namespace System.Text.Json Debug.Assert(nextByte == 'E' || nextByte == 'e'); i++; - _numberFormat = JsonConstants.ScientificNotationFormat; signResult = ConsumeSign(ref data, ref i); if (signResult == ConsumeNumberResult.NeedMoreData)