Browse Source

allow parsing of 0X integers (fixed issue #989)

Simon Krajewski 13 years ago
parent
commit
ba5b4af643
4 changed files with 5 additions and 3 deletions
  1. 1 1
      interp.ml
  2. 1 1
      std/flash8/_std/Std.hx
  3. 1 1
      std/js/_std/Std.hx
  4. 2 0
      tests/unit/TestBasetypes.hx

+ 1 - 1
interp.ml

@@ -278,7 +278,7 @@ let parse_int s =
 		| '0'..'9' -> loop sp (i + 1)
 		| ' ' when sp = i -> loop (sp + 1) (i + 1)
 		| '-' when i = 0 -> loop sp (i + 1)
-		| 'x' when i = 1 && String.get s 0 = '0' -> loop_hex (i + 1)
+		| ('x' | 'X') when i = 1 && String.get s 0 = '0' -> loop_hex (i + 1)
 		| _ -> String.sub s sp (i - sp)
 	in
 	int_of_string (loop 0 0)

+ 1 - 1
std/flash8/_std/Std.hx

@@ -41,7 +41,7 @@
 
 	public static function parseInt( x : String ) : Null<Int> untyped {
 		var v;
-		if( x.charCodeAt(1) == 'x'.code )
+		if( x.charCodeAt(1) == 'x'.code || x.charCodeAt(1) == 'X'.code)
 			v = _global["parseInt"](x);
 		else
 			v = _global["parseInt"](x, 10);

+ 1 - 1
std/js/_std/Std.hx

@@ -41,7 +41,7 @@ import js.Boot;
 	public static function parseInt( x : String ) : Null<Int> {
 		var v = untyped __js__("parseInt")(x, 10);
 		// parse again if hexadecimal
-		if( v == 0 && x.charCodeAt(1) == 'x'.code )
+		if( v == 0 && (x.charCodeAt(1) == 'x'.code || x.charCodeAt(1) == 'X'.code) )
 			v = untyped __js__("parseInt")(x);
 		if( untyped __js__("isNaN")(v) )
 			return null;

+ 2 - 0
tests/unit/TestBasetypes.hx

@@ -178,6 +178,8 @@ class TestBasetypes extends Test {
 		eq( Std.parseInt(null), null );
 		eq( Std.parseInt("0xFF"), 255 );
 		eq( Std.parseInt("0x123"), 291 );
+		eq( Std.parseInt("0XFF"), 255 );
+		eq( Std.parseInt("0X123"), 291 );		
 		unspec(function() Std.parseInt("0xFG"));
 
 		eq( Std.parseFloat("0"), 0. );