Browse Source

[js] fix Std.parseInt() for haxedecimal with leading whitespaces
closes #8970

Aleksandr Kuzmenko 5 years ago
parent
commit
9d35e809a6
2 changed files with 18 additions and 4 deletions
  1. 11 4
      std/js/_std/Std.hx
  2. 7 0
      tests/unit/src/unit/issues/Issue8970.hx

+ 11 - 4
std/js/_std/Std.hx

@@ -21,6 +21,7 @@
  */
 
 import js.Boot;
+import js.Syntax;
 
 @:keepInit
 @:coreApi class Std {
@@ -48,10 +49,16 @@ import js.Boot;
 
 	@:pure
 	public static function parseInt(x:String):Null<Int> {
-		var v = untyped __js__('parseInt({0}, {0} && {0}[0]=="0" && ({0}[1]=="x" || {0}[1]=="X") ? 16 : 10)', x);
-		if (untyped __js__("isNaN")(v))
-			return null;
-		return cast v;
+		if(x != null) {
+			for(i in 0...x.length) {
+				var c = StringTools.fastCodeAt(x, i);
+				if(c <= 8 || (c >= 14 && c != ' '.code && c != '-'.code)) {
+					var v:Int = Syntax.code('parseInt({0}, ({0}[{1}]=="x" || {0}[{1}]=="X") ? 16 : 10)', x, i + 1);
+					return Math.isNaN(v) ? null : v;
+				}
+			}
+		}
+		return null;
 	}
 
 	public static inline function parseFloat(x:String):Float {

+ 7 - 0
tests/unit/src/unit/issues/Issue8970.hx

@@ -0,0 +1,7 @@
+package unit.issues;
+
+class Issue8970 extends unit.Test {
+	function test() {
+		eq(-16, Std.parseInt('	  -0x10'));
+	}
+}