Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Move NORMALIZE_ENCODING to Rakudo::Internals
Also make it a bit more efficient
  • Loading branch information
lizmat committed Oct 30, 2015
1 parent 2b278e8 commit 3bc921e
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 40 deletions.
9 changes: 5 additions & 4 deletions src/core/Buf.pm
Expand Up @@ -67,7 +67,8 @@ my role Blob[::T = uint8] does Positional[T] does Stringy is repr('VMArray') is
method Int(Blob:D:) { self.elems }

method decode(Blob:D: $encoding = 'utf-8') {
nqp::p6box_s(nqp::decode(self, NORMALIZE_ENCODING($encoding)))
nqp::p6box_s(
nqp::decode(self, Rakudo::Internals.NORMALIZE_ENCODING($encoding)))
}

method list(Blob:D:) {
Expand Down Expand Up @@ -222,7 +223,7 @@ constant blob64 = Blob[uint64];

my class utf8 does Blob[uint8] is repr('VMArray') {
method decode(utf8:D: $encoding = 'utf-8') {
my $enc = NORMALIZE_ENCODING($encoding);
my $enc = Rakudo::Internals.NORMALIZE_ENCODING($encoding);
die "Can not decode a utf-8 buffer as if it were $encoding"
unless $enc eq 'utf8';
nqp::p6box_s(nqp::decode(self, 'utf8'))
Expand All @@ -234,7 +235,7 @@ my class utf8 does Blob[uint8] is repr('VMArray') {

my class utf16 does Blob[uint16] is repr('VMArray') {
method decode(utf16:D: $encoding = 'utf-16') {
my $enc = NORMALIZE_ENCODING($encoding);
my $enc = Rakudo::Internals.NORMALIZE_ENCODING($encoding);
die "Can not decode a utf-16 buffer as if it were $encoding"
unless $enc eq 'utf16';
nqp::p6box_s(nqp::decode(self, 'utf16'))
Expand All @@ -246,7 +247,7 @@ my class utf16 does Blob[uint16] is repr('VMArray') {

my class utf32 does Blob[uint32] is repr('VMArray') {
method decode(utf32:D: $encoding = 'utf-32') {
my $enc = NORMALIZE_ENCODING($encoding);
my $enc = Rakudo::Internals.NORMALIZE_ENCODING($encoding);
die "Can not decode a utf-32 buffer as if it were $encoding"
unless $enc eq 'utf32';
nqp::p6box_s(nqp::decode(self, 'utf32'))
Expand Down
10 changes: 6 additions & 4 deletions src/core/IO/Handle.pm
Expand Up @@ -63,7 +63,8 @@ my class IO::Handle does IO {
die "Don't know how to open '$_' especially";
}
$!chomp = $chomp;
nqp::setencoding($!PIO, NORMALIZE_ENCODING($enc)) unless $bin;
nqp::setencoding($!PIO, Rakudo::Internals.NORMALIZE_ENCODING($enc))
unless $bin;
return self;
}

Expand Down Expand Up @@ -91,7 +92,8 @@ my class IO::Handle does IO {

$!chomp = $chomp;
nqp::setinputlinesep($!PIO, nqp::unbox_s($!nl = $nl));
nqp::setencoding($!PIO, NORMALIZE_ENCODING($enc)) unless $bin;
nqp::setencoding($!PIO, Rakudo::Internals.NORMALIZE_ENCODING($enc))
unless $bin;
self;
}

Expand Down Expand Up @@ -956,8 +958,8 @@ my class IO::Handle does IO {

method encoding(IO::Handle:D: $enc?) {
$enc.defined
?? nqp::setencoding($!PIO, NORMALIZE_ENCODING($enc))
!! $!PIO.encoding
?? nqp::setencoding($!PIO,Rakudo::Internals.NORMALIZE_ENCODING($enc))
!! $!PIO.encoding
}

submethod DESTROY(IO::Handle:D:) {
Expand Down
2 changes: 1 addition & 1 deletion src/core/IO/Socket/INET.pm
Expand Up @@ -87,7 +87,7 @@ my class IO::Socket::INET does IO::Socket {

method get() {
my Mu $io := nqp::getattr(self, $?CLASS, '$!PIO');
my str $encoding = nqp::unbox_s(NORMALIZE_ENCODING($!encoding));
my str $encoding = Rakudo::Internals.NORMALIZE_ENCODING($!encoding);
nqp::setencoding($io, $encoding);
my str $sep = nqp::unbox_s($!input-line-separator);
nqp::setinputlinesep($io, $sep);
Expand Down
9 changes: 6 additions & 3 deletions src/core/Proc.pm
Expand Up @@ -23,7 +23,8 @@ my class Proc {
$!in_fh := nqp::syncpipe();
$!flags += nqp::const::PIPE_CAPTURE_IN;
nqp::setinputlinesep($!in_fh, nqp::unbox_s($nl));
nqp::setencoding($!in_fh, NORMALIZE_ENCODING($enc)) unless $bin;
nqp::setencoding($!in_fh,Rakudo::Internals.NORMALIZE_ENCODING($enc))
unless $bin;
nqp::bindattr(nqp::decont($!in), IO::Handle, '$!PIO', $!in_fh);
}
elsif nqp::istype($in, Str) && $in eq '-' {
Expand All @@ -40,7 +41,8 @@ my class Proc {
$!out_fh := nqp::syncpipe();
$!flags += nqp::const::PIPE_CAPTURE_OUT;
nqp::setinputlinesep($!out_fh, nqp::unbox_s($nl));
nqp::setencoding($!out_fh, NORMALIZE_ENCODING($enc)) unless $bin;
nqp::setencoding($!out_fh,Rakudo::Internals.NORMALIZE_ENCODING($enc))
unless $bin;
nqp::bindattr(nqp::decont($!out), IO::Handle, '$!PIO', $!out_fh);
}
elsif nqp::istype($out, IO::Handle) && $out.DEFINITE {
Expand Down Expand Up @@ -74,7 +76,8 @@ my class Proc {
$!err_fh := nqp::syncpipe();
$!flags += nqp::const::PIPE_CAPTURE_ERR;
nqp::setinputlinesep($!err_fh, nqp::unbox_s($nl));
nqp::setencoding($!err_fh, NORMALIZE_ENCODING($enc)) unless $bin;
nqp::setencoding($!err_fh,Rakudo::Internals.NORMALIZE_ENCODING($enc))
unless $bin;
nqp::bindattr(nqp::decont($!err), IO::Handle, '$!PIO', $!err_fh);
}
else {
Expand Down
36 changes: 36 additions & 0 deletions src/core/Rakudo/Internals.pm
Expand Up @@ -227,6 +227,42 @@ my class Rakudo::Internals {
}
Nil
}

my $encodings := nqp::hash(
# fast mapping for identicals
'utf8', 'utf8',
'utf16', 'utf16',
'utf32', 'utf32',
'ascii', 'ascii',
'iso-8859-1', 'iso-8859-1',
'windows-1252', 'windows-1252',
# with dash
'utf-8', 'utf8',
'utf-16', 'utf16',
'utf-32', 'utf32',
# according to http://de.wikipedia.org/wiki/ISO-8859-1
'iso_8859-1:1987', 'iso-8859-1',
'iso_8859-1', 'iso-8859-1',
'iso-ir-100', 'iso-8859-1',
'latin1', 'iso-8859-1',
'latin-1', 'iso-8859-1',
'csisolatin1', 'iso-8859-1',
'l1', 'iso-8859-1',
'ibm819', 'iso-8859-1',
'cp819', 'iso-8859-1',
);
method NORMALIZE_ENCODING(Str:D \encoding) {
my str $key = nqp::unbox_s(encoding);
if nqp::existskey($encodings,$key) {
nqp::atkey($encodings,$key)
}
else {
my str $lc = nqp::lc($key);
nqp::existskey($encodings,$lc)
?? nqp::atkey($encodings,$lc)
!! nqp::lc($key)
}
}
}

# vim: ft=perl6 expandtab sw=4
29 changes: 1 addition & 28 deletions src/core/Str.pm
Expand Up @@ -9,33 +9,6 @@ my class X::Numeric::Confused { ... }

my constant $?TABSTOP = 8;

sub NORMALIZE_ENCODING(Str:D $s) {
state %map = (
# fast mapping for identicals
'utf8' => 'utf8',
'utf16' => 'utf16',
'utf32' => 'utf32',
'ascii' => 'ascii',
'iso-8859-1' => 'iso-8859-1',
'windows-1252' => 'windows-1252',
# with dash
'utf-8' => 'utf8',
'utf-16' => 'utf16',
'utf-32' => 'utf32',
# according to http://de.wikipedia.org/wiki/ISO-8859-1
'iso_8859-1:1987' => 'iso-8859-1',
'iso_8859-1' => 'iso-8859-1',
'iso-ir-100' => 'iso-8859-1',
'latin1' => 'iso-8859-1',
'latin-1' => 'iso-8859-1',
'csisolatin1' => 'iso-8859-1',
'l1' => 'iso-8859-1',
'ibm819' => 'iso-8859-1',
'cp819' => 'iso-8859-1',
);
%map{$s} // %map{lc $s} // lc $s;
}

my class Str does Stringy { # declared in BOOTSTRAP
# class Str is Cool {
# has str $!value is box_target;
Expand Down Expand Up @@ -1155,7 +1128,7 @@ my class Str does Stringy { # declared in BOOTSTRAP

my %enc_type = utf8 => utf8, utf16 => utf16, utf32 => utf32;
method encode(Str:D $encoding = 'utf8') {
my $enc := NORMALIZE_ENCODING($encoding);
my $enc := Rakudo::Internals.NORMALIZE_ENCODING($encoding);
my $enc_type := %enc_type.EXISTS-KEY($enc) ?? %enc_type{$enc} !! blob8;
nqp::encode(nqp::unbox_s(self), nqp::unbox_s($enc), nqp::decont($enc_type.new))
}
Expand Down

0 comments on commit 3bc921e

Please sign in to comment.