PageRenderTime 16ms CodeModel.GetById 13ms app.highlight 2ms 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
 1using System.Diagnostics.Contracts;
 2using System.Globalization;
 3
 4namespace System.Net.Http.Headers
 5{
 6    internal class Int64NumberHeaderParser : BaseHeaderParser
 7    {
 8        // Note that we don't need a custom comparer even though we have a value type that gets boxed (comparing two
 9        // equal boxed value types returns 'false' since the object instances used for boxing the two values are 
10        // different). The reason is that the comparer is only used by HttpHeaders when comparing values in a collection.
11        // Value types are never used in collections (in fact HttpHeaderValueCollection expects T to be a reference
12        // type).
13
14        internal static readonly Int64NumberHeaderParser Parser = new Int64NumberHeaderParser();
15
16        private Int64NumberHeaderParser()
17            : base(false)
18        {
19        }
20
21        public override string ToString(object value)
22        {
23            Contract.Assert(value is long);
24
25            return ((long)value).ToString(NumberFormatInfo.InvariantInfo);
26        }
27
28        protected override int GetParsedValueLength(string value, int startIndex, object storeValue, 
29            out object parsedValue)
30        {
31            parsedValue = null;
32
33            int numberLength = HttpRuleParser.GetNumberLength(value, startIndex, false);
34
35            if ((numberLength == 0) || (numberLength > HttpRuleParser.MaxInt64Digits))
36            {
37                return 0;
38            }
39
40            long result = 0;
41            if (!HeaderUtilities.TryParseInt64(value.Substring(startIndex, numberLength), out result))
42            {
43                return 0;
44            }
45
46            parsedValue = result;
47            return numberLength;
48        }
49    }
50}