Explorar el Código

[java/cs] Fixed issue with parseFloat and decimals. Fixes Issue #876

Caue Waneck hace 13 años
padre
commit
6dd9a558f8
Se han modificado 2 ficheros con 77 adiciones y 32 borrados
  1. 35 16
      std/cs/_std/Std.hx
  2. 42 16
      std/java/_std/Std.hx

+ 35 - 16
std/cs/_std/Std.hx

@@ -49,7 +49,6 @@ import haxe.lang.Exceptions;
 	public static function parseInt( x : String ) : Null<Int> {
 		if (x == null) return null;
 		
-		x = StringTools.ltrim(x);
 		var ret = 0;
 		var base = 10;
 		var i = -1;
@@ -62,21 +61,28 @@ import haxe.lang.Exceptions;
 		var len = x.length;
 		var foundAny = false;
 		var isNeg = false;
-		var hasValue = false;
 		while (++i < len)
 		{
 			var c = cast(untyped x[i], Int); //fastCodeAt
-			if (!foundAny && c == '-'.code) 
+			if (!foundAny) 
 			{
-				isNeg = true;
-				continue;
+				switch(c)
+				{
+					case '-'.code:
+						isNeg = true;
+						continue;
+					case ' '.code, '\t'.code, '\n'.code, '\r'.code:
+						if (isNeg)
+							return null;
+						continue;
+				}
 			}
 			
 			if (c >= '0'.code && c <= '9'.code)
 			{
 				if (!foundAny && c == '0'.code)
 				{
-					hasValue = true;
+					foundAny = true;
 					continue;
 				}
 				ret *= base; foundAny = true;
@@ -97,7 +103,7 @@ import haxe.lang.Exceptions;
 			}
 		}
 		
-		if (foundAny || hasValue)
+		if (foundAny)
 			return isNeg ? -ret : ret;
 		else
 			return null;
@@ -112,17 +118,24 @@ import haxe.lang.Exceptions;
 		var e = 0.0;
 		
 		var len = x.length;
-		var hasValue = false;
 		var foundAny = false;
 		var isNeg = false;
 		var i = -1;
 		while (++i < len)
 		{
 			var c = cast(untyped x[i], Int); //fastCodeAt
-			if (!foundAny && c == '-'.code)
+			if (!foundAny) 
 			{
-				isNeg = true;
-				continue;
+				switch(c)
+				{
+					case '-'.code:
+						isNeg = true;
+						continue;
+					case ' '.code, '\t'.code, '\n'.code, '\r'.code:
+						if (isNeg)
+							return Math.NaN;
+						continue;
+				}
 			}
 			
 			if (c == '.'.code)
@@ -138,7 +151,7 @@ import haxe.lang.Exceptions;
 			{
 				if (!foundAny && c == '0'.code)
 				{
-					hasValue = true;
+					foundAny = true;
 					continue;
 				}
 				
@@ -148,10 +161,16 @@ import haxe.lang.Exceptions;
 			} else if (foundAny && (c == 'e'.code || c == 'E'.code)) {
 				var eNeg = false;
 				var eFoundAny = false;
-				if (i + 1 < len && untyped cast(x[i + 1], Int) == '-'.code)
+				if (i + 1 < len)
 				{
-					eNeg = true;
-					i++;
+					var next = untyped cast(x[i + 1], Int);
+					if (next == '-'.code)
+					{
+						eNeg = true;
+						i++;
+					} else if (next == '+'.code) {
+						i++;
+					}
 				}
 				
 				while (++i < len)
@@ -177,7 +196,7 @@ import haxe.lang.Exceptions;
 		
 		if (div == 0.0) div = 1.0;
 		
-		if (foundAny || hasValue)
+		if (foundAny)
 		{
 			ret = isNeg ? -(ret / div) : (ret / div);
 			if (e != 0.0)

+ 42 - 16
std/java/_std/Std.hx

@@ -60,7 +60,6 @@ import haxe.lang.Exceptions;
 	@:functionBody('
 		if (x == null) return null;
 		
-		x = x.trim();
 		int ret = 0;
 		int base = 10;
 		
@@ -73,20 +72,30 @@ import haxe.lang.Exceptions;
 		int len = x.length();
 		boolean foundAny = false;
 		boolean isNeg = false;
-		boolean hasValue = false;
 		for (int i = 0; i < len; i++)
 		{
 			char c = x.charAt(i);
-			if (!foundAny && c == \'-\') {
-				isNeg = true;
-				continue;
+			if (!foundAny) 
+			{
+				switch(c)
+				{
+					case \'-\':
+						isNeg = true;
+						continue;
+					case \'\\n\': 
+					case \'\\t\': 
+					case \'\\r\': 
+					case \' \': 
+						if (isNeg) return null;
+						continue;
+				}
 			}
 			
 			if (c >= \'0\' && c <= \'9\')
 			{
 				if (!foundAny && c == \'0\')
 				{
-					hasValue = true;
+					foundAny = true;
 					continue;
 				}
 				ret *= base; foundAny = true;
@@ -107,7 +116,7 @@ import haxe.lang.Exceptions;
 			}
 		}
 		
-		if (foundAny || hasValue)
+		if (foundAny)
 			return isNeg ? -ret : ret;
 		else
 			return null;
@@ -125,15 +134,25 @@ import haxe.lang.Exceptions;
 		double e = 0.0;
 		
 		int len = x.length();
-		boolean hasValue = false;
 		boolean foundAny = false;
 		boolean isNeg = false;
 		for (int i = 0; i < len; i++)
 		{
 			char c = x.charAt(i);
-			if (!foundAny && c == \'-\') {
-				isNeg = true;
-				continue;
+			if (!foundAny) 
+			{
+				switch(c)
+				{
+					case \'-\':
+						isNeg = true;
+						continue;
+					case \'\\n\': 
+					case \'\\t\': 
+					case \'\\r\': 
+					case \' \': 
+					if (isNeg) return java.lang.Double.NaN;
+						continue;
+				}
 			}
 			
 			if (c == \'.\') {
@@ -148,7 +167,7 @@ import haxe.lang.Exceptions;
 			{
 				if (!foundAny && c == \'0\')
 				{
-					hasValue = true;
+					foundAny = true;
 					continue;
 				}
 				ret *= 10.0; foundAny = true; div *= 10.0;
@@ -157,10 +176,17 @@ import haxe.lang.Exceptions;
 			} else if (foundAny && c == \'E\' || c == \'e\') {
 				boolean eNeg = false;
 				boolean eFoundAny = false;
-				if (i + 1 < len && x.charAt(i + 1) == \'-\')
+				
+				char next = x.charAt(i + 1);
+				if (i + 1 < len)
 				{
-					eNeg = true;
-					i++;
+					if (next == \'-\')
+					{
+						eNeg = true;
+						i++;
+					} else if (next == \'+\') {
+						i++;
+					}
 				}
 				
 				while (++i < len)
@@ -186,7 +212,7 @@ import haxe.lang.Exceptions;
 		
 		if (div == 0.0) div = 1.0;
 		
-		if (foundAny || hasValue)
+		if (foundAny)
 		{
 			ret = isNeg ? -(ret / div) : (ret / div);
 			if (e != 0.0)