internal void AddParseValueAction(int minimumDigits, int maximumDigits, char patternChar,
                                          int minimumValue, int maximumValue,
                                          Action <TBucket, int> valueSetter)
        {
            AddParseAction((cursor, bucket) =>
            {
                int startingIndex = cursor.Index;
                int value;
                bool negative = cursor.Match('-');
                if (negative && minimumValue >= 0)
                {
                    cursor.Move(startingIndex);
                    return(ParseResult <TResult> .UnexpectedNegative(cursor));
                }
                if (!cursor.ParseDigits(minimumDigits, maximumDigits, out value))
                {
                    cursor.Move(startingIndex);
                    return(ParseResult <TResult> .MismatchedNumber(cursor, new string(patternChar, minimumDigits)));
                }
                if (negative)
                {
                    value = -value;
                }
                if (value < minimumValue || value > maximumValue)
                {
                    cursor.Move(startingIndex);
                    return(ParseResult <TResult> .FieldValueOutOfRange(cursor, value, patternChar));
                }

                valueSetter(bucket, value);
                return(null);
            });
        }
Esempio n. 2
0
        /// <summary>
        /// Equivalent of <see cref="AddParseValueAction"/> but for 64-bit integers. Currently only
        /// positive values are supported.
        /// </summary>
        internal void AddParseInt64ValueAction(int minimumDigits, int maximumDigits, char patternChar,
                                               long minimumValue, long maximumValue, Action <TBucket, long> valueSetter)
        {
            Preconditions.DebugCheckArgumentRange(nameof(minimumValue), minimumValue, 0, long.MaxValue);
            AddParseAction((cursor, bucket) =>
            {
                int startingIndex = cursor.Index;
                if (!cursor.ParseInt64Digits(minimumDigits, maximumDigits, out long value))
                {
                    cursor.Move(startingIndex);
                    return(ParseResult <TResult> .MismatchedNumber(cursor, new string(patternChar, minimumDigits)));
                }
                if (value < minimumValue || value > maximumValue)
                {
                    cursor.Move(startingIndex);
                    return(ParseResult <TResult> .FieldValueOutOfRange(cursor, value, patternChar));
                }

                valueSetter(bucket, value);
                return(null);
            });
        }