Skip to content

TextDecoder does not handle offset correctly #17

Open
@spongestrong

Description

@spongestrong

At https://github.com/anonyco/FastestSmallestTextEncoderDecoder/blob/master/individual/FastestTextDecoderPolyfill.src.js#L52 the input Uint8Array sometimes carry a non-zero byteOffset, which is needed to get the correct underlying ArrayBuffer.

> var aaa = new Uint8Array(new TextEncoder().encode("wrong hello").buffer, 6);
> new TextDecoder().decode(aaa);
"hello" // Chrome native
> new TextDecoderPolyfill().decode(aaa);
"wrong hello" // polyfill

A simple fix could be
var buffer = (inputArrayOrBuffer && inputArrayOrBuffer.buffer && inputArrayOrBuffer.buffer.slice(inputArrayOrBuffer.byteOffset, inputArrayOrBuffer.byteOffset + inputArrayOrBuffer.byteLength)) || inputArrayOrBuffer;
instead

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions