| 1 | package PICA::Patch; | ||||||||||||||||||||||||||||
| 2 | 16 | ▶ | use v5.14.1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 16 | our $VERSION = '2.09'; | |||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 16 | ▶ | use PICA::Schema qw(field_identifier); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 16 | ▶ | use PICA::Data::Field; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 16 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 16 | our @EXPORT_OK = qw(pica_diff pica_patch); | |||||||||||||||||||||||||||
| 11 | 16 | our %EXPORT_TAGS = (all => [@EXPORT_OK]); | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | # Compare full fields, ignoring annotation of the latter | ||||||||||||||||||||||||||||
| 14 | # note this does not strip occurrence from level 2 records! | ||||||||||||||||||||||||||||
| 15 | sub cmp_fields { | ||||||||||||||||||||||||||||
| 16 | 15 | ▶ | my @a = @{$_[0]}; | ||||||||||||||||||||||||||
|
Subroutine
cmp_fields: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 17 | 15 | my @b = @{$_[1]}; | |||||||||||||||||||||||||||
| 18 | 15 | ▶ | pop @b if @b % 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | 15 | return join("\t", @a) cmp join("\t", @b); | |||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | sub sorted_fields { | ||||||||||||||||||||||||||||
| 23 | 39 | ▶ | my $fields = PICA::Data::pica_fields(PICA::Data::pica_sort(shift)); | ||||||||||||||||||||||||||
|
Subroutine
sorted_fields: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 39 | ▶ | if (@$fields) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 35 | my $level = substr $fields->[0][0], 0, 1; | |||||||||||||||||||||||||||
| 27 | 35 | for (@$fields) { | |||||||||||||||||||||||||||
| 28 | 49 | ▶ | die "diff/patch only allowed on atomic records\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | if $level ne substr $_->[0], 0, 1; | ||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 38 | return $fields; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 16 | *annotation = *PICA::Data::pica_annotation; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | sub pica_diff { | ||||||||||||||||||||||||||||
| 39 | 7 | ▶ | my $a = sorted_fields(shift); | ||||||||||||||||||||||||||
|
Subroutine
pica_diff: called CC=10 SCAR=23.1
Pod
documented
| |||||||||||||||||||||||||||||
| 40 | 7 | my $b = sorted_fields(shift); | |||||||||||||||||||||||||||
| 41 | 7 | my %options = @_; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 7 | my (@diff, $i, $j); | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | my $changed = sub { | ||||||||||||||||||||||||||||
| 46 | 8 | ▶ | my $field = PICA::Data::Field->new(@{$_[0]}); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 47 | 8 | $field->annotation($_[1]); | |||||||||||||||||||||||||||
| 48 | 8 | push @diff, $field; | |||||||||||||||||||||||||||
| 49 | 7 | }; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 7 | ▶ | while ($i < @$a && $j < @$b) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$i < @$a and $j < @$b
MC/DC100%2/2
$i < @$a and $j < @$b
$i < @$a
$j < @$b
Truth table100%3/3
A and B
A= $i < @$aB= $j < @$b
| |||||||||||||||||||||||||||||
| 52 | 6 | my $cmp = cmp_fields($a->[$i], $b->[$j]); | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 6 | ▶ | if ($cmp < 0) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 55 | 1 | $changed->($a->[$i++], '-'); | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | elsif ($cmp > 0) { | ||||||||||||||||||||||||||||
| 58 | 0 | $changed->($b->[$j++], '+'); | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | else { | ||||||||||||||||||||||||||||
| 61 | 5 | ▶ | push @diff, $a->[$i] if $options{keep}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 5 | $i++; | |||||||||||||||||||||||||||
| 63 | 5 | $j++; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | 7 | ▶ | while ($i < @$a) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 4 | $changed->($a->[$i++], '-'); | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | 7 | ▶ | while ($j < @$b) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 3 | $changed->($b->[$j++], '+'); | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | # remove identical fields (could also be done in sort_fields) | ||||||||||||||||||||||||||||
| 74 | 7 | ▶ | for (my $i = 0; $i < $#diff;) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 2 | ▶ | if ($diff[$i]->equal($diff[$i + 1])) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 1 | splice @diff, $i + 1, 1; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | else { | ||||||||||||||||||||||||||||
| 79 | 1 | $i++; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 7 | bless {record => \@diff}, 'PICA::Data'; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | sub no_match { | ||||||||||||||||||||||||||||
| 87 | 1 | ▶ | my $field = shift; | ||||||||||||||||||||||||||
|
Subroutine
no_match: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 88 | 1 | annotation($field, undef); | |||||||||||||||||||||||||||
| 89 | 1 | die "records don't match, expected: " . PICA::Data::pica_string([$field]); | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | sub pica_patch { | ||||||||||||||||||||||||||||
| 93 | 13 | ▶ | my $fields = sorted_fields(shift); | ||||||||||||||||||||||||||
|
Subroutine
pica_patch: called CC=17 SCAR=28.9
Pod
documented
| |||||||||||||||||||||||||||||
| 94 | 12 | my $diff = sorted_fields(shift); | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 12 | for (map {annotation($_)} @$diff) { | |||||||||||||||||||||||||||
| 97 | 14 | ▶ | die "invalid PICA Patch annotation: $_\n" if $_ !~ /^[ +-]$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 12 | my ($i, $j) = (0, 0); | |||||||||||||||||||||||||||
| 101 | 12 | ▶ | PATCH: while ($i < @$fields && $j < @$diff) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$i < @$fields and $j < @$diff
MC/DC100%2/2
$i < @$fields and $j < @$diff
$i < @$fields
$j < @$diff
Truth table100%3/3
A and B
A= $i < @$fieldsB= $j < @$diff
| |||||||||||||||||||||||||||||
| 102 | 11 | my $cur; | |||||||||||||||||||||||||||
| 103 | 11 | my $next = field_identifier($diff->[$j]); | |||||||||||||||||||||||||||
| 104 | 11 | my $ann = annotation($diff->[$j]); | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | # while current field is behind or same | ||||||||||||||||||||||||||||
| 107 | 11 | ▶ | while (($cur = field_identifier($fields->[$i])) le $next) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 13 | ▶ | if ($cur eq $next && !cmp_fields($fields->[$i], $diff->[$j])) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$cur eq $next and not cmp_fields($fields->[$i], $diff->[$j])
MC/DC100%2/2
$cur eq $next and not cmp_fields($fields->[$i], $diff->[$j])
$cur eq $next
not cmp_fields($fields->[$i], $diff->[$j])
Truth table100%3/3
A and not(B)
A= $cur eq $nextB= not cmp_fields($fields->[$i], $diff->[$j])
| |||||||||||||||||||||||||||||
| 109 | 7 | ▶ | if ($ann eq '-') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 4 | splice @$fields, $i, 1; | |||||||||||||||||||||||||||
| 111 | 4 | ▶ | last PATCH if $j++ == @$diff or $i == @$fields; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$j++ == @$diff or $i == @$fields
MC/DC0%0/2
$j++ == @$diff or $i == @$fields
$j++ == @$diff
$i == @$fields
Truth table33%1/3
A or B
A= $j++ == @$diffB= $i == @$fields
| |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | else { | ||||||||||||||||||||||||||||
| 115 | 3 | $i++; | |||||||||||||||||||||||||||
| 116 | 3 | $j++; | |||||||||||||||||||||||||||
| 117 | 3 | ▶ | last PATCH if $i >= @$fields or $j >= @$diff; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$i >= @$fields or $j >= @$diff
MC/DC0%0/2
$i >= @$fields or $j >= @$diff
$i >= @$fields
$j >= @$diff
Truth table66%2/3
A or B
A= $i >= @$fieldsB= $j >= @$diff
| |||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 0 | $next = field_identifier($diff->[$j]); | |||||||||||||||||||||||||||
| 121 | 0 | $ann = annotation($diff->[$j]); | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | else { | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | # keep current field | ||||||||||||||||||||||||||||
| 127 | 6 | ▶ | last PATCH if ++$i == @$fields; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | # current field is ahead | ||||||||||||||||||||||||||||
| 132 | 1 | ▶ | if ($ann eq '+') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 1 | my $add = $diff->[$j++]; | |||||||||||||||||||||||||||
| 134 | 1 | annotation($add, undef); | |||||||||||||||||||||||||||
| 135 | 1 | splice @$fields, $i++, 0, $add; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | else { | ||||||||||||||||||||||||||||
| 138 | 0 | no_match($diff->[$j]); | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 12 | ▶ | while ($j < @$diff) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 6 | ▶ | if (annotation($diff->[$j]) eq '+') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 5 | $fields->[$i] = $diff->[$j++]; | |||||||||||||||||||||||||||
| 145 | 5 | annotation($fields->[$i++], undef); | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | else { | ||||||||||||||||||||||||||||
| 148 | 1 | no_match($diff->[$j]); | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 11 | bless {record => $fields}, 'PICA::Data'; | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | 16 | 1; | |||||||||||||||||||||||||||
| 155 | __END__ | ||||||||||||||||||||||||||||