• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3// From: https://github.com/w3c/web-platform-tests/blob/fa9436d12c/encoding/textdecoder-streaming.html
4// This is the part that can be run without ICU
5
6require('../common');
7
8const assert = require('assert');
9
10const string =
11  '\x00123ABCabc\x80\xFF\u0100\u1000\uFFFD\uD800\uDC00\uDBFF\uDFFF';
12const octets = {
13  'utf-8': [
14    0x00, 0x31, 0x32, 0x33, 0x41, 0x42, 0x43, 0x61, 0x62, 0x63, 0xc2, 0x80,
15    0xc3, 0xbf, 0xc4, 0x80, 0xe1, 0x80, 0x80, 0xef, 0xbf, 0xbd, 0xf0, 0x90,
16    0x80, 0x80, 0xf4, 0x8f, 0xbf, 0xbf],
17  'utf-16le': [
18    0x00, 0x00, 0x31, 0x00, 0x32, 0x00, 0x33, 0x00, 0x41, 0x00, 0x42, 0x00,
19    0x43, 0x00, 0x61, 0x00, 0x62, 0x00, 0x63, 0x00, 0x80, 0x00, 0xFF, 0x00,
20    0x00, 0x01, 0x00, 0x10, 0xFD, 0xFF, 0x00, 0xD8, 0x00, 0xDC, 0xFF, 0xDB,
21    0xFF, 0xDF]
22};
23
24Object.keys(octets).forEach((encoding) => {
25  for (let len = 1; len <= 5; ++len) {
26    const encoded = octets[encoding];
27    const decoder = new TextDecoder(encoding);
28    let out = '';
29    for (let i = 0; i < encoded.length; i += len) {
30      const sub = [];
31      for (let j = i; j < encoded.length && j < i + len; ++j)
32        sub.push(encoded[j]);
33      out += decoder.decode(new Uint8Array(sub), { stream: true });
34    }
35    out += decoder.decode();
36    assert.strictEqual(out, string);
37  }
38});
39