| 1 | package Text::Same; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | Text::Same - Look for similarities between files or arrays | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | use Text::Same; | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | my $matchmap = compare "file_1", "file_2", { ignore_whitespace => 1 }; | ||||||||||||||||||||||||||||
| 12 | my $matchmap = compare \@records1, \@records2, { ignore_simple => 3 }; | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | or use the "psame" command: | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | psame -a file_1 file_2 | more | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | C<compare()> compares two files or arrays of strings and returns a MatchMap | ||||||||||||||||||||||||||||
| 21 | object holding the results. | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =cut | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | =cut | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 4 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 4 | ▶ | use vars qw($VERSION @ISA @EXPORT); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | 4 | ▶ | use Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 4 | @ISA = qw( Exporter ); | |||||||||||||||||||||||||||
| 36 | 4 | @EXPORT = qw( compare ); | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 4 | $VERSION = '0.07'; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 4 | ▶ | use Text::Same::Match; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 4 | ▶ | use Text::Same::ChunkPair; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | 4 | ▶ | use Text::Same::MatchMap; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | 4 | ▶ | use Text::Same::Cache; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | 4 | ▶ | use Text::Same::Util; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | sub _process_hits | ||||||||||||||||||||||||||||
| 47 | 242 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_process_hits: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 48 | 242 | my ($options, $this_chunk_indx, $seen_pairs_ref, $matching_chunk_indexes_ref, | |||||||||||||||||||||||||||
| 49 | $this_chunked_source, $other_chunked_source) = @_; | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 242 | for my $other_chunk_indx (@$matching_chunk_indexes_ref) { | |||||||||||||||||||||||||||
| 52 | 313 | my $chunk_pair = new Text::Same::ChunkPair($this_chunk_indx, | |||||||||||||||||||||||||||
| 53 | $other_chunk_indx); | ||||||||||||||||||||||||||||
| 54 | 313 | my $pair_id = $chunk_pair->packed_pair(); | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 313 | ▶ | if (!exists $seen_pairs_ref->{$pair_id}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 313 | my $this_prev_chunk_indx = | |||||||||||||||||||||||||||
| 58 | $this_chunked_source->get_previous_chunk_indx($options, | ||||||||||||||||||||||||||||
| 59 | $this_chunk_indx); | ||||||||||||||||||||||||||||
| 60 | 313 | my $other_prev_chunk_indx = | |||||||||||||||||||||||||||
| 61 | $other_chunked_source->get_previous_chunk_indx($options, | ||||||||||||||||||||||||||||
| 62 | $other_chunk_indx); | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 313 | ▶ | if (defined $this_prev_chunk_indx && defined $other_prev_chunk_indx) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $this_prev_chunk_indx and defined $other_prev_chunk_indx
MC/DC100%2/2
defined $this_prev_chunk_indx and defined $other_prev_chunk_indx
defined $this_prev_chunk_indx
defined $other_prev_chunk_indx
Truth table100%3/3
A and B
A= defined $this_prev_chunk_indxB= defined $other_prev_chunk_indx
| |||||||||||||||||||||||||||||
| 65 | 254 | my $this_prev_chunk_text = | |||||||||||||||||||||||||||
| 66 | $this_chunked_source->get_chunk_by_indx($this_prev_chunk_indx); | ||||||||||||||||||||||||||||
| 67 | 254 | my $other_prev_chunk_text = | |||||||||||||||||||||||||||
| 68 | $other_chunked_source->get_chunk_by_indx($other_prev_chunk_indx); | ||||||||||||||||||||||||||||
| 69 | 254 | my $this_prev_hash = | |||||||||||||||||||||||||||
| 70 | Text::Same::ChunkedSource::hash($options, $this_prev_chunk_text); | ||||||||||||||||||||||||||||
| 71 | 254 | my $other_prev_hash = | |||||||||||||||||||||||||||
| 72 | Text::Same::ChunkedSource::hash($options, $other_prev_chunk_text); | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 254 | ▶ | if ($this_prev_hash eq $other_prev_hash) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 206 | my $prev_pair_id = | |||||||||||||||||||||||||||
| 76 | Text::Same::ChunkPair::make_packed_pair($this_prev_chunk_indx, | ||||||||||||||||||||||||||||
| 77 | $other_prev_chunk_indx); | ||||||||||||||||||||||||||||
| 78 | 206 | my $prev_match = $seen_pairs_ref->{$prev_pair_id}; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 206 | ▶ | if (defined $prev_match) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 206 | $prev_match->add($chunk_pair); | |||||||||||||||||||||||||||
| 82 | 206 | $seen_pairs_ref->{$pair_id} = $prev_match; | |||||||||||||||||||||||||||
| 83 | 206 | next; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 107 | my $match = new Text::Same::Match(source1=>$this_chunked_source, | |||||||||||||||||||||||||||
| 89 | source2=>$other_chunked_source, | ||||||||||||||||||||||||||||
| 90 | pairs=>[$chunk_pair]); | ||||||||||||||||||||||||||||
| 91 | 107 | $seen_pairs_ref->{$pair_id} = $match; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | sub _find_matches($$$) | ||||||||||||||||||||||||||||
| 97 | 37 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_find_matches: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 98 | 37 | my ($options, $source1, $source2) = @_; | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 37 | my $source1_chunk_indexes = $source1->get_filtered_chunk_indexes($options); | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 37 | my %seen_pairs = (); | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 37 | for my $this_chunk_indx (@$source1_chunk_indexes) { | |||||||||||||||||||||||||||
| 105 | 260 | my $chunk_text = $source1->get_chunk_by_indx($this_chunk_indx); | |||||||||||||||||||||||||||
| 106 | 260 | my @matching_chunk_indexes = | |||||||||||||||||||||||||||
| 107 | $source2->get_matching_chunk_indexes($options, $chunk_text); | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 260 | ▶ | if (@matching_chunk_indexes) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 242 | _process_hits($options, $this_chunk_indx, \%seen_pairs, | |||||||||||||||||||||||||||
| 111 | \@matching_chunk_indexes, $source1, $source2); | ||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 37 | return \%seen_pairs; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | sub _extend_matches | ||||||||||||||||||||||||||||
| 119 | 37 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_extend_matches: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 120 | 37 | my ($options, $matches, $source1, $source2) = @_; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 37 | for my $match (@$matches) { | |||||||||||||||||||||||||||
| 123 | 107 | my ($prev, $next) = undef; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 107 | $prev = $source1->get_previous_chunk_indx($options, $match->min1()); | |||||||||||||||||||||||||||
| 126 | 107 | ▶ | if (defined $prev) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 63 | $match->set_min1($prev + 1); | |||||||||||||||||||||||||||
| 128 | } else { | ||||||||||||||||||||||||||||
| 129 | 44 | $match->set_min1(0); | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | 107 | $prev = $source2->get_previous_chunk_indx($options, $match->min2()); | |||||||||||||||||||||||||||
| 132 | 107 | ▶ | if (defined $prev) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 64 | $match->set_min2($prev + 1); | |||||||||||||||||||||||||||
| 134 | } else { | ||||||||||||||||||||||||||||
| 135 | 43 | $match->set_min2(0); | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | 107 | $next = $source1->get_next_chunk_indx($options, $match->max1()); | |||||||||||||||||||||||||||
| 138 | 107 | ▶ | if (defined $next) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 51 | $match->set_max1($next - 1); | |||||||||||||||||||||||||||
| 140 | } else { | ||||||||||||||||||||||||||||
| 141 | 56 | $match->set_max1($source1->get_all_chunks_count() - 1); | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | 107 | $next = $source2->get_next_chunk_indx($options, $match->max2()); | |||||||||||||||||||||||||||
| 144 | 107 | ▶ | if (defined $next) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 67 | $match->set_max2($next - 1); | |||||||||||||||||||||||||||
| 146 | } else { | ||||||||||||||||||||||||||||
| 147 | 40 | $match->set_max2($source2->get_all_chunks_count() - 1); | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =head2 compare | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | Title : compare | ||||||||||||||||||||||||||||
| 155 | Usage : $matchmap = compare($options, $file1, $file2) | ||||||||||||||||||||||||||||
| 156 | or | ||||||||||||||||||||||||||||
| 157 | $matchmap = compare($options, \@array1, \@array2) | ||||||||||||||||||||||||||||
| 158 | then: | ||||||||||||||||||||||||||||
| 159 | my @all_matches = $matchmap->all_matches; | ||||||||||||||||||||||||||||
| 160 | Function: return a MatchMap object holding matches and non-matches between the | ||||||||||||||||||||||||||||
| 161 | two given files or arrays of strings | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | =cut | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | sub compare | ||||||||||||||||||||||||||||
| 166 | 37 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
compare: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 167 | 37 | ▶ | my $options = shift || {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 168 | 37 | my $data1 = shift; | |||||||||||||||||||||||||||
| 169 | 37 | my $data2 = shift; | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 37 | my $cache = new Text::Same::Cache(); | |||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 37 | my $source1; | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 37 | ▶ | if (ref $data1 eq "ARRAY") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | 1 | $source1 = new Text::Same::ChunkedSource(name=>"array1", chunks=>$data1); | |||||||||||||||||||||||||||
| 177 | } else { | ||||||||||||||||||||||||||||
| 178 | 36 | $source1 = $cache->get($data1, $options); | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 37 | my $source2; | |||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 37 | ▶ | if (ref $data2 eq "ARRAY") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | 1 | $source2 = new Text::Same::ChunkedSource(name=>"array2", chunks=>$data2); | |||||||||||||||||||||||||||
| 185 | } else { | ||||||||||||||||||||||||||||
| 186 | 36 | $source2 = $cache->get($data2, $options); | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 37 | my $seen_pairs_ref = _find_matches $options, $source1, $source2; | |||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 37 | my @matches = values %{$seen_pairs_ref}; | |||||||||||||||||||||||||||
| 192 | 37 | my %uniq_matches = (); | |||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 37 | for my $match (@matches) { | |||||||||||||||||||||||||||
| 195 | 313 | $uniq_matches{$match} = $match; | |||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 37 | my @unique_matches = values %uniq_matches; | |||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 37 | _extend_matches($options, \@unique_matches, $source1, $source2); | |||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 37 | return new Text::Same::MatchMap(options=>$options, source1=>$source1, | |||||||||||||||||||||||||||
| 203 | source2=>$source2, | ||||||||||||||||||||||||||||
| 204 | matches=>\@unique_matches); | ||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | B<psame(1)> | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | Kim Rutherford <kmr+same@xenu.org.uk> | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | Copyright 2005,2006 Kim Rutherford, all rights reserved. | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 220 | under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | =cut | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | 4 | 1; | |||||||||||||||||||||||||||