Skip to content

Commit e3e6b3a

Browse files
RedDwarfianjtenner
authored andcommitted
[Implement] Buffer.swap16/32/64 (#22)
* [Implement] Buffer.swap16/32/64 * [Cleanup] Using proper error message
1 parent 5f0b0dc commit e3e6b3a

3 files changed

Lines changed: 84 additions & 0 deletions

File tree

assembly/buffer/index.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,45 @@ export class Buffer extends Uint8Array {
237237
store<i64>(this.dataStart + offset, bswap<i64>(reinterpret<i64>(value)));
238238
return offset + 8;
239239
}
240+
241+
swap16(): Buffer {
242+
let dataLength = this.dataLength;
243+
// Make sure dataLength is even
244+
if (dataLength & 1) throw new RangeError(E_INVALIDLENGTH);
245+
let dataStart = this.dataStart;
246+
dataLength += dataStart;
247+
while (dataStart < dataLength) {
248+
store<u16>(dataStart, bswap<u16>(load<u16>(dataStart)));
249+
dataStart += 2;
250+
}
251+
return this;
252+
}
253+
254+
swap32(): Buffer {
255+
let dataLength = this.dataLength;
256+
// Make sure dataLength is divisible by 4
257+
if (dataLength & 3) throw new RangeError(E_INVALIDLENGTH);
258+
let dataStart = this.dataStart;
259+
dataLength += dataStart;
260+
while (dataStart < dataLength) {
261+
store<u32>(dataStart, bswap<u32>(load<u32>(dataStart)));
262+
dataStart += 4;
263+
}
264+
return this;
265+
}
266+
267+
swap64(): Buffer {
268+
let dataLength = this.dataLength;
269+
// Make sure dataLength is divisible by 8
270+
if (dataLength & 7) throw new RangeError(E_INVALIDLENGTH);
271+
let dataStart = this.dataStart;
272+
dataLength += dataStart;
273+
while (dataStart < dataLength) {
274+
store<u64>(dataStart, bswap<u64>(load<u64>(dataStart)));
275+
dataStart += 8;
276+
}
277+
return this;
278+
}
240279
}
241280

242281
export namespace Buffer {

assembly/node.d.ts

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

8288
declare namespace Buffer {

tests/buffer.spec.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,45 @@ describe("buffer", () => {
531531
expected = create<Buffer>([5, 6, 7]);
532532
expect<Buffer>(actual).toStrictEqual(expected);
533533
});
534+
535+
test("#swap16", () => {
536+
let actual = create<Buffer>([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]);
537+
let expected = create<Buffer>([0x2, 0x1, 0x4, 0x3, 0x6, 0x5, 0x8, 0x7]);
538+
let swapped = actual.swap16();
539+
expect<Buffer>(actual).toStrictEqual(expected);
540+
expect<Buffer>(swapped).toBe(actual);
541+
// TODO:
542+
// expectFn(() => {
543+
// let newBuff = create<Buffer>([0x1, 0x2, 0x3]);
544+
// newBuff.swap16();
545+
// }).toThrow();
546+
});
547+
548+
test("#swap32", () => {
549+
let actual = create<Buffer>([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]);
550+
let expected = create<Buffer>([0x4, 0x3, 0x2, 0x1, 0x8, 0x7, 0x6, 0x5]);
551+
let swapped = actual.swap32();
552+
expect<Buffer>(actual).toStrictEqual(expected);
553+
expect<Buffer>(swapped).toBe(actual);
554+
// TODO:
555+
// expectFn(() => {
556+
// let newBuff = create<Buffer>([0x1, 0x2, 0x3]);
557+
// newBuff.swap64();
558+
// }).toThrow();
559+
});
560+
561+
test("#swap64", () => {
562+
let actual = create<Buffer>([0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf]);
563+
let expected = create<Buffer>([0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1, 0x0, 0xf, 0xe, 0xd, 0xc, 0xb, 0xa, 0x9, 0x8]);
564+
let swapped = actual.swap64();
565+
expect<Buffer>(actual).toStrictEqual(expected);
566+
expect<Buffer>(swapped).toBe(actual);
567+
// TODO:
568+
// expectFn(() => {
569+
// let newBuff = create<Buffer>([0x1, 0x2, 0x3]);
570+
// newBuff.swap64();
571+
// }).toThrow();
572+
});
534573

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

0 commit comments

Comments
 (0)