Skip to content

Commit

Permalink
more work on bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
ncannasse committed Mar 1, 2015
1 parent 34a0e6e commit f2048bb
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 29 deletions.
33 changes: 24 additions & 9 deletions std/haxe/io/Bytes.hx
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,8 @@ class Bytes {
untyped __global__.__hxcpp_memory_set_double(b,pos,v);
#else
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
}

Expand All @@ -288,7 +288,7 @@ class Bytes {
if( pos < 0 || pos + 4 > length ) throw Error.OutsideBounds;
untyped __global__.__hxcpp_memory_set_float(b,pos,v);
#else
setI32(pos, FPHelper.floatToI32(v));
setInt32(pos, FPHelper.floatToI32(v));
#end
}

Expand All @@ -303,20 +303,35 @@ class Bytes {
#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).
**/
public inline function setInt32( pos : Int, value : Int ) : Void {
public inline function setInt32( pos : Int, v : Int ) : Void {
#if neko_v21
untyped $sset32(b, pos, value, false);
untyped $sset32(b, pos, v, false);
#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
}

/**
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 {
#if !neko
if( pos < 0 || len < 0 || pos + len > length ) throw Error.OutsideBounds;
Expand Down
2 changes: 1 addition & 1 deletion std/haxe/io/BytesBuffer.hx
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class BytesBuffer {

public #if flash9 inline #end function addInt32( v : Int ) {
#if flash9
b.writeUInt32(v);
b.writeUnsignedInt(v);
#else
addByte(v&0xFF);
addByte((v>>8)&0xFF);
Expand Down
17 changes: 13 additions & 4 deletions std/js/_std/haxe/io/Bytes.hx
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,23 @@ class Bytes {
data.setFloat32(pos, v, true);
}

public function getI32( pos : Int ) : Int {
public function getInt32( pos : Int ) : Int {
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();
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 {
Expand Down
15 changes: 0 additions & 15 deletions tests/unit/src/unit/TestBytes.hx
Original file line number Diff line number Diff line change
Expand Up @@ -98,21 +98,6 @@ class TestBytes extends Test {
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() {
var bs = haxe.io.Bytes.ofString("One é accent");
var input = new haxe.io.BytesInput(bs);
Expand Down
44 changes: 44 additions & 0 deletions tests/unit/src/unitstd/haxe/io/BytesBuffer.unit.hx
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit f2048bb

Please sign in to comment.