@@ -36,7 +36,6 @@ private static Half ReadCore(ref Utf8JsonReader reader)
36
36
Half result ;
37
37
38
38
byte [ ] ? rentedByteBuffer = null ;
39
- char [ ] ? rentedCharBuffer = null ;
40
39
int bufferLength = reader . ValueLength ;
41
40
42
41
Span < byte > byteBuffer = bufferLength <= JsonConstants . StackallocByteThreshold
@@ -46,24 +45,12 @@ private static Half ReadCore(ref Utf8JsonReader reader)
46
45
int written = reader . CopyValue ( byteBuffer ) ;
47
46
byteBuffer = byteBuffer . Slice ( 0 , written ) ;
48
47
49
- #if NET8_0_OR_GREATER
50
48
bool success = TryParse ( byteBuffer , out result ) ;
51
- #else
52
- // We need to transcode here instead of letting CopyValue do it for us because TryGetFloatingPointConstant only accepts ROS<byte>.
53
- Span < char > charBuffer = stackalloc char [ MaxFormatLength ] ;
54
- written = JsonReaderHelper . TranscodeHelper ( byteBuffer , charBuffer ) ;
55
- bool success = TryParse ( charBuffer , out result ) ;
56
- #endif
57
49
if ( rentedByteBuffer != null )
58
50
{
59
51
ArrayPool < byte > . Shared . Return ( rentedByteBuffer ) ;
60
52
}
61
53
62
- if ( rentedCharBuffer != null )
63
- {
64
- ArrayPool < char > . Shared . Return ( rentedCharBuffer ) ;
65
- }
66
-
67
54
if ( ! success )
68
55
{
69
56
ThrowHelper . ThrowFormatException ( NumericType . Half ) ;
@@ -186,31 +173,33 @@ private static void WriteFloatingPointConstant(Utf8JsonWriter writer, Half value
186
173
187
174
// Half.TryFormat/TryParse(ROS<byte>) are not available on .NET 7
188
175
// we need to use Half.TryFormat/TryParse(ROS<char>) in that case.
189
- private static bool TryParse (
176
+ private static bool TryParse ( ReadOnlySpan < byte > buffer , out Half result )
177
+ {
190
178
#if NET8_0_OR_GREATER
191
- ReadOnlySpan < byte > buffer ,
179
+ bool success = Half . TryParse ( buffer , NumberStyles . Float | NumberStyles . AllowThousands , CultureInfo . InvariantCulture , out result ) ;
192
180
#else
193
- ReadOnlySpan < char > buffer ,
181
+ char [ ] ? rentedCharBuffer = null ;
182
+
183
+ Span < char > charBuffer = buffer . Length <= JsonConstants . StackallocCharThreshold
184
+ ? stackalloc char [ JsonConstants . StackallocCharThreshold ]
185
+ : ( rentedCharBuffer = ArrayPool < char > . Shared . Rent ( buffer . Length ) ) ;
186
+
187
+ int written = JsonReaderHelper . TranscodeHelper ( buffer , charBuffer ) ;
188
+
189
+ bool success = Half . TryParse ( charBuffer , NumberStyles . Float | NumberStyles . AllowThousands , CultureInfo . InvariantCulture , out result ) ;
190
+
191
+ if ( rentedCharBuffer != null )
192
+ {
193
+ ArrayPool < char > . Shared . Return ( rentedCharBuffer ) ;
194
+ }
194
195
#endif
195
- out Half result )
196
- {
197
- bool success = Half . TryParse ( buffer , NumberStyles . Float | NumberStyles . AllowThousands , CultureInfo . InvariantCulture , out result ) ;
198
196
199
197
// Half.TryParse is more lax with floating-point literals than other S.T.Json floating-point types
200
198
// e.g: it parses "naN" successfully. Only succeed with the exact match.
201
- #if NET8_0_OR_GREATER
202
- ReadOnlySpan < byte > NaN = JsonConstants . NaNValue ;
203
- ReadOnlySpan < byte > PositiveInfinity = JsonConstants . PositiveInfinityValue ;
204
- ReadOnlySpan < byte > NegativeInfinity = JsonConstants . NegativeInfinityValue ;
205
- #else
206
- const string NaN = "NaN" ;
207
- const string PositiveInfinity = "Infinity" ;
208
- const string NegativeInfinity = "-Infinity" ;
209
- #endif
210
199
return success &&
211
- ( ! Half . IsNaN ( result ) || buffer . SequenceEqual ( NaN ) ) &&
212
- ( ! Half . IsPositiveInfinity ( result ) || buffer . SequenceEqual ( PositiveInfinity ) ) &&
213
- ( ! Half . IsNegativeInfinity ( result ) || buffer . SequenceEqual ( NegativeInfinity ) ) ;
200
+ ( ! Half . IsNaN ( result ) || buffer . SequenceEqual ( JsonConstants . NaNValue ) ) &&
201
+ ( ! Half . IsPositiveInfinity ( result ) || buffer . SequenceEqual ( JsonConstants . PositiveInfinityValue ) ) &&
202
+ ( ! Half . IsNegativeInfinity ( result ) || buffer . SequenceEqual ( JsonConstants . NegativeInfinityValue ) ) ;
214
203
}
215
204
216
205
private static void Format (
0 commit comments