UTF16Decoder.js 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var encodings_1 = require("../../encoding/encodings");
  4. var finished_1 = require("../../encoding/finished");
  5. var terminology_1 = require("../../encoding/terminology");
  6. var utilities_1 = require("../../encoding/utilities");
  7. var converCodeUnitToBytes_1 = require("./converCodeUnitToBytes");
  8. /**
  9. * @constructor
  10. * @implements {Decoder}
  11. * @param {boolean} utf16_be True if big-endian, false if little-endian.
  12. * @param {{fatal: boolean}} options
  13. */
  14. var UTF16Decoder = /** @class */ (function () {
  15. function UTF16Decoder(utf16_be, options) {
  16. this.utf16_be = utf16_be;
  17. this.fatal = options.fatal;
  18. /** @type {?number} */ this.utf16_lead_byte = null;
  19. /** @type {?number} */ this.utf16_lead_surrogate = null;
  20. }
  21. /**
  22. * @param {Stream} stream The stream of bytes being decoded.
  23. * @param {number} bite The next byte read from the stream.
  24. * @return {?(number|!Array.<number>)} The next code point(s)
  25. * decoded, or null if not enough data exists in the input
  26. * stream to decode a complete code point.
  27. */
  28. UTF16Decoder.prototype.handler = function (stream, bite) {
  29. // 1. If byte is end-of-stream and either utf-16 lead byte or
  30. // utf-16 lead surrogate is not null, set utf-16 lead byte and
  31. // utf-16 lead surrogate to null, and return error.
  32. if (bite === terminology_1.end_of_stream && (this.utf16_lead_byte !== null ||
  33. this.utf16_lead_surrogate !== null)) {
  34. return encodings_1.decoderError(this.fatal);
  35. }
  36. // 2. If byte is end-of-stream and utf-16 lead byte and utf-16
  37. // lead surrogate are null, return finished.
  38. if (bite === terminology_1.end_of_stream && this.utf16_lead_byte === null &&
  39. this.utf16_lead_surrogate === null) {
  40. return finished_1.finished;
  41. }
  42. // 3. If utf-16 lead byte is null, set utf-16 lead byte to byte
  43. // and return continue.
  44. if (this.utf16_lead_byte === null) {
  45. this.utf16_lead_byte = bite;
  46. return null;
  47. }
  48. // 4. Let code unit be the result of:
  49. var code_unit;
  50. if (this.utf16_be) {
  51. // utf-16be decoder flag is set
  52. // (utf-16 lead byte << 8) + byte.
  53. code_unit = (this.utf16_lead_byte << 8) + bite;
  54. }
  55. else {
  56. // utf-16be decoder flag is unset
  57. // (byte << 8) + utf-16 lead byte.
  58. code_unit = (bite << 8) + this.utf16_lead_byte;
  59. }
  60. // Then set utf-16 lead byte to null.
  61. this.utf16_lead_byte = null;
  62. // 5. If utf-16 lead surrogate is not null, let lead surrogate
  63. // be utf-16 lead surrogate, set utf-16 lead surrogate to null,
  64. // and then run these substeps:
  65. if (this.utf16_lead_surrogate !== null) {
  66. var lead_surrogate = this.utf16_lead_surrogate;
  67. this.utf16_lead_surrogate = null;
  68. // 1. If code unit is in the range U+DC00 to U+DFFF,
  69. // inclusive, return a code point whose value is 0x10000 +
  70. // ((lead surrogate − 0xD800) << 10) + (code unit − 0xDC00).
  71. if (utilities_1.inRange(code_unit, 0xDC00, 0xDFFF)) {
  72. return 0x10000 + (lead_surrogate - 0xD800) * 0x400 +
  73. (code_unit - 0xDC00);
  74. }
  75. // 2. Prepend the sequence resulting of converting code unit
  76. // to bytes using utf-16be decoder flag to stream and return
  77. // error.
  78. stream.prepend(converCodeUnitToBytes_1.convertCodeUnitToBytes(code_unit, this.utf16_be));
  79. return encodings_1.decoderError(this.fatal);
  80. }
  81. // 6. If code unit is in the range U+D800 to U+DBFF, inclusive,
  82. // set utf-16 lead surrogate to code unit and return continue.
  83. if (utilities_1.inRange(code_unit, 0xD800, 0xDBFF)) {
  84. this.utf16_lead_surrogate = code_unit;
  85. return null;
  86. }
  87. // 7. If code unit is in the range U+DC00 to U+DFFF, inclusive,
  88. // return error.
  89. if (utilities_1.inRange(code_unit, 0xDC00, 0xDFFF))
  90. return encodings_1.decoderError(this.fatal);
  91. // 8. Return code point code unit.
  92. return code_unit;
  93. };
  94. return UTF16Decoder;
  95. }());
  96. exports.UTF16Decoder = UTF16Decoder;
  97. //# sourceMappingURL=UTF16Decoder.js.map