EUCKRDecoder.js 3.4 KB

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