PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/WCFWebApi/src/System.Net.Http/System/Net/Http/Headers/Int64NumberHeaderParser.cs

#
C# | 50 lines | 35 code | 10 blank | 5 comment | 4 complexity | 50d2bf39f255a7a324170f1c995020f8 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, Apache-2.0
  1. using System.Diagnostics.Contracts;
  2. using System.Globalization;
  3. namespace System.Net.Http.Headers
  4. {
  5. internal class Int64NumberHeaderParser : BaseHeaderParser
  6. {
  7. // Note that we don't need a custom comparer even though we have a value type that gets boxed (comparing two
  8. // equal boxed value types returns 'false' since the object instances used for boxing the two values are
  9. // different). The reason is that the comparer is only used by HttpHeaders when comparing values in a collection.
  10. // Value types are never used in collections (in fact HttpHeaderValueCollection expects T to be a reference
  11. // type).
  12. internal static readonly Int64NumberHeaderParser Parser = new Int64NumberHeaderParser();
  13. private Int64NumberHeaderParser()
  14. : base(false)
  15. {
  16. }
  17. public override string ToString(object value)
  18. {
  19. Contract.Assert(value is long);
  20. return ((long)value).ToString(NumberFormatInfo.InvariantInfo);
  21. }
  22. protected override int GetParsedValueLength(string value, int startIndex, object storeValue,
  23. out object parsedValue)
  24. {
  25. parsedValue = null;
  26. int numberLength = HttpRuleParser.GetNumberLength(value, startIndex, false);
  27. if ((numberLength == 0) || (numberLength > HttpRuleParser.MaxInt64Digits))
  28. {
  29. return 0;
  30. }
  31. long result = 0;
  32. if (!HeaderUtilities.TryParseInt64(value.Substring(startIndex, numberLength), out result))
  33. {
  34. return 0;
  35. }
  36. parsedValue = result;
  37. return numberLength;
  38. }
  39. }
  40. }