Big5Encoder.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { encoderError } from "../../encoding/encodings";
  2. import { finished } from "../../encoding/finished";
  3. import { indexBig5PointerFor } from "../../encoding/indexes";
  4. import { end_of_stream, isASCIICodePoint } from "../../encoding/terminology";
  5. /**
  6. * @constructor
  7. * @implements {Encoder}
  8. * @param {{fatal: boolean}} options
  9. */
  10. var Big5Encoder = /** @class */ (function () {
  11. function Big5Encoder(options) {
  12. this.fatal = options.fatal;
  13. }
  14. /**
  15. * @param {Stream} stream Input stream.
  16. * @param {number} code_point Next code point read from the stream.
  17. * @return {(number|!Array.<number>)} Byte(s) to emit.
  18. */
  19. Big5Encoder.prototype.handler = function (stream, code_point) {
  20. // 1. If code point is end-of-stream, return finished.
  21. if (code_point === end_of_stream)
  22. return finished;
  23. // 2. If code point is an ASCII code point, return a byte whose
  24. // value is code point.
  25. if (isASCIICodePoint(code_point))
  26. return code_point;
  27. // 3. Let pointer be the index Big5 pointer for code point.
  28. var pointer = indexBig5PointerFor(code_point);
  29. // 4. If pointer is null, return error with code point.
  30. if (pointer === null)
  31. return encoderError(code_point);
  32. // 5. Let lead be Math.floor(pointer / 157) + 0x81.
  33. var lead = Math.floor(pointer / 157) + 0x81;
  34. // 6. If lead is less than 0xA1, return error with code point.
  35. if (lead < 0xA1)
  36. return encoderError(code_point);
  37. // 7. Let trail be pointer % 157.
  38. var trail = pointer % 157;
  39. // 8. Let offset be 0x40 if trail is less than 0x3F and 0x62
  40. // otherwise.
  41. var offset = trail < 0x3F ? 0x40 : 0x62;
  42. // Return two bytes whose values are lead and trail + offset.
  43. return [lead, trail + offset];
  44. };
  45. return Big5Encoder;
  46. }());
  47. export { Big5Encoder };
  48. //# sourceMappingURL=Big5Encoder.js.map