< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 118
Coverable lines: 118
Total lines: 746
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 132
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

C:\h\w\9FD50923\w\C0A20A61\e\runtime-utils\Runner\runtime\src\libraries\System.Private.CoreLib\src\System\Buffers\Text\Utf8Formatter\Utf8Formatter.Boolean.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4namespace System.Buffers.Text
 5{
 6    public static partial class Utf8Formatter
 7    {
 8        /// <summary>
 9        /// Formats a Boolean as a UTF-8 string.
 10        /// </summary>
 11        /// <param name="value">Value to format</param>
 12        /// <param name="destination">Buffer to write the UTF-8 formatted value to</param>
 13        /// <param name="bytesWritten">Receives the length of the formatted text in bytes</param>
 14        /// <param name="format">The standard format to use</param>
 15        /// <returns>
 16        /// true for success. "bytesWritten" contains the length of the formatted text in bytes.
 17        /// false if buffer was too short. Iteratively increase the size of the buffer and retry until it succeeds.
 18        /// </returns>
 19        /// <remarks>
 20        /// Formats supported:
 21        ///     G (default)   True/False
 22        ///     l             true/false
 23        /// </remarks>
 24        /// <exceptions>
 25        /// <cref>System.FormatException</cref> if the format is not valid for this data type.
 26        /// </exceptions>
 27        public static bool TryFormat(bool value, Span<byte> destination, out int bytesWritten, StandardFormat format = d
 28        {
 029            char symbol = FormattingHelpers.GetSymbolOrDefault(format, 'G');
 30
 031            if (value)
 32            {
 033                if (symbol == 'G')
 34                {
 035                    if (!"True"u8.TryCopyTo(destination))
 36                    {
 037                        goto BufferTooSmall;
 38                    }
 39                }
 040                else if (symbol == 'l')
 41                {
 042                    if (!"true"u8.TryCopyTo(destination))
 43                    {
 44                        goto BufferTooSmall;
 45                    }
 46                }
 47                else
 48                {
 49                    goto BadFormat;
 50                }
 51
 052                bytesWritten = 4;
 053                return true;
 54            }
 55            else
 56            {
 057                if (symbol == 'G')
 58                {
 059                    if (!"False"u8.TryCopyTo(destination))
 60                    {
 061                        goto BufferTooSmall;
 62                    }
 63                }
 064                else if (symbol == 'l')
 65                {
 066                    if (!"false"u8.TryCopyTo(destination))
 67                    {
 68                        goto BufferTooSmall;
 69                    }
 70                }
 71                else
 72                {
 73                    goto BadFormat;
 74                }
 75
 076                bytesWritten = 5;
 077                return true;
 78            }
 79
 80        BadFormat:
 081            ThrowHelper.ThrowFormatException_BadFormatSpecifier();
 82
 83        BufferTooSmall:
 084            bytesWritten = 0;
 085            return false;
 86        }
 87    }
 88}

C:\h\w\9FD50923\w\C0A20A61\e\runtime-utils\Runner\runtime\src\libraries\System.Private.CoreLib\src\System\Buffers\Text\Utf8Formatter\Utf8Formatter.Date.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.Diagnostics;
 5using System.Text;
 6
 7namespace System.Buffers.Text
 8{
 9    public static partial class Utf8Formatter
 10    {
 11        /// <summary>
 12        /// Formats a DateTimeOffset as a UTF-8 string.
 13        /// </summary>
 14        /// <param name="value">Value to format</param>
 15        /// <param name="destination">Buffer to write the UTF-8 formatted value to</param>
 16        /// <param name="bytesWritten">Receives the length of the formatted text in bytes</param>
 17        /// <param name="format">The standard format to use</param>
 18        /// <returns>
 19        /// true for success. "bytesWritten" contains the length of the formatted text in bytes.
 20        /// false if buffer was too short. Iteratively increase the size of the buffer and retry until it succeeds.
 21        /// </returns>
 22        /// <exceptions>
 23        /// <remarks>
 24        /// Formats supported:
 25        ///     default       05/25/2017 10:30:15 -08:00
 26        ///     G             05/25/2017 10:30:15
 27        ///     R             Tue, 03 Jan 2017 08:08:05 GMT       (RFC 1123)
 28        ///     l             tue, 03 jan 2017 08:08:05 gmt       (Lowercase RFC 1123)
 29        ///     O             2017-06-12T05:30:45.7680000-07:00   (Round-trippable)
 30        /// </remarks>
 31        /// <cref>System.FormatException</cref> if the format is not valid for this data type.
 32        /// </exceptions>
 33        public static bool TryFormat(DateTimeOffset value, Span<byte> destination, out int bytesWritten, StandardFormat 
 34        {
 035            if (format.IsDefault)
 36            {
 037                return DateTimeFormat.TryFormatInvariantG(value.DateTime, value.Offset, destination, out bytesWritten);
 38            }
 39
 040            switch (format.Symbol)
 41            {
 42                case 'R':
 043                    return DateTimeFormat.TryFormatR(value.UtcDateTime, NullOffset, destination, out bytesWritten);
 44
 45                case 'O':
 046                    return DateTimeFormat.TryFormatO(value.DateTime, value.Offset, destination, out bytesWritten);
 47
 48                case 'l':
 049                    return TryFormatDateTimeL(value.UtcDateTime, destination, out bytesWritten);
 50
 51                case 'G':
 052                    return DateTimeFormat.TryFormatInvariantG(value.DateTime, NullOffset, destination, out bytesWritten)
 53
 54                default:
 055                    ThrowHelper.ThrowFormatException_BadFormatSpecifier();
 56                    goto case 'R';
 57            };
 58        }
 59
 60        /// <summary>
 61        /// Formats a DateTime as a UTF-8 string.
 62        /// </summary>
 63        /// <param name="value">Value to format</param>
 64        /// <param name="destination">Buffer to write the UTF-8 formatted value to</param>
 65        /// <param name="bytesWritten">Receives the length of the formatted text in bytes</param>
 66        /// <param name="format">The standard format to use</param>
 67        /// <returns>
 68        /// true for success. "bytesWritten" contains the length of the formatted text in bytes.
 69        /// false if buffer was too short. Iteratively increase the size of the buffer and retry until it succeeds.
 70        /// </returns>
 71        /// <remarks>
 72        /// Formats supported:
 73        ///     G  (default)  05/25/2017 10:30:15
 74        ///     R             Tue, 03 Jan 2017 08:08:05 GMT       (RFC 1123)
 75        ///     l             tue, 03 jan 2017 08:08:05 gmt       (Lowercase RFC 1123)
 76        ///     O             2017-06-12T05:30:45.7680000-07:00   (Round-trippable)
 77        /// </remarks>
 78        /// <exceptions>
 79        /// <cref>System.FormatException</cref> if the format is not valid for this data type.
 80        /// </exceptions>
 81        public static bool TryFormat(DateTime value, Span<byte> destination, out int bytesWritten, StandardFormat format
 82        {
 083            switch (FormattingHelpers.GetSymbolOrDefault(format, 'G'))
 84            {
 85                case 'R':
 086                    return DateTimeFormat.TryFormatR(value, NullOffset, destination, out bytesWritten);
 87
 88                case 'O':
 089                    return DateTimeFormat.TryFormatO(value, NullOffset, destination, out bytesWritten);
 90
 91                case 'l':
 092                    return TryFormatDateTimeL(value, destination, out bytesWritten);
 93
 94                case 'G':
 095                    return DateTimeFormat.TryFormatInvariantG(value, NullOffset, destination, out bytesWritten);
 96
 97                default:
 098                    ThrowHelper.ThrowFormatException_BadFormatSpecifier();
 99                    goto case 'R'; // unreachable
 100            }
 101        }
 102
 103        // Rfc1123 lowercased
 104        private static bool TryFormatDateTimeL(DateTime value, Span<byte> destination, out int bytesWritten)
 105        {
 0106            if (DateTimeFormat.TryFormatR(value, NullOffset, destination, out bytesWritten))
 107            {
 0108                Debug.Assert(bytesWritten == DateTimeFormat.FormatRLength);
 0109                Ascii.ToLowerInPlace(destination.Slice(0, bytesWritten), out bytesWritten);
 0110                return true;
 111            }
 112
 0113            return false;
 114        }
 115
 0116        private static TimeSpan NullOffset => new TimeSpan(DateTimeFormat.NullOffset);
 117    }
 118}

C:\h\w\9FD50923\w\C0A20A61\e\runtime-utils\Runner\runtime\src\libraries\System.Private.CoreLib\src\System\Buffers\Text\Utf8Formatter\Utf8Formatter.Decimal.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4namespace System.Buffers.Text
 5{
 6    public static partial class Utf8Formatter
 7    {
 8        /// <summary>
 9        /// Formats a Decimal as a UTF-8 string.
 10        /// </summary>
 11        /// <param name="value">Value to format</param>
 12        /// <param name="destination">Buffer to write the UTF-8 formatted value to</param>
 13        /// <param name="bytesWritten">Receives the length of the formatted text in bytes</param>
 14        /// <param name="format">The standard format to use</param>
 15        /// <returns>
 16        /// true for success. "bytesWritten" contains the length of the formatted text in bytes.
 17        /// false if buffer was too short. Iteratively increase the size of the buffer and retry until it succeeds.
 18        /// </returns>
 19        /// <remarks>
 20        /// Formats supported:
 21        ///     G/g  (default)
 22        ///     F/f             12.45       Fixed point
 23        ///     E/e             1.245000e1  Exponential
 24        /// </remarks>
 25        /// <exceptions>
 26        /// <cref>System.FormatException</cref> if the format is not valid for this data type.
 27        /// </exceptions>
 28        public static bool TryFormat(decimal value, Span<byte> destination, out int bytesWritten, StandardFormat format 
 029            FormattingHelpers.TryFormat(value, destination, out bytesWritten, format);
 30    }
 31}

C:\h\w\9FD50923\w\C0A20A61\e\runtime-utils\Runner\runtime\src\libraries\System.Private.CoreLib\src\System\Buffers\Text\Utf8Formatter\Utf8Formatter.Float.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4namespace System.Buffers.Text
 5{
 6    public static partial class Utf8Formatter
 7    {
 8        /// <summary>
 9        /// Formats a Double as a UTF-8 string.
 10        /// </summary>
 11        /// <param name="value">Value to format</param>
 12        /// <param name="destination">Buffer to write the UTF-8 formatted value to</param>
 13        /// <param name="bytesWritten">Receives the length of the formatted text in bytes</param>
 14        /// <param name="format">The standard format to use</param>
 15        /// <returns>
 16        /// true for success. "bytesWritten" contains the length of the formatted text in bytes.
 17        /// false if buffer was too short. Iteratively increase the size of the buffer and retry until it succeeds.
 18        /// </returns>
 19        /// <remarks>
 20        /// Formats supported:
 21        ///     G/g  (default)
 22        ///     F/f             12.45       Fixed point
 23        ///     E/e             1.245000e1  Exponential
 24        /// </remarks>
 25        /// <exceptions>
 26        /// <cref>System.FormatException</cref> if the format is not valid for this data type.
 27        /// </exceptions>
 28        public static bool TryFormat(double value, Span<byte> destination, out int bytesWritten, StandardFormat format =
 029            FormattingHelpers.TryFormat(value, destination, out bytesWritten, format);
 30
 31        /// <summary>
 32        /// Formats a Single as a UTF-8 string.
 33        /// </summary>
 34        /// <param name="value">Value to format</param>
 35        /// <param name="destination">Buffer to write the UTF-8 formatted value to</param>
 36        /// <param name="bytesWritten">Receives the length of the formatted text in bytes</param>
 37        /// <param name="format">The standard format to use</param>
 38        /// <returns>
 39        /// true for success. "bytesWritten" contains the length of the formatted text in bytes.
 40        /// false if buffer was too short. Iteratively increase the size of the buffer and retry until it succeeds.
 41        /// </returns>
 42        /// <remarks>
 43        /// Formats supported:
 44        ///     G/g  (default)
 45        ///     F/f             12.45       Fixed point
 46        ///     E/e             1.245000e1  Exponential
 47        /// </remarks>
 48        /// <exceptions>
 49        /// <cref>System.FormatException</cref> if the format is not valid for this data type.
 50        /// </exceptions>
 51        public static bool TryFormat(float value, Span<byte> destination, out int bytesWritten, StandardFormat format = 
 052            FormattingHelpers.TryFormat(value, destination, out bytesWritten, format);
 53    }
 54}

C:\h\w\9FD50923\w\C0A20A61\e\runtime-utils\Runner\runtime\src\libraries\System.Private.CoreLib\src\System\Buffers\Text\Utf8Formatter\Utf8Formatter.Guid.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4namespace System.Buffers.Text
 5{
 6    public static partial class Utf8Formatter
 7    {
 8        /// <summary>
 9        /// Formats a Guid as a UTF-8 string.
 10        /// </summary>
 11        /// <param name="value">Value to format</param>
 12        /// <param name="destination">Buffer to write the UTF-8 formatted value to</param>
 13        /// <param name="bytesWritten">Receives the length of the formatted text in bytes</param>
 14        /// <param name="format">The standard format to use</param>
 15        /// <returns>
 16        /// true for success. "bytesWritten" contains the length of the formatted text in bytes.
 17        /// false if buffer was too short. Iteratively increase the size of the buffer and retry until it succeeds.
 18        /// </returns>
 19        /// <remarks>
 20        /// Formats supported:
 21        ///     D (default)     nnnnnnnn-nnnn-nnnn-nnnn-nnnnnnnnnnnn
 22        ///     B               {nnnnnnnn-nnnn-nnnn-nnnn-nnnnnnnnnnnn}
 23        ///     P               (nnnnnnnn-nnnn-nnnn-nnnn-nnnnnnnnnnnn)
 24        ///     N               nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
 25        /// </remarks>
 26        /// <exceptions>
 27        /// <cref>System.FormatException</cref> if the format is not valid for this data type.
 28        /// </exceptions>
 29        public static bool TryFormat(Guid value, Span<byte> destination, out int bytesWritten, StandardFormat format = d
 30        {
 31            int flags;
 32
 033            switch (FormattingHelpers.GetSymbolOrDefault(format, 'D'))
 34            {
 35                case 'D': // nnnnnnnn-nnnn-nnnn-nnnn-nnnnnnnnnnnn
 036                    flags = 36 + Guid.TryFormatFlags_UseDashes;
 037                    break;
 38
 39                case 'B': // {nnnnnnnn-nnnn-nnnn-nnnn-nnnnnnnnnnnn}
 040                    flags = 38 + Guid.TryFormatFlags_UseDashes + Guid.TryFormatFlags_CurlyBraces;
 041                    break;
 42
 43                case 'P': // (nnnnnnnn-nnnn-nnnn-nnnn-nnnnnnnnnnnn)
 044                    flags = 38 + Guid.TryFormatFlags_UseDashes + Guid.TryFormatFlags_Parens;
 045                    break;
 46
 47                case 'N': // nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
 048                    flags = 32;
 049                    break;
 50
 51                default:
 052                    ThrowHelper.ThrowFormatException_BadFormatSpecifier();
 53                    goto case 'D'; // unreachable
 54            }
 55
 056            return value.TryFormatCore(destination, out bytesWritten, flags);
 57        }
 58    }
 59}

C:\h\w\9FD50923\w\C0A20A61\e\runtime-utils\Runner\runtime\src\libraries\System.Private.CoreLib\src\System\Buffers\Text\Utf8Formatter\Utf8Formatter.Integer.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.Runtime.CompilerServices;
 5
 6namespace System.Buffers.Text
 7{
 8    /// <summary>
 9    /// Methods to format common data types as Utf8 strings.
 10    /// </summary>
 11    public static partial class Utf8Formatter
 12    {
 13        /// <summary>
 14        /// Formats a Byte as a UTF-8 string.
 15        /// </summary>
 16        /// <param name="value">Value to format</param>
 17        /// <param name="destination">Buffer to write the UTF-8 formatted value to</param>
 18        /// <param name="bytesWritten">Receives the length of the formatted text in bytes</param>
 19        /// <param name="format">The standard format to use</param>
 20        /// <returns>
 21        /// true for success. "bytesWritten" contains the length of the formatted text in bytes.
 22        /// false if buffer was too short. Iteratively increase the size of the buffer and retry until it succeeds.
 23        /// </returns>
 24        /// <remarks>
 25        /// Formats supported:
 26        ///     G/g (default)
 27        ///     D/d             32767
 28        ///     N/n             32,767
 29        ///     X/x             7fff
 30        /// </remarks>
 31        /// <exceptions>
 32        /// <cref>System.FormatException</cref> if the format is not valid for this data type.
 33        /// </exceptions>
 34        public static bool TryFormat(byte value, Span<byte> destination, out int bytesWritten, StandardFormat format = d
 035            TryFormat((uint)value, destination, out bytesWritten, format);
 36
 37        /// <summary>
 38        /// Formats an SByte as a UTF-8 string.
 39        /// </summary>
 40        /// <param name="value">Value to format</param>
 41        /// <param name="destination">Buffer to write the UTF-8 formatted value to</param>
 42        /// <param name="bytesWritten">Receives the length of the formatted text in bytes</param>
 43        /// <param name="format">The standard format to use</param>
 44        /// <returns>
 45        /// true for success. "bytesWritten" contains the length of the formatted text in bytes.
 46        /// false if buffer was too short. Iteratively increase the size of the buffer and retry until it succeeds.
 47        /// </returns>
 48        /// <remarks>
 49        /// Formats supported:
 50        ///     G/g (default)
 51        ///     D/d             32767
 52        ///     N/n             32,767
 53        ///     X/x             7fff
 54        /// </remarks>
 55        /// <exceptions>
 56        /// <cref>System.FormatException</cref> if the format is not valid for this data type.
 57        /// </exceptions>
 58        [CLSCompliant(false)]
 59        public static bool TryFormat(sbyte value, Span<byte> destination, out int bytesWritten, StandardFormat format = 
 060            TryFormat(value, 0xFF, destination, out bytesWritten, format);
 61
 62        /// <summary>
 63        /// Formats a Unt16 as a UTF-8 string.
 64        /// </summary>
 65        /// <param name="value">Value to format</param>
 66        /// <param name="destination">Buffer to write the UTF-8 formatted value to</param>
 67        /// <param name="bytesWritten">Receives the length of the formatted text in bytes</param>
 68        /// <param name="format">The standard format to use</param>
 69        /// <returns>
 70        /// true for success. "bytesWritten" contains the length of the formatted text in bytes.
 71        /// false if buffer was too short. Iteratively increase the size of the buffer and retry until it succeeds.
 72        /// </returns>
 73        /// <remarks>
 74        /// Formats supported:
 75        ///     G/g (default)
 76        ///     D/d             32767
 77        ///     N/n             32,767
 78        ///     X/x             7fff
 79        /// </remarks>
 80        /// <exceptions>
 81        /// <cref>System.FormatException</cref> if the format is not valid for this data type.
 82        /// </exceptions>
 83        [CLSCompliant(false)]
 84        public static bool TryFormat(ushort value, Span<byte> destination, out int bytesWritten, StandardFormat format =
 085            TryFormat((uint)value, destination, out bytesWritten, format);
 86
 87        /// <summary>
 88        /// Formats an Int16 as a UTF-8 string.
 89        /// </summary>
 90        /// <param name="value">Value to format</param>
 91        /// <param name="destination">Buffer to write the UTF-8 formatted value to</param>
 92        /// <param name="bytesWritten">Receives the length of the formatted text in bytes</param>
 93        /// <param name="format">The standard format to use</param>
 94        /// <returns>
 95        /// true for success. "bytesWritten" contains the length of the formatted text in bytes.
 96        /// false if buffer was too short. Iteratively increase the size of the buffer and retry until it succeeds.
 97        /// </returns>
 98        /// <remarks>
 99        /// Formats supported:
 100        ///     G/g (default)
 101        ///     D/d             32767
 102        ///     N/n             32,767
 103        ///     X/x             7fff
 104        /// </remarks>
 105        /// <exceptions>
 106        /// <cref>System.FormatException</cref> if the format is not valid for this data type.
 107        /// </exceptions>
 108        public static bool TryFormat(short value, Span<byte> destination, out int bytesWritten, StandardFormat format = 
 0109            TryFormat(value, 0xFFFF, destination, out bytesWritten, format);
 110
 111        /// <summary>
 112        /// Formats a UInt32 as a UTF-8 string.
 113        /// </summary>
 114        /// <param name="value">Value to format</param>
 115        /// <param name="destination">Buffer to write the UTF-8 formatted value to</param>
 116        /// <param name="bytesWritten">Receives the length of the formatted text in bytes</param>
 117        /// <param name="format">The standard format to use</param>
 118        /// <returns>
 119        /// true for success. "bytesWritten" contains the length of the formatted text in bytes.
 120        /// false if buffer was too short. Iteratively increase the size of the buffer and retry until it succeeds.
 121        /// </returns>
 122        /// <remarks>
 123        /// Formats supported:
 124        ///     G/g (default)
 125        ///     D/d             32767
 126        ///     N/n             32,767
 127        ///     X/x             7fff
 128        /// </remarks>
 129        /// <exceptions>
 130        /// <cref>System.FormatException</cref> if the format is not valid for this data type.
 131        /// </exceptions>
 132        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 133        [CLSCompliant(false)]
 134        public static bool TryFormat(uint value, Span<byte> destination, out int bytesWritten, StandardFormat format = d
 135        {
 0136            if (format.IsDefault)
 137            {
 0138                return Number.TryUInt32ToDecStr(value, destination, out bytesWritten);
 139            }
 140
 0141            switch (format.Symbol | 0x20)
 142            {
 143                case 'd':
 0144                    return Number.TryUInt32ToDecStr(value, format.PrecisionOrZero, destination, out bytesWritten);
 145
 146                case 'x':
 0147                    return Number.TryInt32ToHexStr((int)value, Number.GetHexBase(format.Symbol), format.PrecisionOrZero,
 148
 149                case 'n':
 0150                    return FormattingHelpers.TryFormat(value, destination, out bytesWritten, format);
 151
 152                case 'g' or 'r':
 0153                    if (format.HasPrecision)
 154                    {
 0155                        ThrowGWithPrecisionNotSupported();
 156                    }
 0157                    goto case 'd';
 158
 159                default:
 0160                    ThrowHelper.ThrowFormatException_BadFormatSpecifier();
 161                    goto case 'd';
 162            }
 163        }
 164
 165        /// <summary>
 166        /// Formats an Int32 as a UTF-8 string.
 167        /// </summary>
 168        /// <param name="value">Value to format</param>
 169        /// <param name="destination">Buffer to write the UTF-8 formatted value to</param>
 170        /// <param name="bytesWritten">Receives the length of the formatted text in bytes</param>
 171        /// <param name="format">The standard format to use</param>
 172        /// <returns>
 173        /// true for success. "bytesWritten" contains the length of the formatted text in bytes.
 174        /// false if buffer was too short. Iteratively increase the size of the buffer and retry until it succeeds.
 175        /// </returns>
 176        /// <remarks>
 177        /// Formats supported:
 178        ///     G/g (default)
 179        ///     D/d             32767
 180        ///     N/n             32,767
 181        ///     X/x             7fff
 182        /// </remarks>
 183        /// <exceptions>
 184        /// <cref>System.FormatException</cref> if the format is not valid for this data type.
 185        /// </exceptions>
 186        public static bool TryFormat(int value, Span<byte> destination, out int bytesWritten, StandardFormat format = de
 0187            TryFormat(value, ~0, destination, out bytesWritten, format);
 188
 189        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 190        private static bool TryFormat(int value, int hexMask, Span<byte> destination, out int bytesWritten, StandardForm
 191        {
 0192            if (format.IsDefault)
 193            {
 0194                return value >= 0 ?
 0195                    Number.TryUInt32ToDecStr((uint)value, destination, out bytesWritten) :
 0196                    Number.TryNegativeInt32ToDecStr(value, format.PrecisionOrZero, "-"u8, destination, out bytesWritten)
 197            }
 198
 0199            switch (format.Symbol | 0x20)
 200            {
 201                case 'd':
 0202                    return value >= 0 ?
 0203                        Number.TryUInt32ToDecStr((uint)value, format.PrecisionOrZero, destination, out bytesWritten) :
 0204                        Number.TryNegativeInt32ToDecStr(value, format.PrecisionOrZero, "-"u8, destination, out bytesWrit
 205
 206                case 'x':
 0207                    return Number.TryInt32ToHexStr(value & hexMask, Number.GetHexBase(format.Symbol), format.PrecisionOr
 208
 209                case 'n':
 0210                    return FormattingHelpers.TryFormat(value, destination, out bytesWritten, format);
 211
 212                case 'g' or 'r':
 0213                    if (format.HasPrecision)
 214                    {
 0215                        ThrowGWithPrecisionNotSupported();
 216                    }
 0217                    goto case 'd';
 218
 219                default:
 0220                    ThrowHelper.ThrowFormatException_BadFormatSpecifier();
 221                    goto case 'd';
 222            }
 223        }
 224
 225        /// <summary>
 226        /// Formats a UInt64 as a UTF-8 string.
 227        /// </summary>
 228        /// <param name="value">Value to format</param>
 229        /// <param name="destination">Buffer to write the UTF-8 formatted value to</param>
 230        /// <param name="bytesWritten">Receives the length of the formatted text in bytes</param>
 231        /// <param name="format">The standard format to use</param>
 232        /// <returns>
 233        /// true for success. "bytesWritten" contains the length of the formatted text in bytes.
 234        /// false if buffer was too short. Iteratively increase the size of the buffer and retry until it succeeds.
 235        /// </returns>
 236        /// <remarks>
 237        /// Formats supported:
 238        ///     G/g (default)
 239        ///     D/d             32767
 240        ///     N/n             32,767
 241        ///     X/x             7fff
 242        /// </remarks>
 243        /// <exceptions>
 244        /// <cref>System.FormatException</cref> if the format is not valid for this data type.
 245        /// </exceptions>
 246        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 247        [CLSCompliant(false)]
 248        public static bool TryFormat(ulong value, Span<byte> destination, out int bytesWritten, StandardFormat format = 
 249        {
 0250            if (format.IsDefault)
 251            {
 0252                return Number.TryUInt64ToDecStr(value, destination, out bytesWritten);
 253            }
 254
 0255            switch (format.Symbol | 0x20)
 256            {
 257                case 'd':
 0258                    return Number.TryUInt64ToDecStr(value, format.PrecisionOrZero, destination, out bytesWritten);
 259
 260                case 'x':
 0261                    return Number.TryInt64ToHexStr((long)value, Number.GetHexBase(format.Symbol), format.PrecisionOrZero
 262
 263                case 'n':
 0264                    return FormattingHelpers.TryFormat(value, destination, out bytesWritten, format);
 265
 266                case 'g' or 'r':
 0267                    if (format.HasPrecision)
 268                    {
 0269                        ThrowGWithPrecisionNotSupported();
 270                    }
 0271                    goto case 'd';
 272
 273                default:
 0274                    ThrowHelper.ThrowFormatException_BadFormatSpecifier();
 275                    goto case 'd';
 276            }
 277        }
 278
 279        /// <summary>
 280        /// Formats an Int64 as a UTF-8 string.
 281        /// </summary>
 282        /// <param name="value">Value to format</param>
 283        /// <param name="destination">Buffer to write the UTF-8 formatted value to</param>
 284        /// <param name="bytesWritten">Receives the length of the formatted text in bytes</param>
 285        /// <param name="format">The standard format to use</param>
 286        /// <returns>
 287        /// true for success. "bytesWritten" contains the length of the formatted text in bytes.
 288        /// false if buffer was too short. Iteratively increase the size of the buffer and retry until it succeeds.
 289        /// </returns>
 290        /// <remarks>
 291        /// Formats supported:
 292        ///     G/g (default)
 293        ///     D/d             32767
 294        ///     N/n             32,767
 295        ///     X/x             7fff
 296        /// </remarks>
 297        /// <exceptions>
 298        /// <cref>System.FormatException</cref> if the format is not valid for this data type.
 299        /// </exceptions>
 300        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 301        public static bool TryFormat(long value, Span<byte> destination, out int bytesWritten, StandardFormat format = d
 302        {
 0303            if (format.IsDefault)
 304            {
 0305                return value >= 0 ?
 0306                    Number.TryUInt64ToDecStr((ulong)value, destination, out bytesWritten) :
 0307                    Number.TryNegativeInt64ToDecStr(value, format.PrecisionOrZero, "-"u8, destination, out bytesWritten)
 308            }
 309
 0310            switch (format.Symbol | 0x20)
 311            {
 312                case 'd':
 0313                    return value >= 0 ?
 0314                        Number.TryUInt64ToDecStr((ulong)value, format.PrecisionOrZero, destination, out bytesWritten) :
 0315                        Number.TryNegativeInt64ToDecStr(value, format.PrecisionOrZero, "-"u8, destination, out bytesWrit
 316
 317                case 'x':
 0318                    return Number.TryInt64ToHexStr(value, Number.GetHexBase(format.Symbol), format.PrecisionOrZero, dest
 319
 320                case 'n':
 0321                    return FormattingHelpers.TryFormat(value, destination, out bytesWritten, format);
 322
 323                case 'g' or 'r':
 0324                    if (format.HasPrecision)
 325                    {
 0326                        ThrowGWithPrecisionNotSupported();
 327                    }
 0328                    goto case 'd';
 329
 330                default:
 0331                    ThrowHelper.ThrowFormatException_BadFormatSpecifier();
 332                    goto case 'd';
 333            }
 334        }
 335
 336        private static void ThrowGWithPrecisionNotSupported() =>
 337            // With a precision, 'G' can produce exponential format, even for integers.
 0338            throw new NotSupportedException(SR.Argument_GWithPrecisionNotSupported);
 339    }
 340}

C:\h\w\9FD50923\w\C0A20A61\e\runtime-utils\Runner\runtime\src\libraries\System.Private.CoreLib\src\System\Buffers\Text\Utf8Formatter\Utf8Formatter.TimeSpan.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.Globalization;
 5
 6namespace System.Buffers.Text
 7{
 8    public static partial class Utf8Formatter
 9    {
 10        /// <summary>
 11        /// Formats a TimeSpan as a UTF-8 string.
 12        /// </summary>
 13        /// <param name="value">Value to format</param>
 14        /// <param name="destination">Buffer to write the UTF-8 formatted value to</param>
 15        /// <param name="bytesWritten">Receives the length of the formatted text in bytes</param>
 16        /// <param name="format">The standard format to use</param>
 17        /// <returns>
 18        /// true for success. "bytesWritten" contains the length of the formatted text in bytes.
 19        /// false if buffer was too short. Iteratively increase the size of the buffer and retry until it succeeds.
 20        /// </returns>
 21        /// <remarks>
 22        /// Formats supported:
 23        ///     c/t/T (default) [-][d.]hh:mm:ss[.fffffff]              (constant format)
 24        ///     G               [-]d:hh:mm:ss.fffffff                  (general long)
 25        ///     g               [-][d:][h]h:mm:ss[.f[f[f[f[f[f[f]]]]]] (general short)
 26        /// </remarks>
 27        /// <exceptions>
 28        /// <cref>System.FormatException</cref> if the format is not valid for this data type.
 29        /// </exceptions>
 30        public static bool TryFormat(TimeSpan value, Span<byte> destination, out int bytesWritten, StandardFormat format
 31        {
 032            TimeSpanFormat.StandardFormat sf = TimeSpanFormat.StandardFormat.C;
 033            ReadOnlySpan<byte> decimalSeparator = default;
 34
 035            char symbol = FormattingHelpers.GetSymbolOrDefault(format, 'c');
 036            if (symbol != 'c' && (symbol | 0x20) != 't')
 37            {
 038                decimalSeparator = DateTimeFormatInfo.InvariantInfo.DecimalSeparatorTChar<byte>();
 039                if (symbol == 'g')
 40                {
 041                    sf = TimeSpanFormat.StandardFormat.g;
 42                }
 43                else
 44                {
 045                    sf = TimeSpanFormat.StandardFormat.G;
 046                    if (symbol != 'G')
 47                    {
 048                        ThrowHelper.ThrowFormatException_BadFormatSpecifier();
 49                    }
 50                }
 51            }
 52
 053            return TimeSpanFormat.TryFormatStandard(value, sf, decimalSeparator, destination, out bytesWritten);
 54        }
 55    }
 56}

Methods/Properties

TryFormat(System.Boolean,System.Span`1<System.Byte>,System.Int32&,System.Buffers.StandardFormat)
TryFormat(System.DateTimeOffset,System.Span`1<System.Byte>,System.Int32&,System.Buffers.StandardFormat)
TryFormat(System.DateTime,System.Span`1<System.Byte>,System.Int32&,System.Buffers.StandardFormat)
TryFormatDateTimeL(System.DateTime,System.Span`1<System.Byte>,System.Int32&)
NullOffset()
TryFormat(System.Decimal,System.Span`1<System.Byte>,System.Int32&,System.Buffers.StandardFormat)
TryFormat(System.Double,System.Span`1<System.Byte>,System.Int32&,System.Buffers.StandardFormat)
TryFormat(System.Single,System.Span`1<System.Byte>,System.Int32&,System.Buffers.StandardFormat)
TryFormat(System.Guid,System.Span`1<System.Byte>,System.Int32&,System.Buffers.StandardFormat)
TryFormat(System.Byte,System.Span`1<System.Byte>,System.Int32&,System.Buffers.StandardFormat)
TryFormat(System.SByte,System.Span`1<System.Byte>,System.Int32&,System.Buffers.StandardFormat)
TryFormat(System.UInt16,System.Span`1<System.Byte>,System.Int32&,System.Buffers.StandardFormat)
TryFormat(System.Int16,System.Span`1<System.Byte>,System.Int32&,System.Buffers.StandardFormat)
TryFormat(System.UInt32,System.Span`1<System.Byte>,System.Int32&,System.Buffers.StandardFormat)
TryFormat(System.Int32,System.Span`1<System.Byte>,System.Int32&,System.Buffers.StandardFormat)
TryFormat(System.Int32,System.Int32,System.Span`1<System.Byte>,System.Int32&,System.Buffers.StandardFormat)
TryFormat(System.UInt64,System.Span`1<System.Byte>,System.Int32&,System.Buffers.StandardFormat)
TryFormat(System.Int64,System.Span`1<System.Byte>,System.Int32&,System.Buffers.StandardFormat)
ThrowGWithPrecisionNotSupported()
TryFormat(System.TimeSpan,System.Span`1<System.Byte>,System.Int32&,System.Buffers.StandardFormat)