UTF16Encoder.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var finished_1 = require("../../encoding/finished");
  4. var terminology_1 = require("../../encoding/terminology");
  5. var utilities_1 = require("../../encoding/utilities");
  6. var converCodeUnitToBytes_1 = require("./converCodeUnitToBytes");
  7. /**
  8. * @constructor
  9. * @implements {Encoder}
  10. * @param {boolean} utf16_be True if big-endian, false if little-endian.
  11. * @param {{fatal: boolean}} options
  12. */
  13. var UTF16Encoder = /** @class */ (function () {
  14. function UTF16Encoder(utf16_be, options) {
  15. this.utf16_be = utf16_be;
  16. this.fatal = options.fatal;
  17. }
  18. /**
  19. * @param {Stream} stream Input stream.
  20. * @param {number} code_point Next code point read from the stream.
  21. * @return {(number|!Array.<number>)} Byte(s) to emit.
  22. */
  23. UTF16Encoder.prototype.handler = function (stream, code_point) {
  24. // 1. If code point is end-of-stream, return finished.
  25. if (code_point === terminology_1.end_of_stream)
  26. return finished_1.finished;
  27. // 2. If code point is in the range U+0000 to U+FFFF, inclusive,
  28. // return the sequence resulting of converting code point to
  29. // bytes using utf-16be encoder flag.
  30. if (utilities_1.inRange(code_point, 0x0000, 0xFFFF))
  31. return converCodeUnitToBytes_1.convertCodeUnitToBytes(code_point, this.utf16_be);
  32. // 3. Let lead be ((code point − 0x10000) >> 10) + 0xD800,
  33. // converted to bytes using utf-16be encoder flag.
  34. var lead = converCodeUnitToBytes_1.convertCodeUnitToBytes(((code_point - 0x10000) >> 10) + 0xD800, this.utf16_be);
  35. // 4. Let trail be ((code point − 0x10000) & 0x3FF) + 0xDC00,
  36. // converted to bytes using utf-16be encoder flag.
  37. var trail = converCodeUnitToBytes_1.convertCodeUnitToBytes(((code_point - 0x10000) & 0x3FF) + 0xDC00, this.utf16_be);
  38. // 5. Return a byte sequence of lead followed by trail.
  39. return lead.concat(trail);
  40. };
  41. return UTF16Encoder;
  42. }());
  43. exports.UTF16Encoder = UTF16Encoder;
  44. //# sourceMappingURL=UTF16Encoder.js.map