GB18030Decoder.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 GB18030Decoder = /** @class */ (function () {
  14. function GB18030Decoder(options) {
  15. this.fatal = options.fatal;
  16. // gb18030's decoder has an associated gb18030 first, gb18030
  17. // second, and gb18030 third (all initially 0x00).
  18. /** @type {number} */ this.gb18030_first = 0x00,
  19. /** @type {number} */ this.gb18030_second = 0x00,
  20. /** @type {number} */ this.gb18030_third = 0x00;
  21. }
  22. /**
  23. * @param {Stream} stream The stream of bytes being decoded.
  24. * @param {number} bite The next byte read from the stream.
  25. * @return {?(number|!Array.<number>)} The next code point(s)
  26. * decoded, or null if not enough data exists in the input
  27. * stream to decode a complete code point.
  28. */
  29. GB18030Decoder.prototype.handler = function (stream, bite) {
  30. // 1. If byte is end-of-stream and gb18030 first, gb18030
  31. // second, and gb18030 third are 0x00, return finished.
  32. if (bite === terminology_1.end_of_stream && this.gb18030_first === 0x00 &&
  33. this.gb18030_second === 0x00 && this.gb18030_third === 0x00) {
  34. return finished_1.finished;
  35. }
  36. // 2. If byte is end-of-stream, and gb18030 first, gb18030
  37. // second, or gb18030 third is not 0x00, set gb18030 first,
  38. // gb18030 second, and gb18030 third to 0x00, and return error.
  39. if (bite === terminology_1.end_of_stream &&
  40. (this.gb18030_first !== 0x00 || this.gb18030_second !== 0x00 ||
  41. this.gb18030_third !== 0x00)) {
  42. this.gb18030_first = 0x00;
  43. this.gb18030_second = 0x00;
  44. this.gb18030_third = 0x00;
  45. encodings_1.decoderError(this.fatal);
  46. }
  47. var code_point;
  48. // 3. If gb18030 third is not 0x00, run these substeps:
  49. if (this.gb18030_third !== 0x00) {
  50. // 1. Let code point be null.
  51. code_point = null;
  52. // 2. If byte is in the range 0x30 to 0x39, inclusive, set
  53. // code point to the index gb18030 ranges code point for
  54. // (((gb18030 first − 0x81) × 10 + gb18030 second − 0x30) ×
  55. // 126 + gb18030 third − 0x81) × 10 + byte − 0x30.
  56. if (utilities_1.inRange(bite, 0x30, 0x39)) {
  57. code_point = indexes_1.indexGB18030RangesCodePointFor((((this.gb18030_first - 0x81) * 10 + this.gb18030_second - 0x30) * 126 +
  58. this.gb18030_third - 0x81) * 10 + bite - 0x30);
  59. }
  60. // 3. Let buffer be a byte sequence consisting of gb18030
  61. // second, gb18030 third, and byte, in order.
  62. var buffer = [this.gb18030_second, this.gb18030_third, bite];
  63. // 4. Set gb18030 first, gb18030 second, and gb18030 third to
  64. // 0x00.
  65. this.gb18030_first = 0x00;
  66. this.gb18030_second = 0x00;
  67. this.gb18030_third = 0x00;
  68. // 5. If code point is null, prepend buffer to stream and
  69. // return error.
  70. if (code_point === null) {
  71. stream.prepend(buffer);
  72. return encodings_1.decoderError(this.fatal);
  73. }
  74. // 6. Return a code point whose value is code point.
  75. return code_point;
  76. }
  77. // 4. If gb18030 second is not 0x00, run these substeps:
  78. if (this.gb18030_second !== 0x00) {
  79. // 1. If byte is in the range 0x81 to 0xFE, inclusive, set
  80. // gb18030 third to byte and return continue.
  81. if (utilities_1.inRange(bite, 0x81, 0xFE)) {
  82. this.gb18030_third = bite;
  83. return null;
  84. }
  85. // 2. Prepend gb18030 second followed by byte to stream, set
  86. // gb18030 first and gb18030 second to 0x00, and return error.
  87. stream.prepend([this.gb18030_second, bite]);
  88. this.gb18030_first = 0x00;
  89. this.gb18030_second = 0x00;
  90. return encodings_1.decoderError(this.fatal);
  91. }
  92. // 5. If gb18030 first is not 0x00, run these substeps:
  93. if (this.gb18030_first !== 0x00) {
  94. // 1. If byte is in the range 0x30 to 0x39, inclusive, set
  95. // gb18030 second to byte and return continue.
  96. if (utilities_1.inRange(bite, 0x30, 0x39)) {
  97. this.gb18030_second = bite;
  98. return null;
  99. }
  100. // 2. Let lead be gb18030 first, let pointer be null, and set
  101. // gb18030 first to 0x00.
  102. var lead = this.gb18030_first;
  103. var pointer = null;
  104. this.gb18030_first = 0x00;
  105. // 3. Let offset be 0x40 if byte is less than 0x7F and 0x41
  106. // otherwise.
  107. var offset = bite < 0x7F ? 0x40 : 0x41;
  108. // 4. If byte is in the range 0x40 to 0x7E, inclusive, or 0x80
  109. // to 0xFE, inclusive, set pointer to (lead − 0x81) × 190 +
  110. // (byte − offset).
  111. if (utilities_1.inRange(bite, 0x40, 0x7E) || utilities_1.inRange(bite, 0x80, 0xFE))
  112. pointer = (lead - 0x81) * 190 + (bite - offset);
  113. // 5. Let code point be null if pointer is null and the index
  114. // code point for pointer in index gb18030 otherwise.
  115. code_point = pointer === null ? null :
  116. indexes_1.indexCodePointFor(pointer, indexes_1.index('gb18030'));
  117. // 6. If code point is null and byte is an ASCII byte, prepend
  118. // byte to stream.
  119. if (code_point === null && terminology_1.isASCIIByte(bite))
  120. stream.prepend(bite);
  121. // 7. If code point is null, return error.
  122. if (code_point === null)
  123. return encodings_1.decoderError(this.fatal);
  124. // 8. Return a code point whose value is code point.
  125. return code_point;
  126. }
  127. // 6. If byte is an ASCII byte, return a code point whose value
  128. // is byte.
  129. if (terminology_1.isASCIIByte(bite))
  130. return bite;
  131. // 7. If byte is 0x80, return code point U+20AC.
  132. if (bite === 0x80)
  133. return 0x20AC;
  134. // 8. If byte is in the range 0x81 to 0xFE, inclusive, set
  135. // gb18030 first to byte and return continue.
  136. if (utilities_1.inRange(bite, 0x81, 0xFE)) {
  137. this.gb18030_first = bite;
  138. return null;
  139. }
  140. // 9. Return error.
  141. return encodings_1.decoderError(this.fatal);
  142. };
  143. return GB18030Decoder;
  144. }());
  145. exports.GB18030Decoder = GB18030Decoder;
  146. //# sourceMappingURL=GB18030Decoder.js.map