UTF8Encoder.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { finished } from "../../encoding/finished";
  2. import { end_of_stream, isASCIICodePoint } from "../../encoding/terminology";
  3. import { inRange } from "../../encoding/utilities";
  4. /**
  5. * @constructor
  6. * @implements {Encoder}
  7. * @param {{fatal: boolean}} options
  8. */
  9. var UTF8Encoder = /** @class */ (function () {
  10. function UTF8Encoder(options) {
  11. this.fatal = options.fatal;
  12. }
  13. /**
  14. * @param {Stream} stream Input stream.
  15. * @param {number} code_point Next code point read from the stream.
  16. * @return {(number|!Array.<number>)} Byte(s) to emit.
  17. */
  18. UTF8Encoder.prototype.handler = function (stream, code_point) {
  19. // 1. If code point is end-of-stream, return finished.
  20. if (code_point === end_of_stream)
  21. return finished;
  22. // 2. If code point is an ASCII code point, return a byte whose
  23. // value is code point.
  24. if (isASCIICodePoint(code_point))
  25. return code_point;
  26. // 3. Set count and offset based on the range code point is in:
  27. var count, offset;
  28. // U+0080 to U+07FF, inclusive:
  29. if (inRange(code_point, 0x0080, 0x07FF)) {
  30. // 1 and 0xC0
  31. count = 1;
  32. offset = 0xC0;
  33. }
  34. // U+0800 to U+FFFF, inclusive:
  35. else if (inRange(code_point, 0x0800, 0xFFFF)) {
  36. // 2 and 0xE0
  37. count = 2;
  38. offset = 0xE0;
  39. }
  40. // U+10000 to U+10FFFF, inclusive:
  41. else if (inRange(code_point, 0x10000, 0x10FFFF)) {
  42. // 3 and 0xF0
  43. count = 3;
  44. offset = 0xF0;
  45. }
  46. // 4. Let bytes be a byte sequence whose first byte is (code
  47. // point >> (6 × count)) + offset.
  48. var bytes = [(code_point >> (6 * count)) + offset];
  49. // 5. Run these substeps while count is greater than 0:
  50. while (count > 0) {
  51. // 1. Set temp to code point >> (6 × (count − 1)).
  52. var temp = code_point >> (6 * (count - 1));
  53. // 2. Append to bytes 0x80 | (temp & 0x3F).
  54. bytes.push(0x80 | (temp & 0x3F));
  55. // 3. Decrease count by one.
  56. count -= 1;
  57. }
  58. // 6. Return bytes bytes, in order.
  59. return bytes;
  60. };
  61. return UTF8Encoder;
  62. }());
  63. export { UTF8Encoder };
  64. //# sourceMappingURL=UTF8Encoder.js.map