Пример #1
0
        /// <summary>
        /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.1.2.2
        /// </summary>
        public static JsValue ParseInt(JsValue thisObject, JsValue[] arguments)
        {
            string inputString = TypeConverter.ToString(arguments.At(0));
            var    s           = StringPrototype.TrimEx(inputString);

            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 = arguments.Length > 1 ? TypeConverter.ToInt32(arguments[1]) : 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(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(sign * Parse(s, radix).AsNumber());
            }
            catch
            {
                return(double.NaN);
            }
        }
Пример #2
0
        private string[] ParseArgumentNames(string parameterDeclaration)
        {
            string[] values = parameterDeclaration.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

            var newValues = new string[values.Length];

            for (var i = 0; i < values.Length; i++)
            {
                newValues[i] = StringPrototype.TrimEx(values[i]);
            }
            return(newValues);
        }
Пример #3
0
        /// <summary>
        /// http://www.ecma-international.org/ecma-262/5.1/#sec-9.3
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static double ToNumber(JsValue o)
        {
            // check number first as this is what is usually expected
            if (o.IsNumber())
            {
                return(o.AsNumber());
            }

            if (o.IsObject())
            {
                var p = o.AsObject() as IPrimitiveInstance;
                if (p != null)
                {
                    o = p.PrimitiveValue;
                }
            }

            if (o == Undefined.Instance)
            {
                return(double.NaN);
            }

            if (o == Null.Instance)
            {
                return(0);
            }

            if (o.IsBoolean())
            {
                return(o.AsBoolean() ? 1 : 0);
            }

            if (o.IsString())
            {
                var s = StringPrototype.TrimEx(o.AsString());

                if (String.IsNullOrEmpty(s))
                {
                    return(0);
                }

                if ("+Infinity".Equals(s) || "Infinity".Equals(s))
                {
                    return(double.PositiveInfinity);
                }

                if ("-Infinity".Equals(s))
                {
                    return(double.NegativeInfinity);
                }

                // todo: use a common implementation with JavascriptParser
                try
                {
                    if (!s.StartsWith("0x", StringComparison.OrdinalIgnoreCase))
                    {
                        var start = s[0];
                        if (start != '+' && start != '-' && start != '.' && !char.IsDigit(start))
                        {
                            return(double.NaN);
                        }

                        double n = Double.Parse(s,
                                                NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign |
                                                NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite |
                                                NumberStyles.AllowExponent, CultureInfo.InvariantCulture);
                        if (s.StartsWith("-") && n.Equals(0))
                        {
                            return(-0.0);
                        }

                        return(n);
                    }

                    int i = int.Parse(s.Substring(2), NumberStyles.HexNumber, CultureInfo.InvariantCulture);

                    return(i);
                }
                catch (OverflowException)
                {
                    return(s.StartsWith("-") ? double.NegativeInfinity : double.PositiveInfinity);
                }
                catch
                {
                    return(double.NaN);
                }
            }

            return(ToNumber(ToPrimitive(o, Types.Number)));
        }
Пример #4
0
        private static double ToNumber(string input)
        {
            // eager checks to save time and trimming
            if (string.IsNullOrEmpty(input))
            {
                return(0);
            }

            char first = input[0];

            if (input.Length == 1 && first >= '0' && first <= '9')
            {
                // simple constant number
                return(first - '0');
            }

            var s = StringPrototype.TrimEx(input);

            if (s.Length == 0)
            {
                return(0);
            }

            if (s.Length == 8 || s.Length == 9)
            {
                if ("+Infinity" == s || "Infinity" == s)
                {
                    return(double.PositiveInfinity);
                }

                if ("-Infinity" == s)
                {
                    return(double.NegativeInfinity);
                }
            }

            // todo: use a common implementation with JavascriptParser
            try
            {
                if (s.Length > 2 && s[0] == '0' && char.IsLetter(s[1]))
                {
                    int fromBase = 0;
                    if (s[1] == 'x' || s[1] == 'X')
                    {
                        fromBase = 16;
                    }

                    if (s[1] == 'o' || s[1] == 'O')
                    {
                        fromBase = 8;
                    }

                    if (s[1] == 'b' || s[1] == 'B')
                    {
                        fromBase = 2;
                    }

                    if (fromBase > 0)
                    {
                        return(Convert.ToInt32(s.Substring(2), fromBase));
                    }
                }

                var start = s[0];
                if (start != '+' && start != '-' && start != '.' && !char.IsDigit(start))
                {
                    return(double.NaN);
                }

                double n = double.Parse(s,
                                        NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign |
                                        NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite |
                                        NumberStyles.AllowExponent, CultureInfo.InvariantCulture);
                if (s.StartsWith("-") && n == 0)
                {
                    return(-0.0);
                }

                return(n);
            }
            catch (OverflowException)
            {
                return(s.StartsWith("-") ? double.NegativeInfinity : double.PositiveInfinity);
            }
            catch
            {
                return(double.NaN);
            }
        }
Пример #5
0
        private static double ToNumber(string input)
        {
            // eager checks to save time and trimming
            if (string.IsNullOrEmpty(input))
            {
                return(0);
            }

            var s = StringPrototype.IsWhiteSpaceEx(input[0]) || StringPrototype.IsWhiteSpaceEx(input[input.Length - 1])
                ? StringPrototype.TrimEx(input)
                : input;

            if (s.Length == 0)
            {
                return(0);
            }

            if (s.Length == 8 || s.Length == 9)
            {
                if ("+Infinity" == s || "Infinity" == s)
                {
                    return(double.PositiveInfinity);
                }

                if ("-Infinity" == s)
                {
                    return(double.NegativeInfinity);
                }
            }

            // todo: use a common implementation with JavascriptParser
            try
            {
                if (!s.StartsWith("0x", StringComparison.OrdinalIgnoreCase))
                {
                    var start = s[0];
                    if (start != '+' && start != '-' && start != '.' && !char.IsDigit(start))
                    {
                        return(double.NaN);
                    }

                    double n = double.Parse(s,
                                            NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign |
                                            NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite |
                                            NumberStyles.AllowExponent, CultureInfo.InvariantCulture);
                    if (s.StartsWith("-") && n == 0)
                    {
                        return(-0.0);
                    }

                    return(n);
                }

                int i = int.Parse(s.Substring(2), NumberStyles.HexNumber, CultureInfo.InvariantCulture);

                return(i);
            }
            // TODO
            //    catch (OverflowException)
            //  {
            //    return s.StartsWith("-") ? double.NegativeInfinity : double.PositiveInfinity;
            // }
            catch
            {
                return(double.NaN);
            }
        }