ShiftJISDecoder.js 4.3 KB

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