Browse Source

fixed parseInt("010") issue

Nicolas Cannasse 14 years ago
parent
commit
6b473f0f29
2 changed files with 8 additions and 2 deletions
  1. 5 2
      std/js/_std/Std.hx
  2. 3 0
      tests/unit/TestBasetypes.hx

+ 5 - 2
std/js/_std/Std.hx

@@ -39,8 +39,11 @@
 	}
 
 	public static function parseInt( x : String ) : Null<Int> {
-		var v = untyped __js__("parseInt")(x);
-		if( Math.isNaN(v) )
+		var v = untyped __js__("parseInt")(x, 10);
+		// parse again if hexadecimal
+		if( v == 0 && x.charCodeAt(1) == 'x'.code )
+			v = untyped __js__("parseInt")(x);
+		if( untyped __js__("isNaN")(v) )
 			return null;
 		return cast v;
 	}

+ 3 - 0
tests/unit/TestBasetypes.hx

@@ -91,6 +91,7 @@ class TestBasetypes extends Test {
 		eq( Std.parseInt("0"), 0 );
 		eq( Std.parseInt("   5"), 5 );
 		eq( Std.parseInt("0001"), 1 );
+		eq( Std.parseInt("0010"), 10 );
 		eq( Std.parseInt("100"), 100 );
 		eq( Std.parseInt("-100"), -100 );
 		eq( Std.parseInt("100x123"), 100 );
@@ -98,6 +99,8 @@ class TestBasetypes extends Test {
 		eq( Std.parseInt("abcd"), null );
 		eq( Std.parseInt("a10"), null );
 		eq( Std.parseInt(null), null );
+		eq( Std.parseInt("0xFF"), 255 );
+		unspec(function() Std.parseInt("0xFG"));
 
 		eq( Std.parseFloat("0"), 0. );
 		eq( Std.parseFloat("   5.3"), 5.3 );