Skip to content

Commit

Permalink
Fixing Utf8Formatter.Float to support all the same format specifiers …
Browse files Browse the repository at this point in the history
…as the utf16 formatter. (#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

Signed-off-by: dotnet-bot <[email protected]>
  • Loading branch information
tannergooding authored and dotnet-bot committed Feb 8, 2019
1 parent c4826f7 commit e01e2c8
Showing 1 changed file with 6 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,32 +61,14 @@ public static bool TryFormat(float value, Span<byte> destination, out int bytesW

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,
Expand All @@ -98,7 +80,7 @@ private static bool TryFormatFloatingPoint<T>(T value, Span<byte> destination, o
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);
}
Expand Down

0 comments on commit e01e2c8

Please sign in to comment.