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

Optimize utf8 decoder #28220

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
69 changes: 57 additions & 12 deletions src/loaders/LoaderUtils.js
Expand Up @@ -8,30 +8,75 @@ class LoaderUtils {

}

// Avoid the String.fromCharCode.apply(null, array) shortcut, which
// throws a "maximum call stack size exceeded" error for large arrays.

let s = '';

for ( let i = 0, il = array.length; i < il; i ++ ) {
for ( let i = 0, l = array.length; i < l; ) {

// Implicitly assumes little-endian.
s += String.fromCharCode( array[ i ] );
if ( array[ i ] >> 7 === 0 ) {

}
s += String.fromCodePoint( array[ i ] );
i += 1;
continue;

} else if ( array[ i ] >> 5 === 0b110 ) {

if ( array[ i + 1 ] >> 6 === 0b10 ) {

s += String.fromCodePoint( ( ( array[ i ] & 0b11111 ) << 6 ) + ( array[ i + 1 ] & 0b111111 ) );
i += 2;
continue;

}

} else if ( array[ i ] >> 4 === 0b1110 ) {

if ( array[ i + 1 ] >> 6 === 0b10 && array[ i + 2 ] >> 6 === 0b10 ) {

s += String.fromCodePoint( ( ( array[ i ] & 0b1111 ) << 12 ) + ( ( array[ i + 1 ] & 0b111111 ) << 6 ) + ( array[ i + 2 ] & 0b111111 ) );
i += 3;
continue;

}

try {
} else if ( array[ i ] >> 3 === 0b11110 ) {

// merges multi-byte utf-8 characters.
if ( array[ i + 1 ] >> 6 === 0b10 && array[ i + 2 ] >> 6 === 0b10 && array[ i + 3 ] >> 6 === 0b10 ) {

return decodeURIComponent( escape( s ) );
s += String.fromCodePoint( ( ( array[ i ] & 0b111 ) << 18 ) + ( ( array[ i + 1 ] & 0b111111 ) << 12 ) + ( ( array[ i + 2 ] & 0b111111 ) << 6 ) + ( array[ i + 3 ] & 0b111111 ) );
i += 4;
continue;

} catch ( e ) { // see #16358
}

return s;
} else if ( array[ i ] >> 2 === 0b111110 ) {

if ( array[ i + 1 ] >> 6 === 0b10 && array[ i + 2 ] >> 6 === 0b10 && array[ i + 3 ] >> 6 === 0b10 && array[ i + 4 ] >> 6 === 0b10 ) {

s += String.fromCodePoint( ( ( array[ i ] & 0b11 ) << 24 ) + ( ( array[ i + 1 ] & 0b111111 ) << 18 ) + ( ( array[ i + 2 ] & 0b111111 ) << 12 ) + ( ( array[ i + 3 ] & 0b111111 ) << 6 ) + ( array[ i + 4 ] & 0b111111 ) );
i += 5;
continue;

}

} else if ( array[ i ] >> 1 === 0b1111110 ) {

if ( array[ i + 1 ] >> 6 === 0b10 && array[ i + 2 ] >> 6 === 0b10 && array[ i + 3 ] >> 6 === 0b10 && array[ i + 4 ] >> 6 === 0b10 && array[ i + 5 ] >> 6 === 0b10 ) {

s += String.fromCodePoint( ( ( array[ i ] & 0b1 ) << 30 ) + ( ( array[ i + 1 ] & 0b111111 ) << 24 ) + ( ( array[ i + 2 ] & 0b111111 ) << 18 ) + ( ( array[ i + 3 ] & 0b111111 ) << 12 ) + ( ( array[ i + 4 ] & 0b111111 ) << 6 ) + ( array[ i + 5 ] & 0b111111 ) );
i += 6;
continue;

}

}

s += String.fromCodePoint( 0xfffd );
i += 1;

}

return s;

}

static extractUrlBase( url ) {
Expand Down