private RangeHeaderValue(RangeHeaderValue source)
     : this()
 {
     if (source.ranges != null)
     {
         foreach (var item in source.ranges)
         {
             Ranges.Add(item);
         }
     }
 }
        public static bool TryParse(string input, out RangeHeaderValue parsedValue)
        {
            parsedValue = null;

            var lexer = new Lexer(input);
            var t     = lexer.Scan();

            if (t != Token.Type.Token)
            {
                return(false);
            }

            var value = new RangeHeaderValue();

            value.unit = lexer.GetStringValue(t);

            t = lexer.Scan();
            if (t != Token.Type.SeparatorEqual)
            {
                return(false);
            }

            bool token_read;

            do
            {
                long?from = null, to = null;
                long number;
                token_read = false;

                t = lexer.Scan(recognizeDash: true);
                switch (t.Kind)
                {
                case Token.Type.SeparatorDash:
                    t = lexer.Scan();
                    if (!lexer.TryGetNumericValue(t, out number))
                    {
                        return(false);
                    }

                    to = number;
                    break;

                case Token.Type.Token:
                    string s      = lexer.GetStringValue(t);
                    var    values = s.Split(new [] { '-' }, StringSplitOptions.RemoveEmptyEntries);
                    if (!Parser.Long.TryParse(values[0], out number))
                    {
                        return(false);
                    }

                    switch (values.Length)
                    {
                    case 1:
                        t    = lexer.Scan(recognizeDash: true);
                        from = number;
                        switch (t.Kind)
                        {
                        case Token.Type.SeparatorDash:
                            t = lexer.Scan();
                            if (t != Token.Type.Token)
                            {
                                token_read = true;
                                break;
                            }

                            if (!lexer.TryGetNumericValue(t, out number))
                            {
                                return(false);
                            }

                            to = number;
                            if (to < from)
                            {
                                return(false);
                            }

                            break;

                        case Token.Type.End:
                            if (s.Length > 0 && s [s.Length - 1] != '-')
                            {
                                return(false);
                            }

                            token_read = true;
                            break;

                        case Token.Type.SeparatorComma:
                            token_read = true;
                            break;

                        default:
                            return(false);
                        }
                        break;

                    case 2:
                        from = number;

                        if (!Parser.Long.TryParse(values[1], out number))
                        {
                            return(false);
                        }

                        to = number;
                        if (to < from)
                        {
                            return(false);
                        }

                        break;

                    default:
                        return(false);
                    }

                    break;

                default:
                    return(false);
                }

                value.Ranges.Add(new RangeItemHeaderValue(from, to));
                if (!token_read)
                {
                    t = lexer.Scan();
                }
            } while (t == Token.Type.SeparatorComma);

            if (t != Token.Type.End)
            {
                return(false);
            }

            parsedValue = value;
            return(true);
        }