1// META: title=Encoding API: Streaming decode 2// META: script=resources/encodings.js 3 4var string = '\x00123ABCabc\x80\xFF\u0100\u1000\uFFFD\uD800\uDC00\uDBFF\uDFFF'; 5var octets = { 6 'utf-8': [0x00,0x31,0x32,0x33,0x41,0x42,0x43,0x61,0x62,0x63,0xc2,0x80, 7 0xc3,0xbf,0xc4,0x80,0xe1,0x80,0x80,0xef,0xbf,0xbd,0xf0,0x90, 8 0x80,0x80,0xf4,0x8f,0xbf,0xbf], 9 'utf-16le': [0x00,0x00,0x31,0x00,0x32,0x00,0x33,0x00,0x41,0x00,0x42,0x00, 10 0x43,0x00,0x61,0x00,0x62,0x00,0x63,0x00,0x80,0x00,0xFF,0x00, 11 0x00,0x01,0x00,0x10,0xFD,0xFF,0x00,0xD8,0x00,0xDC,0xFF,0xDB, 12 0xFF,0xDF], 13 'utf-16be': [0x00,0x00,0x00,0x31,0x00,0x32,0x00,0x33,0x00,0x41,0x00,0x42, 14 0x00,0x43,0x00,0x61,0x00,0x62,0x00,0x63,0x00,0x80,0x00,0xFF, 15 0x01,0x00,0x10,0x00,0xFF,0xFD,0xD8,0x00,0xDC,0x00,0xDB,0xFF, 16 0xDF,0xFF] 17}; 18 19Object.keys(octets).forEach(function(encoding) { 20 for (var len = 1; len <= 5; ++len) { 21 test(function() { 22 var encoded = octets[encoding]; 23 24 var out = ''; 25 var decoder = new TextDecoder(encoding); 26 for (var i = 0; i < encoded.length; i += len) { 27 var sub = []; 28 for (var j = i; j < encoded.length && j < i + len; ++j) 29 sub.push(encoded[j]); 30 out += decoder.decode(new Uint8Array(sub), {stream: true}); 31 } 32 out += decoder.decode(); 33 assert_equals(out, string); 34 }, 'Streaming decode: ' + encoding + ', ' + len + ' byte window'); 35 } 36}); 37