PerlIO::via::EscStatus::Parser - parse out status escape lines


PerlIO-via-EscStatus documentation Contained in the PerlIO-via-EscStatus distribution.

Index


Code Index:

NAME

Top

PerlIO::via::EscStatus::Parser - parse out status escape lines

SYNOPSIS

Top

 use PerlIO::via::EscStatus::Parser;
 my $ep = PerlIO::via::EscStatus::Parser->new;
 my ($text, $status) = $ep->parse ($input);

DESCRIPTION

Top

An EscStatus::Parser object parses out EscStatus format status strings from text. This is used by the EscStatus layers and is offered for parsing a stream the same way the layers do.

FUNCTIONS

Top

$ep = PerlIO::via::EscStatus::Parser->new

Create and return a new parser object.

($text, $status) = $ep->parse ($input)

Parse an input string $input and return the plain $text part of that input, and the last complete $status line. If there's no complete status line yet then $status is undef. If there's no plain text, ie. if the input is entirely status, then $text is an empty string "".

$input doesn't have to be complete lines. Any partial status at the end of it is held in $ep and will be returned on a later parse call when the full line has been received.

SEE ALSO

Top

PerlIO::via::EscStatus

HOME PAGE

Top

http://user42.tuxfamily.org/perlio-via-escstatus/index.html

LICENSE

Top

Copyright 2008, 2009, 2010, 2011 Kevin Ryde

PerlIO-via-EscStatus is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3, or (at your option) any later version.

PerlIO-via-EscStatus is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with PerlIO-via-EscStatus. If not, see http://www.gnu.org/licenses/


PerlIO-via-EscStatus documentation Contained in the PerlIO-via-EscStatus distribution.

# Copyright 2008, 2009, 2010, 2011 Kevin Ryde

# This file is part of PerlIO-via-EscStatus.
#
# PerlIO-via-EscStatus is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation; either version 3, or (at your option) any
# later version.
#
# PerlIO-via-EscStatus is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with PerlIO-via-EscStatus.  If not, see <http://www.gnu.org/licenses/>.

package PerlIO::via::EscStatus::Parser;
use 5.006;  # for $-[0]
use strict;
use warnings;

our $VERSION = 10;

# This regexp generated by devel/partial-end.pl.  It matches either a whole
# ESCSTATUS_STR anywhere, or a leading portion of it at the end of the
# string, like say /\e_$/.  Frequently there won't be any Esc's left in the
# string and the first \e will quickly fail to match.
#
use constant ESCSTATUS_STR_PARTIAL_REGEXP =>
  qr/\e(?:$|_(?:$|E(?:$|s(?:$|c(?:$|S(?:$|t(?:$|a(?:$|t(?:$|u(?:$|s(?:$|\e(?:$|\\))))))))))))/;

sub new {
  my ($class) = @_;
  return bless { partial => '' }, $class;
}

sub parse {
  my ($self, $buf) = @_;
  my $status = undef;
  $buf = $self->{'partial'} . $buf;

  # whole statuses
  if ($buf =~ s/\e_EscStatus\e\\([^\n]*)\n//g) {
    $status = $1;
  }
  my $pos = ($buf =~ ESCSTATUS_STR_PARTIAL_REGEXP
             ? $-[0] # start of match
             : length ($buf));
  $self->{'partial'} = substr ($buf, $pos); # match onwards
  $buf = substr ($buf, 0, $pos);            # prematch
  return ($status, $buf);
}

1;
__END__