Big5Decoder.js 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 Big5Decoder = /** @class */ (function () {
  14. function Big5Decoder(options) {
  15. this.fatal = options.fatal;
  16. // Big5's decoder has an associated Big5 lead (initially 0x00).
  17. /** @type {number} */ this.Big5_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. Big5Decoder.prototype.handler = function (stream, bite) {
  27. // 1. If byte is end-of-stream and Big5 lead is not 0x00, set
  28. // Big5 lead to 0x00 and return error.
  29. if (bite === terminology_1.end_of_stream && this.Big5_lead !== 0x00) {
  30. this.Big5_lead = 0x00;
  31. return encodings_1.decoderError(this.fatal);
  32. }
  33. // 2. If byte is end-of-stream and Big5 lead is 0x00, return
  34. // finished.
  35. if (bite === terminology_1.end_of_stream && this.Big5_lead === 0x00)
  36. return finished_1.finished;
  37. // 3. If Big5 lead is not 0x00, let lead be Big5 lead, let
  38. // pointer be null, set Big5 lead to 0x00, and then run these
  39. // substeps:
  40. if (this.Big5_lead !== 0x00) {
  41. var lead = this.Big5_lead;
  42. var pointer = null;
  43. this.Big5_lead = 0x00;
  44. // 1. Let offset be 0x40 if byte is less than 0x7F and 0x62
  45. // otherwise.
  46. var offset = bite < 0x7F ? 0x40 : 0x62;
  47. // 2. If byte is in the range 0x40 to 0x7E, inclusive, or 0xA1
  48. // to 0xFE, inclusive, set pointer to (lead − 0x81) × 157 +
  49. // (byte − offset).
  50. if (utilities_1.inRange(bite, 0x40, 0x7E) || utilities_1.inRange(bite, 0xA1, 0xFE))
  51. pointer = (lead - 0x81) * 157 + (bite - offset);
  52. // 3. If there is a row in the table below whose first column
  53. // is pointer, return the two code points listed in its second
  54. // column
  55. // Pointer | Code points
  56. // --------+--------------
  57. // 1133 | U+00CA U+0304
  58. // 1135 | U+00CA U+030C
  59. // 1164 | U+00EA U+0304
  60. // 1166 | U+00EA U+030C
  61. switch (pointer) {
  62. case 1133: return [0x00CA, 0x0304];
  63. case 1135: return [0x00CA, 0x030C];
  64. case 1164: return [0x00EA, 0x0304];
  65. case 1166: return [0x00EA, 0x030C];
  66. }
  67. // 4. Let code point be null if pointer is null and the index
  68. // code point for pointer in index Big5 otherwise.
  69. var code_point = (pointer === null) ? null :
  70. indexes_1.indexCodePointFor(pointer, indexes_1.index('big5'));
  71. // 5. If code point is null and byte is an ASCII byte, prepend
  72. // byte to stream.
  73. if (code_point === null && terminology_1.isASCIIByte(bite))
  74. stream.prepend(bite);
  75. // 6. If code point is null, return error.
  76. if (code_point === null)
  77. return encodings_1.decoderError(this.fatal);
  78. // 7. Return a code point whose value is code point.
  79. return code_point;
  80. }
  81. // 4. If byte is an ASCII byte, return a code point whose value
  82. // is byte.
  83. if (terminology_1.isASCIIByte(bite))
  84. return bite;
  85. // 5. If byte is in the range 0x81 to 0xFE, inclusive, set Big5
  86. // lead to byte and return continue.
  87. if (utilities_1.inRange(bite, 0x81, 0xFE)) {
  88. this.Big5_lead = bite;
  89. return null;
  90. }
  91. // 6. Return error.
  92. return encodings_1.decoderError(this.fatal);
  93. };
  94. return Big5Decoder;
  95. }());
  96. exports.Big5Decoder = Big5Decoder;
  97. //# sourceMappingURL=Big5Decoder.js.map