File Coverage

blib/lib/CodeGen/Cpppp/AntiCharacter.pm
Criterion Covered Total %
statement 27 28 96.4
branch 7 10 70.0
condition 7 8 87.5
subroutine 7 7 100.0
pod 2 2 100.0
total 50 55 90.9


line stmt bran cond sub pod time code
1             package CodeGen::Cpppp::AntiCharacter;
2              
3             our $VERSION = '0.005'; # VERSION
4             # ABSTRACT: AntiCharacters combine with characters to produce nothing
5              
6 18     18   273430 use v5.20;
  18         95  
7 18     18   100 use warnings;
  18         88  
  18         967  
8 18     18   147 use Carp;
  18         32  
  18         2182  
9             use overload
10             '.' => \&concat,
11 18     18   150 '""' => sub { $_[0][2] };
  18     5   66  
  18         204  
  5         11  
12              
13              
14             sub new {
15 66     66 1 274211 my ($class, $negate, $skip)= @_;
16 66   100     296 $skip //= '';
17 66 50       247 ref $negate eq 'Regexp' or croak "Expected qr// for negation argument";
18 66         228 bless [ $negate, $skip, '' ], $class;
19             }
20              
21             sub concat {
22 15     15 1 7934 my ($negate, $skip, $suffix)= @{$_[0]};
  15         36  
23 15 50       52 if ($_[2]) { # $string . $anticharacter; perform character destruction
24 15         21 my $tmp= $_[1];
25             # does this entire string consist of 'skip' pattern? If so, need to
26             # try again later.
27 15 100 66     218 if (!length $tmp || $tmp =~ /^$skip\Z/) {
28 1         6 return bless [ $negate, $skip, $tmp . $suffix ], ref $_[0];
29             }
30             # Does it match the pattern we're trying to cancel?
31 14 50       313 if ($tmp =~ /($negate)$skip\Z/) {
32 14         69 substr($tmp, $-[1], $+[1] - $-[1], '');
33             # Did it run into the start of the string, and could it cancel more?
34 14 100 100     105 if ($-[1] == 0
35             && (substr($_[1],0,1) . $_[1]) =~ /^($negate)$skip\Z/
36             ) {
37 1         6 return bless [ $negate, $skip, $tmp . $suffix ], ref $_[0];
38             }
39             }
40 13         68 return $tmp . $suffix;
41             }
42             else { # $anticharacter . $string; carry suffix for later
43 0           return bless [ $negate, $skip, $suffix . $_[1] ], ref $_[0];
44             }
45             }
46              
47             1;
48              
49             __END__