| 1 | package Template::Reverse; | ||||||||||
| 2 | |||||||||||
| 3 | # ABSTRACT: A template generator getting different parts between pair of text | ||||||||||
| 4 | |||||||||||
| 5 | 9 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 9 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 9 | ▶ | use Template::Reverse::Part; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 9 | ▶ | use Algorithm::Diff qw(sdiff); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 9 | ▶ | use Scalar::Util qw(blessed); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 9 | our $VERSION = '0.143'; # VERSION | |||||||||
| 11 | |||||||||||
| 12 | |||||||||||
| 13 | has 'sidelen' => ( | ||||||||||
| 14 | is=>'rw', | ||||||||||
| 15 | 2 | ▶ | default => sub{return 10;} | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 9 | ); | |||||||||
| 17 | |||||||||||
| 18 | |||||||||||
| 19 | 9 | my $_WILDCARD = bless [], 'WILDCARD'; | |||||||||
| 20 | 52 | ▶ | sub WILDCARD{return $_WILDCARD}; | ||||||||
|
Subroutine
WILDCARD: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 21 | sub _isWILDCARD{ | ||||||||||
| 22 | 387 | ▶ | return ref $_[0] eq 'WILDCARD'; | ||||||||
|
Subroutine
_isWILDCARD: called CC=1 SCAR=0.0
| |||||||||||
| 23 | } | ||||||||||
| 24 | |||||||||||
| 25 | |||||||||||
| 26 | sub detect{ | ||||||||||
| 27 | 4 | ▶ | my $self = shift; | ||||||||
|
Subroutine
detect: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 28 | 4 | my @strs = @_; | |||||||||
| 29 | 4 | my $diff = _diff($strs[0],$strs[1]); | |||||||||
| 30 | 4 | my $pattern = _detect($diff,$self->sidelen()); | |||||||||
| 31 | 4 | return $pattern; | |||||||||
| 32 | } | ||||||||||
| 33 | |||||||||||
| 34 | |||||||||||
| 35 | ### internal functions | ||||||||||
| 36 | sub _detect{ | ||||||||||
| 37 | 29 | ▶ | my $diff = shift; | ||||||||
|
Subroutine
_detect: called CC=11 SCAR=24.0
| |||||||||||
| 38 | 29 | my $sidelen = shift; | |||||||||
| 39 | 29 | ▶ | $sidelen = 0 unless $sidelen; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | 29 | my @d = @{$diff}; | |||||||||
| 41 | 29 | my $lastStar = 0; | |||||||||
| 42 | 29 | my @res; | |||||||||
| 43 | 29 | ▶ | for(my $i=0; $i<@d; $i++) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | { | ||||||||||
| 45 | 230 | ▶ | if( _isWILDCARD($d[$i] ) ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | { | ||||||||||
| 47 | 45 | my $from = $lastStar; | |||||||||
| 48 | 45 | my $to = $i-1; | |||||||||
| 49 | 45 | ▶ | if( $sidelen ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | 19 | ▶ | $from = $to-$sidelen+1 if $to-$from+1 > $sidelen; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | } | ||||||||||
| 52 | 45 | my @pre = @d[$from..$to]; | |||||||||
| 53 | |||||||||||
| 54 | 45 | my $j = @d; | |||||||||
| 55 | 45 | ▶ | if( $i+1 < @d ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | 37 | ▶ | for( $j=$i+1; $j<@d; $j++) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | { | ||||||||||
| 58 | 131 | ▶ | if( _isWILDCARD( $d[$j] ) ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | 19 | last; | |||||||||
| 60 | } | ||||||||||
| 61 | } | ||||||||||
| 62 | } | ||||||||||
| 63 | 45 | $from = $i+1; | |||||||||
| 64 | 45 | $to = $j-1; | |||||||||
| 65 | 45 | ▶ | if( $sidelen ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | 19 | ▶ | $to = $from + $sidelen-1 if $to-$from+1 > $sidelen; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 67 | } | ||||||||||
| 68 | 45 | my @post = @d[$from..$to]; | |||||||||
| 69 | 45 | my $part = Template::Reverse::Part->new(pre=>\@pre, post=>\@post); | |||||||||
| 70 | 45 | push(@res,$part); | |||||||||
| 71 | 45 | $lastStar = $i+1; | |||||||||
| 72 | } | ||||||||||
| 73 | } | ||||||||||
| 74 | 29 | return \@res; | |||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | |||||||||||
| 78 | sub _diff{ | ||||||||||
| 79 | 16 | ▶ | my ($a,$b) = @_; | ||||||||
|
Subroutine
_diff: called CC=7 SCAR=19.5
| |||||||||||
| 80 | 16 | my ($org_a,$org_b) = @_; | |||||||||
| 81 | |||||||||||
| 82 | 16 | ▶ | $a = [map{blessed($_)?$_->as_string:$_}@{$a}]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | 16 | ▶ | $b = [map{blessed($_)?$_->as_string:$_}@{$b}]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | |||||||||||
| 85 | 16 | my @d = sdiff($a,$b); | |||||||||
| 86 | 16 | my @rr; | |||||||||
| 87 | 16 | my $before=''; | |||||||||
| 88 | 16 | my $idx = 0; | |||||||||
| 89 | 16 | for my $r (@d){ | |||||||||
| 90 | 90 | ▶ | if( $r->[0] eq 'u' ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 91 | 64 | push(@rr,$org_a->[$idx]); | |||||||||
| 92 | 64 | $before = ''; | |||||||||
| 93 | } | ||||||||||
| 94 | else{ | ||||||||||
| 95 | 26 | ▶ | push(@rr,WILDCARD) unless _isWILDCARD($before); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | 26 | $before = WILDCARD; | |||||||||
| 97 | } | ||||||||||
| 98 | 90 | ▶ | $idx++ if $r->[0] ne '+'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 99 | |||||||||||
| 100 | } | ||||||||||
| 101 | 16 | return \@rr; | |||||||||
| 102 | } | ||||||||||
| 103 | |||||||||||
| 104 | |||||||||||
| 105 | |||||||||||
| 106 | 9 | 1; | |||||||||
| 107 | |||||||||||
| 108 | __END__ | ||||||||||