Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add encoding support to new encoder API.
This will be used by I/O handles for doing output, and may be used by
Perl 6 language users directly also. It is designed to allow various
decisions to be made up front (on newline translation, replacement
chars, and output blob type), while `.encode` today has to make them
for every single call.
  • Loading branch information
jnthn committed Jun 20, 2017
1 parent e62ab72 commit 274e7c0
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/core/Encoding/Builtin.pm
Expand Up @@ -12,8 +12,26 @@ class Encoding::Builtin does Encoding {
Encoding::Decoder::Builtin.new($!name, |%options)
}

method encoder(--> Encoding::Encoder) {
die "NYI"
my int $is-win = Rakudo::Internals.IS-WIN;
method encoder(:$replacement, :$translate-nl --> Encoding::Encoder) {
my $encoder = $replacement
?? Encoding::Encoder::Builtin::Replacement.new($!name,
self!buf-type(), self!rep-char($replacement))
!! Encoding::Encoder::Builtin.new($!name, self!buf-type());
$translate-nl && $is-win
?? Encoding::Encoder::TranslateNewlineWrapper.new($encoder)
!! $encoder
}

my $enc_type := nqp::hash('utf8',utf8,'utf16',utf16,'utf32',utf32);
method !buf-type() {
nqp::ifnull(nqp::atkey($enc_type, $!name), Blob)
}

method !rep-char($replacement) {
nqp::istype($replacement, Bool)
?? ($!name.starts-with('utf') ?? "\x[FFFD]" !! "?")
!! $replacement.Str
}
}

Expand Down
44 changes: 44 additions & 0 deletions src/core/Encoding/Encoder/Builtin.pm
@@ -0,0 +1,44 @@
my class Encoding::Encoder::Builtin does Encoding::Encoder {
has str $!encoding;
has Blob $!type;

method new(Str $encoding, Blob:U $type) {
nqp::create(self)!setup($encoding, $type)
}

method !setup($encoding, $type) {
$!encoding = $encoding;
$!type := nqp::can($type.HOW, 'pun') ?? $type.^pun !! $type.WHAT;
self
}

method encode-chars(Str:D $str --> Blob:D) {
nqp::encode($str, $!encoding, nqp::create($!type))
}
}

my class Encoding::Encoder::Builtin::Replacement does Encoding::Encoder {
has str $!encoding;
has Blob $!type;
has str $!replacement;

method new(Str $encoding, Blob:U $type, Str $replacement) {
nqp::create(self)!setup($encoding, $type, $replacement)
}

method !setup($encoding, $type, $replacement) {
$!encoding = $encoding;
$!type := nqp::can($type.HOW, 'pun') ?? $type.^pun !! $type.WHAT;
$!replacement = $replacement;
self
}

method encode-chars(Str:D $str --> Blob:D) {
#?if moar
nqp::encoderep($str, $!encoding, $!replacement, nqp::create($!type))
#?endif
#?if !moar
X::NYI.new(feature => 'encoding with replacement').throw
#?endif
}
}
16 changes: 16 additions & 0 deletions src/core/Encoding/Encoder/TranslateNewlineWrapper.pm
@@ -0,0 +1,16 @@
my class Encoding::Encoder::TranslateNewlineWrapper {

This comment has been minimized.

Copy link
@b2gills

b2gills Jun 20, 2017

Contributor

Shouldn't does Encoding::Encoder be added to Encoding::Encoder::TranslateNewlineWrapper?

It breaks the build on my Windows computer.

Submitted as PR #1105

This comment has been minimized.

Copy link
@jnthn

jnthn Jun 21, 2017

Author Member

Yes, indeed it should have; thanks! Was going to test it on Windows pre-merge, but the place where my Windows box is had some connectivity problems yesterday.

has Encoding::Encoder $!delegate;

method new(Encoding::Encoder $delegate) {
nqp::create(self)!setup($delegate)
}

method !setup(Encoding::Encoder $delegate) {
$!delegate := $delegate;
self
}

method encode-chars(Str:D $str --> Blob:D) {
$!delegate.encode-chars(Rakudo::Internals.TRANSPOSE($str, "\n", "\r\n"))
}
}
2 changes: 2 additions & 0 deletions tools/build/jvm_core_sources
Expand Up @@ -47,6 +47,8 @@ src/core/Buf.pm
src/core/Encoding/Decoder.pm
src/core/Encoding/Decoder/Builtin.pm
src/core/Encoding/Encoder.pm
src/core/Encoding/Encoder/Builtin.pm
src/core/Encoding/Encoder/TranslateNewlineWrapper.pm
src/core/Encoding.pm
src/core/Encoding/Registry.pm
src/core/Encoding/Builtin.pm
Expand Down
2 changes: 2 additions & 0 deletions tools/build/moar_core_sources
Expand Up @@ -49,6 +49,8 @@ src/core/Collation.pm
src/core/Encoding/Decoder.pm
src/core/Encoding/Decoder/Builtin.pm
src/core/Encoding/Encoder.pm
src/core/Encoding/Encoder/Builtin.pm
src/core/Encoding/Encoder/TranslateNewlineWrapper.pm
src/core/Encoding.pm
src/core/Encoding/Registry.pm
src/core/Encoding/Builtin.pm
Expand Down

0 comments on commit 274e7c0

Please sign in to comment.