Skip to content

Commit a333399

Browse files
committed
Merge branch 'master' of https://github.com/assemblyscript/node into from
2 parents c6997e1 + e3e6b3a commit a333399

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

assembly/buffer/index.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,45 @@ export class Buffer extends Uint8Array {
290290
store<i64>(this.dataStart + offset, bswap<i64>(reinterpret<i64>(value)));
291291
return offset + 8;
292292
}
293+
294+
swap16(): Buffer {
295+
let dataLength = this.dataLength;
296+
// Make sure dataLength is even
297+
if (dataLength & 1) throw new RangeError(E_INVALIDLENGTH);
298+
let dataStart = this.dataStart;
299+
dataLength += dataStart;
300+
while (dataStart < dataLength) {
301+
store<u16>(dataStart, bswap<u16>(load<u16>(dataStart)));
302+
dataStart += 2;
303+
}
304+
return this;
305+
}
306+
307+
swap32(): Buffer {
308+
let dataLength = this.dataLength;
309+
// Make sure dataLength is divisible by 4
310+
if (dataLength & 3) throw new RangeError(E_INVALIDLENGTH);
311+
let dataStart = this.dataStart;
312+
dataLength += dataStart;
313+
while (dataStart < dataLength) {
314+
store<u32>(dataStart, bswap<u32>(load<u32>(dataStart)));
315+
dataStart += 4;
316+
}
317+
return this;
318+
}
319+
320+
swap64(): Buffer {
321+
let dataLength = this.dataLength;
322+
// Make sure dataLength is divisible by 8
323+
if (dataLength & 7) throw new RangeError(E_INVALIDLENGTH);
324+
let dataStart = this.dataStart;
325+
dataLength += dataStart;
326+
while (dataStart < dataLength) {
327+
store<u64>(dataStart, bswap<u64>(load<u64>(dataStart)));
328+
dataStart += 8;
329+
}
330+
return this;
331+
}
293332
}
294333

295334
export namespace Buffer {

assembly/node.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ declare class Buffer extends Uint8Array {
7979
writeDoubleLE(value: f64, offset?: i32): i32;
8080
/** Writes an inputted 64-bit double at the designated offset, stored in Big Endian format */
8181
writeDoubleBE(value: f64, offset?: i32): i32;
82+
/** Swaps every group of two bytes in a Buffer in-place */
83+
swap16(): Buffer;
84+
/** Swaps every group of four bytes in a Buffer in-place */
85+
swap32(): Buffer;
86+
/** Swaps every group of eight bytes in a Buffer in-place */
87+
swap64(): Buffer;
8288
}
8389

8490
declare namespace Buffer {

tests/buffer.spec.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,45 @@ describe("buffer", () => {
571571
expected = create<Buffer>([5, 6, 7]);
572572
expect<Buffer>(actual).toStrictEqual(expected);
573573
});
574+
575+
test("#swap16", () => {
576+
let actual = create<Buffer>([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]);
577+
let expected = create<Buffer>([0x2, 0x1, 0x4, 0x3, 0x6, 0x5, 0x8, 0x7]);
578+
let swapped = actual.swap16();
579+
expect<Buffer>(actual).toStrictEqual(expected);
580+
expect<Buffer>(swapped).toBe(actual);
581+
// TODO:
582+
// expectFn(() => {
583+
// let newBuff = create<Buffer>([0x1, 0x2, 0x3]);
584+
// newBuff.swap16();
585+
// }).toThrow();
586+
});
587+
588+
test("#swap32", () => {
589+
let actual = create<Buffer>([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]);
590+
let expected = create<Buffer>([0x4, 0x3, 0x2, 0x1, 0x8, 0x7, 0x6, 0x5]);
591+
let swapped = actual.swap32();
592+
expect<Buffer>(actual).toStrictEqual(expected);
593+
expect<Buffer>(swapped).toBe(actual);
594+
// TODO:
595+
// expectFn(() => {
596+
// let newBuff = create<Buffer>([0x1, 0x2, 0x3]);
597+
// newBuff.swap64();
598+
// }).toThrow();
599+
});
600+
601+
test("#swap64", () => {
602+
let actual = create<Buffer>([0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf]);
603+
let expected = create<Buffer>([0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1, 0x0, 0xf, 0xe, 0xd, 0xc, 0xb, 0xa, 0x9, 0x8]);
604+
let swapped = actual.swap64();
605+
expect<Buffer>(actual).toStrictEqual(expected);
606+
expect<Buffer>(swapped).toBe(actual);
607+
// TODO:
608+
// expectFn(() => {
609+
// let newBuff = create<Buffer>([0x1, 0x2, 0x3]);
610+
// newBuff.swap64();
611+
// }).toThrow();
612+
});
574613

575614
test("#Hex.encode", () => {
576615
let actual = "000102030405060708090a0b0c0d0e0f102030405060708090a0b0c0d0e0f0";

0 commit comments

Comments
 (0)