@@ -36,7 +36,6 @@ private static Half ReadCore(ref Utf8JsonReader reader)
3636 Half result ;
3737
3838 byte [ ] ? rentedByteBuffer = null ;
39- char [ ] ? rentedCharBuffer = null ;
4039 int bufferLength = reader . ValueLength ;
4140
4241 Span < byte > byteBuffer = bufferLength <= JsonConstants . StackallocByteThreshold
@@ -46,24 +45,12 @@ private static Half ReadCore(ref Utf8JsonReader reader)
4645 int written = reader . CopyValue ( byteBuffer ) ;
4746 byteBuffer = byteBuffer . Slice ( 0 , written ) ;
4847
49- #if NET8_0_OR_GREATER
5048 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
5749 if ( rentedByteBuffer != null )
5850 {
5951 ArrayPool < byte > . Shared . Return ( rentedByteBuffer ) ;
6052 }
6153
62- if ( rentedCharBuffer != null )
63- {
64- ArrayPool < char > . Shared . Return ( rentedCharBuffer ) ;
65- }
66-
6754 if ( ! success )
6855 {
6956 ThrowHelper . ThrowFormatException ( NumericType . Half ) ;
@@ -186,31 +173,33 @@ private static void WriteFloatingPointConstant(Utf8JsonWriter writer, Half value
186173
187174 // Half.TryFormat/TryParse(ROS<byte>) are not available on .NET 7
188175 // 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+ {
190178#if NET8_0_OR_GREATER
191- ReadOnlySpan < byte > buffer ,
179+ bool success = Half . TryParse ( buffer , NumberStyles . Float | NumberStyles . AllowThousands , CultureInfo . InvariantCulture , out result ) ;
192180#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+ }
194195#endif
195- out Half result )
196- {
197- bool success = Half . TryParse ( buffer , NumberStyles . Float | NumberStyles . AllowThousands , CultureInfo . InvariantCulture , out result ) ;
198196
199197 // Half.TryParse is more lax with floating-point literals than other S.T.Json floating-point types
200198 // 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
210199 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 ) ) ;
214203 }
215204
216205 private static void Format (
0 commit comments