class Bytes {
public var length(default,null) : Int;
var b : BytesData;
function new(length,b) {
this.length = length;
this.b = b;
}
public inline function get( pos : Int ) : Int {
return b[pos];
}
public inline function set( pos : Int, v : Int ) : Void {
b[pos] = v;
}
public function blit( pos : Int, src : Bytes, srcpos : Int, len : Int ) : Void {
var subSrc = src.subarray(srcpos, srcpos + len);
b.set(srcSub, pos);
}
public function sub( pos : Int, len : Int ) : Bytes {
return new Bytes(len, b.subarray(pos, pos + len);
}
public function compare( other : Bytes ) : Int {
// no changes
}
public function readString( pos : Int, len : Int ) : String {
return String.fromCharCode.apply(null, new Uint16Array(b.buffer, pos, len));
}
public function toString() : String {
return readString(0,length);
}
public static function alloc( length : Int ) : Bytes {
return new Bytes(length, new Uint8Array(length));
}
public static function ofString( s : String ) : Bytes {
var bufView = new Uint16Array(new ArrayBuffer(s.length * 2));
var strlen = s.length;
for(i in 0 ... strlen) {
bufView[i] = str.charCodeAt(i);
}
var b = new Uint8Array(bufView.buffer);
return new Bytes(b.length, b);
}
public static function ofData( b : BytesData ) {
return new Bytes(b.length, b);
}
}