Skip to content

Commit b6ef8c7

Browse files
authored
Buffer refactoring (#24)
* refactor Buffer * remove inline for exported methods
1 parent e3e6b3a commit b6ef8c7

File tree

1 file changed

+46
-48
lines changed

1 file changed

+46
-48
lines changed

assembly/buffer/index.ts

Lines changed: 46 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,28 @@ export class Buffer extends Uint8Array {
77
super(size);
88
}
99

10-
public static alloc(size: i32): Buffer {
10+
static alloc(size: i32): Buffer {
1111
return new Buffer(size);
1212
}
1313

14-
@unsafe public static allocUnsafe(size: i32): Buffer {
14+
@unsafe static allocUnsafe(size: i32): Buffer {
1515
// Node throws an error if size is less than 0
16-
if (u32(size) > BLOCK_MAXSIZE) throw new RangeError(E_INVALIDLENGTH);
16+
if (<u32>size > BLOCK_MAXSIZE) throw new RangeError(E_INVALIDLENGTH);
1717
let buffer = __alloc(size, idof<ArrayBuffer>());
1818
// This retains the pointer to the result Buffer.
1919
let result = changetype<Buffer>(__alloc(offsetof<Buffer>(), idof<Buffer>()));
2020
result.data = changetype<ArrayBuffer>(buffer);
21-
result.dataStart = changetype<usize>(buffer);
21+
result.dataStart = buffer;
2222
result.dataLength = size;
2323
return result;
2424
}
2525

26-
public static isBuffer<T>(value: T): bool {
26+
static isBuffer<T>(value: T): bool {
2727
return value instanceof Buffer;
2828
}
2929

3030
// Adapted from https://github.com/AssemblyScript/assemblyscript/blob/master/std/assembly/typedarray.ts
31-
public subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Buffer {
31+
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Buffer {
3232
var len = <i32>this.dataLength;
3333
begin = begin < 0 ? max(len + begin, 0) : min(begin, len);
3434
end = end < 0 ? max(len + end, 0) : min(end, len);
@@ -69,7 +69,7 @@ export class Buffer extends Uint8Array {
6969

7070
readInt16BE(offset: i32 = 0): i16 {
7171
if (i32(offset < 0) | i32(<u32>offset + 2 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
72-
return bswap<i16>(load<i16>(this.dataStart + <usize>offset));
72+
return bswap(load<i16>(this.dataStart + <usize>offset));
7373
}
7474

7575
readUInt16LE(offset: i32 = 0): u16 {
@@ -79,7 +79,7 @@ export class Buffer extends Uint8Array {
7979

8080
readUInt16BE(offset: i32 = 0): u16 {
8181
if (i32(offset < 0) | i32(<u32>offset + 2 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
82-
return bswap<u16>(load<u16>(this.dataStart + <usize>offset));
82+
return bswap(load<u16>(this.dataStart + <usize>offset));
8383
}
8484

8585
writeInt16LE(value: i16, offset: i32 = 0): i32 {
@@ -90,7 +90,7 @@ export class Buffer extends Uint8Array {
9090

9191
writeInt16BE(value: i16, offset: i32 = 0): i32 {
9292
if (i32(offset < 0) | i32(<u32>offset + 2 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
93-
store<i16>(this.dataStart + offset, bswap<i16>(value));
93+
store<i16>(this.dataStart + offset, bswap(value));
9494
return offset + 2;
9595
}
9696

@@ -102,7 +102,7 @@ export class Buffer extends Uint8Array {
102102

103103
writeUInt16BE(value: u16, offset: i32 = 0): i32 {
104104
if (i32(offset < 0) | i32(<u32>offset + 2 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
105-
store<u16>(this.dataStart + offset, bswap<u16>(value));
105+
store<u16>(this.dataStart + offset, bswap(value));
106106
return offset + 2;
107107
}
108108

@@ -113,7 +113,7 @@ export class Buffer extends Uint8Array {
113113

114114
readInt32BE(offset: i32 = 0): i32 {
115115
if (i32(offset < 0) | i32(<u32>offset + 4 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
116-
return bswap<i32>(load<i32>(this.dataStart + <usize>offset));
116+
return bswap(load<i32>(this.dataStart + <usize>offset));
117117
}
118118

119119
readUInt32LE(offset: i32 = 0): u32 {
@@ -123,7 +123,7 @@ export class Buffer extends Uint8Array {
123123

124124
readUInt32BE(offset: i32 = 0): u32 {
125125
if (i32(offset < 0) | i32(<u32>offset + 4 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
126-
return bswap<u32>(load<u32>(this.dataStart + <usize>offset));
126+
return bswap(load<u32>(this.dataStart + <usize>offset));
127127
}
128128

129129
writeInt32LE(value: i32, offset: i32 = 0): i32 {
@@ -134,7 +134,7 @@ export class Buffer extends Uint8Array {
134134

135135
writeInt32BE(value: i32, offset: i32 = 0): i32 {
136136
if (i32(offset < 0) | i32(<u32>offset + 4 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
137-
store<i32>(this.dataStart + offset, bswap<i32>(value));
137+
store<i32>(this.dataStart + offset, bswap(value));
138138
return offset + 4;
139139
}
140140

@@ -146,7 +146,7 @@ export class Buffer extends Uint8Array {
146146

147147
writeUInt32BE(value: u32, offset: i32 = 0): i32 {
148148
if (i32(offset < 0) | i32(<u32>offset + 4 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
149-
store<u32>(this.dataStart + offset, bswap<u32>(value));
149+
store<u32>(this.dataStart + offset, bswap(value));
150150
return offset + 4;
151151
}
152152

@@ -157,7 +157,7 @@ export class Buffer extends Uint8Array {
157157

158158
readFloatBE(offset: i32 = 0): f32 {
159159
if (i32(offset < 0) | i32(<u32>offset + 4 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
160-
return reinterpret<f32>(bswap<i32>(load<i32>(this.dataStart + <usize>offset)));
160+
return reinterpret<f32>(bswap(load<i32>(this.dataStart + <usize>offset)));
161161
}
162162

163163
writeFloatLE(value: f32, offset: i32 = 0): i32 {
@@ -168,7 +168,7 @@ export class Buffer extends Uint8Array {
168168

169169
writeFloatBE(value: f32, offset: i32 = 0): i32 {
170170
if (i32(offset < 0) | i32(<u32>offset + 4 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
171-
store<i32>(this.dataStart + offset, bswap<i32>(reinterpret<i32>(value)));
171+
store<i32>(this.dataStart + offset, bswap(reinterpret<i32>(value)));
172172
return offset + 4;
173173
}
174174

@@ -179,7 +179,7 @@ export class Buffer extends Uint8Array {
179179

180180
readBigInt64BE(offset: i32 = 0): i64 {
181181
if (i32(offset < 0) | i32(<u32>offset + 8 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
182-
return bswap<i64>(load<i64>(this.dataStart + <usize>offset));
182+
return bswap(load<i64>(this.dataStart + <usize>offset));
183183
}
184184

185185
readBigUInt64LE(offset: i32 = 0): u64 {
@@ -189,7 +189,7 @@ export class Buffer extends Uint8Array {
189189

190190
readBigUInt64BE(offset: i32 = 0): u64 {
191191
if (i32(offset < 0) | i32(<u32>offset + 8 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
192-
return bswap<u64>(load<u64>(this.dataStart + <usize>offset));
192+
return bswap(load<u64>(this.dataStart + <usize>offset));
193193
}
194194

195195
writeBigInt64LE(value: i64, offset: i32 = 0): i32 {
@@ -200,7 +200,7 @@ export class Buffer extends Uint8Array {
200200

201201
writeBigInt64BE(value: i64, offset: i32 = 0): i32 {
202202
if (i32(offset < 0) | i32(<u32>offset + 8 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
203-
store<i64>(this.dataStart + offset, bswap<i64>(value));
203+
store<i64>(this.dataStart + offset, bswap(value));
204204
return offset + 8;
205205
}
206206

@@ -212,7 +212,7 @@ export class Buffer extends Uint8Array {
212212

213213
writeBigUInt64BE(value: u64, offset: i32 = 0): i32 {
214214
if (i32(offset < 0) | i32(<u32>offset + 8 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
215-
store<u64>(this.dataStart + offset, bswap<u64>(value));
215+
store<u64>(this.dataStart + offset, bswap(value));
216216
return offset + 8;
217217
}
218218

@@ -223,7 +223,7 @@ export class Buffer extends Uint8Array {
223223

224224
readDoubleBE(offset: i32 = 0): f64 {
225225
if (i32(offset < 0) | i32(<u32>offset + 8 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
226-
return reinterpret<f64>(bswap<i64>(load<i64>(this.dataStart + <usize>offset)));
226+
return reinterpret<f64>(bswap(load<i64>(this.dataStart + <usize>offset)));
227227
}
228228

229229
writeDoubleLE(value: f64, offset: i32 = 0): i32 {
@@ -234,7 +234,7 @@ export class Buffer extends Uint8Array {
234234

235235
writeDoubleBE(value: f64, offset: i32 = 0): i32 {
236236
if (i32(offset < 0) | i32(<u32>offset + 8 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
237-
store<i64>(this.dataStart + offset, bswap<i64>(reinterpret<i64>(value)));
237+
store<i64>(this.dataStart + offset, bswap(reinterpret<i64>(value)));
238238
return offset + 8;
239239
}
240240

@@ -245,33 +245,33 @@ export class Buffer extends Uint8Array {
245245
let dataStart = this.dataStart;
246246
dataLength += dataStart;
247247
while (dataStart < dataLength) {
248-
store<u16>(dataStart, bswap<u16>(load<u16>(dataStart)));
248+
store<u16>(dataStart, bswap(load<u16>(dataStart)));
249249
dataStart += 2;
250250
}
251251
return this;
252252
}
253-
253+
254254
swap32(): Buffer {
255255
let dataLength = this.dataLength;
256256
// Make sure dataLength is divisible by 4
257257
if (dataLength & 3) throw new RangeError(E_INVALIDLENGTH);
258258
let dataStart = this.dataStart;
259259
dataLength += dataStart;
260260
while (dataStart < dataLength) {
261-
store<u32>(dataStart, bswap<u32>(load<u32>(dataStart)));
261+
store<u32>(dataStart, bswap(load<u32>(dataStart)));
262262
dataStart += 4;
263263
}
264264
return this;
265265
}
266-
266+
267267
swap64(): Buffer {
268268
let dataLength = this.dataLength;
269269
// Make sure dataLength is divisible by 8
270270
if (dataLength & 7) throw new RangeError(E_INVALIDLENGTH);
271271
let dataStart = this.dataStart;
272272
dataLength += dataStart;
273273
while (dataStart < dataLength) {
274-
store<u64>(dataStart, bswap<u64>(load<u64>(dataStart)));
274+
store<u64>(dataStart, bswap(load<u64>(dataStart)));
275275
dataStart += 8;
276276
}
277277
return this;
@@ -280,29 +280,20 @@ export class Buffer extends Uint8Array {
280280

281281
export namespace Buffer {
282282
export namespace HEX {
283-
/** Calculates the two char combination from the byte. */
284-
@inline export function charsFromByte(byte: u32): u32 {
285-
let top = (byte >>> 4) & 0xF;
286-
let bottom = (0xF & byte);
287-
top += select(0x57, 0x30, top > 9);
288-
bottom += select(0x57, 0x30, bottom > 9);
289-
return (bottom << 16) | top;
290-
}
291-
292283
/** Calculates the byte length of the specified string when encoded as HEX. */
293284
export function byteLength(str: string): i32 {
294285
let ptr = changetype<usize>(str);
295286
let byteCount = changetype<BLOCK>(changetype<usize>(str) - BLOCK_OVERHEAD).rtSize;
296287
let length = byteCount >> 2;
297288
// The string length must be even because the bytes come in pairs of characters two wide
298-
if (byteCount & 0x3) return 0; // encoding fails and returns an empty ArrayBuffer
289+
if (byteCount & 3) return 0; // encoding fails and returns an empty ArrayBuffer
299290

300291
byteCount += ptr;
301292
while (ptr < byteCount) {
302293
var char = load<u16>(ptr);
303-
if ( ((char - 0x30) <= 0x9)
304-
|| ((char - 0x61) <= 0x5)
305-
|| ((char - 0x41) <= 0x5)) {
294+
if ( ((char - 0x30) <= 9)
295+
|| ((char - 0x61) <= 5)
296+
|| ((char - 0x41) <= 5)) {
306297
ptr += 2;
307298
continue;
308299
} else {
@@ -325,15 +316,14 @@ export namespace Buffer {
325316
let b: u32 = 0;
326317
let outChar = 0;
327318
for (let i: usize = 0; ptr < byteEnd; i++) {
328-
let odd = i & 1;
329-
if (odd) {
319+
if (i & 1) {
330320
outChar <<= 4;
331321
b >>>= 16;
332322
if ((b - 0x30) <= 9) {
333323
outChar |= b - 0x30;
334-
} else if ((b - 0x61) <= 0x5) {
324+
} else if ((b - 0x61) <= 5) {
335325
outChar |= b - 0x57;
336-
} else if (b - 0x41 <= 0x5) {
326+
} else if (b - 0x41 <= 5) {
337327
outChar |= b - 0x37;
338328
}
339329
store<u8>(result + (i >> 1), <u8>(outChar & 0xFF));
@@ -344,9 +334,9 @@ export namespace Buffer {
344334
let c = b & 0xFF;
345335
if ((c - 0x30) <= 9) {
346336
outChar |= c - 0x30;
347-
} else if ((c - 0x61) <= 0x5) {
337+
} else if ((c - 0x61) <= 5) {
348338
outChar |= c - 0x57;
349-
} else if (c - 0x41 <= 0x5) {
339+
} else if (c - 0x41 <= 5) {
350340
outChar |= c - 0x37;
351341
}
352342
}
@@ -368,12 +358,20 @@ export namespace Buffer {
368358

369359
// loop over each byte and store a `u32` for each one
370360
while (ptr < inputByteLength) {
371-
store<u32>(result + i, charsFromByte(<u32>load<u8>(ptr)));
361+
store<u32>(result + i, charsFromByte(<u32>load<u8>(ptr++)));
372362
i += 4;
373-
ptr++;
374363
}
375364

376365
return changetype<string>(result);
377366
}
367+
368+
/** Calculates the two char combination from the byte. */
369+
@inline function charsFromByte(byte: u32): u32 {
370+
let hi = (byte >>> 4) & 0xF;
371+
let lo = byte & 0xF;
372+
hi += select(0x57, 0x30, hi > 9);
373+
lo += select(0x57, 0x30, lo > 9);
374+
return (lo << 16) | hi;
375+
}
378376
}
379377
}

0 commit comments

Comments
 (0)