Skip to content

Commit efa75be

Browse files
committed
use utf8array to output utf8encode results
1 parent 44cf20e commit efa75be

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

src/Encoder.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,15 @@ export class Encoder {
151151
const maxSize = maxHeaderSize + strLength * 4;
152152
this.ensureBufferSizeToWrite(maxSize);
153153

154-
const consumedLength = utf8EncodeWasm(object, this.bytes, this.pos);
155-
this.pos += consumedLength;
154+
// utf8EncodeWasm() handles headByte+size as well as string itself
155+
const ouputLength = utf8EncodeWasm(object, this.bytes, this.pos);
156+
this.pos += ouputLength;
156157
return;
157158
} else {
158159
const byteLength = utf8Count(object);
159160
this.ensureBufferSizeToWrite(maxHeaderSize + byteLength);
160161
this.writeStringHeader(byteLength);
161-
utf8Encode(object, this.view, this.pos);
162+
utf8Encode(object, this.bytes, this.pos);
162163
this.pos += byteLength;
163164
}
164165
}

src/utils/utf8.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export function utf8Count(str: string): number {
3838
return byteLength;
3939
}
4040

41-
export function utf8Encode(str: string, output: DataView, outputOffset: number): void {
41+
export function utf8Encode(str: string, output: Uint8Array, outputOffset: number): void {
4242
const strLength = str.length;
4343
let offset = outputOffset;
4444
let pos = 0;
@@ -47,11 +47,11 @@ export function utf8Encode(str: string, output: DataView, outputOffset: number):
4747

4848
if ((value & 0xffffff80) === 0) {
4949
// 1-byte
50-
output.setUint8(offset++, value);
50+
output[offset++] = value;
5151
continue;
5252
} else if ((value & 0xfffff800) === 0) {
5353
// 2-bytes
54-
output.setUint8(offset++, ((value >> 6) & 0x1f) | 0xc0);
54+
output[offset++] = ((value >> 6) & 0x1f) | 0xc0;
5555
} else {
5656
// handle surrogate pair
5757
if (value >= 0xd800 && value <= 0xdbff) {
@@ -67,17 +67,17 @@ export function utf8Encode(str: string, output: DataView, outputOffset: number):
6767

6868
if ((value & 0xffff0000) === 0) {
6969
// 3-byte
70-
output.setUint8(offset++, ((value >> 12) & 0x0f) | 0xe0);
71-
output.setUint8(offset++, ((value >> 6) & 0x3f) | 0x80);
70+
output[offset++] = ((value >> 12) & 0x0f) | 0xe0;
71+
output[offset++] = ((value >> 6) & 0x3f) | 0x80;
7272
} else {
7373
// 4-byte
74-
output.setUint8(offset++, ((value >> 18) & 0x07) | 0xf0);
75-
output.setUint8(offset++, ((value >> 12) & 0x3f) | 0x80);
76-
output.setUint8(offset++, ((value >> 6) & 0x3f) | 0x80);
74+
output[offset++] = ((value >> 18) & 0x07) | 0xf0;
75+
output[offset++] = ((value >> 12) & 0x3f) | 0x80;
76+
output[offset++] = ((value >> 6) & 0x3f) | 0x80;
7777
}
7878
}
7979

80-
output.setUint8(offset++, (value & 0x3f) | 0x80);
80+
output[offset++] = (value & 0x3f) | 0x80;
8181
}
8282
}
8383

0 commit comments

Comments
 (0)