Browse Source

Merge pull request #4054 from jonasmalacofilho/interp-base-code

Better support for BaseCode on the interpreter
Dan Korostelev 10 years ago
parent
commit
19a1b13915
2 changed files with 10 additions and 4 deletions
  1. 4 4
      std/haxe/crypto/BaseCode.hx
  2. 6 0
      tests/unit/src/unit/TestMisc.hx

+ 4 - 4
std/haxe/crypto/BaseCode.hx

@@ -42,7 +42,7 @@ class BaseCode {
 	}
 
 	public function encodeBytes( b : haxe.io.Bytes ) : haxe.io.Bytes {
-		#if neko
+		#if (neko && !interp)
 		return haxe.io.Bytes.ofData( base_encode(b.getData(),base.getData()) );
 		#else
 		var nbits = this.nbits;
@@ -79,7 +79,7 @@ class BaseCode {
 	}
 
 	public function decodeBytes( b : haxe.io.Bytes ) : haxe.io.Bytes {
-		#if neko
+		#if (neko && !interp)
 		return haxe.io.Bytes.ofData( base_decode(b.getData(),base.getData()) );
 		#else
 		var nbits = this.nbits;
@@ -109,7 +109,7 @@ class BaseCode {
 	}
 
 	public function encodeString( s : String ) {
-		#if neko
+		#if (neko && !interp)
 		return neko.NativeString.toString( base_encode(neko.NativeString.ofString(s),base.getData()) );
 		#else
 		return encodeBytes(haxe.io.Bytes.ofString(s)).toString();
@@ -117,7 +117,7 @@ class BaseCode {
 	}
 
 	public function decodeString( s : String ) {
-		#if neko
+		#if (neko && !interp)
 		return neko.NativeString.toString( base_decode(neko.NativeString.ofString(s),base.getData()) );
 		#else
 		return decodeBytes(haxe.io.Bytes.ofString(s)).toString();

+ 6 - 0
tests/unit/src/unit/TestMisc.hx

@@ -309,9 +309,15 @@ class TestMisc extends Test {
 	}
 
 	function testBaseCode() {
+		// alternative base64
 		var b = new haxe.crypto.BaseCode(haxe.io.Bytes.ofString("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-"));
 		eq( b.encodeString("Héllow"), "iceFr6NLtM" );
 		eq( b.decodeString("iceFr6NLtM"), "Héllow" );
+
+		// base32-hex
+		var b = new haxe.crypto.BaseCode(haxe.io.Bytes.ofString("0123456789ABCDEFGHIJKLMNOPQRSTUV"));
+		eq( b.encodeString("foo"), "CPNMU" );
+		eq( b.decodeString("CPNMU"), "foo" );
 	}
 
 	function testUrlEncode() {