| | | 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 | | |
| | | 4 | | using System.Runtime.CompilerServices; |
| | | 5 | | |
| | | 6 | | namespace 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 |
| | 0 | 35 | | 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 = |
| | 0 | 60 | | 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 = |
| | 0 | 85 | | 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 = |
| | 0 | 109 | | 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 | | { |
| | 0 | 136 | | if (format.IsDefault) |
| | | 137 | | { |
| | 0 | 138 | | return Number.TryUInt32ToDecStr(value, destination, out bytesWritten); |
| | | 139 | | } |
| | | 140 | | |
| | 0 | 141 | | switch (format.Symbol | 0x20) |
| | | 142 | | { |
| | | 143 | | case 'd': |
| | 0 | 144 | | return Number.TryUInt32ToDecStr(value, format.PrecisionOrZero, destination, out bytesWritten); |
| | | 145 | | |
| | | 146 | | case 'x': |
| | 0 | 147 | | return Number.TryInt32ToHexStr((int)value, Number.GetHexBase(format.Symbol), format.PrecisionOrZero, |
| | | 148 | | |
| | | 149 | | case 'n': |
| | 0 | 150 | | return FormattingHelpers.TryFormat(value, destination, out bytesWritten, format); |
| | | 151 | | |
| | | 152 | | case 'g' or 'r': |
| | 0 | 153 | | if (format.HasPrecision) |
| | | 154 | | { |
| | 0 | 155 | | ThrowGWithPrecisionNotSupported(); |
| | | 156 | | } |
| | 0 | 157 | | goto case 'd'; |
| | | 158 | | |
| | | 159 | | default: |
| | 0 | 160 | | 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 |
| | 0 | 187 | | 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 | | { |
| | 0 | 192 | | if (format.IsDefault) |
| | | 193 | | { |
| | 0 | 194 | | return value >= 0 ? |
| | 0 | 195 | | Number.TryUInt32ToDecStr((uint)value, destination, out bytesWritten) : |
| | 0 | 196 | | Number.TryNegativeInt32ToDecStr(value, format.PrecisionOrZero, "-"u8, destination, out bytesWritten) |
| | | 197 | | } |
| | | 198 | | |
| | 0 | 199 | | switch (format.Symbol | 0x20) |
| | | 200 | | { |
| | | 201 | | case 'd': |
| | 0 | 202 | | return value >= 0 ? |
| | 0 | 203 | | Number.TryUInt32ToDecStr((uint)value, format.PrecisionOrZero, destination, out bytesWritten) : |
| | 0 | 204 | | Number.TryNegativeInt32ToDecStr(value, format.PrecisionOrZero, "-"u8, destination, out bytesWrit |
| | | 205 | | |
| | | 206 | | case 'x': |
| | 0 | 207 | | return Number.TryInt32ToHexStr(value & hexMask, Number.GetHexBase(format.Symbol), format.PrecisionOr |
| | | 208 | | |
| | | 209 | | case 'n': |
| | 0 | 210 | | return FormattingHelpers.TryFormat(value, destination, out bytesWritten, format); |
| | | 211 | | |
| | | 212 | | case 'g' or 'r': |
| | 0 | 213 | | if (format.HasPrecision) |
| | | 214 | | { |
| | 0 | 215 | | ThrowGWithPrecisionNotSupported(); |
| | | 216 | | } |
| | 0 | 217 | | goto case 'd'; |
| | | 218 | | |
| | | 219 | | default: |
| | 0 | 220 | | 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 | | { |
| | 0 | 250 | | if (format.IsDefault) |
| | | 251 | | { |
| | 0 | 252 | | return Number.TryUInt64ToDecStr(value, destination, out bytesWritten); |
| | | 253 | | } |
| | | 254 | | |
| | 0 | 255 | | switch (format.Symbol | 0x20) |
| | | 256 | | { |
| | | 257 | | case 'd': |
| | 0 | 258 | | return Number.TryUInt64ToDecStr(value, format.PrecisionOrZero, destination, out bytesWritten); |
| | | 259 | | |
| | | 260 | | case 'x': |
| | 0 | 261 | | return Number.TryInt64ToHexStr((long)value, Number.GetHexBase(format.Symbol), format.PrecisionOrZero |
| | | 262 | | |
| | | 263 | | case 'n': |
| | 0 | 264 | | return FormattingHelpers.TryFormat(value, destination, out bytesWritten, format); |
| | | 265 | | |
| | | 266 | | case 'g' or 'r': |
| | 0 | 267 | | if (format.HasPrecision) |
| | | 268 | | { |
| | 0 | 269 | | ThrowGWithPrecisionNotSupported(); |
| | | 270 | | } |
| | 0 | 271 | | goto case 'd'; |
| | | 272 | | |
| | | 273 | | default: |
| | 0 | 274 | | 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 | | { |
| | 0 | 303 | | if (format.IsDefault) |
| | | 304 | | { |
| | 0 | 305 | | return value >= 0 ? |
| | 0 | 306 | | Number.TryUInt64ToDecStr((ulong)value, destination, out bytesWritten) : |
| | 0 | 307 | | Number.TryNegativeInt64ToDecStr(value, format.PrecisionOrZero, "-"u8, destination, out bytesWritten) |
| | | 308 | | } |
| | | 309 | | |
| | 0 | 310 | | switch (format.Symbol | 0x20) |
| | | 311 | | { |
| | | 312 | | case 'd': |
| | 0 | 313 | | return value >= 0 ? |
| | 0 | 314 | | Number.TryUInt64ToDecStr((ulong)value, format.PrecisionOrZero, destination, out bytesWritten) : |
| | 0 | 315 | | Number.TryNegativeInt64ToDecStr(value, format.PrecisionOrZero, "-"u8, destination, out bytesWrit |
| | | 316 | | |
| | | 317 | | case 'x': |
| | 0 | 318 | | return Number.TryInt64ToHexStr(value, Number.GetHexBase(format.Symbol), format.PrecisionOrZero, dest |
| | | 319 | | |
| | | 320 | | case 'n': |
| | 0 | 321 | | return FormattingHelpers.TryFormat(value, destination, out bytesWritten, format); |
| | | 322 | | |
| | | 323 | | case 'g' or 'r': |
| | 0 | 324 | | if (format.HasPrecision) |
| | | 325 | | { |
| | 0 | 326 | | ThrowGWithPrecisionNotSupported(); |
| | | 327 | | } |
| | 0 | 328 | | goto case 'd'; |
| | | 329 | | |
| | | 330 | | default: |
| | 0 | 331 | | 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. |
| | 0 | 338 | | throw new NotSupportedException(SR.Argument_GWithPrecisionNotSupported); |
| | | 339 | | } |
| | | 340 | | } |