| 1 | package Text::Clip; | ||||||||||||||||||||||||||||
| 2 | 4 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 4 | $Text::Clip::VERSION = '0.0014'; | |||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||
| 5 | # ABSTRACT: Clip and extract text in clipboard-like way | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use Any::Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 4 | has data => qw/ reader data writer _data required 1 /; | |||||||||||||||||||||||||||
| 11 | 4 | has [qw/ start head tail mhead mtail /] => qw/ is rw required 1 isa Int default 0 /; | |||||||||||||||||||||||||||
| 12 | 4 | has _parent => qw/ is ro isa Maybe[Text::Clip] init_arg parent /; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 4 | has found => qw/ is ro required 1 isa Str /, default => ''; | |||||||||||||||||||||||||||
| 15 | 4 | has content => qw/ is ro required 1 isa Str /, default => ''; | |||||||||||||||||||||||||||
| 16 | 7 | ▶ | has _matched => qw/ init_arg matched is ro isa ArrayRef /, default => sub { [] }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 0 | ▶ | sub matched { return @{ $_[0]->matched } } | ||||||||||||||||||||||||||
|
Subroutine
matched: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 18 | 4 | has matcher => qw/ is ro /, default => undef; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 4 | has default => qw/ is ro lazy_build 1 isa HashRef /; | |||||||||||||||||||||||||||
| 21 | 7 | ▶ | sub _build_default { { | ||||||||||||||||||||||||||
|
Subroutine
_build_default: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 7 | slurp => '[)', | |||||||||||||||||||||||||||
| 23 | } } | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 22 | ▶ | sub BUILD { | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 26 | 22 | my $self = shift; | |||||||||||||||||||||||||||
| 27 | 22 | my $data = $self->data; | |||||||||||||||||||||||||||
| 28 | 22 | ▶ | if ( ref $data ne 'SCALAR' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 7 | chomp $data; | |||||||||||||||||||||||||||
| 30 | 7 | ▶ | $data .= "\n" if length $data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 7 | $self->_data( \$data ); | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 15 | ▶ | sub _fhead ($$) { | ||||||||||||||||||||||||||
|
Subroutine
_fhead: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||
| 36 | 15 | my ( $data, $from ) = @_; | |||||||||||||||||||||||||||
| 37 | 15 | my $i0 = rindex $$data, "\n", $from; | |||||||||||||||||||||||||||
| 38 | 15 | ▶ | return $i0 + 1 unless -1 == $i0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 0 | return 0; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 15 | ▶ | sub _ftail ($$) { | ||||||||||||||||||||||||||
|
Subroutine
_ftail: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||
| 43 | 15 | my ( $data, $from ) = @_; | |||||||||||||||||||||||||||
| 44 | 15 | my $i0 = index $$data, "\n", $from; | |||||||||||||||||||||||||||
| 45 | 15 | ▶ | return $i0 unless -1 == $i0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 0 | return -1 + length $$data; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 11 | ▶ | sub parent { | ||||||||||||||||||||||||||
|
Subroutine
parent: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 50 | 11 | my $self = shift; | |||||||||||||||||||||||||||
| 51 | 11 | ▶ | if ( my $parent = $self->_parent ) { return $parent } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 0 | return $self; # We are the base (root) split | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 5 | ▶ | sub is_root { | ||||||||||||||||||||||||||
|
Subroutine
is_root: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 56 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 57 | 5 | return ! $self->_parent; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 35 | ▶ | sub _strip_edness ($) { | ||||||||||||||||||||||||||
|
Subroutine
_strip_edness: called CC=5 SCAR=17.5
| |||||||||||||||||||||||||||||
| 61 | 35 | my $slurp = $_[0]; | |||||||||||||||||||||||||||
| 62 | $slurp->{chomp} = delete $slurp->{chomped} if | ||||||||||||||||||||||||||||
| 63 | 35 | ▶ | exists $slurp->{chomped} && not exists $slurp->{chomp}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $slurp->{"chomped"} and not exists $slurp->{"chomp"}
MC/DC0%0/2
exists $slurp->{"chomped"} and not exists $slurp->{"chomp"}
exists $slurp->{"chomped"}
not exists $slurp->{"chomp"}
Truth table33%1/3
A and not(B)
A= exists $slurp->{"chomped"}B= not exists $slurp->{"chomp"}
| |||||||||||||||||||||||||||||
| 64 | $slurp->{trim} = delete $slurp->{trimmed} if | ||||||||||||||||||||||||||||
| 65 | 35 | ▶ | exists $slurp->{trimmed} && not exists $slurp->{trim}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $slurp->{"trimmed"} and not exists $slurp->{"trim"}
MC/DC50%1/2
exists $slurp->{"trimmed"} and not exists $slurp->{"trim"}
exists $slurp->{"trimmed"}
not exists $slurp->{"trim"}
Truth table66%2/3
A and not(B)
A= exists $slurp->{"trimmed"}B= not exists $slurp->{"trim"}
| |||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 22 | ▶ | sub _parse_slurp ($@) { | ||||||||||||||||||||||||||
|
Subroutine
_parse_slurp: called CC=8 SCAR=21.6
| |||||||||||||||||||||||||||||
| 69 | 22 | my $slurp = shift; | |||||||||||||||||||||||||||
| 70 | 22 | my %slurp = @_; # Can/will be overidden | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 22 | _strip_edness \%slurp; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 22 | ▶ | if ( ref $slurp eq 'HASH' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 0 | $slurp = { %$slurp }; | |||||||||||||||||||||||||||
| 76 | 0 | _strip_edness $slurp; | |||||||||||||||||||||||||||
| 77 | 0 | %slurp = ( %slurp, %$slurp ); | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | else { | ||||||||||||||||||||||||||||
| 80 | 22 | ▶ | $slurp =~ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | m{^ | ||||||||||||||||||||||||||||
| 82 | ([\@\$])? | ||||||||||||||||||||||||||||
| 83 | ([\(\[]) | ||||||||||||||||||||||||||||
| 84 | ([\)\]]) | ||||||||||||||||||||||||||||
| 85 | (/)? | ||||||||||||||||||||||||||||
| 86 | }x or die "Invalid slurp pattern ($slurp)"; | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 22 | ▶ | $slurp{wantlist} = $1 eq '@' ? 1 : 0 if $1; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 89 | 22 | ▶ | $slurp{slurpl} = $2 eq '[' ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 22 | ▶ | $slurp{slurpr} = $3 eq ']' ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 22 | ▶ | $slurp{chomp} = 1 if $4; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 22 | return %slurp; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 12 | ▶ | sub find { | ||||||||||||||||||||||||||
|
Subroutine
find: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 98 | 12 | return shift->split( @_ ); | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 16 | ▶ | sub split { | ||||||||||||||||||||||||||
|
Subroutine
split: called CC=8 SCAR=20.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 102 | 16 | my $self = shift; | |||||||||||||||||||||||||||
| 103 | 16 | my $matcher; | |||||||||||||||||||||||||||
| 104 | 16 | ▶ | $matcher = shift if @_ % 2; # Odd number of arguments | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 16 | my %given = @_; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 16 | my $data = $self->data; | |||||||||||||||||||||||||||
| 108 | 16 | my $length = length $$data; | |||||||||||||||||||||||||||
| 109 | 16 | ▶ | return unless $length; # Nothing to split | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 16 | ▶ | my $from = $self->_parent ? $self->tail + 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 16 | ▶ | return if $length <= $from; # Was already at end of data | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 15 | pos $data = $from; | |||||||||||||||||||||||||||
| 115 | 15 | ▶ | return unless $$data =~ m/\G[[:ascii:]]*?($matcher)/mgc; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 15 | my @match = map { substr $$data, $-[$_], $+[$_] - $-[$_] } ( 0 .. -1 + scalar @- ); | |||||||||||||||||||||||||||
| 117 | 15 | shift @match; | |||||||||||||||||||||||||||
| 118 | 15 | my $found = shift @match; | |||||||||||||||||||||||||||
| 119 | 15 | my ( $mhead, $mtail ) = ( $-[1], $+[1] - 1 ); | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 15 | my $head = _fhead $data, $mhead; | |||||||||||||||||||||||||||
| 122 | 15 | my $tail = _ftail $data, $mtail; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | # TODO This is hacky | ||||||||||||||||||||||||||||
| 125 | 15 | my @matched = @match; | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 15 | my $content = substr $$data, $head, 1 + $tail - $head; | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 15 | my $split = __PACKAGE__->new( | |||||||||||||||||||||||||||
| 130 | data => $data, parent => $self, | ||||||||||||||||||||||||||||
| 131 | start => $from, mhead => $mhead, mtail => $mtail, head => $head, tail => $tail, | ||||||||||||||||||||||||||||
| 132 | matcher => $matcher, found => $found, matched => \@matched, | ||||||||||||||||||||||||||||
| 133 | content => $content, | ||||||||||||||||||||||||||||
| 134 | default => $self->default, | ||||||||||||||||||||||||||||
| 135 | ); | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 15 | ▶ | return $split unless wantarray && ( my $slurp = delete $given{slurp} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
wantarray and my $slurp = delete $given{"slurp"}
MC/DC50%1/2
wantarray and my $slurp = delete $given{"slurp"}
wantarray
my $slurp = delete $given{"slurp"}
Truth table66%2/3
A and B
A= wantarrayB= my $slurp = delete $given{"slurp"}
| |||||||||||||||||||||||||||||
| 138 | 3 | return ( $split, $split->slurp( $slurp, %given ) ); | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 13 | ▶ | sub slurp { | ||||||||||||||||||||||||||
|
Subroutine
slurp: called CC=12 SCAR=24.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 142 | 13 | my $self = shift; | |||||||||||||||||||||||||||
| 143 | 13 | my $slurp = 1; | |||||||||||||||||||||||||||
| 144 | 13 | ▶ | $slurp = shift if @_ % 2; # Odd number of arguments | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 13 | my %given = @_; | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 13 | my $split = $self; | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 13 | _strip_edness \%given; | |||||||||||||||||||||||||||
| 150 | 13 | my %slurp = _parse_slurp $self->default->{slurp}; | |||||||||||||||||||||||||||
| 151 | 13 | ▶ | exists $given{$_} and $slurp{$_} = $given{$_} for qw/ chomp trim /; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 13 | ▶ | %slurp = _parse_slurp $slurp, %slurp unless $slurp eq 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 13 | my @content; | |||||||||||||||||||||||||||
| 155 | 13 | ▶ | push @content, $self->parent->content if $slurp{slurpl}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 13 | push @content, $split->preceding; | |||||||||||||||||||||||||||
| 157 | 13 | ▶ | push @content, $split->content if $slurp{slurpr}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 13 | my $content = join '', @content; | |||||||||||||||||||||||||||
| 160 | 13 | ▶ | if ( $slurp{trim} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | 3 | s/^\s*//, s/\s*$//, for $content; | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 13 | ▶ | if ( wantarray && $slurp{wantlist} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
wantarray and $slurp{"wantlist"}
MC/DC100%2/2
wantarray and $slurp{"wantlist"}
wantarray
$slurp{"wantlist"}
Truth table100%3/3
A and B
A= wantarrayB= $slurp{"wantlist"}
| |||||||||||||||||||||||||||||
| 165 | 4 | @content = grep { $_ ne "\n" } split m/(\n)/, $content; | |||||||||||||||||||||||||||
| 166 | 0 | ▶ | @content = map { "$_\n" } @content unless $slurp{chomp}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | 4 | return @content; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | else { | ||||||||||||||||||||||||||||
| 170 | 9 | return $content; | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 17 | ▶ | sub preceding { | ||||||||||||||||||||||||||
|
Subroutine
preceding: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 175 | 17 | my $self = shift; | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 17 | my $data = $self->data; | |||||||||||||||||||||||||||
| 178 | 17 | my $length = $self->head - $self->start; | |||||||||||||||||||||||||||
| 179 | 17 | ▶ | return '' unless $length; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | 16 | return substr $$data, $self->start, $length; | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | 0 | ▶ | sub pre { return shift->preceding( @_ ) } | ||||||||||||||||||||||||||
|
Subroutine
pre: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 5 | ▶ | sub remaining { | ||||||||||||||||||||||||||
|
Subroutine
remaining: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 185 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 5 | my $data = $self->data; | |||||||||||||||||||||||||||
| 188 | 5 | ▶ | return $$data if $self->is_root; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 4 | my $from = $self->tail + 1; | |||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 4 | my $length = length( $$data ) - $from + 1; | |||||||||||||||||||||||||||
| 193 | 4 | ▶ | return '' unless $length; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | 4 | return substr $$data, $from, $length; | |||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | 0 | ▶ | sub re { return shift->remaining( @_ ) } | ||||||||||||||||||||||||||
|
Subroutine
re: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 4 | ▶ | sub match { | ||||||||||||||||||||||||||
|
Subroutine
match: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 199 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 200 | 4 | my $ii = shift; | |||||||||||||||||||||||||||
| 201 | 4 | ▶ | return $self->found if $ii == -1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 202 | 4 | return $self->_matched->[$ii]; | |||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 0 | ▶ | sub is { | ||||||||||||||||||||||||||
|
Subroutine
is: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 206 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 207 | 0 | my $ii = shift; | |||||||||||||||||||||||||||
| 208 | 0 | my $is = shift; | |||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 0 | ▶ | return unless defined ( my $match = $self->match( $ii ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 211 | 0 | ▶ | if ( ref $is eq 'Regexp' ) { $match =~ $is } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 212 | 0 | else { return $match eq $is } | |||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 4 | 1; | |||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | __END__ | ||||||||||||||||||||||||||||