Browse Source

more work on bytes

Nicolas Cannasse 10 years ago
parent
commit
f2048bbc5a

+ 24 - 9
std/haxe/io/Bytes.hx

@@ -266,8 +266,8 @@ class Bytes {
 		untyped __global__.__hxcpp_memory_set_double(b,pos,v);
 		untyped __global__.__hxcpp_memory_set_double(b,pos,v);
 		#else
 		#else
 		var i = FPHelper.doubleToI64(v);
 		var i = FPHelper.doubleToI64(v);
-		setI32(pos, haxe.Int64.getLow(i));
-		setI32(pos + 4, haxe.Int64.getHigh(i));
+		setInt32(pos, haxe.Int64.getLow(i));
+		setInt32(pos + 4, haxe.Int64.getHigh(i));
 		#end
 		#end
 	}
 	}
 
 
@@ -288,7 +288,7 @@ class Bytes {
 		if( pos < 0 || pos + 4 > length ) throw Error.OutsideBounds;
 		if( pos < 0 || pos + 4 > length ) throw Error.OutsideBounds;
 		untyped __global__.__hxcpp_memory_set_float(b,pos,v);
 		untyped __global__.__hxcpp_memory_set_float(b,pos,v);
 		#else
 		#else
-		setI32(pos, FPHelper.floatToI32(v));
+		setInt32(pos, FPHelper.floatToI32(v));
 		#end
 		#end
 	}
 	}
 
 
@@ -303,20 +303,35 @@ class Bytes {
 		#end
 		#end
 	}
 	}
 
 
+	/**
+		Returns the 64 bit integer at given position (in low endian encoding).
+	**/
+	public inline function getInt64( pos : Int ) : haxe.Int64 {
+		return haxe.Int64.make(getInt32(pos+4),getInt32(pos));
+	}
+
 	/**
 	/**
 		Store the 32 bit integer at given position (in low endian encoding).
 		Store the 32 bit integer at given position (in low endian encoding).
 	**/
 	**/
-	public inline function setInt32( pos : Int, value : Int ) : Void {
+	public inline function setInt32( pos : Int, v : Int ) : Void {
 		#if neko_v21
 		#if neko_v21
-		untyped $sset32(b, pos, value, false);
+		untyped $sset32(b, pos, v, false);
 		#else
 		#else
-		set(pos, value);
-		set(pos + 1, value >> 8);
-		set(pos + 2, value >> 16);
-		set(pos + 3, value >>> 24);
+		set(pos, v);
+		set(pos + 1, v >> 8);
+		set(pos + 2, v >> 16);
+		set(pos + 3, v >>> 24);
 		#end
 		#end
 	}
 	}
 
 
