| | | 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 | | // See the LICENSE file in the project root for more information. |
| | | 4 | | |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | |
| | | 7 | | namespace System.Buffers |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Represents a standard formatting string without using an actual String. A StandardFormat consists of a character |
| | | 11 | | /// and an optional precision ranging from 0..99, or the special value NoPrecision. |
| | | 12 | | /// </summary> |
| | | 13 | | public readonly struct StandardFormat : IEquatable<StandardFormat> |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// Precision values for format that don't use a precision, or for when the precision is to be unspecified. |
| | | 17 | | /// </summary> |
| | | 18 | | public const byte NoPrecision = byte.MaxValue; |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// The maximum valid precision value. |
| | | 22 | | /// </summary> |
| | | 23 | | public const byte MaxPrecision = 99; |
| | | 24 | | |
| | | 25 | | private readonly byte _format; |
| | | 26 | | private readonly byte _precision; |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// The character component of the format. |
| | | 30 | | /// </summary> |
| | 1 | 31 | | public char Symbol => (char)_format; |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// The precision component of the format. Ranges from 0..9 or the special value NoPrecision. |
| | | 35 | | /// </summary> |
| | 1 | 36 | | public byte Precision => _precision; |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// true if Precision is a value other than NoPrecision |
| | | 40 | | /// </summary> |
| | 1 | 41 | | public bool HasPrecision => _precision != NoPrecision; |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// true if the StandardFormat == default(StandardFormat) |
| | | 45 | | /// </summary> |
| | 1 | 46 | | public bool IsDefault => _format == 0 && _precision == 0; |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Create a StandardFormat. |
| | | 50 | | /// </summary> |
| | | 51 | | /// <param name="symbol">A type-specific formatting character such as 'G', 'D' or 'X'</param> |
| | | 52 | | /// <param name="precision">An optional precision ranging from 0..9 or the special value NoPrecision (the defaul |
| | | 53 | | public StandardFormat(char symbol, byte precision = NoPrecision) |
| | 1 | 54 | | { |
| | 1 | 55 | | if (precision != NoPrecision && precision > MaxPrecision) |
| | 1 | 56 | | ThrowHelper.ThrowArgumentOutOfRangeException_PrecisionTooLarge(); |
| | 1 | 57 | | if (symbol != (byte)symbol) |
| | 1 | 58 | | ThrowHelper.ThrowArgumentOutOfRangeException_SymbolDoesNotFit(); |
| | | 59 | | |
| | 1 | 60 | | _format = (byte)symbol; |
| | 1 | 61 | | _precision = precision; |
| | 1 | 62 | | } |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Converts a character to a StandardFormat using the NoPrecision precision. |
| | | 66 | | /// </summary> |
| | 1 | 67 | | public static implicit operator StandardFormat(char symbol) => new StandardFormat(symbol); |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Converts a classic .NET format string into a StandardFormat |
| | | 71 | | /// </summary> |
| | | 72 | | public static StandardFormat Parse(ReadOnlySpan<char> format) |
| | 1 | 73 | | { |
| | 1 | 74 | | if (format.Length == 0) |
| | 1 | 75 | | return default; |
| | | 76 | | |
| | 1 | 77 | | char symbol = format[0]; |
| | | 78 | | byte precision; |
| | 1 | 79 | | if (format.Length == 1) |
| | 1 | 80 | | { |
| | 1 | 81 | | precision = NoPrecision; |
| | 1 | 82 | | } |
| | | 83 | | else |
| | 1 | 84 | | { |
| | 1 | 85 | | uint parsedPrecision = 0; |
| | 3 | 86 | | for (int srcIndex = 1; srcIndex < format.Length; srcIndex++) |
| | 1 | 87 | | { |
| | 1 | 88 | | uint digit = format[srcIndex] - 48u; // '0' |
| | 1 | 89 | | if (digit > 9) |
| | 1 | 90 | | throw new FormatException(SR.Format(SR.Argument_CannotParsePrecision, MaxPrecision)); |
| | | 91 | | |
| | 1 | 92 | | parsedPrecision = parsedPrecision * 10 + digit; |
| | 1 | 93 | | if (parsedPrecision > MaxPrecision) |
| | 1 | 94 | | throw new FormatException(SR.Format(SR.Argument_PrecisionTooLarge, MaxPrecision)); |
| | 1 | 95 | | } |
| | | 96 | | |
| | 1 | 97 | | precision = (byte)parsedPrecision; |
| | 1 | 98 | | } |
| | | 99 | | |
| | 1 | 100 | | return new StandardFormat(symbol, precision); |
| | 1 | 101 | | } |
| | | 102 | | |
| | | 103 | | /// <summary> |
| | | 104 | | /// Converts a classic .NET format string into a StandardFormat |
| | | 105 | | /// </summary> |
| | 1 | 106 | | public static StandardFormat Parse(string format) => format == null ? default : Parse(format.AsSpan()); |
| | | 107 | | |
| | | 108 | | /// <summary> |
| | | 109 | | /// Returns true if both the Symbol and Precision are equal. |
| | | 110 | | /// </summary> |
| | 1 | 111 | | public override bool Equals(object obj) => obj is StandardFormat other && Equals(other); |
| | | 112 | | |
| | | 113 | | /// <summary> |
| | | 114 | | /// Compute a hash code. |
| | | 115 | | /// </summary> |
| | 1 | 116 | | public override int GetHashCode() => _format.GetHashCode() ^ _precision.GetHashCode(); |
| | | 117 | | |
| | | 118 | | /// <summary> |
| | | 119 | | /// Returns true if both the Symbol and Precision are equal. |
| | | 120 | | /// </summary> |
| | 1 | 121 | | public bool Equals(StandardFormat other) => _format == other._format && _precision == other._precision; |
| | | 122 | | |
| | | 123 | | /// <summary> |
| | | 124 | | /// Returns the format in classic .NET format. |
| | | 125 | | /// </summary> |
| | | 126 | | public override string ToString() |
| | 1 | 127 | | { |
| | | 128 | | unsafe |
| | 1 | 129 | | { |
| | | 130 | | const int MaxLength = 4; |
| | 1 | 131 | | char* pBuffer = stackalloc char[MaxLength]; |
| | | 132 | | |
| | 1 | 133 | | int dstIndex = 0; |
| | 1 | 134 | | char symbol = Symbol; |
| | 1 | 135 | | if (symbol != default) |
| | 1 | 136 | | { |
| | 1 | 137 | | pBuffer[dstIndex++] = symbol; |
| | | 138 | | |
| | 1 | 139 | | byte precision = Precision; |
| | 1 | 140 | | if (precision != NoPrecision) |
| | 1 | 141 | | { |
| | 1 | 142 | | if (precision >= 100) |
| | 1 | 143 | | { |
| | 1 | 144 | | pBuffer[dstIndex++] = (char)('0' + (precision / 100) % 10); |
| | 1 | 145 | | precision = (byte)(precision % 100); |
| | 1 | 146 | | } |
| | | 147 | | |
| | 1 | 148 | | if (precision >= 10) |
| | 1 | 149 | | { |
| | 1 | 150 | | pBuffer[dstIndex++] = (char)('0' + (precision / 10) % 10); |
| | 1 | 151 | | precision = (byte)(precision % 10); |
| | 1 | 152 | | } |
| | | 153 | | |
| | 1 | 154 | | pBuffer[dstIndex++] = (char)('0' + precision); |
| | 1 | 155 | | } |
| | 1 | 156 | | } |
| | | 157 | | |
| | 1 | 158 | | Debug.Assert(dstIndex <= MaxLength); |
| | | 159 | | |
| | 1 | 160 | | return new string(pBuffer, startIndex: 0, length: dstIndex); |
| | | 161 | | } |
| | 1 | 162 | | } |
| | | 163 | | |
| | | 164 | | /// <summary> |
| | | 165 | | /// Returns true if both the Symbol and Precision are equal. |
| | | 166 | | /// </summary> |
| | 1 | 167 | | public static bool operator ==(StandardFormat left, StandardFormat right) => left.Equals(right); |
| | | 168 | | |
| | | 169 | | /// <summary> |
| | | 170 | | /// Returns false if both the Symbol and Precision are equal. |
| | | 171 | | /// </summary> |
| | 1 | 172 | | public static bool operator !=(StandardFormat left, StandardFormat right) => !left.Equals(right); |
| | | 173 | | } |
| | | 174 | | } |