예제 #1
0
        private EcmaValue ParseInt(EcmaHeadObject obj, EcmaValue[] arg)
        {
            string inputString = arg[0].ToString(State);
            var    s           = inputString.TrimStart();

            var sign = 1;

            if (!System.String.IsNullOrEmpty(s))
            {
                if (s[0] == '-')
                {
                    sign = -1;
                }

                if (s[0] == '-' || s[0] == '+')
                {
                    s = s.Substring(1);
                }
            }

            var stripPrefix = true;

            int radix = arg.Length > 1 ? arg[1].ToInt32(State) : 0;

            if (radix == 0)
            {
                if (s.Length >= 2 && s.StartsWith("0x") || s.StartsWith("0X"))
                {
                    radix = 16;
                }
                else
                {
                    radix = 10;
                }
            }
            else if (radix < 2 || radix > 36)
            {
                return(EcmaValue.Number(double.NaN));
            }
            else if (radix != 16)
            {
                stripPrefix = false;
            }

            if (stripPrefix && s.Length >= 2 && s.StartsWith("0x") || s.StartsWith("0X"))
            {
                s = s.Substring(2);
            }

            try
            {
                return(EcmaValue.Number(sign * Parse(s, radix)));
            }
            catch
            {
                return(EcmaValue.Number(double.NaN));
            }
        }
예제 #2
0
        private EcmaValue ParseFloat(EcmaHeadObject obj, EcmaValue[] arg)
        {
            string input = arg[0].ToString(State).Trim();

            if (input.StartsWith("Infinity"))
            {
                return(EcmaValue.Number(Double.PositiveInfinity));
            }

            StringBuilder builder = new StringBuilder();
            int           i       = 0;

            for (; i < input.Length; i++)
            {
                char c = input[i];
                if (!EcmaChar.IsDigit(c))
                {
                    break;
                }
                builder.Append(c);
            }

            if (i >= input.Length || input[i] != '.')
            {
                if (builder.Length == 0)
                {
                    return(EcmaValue.Number(0));
                }
                return(EcmaValue.Number(Double.Parse(builder.ToString())));
            }

            builder.Append(".");
            i++;

            for (; i < input.Length; i++)
            {
                if (!EcmaChar.IsDigit(input[i]))
                {
                    return(EcmaValue.Number(Double.Parse(builder.ToString())));
                }
                builder.Append(input[i]);
            }

            return(EcmaValue.Number(Double.Parse(builder.ToString())));
        }