ShiftJISDecoder.js 4.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 ShiftJISDecoder = /** @class */ (function () {
  14. function ShiftJISDecoder(options) {
  15. this.fatal = options.fatal;
  16. // Shift_JIS's decoder has an associated Shift_JIS lead (initially
  17. // 0x00).
  18. /** @type {number} */ this.Shift_JIS_lead = 0x00;
  19. }
  20. /**
  21. * @param {Stream} stream The stream of bytes being decoded.
  22. * @param {number} bite The next byte read from the stream.
  23. * @return {?(number|!Array.<number>)} The next code point(s)
  24. * decoded, or null if not enough data exists in the input
  25. * stream to decode a complete code point.
  26. */
  27. ShiftJISDecoder.prototype.handler = function (stream, bite) {
  28. // 1. If byte is end-of-stream and Shift_JIS lead is not 0x00,
  29. // set Shift_JIS lead to 0x00 and return error.
  30. if (bite === terminology_1.end_of_stream && this.Shift_JIS_lead !== 0x00) {
  31. this.Shift_JIS_lead = 0x00;
  32. return encodings_1.decoderError(this.fatal);
  33. }
  34. // 2. If byte is end-of-stream and Shift_JIS lead is 0x00,
  35. // return finished.
  36. if (bite === terminology_1.end_of_stream && this.Shift_JIS_lead === 0x00)
  37. return finished_1.finished;
  38. // 3. If Shift_JIS lead is not 0x00, let lead be Shift_JIS lead,
  39. // let pointer be null, set Shift_JIS lead to 0x00, and then run
  40. // these substeps:
  41. if (this.Shift_JIS_lead !== 0x00) {
  42. var lead = this.Shift_JIS_lead;
  43. var pointer = null;
  44. this.Shift_JIS_lead = 0x00;
  45. // 1. Let offset be 0x40, if byte is less than 0x7F, and 0x41
  46. // otherwise.
  47. var offset = (bite < 0x7F) ? 0x40 : 0x41;
  48. // 2. Let lead offset be 0x81, if lead is less than 0xA0, and
  49. // 0xC1 otherwise.
  50. var lead_offset = (lead < 0xA0) ? 0x81 : 0xC1;
  51. // 3. If byte is in the range 0x40 to 0x7E, inclusive, or 0x80
  52. // to 0xFC, inclusive, set pointer to (lead − lead offset) ×
  53. // 188 + byte − offset.
  54. if (utilities_1.inRange(bite, 0x40, 0x7E) || utilities_1.inRange(bite, 0x80, 0xFC))
  55. pointer = (lead - lead_offset) * 188 + bite - offset;
  56. // 4. If pointer is in the range 8836 to 10715, inclusive,
  57. // return a code point whose value is 0xE000 − 8836 + pointer.
  58. if (utilities_1.inRange(pointer, 8836, 10715))
  59. return 0xE000 - 8836 + pointer;
  60. // 5. Let code point be null, if pointer is null, and the
  61. // index code point for pointer in index jis0208 otherwise.
  62. var code_point = (pointer === null) ? null :
  63. indexes_1.indexCodePointFor(pointer, indexes_1.index('jis0208'));
  64. // 6. If code point is null and byte is an ASCII byte, prepend
  65. // byte to stream.
  66. if (code_point === null && terminology_1.isASCIIByte(bite))
  67. stream.prepend(bite);
  68. // 7. If code point is null, return error.
  69. if (code_point === null)
  70. return encodings_1.decoderError(this.fatal);
  71. // 8. Return a code point whose value is code point.
  72. return code_point;
  73. }
  74. // 4. If byte is an ASCII byte or 0x80, return a code point
  75. // whose value is byte.
  76. if (terminology_1.isASCIIByte(bite) || bite === 0x80)
  77. return bite;
  78. // 5. If byte is in the range 0xA1 to 0xDF, inclusive, return a
  79. // code point whose value is 0xFF61 − 0xA1 + byte.
  80. if (utilities_1.inRange(bite, 0xA1, 0xDF))
  81. return 0xFF61 - 0xA1 + bite;
  82. // 6. If byte is in the range 0x81 to 0x9F, inclusive, or 0xE0
  83. // to 0xFC, inclusive, set Shift_JIS lead to byte and return
  84. // continue.
  85. if (utilities_1.inRange(bite, 0x81, 0x9F) || utilities_1.inRange(bite, 0xE0, 0xFC)) {
  86. this.Shift_JIS_lead = bite;
  87. return null;
  88. }
  89. // 7. Return error.
  90. return encodings_1.decoderError(this.fatal);
  91. };
  92. return ShiftJISDecoder;
  93. }());
  94. exports.ShiftJISDecoder = ShiftJISDecoder;
  95. //# sourceMappingURL=ShiftJISDecoder.js.map