diff --git a/src/System.Private.CoreLib/shared/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Float.cs b/src/System.Private.CoreLib/shared/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Float.cs index e546a444aaf..6dde973a897 100644 --- a/src/System.Private.CoreLib/shared/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Float.cs +++ b/src/System.Private.CoreLib/shared/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Float.cs @@ -61,32 +61,14 @@ public static bool TryFormat(float value, Span destination, out int bytesW 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 @@ private static bool TryFormatFloatingPoint(T value, Span destination, o 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); }