File Coverage

blib/lib/PerlIO/via/EscStatus/Parser.pm
Criterion Covered Total %
statement 23 23 100.0
branch 4 4 100.0
condition n/a
subroutine 6 6 100.0
pod 2 2 100.0
total 35 35 100.0


line stmt bran cond sub pod time code
1             # Copyright 2008, 2009, 2010, 2011, 2012 Kevin Ryde
2              
3             # This file is part of PerlIO-via-EscStatus.
4             #
5             # PerlIO-via-EscStatus is free software; you can redistribute it and/or
6             # modify it under the terms of the GNU General Public License as published
7             # by the Free Software Foundation; either version 3, or (at your option) any
8             # later version.
9             #
10             # PerlIO-via-EscStatus is distributed in the hope that it will be useful,
11             # but WITHOUT ANY WARRANTY; without even the implied warranty of
12             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13             # Public License for more details.
14             #
15             # You should have received a copy of the GNU General Public License along
16             # with PerlIO-via-EscStatus. If not, see .
17              
18             package PerlIO::via::EscStatus::Parser;
19 5     5   96507 use 5.006; # for $-[0]
  5         17  
  5         244  
20 5     5   31 use strict;
  5         7  
  5         245  
21 5     5   29 use warnings;
  5         10  
  5         527  
22              
23             our $VERSION = 11;
24              
25             # This regexp generated by devel/partial-end.pl. It matches either a whole
26             # ESCSTATUS_STR anywhere, or a leading portion of it at the end of the
27             # string, like say /\e_$/. Frequently there won't be any Esc's left in the
28             # string and the first \e will quickly fail to match.
29             #
30 5         1458 use constant ESCSTATUS_STR_PARTIAL_REGEXP =>
31 5     5   30 qr/\e(?:$|_(?:$|E(?:$|s(?:$|c(?:$|S(?:$|t(?:$|a(?:$|t(?:$|u(?:$|s(?:$|\e(?:$|\\))))))))))))/;
  5         8  
32              
33             sub new {
34 7     7 1 2475 my ($class) = @_;
35 7         71 return bless { partial => '' }, $class;
36             }
37              
38             sub parse {
39 20     20 1 18416 my ($self, $buf) = @_;
40 20         32 my $status = undef;
41 20         59 $buf = $self->{'partial'} . $buf;
42              
43             # whole statuses
44 20 100       124 if ($buf =~ s/\e_EscStatus\e\\([^\n]*)\n//g) {
45 9         29 $status = $1;
46             }
47 20 100       80 my $pos = ($buf =~ ESCSTATUS_STR_PARTIAL_REGEXP
48             ? $-[0] # start of match
49             : length ($buf));
50 20         51 $self->{'partial'} = substr ($buf, $pos); # match onwards
51 20         36 $buf = substr ($buf, 0, $pos); # prematch
52 20         71 return ($status, $buf);
53             }
54              
55             1;
56             __END__