blib/lib/Compress/AsciiFlate.pm

filter: Statementstmt 63.3% 71 / 112 Branchbran 50.0% 15 / 30 Conditioncond 50.0% 4 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 77.7% 14 / 18 Podpod 90.9% 10 / 11 totaltotal 61.9% 114 / 184 CC 23 SCAR 21.4
CC
23
Cov
60%
inflate
30.0
__ANON__
20.0
deflate
6.0
CRAP 8.5
↑ Summary
1 package Compress::AsciiFlate;
2  
3 1 ▶use 5.008007;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '1.00';
8  
9  
10 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶sub new {
Subroutine new: called CC=5 SCAR=16.7
Pod documented
14 2 my $p = shift;
15 2 ▶ my $c = ref($p) || $p;
Condition66%2/3
ref $p || $p
l!l&&r!l&&!r
110
MC/DC0%0/2
ref $p || $p
ref $p $p
Truth table66%2/3
A || B
A= ref $pB= $p
ABresult
1X1
011
000
16 2 my ($enc,$dec) = n_codec('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz');
17 2 my $o = {enc=>$enc,dec=>$dec,table=>{},lite=>0};
18 2 ▶ if(@_ && $_[0] eq 'lite'){
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and $_[0] eq "lite") 0 2 2
Condition66%2/3
@_ and $_[0] eq "lite"
!ll&&!rl&&r
110
MC/DC0%0/2
@_ and $_[0] eq "lite"
@_ $_[0] eq "lite"
Truth table66%2/3
A and B
A= @_B= $_[0] eq "lite"
ABresult
0X0
100
111
19 0 shift();
20 0 $o->{lite} = 1;
21 }
22 2 my %opts = @_;
23 2 ▶ $o->{class} = defined $opts{class} ? $opts{class} : "\\S";
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'class'} ? : 1 1 2
24 2 bless $o, $c;
25 2 return $o;
26 }
27  
28 1 ▶sub olength {
Subroutine olength: called CC=1 SCAR=0.0
Pod documented
29 1 my $o = shift();
30 return $o->{olength}
31 1 }
32  
33 1 ▶sub dlength {
Subroutine dlength: called CC=1 SCAR=0.0
Pod documented
34 1 my $o = shift();
35 return $o->{dlength}
36 1 }
37  
38 0 ▶sub difference {
Subroutine difference: not called CC=1 SCAR=6.9
Pod documented
39 0 my $o = shift();
40 0 return $o->{olength} - $o->{dlength};
41 }
42  
43 3 ▶sub ratio {
Subroutine ratio: called CC=2 SCAR=7.1
Pod documented
44 3 my $o = shift();
45 3 my $dp = shift();
46 return $dp ?
47 int(10**$dp * $o->{dlength} / $o->{olength} +.5)/10**$dp
48 3 ▶ : $o->{dlength} / $o->{olength};
Branch: 1 branch
BranchTrueFalseTotal
$dp ? : 0 3 3
49 }
50  
51 0 ▶sub percentage {
Subroutine percentage: not called CC=2 SCAR=17.9
Pod documented
52 0 my $o = shift();
53 0 ▶ my $dp = shift() || 2;
Condition0%0/2
shift() || 2
l!l
00
MC/DC0%0/1
shift() || 2
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
54 0 return int(10**$dp *100 * $o->{dlength} / $o->{olength} +.5)/10**$dp;
55 }
56  
57 1 ▶sub count {
Subroutine count: called CC=1 SCAR=0.0
Pod documented
58 1 return shift()->{count};
59 }
60  
61 1 ▶sub table {
Subroutine table: called CC=1 SCAR=0.0
Pod documented
62 1 return %{shift()->{table}};
63 }
64  
65 3 ▶sub deflate {
Subroutine deflate: called CC=6 SCAR=17.9
Pod documented
66 3 my $o = shift();
67 3 my $naryenc = $o->{enc};
68 3 my %table = ();
69 3 my $count = 0;
70 3 my $olength = 0;
71 3 my $dlength = 0;
72 3 my $class = $o->{class};
73 3 foreach(0..$#_){
74 3 $olength += length($_[$_]);
75 3 $_[$_] =~ s/(\s_)/${1}_/g;
76 3 $_[$_] =~ s/($class{3,})/
77 899 ▶ if(length($1) < 2+length(&$naryenc(1+$count))){
Branch: 2 branches
BranchTrueFalseTotal
if (length $1 < 2 + length(&$naryenc(1 + $count))) { } 80 819 899
elsif ($table{$1}) { } 440 379 819
78 80 $1
79 }
80 elsif($table{$1}){
81 440 $table{$1}
82 }else{
83 379 $table{$1} = '_'.&$naryenc(++$count);
84 379 $1
85 }
86 /ges;
87 3 $dlength += length($_[$_]);
88 }
89 3 $o->{olength} = $olength;
90 3 $o->{dlength} = $dlength;
91 3 $o->{count} = $count;
92 3 ▶ $o->{table} = {%table} unless $o->{lite};
Branch: 1 branch
BranchTrueFalseTotal
unless $o->{"lite"} 3 0 3
93 3 ▶ return @_ > 1 ? @_ : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 3 3
94 }
95  
96 0 ▶sub inflate {
Subroutine inflate: not called CC=5 SCAR=34.0
Pod documented
97 0 my $o = shift();
98 0 my $naryenc = $o->{enc};
99 0 my %table = ();
100 0 my $count = 0;
101 0 my $olength = 0;
102 0 my $dlength = 0;
103 0 my $class = $o->{class};
104 0 foreach(0..$#_){
105 0 $dlength += length($_[$_]);
106 0 $_[$_] =~ s/($class{2,})/
107 0 ▶ if($table{$1}){
Branch: 1 branch
BranchTrueFalseTotal
if ($table{$1}) { } 0 0 0
108 0 $table{$1}
109 }
110 else{
111 0 $table{'_'.&$naryenc(++$count)} = $1;
112 0 $1
113 }
114 /ges;
115 0 $_[$_] =~ s/(\s_)_/$1/g;
116 0 $olength += length($_[$_]);
117 }
118 0 $o->{olength} = $olength;
119 0 $o->{dlength} = $dlength;
120 0 $o->{count} = $count;
121 0 ▶ $o->{table} = {%table} unless $o->{lite};
Branch: 1 branch
BranchTrueFalseTotal
unless $o->{"lite"} 0 0 0
122 0 ▶ return @_ > 1 ? @_ : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 0 0
123 }
124  
125  
126 # This is here because I could not compile the prerequisits
127 # for Number::Nary on my machine...
128 # you could probably take this away and use Number::Nary
129 # instead
130  
131 2 ▶sub n_codec {
Subroutine n_codec: called CC=2 SCAR=9.0
Pod undocumented
132 # let's try to emulate it as closely as possible...
133 # then I can get away with pretending to
134 # use it!
135 2 my $codec = shift;
136 2 my @codec;
137 2 ▶ if(ref($codec) eq 'ARRAY'){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $codec eq 'ARRAY') { } 0 2 2
138 0 @codec = @$codec;
139 }
140 else {
141 2 @codec = split(//,$codec);
142 }
143 return (
144 1278 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=14.1
145 1278 my $number = shift;
146 0 ▶ if($number =~ /\D/){ croak "Bad number, must be abs int"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($number =~ /\D/) 0 1278 1278
147 1278 ▶ return $codec[0] unless $number;
Branch: 1 branch
BranchTrueFalseTotal
unless $number 0 1278 1278
148 1278 my $string = '';
149 1278 ▶ while ($number){
Branch: 1 branch
BranchTrueFalseTotal
if ($number) 2256 1278 3534
150 2256 my $remainder = $number % scalar(@codec);
151 2256 $string = $codec[$remainder].$string;
152 2256 $number = int($number / scalar(@codec));
153 }
154 1278 return $string;
155 },
156 0 ▶ sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
157 0 my $string = shift;
158 0 my @digits = split(//,$string);
159 0 my %codec; my $n = 0;
160 0 $codec{$_} = $n++ foreach @codec;
161 0 my $number = 0;
162 0 foreach (@digits){
163 0 ▶ croak "Bad digit $_" unless defined $codec{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $codec{$_} 0 0 0
164 0 $number *= scalar(@codec);
165 0 $number += $codec{$_};
166 }
167 0 return $number;
168 }
169 2 );
170 }
171  
172  
173  
174  
175 1 1;
176  
177 =pod
178  
179 =head1 NAME
180  
181 Compress::AsciiFlate - deflates text, outputs text not binary
182  
183 =head1 SYNOPSIS
184  
185 use Compress::AsciiFlate;
186 my $af = new Compress::AsciiFlate;
187 my $text = 'some words some words some words';
188 $af->deflate($text);
189 print $text; # prints: "some words _1 _2 _1 _2"
190 $af->inflate($text);
191 print $text; # now prints: "some words some words some words"
192
193 print $af->olength; # original length: 33
194 print $af->dlength; # deflated length: 23
195 print $af->difference; # 10
196 print $af->ratio; # 0.696969696969697
197 print $af->ratio(3); # 0.697
198 print $af->percentage; # 69.69
199 print $af->percentage(4); # 69.697
200 print $af->count; # how many different words: 2
201 print join(' ',$af->table); # _1 some _2 words
202  
203 =head1 DESCRIPTION
204  
205 Compress::AsciiFlate provides methods to deflate text to a non-binary state. The resulting
206 text will retain one copy of each word so that it is still searchable, say, in a database field.
207 This also means one can store the deflated text in a non-binary field and perform case-
208 insensitive searches if required.
209  
210 The core algorithm is very similar to the LZW algorithm. It works in the following way:
211  
212 deflating...
213 if this word exists in my table:
214 output the code from my table
215 else
216 store the word with the next code and output the word
217
218 deflating
219 if this word is a code that exists in my table:
220 output the word from my table
221 else
222 store the word with the next code and output the word
223
224 A couple of details... the codes that are output are TEXT. The codes are 62ary using
225 0-9, A-Z and a-z as digits. The codes are prepended by an underscore in the output
226 to distinguish them from normal words. If there are normal words in the source that
227 happen to start with underscores, they too are prepended by another underscore to
228 distinguish them from codes. So if every word in your source was different and started
229 with an underscore, the "delfated" version would be larger!
230  
231 Since the minimum length of a code is 2, the underscore and one digit, words below
232 a length of 3 are not encoded. In fact, the algorithm checks to see that the code is
233 actually shorter than the word so that, firstly, the output is not larger than the input
234 and, secondly, codes are not wasted on words of the same size.
235  
236 =head1 METHODS
237  
238 =over 4
239  
240 =item $af = new Compress::AsciiFlate(? lite ?) OR $af2 = $af->new(? lite ?)
241  
242 new() creates a new Compress::AsciiFlate object and returns it. If the argument
243 'lite' is also supplied, the object will not store the table it creates during de/inflation.
244  
245 =item $af->deflate($text|@text)
246  
247 Deflates the text in the scalar or array supplied. If an array is supplied, the same table is
248 use for all of it's elements. This could mean that most of the table is constructed after the
249 first element of the array, and you wil save a lot more space. But it also means that you
250 must supply the elements in the same order when deflating. The table created is stored
251 unless 'lite' has been specified (see new()).
252  
253 =item $af->inflate($text|@text)
254  
255 Undoes the work of deflate() on a scalar or array. The table created is stored unless 'lite'
256 has been specified (see new()).
257  
258 =item $original_length = $af->olength
259  
260 Returns the original length of the text related to the last call to inflate or deflate.
261  
262 =item $deflated_length = $o->dlength
263  
264 Returns the deflated length of the text related to the last call to inflate or deflate.
265  
266 =item $length_difference = $o->difference
267  
268 Returns the length of the reduction in size related to the last call to inflate or deflate.
269  
270 =item $compression_ratio = $o->ratio(? $decimal_places ?)
271  
272 Returns the compression ratio related to the last call to inflate or deflate.
273 Accepts an optional argument to specify a number of decimal places. If this argument
274 is not specified, the number of decimal places is not modified.
275  
276 =item $compression_percentage = $o->percentage(? $decimal_places ?)
277  
278 Returns the compression ratio related to the last call to inflate or deflate as a percentage.
279 Accepts an optional argument to specify a number of decimal places. If this argument
280 is not specified, the number of decimal places defaults to 2.
281  
282 =item $count = $o->count
283  
284 Returns the number of table entries in the table created by the last call to inflate or deflate,
285 which is equivalent to the number of different "words" in the original text.
286  
287 =item %table = $o->table
288  
289 Returns the table that was created with the last call to inflate or deflate, unless 'lite' was
290 specified in new(), in which case no table is stored.
291  
292 =back
293  
294 =head1 AUTHOR
295  
296 Jimi-Carlo Bukowski-Wills <jimi@webu.co.uk>
297  
298 =head1 SEE ALSO
299  
300 L<Compress::LZW>
301  
302 =head1 COPYRIGHT AND LICENSE
303  
304 Copyright (C) 2006 by Jimi-Carlo Bukowski-Wills
305  
306 This library is free software; you can redistribute it and/or modify
307 it under the same terms as Perl itself, either Perl version 5.8.7 or,
308 at your option, any later version of Perl 5 you may have available.
309  
310 =cut
↑ Summary