Exemplo n.º 1
0
 public void AfterLoop(IntParser parser)
 {
     if (parser.AllowHexSpecifier)
     {
         number = (int)hex;
     }
 }
Exemplo n.º 2
0
        internal static bool Parse(string s, NumberStyles style, IFormatProvider provider, bool tryParse, out uint result, out Exception exc)
        {
            result = 0;

            if (!IntParser.CheckInput(s, style, tryParse, out exc))
            {
                return(false);
            }

            IntParser parser = new IntParser(s, style, provider, tryParse);
            Num       n      = new Num();

            if (!parser.Start(n))
            {
                exc = parser.exc;
                return(false);
            }

            result = n.number;

            return(true);
        }
Exemplo n.º 3
0
        internal static bool Parse(string s, bool tryParse, out sbyte result, out Exception exc)
        {
            int  ival = 0;
            int  len;
            int  i;
            bool neg         = false;
            bool digits_seen = false;

            result = 0;
            exc    = null;

            if (s == null)
            {
                if (!tryParse)
                {
                    exc = new ArgumentNullException("s");
                }
                return(false);
            }

            len = s.Length;

            char c;

            for (i = 0; i < len; i++)
            {
                c = s[i];
                if (!Char.IsWhiteSpace(c))
                {
                    break;
                }
            }

            if (i == len)
            {
                if (!tryParse)
                {
                    exc = IntParser.GetFormatException();
                }
                return(false);
            }

            c = s[i];
            if (c == '+')
            {
                i++;
            }
            else if (c == '-')
            {
                neg = true;
                i++;
            }

            for (; i < len; i++)
            {
                c = s[i];

                if (c >= '0' && c <= '9')
                {
                    if (tryParse)
                    {
                        int intval = ival * 10 - (int)(c - '0');

                        if (intval < MinValue)
                        {
                            return(false);
                        }
                        ival = (sbyte)intval;
                    }
                    else
                    {
                        ival = checked (ival * 10 - (int)(c - '0'));
                    }
                    digits_seen = true;
                }
                else
                {
                    if (Char.IsWhiteSpace(c))
                    {
                        for (i++; i < len; i++)
                        {
                            if (!Char.IsWhiteSpace(s[i]))
                            {
                                if (!tryParse)
                                {
                                    exc = IntParser.GetFormatException();
                                }
                                return(false);
                            }
                        }
                        break;
                    }
                    else
                    {
                        if (!tryParse)
                        {
                            exc = IntParser.GetFormatException();
                        }
                        return(false);
                    }
                }
            }
            if (!digits_seen)
            {
                if (!tryParse)
                {
                    exc = IntParser.GetFormatException();
                }
                return(false);
            }

            ival = neg ? ival : -ival;
            if (ival < MinValue || ival > MaxValue)
            {
                if (!tryParse)
                {
                    exc = new OverflowException();
                }
                return(false);
            }

            result = (sbyte)ival;
            return(true);
        }
Exemplo n.º 4
0
 public void AfterLoop(IntParser parser)
 {
 }
Exemplo n.º 5
0
 public void BeforeLoop(IntParser parser)
 {
 }
Exemplo n.º 6
0
 public bool IsHexOverflow()
 {
     return(IntParser.IsHexOverflow(number));
 }
