Skip to content

Commit

Permalink
Merge pull request #1617 from samcv/encoder
Browse files Browse the repository at this point in the history
Add strict, replacement options for IO::Handle, Str.encode & other fixes
  • Loading branch information
samcv committed Mar 20, 2018
2 parents a0ee718 + c9c5f34 commit 3420b67
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 38 deletions.
9 changes: 5 additions & 4 deletions src/core/Encoding/Builtin.pm6
Expand Up @@ -17,15 +17,16 @@ class Encoding::Builtin does Encoding {
method alternative-names() { $!alternative-names }

method decoder(*%options --> Encoding::Decoder) {
%options<replacement> = self!rep-char(%options<replacement>) if %options<replacement>:exists && %options<replacement>.DEFINITE && %options<replacement> !=== False;
Encoding::Decoder::Builtin.new($!name, |%options)
}

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

$translate-nl && $is-win
?? Encoding::Encoder::TranslateNewlineWrapper.new($encoder)
!! $encoder
Expand Down
45 changes: 18 additions & 27 deletions src/core/Encoding/Encoder/Builtin.pm6
@@ -1,44 +1,35 @@
my class Encoding::Encoder::Builtin does Encoding::Encoder {
has str $!encoding;
has Blob $!type;
has $!replacement;
has int $!config;

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

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

method encode-chars(str $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) {
method !setup($encoding, $type, :$replacement, :$strict) {
$!encoding = $encoding;
$!type := nqp::can($type.HOW, 'pun') ?? $type.^pun !! $type.WHAT;
$!replacement = $replacement;
$!replacement = $replacement.defined ?? $replacement !! nqp::null_s();
$!config = $strict ?? 0 !! 1;
#?if !moar
X::NYI.new(feature => 'encoding with replacement').throw if $replacement.defined;
X::NYI.new(feature => 'encoding with strict').throw if $strict;
#?endif
self
}

method encode-chars(str $str --> Blob:D) {
#?if moar
nqp::encoderep($str, $!encoding, $!replacement, nqp::create($!type))
nqp::encoderepconf($str,
$!encoding,
$!replacement,
nqp::create($!type),
$!config)
#?endif
#?if !moar
X::NYI.new(feature => 'encoding with replacement').throw
nqp::encode($str, $!encoding, nqp::create($!type));

#?endif
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/core/IO/Handle.pm6
Expand Up @@ -730,7 +730,7 @@ my class IO::Handle {

proto method encoding(|) {*}
multi method encoding(IO::Handle:D:) { $!encoding // Nil }
multi method encoding(IO::Handle:D: $new-encoding is copy) {
multi method encoding(IO::Handle:D: $new-encoding is copy, :$replacement, :$strict) {
with $new-encoding {
if $_ eq 'bin' {
$_ = Nil;
Expand All @@ -751,7 +751,7 @@ my class IO::Handle {
$!decoder.set-line-separators($!nl-in.list);
$!decoder.add-bytes($prev-decoder.consume-exactly-bytes($available))
if $available;
$!encoder := $encoding.encoder(:translate-nl);
$!encoder := $encoding.encoder(:translate-nl, :$replacement, :$strict);
$!encoding = $encoding.name;
}
else {
Expand All @@ -768,7 +768,7 @@ my class IO::Handle {
my $encoding = Encoding::Registry.find($new-encoding);
$!decoder := $encoding.decoder(:translate-nl);
$!decoder.set-line-separators($!nl-in.list);
$!encoder := $encoding.encoder(:translate-nl);
$!encoder := $encoding.encoder(:translate-nl, :$replacement, :$strict);
$!encoding = $encoding.name;
}
else {
Expand Down
4 changes: 2 additions & 2 deletions src/core/Rakudo/Internals.pm6
Expand Up @@ -275,8 +275,8 @@ my class Rakudo::Internals {
'windows-1252', 'windows-1252',
'windows-1251', 'windows-1251',
# windows without dash
'windows1251', 'windows-1252',
'windows1252', 'windows-1251',
'windows1251', 'windows-1251',
'windows1252', 'windows-1252',
# utf with dash
'utf-8', 'utf8',
'utf-16', 'utf16',
Expand Down
4 changes: 2 additions & 2 deletions src/core/Str.pm6
Expand Up @@ -2179,9 +2179,9 @@ my class Str does Stringy { # declared in BOOTSTRAP
}

proto method encode(|) {*}
multi method encode(Str:D $encoding = 'utf8', :$replacement, Bool() :$translate-nl = False) {
multi method encode(Str:D $encoding = 'utf8', :$replacement, Bool() :$translate-nl = False, :$strict) {
Encoding::Registry.find($encoding)
.encoder(:$replacement, :$translate-nl)
.encoder(:$replacement, :$translate-nl, :$strict)
.encode-chars(self)
}

Expand Down

0 comments on commit 3420b67

Please sign in to comment.