Remove unnecessary logic to determine E format when parsing floating point values...
authorDavid CantĂș <dacantu@microsoft.com>
Thu, 17 Sep 2020 22:45:20 +0000 (15:45 -0700)
committerGitHub <noreply@github.com>
Thu, 17 Sep 2020 22:45:20 +0000 (15:45 -0700)
src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.DbRow.cs
src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.cs
src/libraries/System.Text.Json/src/System/Text/Json/JsonConstants.cs
src/libraries/System.Text.Json/src/System/Text/Json/Reader/JsonReaderHelper.cs
src/libraries/System.Text.Json/src/System/Text/Json/Reader/JsonReaderState.cs
src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.MultiSegment.cs
src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.TryGet.cs
src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.cs

index d11c5a8..1a391e3 100644 (file)
@@ -38,7 +38,6 @@ namespace System.Text.Json
             internal bool IsUnknownSize => _sizeOrLengthUnion == UnknownSize;
 
             /// <summary>
-            /// Number: Use scientific format.
             /// String/PropertyName: Unescaping is required.
             /// Array: At least one element is an object/array.
             /// Otherwise; false
index 633bfc7..5a472f5 100644 (file)
@@ -586,9 +586,7 @@ namespace System.Text.Json
             ReadOnlySpan<byte> data = _utf8Json.Span;
             ReadOnlySpan<byte> 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<byte> data = _utf8Json.Span;
             ReadOnlySpan<byte> 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<byte> data = _utf8Json.Span;
             ReadOnlySpan<byte> 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;
-                            }
-                        }
                     }
                 }
 
index 2e1bb4e..e828113 100644 (file)
@@ -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';
index dbea5c0..4ba1bff 100644 (file)
@@ -322,21 +322,6 @@ namespace System.Text.Json
             return false;
         }
 
-        public static char GetFloatingPointStandardParseFormat(ReadOnlySpan<byte> 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<byte> span, out float value)
         {
             if (span.Length == 3)
index 92f3913..5d215e8 100644 (file)
@@ -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;
index 33e3e78..e639610 100644 (file)
@@ -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);
index b888bf3..faa0dc2 100644 (file)
@@ -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<byte> 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;
         }
 
         /// <summary>
@@ -979,7 +973,7 @@ namespace System.Text.Json
 
             ReadOnlySpan<byte> 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<byte> 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<byte> 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<byte> span)
+        {
+            if (Utf8Parser.TryParse(span, out decimal tmp, out int bytesConsumed)
                 && span.Length == bytesConsumed)
             {
                 value = tmp;
index c6224ee..a25c3d5 100644 (file)
@@ -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)