+	/**
+		Store the 64 bit integer at given position (in low endian encoding).
+	**/
+	public inline function setInt64( pos : Int, v : haxe.Int64 ) : Void {
+		setInt32(pos, haxe.Int64.getLow(v));
+		setInt32(pos + 4, haxe.Int64.getHigh(v));
+	}
+
 	public function getString( pos : Int, len : Int ) : String {
 	public function getString( pos : Int, len : Int ) : String {
 		#if !neko
 		#if !neko
 		if( pos < 0 || len < 0 || pos + len > length ) throw Error.OutsideBounds;
 		if( pos < 0 || len < 0 || pos + len > length ) throw Error.OutsideBounds;

+ 1 - 1
std/haxe/io/BytesBuffer.hx

@@ -127,7 +127,7 @@ class BytesBuffer {
 	
 	
 	public #if flash9 inline #end function addInt32( v : Int ) {
 	public #if flash9 inline #end function addInt32( v : Int ) {
 		#if flash9
 		#if flash9
-		b.writeUInt32(v);
+		b.writeUnsignedInt(v);
 		#else
 		#else
 		addByte(v&0xFF);
 		addByte(v&0xFF);
 		addByte((v>>8)&0xFF);
 		addByte((v>>8)&0xFF);

+ 13 - 4
std/js/_std/haxe/io/Bytes.hx

@@ -95,14 +95,23 @@ class Bytes {
 		data.setFloat32(pos, v, true);
 		data.setFloat32(pos, v, true);
 	}
 	}
 	
 	
-	public function getI32( pos : Int ) : Int {
+	public function getInt32( pos : Int ) : Int {
 		initData();
 		initData();
-		return data.getInt32(pos);
+		return data.getInt32(pos, true);
 	}
 	}
 
 
-	public function setI32( pos : Int, value : Int ) : Void {
+	public function setInt32( pos : Int, v : Int ) : Void {
 		initData();
 		initData();
-		data.setInt32(pos, value);
+		data.setInt32(pos, v, true);
+	}
+
+	public function getInt64( pos : Int ) : haxe.Int64 {
+		return Int64.make(getInt32(pos + 4),getInt32(pos));
+	}
+
+	public function setInt64( pos : Int, v : haxe.Int64 ) : Void {
+		setInt32(pos, haxe.Int64.getLow(v));
+		setInt32(pos + 4, haxe.Int64.getHigh(v));
 	}
 	}
 	
 	
 	public function getString( pos : Int, len : Int ) : String {
 	public function getString( pos : Int, len : Int ) : String {

+ 0 - 15
tests/unit/src/unit/TestBytes.hx

@@ -98,21 +98,6 @@ class TestBytes extends Test {
 		exc(function() bs.sub(1,10));
 		exc(function() bs.sub(1,10));
 	}
 	}
 
 
-	function testBuffer() {
-		var out = new haxe.io.BytesBuffer();
-		eq( out.length, 0 );
-		out.add( haxe.io.Bytes.ofString("ABCDEF") );
-		for( i in 1...6 )
-			out.addByte(i);
-		out.addBytes( haxe.io.Bytes.ofString("ABCDEF"),1,3 );
-		eq( out.length, 14 );
-		var b = out.getBytes();
-		var str = "ABCDEF\x01\x02\x03\x04\x05BCD";
-		eq( b.length, str.length );
-		for( i in 0...str.length )
-			eq( b.get(i), str.charCodeAt(i) );
-	}
-
 	function testInput() {
 	function testInput() {
 		var bs = haxe.io.Bytes.ofString("One é accent");
 		var bs = haxe.io.Bytes.ofString("One é accent");
 		var input = new haxe.io.BytesInput(bs);
 		var input = new haxe.io.BytesInput(bs);

+ 44 - 0
tests/unit/src/unitstd/haxe/io/BytesBuffer.unit.hx

@@ -0,0 +1,44 @@
+var out = new haxe.io.BytesBuffer();
+
+out.length == 0;
+out.add( haxe.io.Bytes.ofString("ABCDEF") );
+
+out.length == 6;
+
+for( i in 1...6 )
+	out.addByte(i);
+
+out.addBytes( haxe.io.Bytes.ofString("ABCDEF"),1,3 );
+
+out.length == 14;
+
+var b = out.getBytes();
+var str = "ABCDEF\x01\x02\x03\x04\x05BCD";
+b.length == str.length;
+for( i in 0...str.length )
+	b.get(i) == str.charCodeAt(i);
+	
+
+var out = new haxe.io.BytesBuffer();
+out.addInt32(0xABCDEF00);
+out.addByte(42);
+out.addFloat(1.3);
+out.addDouble(2.4);
+out.addInt64(haxe.Int64.make(0xABCDEF00,0xCAFFEED1));
+
+var b = out.getBytes();
+
+b.length == 25;
+
+b.getInt32(0) == 0xABCDEF00;
+b.get(4) == 42;
+b.getFloat(5) == 1.2999999523162842;
+b.getDouble(9) == 2.4;
+b.getInt64(17) == haxe.Int64.make(0xABCDEF00,0xCAFFEED1);
+
+// check correct low endian encoding
+b.get(3) == 0xAB;
+b.get(5) == 102;
+b.get(9) == 51;
+b.get(17) == 0xD1;
+b.get(22) == 0xEF;