Exemplo n.º 7
0
        internal static bool Parse(string s, bool tryParse, out uint result, out Exception exc)
        {
            uint val = 0;
            int  len;
            int  i;
            bool digits_seen       = false;
            bool has_negative_sign = false;

            result = 0;
            exc    = null;

            if (s == null)
            {
                if (!tryParse)
                {
                    exc = new ArgumentNullException("s");
                }
                return(false);
            }

            len = s.Length;

            char c;

            for (i = 0; i < len; i++)
            {
                c = s[i];
                if (!Char.IsWhiteSpace(c))
                {
                    break;
                }
            }

            if (i == len)
            {
                if (!tryParse)
                {
                    exc = IntParser.GetFormatException();
                }
                return(false);
            }

            if (s[i] == '+')
            {
                i++;
            }
            else
            if (s[i] == '-')
            {
                i++;
                has_negative_sign = true;
            }

            for (; i < len; i++)
            {
                c = s[i];

                if (c >= '0' && c <= '9')
                {
                    uint d = (uint)(c - '0');

                    ulong v = ((ulong)val) * 10 + d;
                    if (v > MaxValue)
                    {
                        if (!tryParse)
                        {
                            exc = IntParser.GetOverflowException();
                        }
                        return(false);
                    }
                    val         = (uint)v;
                    digits_seen = true;
                }
                else
                {
                    if (Char.IsWhiteSpace(c))
                    {
                        for (i++; i < len; i++)
                        {
                            if (!Char.IsWhiteSpace(s[i]))
                            {
                                if (!tryParse)
                                {
                                    exc = IntParser.GetFormatException();
                                }
                                return(false);
                            }
                        }
                        break;
                    }
                    else
                    {
                        if (!tryParse)
                        {
                            exc = IntParser.GetFormatException();
                        }
                        return(false);
                    }
                }
            }
            if (!digits_seen)
            {
                if (!tryParse)
                {
                    exc = IntParser.GetFormatException();
                }
                return(false);
            }

            // -0 is legal but other negative values are not
            if (has_negative_sign && (val > 0))
            {
                if (!tryParse)
                {
                    exc = new OverflowException(
                        Locale.GetText("Negative number"));
                }
                return(false);
            }

            result = val;
            return(true);
        }
