Переглянути джерело

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

Aleksandr Kuzmenko 2 роки тому
батько
коміт
b2799128d8
4 змінених файлів з 12 додано та 4 видалено
  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.
 
 		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` 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>;
 
+	/**
+		@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
 	**/

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

@@ -61,6 +61,12 @@ import php.Syntax;
 				return null;
 			return val;
 		}
+
+		switch Global.stripos(x, 'e') {
+			case false:
+			case ePos: x = Global.substr(x, 0, ePos);
+		}
+
 		final val = Global.intval(x, 10);
 		// 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

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

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