SingleByteDecoder.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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 terminology_1 = require("../../encoding/terminology");
  6. /**
  7. * @constructor
  8. * @implements {Decoder}
  9. * @param {!Array.<number>} index The encoding index.
  10. * @param {{fatal: boolean}} options
  11. */
  12. var SingleByteDecoder = /** @class */ (function () {
  13. function SingleByteDecoder(index, options) {
  14. this.index = index;
  15. this.fatal = options.fatal;
  16. }
  17. /**
  18. * @param {Stream} stream The stream of bytes being decoded.
  19. * @param {number} bite The next byte read from the stream.
  20. * @return {?(number|!Array.<number>)} The next code point(s)
  21. * decoded, or null if not enough data exists in the input
  22. * stream to decode a complete code point.
  23. */
  24. SingleByteDecoder.prototype.handler = function (stream, bite) {
  25. // 1. If byte is end-of-stream, return finished.
  26. if (bite === terminology_1.end_of_stream)
  27. return finished_1.finished;
  28. // 2. If byte is an ASCII byte, return a code point whose value
  29. // is byte.
  30. if (terminology_1.isASCIIByte(bite))
  31. return bite;
  32. // 3. Let code point be the index code point for byte − 0x80 in
  33. // index single-byte.
  34. var code_point = this.index[bite - 0x80];
  35. // 4. If code point is null, return error.
  36. if (!code_point)
  37. return encodings_1.decoderError(this.fatal);
  38. // 5. Return a code point whose value is code point.
  39. return code_point;
  40. };
  41. return SingleByteDecoder;
  42. }());
  43. exports.SingleByteDecoder = SingleByteDecoder;
  44. //# sourceMappingURL=SingleByteDecoder.js.map