Chromium Code Reviews| 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 12 matching lines...) Expand all Loading... | |
| 23 for (var i = cp; i < cp + BATCH_SIZE && cp < 0x10FFFF; i += SKIP_SIZE) { | 23 for (var i = cp; i < cp + BATCH_SIZE && cp < 0x10FFFF; i += SKIP_SIZE) { |
| 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 | |
|
jsbell
2016/05/02 20:20:06
nit: remove this extra whitespace; one or two blan
| |
| 34 | |
| 33 utf_encodings.forEach(function(encoding) { | 35 utf_encodings.forEach(function(encoding) { |
|
jsbell
2016/05/02 20:20:06
Maybe just split this into a test for utf-8 and th
lpan
2016/05/03 13:42:00
Acknowledged. Will do once I figure out how to han
jsbell
2016/05/03 22:22:48
function encode_utf16(s, littleEndian) {
var a =
| |
| 34 test(function() { | 36 var utf16_exception; |
|
jsbell
2016/05/02 20:20:06
this is unused - remove?
lpan
2016/05/03 13:42:00
Done.
| |
| 35 for (var i = 0; i < 0x10FFFF; i += BATCH_SIZE) { | 37 if (encoding == "utf-8") { |
|
jsbell
2016/05/02 20:20:06
nit: prefer ===
lpan
2016/05/03 13:42:00
Acknowledged.
| |
| 36 var string = makeBatch(i); | 38 test(function() { |
| 37 var encoded = new TextEncoder(encoding).encode(string); | 39 for (var i = 0; i < 0x10FFFF; i += BATCH_SIZE) { |
| 38 var decoded = new TextDecoder(encoding).decode(encoded); | 40 var string = makeBatch(i); |
| 39 assert_equals(decoded, string); | 41 var encoded = new TextEncoder(encoding).encode(string); |
|
jsbell
2016/05/02 20:20:06
Don't pass argument to TextEncoder
lpan
2016/05/03 13:42:01
Acknowledged.
| |
| 40 } | 42 var decoded = new TextDecoder(encoding).decode(encoded); |
| 41 }, encoding + ' - encode/decode round trip'); | 43 assert_equals(string, decoded); |
| 44 } | |
| 45 }, encoding + ' - encode/decode round trip'); | |
| 46 } | |
| 47 else { | |
|
jsbell
2016/05/02 20:20:06
nit: format as `} else {`
lpan
2016/05/03 13:42:00
Acknowledged.
| |
| 48 test(function() { | |
| 49 for (var i = 0; i < 0x10FFFF; i += BATCH_SIZE) { | |
| 50 var string = makeBatch(i); | |
| 51 var encoded = new TextEncoder(encoding).encode(string); | |
|
jsbell
2016/05/02 20:20:06
Since encoding is ignored, how is this test intend
lpan
2016/05/03 13:42:00
This was my (poorly thought out) attempt at showin
jsbell
2016/05/03 22:22:48
Right, it's no longer round-trip, so maybe look to
| |
| 52 var decoded = new TextDecoder(encoding).decode(encoded); | |
| 53 assert_not_equals(encoded, decoded); | |
| 54 } | |
| 55 }, encoding + ' - legacy encoding not supported'); | |
| 56 } | |
| 57 | |
| 42 }); | 58 }); |
| 43 | 59 |
| 44 | 60 |
| 45 // Inspired by: | 61 // Inspired by: |
| 46 // http://ecmanaut.blogspot.com/2006/07/encoding-decoding-utf8-in-javascript.htm l | 62 // http://ecmanaut.blogspot.com/2006/07/encoding-decoding-utf8-in-javascript.htm l |
| 47 function encode_utf8(string) { | 63 function encode_utf8(string) { |
| 48 var utf8 = unescape(encodeURIComponent(string)); | 64 var utf8 = unescape(encodeURIComponent(string)); |
| 49 var octets = []; | 65 var octets = []; |
| 50 for (var i = 0; i < utf8.length; i += 1) | 66 for (var i = 0; i < utf8.length; i += 1) |
| 51 octets.push(utf8.charCodeAt(i)); | 67 octets.push(utf8.charCodeAt(i)); |
| 52 return octets; | 68 return octets; |
| 53 } | 69 } |
| 54 | 70 |
| 55 function decode_utf8(octets) { | 71 function decode_utf8(octets) { |
| 56 var utf8 = String.fromCharCode.apply(null, octets); | 72 var utf8 = String.fromCharCode.apply(null, octets); |
| 57 return decodeURIComponent(escape(utf8)); | 73 return decodeURIComponent(escape(utf8)); |
| 58 } | 74 } |
| 59 | 75 |
| 60 test(function() { | 76 test(function() { |
| 61 for (var i = 0; i < 0x10FFFF; i += BATCH_SIZE) { | 77 for (var i = 0; i < 0x10FFFF; i += BATCH_SIZE) { |
| 62 var string = makeBatch(i); | 78 var string = makeBatch(i); |
| 63 var expected = encode_utf8(string); | 79 var expected = encode_utf8(string); |
| 64 var actual = new TextEncoder('UTF-8').encode(string); | 80 var actual = new TextEncoder().encode(string); |
| 65 assert_array_equals(actual, expected); | 81 assert_array_equals(actual, expected); |
| 66 } | 82 } |
| 67 }, 'UTF-8 encoding (compare against unescape/encodeURIComponent)'); | 83 }, 'UTF-8 encoding (compare against unescape/encodeURIComponent)'); |
| 68 | 84 |
| 69 test(function() { | 85 test(function() { |
| 70 for (var i = 0; i < 0x10FFFF; i += BATCH_SIZE) { | 86 for (var i = 0; i < 0x10FFFF; i += BATCH_SIZE) { |
| 71 var string = makeBatch(i); | 87 var string = makeBatch(i); |
| 72 var encoded = encode_utf8(string); | 88 var encoded = encode_utf8(string); |
| 73 var expected = decode_utf8(encoded); | 89 var expected = decode_utf8(encoded); |
| 74 var actual = new TextDecoder('UTF-8').decode(new Uint8Array(encoded)); | 90 var actual = new TextDecoder().decode(new Uint8Array(encoded)); |
| 75 assert_equals(actual, expected); | 91 assert_equals(actual, expected); |
| 76 } | 92 } |
| 77 }, 'UTF-8 decoding (compare against decodeURIComponent/escape)'); | 93 }, 'UTF-8 decoding (compare against decodeURIComponent/escape)'); |
| 78 | 94 |
| 79 </script> | 95 </script> |
| OLD | NEW |