소스 검색

fixed StringTools.hex with negative numbers

Nicolas Cannasse 15 년 전
부모
커밋
ad5d6b2095
3개의 변경된 파일10개의 추가작업 그리고 10개의 파일을 삭제
  1. 1 0
      doc/CHANGES.txt
  2. 4 10
      std/StringTools.hx
  3. 5 0
      tests/unit/TestBasetypes.hx

+ 1 - 0
doc/CHANGES.txt

@@ -24,6 +24,7 @@
 	all : only display errors with --display if no completion matched
 	all : some completion related errors fixed
 	flash9 : added @:bind support
+	all : fixed StringTools.hex with negative numbers
 
 2010-01-09: 2.05
 	js : added js.Scroll

+ 4 - 10
std/StringTools.hx

@@ -228,27 +228,21 @@ class StringTools {
 		Encode a number into a hexadecimal representation, with an optional number of zeros for left padding.
 	**/
 	public static function hex( n : Int, ?digits : Int ) {
-		var neg = false;
-		if( n < 0 ) {
-			neg = true;
-			n = -n;
-		}
-		#if (flash || js)
+		#if flash9
+			var n : UInt = n;
 			var s : String = untyped n.toString(16);
 			s = s.toUpperCase();
 		#else
 			var s = "";
 			var hexChars = "0123456789ABCDEF";
 			do {
-				s = hexChars.charAt(n%16) + s;
-				n = Std.int(n/16);
+				s = hexChars.charAt(n&15) + s;
+				n >>>= 4;
 			} while( n > 0 );
 		#end
 		if( digits != null )
 			while( s.length < digits )
 				s = "0"+s;
-		if( neg )
-			s = "-"+s;
 		return s;
 	}
 

+ 5 - 0
tests/unit/TestBasetypes.hx

@@ -86,4 +86,9 @@ class TestBasetypes extends Test {
 		eq( Std.parseInt(null), null );
 	}
 
+	function testStringTools() {
+		eq( StringTools.hex(0xABCDEF,7), "0ABCDEF" );
+		eq( StringTools.hex(-1,8), "FFFFFFFF" );
+	}
+
 }