Bladeren bron

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

Simon Krajewski 13 jaren geleden
bovenliggende
commit
bbcf119cc2
2 gewijzigde bestanden met toevoegingen van 10 en 2 verwijderingen
  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 {
-		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 {

+ 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("a10")) );
 		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() {