1
1
// Licensed to the .NET Foundation under one or more agreements.
2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
4
+ using System . Runtime . CompilerServices ;
5
+
4
6
namespace System . Buffers . Text
5
7
{
6
8
/// <summary>
@@ -30,7 +32,7 @@ public static partial class Utf8Formatter
30
32
/// <cref>System.FormatException</cref> if the format is not valid for this data type.
31
33
/// </exceptions>
32
34
public static bool TryFormat ( byte value , Span < byte > destination , out int bytesWritten , StandardFormat format = default ) =>
33
- FormattingHelpers . TryFormat ( value , destination , out bytesWritten , format ) ;
35
+ TryFormat ( ( uint ) value , destination , out bytesWritten , format ) ;
34
36
35
37
/// <summary>
36
38
/// Formats an SByte as a UTF8 string.
@@ -55,7 +57,7 @@ public static bool TryFormat(byte value, Span<byte> destination, out int bytesWr
55
57
/// </exceptions>
56
58
[ CLSCompliant ( false ) ]
57
59
public static bool TryFormat ( sbyte value , Span < byte > destination , out int bytesWritten , StandardFormat format = default ) =>
58
- FormattingHelpers . TryFormat ( value , destination , out bytesWritten , format ) ;
60
+ TryFormat ( value , 0xFF , destination , out bytesWritten , format ) ;
59
61
60
62
/// <summary>
61
63
/// Formats a Unt16 as a UTF8 string.
@@ -80,7 +82,7 @@ public static bool TryFormat(sbyte value, Span<byte> destination, out int bytesW
80
82
/// </exceptions>
81
83
[ CLSCompliant ( false ) ]
82
84
public static bool TryFormat ( ushort value , Span < byte > destination , out int bytesWritten , StandardFormat format = default ) =>
83
- FormattingHelpers . TryFormat ( value , destination , out bytesWritten , format ) ;
85
+ TryFormat ( ( uint ) value , destination , out bytesWritten , format ) ;
84
86
85
87
/// <summary>
86
88
/// Formats an Int16 as a UTF8 string.
@@ -104,7 +106,7 @@ public static bool TryFormat(ushort value, Span<byte> destination, out int bytes
104
106
/// <cref>System.FormatException</cref> if the format is not valid for this data type.
105
107
/// </exceptions>
106
108
public static bool TryFormat ( short value , Span < byte > destination , out int bytesWritten , StandardFormat format = default ) =>
107
- FormattingHelpers . TryFormat ( value , destination , out bytesWritten , format ) ;
109
+ TryFormat ( value , 0xFFFF , destination , out bytesWritten , format ) ;
108
110
109
111
/// <summary>
110
112
/// Formats a UInt32 as a UTF8 string.
@@ -127,9 +129,38 @@ public static bool TryFormat(short value, Span<byte> destination, out int bytesW
127
129
/// <exceptions>
128
130
/// <cref>System.FormatException</cref> if the format is not valid for this data type.
129
131
/// </exceptions>
132
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
130
133
[ CLSCompliant ( false ) ]
131
- public static bool TryFormat ( uint value , Span < byte > destination , out int bytesWritten , StandardFormat format = default ) =>
132
- FormattingHelpers . TryFormat ( value , destination , out bytesWritten , format ) ;
134
+ public static bool TryFormat ( uint value , Span < byte > destination , out int bytesWritten , StandardFormat format = default )
135
+ {
136
+ if ( format . IsDefault )
137
+ {
138
+ return Number . TryUInt32ToDecStr ( value , destination , out bytesWritten ) ;
139
+ }
140
+
141
+ switch ( format . Symbol | 0x20 )
142
+ {
143
+ case 'd' :
144
+ return Number . TryUInt32ToDecStr ( value , format . PrecisionOrZero , destination , out bytesWritten ) ;
145
+
146
+ case 'x' :
147
+ return Number . TryInt32ToHexStr ( ( int ) value , Number . GetHexBase ( format . Symbol ) , format . PrecisionOrZero , destination , out bytesWritten ) ;
148
+
149
+ case 'n' :
150
+ return FormattingHelpers . TryFormat ( value , destination , out bytesWritten , format ) ;
151
+
152
+ case 'g' or 'r' :
153
+ if ( format . HasPrecision )
154
+ {
155
+ ThrowGWithPrecisionNotSupported ( ) ;
156
+ }
157
+ goto case 'd' ;
158
+
159
+ default :
160
+ ThrowHelper . ThrowFormatException_BadFormatSpecifier ( ) ;
161
+ goto case 'd' ;
162
+ }
163
+ }
133
164
134
165
/// <summary>
135
166
/// Formats an Int32 as a UTF8 string.
@@ -153,7 +184,43 @@ public static bool TryFormat(uint value, Span<byte> destination, out int bytesWr
153
184
/// <cref>System.FormatException</cref> if the format is not valid for this data type.
154
185
/// </exceptions>
155
186
public static bool TryFormat ( int value , Span < byte > destination , out int bytesWritten , StandardFormat format = default ) =>
156
- FormattingHelpers . TryFormat ( value , destination , out bytesWritten , format ) ;
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 , StandardFormat format = default )
191
+ {
192
+ if ( format . IsDefault )
193
+ {
194
+ return value >= 0 ?
195
+ Number . TryUInt32ToDecStr ( ( uint ) value , destination , out bytesWritten ) :
196
+ Number . TryNegativeInt32ToDecStr ( value , format . PrecisionOrZero , "-"u8 , destination , out bytesWritten ) ;
197
+ }
198
+
199
+ switch ( format . Symbol | 0x20 )
200
+ {
201
+ case 'd' :
202
+ return value >= 0 ?
203
+ Number . TryUInt32ToDecStr ( ( uint ) value , format . PrecisionOrZero , destination , out bytesWritten ) :
204
+ Number . TryNegativeInt32ToDecStr ( value , format . PrecisionOrZero , "-"u8 , destination , out bytesWritten ) ;
205
+
206
+ case 'x' :
207
+ return Number . TryInt32ToHexStr ( value & hexMask , Number . GetHexBase ( format . Symbol ) , format . PrecisionOrZero , destination , out bytesWritten ) ;
208
+
209
+ case 'n' :
210
+ return FormattingHelpers . TryFormat ( value , destination , out bytesWritten , format ) ;
211
+
212
+ case 'g' or 'r' :
213
+ if ( format . HasPrecision )
214
+ {
215
+ ThrowGWithPrecisionNotSupported ( ) ;
216
+ }
217
+ goto case 'd' ;
218
+
219
+ default :
220
+ ThrowHelper . ThrowFormatException_BadFormatSpecifier ( ) ;
221
+ goto case 'd' ;
222
+ }
223
+ }
157
224
158
225
/// <summary>
159
226
/// Formats a UInt64 as a UTF8 string.
@@ -176,9 +243,38 @@ public static bool TryFormat(int value, Span<byte> destination, out int bytesWri
176
243
/// <exceptions>
177
244
/// <cref>System.FormatException</cref> if the format is not valid for this data type.
178
245
/// </exceptions>
246
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
179
247
[ CLSCompliant ( false ) ]
180
- public static bool TryFormat ( ulong value , Span < byte > destination , out int bytesWritten , StandardFormat format = default ) =>
181
- FormattingHelpers . TryFormat ( value , destination , out bytesWritten , format ) ;
248
+ public static bool TryFormat ( ulong value , Span < byte > destination , out int bytesWritten , StandardFormat format = default )
249
+ {
250
+ if ( format . IsDefault )
251
+ {
252
+ return Number . TryUInt64ToDecStr ( value , destination , out bytesWritten ) ;
253
+ }
254
+
255
+ switch ( format . Symbol | 0x20 )
256
+ {
257
+ case 'd' :
258
+ return Number . TryUInt64ToDecStr ( value , format . PrecisionOrZero , destination , out bytesWritten ) ;
259
+
260
+ case 'x' :
261
+ return Number . TryInt64ToHexStr ( ( long ) value , Number . GetHexBase ( format . Symbol ) , format . PrecisionOrZero , destination , out bytesWritten ) ;
262
+
263
+ case 'n' :
264
+ return FormattingHelpers . TryFormat ( value , destination , out bytesWritten , format ) ;
265
+
266
+ case 'g' or 'r' :
267
+ if ( format . HasPrecision )
268
+ {
269
+ ThrowGWithPrecisionNotSupported ( ) ;
270
+ }
271
+ goto case 'd' ;
272
+
273
+ default :
274
+ ThrowHelper . ThrowFormatException_BadFormatSpecifier ( ) ;
275
+ goto case 'd' ;
276
+ }
277
+ }
182
278
183
279
/// <summary>
184
280
/// Formats an Int64 as a UTF8 string.
@@ -201,7 +297,44 @@ public static bool TryFormat(ulong value, Span<byte> destination, out int bytesW
201
297
/// <exceptions>
202
298
/// <cref>System.FormatException</cref> if the format is not valid for this data type.
203
299
/// </exceptions>
204
- public static bool TryFormat ( long value , Span < byte > destination , out int bytesWritten , StandardFormat format = default ) =>
205
- FormattingHelpers . TryFormat ( value , destination , out bytesWritten , format ) ;
300
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
301
+ public static bool TryFormat ( long value , Span < byte > destination , out int bytesWritten , StandardFormat format = default )
302
+ {
303
+ if ( format . IsDefault )
304
+ {
305
+ return value >= 0 ?
306
+ Number . TryUInt64ToDecStr ( ( ulong ) value , destination , out bytesWritten ) :
307
+ Number . TryNegativeInt64ToDecStr ( value , format . PrecisionOrZero , "-"u8 , destination , out bytesWritten ) ;
308
+ }
309
+
310
+ switch ( format . Symbol | 0x20 )
311
+ {
312
+ case 'd' :
313
+ return value >= 0 ?
314
+ Number . TryUInt64ToDecStr ( ( ulong ) value , format . PrecisionOrZero , destination , out bytesWritten ) :
315
+ Number . TryNegativeInt64ToDecStr ( value , format . PrecisionOrZero , "-"u8 , destination , out bytesWritten ) ;
316
+
317
+ case 'x' :
318
+ return Number . TryInt64ToHexStr ( value , Number . GetHexBase ( format . Symbol ) , format . PrecisionOrZero , destination , out bytesWritten ) ;
319
+
320
+ case 'n' :
321
+ return FormattingHelpers . TryFormat ( value , destination , out bytesWritten , format ) ;
322
+
323
+ case 'g' or 'r' :
324
+ if ( format . HasPrecision )
325
+ {
326
+ ThrowGWithPrecisionNotSupported ( ) ;
327
+ }
328
+ goto case 'd' ;
329
+
330
+ default :
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.
338
+ throw new NotSupportedException ( SR . Argument_GWithPrecisionNotSupported ) ;
206
339
}
207
340
}
0 commit comments