TryGetNumericValue() public method

public TryGetNumericValue ( Token token, int &value ) : bool
token Token
value int
return bool
Example #1
0
		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 {
				int? from = null, to = null;
				int number;
				token_read = false;

				t = lexer.Scan ();
				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 (!int.TryParse (values[0], out number))
						return false;

					switch (values.Length) {
					case 1:
						t = lexer.Scan ();
						switch (t.Kind) {
						case Token.Type.SeparatorDash:
							from = number;

							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;
						default:
							return false;
						}
						break;
					case 2:
						from = number;

						if (!int.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;
		}
Example #2
0
        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
            {
                int?from = null, to = null;
                int number;
                token_read = false;

                t = lexer.Scan();
                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 (!int.TryParse(values[0], out number))
                    {
                        return(false);
                    }

                    switch (values.Length)
                    {
                    case 1:
                        t    = lexer.Scan();
                        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 (!int.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);
        }
		public static bool TryParse (string input, out ContentRangeHeaderValue parsedValue)
		{
			parsedValue = null;

			var lexer = new Lexer (input);
			var t = lexer.Scan ();
			if (t != Token.Type.Token)
				return false;

			var value = new ContentRangeHeaderValue ();
			value.unit = lexer.GetStringValue (t);

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

			int nvalue;
			if (!lexer.IsStarStringValue (t)) {
				if (!lexer.TryGetNumericValue (t, out nvalue)) {
					var s = lexer.GetStringValue (t);
					if (s.Length < 3)
						return false;

					var sep = s.Split ('-');
					if (sep.Length != 2)
						return false;

					if (!int.TryParse (sep[0], NumberStyles.None, CultureInfo.InvariantCulture, out nvalue))
						return false;

					value.From = nvalue;

					if (!int.TryParse (sep[1], NumberStyles.None, CultureInfo.InvariantCulture, out nvalue))
						return false;

					value.To = nvalue;
				} else {
					value.From = nvalue;

					t = lexer.Scan (recognizeDash: true);
					if (t != Token.Type.SeparatorDash)
						return false;

					t = lexer.Scan ();

					if (!lexer.TryGetNumericValue (t, out nvalue))
						return false;

					value.To = nvalue;
				}
			}

			t = lexer.Scan ();

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

			t = lexer.Scan ();

			if (!lexer.IsStarStringValue (t)) {
				long lvalue;
				if (!lexer.TryGetNumericValue (t, out lvalue))
					return false;

				value.Length = lvalue;
			}

			t = lexer.Scan ();

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

			parsedValue = value;
 
			return true;
		}
        public static bool TryParse(string input, out ContentRangeHeaderValue parsedValue)
        {
            parsedValue = null;

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

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

            var value = new ContentRangeHeaderValue();

            value.unit = lexer.GetStringValue(t);

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

            int nvalue;

            if (!lexer.IsStarStringValue(t))
            {
                if (!lexer.TryGetNumericValue(t, out nvalue))
                {
                    var s = lexer.GetStringValue(t);
                    if (s.Length < 3)
                    {
                        return(false);
                    }

                    var sep = s.Split('-');
                    if (sep.Length != 2)
                    {
                        return(false);
                    }

                    if (!int.TryParse(sep[0], NumberStyles.None, CultureInfo.InvariantCulture, out nvalue))
                    {
                        return(false);
                    }

                    value.From = nvalue;

                    if (!int.TryParse(sep[1], NumberStyles.None, CultureInfo.InvariantCulture, out nvalue))
                    {
                        return(false);
                    }

                    value.To = nvalue;
                }
                else
                {
                    value.From = nvalue;

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

                    t = lexer.Scan();

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

                    value.To = nvalue;
                }
            }

            t = lexer.Scan();

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

            t = lexer.Scan();

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

                value.Length = nvalue;
            }

            t = lexer.Scan();

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

            parsedValue = value;

            return(true);
        }
Example #5
0
		public static bool TryParse (string input, out WarningHeaderValue parsedValue)
		{
			parsedValue = null;

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

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

			int code;
			if (!lexer.TryGetNumericValue (t, out code) || !IsCodeValid (code))
				return false;

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

			var next = t;
			if (lexer.PeekChar () == ':') {
				lexer.EatChar ();

				next = lexer.Scan ();
				if (next != Token.Type.Token)
					return false;
			}

			var value = new WarningHeaderValue ();
			value.Code = code;
			value.Agent = lexer.GetStringValue (t, next);

			t = lexer.Scan ();
			if (t != Token.Type.QuotedString)
				return false;

			value.Text = lexer.GetStringValue (t);

			t = lexer.Scan ();
			if (t == Token.Type.QuotedString) {
				DateTimeOffset date;
				if (!lexer.TryGetDateValue (t, out date))
					return false;

				value.Date = date;
				t = lexer.Scan ();
			}

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

			parsedValue = value;
			return true;
		}
Example #6
0
        public static bool TryParse(string input, out WarningHeaderValue parsedValue)
        {
            parsedValue = null;

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

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

            int code;

            if (!lexer.TryGetNumericValue(t, out code) || !IsCodeValid(code))
            {
                return(false);
            }

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

            var next = t;

            if (lexer.PeekChar() == ':')
            {
                lexer.EatChar();

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

            var value = new WarningHeaderValue();

            value.Code  = code;
            value.Agent = lexer.GetStringValue(t, next);

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

            value.Text = lexer.GetStringValue(t);

            t = lexer.Scan();
            if (t == Token.Type.QuotedString)
            {
                DateTimeOffset date;
                if (!lexer.TryGetDateValue(t, out date))
                {
                    return(false);
                }

                value.Date = date;
                t          = lexer.Scan();
            }

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

            parsedValue = value;
            return(true);
        }