Example #1
0
        internal static bool Parse(string s, bool tryParse, out Int64 result, out Exception exc)
        {
            Int64 val = 0;
            int   len;
            int   i;
            int   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' && c <= '9')
                {
                    if (tryParse)
                    {
                        val = val * 10 + (c - '0') * sign;

                        if (sign == 1)
                        {
                            if (val < 0)
                            {
                                return(false);
                            }
                        }
                        else if (val > 0)
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        val = checked (val * 10 + (c - '0') * sign);
                    }
                    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);
        }