| 1 | package BoardStreams::Diff; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 4 | ▶ | use v5.44; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 4 | our $VERSION = "v0.0.59"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Mojo::JSON qw(encode_json); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 4 | our @EXPORT_OK = qw(diff patch); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | # A diff is undef (no change) or a hash with one of these shapes: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | # { v => $new } replace the value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | # { o => { $key => $subdiff, ... }, change keys of an object | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | # d => [ $deleted_key, ... ] } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | # { s => [ $start, $end ], change the ends of an array: keep | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | # p => [ $item, ... ], the old items from index $start up | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | # a => [ $item, ... ] } to, not including, $end (all of | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | # them without "s"), put the "p" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | # items before them and the "a" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | # items after them | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | # Each of "s", "p" and "a" is left out when it has nothing to say, so a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | # push is { a => [...] }, a pop { s => [0, $n - 1] }, a shift | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | # { s => [1, $n] } and an unshift { p => [...] }, and they combine. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | # The JavaScript client implements the same format in src/diff.ts. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | # How many places a run of old items is looked for in the new array. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 4 | ▶ | use constant ARRAY_TRIES => 8; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 46 | ▶ | sub diff ($old, $new) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
diff: called CC=16 SCAR=27.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 46 | ▶ | if (ref $old eq 'HASH' and ref $new eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $old eq "HASH" and ref $new eq "HASH"
MC/DC100%2/2
ref $old eq "HASH" and ref $new eq "HASH"
ref $old eq "HASH"
ref $new eq "HASH"
Truth table100%3/3
A and B
A= ref $old eq "HASH"B= ref $new eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 15 | my %changed; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 15 | for my $key (keys %$new) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 19 | ▶ | if (exists $old->{$key}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 18 | my $sub = diff($old->{$key}, $new->{$key}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 18 | ▶ | $changed{$key} = $sub if $sub; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 1 | $changed{$key} = { v => $new->{$key} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 15 | my @deleted = sort grep { !exists $new->{$_} } keys %$old; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 15 | ▶ | return undef unless %changed or @deleted; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
%changed or @deleted
MC/DC100%2/2
%changed or @deleted
%changed
@deleted
Truth table100%3/3
A or B
A= %changedB= @deleted
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 14 | ▶ | (%changed ? (o => \%changed) : ()), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | (@deleted ? (d => \@deleted) : ()), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 31 | ▶ | return undef if _json($old) eq _json($new); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 26 | ▶ | if (ref $old eq 'ARRAY' and ref $new eq 'ARRAY' and @$old and @$new) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%9/9
ref $old eq "ARRAY" and ref $new eq "ARRAY" and @$old and @$new
ref $old eq "ARRAY" and ref $new eq "ARRAY" and @$old
ref $old eq "ARRAY" and ref $new eq "ARRAY"
MC/DC100%4/4
ref $old eq "ARRAY" and ref $new eq "ARRAY" and @$old and @$new
ref $old eq "ARRAY"
ref $new eq "ARRAY"
@$old
@$new
Truth table100%5/5
A and B and C and D
A= ref $old eq "ARRAY"B= ref $new eq "ARRAY"C= @$oldD= @$new
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 19 | my $ends = _array_diff($old, $new); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 19 | ▶ | return $ends if $ends; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 9 | return { v => $new }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | # The diff for an array whose ends changed while a run of its items stayed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | # as it was: what push, pop, shift and unshift do, alone or together. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | # Returns nothing if no such run is found. The run has to start with the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | # first old item or with the first new one, which leaves out only an array | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | # that lost items at its start and gained others there at the same time. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 19 | ▶ | sub _array_diff ($old, $new) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_array_diff: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 19 | my @was = map { _json($_) } @$old; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 19 | my @is = map { _json($_) } @$new; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 19 | my ($kept, $from, $at) = (0, 0, 0); # length of the run, where it starts in old, and in new | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 21 | ▶ | my $consider = sub ($start, $position) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 21 | my $run = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 21 | ▶ | $run++ while $start + $run < @was and $position + $run < @is and $was[$start + $run] eq $is[$position + $run]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$start + $run < @was and $position + $run < @is and $was[$start + $run] eq $is[$position + $run]
$start + $run < @was and $position + $run < @is
MC/DC100%3/3
$start + $run < @was and $position + $run < @is and $was[$start + $run] eq $is[$position + $run]
$start + $run < @was
$position + $run < @is
$was[$start + $run] eq $is[$position + $run]
Truth table100%4/4
A and B and C
A= $start + $run < @wasB= $position + $run < @isC= $was[$start + $run] eq $is[$position + $run]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 21 | ▶ | ($kept, $from, $at) = ($run, $start, $position) if $run > $kept; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 19 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | # The old array from its first item on, wherever that is now: at the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | # start (push, pop) or further in (unshift). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 19 | my @positions = grep { $is[$_] eq $was[0] } 0 .. $#is; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 19 | $consider->(0, $_) for splice @positions, 0, ARRAY_TRIES; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | # The new array from its first item on, wherever that was: shift. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 19 | my @starts = grep { $was[$_] eq $is[0] } 1 .. $#was; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 19 | $consider->($_, 0) for splice @starts, 0, ARRAY_TRIES; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 19 | ▶ | return undef unless $kept; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 17 | ▶ | ($kept < @was ? (s => [$from, $from + $kept]) : ()), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | ($at ? (p => [@$new[0 .. $at - 1]]) : ()), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | ($at + $kept < @is ? (a => [@$new[$at + $kept .. $#$new]]) : ()), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 54 | ▶ | sub patch ($value, $diff) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
patch: called CC=12 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 54 | ▶ | return $value unless $diff; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 53 | ▶ | return $diff->{v} if exists $diff->{v}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 37 | ▶ | if ($diff->{s} or $diff->{p} or $diff->{a}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$diff->{"s"} or $diff->{"p"} or $diff->{"a"}
$diff->{"s"} or $diff->{"p"}
MC/DC100%3/3
$diff->{"s"} or $diff->{"p"} or $diff->{"a"}
$diff->{"s"}
$diff->{"p"}
$diff->{"a"}
Truth table100%4/4
A or B or C
A= $diff->{"s"}B= $diff->{"p"}C= $diff->{"a"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 17 | ▶ | my ($start, $end) = $diff->{s} ? $diff->{s}->@* : (0, scalar @$value); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 17 | ▶ | return [@{ $diff->{p} // [] }, @$value[$start .. $end - 1], @{ $diff->{a} // [] }]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%4/4
$diff->{"p"} // []
$diff->{"a"} // []
MC/DC100%1/1
$diff->{"p"} // []
$diff->{"p"}
MC/DC100%1/1
$diff->{"a"} // []
$diff->{"a"}
Truth table100%2/2
A
A= $diff->{"p"}
Truth table100%2/2
A
A= $diff->{"a"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 20 | my %copy = %$value; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 20 | ▶ | delete @copy{ @{ $diff->{d} // [] } }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$diff->{"d"} // []
MC/DC100%1/1
$diff->{"d"} // []
$diff->{"d"}
Truth table100%2/2
A
A= $diff->{"d"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 20 | ▶ | my $changed = $diff->{o} // {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$diff->{"o"} // {}
MC/DC100%1/1
$diff->{"o"} // {}
$diff->{"o"}
Truth table100%2/2
A
A= $diff->{"o"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 20 | $copy{$_} = patch($copy{$_}, $changed->{$_}) for keys %$changed; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 20 | return \%copy; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | # Mojo::JSON sorts hash keys, so equal structures encode identically. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 164 | ▶ | sub _json ($value) { encode_json([$value]) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_json: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||