Explorar o código

Merge pull request #4179 from georgkoester/feature/int64_parsing2

Add Int64.fromFloat and Int64.fromString.
Will rename fromString to parseString in the next commit.
Andy Li %!s(int64=9) %!d(string=hai) anos
pai
achega
4b0f25a374

+ 9 - 1
std/cpp/_std/haxe/Int64.hx

@@ -21,7 +21,7 @@
  */
 package haxe;
 
-
+import haxe.Int64Helper;
 
 @:notNull
 
@@ -120,6 +120,14 @@ abstract Int64( __Int64 ) from __Int64 to __Int64
 		return untyped __cpp__("String( ({0}).get() )", this);
 	}
 
+	public static function fromString( sParam : String ) : Int64 {
+		return Int64Helper.fromString( sParam );
+	}
+
+	public static function fromFloat( f : Float ) : Int64 {
+		return Int64Helper.fromFloat( f );
+	}
+
 	/**
 		Performs signed integer divison of `dividend` by `divisor`.
 		Returns `{ quotient : Int64, modulus : Int64 }`.

+ 10 - 0
std/cs/_std/haxe/Int64.hx

@@ -22,6 +22,8 @@
 package haxe;
 using haxe.Int64;
 
+import haxe.Int64Helper;
+
 private typedef __Int64 = cs.StdTypes.Int64;
 
 @:coreApi
@@ -93,6 +95,14 @@ abstract Int64(__Int64) from __Int64 to __Int64
 	private inline function toString() : String
 		return '$this';
 
+	public static function fromString( sParam : String ) : Int64 {
+		return Int64Helper.fromString( sParam );
+	}
+
+	public static function fromFloat( f : Float ) : Int64 {
+		return Int64Helper.fromFloat( f );
+	}
+
 	@:op(-A) public static function neg( x : Int64 ) : Int64
 		return -x.val;
 

+ 8 - 0
std/haxe/Int64.hx

@@ -150,6 +150,14 @@ abstract Int64(__Int64) from __Int64 to __Int64
 		return str;
 	}
 
+	public static inline function fromString( sParam : String ) : Int64 {
+		return Int64Helper.fromString( sParam );
+	}
+
+	public static inline function fromFloat( f : Float ) : Int64 {
+		return Int64Helper.fromFloat( f );
+	}
+
 	/**
 		Performs signed integer divison of `dividend` by `divisor`.
 		Returns `{ quotient : Int64, modulus : Int64 }`.

+ 101 - 0
std/haxe/Int64Helper.hx

@@ -0,0 +1,101 @@
+/*
+ * Copyright (C)2005-2015 Haxe Foundation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+package haxe;
+
+using haxe.Int64;
+
+import StringTools;
+
+class Int64Helper {
+
+	public static function fromString( sParam : String ) : Int64 {
+		var base = Int64.ofInt(10);
+		var current = Int64.ofInt(0);
+		var multiplier = Int64.ofInt(1);
+		var sIsNegative = false;
+
+		var s = StringTools.trim(sParam);
+		if (s.charAt(0) == "-") {
+			sIsNegative = true;
+			s = s.substring(1, s.length);
+		}
+		var len = s.length;
+
+		for (i in 0...len) {
+			var digitInt = s.charCodeAt(len - 1 - i) - '0'.code;
+
+			if (digitInt < 0 || digitInt > 9) {
+				throw "NumberFormatError";
+			}
+
+			var digit:Int64 = Int64.ofInt(digitInt);
+			if (sIsNegative) {
+				current = Int64.sub(current, Int64.mul(multiplier, digit));
+				if (!Int64.isNeg(current)) {
+					throw "NumberFormatError: Underflow";
+				}
+			} else {
+				current = Int64.add(current, Int64.mul(multiplier, digit));
+				if (Int64.isNeg(current)) {
+					throw "NumberFormatError: Overflow";
+				}
+			}
+			multiplier = Int64.mul(multiplier, base);
+		}
+		return current;
+	}
+
+	public static function fromFloat( f : Float ) : Int64 {
+		if (Math.isNaN(f) || !Math.isFinite(f)) {
+			throw "Number is NaN or Infinite";
+		}
+
+		var noFractions = f - (f % 1);
+
+		// 2^53-1 and -2^53: these are parseable without loss of precision
+		if (noFractions > 9007199254740991) {
+			throw "Conversion overflow";
+		}
+		if (noFractions < -9007199254740991) {
+			throw "Conversion underflow";
+		}
+
+		var result = Int64.ofInt(0);
+		var neg = noFractions < 0;
+		var rest = neg ? -noFractions : noFractions;
+
+		var i = 0;
+		while (rest >= 1) {
+			var curr = rest % 2;
+			rest = rest / 2;
+			if (curr >= 1) {
+				result = Int64.add(result, Int64.shl(Int64.ofInt(1), i));
+			}
+			i++;
+		}
+
+		if (neg) {
+			result = Int64.neg(result);
+		}
+		return result;
+	}
+}

+ 11 - 0
std/java/_std/haxe/Int64.hx

@@ -22,6 +22,8 @@
 package haxe;
 using haxe.Int64;
 
+import haxe.Int64Helper;
+
 private typedef __Int64 = java.StdTypes.Int64;
 
 @:coreApi
@@ -93,6 +95,15 @@ abstract Int64(__Int64) from __Int64 to __Int64
 	private inline function toString() : String
 		return '$this';
 
+	public static function fromString( sParam : String ) : Int64 {
+		// can this be done?: return new Int64( java.lang.Long.LongClass.parseLong( sParam ) );
+		return Int64Helper.fromString( sParam );
+	}
+
+	public static function fromFloat( f : Float ) : Int64 {
+		return Int64Helper.fromFloat( f );
+	}
+
 	@:op(-A) public static function neg( x : Int64 ) : Int64
 		return -x.val;
 

+ 100 - 0
tests/unit/src/unit/TestInt64.hx

@@ -451,4 +451,104 @@ class TestInt64 extends Test {
 			Test.success = false;
 		}
 	}
+
+	public function testFromString()
+	{
+		for (v in ["0", "1", "-1", "9223372036854775807", "-9223372036854775807"]) {
+			eq(Std.string(fromString(v)), v);
+		}
+
+		// trims the string:
+		eq("-23", Std.string(fromString("  -23 ")));
+
+
+		// overflow and underflow raise exceptions:
+		try
+		{
+			fromString("9223372036854775808");
+			f(true);
+		}
+		catch (e:Dynamic)
+		{
+			// fine
+		}
+
+		try
+		{
+			fromString("-9223372036854775809");
+			f(true);
+		}
+		catch (e:Dynamic)
+		{
+			// fine
+		}
+
+		try
+		{
+			fromString("--1");
+			f(true);
+		}
+		catch (e:Dynamic)
+		{
+			// fine
+		}
+
+		try
+		{
+			fromString("asd1");
+			f(true);
+		}
+		catch (e:Dynamic)
+		{
+			// fine
+		}
+
+		try
+		{
+			fromString("1asdf");
+			f(true);
+		}
+		catch (e:Dynamic)
+		{
+			// fine
+		}
+	}
+
+	public function testFromFloat()
+	{
+		for (v in [0.0, 1.0, -1.0, 9007199254740991, -9007199254740991]) {
+			eq(Std.parseFloat(Std.string(fromFloat(v))), v);
+		}
+
+		try
+		{
+			fromFloat(9007199254740992);
+			f(true);
+		}
+		catch (e:Dynamic)
+		{
+			// fine
+		}
+
+		try
+		{
+			fromFloat(-9007199254740992);
+			f(true);
+		}
+		catch (e:Dynamic)
+		{
+			// fine
+		}
+
+		var nan = Math.NaN;
+		try
+		{
+			fromFloat(nan);
+			f(true);
+		}
+		catch (e:Dynamic)
+		{
+			// fine
+		}
+	}
 }