1. my class Encoding::Decoder::Builtin is repr('Decoder') does Encoding::Decoder {
  2. method new(str $encoding, :$translate-nl, :$replacement, :$strict) {
  3. nqp::decoderconfigure(nqp::create(self), $encoding,
  4. nqp::hash(
  5. 'translate_newlines', $translate-nl ?? 1 !! 0,
  6. 'replacement', $replacement.defined ?? nqp::unbox_s($replacement) !! nqp::null_s(),
  7. 'config', $strict ?? 0 !! 1
  8. # Config set to 0 uses the decoder's new default, which is strict
  9. # decoding. Setting to 1 uses the 6.c specced functionality where
  10. # unmapped codepoints will still decode, e.g. codepoint 129, which
  11. # in windows-1252 does not exist.
  12. # In 6.d, 'config' will default to 0
  13. )
  14. )
  15. }
  16. method add-bytes(Blob:D $bytes --> Nil) {
  17. nqp::decoderaddbytes(self, nqp::decont($bytes));
  18. }
  19. method consume-available-chars(--> Str:D) {
  20. nqp::decodertakeavailablechars(self)
  21. }
  22. method consume-all-chars(--> Str:D) {
  23. nqp::decodertakeallchars(self)
  24. }
  25. method consume-exactly-chars(int $chars, Bool:D :$eof = False --> Str) {
  26. my str $result = $eof
  27. ?? nqp::decodertakecharseof(self, $chars)
  28. !! nqp::decodertakechars(self, $chars);
  29. nqp::isnull_s($result) ?? Str !! $result
  30. }
  31. method set-line-separators(@seps --> Nil) {
  32. my $sep-strs := nqp::list_s();
  33. nqp::push_s($sep-strs, .Str) for @seps;
  34. nqp::decodersetlineseps(self, $sep-strs);
  35. }
  36. method consume-line-chars(Bool:D :$chomp = False, Bool:D :$eof = False --> Str) {
  37. my str $line = nqp::decodertakeline(self, $chomp, $eof);
  38. nqp::isnull_s($line) ?? Str !! $line
  39. }
  40. method is-empty() {
  41. nqp::decoderempty(self)
  42. }
  43. method bytes-available() {
  44. nqp::decoderbytesavailable(self)
  45. }
  46. method consume-exactly-bytes(int $bytes --> Blob) {
  47. nqp::ifnull(nqp::decodertakebytes(self, buf8.new, $bytes), Blob)
  48. }
  49. }
  50. my class Supply { ... }
  51. my class Encoding::Registry { ... }
  52. augment class Rakudo::Internals {
  53. method BYTE_SUPPLY_DECODER(Supply:D $bin-supply, Str:D $enc, :$translate-nl) {
  54. supply {
  55. my $decoder = Encoding::Registry.find($enc).decoder(:$translate-nl);
  56. whenever $bin-supply {
  57. $decoder.add-bytes($_);
  58. my $available = $decoder.consume-available-chars();
  59. emit $available if $available ne '';
  60. LAST {
  61. # XXX The `with` is required due to a bug where the
  62. # LAST phaser is not properly scoped if we don't get
  63. # any bytes. Since that means there's nothing to emit
  64. # anyway, we'll not worry about this case for now.
  65. #
  66. # --- or at least that was the the idea before we fixed
  67. # that bug: https://irclog.perlgeek.de/perl6/2016-12-07#i_13698178
  68. # and tried removing the `with` in 58cdfd8, but then the error
  69. # `No such method 'consume-all-chars' for invocant of type 'Any`
  70. # started popping up on Proc::Async tests, so...
  71. # there may be some other bug affecting this?
  72. with $decoder {
  73. my $rest = .consume-all-chars();
  74. emit $rest if $rest ne '';
  75. }
  76. }
  77. }
  78. }
  79. }
  80. }