EUCKRDecoder.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { decoderError } from "../../encoding/encodings";
  2. import { finished } from "../../encoding/finished";
  3. import { index, indexCodePointFor } from "../../encoding/indexes";
  4. import { end_of_stream, isASCIIByte } from "../../encoding/terminology";
  5. import { inRange } from "../../encoding/utilities";
  6. /**
  7. * @constructor
  8. * @implements {Decoder}
  9. * @param {{fatal: boolean}} options
  10. */
  11. var EUCKRDecoder = /** @class */ (function () {
  12. function EUCKRDecoder(options) {
  13. this.fatal = options.fatal;
  14. // euc-kr's decoder has an associated euc-kr lead (initially 0x00).
  15. /** @type {number} */ this.euckr_lead = 0x00;
  16. }
  17. /**
  18. * @param {Stream} stream The stream of bytes being decoded.
  19. * @param {number} bite The next byte read from the stream.
  20. * @return {?(number|!Array.<number>)} The next code point(s)
  21. * decoded, or null if not enough data exists in the input
  22. * stream to decode a complete code point.
  23. */
  24. EUCKRDecoder.prototype.handler = function (stream, bite) {
  25. // 1. If byte is end-of-stream and euc-kr lead is not 0x00, set
  26. // euc-kr lead to 0x00 and return error.
  27. if (bite === end_of_stream && this.euckr_lead !== 0) {
  28. this.euckr_lead = 0x00;
  29. return decoderError(this.fatal);
  30. }
  31. // 2. If byte is end-of-stream and euc-kr lead is 0x00, return
  32. // finished.
  33. if (bite === end_of_stream && this.euckr_lead === 0)
  34. return finished;
  35. // 3. If euc-kr lead is not 0x00, let lead be euc-kr lead, let
  36. // pointer be null, set euc-kr lead to 0x00, and then run these
  37. // substeps:
  38. if (this.euckr_lead !== 0x00) {
  39. var lead = this.euckr_lead;
  40. var pointer = null;
  41. this.euckr_lead = 0x00;
  42. // 1. If byte is in the range 0x41 to 0xFE, inclusive, set
  43. // pointer to (lead − 0x81) × 190 + (byte − 0x41).
  44. if (inRange(bite, 0x41, 0xFE))
  45. pointer = (lead - 0x81) * 190 + (bite - 0x41);
  46. // 2. Let code point be null, if pointer is null, and the
  47. // index code point for pointer in index euc-kr otherwise.
  48. var code_point = (pointer === null)
  49. ? null : indexCodePointFor(pointer, index('euc-kr'));
  50. // 3. If code point is null and byte is an ASCII byte, prepend
  51. // byte to stream.
  52. if (pointer === null && isASCIIByte(bite))
  53. stream.prepend(bite);
  54. // 4. If code point is null, return error.
  55. if (code_point === null)
  56. return decoderError(this.fatal);
  57. // 5. Return a code point whose value is code point.
  58. return code_point;
  59. }
  60. // 4. If byte is an ASCII byte, return a code point whose value
  61. // is byte.
  62. if (isASCIIByte(bite))
  63. return bite;
  64. // 5. If byte is in the range 0x81 to 0xFE, inclusive, set
  65. // euc-kr lead to byte and return continue.
  66. if (inRange(bite, 0x81, 0xFE)) {
  67. this.euckr_lead = bite;
  68. return null;
  69. }
  70. // 6. Return error.
  71. return decoderError(this.fatal);
  72. };
  73. return EUCKRDecoder;
  74. }());
  75. export { EUCKRDecoder };
  76. //# sourceMappingURL=EUCKRDecoder.js.map