예제 #1
0
파일: UInt32.cs 프로젝트: zgramana/mono
        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 = Int32.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');

                    if ((val > MaxValue / 10) || (val == (MaxValue / 10) && d > (MaxValue % 10)))
                    {
                        if (!tryParse)
                        {
                            exc = new OverflowException(Locale.GetText("Value is too large"));
                        }
                        return(false);
                    }
                    val         = (val * 10) + d;
                    digits_seen = true;
                }
                else if (!Int32.ProcessTrailingWhitespace(tryParse, s, i, ref exc))
                {
                    return(false);
                }
            }
            if (!digits_seen)
            {
                if (!tryParse)
                {
                    exc = Int32.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);
        }
예제 #2
0
파일: Int16.cs 프로젝트: yonder/mono
        internal static bool Parse(string s, bool tryParse, out short result, out Exception exc)
        {
            short 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 = Int32.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')
                {
                    byte d = (byte)(c - '0');

                    if (val > (MaxValue / 10))
                    {
                        goto overflow;
                    }

                    if (val == (MaxValue / 10))
                    {
                        if ((d > (MaxValue % 10)) && (sign == 1 || (d > ((MaxValue % 10) + 1))))
                        {
                            goto overflow;
                        }
                        if (sign == -1)
                        {
                            val = (short)((val * sign * 10) - d);
                        }
                        else
                        {
                            val = (short)((val * 10) + d);
                        }

                        if (Int32.ProcessTrailingWhitespace(tryParse, s, i + 1, ref exc))
                        {
                            result = val;
                            return(true);
                        }
                        goto overflow;
                    }
                    else
                    {
                        val = (short)(val * 10 + d);
                    }


                    digits_seen = true;
                }
                else if (!Int32.ProcessTrailingWhitespace(tryParse, s, i, ref exc))
                {
                    return(false);
                }
            }
            if (!digits_seen)
            {
                if (!tryParse)
                {
                    exc = Int32.GetFormatException();
                }
                return(false);
            }

            if (sign == -1)
            {
                result = (short)(val * sign);
            }
            else
            {
                result = val;
            }

            return(true);

overflow:
            if (!tryParse)
            {
                exc = new OverflowException("Value is too large");
            }
            return(false);
        }
예제 #3
0
        internal static bool Parse(string s, bool tryParse, out long result, out Exception exc)
        {
            long val = 0;
            int  len;
            int  i, sign = 1;
            bool digits_seen = false;

            result = 0;
            exc    = null;
            NumberFormatInfo nfi = Thread.CurrentThread.CurrentCulture.NumberFormat;

            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 = Int32.GetFormatException();
                }
                return(false);
            }

            if (String.Compare(s, i, nfi.PositiveSign, 0, nfi.PositiveSign.Length) == 0)
            {
                i += nfi.PositiveSign.Length;
            }
            else if (String.Compare(s, i, nfi.NegativeSign, 0, nfi.NegativeSign.Length) == 0)
            {
                sign = -1;
                i   += nfi.NegativeSign.Length;
            }

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

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

                    if (val > (MaxValue / 10))
                    {
                        goto overflow;
                    }

                    if (val == (MaxValue / 10))
                    {
                        if ((d > (MaxValue % 10)) && (sign == 1 || (d > ((MaxValue % 10) + 1))))
                        {
                            goto overflow;
                        }
                        if (sign == -1)
                        {
                            val = (val * sign * 10) - d;
                        }
                        else
                        {
                            val = (val * 10) + d;
                        }

                        if (Int32.ProcessTrailingWhitespace(tryParse, s, i + 1, ref exc))
                        {
                            result = val;
                            return(true);
                        }
                        goto overflow;
                    }
                    else
                    {
                        val = val * 10 + d;
                    }

                    digits_seen = true;
                }
                else if (!Int32.ProcessTrailingWhitespace(tryParse, s, i, ref exc))
                {
                    return(false);
                }
            }
            if (!digits_seen)
            {
                if (!tryParse)
                {
                    exc = Int32.GetFormatException();
                }
                return(false);
            }

            if (sign == -1)
            {
                result = val * sign;
            }
            else
            {
                result = val;
            }

            return(true);

overflow:
            if (!tryParse)
            {
                exc = new OverflowException("Value is too large");
            }
            return(false);
        }
예제 #4
0
파일: UInt64.cs 프로젝트: yonder/mono
        internal static bool Parse(string s, bool tryParse, out ulong result, out Exception exc)
        {
            ulong val = 0;
            int   len;
            int   i;
            bool  digits_seen       = false;
            bool  has_negative_sign = false;

            exc    = null;
            result = 0;

            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 = Int32.GetFormatException();
                }
                return(false);
            }

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

            // Actual number stuff
            for (; i < len; i++)
            {
                c = s [i];

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

                    if (val > MaxValue / 10 || (val == MaxValue / 10 && d > MaxValue % 10))
                    {
                        if (!tryParse)
                        {
                            exc = new OverflowException("Value is too large.");
                        }
                        return(false);
                    }

                    val         = (val * 10) + d;
                    digits_seen = true;
                }
                else if (!Int32.ProcessTrailingWhitespace(tryParse, s, i, ref exc))
                {
                    return(false);
                }
            }

            if (!digits_seen)
            {
                if (!tryParse)
                {
                    exc = Int32.GetFormatException();
                }
                return(false);
            }

            if (has_negative_sign && val > 0)
            {
                if (!tryParse)
                {
                    exc = new OverflowException("Negative number.");
                }
                return(false);
            }

            result = val;
            return(true);
        }