Big5Encoder.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. export class Big5Encoder {
  11. constructor(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. handler(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. const 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. const 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. const trail = pointer % 157;
  39. // 8. Let offset be 0x40 if trail is less than 0x3F and 0x62
  40. // otherwise.
  41. const offset = trail < 0x3F ? 0x40 : 0x62;
  42. // Return two bytes whose values are lead and trail + offset.
  43. return [lead, trail + offset];
  44. }
  45. }
  46. //# sourceMappingURL=Big5Encoder.js.map