Explorar o código

make PHP's behavior on some invalid input to Std.parseFloat more consistent with other targets (fixed issue #823)

Simon Krajewski %!s(int64=13) %!d(string=hai) anos
pai
achega
bbcf119cc2
Modificáronse 2 ficheiros con 10 adicións e 2 borrados
  1. 7 1
      std/php/_std/Std.hx
  2. 3 1
      tests/unit/TestBasetypes.hx

+ 7 - 1
std/php/_std/Std.hx

@@ -47,7 +47,13 @@
 	}
 	}
 
 
 	public static function parseFloat( x : String ) : Float {
 	public static function parseFloat( x : String ) : Float {
-		return untyped __php__("is_numeric($x) ? floatval($x) : acos(1.01)");
+		var v : Float = untyped __call__("floatval", x);
+		untyped	if (v==0.0) {
+			x=untyped __call__("rtrim", x);
+			v=untyped __call__("floatval", x);
+			if (v == 0.0 && !__call__("is_numeric", x)) v = untyped __call__("acos", 1.01);
+		}
+		return v;
 	}
 	}
 
 
 	public static function random( x : Int ) : Int {
 	public static function random( x : Int ) : Int {

+ 3 - 1
tests/unit/TestBasetypes.hx

@@ -120,7 +120,9 @@ class TestBasetypes extends Test {
 		t( Math.isNaN(Std.parseFloat("abcd")) );
 		t( Math.isNaN(Std.parseFloat("abcd")) );
 		t( Math.isNaN(Std.parseFloat("a10")) );
 		t( Math.isNaN(Std.parseFloat("a10")) );
 		t( Math.isNaN(Std.parseFloat(null)) );
 		t( Math.isNaN(Std.parseFloat(null)) );
-
+		eq( Std.parseFloat("5.3 "), 5.3 );
+		eq( Std.parseFloat("0.0"), 0. );
+		eq( Std.parseFloat("5.3 1"), 5.3 );
 	}
 	}
 
 
 	function testStringTools() {
 	function testStringTools() {