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

Introducing late Uint8Array initialization #275

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions lib/utils/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@ try { String.fromCharCode.apply(null, new Uint8Array(1)); } catch (__) { STR_APP
// Table with utf8 lengths (calculated by first byte of sequence)
// Note, that 5 & 6-byte values and some 4-byte values can not be represented in JS,
// because max possible codepoint is 0x10ffff
const _utf8len = new Uint8Array(256);
for (let q = 0; q < 256; q++) {
_utf8len[q] = (q >= 252 ? 6 : q >= 248 ? 5 : q >= 240 ? 4 : q >= 224 ? 3 : q >= 192 ? 2 : 1);
let _utf8len = null;
function maybeInitializeUtf8LenBuffer() {
if (_utf8len === null) {
_utf8len = new Uint8Array(256);
for (let q = 0; q < 256; q++) {
_utf8len[q] = (q >= 252 ? 6 : q >= 248 ? 5 : q >= 240 ? 4 : q >= 224 ? 3 : q >= 192 ? 2 : 1);
}
_utf8len[254] = _utf8len[254] = 1; // Invalid sequence start
}
}
_utf8len[254] = _utf8len[254] = 1; // Invalid sequence start


// convert string to array (typed, when possible)
module.exports.string2buf = (str) => {
Expand Down Expand Up @@ -113,6 +117,7 @@ module.exports.buf2string = (buf, max) => {
// NB: by unknown reasons, Array is significantly faster for
// String.fromCharCode.apply than Uint16Array.
const utf16buf = new Array(len * 2);
maybeInitializeUtf8LenBuffer();

for (out = 0, i = 0; i < len;) {
let c = buf[i++];
Expand Down Expand Up @@ -169,6 +174,8 @@ module.exports.utf8border = (buf, max) => {
// If we came to start of buffer - that means buffer is too small,
// return max too.
if (pos === 0) { return max; }

maybeInitializeUtf8LenBuffer();

return (pos + _utf8len[buf[pos]] > max) ? pos : max;
};