From f5a91d0928522289ef1e9e999ef950afa2439580 Mon Sep 17 00:00:00 2001 From: Tanner Gooding Date: Thu, 7 Feb 2019 09:12:08 -0800 Subject: [PATCH] Fixing Utf8Formatter.Float to support all the same format specifiers as the utf16 formatter. (dotnet/coreclr#22434) * 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 | 8 ++++++ .../Text/Utf8Formatter/Utf8Formatter.Float.cs | 30 +++++----------------- 2 files changed, 14 insertions(+), 24 deletions(-) diff --git a/src/coreclr/tests/CoreFX/CoreFX.issues.json b/src/coreclr/tests/CoreFX/CoreFX.issues.json index c76dbe7..aa80e3f 100644 --- a/src/coreclr/tests/CoreFX/CoreFX.issues.json +++ b/src/coreclr/tests/CoreFX/CoreFX.issues.json @@ -751,6 +751,14 @@ "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" + }, ] } }, diff --git a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Float.cs b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Float.cs index e546a44..6dde973 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Float.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Float.cs @@ -61,32 +61,14 @@ namespace System.Buffers.Text private static bool TryFormatFloatingPoint(T value, Span destination, out int bytesWritten, StandardFormat format) where T : IFormattable, ISpanFormattable { - if (format.IsDefault) - { - format = 'G'; - } + Span 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 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 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); } -- 2.7.4