SingleByteDecoder.js 1.6 KB

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