Browse Source

always return NaN on parseFloat failure

Nicolas Cannasse 15 years ago
parent
commit
5ac87fd8e1
3 changed files with 20 additions and 12 deletions
  1. 2 0
      doc/CHANGES.txt
  2. 13 10
      interp.ml
  3. 5 2
      std/neko/_std/Std.hx

+ 2 - 0
doc/CHANGES.txt

@@ -6,6 +6,8 @@
 	flash9 : fixed issue with @:bind
 	flash9 : added some missing errors
 	flash9 : fixed TypedDictionary.exists
+	all : added @:build for enums
+	neko : Std.parseFloat now returns NaN with invalid string
 
 2010-08-14: 2.06
 	neko : change serializer to be able to handle instances of basic classes from other modules

+ 13 - 10
interp.ml

@@ -152,23 +152,26 @@ let parse_int s =
 		| '0'..'9' | 'a'..'f' | 'A'..'F' -> loop_hex (i + 1)
 		| _ -> String.sub s 0 i
 	in
-	let rec loop i =
-		if i = String.length s then s else
+	let rec loop sp i =
+		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
-		| '0'..'9' | '-' -> loop (i + 1)
+		| '0'..'9' -> loop sp (i + 1)
+		| ' ' when sp = i -> loop (sp + 1) (i + 1)
+		| '-' when i = 0 -> loop sp (i + 1)
 		| 'x' when i = 1 && String.get s 0 = '0' -> loop_hex (i + 1)
-		| _ -> String.sub s 0 i
+		| _ -> String.sub s sp (i - sp)
 	in
-	int_of_string (loop 0)
+	int_of_string (loop 0 0)
 
 let parse_float s =
-	let rec loop i =
-		if i = String.length s then s else
+	let rec loop sp i =
+		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
-		| '0'..'9' | '-' | 'e' | 'E' | '.' -> loop (i + 1)
-		| _ -> String.sub s 0 i
+		| ' ' when sp = i -> loop (sp + 1) (i + 1)
+		| '0'..'9' | '-' | 'e' | 'E' | '.' -> loop sp (i + 1)
+		| _ -> String.sub s sp (i - sp)
 	in
-	float_of_string (loop 0)
+	float_of_string (loop 0 0)
 
 let find_sub str sub start =
 	let sublen = String.length sub in

+ 5 - 2
std/neko/_std/Std.hx

@@ -49,8 +49,11 @@
 		return __dollar__int(x.__s);
 	}
 
-	public static function parseFloat( x : String ) : Float {
-		return untyped __dollar__float(x.__s);
+	public static function parseFloat( x : String ) : Float untyped {
+		if( x == null ) return Math.NaN;
+		var t = __dollar__float(x.__s);
+		if( t == null ) t = Math.NaN;
+		return t;
 	}
 
 	public static function random( x : Int ) : Int {