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