Beispiel #1
0
 public bool IsOverflow()
 {
     return(IntParser.IsOverflow(number, sign));
 }
Beispiel #2
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);
        }