@@ -12,7 +12,7 @@ namespace Microsoft.Net.Http.Headers
12
12
{
13
13
public static class HeaderUtilities
14
14
{
15
- private static readonly int _int64MaxStringLength = 20 ;
15
+ private static readonly int _int64MaxStringLength = 19 ;
16
16
private const string QualityName = "q" ;
17
17
internal const string BytesUnit = "bytes" ;
18
18
@@ -269,7 +269,7 @@ public static bool TryParseSeconds(StringValues headerValues, string targetValue
269
269
var tokenLength = HttpRuleParser . GetTokenLength ( headerValues [ i ] , current ) ;
270
270
if ( tokenLength == targetValue . Length
271
271
&& string . Compare ( headerValues [ i ] , current , targetValue , 0 , tokenLength , StringComparison . OrdinalIgnoreCase ) == 0
272
- && TryParseInt64FromHeaderValue ( current + tokenLength , headerValues [ i ] , out seconds ) )
272
+ && TryParseNonNegativeInt64FromHeaderValue ( current + tokenLength , headerValues [ i ] , out seconds ) )
273
273
{
274
274
// Token matches target value and seconds were parsed
275
275
value = TimeSpan . FromSeconds ( seconds ) ;
@@ -342,7 +342,7 @@ public static bool ContainsCacheDirective(StringValues cacheControlDirectives, s
342
342
return false ;
343
343
}
344
344
345
- private static unsafe bool TryParseInt64FromHeaderValue ( int startIndex , string headerValue , out long result )
345
+ private static unsafe bool TryParseNonNegativeInt64FromHeaderValue ( int startIndex , string headerValue , out long result )
346
346
{
347
347
// Trim leading whitespace
348
348
startIndex += HttpRuleParser . GetWhitespaceLength ( headerValue , startIndex ) ;
@@ -359,7 +359,7 @@ private static unsafe bool TryParseInt64FromHeaderValue(int startIndex, string h
359
359
startIndex += HttpRuleParser . GetWhitespaceLength ( headerValue , startIndex ) ;
360
360
361
361
// Try parse the number
362
- if ( TryParseInt64 ( new StringSegment ( headerValue , startIndex , HttpRuleParser . GetNumberLength ( headerValue , startIndex , false ) ) , out result ) )
362
+ if ( TryParseNonNegativeInt64 ( new StringSegment ( headerValue , startIndex , HttpRuleParser . GetNumberLength ( headerValue , startIndex , false ) ) , out result ) )
363
363
{
364
364
return true ;
365
365
}
@@ -368,9 +368,9 @@ private static unsafe bool TryParseInt64FromHeaderValue(int startIndex, string h
368
368
return false ;
369
369
}
370
370
371
- internal static bool TryParseInt32 ( string value , out int result )
371
+ internal static bool TryParseNonNegativeInt32 ( string value , out int result )
372
372
{
373
- return TryParseInt32 ( new StringSegment ( value ) , out result ) ;
373
+ return TryParseNonNegativeInt32 ( new StringSegment ( value ) , out result ) ;
374
374
}
375
375
376
376
/// <summary>
@@ -388,12 +388,12 @@ internal static bool TryParseInt32(string value, out int result)
388
388
/// result will be overwritten.
389
389
/// </param>
390
390
/// <returns><code>true</code> if parsing succeeded; otherwise, <code>false</code>.</returns>
391
- public static bool TryParseInt64 ( string value , out long result )
391
+ public static bool TryParseNonNegativeInt64 ( string value , out long result )
392
392
{
393
- return TryParseInt64 ( new StringSegment ( value ) , out result ) ;
393
+ return TryParseNonNegativeInt64 ( new StringSegment ( value ) , out result ) ;
394
394
}
395
395
396
- internal static unsafe bool TryParseInt32 ( StringSegment value , out int result )
396
+ internal static unsafe bool TryParseNonNegativeInt32 ( StringSegment value , out int result )
397
397
{
398
398
if ( string . IsNullOrEmpty ( value . Buffer ) || value . Length == 0 )
399
399
{
@@ -444,7 +444,7 @@ internal static unsafe bool TryParseInt32(StringSegment value, out int result)
444
444
/// originally supplied in result will be overwritten.
445
445
/// </param>
446
446
/// <returns><code>true</code> if parsing succeeded; otherwise, <code>false</code>.</returns>
447
- public static unsafe bool TryParseInt64 ( StringSegment value , out long result )
447
+ public static unsafe bool TryParseNonNegativeInt64 ( StringSegment value , out long result )
448
448
{
449
449
if ( string . IsNullOrEmpty ( value . Buffer ) || value . Length == 0 )
450
450
{
@@ -481,31 +481,22 @@ public static unsafe bool TryParseInt64(StringSegment value, out long result)
481
481
}
482
482
483
483
/// <summary>
484
- /// Converts the signed 64-bit numeric value to its equivalent string representation.
484
+ /// Converts the non-negative 64-bit numeric value to its equivalent string representation.
485
485
/// </summary>
486
486
/// <param name="value">
487
487
/// The number to convert.
488
488
/// </param>
489
489
/// <returns>
490
- /// The string representation of the value of this instance, consisting of a minus sign if the value is
491
- /// negative, and a sequence of digits ranging from 0 to 9 with no leading zeroes.
490
+ /// The string representation of the value of this instance, consisting of a sequence of digits ranging from 0 to 9 with no leading zeroes.
492
491
/// </returns>
493
- public unsafe static string FormatInt64 ( long value )
492
+ public unsafe static string FormatNonNegativeInt64 ( long value )
494
493
{
495
- var position = _int64MaxStringLength ;
496
- var negative = false ;
497
-
498
494
if ( value < 0 )
499
495
{
500
- // Not possible to compute absolute value of MinValue, return the exact string instead.
501
- if ( value == long . MinValue )
502
- {
503
- return "-9223372036854775808" ;
504
- }
505
- negative = true ;
506
- value = - value ;
496
+ throw new ArgumentOutOfRangeException ( nameof ( value ) , value , "The value to be formatted must be non-negative." ) ;
507
497
}
508
498
499
+ var position = _int64MaxStringLength ;
509
500
char * charBuffer = stackalloc char [ _int64MaxStringLength ] ;
510
501
511
502
do
@@ -517,11 +508,6 @@ public unsafe static string FormatInt64(long value)
517
508
}
518
509
while ( value != 0 ) ;
519
510
520
- if ( negative )
521
- {
522
- charBuffer [ -- position ] = '-' ;
523
- }
524
-
525
511
return new string ( charBuffer , position , _int64MaxStringLength - position ) ;
526
512
}
527
513
0 commit comments