Browse Source

[php] Std.parseInt: ignore scientific notation (fixes #10617)

Aleksandr Kuzmenko 2 years ago
parent
commit
b2799128d8
4 changed files with 12 additions and 4 deletions
  1. 1 2
      std/Std.hx
  2. 5 0
      std/php/Global.hx
  3. 6 0
      std/php/_std/Std.hx
  4. 0 2
      tests/unit/src/unitstd/Std.unit.hx

+ 1 - 2
std/Std.hx

@@ -106,8 +106,7 @@ extern class Std {
 		notations are not supported.
 		notations are not supported.
 
 
 		Parsing continues until an invalid character is detected, in which case the result up to
 		Parsing continues until an invalid character is detected, in which case the result up to
-		that point is returned. However, in decimal mode, the effect of `x` containing an e or E
-		is unspecified, and it may be taken as scientific exponential notation.
+		that point is returned. Scientific notation is not supported. That is `Std.parseInt('10e2')` produces `10`.
 
 
 		If `x` is `null`, the result is `null`.
 		If `x` is `null`, the result is `null`.
 		If `x` cannot be parsed as integer or is empty, the result is `null`.
 		If `x` cannot be parsed as integer or is empty, the result is `null`.

+ 5 - 0
std/php/Global.hx

@@ -370,6 +370,11 @@ extern class Global {
 	**/
 	**/
 	static function strpos(haystack:String, needle:String, offset:Int = 0):EitherType<Bool, Int>;
 	static function strpos(haystack:String, needle:String, offset:Int = 0):EitherType<Bool, Int>;
 
 
+	/**
+		@see http://php.net/manual/en/function.stripos.php
+	**/
+	static function stripos(haystack:String, needle:String, offset:Int = 0):EitherType<Bool, Int>;
+
 	/**
 	/**
 		@see http://php.net/manual/en/function.strrpos.php
 		@see http://php.net/manual/en/function.strrpos.php
 	**/
 	**/

+ 6 - 0
std/php/_std/Std.hx

@@ -61,6 +61,12 @@ import php.Syntax;
 				return null;
 				return null;
 			return val;
 			return val;
 		}
 		}
+
+		switch Global.stripos(x, 'e') {
+			case false:
+			case ePos: x = Global.substr(x, 0, ePos);
+		}
+
 		final val = Global.intval(x, 10);
 		final val = Global.intval(x, 10);
 		// if the value was 0, make sure it wasn't because the string had no valid digits
 		// if the value was 0, make sure it wasn't because the string had no valid digits
 		// last check ensures there is only a maximum of one + or - sign
 		// last check ensures there is only a maximum of one + or - sign

+ 0 - 2
tests/unit/src/unitstd/Std.unit.hx

@@ -81,9 +81,7 @@ Std.parseInt("0010") == 10;
 // trailing text
 // trailing text
 Std.parseInt("100x123") == 100;
 Std.parseInt("100x123") == 100;
 Std.parseInt("12foo13") == 12;
 Std.parseInt("12foo13") == 12;
-#if !php // https://github.com/HaxeFoundation/haxe/issues/10617
 Std.parseInt("23e2") == 23;
 Std.parseInt("23e2") == 23;
-#end
 Std.parseInt("0x10z") == 16;
 Std.parseInt("0x10z") == 16;
 Std.parseInt("0x10x123") == 16;
 Std.parseInt("0x10x123") == 16;
 Std.parseInt("0xff\n") == 255;
 Std.parseInt("0xff\n") == 255;