Example #1
0
        public bool Start(INumber number)
        {
            if (!BeforeLoop())
            {
                return(false);
            }

            int nDigits = 0;

            foundPoint = false;

            number.BeforeLoop(this);

            // Number stuff
            do
            {
                if (!IsValidDigit(s[pos], AllowHexSpecifier))
                {
                    if (FindGroupSeparator())
                    {
                        if (!AllowThousands)
                        {
                            if (!tryParse)
                            {
                                exc = GetFormatException();
                            }
                            return(false);
                        }
                        continue;
                    }

                    if (FindDecimalSeparator())
                    {
                        if (!AllowDecimalPoint)
                        {
                            if (!tryParse)
                            {
                                exc = GetFormatException("Decimal point is not allowed");
                            }
                            return(false);
                        }
                        foundPoint = true;
                        continue;
                    }

                    break;
                }
                else if (AllowHexSpecifier)
                {
                    nDigits++;
                    if (number.IsHexOverflow())
                    {
                        if (!tryParse)
                        {
                            exc = GetOverflowException(s);
                        }
                        return(false);
                    }
                    uint d = (uint)Hex2Dec(s[pos++]);
                    number.AddHexDigit(d);
                }
                else if (foundPoint)
                {
                    nDigits++;
                    // Allows decimal point as long as it's only
                    // followed by zeroes.
                    if (s[pos++] != '0')
                    {
                        if (!tryParse)
                        {
                            exc = GetOverflowException();
                        }
                        return(false);
                    }
                }
                else
                {
                    nDigits++;
                    int d = s[pos++] - '0';

                    if (number.CatchOverflowException)
                    {
                        try
                        {
                            number.AddDigit(d);
                        }
                        catch (OverflowException)
                        {
                            if (!tryParse)
                            {
                                exc = GetOverflowException();
                            }
                            return(false);
                        }
                    }
                    else
                    {
                        number.AddDigit(d);
                        if (number.IsOverflow())
                        {
                            if (!tryParse)
                            {
                                exc = GetOverflowException(s);
                            }
                            return(false);
                        }
                    }
                }
            } while (pos < s.Length);

            if (!AfterLoop(number, nDigits))
            {
                return(false);
            }

            return(true);
        }