Exemplo n.º 8
0
        // FIXME: check if digits are group in correct numbers between the group separators
        internal static bool Parse(string s, NumberStyles style, IFormatProvider provider, bool tryParse, out double result, out Exception exc)
        {
            result = 0;
            exc    = null;

            if (s == null)
            {
                if (!tryParse)
                {
                    exc = new ArgumentNullException("s");
                }
                return(false);
            }
            if (s.Length == 0)
            {
                if (!tryParse)
                {
                    exc = new FormatException();
                }
                return(false);
            }
#if NET_2_0
            // yes it's counter intuitive (buggy?) but even TryParse actually throws in this case
            if ((style & NumberStyles.AllowHexSpecifier) != 0)
            {
                string msg = Locale.GetText("Double doesn't support parsing with '{0}'.", "AllowHexSpecifier");
                throw new ArgumentException(msg);
            }
#endif
            if (style > NumberStyles.Any)
            {
                if (!tryParse)
                {
                    exc = new ArgumentException();
                }
                return(false);
            }

            NumberFormatInfo format = NumberFormatInfo.GetInstance(provider);
            if (format == null)
            {
                throw new Exception("How did this happen?");
            }

            if (s == format.NaNSymbol)
            {
                result = Double.NaN;
                return(true);
            }
            if (s == format.PositiveInfinitySymbol)
            {
                result = Double.PositiveInfinity;
                return(true);
            }
            if (s == format.NegativeInfinitySymbol)
            {
                result = Double.NegativeInfinity;
                return(true);
            }

            //
            // validate and prepare string for C
            //
            int    len    = s.Length;
            string numstr = "";
            int    didx   = 0;
            int    sidx   = 0;
            char   c;

            if ((style & NumberStyles.AllowLeadingWhite) != 0)
            {
                while (sidx < len && Char.IsWhiteSpace(c = s[sidx]))
                {
                    sidx++;
                }

                if (sidx == len)
                {
                    if (!tryParse)
                    {
                        exc = IntParser.GetFormatException();
                    }
                    return(false);
                }
            }

            bool allow_trailing_white = ((style & NumberStyles.AllowTrailingWhite) != 0);

            //
            // Machine state
            //
            int state = State_AllowSign;

            //
            // Setup
            //
            string decimal_separator     = null;
            string group_separator       = null;
            string currency_symbol       = null;
            int    decimal_separator_len = 0;
            int    group_separator_len   = 0;
            int    currency_symbol_len   = 0;
            if ((style & NumberStyles.AllowDecimalPoint) != 0)
            {
                decimal_separator     = format.NumberDecimalSeparator;
                decimal_separator_len = decimal_separator.Length;
            }
            if ((style & NumberStyles.AllowThousands) != 0)
            {
                group_separator     = format.NumberGroupSeparator;
                group_separator_len = group_separator.Length;
            }
            if ((style & NumberStyles.AllowCurrencySymbol) != 0)
            {
                currency_symbol     = format.CurrencySymbol;
                currency_symbol_len = currency_symbol.Length;
            }
            string positive = format.PositiveSign;
            string negative = format.NegativeSign;

            for (; sidx < len; sidx++)
            {
                c = s[sidx];

                if (c == '\0')
                {
                    sidx = len;
                    continue;
                }

                switch (state)
                {
                case State_AllowSign:
                    if ((style & NumberStyles.AllowLeadingSign) != 0)
                    {
                        if (c == positive[0] &&
                            s.Substring(sidx, positive.Length) == positive)
                        {
                            state = State_Digits;
                            sidx += positive.Length - 1;
                            continue;
                        }

                        if (c == negative[0] &&
                            s.Substring(sidx, negative.Length) == negative)
                        {
                            state   = State_Digits;
                            numstr += '-';
                            sidx   += negative.Length - 1;
                            continue;
                        }
                    }
                    state = State_Digits;
                    goto case State_Digits;

                case State_Digits:
                    if (Char.IsDigit(c))
                    {
                        numstr += c;
                        break;
                    }
                    if (c == 'e' || c == 'E')
                    {
                        goto case State_Decimal;
                    }

                    if (decimal_separator != null &&
                        decimal_separator[0] == c)
                    {
                        if (String.CompareOrdinal(s, sidx, decimal_separator, 0, decimal_separator_len) == 0)
                        {
                            numstr += '.';
                            sidx   += decimal_separator_len - 1;
                            state   = State_Decimal;
                            break;
                        }
                    }
                    if (group_separator != null &&
                        group_separator[0] == c)
                    {
                        if (s.Substring(sidx, group_separator_len) ==
                            group_separator)
                        {
                            sidx += group_separator_len - 1;
                            state = State_Digits;
                            break;
                        }
                    }
                    if (currency_symbol != null &&
                        currency_symbol[0] == c)
                    {
                        if (s.Substring(sidx, currency_symbol_len) ==
                            currency_symbol)
                        {
                            sidx += currency_symbol_len - 1;
                            state = State_Digits;
                            break;
                        }
                    }

                    if (Char.IsWhiteSpace(c))
                    {
                        goto case State_ConsumeWhiteSpace;
                    }

                    if (!tryParse)
                    {
                        exc = new FormatException("Unknown char: " + c);
                    }
                    return(false);

                case State_Decimal:
                    if (Char.IsDigit(c))
                    {
                        numstr += c;
                        break;
                    }

                    if (c == 'e' || c == 'E')
                    {
                        if ((style & NumberStyles.AllowExponent) == 0)
                        {
                            if (!tryParse)
                            {
                                exc = new FormatException("Unknown char: " + c);
                            }
                            return(false);
                        }
                        numstr += c;
                        state   = State_ExponentSign;
                        break;
                    }

                    if (Char.IsWhiteSpace(c))
                    {
                        goto case State_ConsumeWhiteSpace;
                    }

                    if (!tryParse)
                    {
                        exc = new FormatException("Unknown char: " + c);
                    }
                    return(false);

                case State_ExponentSign:
                    if (Char.IsDigit(c))
                    {
                        state = State_Exponent;
                        goto case State_Exponent;
                    }

                    if (c == positive[0] &&
                        s.Substring(sidx, positive.Length) == positive)
                    {
                        state = State_Digits;
                        sidx += positive.Length - 1;
                        continue;
                    }

                    if (c == negative[0] &&
                        s.Substring(sidx, negative.Length) == negative)
                    {
                        state   = State_Digits;
                        numstr += '-';
                        sidx   += negative.Length - 1;
                        continue;
                    }

                    if (Char.IsWhiteSpace(c))
                    {
                        goto case State_ConsumeWhiteSpace;
                    }

                    if (!tryParse)
                    {
                        exc = new FormatException("Unknown char: " + c);
                    }
                    return(false);

                case State_Exponent:
                    if (Char.IsDigit(c))
                    {
                        numstr += c;
                        break;
                    }

                    if (Char.IsWhiteSpace(c))
                    {
                        goto case State_ConsumeWhiteSpace;
                    }

                    if (!tryParse)
                    {
                        exc = new FormatException("Unknown char: " + c);
                    }
                    return(false);

                case State_ConsumeWhiteSpace:
                    if (allow_trailing_white && Char.IsWhiteSpace(c))
                    {
                        state = State_ConsumeWhiteSpace;
                        break;
                    }

                    if (!tryParse)
                    {
                        exc = new FormatException("Unknown char");
                    }
                    return(false);
                }

                if (state == State_Exit)
                {
                    break;
                }
            }

            double retVal;
            try
            {
                retVal = Native.Global.parseFloat(numstr);
            }
            catch
            {
                if (!tryParse)
                {
                    exc = IntParser.GetFormatException();
                }
                return(false);
            }

            //if (!ParseImpl(p, out retVal))
            //{
            //    if (!tryParse)
            //        exc = IntParser.GetFormatException();
            //    return false;
            //}

            if (IsPositiveInfinity(retVal) || IsNegativeInfinity(retVal))
            {
                if (!tryParse)
                {
                    exc = new OverflowException();
                }
                return(false);
            }

            result = retVal;
            return(true);
        }
