I have two Uint8Arrays of length 32 bytes each and I want to merge them to one Uint8Array of length 64.
So my question is:
Is there a way to merge two TypedArrays to a new TypedArray of the same type in AssemblyScript.
I guess I'm looking for the equivalent of the TypedArray.set()
method in Javascript:
var c = new Uint8Array(Uint8Array_1.length + Uint8Array_2.length);
c.set(Uint8Array_1);
c.set(Uint8Array_2, Uint8Array_1.length);
Any hints are welcome!
UPDATE
TypedArray.set
have already landed to master and assemblyscript@nightly
so you could do it in usual way for now:
var c = new Uint8Array(a.length + b.length);
c.set(a);
c.set(b, a.length);
Currently TypedArray.set
not implemented but you still have variants:
Version 1 (JavaScript way):
var a: Int8Array = ...
var b: Int8Array = ...
var alen = a.length;
var blen = b.length;
var c = new Int8Array(alen + blen);
for (let i = 0; i < alen; i++) {
unchecked(c[i] = a[i]);
}
for (let i = 0; i < blen; i++) {
unchecked(c[i + alen] = b[i]);
}
Version 2 (AssemblyScript way):
var a: Int8Array = ...
var b: Int8Array = ...
var alen = a.length;
var blen = b.length;
var c = new Int8Array(alen + blen);
var aPtr = a.dataStart;
var bPtr = b.dataStart;
var cPtr = c.dataStart;
memory.copy(cPtr, aPtr, alen);
memory.copy(cPtr + alen, bPtr, blen);