From c61e3d26188c644754bf04111efe7a2eb7ddd89f Mon Sep 17 00:00:00 2001 From: xtqqczze Date: Tue, 11 May 2021 12:42:24 +0100 Subject: [PATCH 1/2] Migrate `Utf8Formatter` to `Span.Fill` --- .../Buffers/Text/Utf8Formatter/FormattingHelpers.cs | 13 ------------- .../Utf8Formatter.Integer.Unsigned.D.cs | 2 +- .../Utf8Formatter.Integer.Unsigned.N.cs | 2 +- 3 files changed, 2 insertions(+), 15 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/FormattingHelpers.cs b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/FormattingHelpers.cs index 6316a19b33559e..991ba2448451c7 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/FormattingHelpers.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/FormattingHelpers.cs @@ -33,19 +33,6 @@ public static char GetSymbolOrDefault(in StandardFormat format, char defaultSymb #region UTF-8 Helper methods - /// - /// Fills a buffer with the ASCII character '0' (0x30). - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void FillWithAsciiZeros(Span buffer) - { - // This is a faster implementation of Span.Fill(). - for (int i = 0; i < buffer.Length; i++) - { - buffer[i] = (byte)'0'; - } - } - [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void WriteDigits(ulong value, Span buffer) { diff --git a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Integer.Unsigned.D.cs b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Integer.Unsigned.D.cs index 2a2059674f9163..0a941e4978e116 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Integer.Unsigned.D.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Integer.Unsigned.D.cs @@ -43,7 +43,7 @@ private static bool TryFormatUInt64D(ulong value, byte precision, Span des if (leadingZeroCount > 0) { - FormattingHelpers.FillWithAsciiZeros(destination.Slice(0, leadingZeroCount)); + destination.Slice(0, leadingZeroCount).Fill((byte)'0'); } FormattingHelpers.WriteDigits(value, destination.Slice(leadingZeroCount, digitCount)); diff --git a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Integer.Unsigned.N.cs b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Integer.Unsigned.N.cs index c7f564ae6c9f60..e072151b6339b9 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Integer.Unsigned.N.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Integer.Unsigned.N.cs @@ -48,7 +48,7 @@ private static bool TryFormatUInt64N(ulong value, byte precision, Span des if (trailingZeroCount > 0) { destination[digitCount + commaCount] = Utf8Constants.Period; - FormattingHelpers.FillWithAsciiZeros(destination.Slice(digitCount + commaCount + 1, trailingZeroCount)); + destination.Slice(digitCount + commaCount + 1, trailingZeroCount).Fill((byte)'0'); } return true; From 8578d06dcd6df32afb25ce2babde559a4177d3fe Mon Sep 17 00:00:00 2001 From: xtqqczze Date: Tue, 11 May 2021 12:43:01 +0100 Subject: [PATCH 2/2] Add comments --- .../Text/Utf8Formatter/Utf8Formatter.Integer.Unsigned.D.cs | 1 + .../Text/Utf8Formatter/Utf8Formatter.Integer.Unsigned.N.cs | 2 ++ 2 files changed, 3 insertions(+) diff --git a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Integer.Unsigned.D.cs b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Integer.Unsigned.D.cs index 0a941e4978e116..c449b3341af840 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Integer.Unsigned.D.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Integer.Unsigned.D.cs @@ -43,6 +43,7 @@ private static bool TryFormatUInt64D(ulong value, byte precision, Span des if (leadingZeroCount > 0) { + // Fill with ASCII zeros destination.Slice(0, leadingZeroCount).Fill((byte)'0'); } FormattingHelpers.WriteDigits(value, destination.Slice(leadingZeroCount, digitCount)); diff --git a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Integer.Unsigned.N.cs b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Integer.Unsigned.N.cs index e072151b6339b9..f28935a197758d 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Integer.Unsigned.N.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Integer.Unsigned.N.cs @@ -48,6 +48,8 @@ private static bool TryFormatUInt64N(ulong value, byte precision, Span des if (trailingZeroCount > 0) { destination[digitCount + commaCount] = Utf8Constants.Period; + + // Fill with ASCII zeros destination.Slice(digitCount + commaCount + 1, trailingZeroCount).Fill((byte)'0'); }