Diff3.pm

filter: Statementstmt 87.2% 212 / 243 Branchbran 90.9% 60 / 66 Conditioncond 75.0% 27 / 36 MC/DCmcdc 60.8% 14 / 23 Subroutinesub 100% 12 / 12 Podpod 100% 3 / 3 totaltotal 85.6% 328 / 383 CC 67 SCAR 28.1
CC
67
Cov
87%
_conflict_range
23.1
_diff_heckel
21.1
diff3
20.3
CRAP 16.6
« Diff3/DiffHeckel.pm ↑ Summary Diff3/Diff3.pm »
1 package Text::Diff3;
2 10 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 10 ▶use version; our $VERSION = '0.10';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 10 our @EXPORT_OK = qw(diff3 merge diff);
9  
10 # Two way diff procedure for function style diff3.
11 # Change this as your like.
12 10 our $DIFF_PROC = \&_diff_heckel;
13  
14 94 ▶sub diff { return $DIFF_PROC->(@_) }
Subroutine diff: called CC=1 SCAR=0.0
Pod documented
15  
16 10 ▶sub import {
Subroutine import: called CC=6 SCAR=19.1
17 10 my($class, @arg) = @_;
18 10 my $pkg = caller;
19 10 my %opt = map { $_ => 1 } @arg;
20 10 my %export;
21 10 ▶ if (exists $opt{':factory'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opt{":factory"}) 6 4 10
22 # compatibility for old version's component style interface.
23 6 warn "Text::Diff3 ':factory' is deprecated.\n";
24 6 require Text::Diff3::Factory;
25 6 return;
26 }
27 4 ▶ if (exists $opt{':all'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opt{':all'}) { } 0 4 4
28 0 %export = map { $_ => 1 } @EXPORT_OK;
29 }
30 else {
31 4 for my $func (@EXPORT_OK) {
32 12 ▶ if (exists $opt{$func}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opt{$func}) 0 12 12
33 0 $export{$func} = 1;
34 }
35 }
36 }
37 4 for my $func (keys %export) {
38 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
39 0 *{"${pkg}::${func}"} = \&{"${class}::${func}"};
40 }
41 4 return;
42 }
43  
44 # the three-way diff based on the GNU diff3.c by R. Smith.
45 42 ▶sub diff3 {
Subroutine diff3: called CC=20 SCAR=30.1
Pod documented
46 42 my($text0, $text2, $text1) = @_;
47 # diff result => [[$cmd, $loA, $hiA, $loB, $hiB], ...]
48 42 my @diff2 = (
49 diff($text2, $text0),
50 diff($text2, $text1),
51 );
52 42 my $diff3 = [];
53 42 my $range3 = [undef, 0, 0, 0, 0, 0, 0];
54 42 ▶ while (@{$diff2[0]} || @{$diff2[1]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$diff2[0];} or @{$diff2[1];}) 66 42 108
Condition100%3/3
@{$diff2[0];} or @{$diff2[1];}
l!l&&r!l&&!r
462042
MC/DC100%2/2
@{$diff2[0];} or @{$diff2[1];}
@{$diff2[0];} @{$diff2[1];}
Truth table100%3/3
A or B
A= @{$diff2[0];}B= @{$diff2[1];}
ABresult
1X1
011
000
55 # find a continual range in text2 $lo2..$hi2
56 # changed by text0 or by text1.
57 #
58 # diff2[0] 222 222222222
59 # text2 ...L!!!!!!!!!!!!!!!!!!!!H...
60 # diff2[1] 222222 22 2222222
61 66 my @range2 = ([], []);
62 my $i =
63 66 ▶ ! @{$diff2[0]} ? 1
Branch: 1 branch
BranchTrueFalseTotal
!@{$diff2[1];} ? : 18 28 46
64 66 ▶ : ! @{$diff2[1]} ? 0
Branch: 2 branches
BranchTrueFalseTotal
!@{$diff2[0];} ? : 20 46 66
$diff2[0][0][1] <= $diff2[1][0][1] ? : 24 4 28
65 : $diff2[0][0][1] <= $diff2[1][0][1] ? 0
66 : 1;
67 66 my $j = $i;
68 66 my $k = $i ^ 1;
69 66 my $hi = $diff2[$j][0][2];
70 66 push @{$range2[$j]}, shift @{$diff2[$j]};
71 66 ▶ while (@{$diff2[$k]} && $diff2[$k][0][1] <= $hi + 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$diff2[$k];} and $diff2[$k][0][1] <= $hi + 1) 20 66 86
Condition100%3/3
@{$diff2[$k];} and $diff2[$k][0][1] <= $hi + 1
!ll&&!rl&&r
501620
MC/DC100%2/2
@{$diff2[$k];} and $diff2[$k][0][1] <= $hi + 1
@{$diff2[$k];} $diff2[$k][0][1] <= $hi + 1
Truth table100%3/3
A and B
A= @{$diff2[$k];}B= $diff2[$k][0][1] <= $hi + 1
ABresult
0X0
100
111
72 20 my $hi_k = $diff2[$k][0][2];
73 20 push @{$range2[$k]}, shift @{$diff2[$k]};
74 20 ▶ if ($hi < $hi_k) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hi < $hi_k) 0 20 20
75 0 $hi = $hi_k;
76 0 $j = $k;
77 0 $k = $k ^ 1;
78 }
79 }
80 66 my $lo2 = $range2[$i][ 0][1];
81 66 my $hi2 = $range2[$j][-1][2];
82 # take the corresponding ranges in text0 $lo0..$hi0
83 # and in text1 $lo1..$hi1.
84 #
85 # text0 ..L!!!!!!!!!!!!!!!!!!!!!!!!!!!!H...
86 # diff2[0] 222 222222222
87 # text2 ...00!1111!000!!00!111111...
88 # diff2[1] 222222 22 2222222
89 # text1 ...L!!!!!!!!!!!!!!!!H...
90 66 my($lo0, $hi0);
91 66 ▶ if (@{$range2[0]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$range2[0];}) { } 42 24 66
92 42 $lo0 = $range2[0][ 0][3] - $range2[0][ 0][1] + $lo2;
93 42 $hi0 = $range2[0][-1][4] - $range2[0][-1][2] + $hi2;
94 }
95 else {
96 24 $lo0 = $range3->[2] - $range3->[6] + $lo2;
97 24 $hi0 = $range3->[2] - $range3->[6] + $hi2;
98 }
99 66 my($lo1, $hi1);
100 66 ▶ if (@{$range2[1]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$range2[1];}) { } 44 22 66
101 44 $lo1 = $range2[1][ 0][3] - $range2[1][ 0][1] + $lo2;
102 44 $hi1 = $range2[1][-1][4] - $range2[1][-1][2] + $hi2;
103 }
104 else {
105 22 $lo1 = $range3->[4] - $range3->[6] + $lo2;
106 22 $hi1 = $range3->[4] - $range3->[6] + $hi2;
107 }
108 66 $range3 = [undef, $lo0, $hi0, $lo1, $hi1, $lo2, $hi2];
109 # detect type of changes.
110 66 ▶ if (! @{$range2[0]}) {
Branch: 3 branches
BranchTrueFalseTotal
if (not @{$range2[0];}) { } 24 42 66
elsif (not @{$range2[1];}) { } 22 20 42
elsif ($hi0 - $lo0 != $hi1 - $lo1) { } 4 16 20
111 24 $range3->[0] = '1';
112 }
113 42 elsif (! @{$range2[1]}) {
114 22 $range3->[0] = '0';
115 }
116 elsif ($hi0 - $lo0 != $hi1 - $lo1) {
117 4 $range3->[0] = 'A';
118 }
119 else {
120 16 $range3->[0] = '2';
121 16 for my $d (0 .. $hi0 - $lo0) {
122 26 my($i0, $i1) = ($lo0 + $d - 1, $lo1 + $d - 1);
123 26 ▶ my $ok0 = 0 <= $i0 && $i0 <= $#{$text0};
Condition33%1/3
0 <= $i0 && $i0 <= $#{$text0;}
!ll&&!rl&&r
0026
MC/DC0%0/2
0 <= $i0 && $i0 <= $#{$text0;}
0 <= $i0 $i0 <= $#{$text0;}
Truth table33%1/3
A && B
A= 0 <= $i0B= $i0 <= $#{$text0;}
ABresult
0X0
100
111
124 26 ▶ my $ok1 = 0 <= $i1 && $i1 <= $#{$text1};
Condition33%1/3
0 <= $i1 && $i1 <= $#{$text1;}
!ll&&!rl&&r
0026
MC/DC0%0/2
0 <= $i1 && $i1 <= $#{$text1;}
0 <= $i1 $i1 <= $#{$text1;}
Truth table33%1/3
A && B
A= 0 <= $i1B= $i1 <= $#{$text1;}
ABresult
0X0
100
111
125 26 ▶ if ($ok0 ^ $ok1 || ($ok0 && $text0->[$i0] ne $text1->[$i1])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok0 ^ $ok1 or $ok0 and $text0->[$i0] ne $text1->[$i1]) 4 22 26
Condition66%4/6
$ok0 ^ $ok1 or $ok0 and $text0->[$i0] ne $text1->[$i1]
l!l&&r!l&&!r
0422
$ok0 and $text0->[$i0] ne $text1->[$i1]
!ll&&!rl&&r
0224
MC/DC33%1/3
$ok0 ^ $ok1 or $ok0 and $text0->[$i0] ne $text1->[$i1]
$ok0 ^ $ok1 $ok0 $text0->[$i0] ne $text1->[$i1]
Truth table50%2/4
A or B and C
A= $ok0 ^ $ok1B= $ok0C= $text0->[$i0] ne $text1->[$i1]
ABCresult
1XX1
0111
00X0
0100
126 4 $range3->[0] = 'A';
127 4 last;
128 }
129 }
130 }
131 66 push @{$diff3}, $range3;
132 }
133 42 return $diff3;
134 }
135  
136 21 ▶sub merge {
Subroutine merge: called CC=8 SCAR=20.8
Pod documented
137 21 my($mytext, $origtext, $yourtext) = @_;
138 21 my $text3 = [$mytext, $yourtext, $origtext];
139 21 my $res = {conflict => 0, body => []};
140 21 my $diff3 = diff3(@{$text3}[0, 2, 1]);
141 21 my $i2 = 1;
142 21 for my $r3 (@{$diff3}) {
143 33 for my $lineno ($i2 .. $r3->[5] - 1) {
144 50 push @{$res->{body}}, $text3->[2][$lineno - 1];
145 }
146 33 ▶ if ($r3->[0] eq '0') {
Branch: 2 branches
BranchTrueFalseTotal
if ($r3->[0] eq '0') { } 11 22 33
elsif ($r3->[0] ne 'A') { } 18 4 22
147 11 for my $lineno ($r3->[1] .. $r3->[2]) {
148 15 push @{$res->{body}}, $text3->[0][$lineno - 1];
149 }
150 }
151 elsif ($r3->[0] ne 'A') {
152 18 for my $lineno ($r3->[3] .. $r3->[4]) {
153 26 push @{$res->{body}}, $text3->[1][$lineno - 1];
154 }
155 }
156 else {
157 4 _conflict_range($text3, $r3, $res);
158 }
159 33 $i2 = $r3->[6] + 1;
160 }
161 21 for my $lineno ($i2 .. $#{$text3->[2]} + 1) {
162 42 push @{$res->{body}}, $text3->[2][$lineno - 1];
163 }
164 21 return $res;
165 }
166  
167 4 ▶sub _conflict_range {
Subroutine _conflict_range: called CC=14 SCAR=31.4
168 4 my($text3, $r3, $res) = @_;
169 my $text2 = [
170 10 [map { $text3->[1][$_ - 1] } $r3->[3] .. $r3->[4]], # yourtext
171 4 [map { $text3->[0][$_ - 1] } $r3->[1] .. $r3->[2]], # mytext
172 ];
173 4 my $diff = diff(@{$text2});
174 4 ▶ if (_assoc_range($diff, 'c') && $r3->[5] <= $r3->[6]) {
Branch: 1 branch
BranchTrueFalseTotal
if (_assoc_range($diff, "c") and $r3->[5] <= $r3->[6]) 3 1 4
Condition66%2/3
_assoc_range($diff, "c") and $r3->[5] <= $r3->[6]
!ll&&!rl&&r
103
MC/DC50%1/2
_assoc_range($diff, "c") and $r3->[5] <= $r3->[6]
_assoc_range($diff, "c") $r3->[5] <= $r3->[6]
Truth table66%2/3
A and B
A= _assoc_range($diff, "c")B= $r3->[5] <= $r3->[6]
ABresult
0X0
100
111
175 3 $res->{conflict}++;
176 3 push @{$res->{body}}, '<<<<<<<';
177 3 for my $lineno ($r3->[1] .. $r3->[2]) {
178 10 push @{$res->{body}}, $text3->[0][$lineno - 1];
179 }
180 3 push @{$res->{body}}, '|||||||';
181 3 for my $lineno ($r3->[5] .. $r3->[6]) {
182 10 push @{$res->{body}}, $text3->[2][$lineno - 1];
183 }
184 3 push @{$res->{body}}, '=======';
185 3 for my $lineno ($r3->[3] .. $r3->[4]) {
186 9 push @{$res->{body}}, $text3->[1][$lineno - 1];
187 }
188 3 push @{$res->{body}}, '>>>>>>>';
189 3 return;
190 }
191 1 my $ia = 1;
192 1 for my $r2 (@{$diff}) {
193 1 for my $lineno ($ia .. $r2->[1] - 1) {
194 0 push @{$res->{body}}, $text2->[0][$lineno - 1];
195 }
196 1 ▶ if ($r2->[0] eq 'c') {
Branch: 2 branches
BranchTrueFalseTotal
if ($r2->[0] eq 'c') { } 0 1 1
elsif ($r2->[0] eq 'a') { } 0 1 1
197 0 $res->{conflict}++;
198 0 push @{$res->{body}}, '<<<<<<<';
199 0 for my $lineno ($r2->[3] .. $r2->[4]) {
200 0 push @{$res->{body}}, $text2->[1][$lineno - 1];
201 }
202 0 push @{$res->{body}}, '=======';
203 0 for my $lineno ($r2->[1] .. $r2->[2]) {
204 0 push @{$res->{body}}, $text2->[0][$lineno - 1];
205 }
206 0 push @{$res->{body}}, '>>>>>>>';
207 }
208 elsif ($r2->[0] eq 'a') {
209 0 for my $lineno ($r2->[3] .. $r2->[4]) {
210 0 push @{$res->{body}}, $text2->[1][$lineno - 1];
211 }
212 }
213 1 $ia = $r2->[2] + 1;
214 }
215 1 for my $lineno ($ia .. $#{$text2->[0]} + 1) {
216 0 push @{$res->{body}}, $text2->[0][$lineno - 1];
217 }
218 1 return;
219 }
220  
221 4 ▶sub _assoc_range {
Subroutine _assoc_range: called CC=3 SCAR=11.0
222 4 my($diff, $type) = @_;
223 4 for my $r (@{$diff}) {
224 4 ▶ return $r if $r->[0] eq $type;
Branch: 1 branch
BranchTrueFalseTotal
if $r->[0] eq $type 3 1 4
225 }
226 1 return;
227 }
228  
229 # the two-way diff based on the algorithm by P. Heckel.
230 94 ▶sub _diff_heckel {
Subroutine _diff_heckel: called CC=21 SCAR=30.5
231 94 my($text_a, $text_b) = @_;
232 94 my $diff = [];
233 94 my @uniq = ([$#{$text_a} + 1, $#{$text_b} + 1]);
234 94 my(%freq, %ap, %bp);
235 94 for my $i (0 .. $#{$text_a}) {
236 565 my $s = $text_a->[$i];
237 565 $freq{$s} += 2;
238 565 $ap{$s} = $i;
239 }
240 94 for my $i (0 .. $#{$text_b}) {
241 591 my $s = $text_b->[$i];
242 591 $freq{$s} += 3;
243 591 $bp{$s} = $i;
244 }
245 94 ▶ while (my($s, $x) = each %freq) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %freq) 700 94 794
246 700 ▶ next if $x != 5;
Branch: 1 branch
BranchTrueFalseTotal
if $x != 5 266 434 700
247 434 push @uniq, [$ap{$s}, $bp{$s}];
248 }
249 94 %freq = (); %ap = (); %bp = ();
250 94 @uniq = sort { $a->[0] <=> $b->[0] } @uniq;
251 94 my($a1, $b1) = (0, 0);
252 94 ▶ while ($a1 <= $#{$text_a} && $b1 <= $#{$text_b}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a1 <= $#{$text_a;} and $b1 <= $#{$text_b;}) 320 43 363
Condition100%3/3
$a1 <= $#{$text_a;} and $b1 <= $#{$text_b;}
!ll&&!rl&&r
376320
MC/DC100%2/2
$a1 <= $#{$text_a;} and $b1 <= $#{$text_b;}
$a1 <= $#{$text_a;} $b1 <= $#{$text_b;}
Truth table100%3/3
A and B
A= $a1 <= $#{$text_a;}B= $b1 <= $#{$text_b;}
ABresult
0X0
100
111
253 320 ▶ last if $text_a->[$a1] ne $text_b->[$b1];
Branch: 1 branch
BranchTrueFalseTotal
if $text_a->[$a1] ne $text_b->[$b1] 51 269 320
254 269 ++$a1;
255 269 ++$b1;
256 }
257 94 for (@uniq) {
258 528 my($a_uniq, $b_uniq) = @{$_};
259 528 ▶ next if $a_uniq < $a1 || $b_uniq < $b1;
Branch: 1 branch
BranchTrueFalseTotal
if $a_uniq < $a1 or $b_uniq < $b1 356 172 528
Condition66%2/3
$a_uniq < $a1 or $b_uniq < $b1
l!l&&r!l&&!r
3560172
MC/DC50%1/2
$a_uniq < $a1 or $b_uniq < $b1
$a_uniq < $a1 $b_uniq < $b1
Truth table66%2/3
A or B
A= $a_uniq < $a1B= $b_uniq < $b1
ABresult
1X1
011
000
260 172 my($a0, $b0) = ($a1, $b1);
261 172 ($a1, $b1) = ($a_uniq - 1, $b_uniq - 1);
262 172 ▶ while ($a0 <= $a1 && $b0 <= $b1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a0 <= $a1 and $b0 <= $b1) 34 138 172
Condition100%3/3
$a0 <= $a1 and $b0 <= $b1
!ll&&!rl&&r
1112734
MC/DC100%2/2
$a0 <= $a1 and $b0 <= $b1
$a0 <= $a1 $b0 <= $b1
Truth table100%3/3
A and B
A= $a0 <= $a1B= $b0 <= $b1
ABresult
0X0
100
111
263 34 ▶ last if $text_a->[$a1] ne $text_b->[$b1];
Branch: 1 branch
BranchTrueFalseTotal
if $text_a->[$a1] ne $text_b->[$b1] 34 0 34
264 0 --$a1;
265 0 --$b1;
266 }
267 172 ▶ if ($a0 <= $a1 && $b0 <= $b1) {
Branch: 3 branches
BranchTrueFalseTotal
if ($a0 <= $a1 and $b0 <= $b1) { } 34 138 172
elsif ($a0 <= $a1) { } 27 111 138
elsif ($b0 <= $b1) { } 37 74 111
Condition100%3/3
$a0 <= $a1 and $b0 <= $b1
!ll&&!rl&&r
1112734
MC/DC100%2/2
$a0 <= $a1 and $b0 <= $b1
$a0 <= $a1 $b0 <= $b1
Truth table100%3/3
A and B
A= $a0 <= $a1B= $b0 <= $b1
ABresult
0X0
100
111
268 34 push @{$diff}, ['c', $a0 + 1, $a1 + 1, $b0 + 1, $b1 + 1];
269 }
270 elsif ($a0 <= $a1) {
271 27 push @{$diff}, ['d', $a0 + 1, $a1 + 1, $b0 + 1, $b0];
272 }
273 elsif ($b0 <= $b1) {
274 37 push @{$diff}, ['a', $a0 + 1, $a0, $b0 + 1, $b1 + 1];
275 }
276 172 ($a1, $b1) = ($a_uniq + 1, $b_uniq + 1);
277 172 ▶ while ($a1 <= $#{$text_a} && $b1 <= $#{$text_b}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a1 <= $#{$text_a;} and $b1 <= $#{$text_b;}) 117 142 259
Condition66%2/3
$a1 <= $#{$text_a;} and $b1 <= $#{$text_b;}
!ll&&!rl&&r
1420117
MC/DC50%1/2
$a1 <= $#{$text_a;} and $b1 <= $#{$text_b;}
$a1 <= $#{$text_a;} $b1 <= $#{$text_b;}
Truth table66%2/3
A and B
A= $a1 <= $#{$text_a;}B= $b1 <= $#{$text_b;}
ABresult
0X0
100
111
278 117 ▶ last if $text_a->[$a1] ne $text_b->[$b1];
Branch: 1 branch
BranchTrueFalseTotal
if $text_a->[$a1] ne $text_b->[$b1] 30 87 117
279 87 ++$a1;
280 87 ++$b1;
281 }
282 }
283 94 return $diff;
284 }
285  
286 10 1;
287  
288 __END__
« Diff3/DiffHeckel.pm ↑ Summary Diff3/Diff3.pm »