Преглед изворни кода

Add test cases for issues #10544 & #10545 (#10546)

* Add test cases for issues #10544 & #10545

* [eval] ignore tabs in parseFloat

* ignore neko

* [lua] Std.parseFloat: ignore any leading whitespace chars

Co-authored-by: Simon Krajewski <[email protected]>
Co-authored-by: Aleksandr Kuzmenko <[email protected]>
zagortenej пре 3 година
родитељ
комит
13ee0754fa
3 измењених фајлова са 8 додато и 2 уклоњено
  1. 1 1
      src/core/numeric.ml
  2. 1 1
      std/lua/_std/Std.hx
  3. 6 0
      tests/unit/src/unitstd/Std.unit.hx

+ 1 - 1
src/core/numeric.ml

@@ -49,7 +49,7 @@ let parse_float s =
 	let rec loop sp i =
 	let rec loop sp i =
 		if i = String.length s then (if sp = 0 then s else String.sub s sp (i - sp)) else
 		if i = String.length s then (if sp = 0 then s else String.sub s sp (i - sp)) else
 		match String.unsafe_get s i with
 		match String.unsafe_get s i with
-		| ' ' when sp = i -> loop (sp + 1) (i + 1)
+		| ' ' | '\t' when sp = i -> loop (sp + 1) (i + 1)
 		| '0'..'9' | '-' | '+' | 'e' | 'E' | '.' -> loop sp (i + 1)
 		| '0'..'9' | '-' | '+' | 'e' | 'E' | '.' -> loop sp (i + 1)
 		| _ -> String.sub s sp (i - sp)
 		| _ -> String.sub s sp (i - sp)
 	in
 	in

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

@@ -79,7 +79,7 @@ import lua.NativeStringTools;
 	public static function parseFloat(x:String):Float {
 	public static function parseFloat(x:String):Float {
 		if (x == null || x == "")
 		if (x == null || x == "")
 			return Math.NaN;
 			return Math.NaN;
-		var digitMatch = NativeStringTools.match(x, "^ *[%.%-+]?[0-9]%d*");
+		var digitMatch = NativeStringTools.match(x, "^%s*[%.%-+]?[0-9]%d*");
 		if (digitMatch == null) {
 		if (digitMatch == null) {
 			return Math.NaN;
 			return Math.NaN;
 		}
 		}

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

@@ -98,6 +98,11 @@ Std.parseInt('  	-0x10') == -16;
 #end
 #end
 #end
 #end
 
 
+#if !neko
+Std.parseInt("+123") == 123;
+Std.parseInt("-0xa0") == -160;
+#end
+
 // parseFloat
 // parseFloat
 Std.parseFloat("0") == 0.;
 Std.parseFloat("0") == 0.;
 Std.parseFloat("   5.3") == 5.3;
 Std.parseFloat("   5.3") == 5.3;
@@ -118,6 +123,7 @@ Std.parseFloat("2.426670815e-12") == 2.426670815e-12;
 Std.parseFloat("2.426670815E-12") == 2.426670815e-12;
 Std.parseFloat("2.426670815E-12") == 2.426670815e-12;
 // Std.parseInt("0x C") == 0;
 // Std.parseInt("0x C") == 0;
 // Std.parseInt("0x+A") == 0;
 // Std.parseInt("0x+A") == 0;
+Std.parseFloat("    \t42.2") == 42.2;
 
 
 // random
 // random
 var x = Std.random(2);
 var x = Std.random(2);