// todo private JsValue ParseBoolean(JsValue thisObject, JsValue[] arguments) { var inputString = TypeConverter.ToString(arguments.At(0)); var trimmedString = StringPrototype.TrimStartEx(inputString); if (trimmedString == "true") { return(true); } if (trimmedString == "false") { return(false); } throw new JavaScriptException(Engine.RangeError, string.Format("Invalid string {0}, Convert to boolean faild!", trimmedString)); }
/// <summary> /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.1.2.3 /// </summary> public static JsValue ParseFloat(JsValue thisObject, JsValue[] arguments) { var inputString = TypeConverter.ToString(arguments.At(0)); var trimmedString = StringPrototype.TrimStartEx(inputString); var sign = 1; if (trimmedString.Length > 0) { if (trimmedString[0] == '-') { sign = -1; trimmedString = trimmedString.Substring(1); } else if (trimmedString[0] == '+') { trimmedString = trimmedString.Substring(1); } } if (trimmedString.StartsWith("Infinity")) { return(sign * double.PositiveInfinity); } if (trimmedString.StartsWith("NaN")) { return(double.NaN); } var separator = (char)0; bool isNan = true; decimal number = 0; var i = 0; for (; i < trimmedString.Length; i++) { var c = trimmedString[i]; if (c == '.') { i++; separator = '.'; break; } if (c == 'e' || c == 'E') { i++; separator = 'e'; break; } var digit = c - '0'; if (digit >= 0 && digit <= 9) { isNan = false; number = number * 10 + digit; } else { break; } } decimal pow = 0.1m; if (separator == '.') { for (; i < trimmedString.Length; i++) { var c = trimmedString[i]; var digit = c - '0'; if (digit >= 0 && digit <= 9) { isNan = false; number += digit * pow; pow *= 0.1m; } else if (c == 'e' || c == 'E') { i++; separator = 'e'; break; } else { break; } } } var exp = 0; var expSign = 1; if (separator == 'e') { if (i < trimmedString.Length) { if (trimmedString[i] == '-') { expSign = -1; i++; } else if (trimmedString[i] == '+') { i++; } } for (; i < trimmedString.Length; i++) { var c = trimmedString[i]; var digit = c - '0'; if (digit >= 0 && digit <= 9) { exp = exp * 10 + digit; } else { break; } } } if (isNan) { return(double.NaN); } for (var k = 1; k <= exp; k++) { if (expSign > 0) { number *= 10; } else { number /= 10; } } return((double)(sign * number)); }