Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixes/improvements to IO::Socket::INET.get.
* Bring API in line with IO::Handle, using nl-in for the input
  separator and allowing multiple separators
* Make default separators CR and CRLF, like other handles (this fixes
  the recent Panda regression)
* Use VM-backed chomped line reading, and remove custom logic for it.
  • Loading branch information
jnthn committed Nov 5, 2015
1 parent 13936ab commit 2e3da75
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions src/core/IO/Socket/INET.pm
Expand Up @@ -25,8 +25,8 @@ my class IO::Socket::INET does IO::Socket {
has $.family = PIO::PF_INET;
has $.proto = PIO::PROTO_TCP;
has $.type = PIO::SOCK_STREAM;
has Str $.input-line-separator is rw = "\x0A";
has Int $.ins = 0;
has $.nl-in is rw = ["\x0A", "\r\n"];
has int $.ins;

my sub v4-split($uri) {
return $uri.split(':', 2);
Expand Down Expand Up @@ -85,34 +85,34 @@ my class IO::Socket::INET does IO::Socket {
self;
}

method input-line-separator is rw {
DEPRECATED('nl-in');
self.nl-in
}

method get() {
my Mu $io := nqp::getattr(self, $?CLASS, '$!PIO');
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);
my int $sep-len = nqp::chars($sep);
my str $line = nqp::readlinefh($io);
my int $len = nqp::chars($line);

if $len == 0 { Str }
my Mu $io := nqp::getattr(self, $?CLASS, '$!PIO');
nqp::setencoding($io, Rakudo::Internals.NORMALIZE_ENCODING($!encoding));
Rakudo::Internals.SET_LINE_ENDING_ON_HANDLE($io, $!nl-in);
my str $line = nqp::readlinechompfh($io);
if nqp::chars($line) || !nqp::eoffh($io) {
$!ins = $!ins + 1;
$line
}
else {
++$!ins;
$len >= $sep-len && nqp::eqat($line, $sep, $len - $sep-len)
?? nqp::p6box_s(nqp::substr($line, 0, $len - $sep-len))
!! nqp::p6box_s($line);
Str
}
}

method lines() {
gather while (my $line = self.get()).defined {
gather while (my $line = self.get()).DEFINITE {
take $line;
}
}

method accept() {
## A solution as proposed by moritz
my $new_sock := $?CLASS.bless(:$!family, :$!proto, :$!type, :$!input-line-separator);
my $new_sock := $?CLASS.bless(:$!family, :$!proto, :$!type, :$!nl-in);
#?if jvm
nqp::getattr($new_sock, $?CLASS, '$!buffer') = buf8.new;
#?endif
Expand Down

0 comments on commit 2e3da75

Please sign in to comment.