| OLD | NEW |
| 1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
| 2 <title>Encoding API: UTF encoding round trips</title> | 2 <title>Encoding API: UTF encoding round trips</title> |
| 3 <script src="../../../resources/testharness.js"></script> | 3 <script src="../../../resources/testharness.js"></script> |
| 4 <script src="../../../resources/testharnessreport.js"></script> | 4 <script src="../../../resources/testharnessreport.js"></script> |
| 5 <script src="resources/encodings.js"></script> | 5 <script src="resources/encodings.js"></script> |
| 6 <script> | 6 <script> |
| 7 | 7 |
| 8 var BATCH_SIZE = 0x1000; // Convert in batches spanning this many code points. | 8 var BATCH_SIZE = 0x1000; // Convert in batches spanning this many code points. |
| 9 var SKIP_SIZE = 0x77; // For efficiency, don't test every code point. | 9 var SKIP_SIZE = 0x77; // For efficiency, don't test every code point. |
| 10 | 10 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 if (0xD800 <= i && i <= 0xDFFF) { | 24 if (0xD800 <= i && i <= 0xDFFF) { |
| 25 // surrogate half | 25 // surrogate half |
| 26 continue; | 26 continue; |
| 27 } | 27 } |
| 28 string += fromCodePoint(i); | 28 string += fromCodePoint(i); |
| 29 } | 29 } |
| 30 return string; | 30 return string; |
| 31 } | 31 } |
| 32 | 32 |
| 33 utf_encodings.forEach(function(encoding) { | 33 utf_encodings.forEach(function(encoding) { |
| 34 if (encoding === 'utf-8') { |
| 35 test(function() { |
| 36 for (var i = 0; i < 0x10FFFF; i += BATCH_SIZE) { |
| 37 var string = makeBatch(i); |
| 38 var encoded = new TextEncoder().encode(string); |
| 39 var decoded = new TextDecoder(encoding).decode(encoded); |
| 40 assert_equals(string, decoded); |
| 41 } |
| 42 }, encoding + ' - encode/decode round trip'); |
| 43 } |
| 44 }); |
| 45 |
| 46 ['utf-16le', 'utf-16be'].forEach(function(encoding) { |
| 34 test(function() { | 47 test(function() { |
| 35 for (var i = 0; i < 0x10FFFF; i += BATCH_SIZE) { | 48 for (var i = 0; i < 0x10FFFF; i += BATCH_SIZE) { |
| 36 var string = makeBatch(i); | 49 var string = makeBatch(i); |
| 37 var encoded = new TextEncoder(encoding).encode(string); | 50 |
| 51 if (encoding === 'utf-16le') |
| 52 var encoded = encode_utf16(string, true); |
| 53 else |
| 54 var encoded = encode_utf16(string, false); |
| 55 |
| 38 var decoded = new TextDecoder(encoding).decode(encoded); | 56 var decoded = new TextDecoder(encoding).decode(encoded); |
| 39 assert_equals(decoded, string); | 57 assert_equals(string, decoded); |
| 40 } | 58 } |
| 41 }, encoding + ' - encode/decode round trip'); | 59 }, encoding + ' - encode/decode round trip'); |
| 42 }); | 60 }); |
| 43 | 61 |
| 62 function encode_utf16(s, littleEndian) { |
| 63 var a = new Uint8Array(s.length * 2), view = new DataView(a.buffer); |
| 64 s.split('').forEach(function(c, i) { |
| 65 view.setUint16(i * 2, c.charCodeAt(0), littleEndian); |
| 66 }); |
| 67 return a; |
| 68 } |
| 44 | 69 |
| 45 // Inspired by: | 70 // Inspired by: |
| 46 // http://ecmanaut.blogspot.com/2006/07/encoding-decoding-utf8-in-javascript.htm
l | 71 // http://ecmanaut.blogspot.com/2006/07/encoding-decoding-utf8-in-javascript.htm
l |
| 47 function encode_utf8(string) { | 72 function encode_utf8(string) { |
| 48 var utf8 = unescape(encodeURIComponent(string)); | 73 var utf8 = unescape(encodeURIComponent(string)); |
| 49 var octets = []; | 74 var octets = []; |
| 50 for (var i = 0; i < utf8.length; i += 1) | 75 for (var i = 0; i < utf8.length; i += 1) |
| 51 octets.push(utf8.charCodeAt(i)); | 76 octets.push(utf8.charCodeAt(i)); |
| 52 return octets; | 77 return octets; |
| 53 } | 78 } |
| 54 | 79 |
| 55 function decode_utf8(octets) { | 80 function decode_utf8(octets) { |
| 56 var utf8 = String.fromCharCode.apply(null, octets); | 81 var utf8 = String.fromCharCode.apply(null, octets); |
| 57 return decodeURIComponent(escape(utf8)); | 82 return decodeURIComponent(escape(utf8)); |
| 58 } | 83 } |
| 59 | 84 |
| 60 test(function() { | 85 test(function() { |
| 61 for (var i = 0; i < 0x10FFFF; i += BATCH_SIZE) { | 86 for (var i = 0; i < 0x10FFFF; i += BATCH_SIZE) { |
| 62 var string = makeBatch(i); | 87 var string = makeBatch(i); |
| 63 var expected = encode_utf8(string); | 88 var expected = encode_utf8(string); |
| 64 var actual = new TextEncoder('UTF-8').encode(string); | 89 var actual = new TextEncoder().encode(string); |
| 65 assert_array_equals(actual, expected); | 90 assert_array_equals(actual, expected); |
| 66 } | 91 } |
| 67 }, 'UTF-8 encoding (compare against unescape/encodeURIComponent)'); | 92 }, 'UTF-8 encoding (compare against unescape/encodeURIComponent)'); |
| 68 | 93 |
| 69 test(function() { | 94 test(function() { |
| 70 for (var i = 0; i < 0x10FFFF; i += BATCH_SIZE) { | 95 for (var i = 0; i < 0x10FFFF; i += BATCH_SIZE) { |
| 71 var string = makeBatch(i); | 96 var string = makeBatch(i); |
| 72 var encoded = encode_utf8(string); | 97 var encoded = encode_utf8(string); |
| 73 var expected = decode_utf8(encoded); | 98 var expected = decode_utf8(encoded); |
| 74 var actual = new TextDecoder('UTF-8').decode(new Uint8Array(encoded)); | 99 var actual = new TextDecoder().decode(new Uint8Array(encoded)); |
| 75 assert_equals(actual, expected); | 100 assert_equals(actual, expected); |
| 76 } | 101 } |
| 77 }, 'UTF-8 decoding (compare against decodeURIComponent/escape)'); | 102 }, 'UTF-8 decoding (compare against decodeURIComponent/escape)'); |
| 78 | 103 </script> |
| 79 </script> | |
| OLD | NEW |