Exemplo n.º 9
0
 public void BeforeLoop(IntParser parser)
 {
     sign = parser.negative ? -1 : 1;
 }
Exemplo n.º 10
0
 public bool IsOverflow()
 {
     return(IntParser.IsOverflow(number, sign));
 }
Exemplo n.º 11
0
 public bool IsHexOverflow()
 {
     return(IntParser.IsHexOverflow(hex));
 }
Exemplo n.º 12
0
        //#endif
        #endregion

        #region Parse
        internal static bool Parse(string s, bool tryParse, out int result, out Exception exc)
        {
            int  val = 0;
            int  len;
            int  i, sign = 1;
            bool digits_seen = false;

            result = 0;
            exc    = null;

            if (s == null)
            {
                if (!tryParse)
                {
                    exc = new ArgumentNullException("s");
                }
                return(false);
            }

            len = s.Length;

            char c;

            for (i = 0; i < len; i++)
            {
                c = s[i];
                if (!Char.IsWhiteSpace(c))
                {
                    break;
                }
            }

            if (i == len)
            {
                if (!tryParse)
                {
                    exc = IntParser.GetFormatException();
                }
                return(false);
            }

            c = s[i];
            if (c == '+')
            {
                i++;
            }
            else if (c == '-')
            {
                sign = -1;
                i++;
            }

            for (; i < len; i++)
            {
                c = s[i];

                if (c == '\0')
                {
                    i = len;
                    continue;
                }

                if (c >= '0' && c <= '9')
                {
                    byte d = (byte)(c - '0');

                    val = val * 10 + d * sign;
                    if (IntParser.IsOverflow(val, sign))
                    {
                        if (!tryParse)
                        {
                            exc = IntParser.GetOverflowException();
                        }
                        return(false);
                    }

                    digits_seen = true;
                }
                else
                {
                    if (Char.IsWhiteSpace(c))
                    {
                        for (i++; i < len; i++)
                        {
                            if (!Char.IsWhiteSpace(s[i]))
                            {
                                if (!tryParse)
                                {
                                    exc = IntParser.GetFormatException();
                                }
                                return(false);
                            }
                        }
                        break;
                    }
                    else
                    {
                        if (!tryParse)
                        {
                            exc = IntParser.GetFormatException();
                        }
                        return(false);
                    }
                }
            }
            if (!digits_seen)
            {
                if (!tryParse)
                {
                    exc = IntParser.GetFormatException();
                }
                return(false);
            }

            result = val;

            return(true);
        }