x-user-defined.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { inRange, encoderError, end_of_stream, finished, isASCIIByte, isASCIICodePoint } from './text_decoder_utils.js'
  2. // 15.5 x-user-defined
  3. // 15.5.1 x-user-defined decoder
  4. /**
  5. * @implements {Decoder}
  6. */
  7. export class XUserDefinedDecoder {
  8. /**
  9. * @param {Stream} stream The stream of bytes being decoded.
  10. * @param {number} bite The next byte read from the stream.
  11. */
  12. handler(stream, bite) {
  13. // 1. If byte is end-of-stream, return finished.
  14. if (bite === end_of_stream)
  15. return finished
  16. // 2. If byte is an ASCII byte, return a code point whose value
  17. // is byte.
  18. if (isASCIIByte(bite))
  19. return bite
  20. // 3. Return a code point whose value is 0xF780 + byte − 0x80.
  21. return 0xF780 + bite - 0x80
  22. }
  23. }
  24. // 15.5.2 x-user-defined encoder
  25. /**
  26. * @implements {Encoder}
  27. */
  28. export class XUserDefinedEncoder {
  29. /**
  30. * @param {Stream} stream Input stream.
  31. * @param {number} code_point Next code point read from the stream.
  32. */
  33. handler(stream, code_point) {
  34. // 1.If code point is end-of-stream, return finished.
  35. if (code_point === end_of_stream)
  36. return finished
  37. // 2. If code point is an ASCII code point, return a byte whose
  38. // value is code point.
  39. if (isASCIICodePoint(code_point))
  40. return code_point
  41. // 3. If code point is in the range U+F780 to U+F7FF, inclusive,
  42. // return a byte whose value is code point − 0xF780 + 0x80.
  43. if (inRange(code_point, 0xF780, 0xF7FF))
  44. return code_point - 0xF780 + 0x80
  45. // 4. Return error with code point.
  46. return encoderError(code_point)
  47. }
  48. }