Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use TextEncoder and TextDecoder for utf8 strings #4513

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
26 changes: 11 additions & 15 deletions packages/adblocker/src/data-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ export const EMPTY_UINT32_ARRAY = new Uint32Array(0);
// Check if current architecture is little endian
const LITTLE_ENDIAN: boolean = new Int8Array(new Int16Array([1]).buffer)[0] === 1;

// TextEncoder doesn't need to be recreated every time unlike TextDecoder
const TEXT_ENCODER = new TextEncoder();
seia-soto marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alternatively you could do:

const encoder = new TextEncoder();
const encode = encoder.encode.bind(encoder);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

encode is already exported by punycode. Would you like to have another variable name?


// Store compression in a lazy, global singleton
let getCompressionSingleton: () => Compression = () => {
const COMPRESSION = new Compression();
Expand Down Expand Up @@ -87,8 +90,7 @@ export function sizeOfASCII(str: string): number {
* Return number of bytes needed to serialize `str` UTF8 string.
*/
export function sizeOfUTF8(str: string): number {
const encodedLength = encode(str).length;
return encodedLength + sizeOfLength(encodedLength);
return 4 + TEXT_ENCODER.encode(str).length;
seia-soto marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down Expand Up @@ -389,23 +391,17 @@ export class StaticDataView {
}

public pushUTF8(raw: string): void {
const str = encode(raw);
this.pushLength(str.length);

for (let i = 0; i < str.length; i += 1) {
this.buffer[this.pos++] = str.charCodeAt(i);
}
const { written } = TEXT_ENCODER.encodeInto(raw, this.buffer.subarray(this.pos + 4));
this.pushLength(written);
seia-soto marked this conversation as resolved.
Show resolved Hide resolved
this.setPos(this.pos + written);
}

public getUTF8(): string {
const byteLength = this.getLength();
this.pos += byteLength;
return decode(
String.fromCharCode.apply(
null,
// @ts-ignore
this.buffer.subarray(this.pos - byteLength, this.pos),
),
const pos = this.getPos();
this.setPos(pos + byteLength);
return new TextDecoder('utf8', { ignoreBOM: true }).decode(
this.buffer.subarray(pos, pos + byteLength),
seia-soto marked this conversation as resolved.
Show resolved Hide resolved
);
}

Expand Down
Loading