Fixing Utf8Formatter.Float to support all the same format specifiers as the utf16...
authorTanner Gooding <tagoo@outlook.com>
Thu, 7 Feb 2019 17:12:08 +0000 (09:12 -0800)
committerGitHub <noreply@github.com>
Thu, 7 Feb 2019 17:12:08 +0000 (09:12 -0800)
* Fixing Utf8Formatter.Float to support all the same format specifiers as the utf16 formatter.

* Disabling some outdated CoreFX tests.

* Fixing TryFormatFloatingPoint to special-case format.IsDefault

Commit migrated from https://github.com/dotnet/coreclr/commit/752c8f157c75e9530d2b0d6b0f52b05049849bd5

src/coreclr/tests/CoreFX/CoreFX.issues.json
src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Float.cs

index c76dbe7..aa80e3f 100644 (file)
                     "name": "System.Buffers.Text.Tests.ParserTests.TestParserDouble",
                     "reason": "https://github.com/dotnet/coreclr/pull/20707"
                 },
+                {
+                    "name": "System.Buffers.Text.Tests.FormatterTests.TestGFormatWithPrecisionNotSupported",
+                    "reason": "https://github.com/dotnet/coreclr/pull/22434"
+                },
+                {
+                    "name": "System.Buffers.Text.Tests.FormatterTests.TestBadFormat",
+                    "reason": "https://github.com/dotnet/coreclr/pull/22434"
+                },
             ]
         }
     },
index e546a44..6dde973 100644 (file)
@@ -61,32 +61,14 @@ namespace System.Buffers.Text
 
         private static bool TryFormatFloatingPoint<T>(T value, Span<byte> destination, out int bytesWritten, StandardFormat format) where T : IFormattable, ISpanFormattable
         {
-            if (format.IsDefault)
-            {
-                format = 'G';
-            }
+            Span<char> formatText = stackalloc char[0];
 
-            switch (format.Symbol)
+            if (!format.IsDefault)
             {
-                case 'g':
-                case 'G':
-                    if (format.Precision != StandardFormat.NoPrecision)
-                        throw new NotSupportedException(SR.Argument_GWithPrecisionNotSupported);
-                    break;
-
-                case 'f':
-                case 'F':
-                case 'e':
-                case 'E':
-                    break;
-
-                default:
-                    return FormattingHelpers.TryFormatThrowFormatException(out bytesWritten);
+                formatText = stackalloc char[StandardFormat.FormatStringLength];
+                int formatTextLength = format.Format(formatText);
+                formatText = formatText.Slice(0, formatTextLength);
             }
-            
-            Span<char> formatText = stackalloc char[StandardFormat.FormatStringLength];
-            int formattedLength = format.Format(formatText);
-            formatText = formatText.Slice(0, formattedLength);
 
             // We first try to format into a stack-allocated buffer, and if it succeeds, we can avoid
             // all allocation.  If that fails, we fall back to allocating strings.  If it proves impactful,
@@ -98,7 +80,7 @@ namespace System.Buffers.Text
             ReadOnlySpan<char> utf16Text = stackalloc char[0];
 
             // Try to format into the stack buffer.  If we're successful, we can avoid all allocations.
-            if (value.TryFormat(stackBuffer, out formattedLength, formatText, CultureInfo.InvariantCulture))
+            if (value.TryFormat(stackBuffer, out int formattedLength, formatText, CultureInfo.InvariantCulture))
             {
                 utf16Text = stackBuffer.Slice(0, formattedLength);
             }