Kaynağa Gözat

[cs] Completed Std.hx and Java bugfix for parseFloat

Caue Waneck 13 yıl önce
ebeveyn
işleme
94f41fb2a8
2 değiştirilmiş dosya ile 139 ekleme ve 20 silme
  1. 136 19
      std/cs/_std/Std.hx
  2. 3 1
      std/java/_std/Std.hx

+ 136 - 19
std/cs/_std/Std.hx

@@ -38,7 +38,7 @@ import haxe.lang.Exceptions;
 		return native.IsAssignableFrom(Lib.getNativeType(v));
 	}
 
-	public static function string( s : Dynamic ) : String {
+	public static inline function string( s : Dynamic ) : String {
 		return s + "";
 	}
 
@@ -46,32 +46,149 @@ import haxe.lang.Exceptions;
 		return cast x;
 	}
 	
-	@:functionBody('
-			try 
+	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;
+		if (StringTools.startsWith(x, "0x"))
+		{
+			i = 1;
+			base = 16;
+		}
+		
+		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) 
 			{
-				return new haxe.lang.Null<int>(System.Int32.Parse(x), true);
-			} 
-			catch (System.FormatException fe)
+				isNeg = true;
+				continue;
+			}
+			
+			if (c >= '0'.code && c <= '9'.code)
 			{
-				return default(haxe.lang.Null<int>);
+				if (!foundAny && c == '0'.code)
+				{
+					hasValue = true;
+					continue;
+				}
+				ret *= base; foundAny = true;
+				
+				ret += c - '0'.code;
+			} else if (base == 16) {
+				if (c >= 'a'.code && c <= 'f'.code) {
+					ret *= base; foundAny = true;
+					ret += c - 'a'.code + 10;
+				} else if (c >= 'A'.code && c <= 'F'.code) {
+					ret *= base; foundAny = true;
+					ret += c - 'A'.code + 10;
+				} else {
+					break;
+				}
+			} else {
+				break;
 			}
-	')
-	public static function parseInt( x : String ) : Null<Int> {
-		return null;
+		}
+		
+		if (foundAny || hasValue)
+			return isNeg ? -ret : ret;
+		else
+			return null;
 	}
 
-	@:functionBody('
-			try 
+	public static function parseFloat( x : String ) : Float {
+		if (x == null) return Math.NaN;
+		x = StringTools.ltrim(x);
+		
+		var ret = 0.0;
+		var div = 0.0;
+		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)
 			{
-				return System.Double.Parse(x);
-			} 
-			catch (System.FormatException fe)
+				isNeg = true;
+				continue;
+			}
+			
+			if (c == '.'.code)
 			{
-				return double.NaN;
+				if (div != 0.0)
+					break;
+				div = 1.0;
+				
+				continue;
 			}
-	')
-	public static function parseFloat( x : String ) : Float {
-		return 0.0;
+			
+			if (c >= '0'.code && c <= '9'.code)
+			{
+				if (!foundAny && c == '0'.code)
+				{
+					hasValue = true;
+					continue;
+				}
+				
+				ret *= 10; foundAny = true; div *= 10;
+				
+				ret += c - '0'.code;
+			} 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)
+				{
+					eNeg = true;
+					i++;
+				}
+				
+				while (++i < len)
+				{
+					c = untyped cast(x[i], Int);
+					if (c >= '0'.code && c <= '9'.code)
+					{
+						if (!eFoundAny && c == '0'.code)
+							continue;
+						eFoundAny = true;
+						e *= 10;
+						e += c - '0'.code;
+					} else {
+						break;
+					}
+				}
+				
+				if (eNeg) e = -e;
+			} else {
+				break;
+			}
+		}
+		
+		if (div == 0.0) div = 1.0;
+		
+		if (foundAny || hasValue)
+		{
+			ret = isNeg ? -(ret / div) : (ret / div);
+			if (e != 0.0)
+			{
+				return ret * Math.pow(10.0, e);
+			} else {
+				return ret;
+			}
+		} else {
+			return Math.NaN;
+		}
 	}
 
 	public static function random( x : Int ) : Int {

+ 3 - 1
std/java/_std/Std.hx

@@ -156,6 +156,7 @@ import haxe.lang.Exceptions;
 				ret += ((int) (c - \'0\'));
 			} else if (foundAny && c == \'E\' || c == \'e\') {
 				boolean eNeg = false;
+				boolean eFoundAny = false;
 				if (i + 1 < len && x.charAt(i + 1) == \'-\')
 				{
 					eNeg = true;
@@ -167,8 +168,9 @@ import haxe.lang.Exceptions;
 					c = x.charAt(i);
 					if (c >= \'0\' && c <= \'9\')
 					{
-						if (!foundAny && c == \'0\')
+						if (!eFoundAny && c == \'0\')
 							continue;
+						eFoundAny = true;
 						e *= 10.0;
 						e += ((int) (c - \'0\'));
 					} else {