Acme-Throw
view release on metacpan or search on metacpan
t/lib/IO/String.pm view on Meta::CPAN
sub seek
{
my($self,$off,$whence) = @_;
my $buf = *$self->{buf} || return 0;
my $len = length($$buf);
my $pos = *$self->{pos};
_init_seek_constants() unless defined $SEEK_SET;
if ($whence == $SEEK_SET) { $pos = $off }
elsif ($whence == $SEEK_CUR) { $pos += $off }
elsif ($whence == $SEEK_END) { $pos = $len + $off }
else { die "Bad whence ($whence)" }
print "SEEK(POS=$pos,OFF=$off,LEN=$len)\n" if $DEBUG;
$pos = 0 if $pos < 0;
$self->truncate($pos) if $pos > $len; # extend file
*$self->{pos} = $pos;
return 1;
}
sub pos
{
my $self = shift;
my $old = *$self->{pos};
if (@_) {
my $pos = shift || 0;
my $buf = *$self->{buf};
my $len = $buf ? length($$buf) : 0;
$pos = $len if $pos > $len;
*$self->{pos} = $pos;
}
return $old;
}
sub getpos { shift->pos; }
*sysseek = \&seek;
*setpos = \&pos;
*tell = \&getpos;
sub getline
{
my $self = shift;
my $buf = *$self->{buf} || return;
my $len = length($$buf);
my $pos = *$self->{pos};
return if $pos >= $len;
unless (defined $/) { # slurp
*$self->{pos} = $len;
return substr($$buf, $pos);
}
unless (length $/) { # paragraph mode
# XXX slow&lazy implementation using getc()
my $para = "";
my $eol = 0;
my $c;
while (defined($c = $self->getc)) {
if ($c eq "\n") {
$eol++;
next if $eol > 2;
}
elsif ($eol > 1) {
$self->ungetc($c);
last;
}
else {
$eol = 0;
}
$para .= $c;
}
return $para; # XXX wantarray
}
my $idx = index($$buf,$/,$pos);
if ($idx < 0) {
# return rest of it
*$self->{pos} = $len;
$. = ++ *$self->{lno};
return substr($$buf, $pos);
}
$len = $idx - $pos + length($/);
*$self->{pos} += $len;
$. = ++ *$self->{lno};
return substr($$buf, $pos, $len);
}
sub getlines
{
die "getlines() called in scalar context\n" unless wantarray;
my $self = shift;
my($line, @lines);
push(@lines, $line) while defined($line = $self->getline);
return @lines;
}
sub READLINE
{
goto &getlines if wantarray;
goto &getline;
}
sub input_line_number
{
my $self = shift;
my $old = *$self->{lno};
*$self->{lno} = shift if @_;
return $old;
}
sub truncate
{
my $self = shift;
my $len = shift || 0;
my $buf = *$self->{buf};
if (length($$buf) >= $len) {
substr($$buf, $len) = '';
*$self->{pos} = $len if $len < *$self->{pos};
}
else {
$$buf .= ($self->pad x ($len - length($$buf)));
}
return 1;
}
sub read
{
my $self = shift;
( run in 0.854 second using v1.01-cache-2.11-cpan-96521ef73a4 )