Skip to content

What's the correct use of {stream: true} option for Textdecoder.decode #184

Closed

Description

@lin7sh

Just came cross there is a second option can be passed to decoder, My first thought about it is a buffer, so I can do

 decoder.decode( new Uint8ArrayBuffer([97, 98, 99]), { stream: true })

and append another buffer

 decoder.decode( new Uint8ArrayBuffer([100,  101 , 102]), { stream: true })

and finally get me a string back which include the 2 parts

 const finalString = decoder.decode( new Uint8ArrayBuffer([]) , { stream: false })

but it isn't the case
the first two expression return "abc" and "def" the third give me empty, just like them without the stream option. and I've tried TextDecoder in Nodejs, it have the same behaviour

Can anyone can tell me how to use it correctly?

Activity

  1. ricea commented on Sep 4, 2019

    @ricea
    Collaborator

    The stream option changes the handling of the end of the input to allow it to be in the middle of a character. Compare:

    decoder.decode(new Uint8Array([226, 153]), { stream: true });
    // ""
    decoder.decode(new Uint8Array([165]), { stream: true });
    // "♥"

    to

    decoder.decode(new Uint8Array([226, 153]));
    // "��"
    decoder.decode(new Uint8Array([165]));
    // "�"

    Even with {stream: true} the TextDecoder emits all complete characters as soon as possible.

    You may find TextDecoderStream a more intuitive way to do the same thing.

  2. lin7sh commented on Sep 4, 2019

    @lin7sh
    Author

    @ricea So it's for multi-byte decoding, that make sense and TextDecoderStream is what I'm looking for, hopefully it'll be available soon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions