SingleByteEncoder.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { encoderError } from "../../encoding/encodings";
  2. import { finished } from "../../encoding/finished";
  3. import { indexPointerFor } from "../../encoding/indexes";
  4. import { end_of_stream, isASCIICodePoint } from "../../encoding/terminology";
  5. /**
  6. * @constructor
  7. * @implements {Encoder}
  8. * @param {!Array.<?number>} index The encoding index.
  9. * @param {{fatal: boolean}} options
  10. */
  11. export class SingleByteEncoder {
  12. constructor(index, options) {
  13. this.index = index;
  14. this.fatal = options.fatal;
  15. }
  16. /**
  17. * @param {Stream} stream Input stream.
  18. * @param {number} code_point Next code point read from the stream.
  19. * @return {(number|!Array.<number>)} Byte(s) to emit.
  20. */
  21. handler(stream, code_point) {
  22. // 1. If code point is end-of-stream, return finished.
  23. if (code_point === end_of_stream)
  24. return finished;
  25. // 2. If code point is an ASCII code point, return a byte whose
  26. // value is code point.
  27. if (isASCIICodePoint(code_point))
  28. return code_point;
  29. // 3. Let pointer be the index pointer for code point in index
  30. // single-byte.
  31. const pointer = indexPointerFor(code_point, this.index);
  32. // 4. If pointer is null, return error with code point.
  33. if (pointer === null)
  34. encoderError(code_point);
  35. // 5. Return a byte whose value is pointer + 0x80.
  36. return pointer + 0x80;
  37. }
  38. }
  39. //# sourceMappingURL=SingleByteEncoder.js.map