lib/Encode/Mapper.pm

filter: Statementstmt 66.8% 222 / 332 Branchbran 65.4% 140 / 214 Conditioncond 57.8% 22 / 38 MC/DCmcdc 16.0% 4 / 25 Subroutinesub 83.3% 15 / 18 Podpod 81.8% 9 / 11 totaltotal 64.5% 412 / 638 CC 136 SCAR 40.6
CC
136
Cov
66%
describe
306.0
compile
42.6
verify_rule
33.8
CRAP 58.1
« lib/Encode/Arabic/CGI.pm ↑ Summary lib/Encode/Arabic/ArabTeX/RE.pm »
1 # ############################################################################ Otakar Smrz, 2003/01/23
2 #
3 # Mapper Engine Class ##################################################################### 2003/06/19
4  
5 package Encode::Mapper;
6  
7 8 our $VERSION = '14.1';
8  
9 8 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 8 ▶use bytes; # ensures splitting into one-byte tokens .. lexically scoped
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18  
19 8 our %options; # records of options per package .. global register
20 8 our %option; # options of the caller package .. used with local
21  
22  
23 13 ▶sub import { # enforces setting of options
Subroutine import: called CC=2 SCAR=6.9
24 13 my $cls = shift @_;
25 13 ▶ $cls->options(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 10 3 13
26 }
27  
28  
29 1 ▶sub whisper ($) { # module's internal function
Subroutine whisper: called CC=2 SCAR=7.6
Pod undocumented
30  
31 1 ▶ carp shift unless $option{'silent'};
Branch: 1 branch
BranchTrueFalseTotal
unless $Encode::Mapper::option{"silent"} 0 1 1
32 }
33  
34  
35 158302 ▶sub verify_rule ($$) { # module's internal function
Subroutine verify_rule: called CC=12 SCAR=35.2
Pod undocumented
36  
37 158302 ▶ unless (defined $_[0] and $_[0] ne '') {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_[0] and $_[0] ne "") 0 158302 158302
Condition33%1/3
defined $_[0] and $_[0] ne ""
!ll&&!rl&&r
00158302
MC/DC0%0/2
defined $_[0] and $_[0] ne ""
defined $_[0] $_[0] ne ""
Truth table33%1/3
A and B
A= defined $_[0]B= $_[0] ne ""
ABresult
0X0
100
111
38 0 whisper "Rule's LHS is empty, rule ignored";
39 0 return;
40 }
41 158302 ▶ unless (defined $_[1]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_[1]) 0 158302 158302
42 0 whisper "Rule's RHS is undefined, rule ignored";
43 0 return;
44 }
45  
46 158302 ▶ if (UNIVERSAL::isa($_[1], 'ARRAY')) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($_[1], 'ARRAY')) { } 130977 27325 158302
elsif (ref \$_[1] ne 'SCALAR' and not UNIVERSAL::isa($_[1], 'CODE')) { } 0 54646 54646
47 130977 ▶ unless (defined $_[1]->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_[1][0]) 0 130977 130977
48 0 whisper "Rule's RHS is undefined, rule ignored";
49 0 return;
50 }
51 130977 ▶ unless (ref \$_[1]->[0] eq 'SCALAR' or UNIVERSAL::isa($_[1]->[0], 'CODE')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref \$_[1][0] eq "SCALAR" or UNIVERSAL::isa($_[1][0], "CODE")) 0 130977 130977
Condition66%2/3
ref \$_[1][0] eq "SCALAR" or UNIVERSAL::isa($_[1][0], "CODE")
l!l&&r!l&&!r
13097340
MC/DC0%0/2
ref \$_[1][0] eq "SCALAR" or UNIVERSAL::isa($_[1][0], "CODE")
ref \$_[1][0] eq "SCALAR" UNIVERSAL::isa($_[1][0], "CODE")
Truth table66%2/3
A or B
A= ref \$_[1][0] eq "SCALAR"B= UNIVERSAL::isa($_[1][0], "CODE")
ABresult
1X1
011
000
52 0 whisper "Rule's RHS is neither literal nor subroutine reference, rule ignored";
53 0 return;
54 }
55 130977 ▶ unless (defined $_[1]->[1] and length $_[1]->[1] < length $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_[1][1] and length $_[1][1] < length $_[0]) 0 130977 130977
Condition33%1/3
defined $_[1][1] and length $_[1][1] < length $_[0]
!ll&&!rl&&r
00130977
MC/DC0%0/2
defined $_[1][1] and length $_[1][1] < length $_[0]
defined $_[1][1] length $_[1][1] < length $_[0]
Truth table33%1/3
A and B
A= defined $_[1][1]B= length $_[1][1] < length $_[0]
ABresult
0X0
100
111
56 0 whisper "Rule type '\$A => [\$X, \$Y], length \$A > length \$Y' misused, considering it '\$A => \$X'";
57 0 $_[1] = $_[1]->[0];
58 }
59 }
60 ▶ elsif (ref \$_[1] ne 'SCALAR' and not UNIVERSAL::isa($_[1], 'CODE')) {
Condition66%2/3
ref \$_[1] ne "SCALAR" and not UNIVERSAL::isa($_[1], "CODE")
!ll&&!rl&&r
2732140
MC/DC0%0/2
ref \$_[1] ne "SCALAR" and not UNIVERSAL::isa($_[1], "CODE")
ref \$_[1] ne "SCALAR" not UNIVERSAL::isa($_[1], "CODE")
Truth table66%2/3
A and not(B)
A= ref \$_[1] ne "SCALAR"B= not UNIVERSAL::isa($_[1], "CODE")
ABresult
0X0
100
111
61 0 whisper "Rule's RHS is neither literal nor subroutine reference, rule ignored";
62 0 return;
63 }
64  
65 158302 return 1;
66 }
67  
68  
69 27 ▶sub options ($%) { # options for general compilation of Mappers
Subroutine options: called CC=22 SCAR=31.6
Pod documented
70 27 my $cls = shift @_;
71 27 my ($i, $opt, %opt);
72  
73 27 ▶ my $caller = caller 0; $caller = caller 1 if $caller eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if $caller eq __PACKAGE__ 21 6 27
74 27 ▶ my @returns = exists $options{$caller} ? %{$options{$caller}} : ();
Branch: 1 branch
BranchTrueFalseTotal
exists $Encode::Mapper::options{$caller} ? : 17 10 27
75  
76 27 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 52 27 79
77 52 $opt = lc shift @_;
78  
79 52 ▶ if ($opt =~ /^\:/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt =~ /^\:/) { } 28 24 52
80 28 ▶ $opt eq ':others' and $opt{'others'} = sub { shift } and next;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
$opt eq ":others" and $opt{"others"} = sub { shift(); } and next 10 18 28
Condition66%2/3
$opt eq ":others" and $opt{"others"} = sub { shift(); }
!ll&&!rl&&r
18010
MC/DC50%1/2
$opt eq ":others" and $opt{"others"} = sub { shift(); }
$opt eq ":others" $opt{"others"} = sub { shift(); }
Truth table66%2/3
A and B
A= $opt eq ":others"B= $opt{"others"} = sub { shift(); }
ABresult
0X0
100
111
81 18 ▶ $opt eq ':silent' and $opt{'silent'} = 1 and next;
Branch: 1 branch
BranchTrueFalseTotal
$opt eq ":silent" and $opt{"silent"} = 1 and next 10 8 18
Condition100%2/2
$opt eq ":silent" and $opt{"silent"} = 1
!ll
810
MC/DC100%1/1
$opt eq ":silent" and $opt{"silent"} = 1
$opt eq ":silent"
Truth table100%2/2
A
A= $opt eq ":silent"
Aresult
00
11
82 8 ▶ $opt eq ':join' and $opt{'join'} = '';
Branch: 1 branch
BranchTrueFalseTotal
$opt eq ":join" and $opt{"join"} = "" 8 0 8
83 }
84 else {
85 24 $opt =~ /^\-*(.*)$/;
86 24 $opt{$1} = shift @_;
87 }
88 }
89  
90 {
91 27 ▶ local $option{'silent'} = exists $opt{'silent'} ? $opt{'silent'} : $options{$caller}{'silent'};
Branch: 1 branch
BranchTrueFalseTotal
exists $opt{'silent'} ? : 24 3 27
92  
93 27 ▶ if (defined $opt{'complement'} and UNIVERSAL::isa($opt{'complement'}, 'ARRAY')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{"complement"} and UNIVERSAL::isa($opt{"complement"}, "ARRAY")) 1 26 27
Condition66%2/3
defined $opt{"complement"} and UNIVERSAL::isa($opt{"complement"}, "ARRAY")
!ll&&!rl&&r
2601
MC/DC50%1/2
defined $opt{"complement"} and UNIVERSAL::isa($opt{"complement"}, "ARRAY")
defined $opt{"complement"} UNIVERSAL::isa($opt{"complement"}, "ARRAY")
Truth table66%2/3
A and B
A= defined $opt{"complement"}B= UNIVERSAL::isa($opt{"complement"}, "ARRAY")
ABresult
0X0
100
111
94 1 ▶ for ($i = 0; $i < @{$opt{'complement'}}; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$opt{"complement"};}) 2 1 3
95 2 verify_rule $opt{'complement'}->[$i], $opt{'complement'}->[$i + 1];
96 }
97 }
98  
99 27 ▶ if (defined $opt{'override'} and UNIVERSAL::isa($opt{'override'}, 'ARRAY')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{"override"} and UNIVERSAL::isa($opt{"override"}, "ARRAY")) 3 24 27
Condition66%2/3
defined $opt{"override"} and UNIVERSAL::isa($opt{"override"}, "ARRAY")
!ll&&!rl&&r
2403
MC/DC50%1/2
defined $opt{"override"} and UNIVERSAL::isa($opt{"override"}, "ARRAY")
defined $opt{"override"} UNIVERSAL::isa($opt{"override"}, "ARRAY")
Truth table66%2/3
A and B
A= defined $opt{"override"}B= UNIVERSAL::isa($opt{"override"}, "ARRAY")
ABresult
0X0
100
111
100 3 ▶ for ($i = 0; $i < @{$opt{'override'}}; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$opt{"override"};}) 1551 3 1554
101 1551 verify_rule $opt{'override'}->[$i], $opt{'override'}->[$i + 1];
102 }
103 }
104  
105 27 ▶ if (defined $opt{'others'} and not $option{'silent'}) { # see whisper
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{"others"} and not $Encode::Mapper::option{"silent"}) 0 27 27
Condition66%2/3
defined $opt{"others"} and not $Encode::Mapper::option{"silent"}
!ll&&!rl&&r
15120
MC/DC0%0/2
defined $opt{"others"} and not $Encode::Mapper::option{"silent"}
defined $opt{"others"} not $Encode::Mapper::option{"silent"}
Truth table66%2/3
A and not(B)
A= defined $opt{"others"}B= not $Encode::Mapper::option{"silent"}
ABresult
0X0
100
111
106 0 ▶ if (UNIVERSAL::isa($opt{'others'}, 'CODE')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($opt{'others'}, 'CODE')) { } 0 0 0
107 0 carp "The subroutine will be called with the 'other' LHS parameter to get the rule's RHS";
108 }
109 else {
110 0 carp "The scalar value will become the RHS of each 'other' LHS";
111 }
112 }
113 }
114  
115 27 ▶ return %opt unless defined $cls;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cls 11 16 27
116  
117 16 $options{$caller}{$_} = $opt{$_} foreach keys %opt;
118  
119 16 return @returns;
120 }
121  
122  
123 8 *new = *compile{'CODE'}; # provides the 'new' constructor .. the 'compile' method
124 # *new = \&compile; # might be known at compile-time
125  
126  
127 15 ▶sub compile ($@) { # returns Mapper .. modified Aho-Corasick and Boyer-Moore search engine
Subroutine compile: called CC=42 SCAR=37.5
Pod documented
128 15 my $cls = shift @_;
129 15 my (@tree, @bell, @skip, @queue, %redef);
130 15 my ($q, $r, $s, $t, $i, $token, $trick);
131  
132 15 my ($null_list, $null_hash) = ([], {}); # references to empties need not consume unique memory
133 15 my ($no_code, $no_list) = (1, 1); # optimization indicators
134  
135 15 ▶ local %option = exists $options{caller 0} ? %{$options{caller 0}} : ();
Branch: 1 branch
BranchTrueFalseTotal
exists $Encode::Mapper::options{caller 0} ? : 14 1 15
136 # options be local due to verify_rule and whisper
137  
138 15 ▶ if (UNIVERSAL::isa($_[0], 'ARRAY')) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($_[0], 'ARRAY')) { } 11 4 15
elsif (UNIVERSAL::isa($_[0], 'HASH')) { } 0 4 4
139 11 %option = (%option, options undef, @{shift @_});
140 }
141 elsif (UNIVERSAL::isa($_[0], 'HASH')) {
142 0 %option = (%option, options undef, %{shift @_});
143 }
144  
145 15 $skip[0] = undef; # never ever used .. fix the number of list elements equal
146 15 $bell[0] = $null_list; # important .. depth-wise inheritation of the lists
147  
148 15 ▶ if (defined $option{'complement'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Encode::Mapper::option{"complement"}) 1 14 15
149 1 ▶ for ($i = 0; $i < @{$option{'complement'}}; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$Encode::Mapper::option{"complement"};}) 2 1 3
150  
151 2 $q = 0;
152  
153 2 foreach $token (split //, $option{'complement'}->[$i]) {
154 2 ▶ $tree[$q]->{$token} = ++$r unless defined $tree[$q]->{$token}; # increment $r ^^
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tree[$q]{$token} 1 1 2
155 2 $q = $tree[$q]->{$token};
156 }
157  
158 2 ▶ $tree[$q] = {} unless defined $tree[$q]; # define trees correctly, economize below
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tree[$q] 1 1 2
159  
160 2 ▶ whisper "Redefining the mapping for '" . $option{'complement'}->[$i] . "'" if defined $bell[$q];
Branch: 1 branch
BranchTrueFalseTotal
if defined $bell[$q] 1 1 2
161  
162 2 $bell[$q] = [ $option{'complement'}->[$i + 1] ];
163 }
164 }
165  
166 15 ▶ for ($i = 0; $i < @_; $i += 2) { # generate $tree[$q] transition function and initial $bell[$q]
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @_) 156749 15 156764
167  
168 156749 ▶ next unless verify_rule $_[$i], $_[$i + 1];
Branch: 1 branch
BranchTrueFalseTotal
unless &verify_rule($_[$i], $_[$i + 1]) 0 156749 156749
169  
170 156749 $q = 0;
171  
172 156749 foreach $token (split //, $_[$i]) {
173 945101 ▶ $tree[$q]->{$token} = ++$r unless defined $tree[$q]->{$token}; # increment $r ^^
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tree[$q]{$token} 180502 764599 945101
174 945101 $q = $tree[$q]->{$token};
175 }
176  
177 156749 ▶ $tree[$q] = {} unless defined $tree[$q]; # define trees correctly, economize below
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tree[$q] 156240 509 156749
178  
179 156749 ▶ whisper "Redefining the mapping for '$_[$i]'" if $redef{$q}++;
Branch: 1 branch
BranchTrueFalseTotal
if $redef{$q}++ 0 156749 156749
180  
181 156749 $bell[$q] = [ $_[$i + 1] ];
182 }
183  
184 15 ▶ if (defined $option{'override'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Encode::Mapper::option{"override"}) 10 5 15
185 10 ▶ for ($i = 0; $i < @{$option{'override'}}; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$Encode::Mapper::option{"override"};}) 5170 10 5180
186  
187 5170 $q = 0;
188  
189 5170 foreach $token (split //, $option{'override'}->[$i]) {
190 10350 ▶ $tree[$q]->{$token} = ++$r unless defined $tree[$q]->{$token}; # increment $r ^^
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tree[$q]{$token} 5170 5180 10350
191 10350 $q = $tree[$q]->{$token};
192 }
193  
194 5170 ▶ $tree[$q] = {} unless defined $tree[$q]; # define trees correctly, economize below
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tree[$q] 5150 20 5170
195  
196 5170 ▶ whisper "Redefining the mapping for '" . $option{'override'}->[$i] . "'" if $redef{$q}++;
Branch: 1 branch
BranchTrueFalseTotal
if $redef{$q}++ 0 5170 5170
197  
198 5170 $bell[$q] = [ $option{'override'}->[$i + 1] ];
199 }
200 }
201  
202 15 foreach $token (map { chr } 0x00..0xFF) {
203 3840 ▶ unless (defined $tree[0]->{$token}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tree[0]{$token}) 3283 557 3840
204 3283 ▶ unless (defined $option{'others'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Encode::Mapper::option{'others'}) { } 2385 898 3283
205 898 $tree[0]->{$token} = 0;
206 }
207 else {
208 2385 $tree[0]->{$token} = ++$r; # increment $r ^^
209 2385 $tree[$r] = {}; # define trees correctly
210 }
211 }
212  
213 3840 $q = $tree[0]->{$token}; # including existing prefixes
214  
215 3840 ▶ unless ($q == 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($q == 0) 2942 898 3840
216 2942 ▶ unless (defined $bell[$q]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $bell[$q]) 2458 484 2942
217 2458 ▶ if (not defined $option{'others'}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $Encode::Mapper::option{'others'}) { } 12 2446 2458
elsif (UNIVERSAL::isa($Encode::Mapper::option{'others'}, 'CODE')) { } 2446 0 2446
218 12 $bell[$q] = $bell[0];
219 }
220 elsif (UNIVERSAL::isa($option{'others'}, 'CODE')) {
221 2446 $bell[$q] = [ $option{'others'}->($token) ];
222 }
223 else {
224 0 $bell[$q] = [ $option{'others'} ];
225 }
226 }
227  
228 2942 $skip[$q] = 0;
229  
230 2942 push @queue, $q;
231 }
232 }
233  
234 15 ▶ while (@queue) { # generate $skip[$q] backward function and complete $bell[$q]
Branch: 1 branch
BranchTrueFalseTotal
if (@queue) 188058 15 188073
235 188058 $q = shift @queue;
236  
237 188058 foreach $token (keys %{$tree[$q]}) {
238 185116 $t = $tree[$q]->{$token};
239 185116 push @queue, $t;
240  
241 185116 ▶ if (defined $bell[$t]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bell[$t]) { } 161435 23681 185116
242 161435 $skip[$t] = 0;
243  
244 161435 ▶ if (UNIVERSAL::isa($bell[$t]->[0], 'ARRAY')) { # shortening property of the rules
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($bell[$t][0], "ARRAY")) 134568 26867 161435
245 134568 $s = $skip[$t];
246  
247 134568 foreach $trick (split //, $bell[$t]->[0]->[1]) {
248 327467 ▶ until (defined $tree[$s]->{$trick}) { # loops only if not in the root ^^
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tree[$s]{$trick}) 114135 327467 441602
249 114135 push @{$bell[$t]}, @{$bell[$s]};
250 114135 $s = $skip[$s];
251 }
252 327467 $s = $tree[$s]->{$trick};
253 }
254  
255 134568 $skip[$t] = $s;
256 134568 $bell[$t]->[0] = $bell[$t]->[0]->[0];
257 }
258 }
259 else {
260 23681 $s = $skip[$q];
261 23681 $bell[$t] = [ @{$bell[$q]} ]; # unique reference quite important ^^
262  
263 23681 ▶ until (defined $tree[$s]->{$token}) { # extremely tricky ...
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tree[$s]{$token}) 6147 23681 29828
264 6147 push @{$bell[$t]}, @{$bell[$s]};
265 6147 $s = $skip[$s];
266 }
267  
268 23681 $skip[$t] = $tree[$s]->{$token};
269 }
270 }
271  
272 188058 ▶ $tree[$q] = $null_hash unless keys %{$tree[$q]}; # economize with memory
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$tree[$q];} 153642 34416 188058
273 }
274  
275 15 ▶ for ($q = 1; $q < @bell; $q++) { # optimize the bell function for $q > 0
Branch: 1 branch
BranchTrueFalseTotal
if ($q < @bell) 188058 15 188073
276  
277 188058 ▶ if (grep { UNIVERSAL::isa($_, 'CODE') } @{$bell[$q]}) {
Branch: 2 branches
BranchTrueFalseTotal
if (grep {UNIVERSAL::isa($_, 'CODE');} @{$bell[$q];}) { } 8 188050 188058
elsif (defined $Encode::Mapper::option{'join'}) { } 0 188050 188050
278 8 $no_code = 0;
279 }
280 elsif (defined $option{'join'}) {
281 0 $bell[$q] = join $option{'join'}, @{$bell[$q]};
282 0 next;
283 }
284  
285 188058 ▶ if (@{$bell[$q]} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$bell[$q];} == 1) { } 68344 119714 188058
286 68344 $bell[$q] = $bell[$q]->[0];
287 }
288 else {
289 119714 ▶ $bell[$q] = $null_list if @{$bell[$q]} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @{$bell[$q];} == 0 20 119694 119714
290 119714 $no_list = 0;
291 }
292 }
293  
294 return bless {
295 'current' => 0,
296 'tree' => \@tree,
297 'bell' => \@bell,
298 'skip' => \@skip,
299 'null' => { 'list' => $null_list, 'hash' => $null_hash },
300 15 'join' => $option{'join'},
301 'no_code' => $no_code,
302 'no_list' => $no_list,
303 }, $cls;
304 }
305  
306  
307 18 ▶sub process ($@) { # returns the list of search results performed by Mapper
Subroutine process: called CC=9 SCAR=22.0
Pod documented
308 18 my $obj = shift @_;
309 18 my (@returns, $phrase, $token, $q);
310  
311 18 $q = $obj->{'current'};
312  
313 18 ▶ if ($obj->{'no_list'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->{'no_list'}) { } 7 11 18
314 7 foreach $phrase (@_) {
315 118 foreach $token (split //, $phrase) {
316 724 ▶ until (defined $obj->{'tree'}[$q]->{$token}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $obj->{"tree"}[$q]{$token}) 496 724 1220
317 496 push @returns, $obj->{'bell'}[$q];
318 496 $q = $obj->{'skip'}[$q];
319 }
320 724 $q = $obj->{'tree'}[$q]->{$token};
321 }
322 }
323 }
324 else {
325 11 foreach $phrase (@_) {
326 21 foreach $token (split //, $phrase) {
327 572 ▶ until (defined $obj->{'tree'}[$q]->{$token}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $obj->{"tree"}[$q]{$token}) 386 572 958
328 push @returns, ref $obj->{'bell'}[$q] eq 'ARRAY' ?
329 386 ▶ @{$obj->{'bell'}[$q]} : $obj->{'bell'}[$q];
Branch: 1 branch
BranchTrueFalseTotal
ref $obj->{'bell'}[$q] eq 'ARRAY' ? : 27 359 386
330 386 $q = $obj->{'skip'}[$q];
331 }
332 572 $q = $obj->{'tree'}[$q]->{$token};
333 }
334 }
335 }
336  
337 18 $obj->{'current'} = $q;
338  
339 18 return @returns;
340 }
341  
342  
343 18 ▶sub recover ($;$$) { # returns the 'in-progress' search result and resets Mapper
Subroutine recover: called CC=7 SCAR=19.5
Pod documented
344 18 my ($obj, $r, $q) = @_;
345 18 my (@returns);
346  
347 18 ▶ $q = $obj->{'current'} unless defined $q;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $q 18 0 18
348  
349 18 ▶ if ($obj->{'no_list'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->{'no_list'}) { } 7 11 18
350 7 ▶ until ($q == 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($q == 0) 7 7 14
351 7 push @returns, $obj->{'bell'}[$q];
352 7 $q = $obj->{'skip'}[$q];
353 }
354 }
355 else {
356 11 ▶ until ($q == 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($q == 0) 13 11 24
357 push @returns, ref $obj->{'bell'}[$q] eq 'ARRAY' ?
358 13 ▶ @{$obj->{'bell'}[$q]} : $obj->{'bell'}[$q];
Branch: 1 branch
BranchTrueFalseTotal
ref $obj->{'bell'}[$q] eq 'ARRAY' ? : 2 11 13
359 13 $q = $obj->{'skip'}[$q];
360 }
361 }
362  
363 18 ▶ $obj->{'current'} = defined $r ? $r : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $r ? : 0 18 18
364  
365 18 return @returns;
366 }
367  
368  
369 0 ▶sub compute ($@) {
Subroutine compute: not called CC=4 SCAR=30.0
Pod documented
370 0 my $obj = shift @_;
371 0 my (@returns, $phrase, $token, $q);
372  
373 0 $obj->recover();
374  
375 0 foreach $phrase (@_) {
376 0 foreach $token (split //, $phrase) {
377 0 push @returns, [$token, $obj->{'current'}];
378 0 push @{$returns[-1]}, [$obj->process($token)];
379 0 $q = $obj->{'current'};
380 0 push @{$returns[-1]}, $q, $obj->{'bell'}[$q], $obj->{'skip'}[$q];
381 }
382 }
383  
384 0 push @returns, ['recover', $obj->{'current'}];
385 0 push @{$returns[-1]}, [$obj->recover()];
386 0 $q = $obj->{'current'};
387 0 ▶ push @{$returns[-1]}, $q, $obj->{'bell'}[$q], ($q == 0 ? 'undef' : $obj->{'skip'}[$q]);
Branch: 1 branch
BranchTrueFalseTotal
$q == 0 ? : 0 0 0
388  
389 0 return @returns;
390 }
391  
392  
393 0 ▶sub dumper ($;$) {
Subroutine dumper: not called CC=2 SCAR=17.9
Pod documented
394 0 my ($obj, $ref) = @_;
395  
396 0 ▶ $ref = ['L', 'H', 'mapper'] unless defined $ref;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ref 0 0 0
397  
398 0 require Data::Dumper;
399  
400 0 return Data::Dumper->new([$obj->{'null'}{'list'}, $obj->{'null'}{'hash'}, $obj], $ref);
401 }
402  
403  
404 0 ▶sub describe ($;$) {
Subroutine describe: not called CC=17 SCAR=57.2
Pod documented
405 0 my ($obj, $ref) = @_;
406 0 my ($q, $nodes, $edges, $skips, $bells, $paths, $lists);
407  
408 0 $nodes = @{$obj->{'tree'}};
409 0 $edges = [];
410 0 $lists = [];
411  
412 0 ▶ if ($obj->{'no_list'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->{'no_list'}) { } 0 0 0
413 0 ▶ for ($q = 0; $q < @{$obj->{'tree'}}; $q++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($q < @{$obj->{"tree"};}) 0 0 0
414 0 $lists->[$q * 3] = scalar %{$obj->{'tree'}[$q]};
415 0 $lists->[$q * 3] =~ m{^([0-9]+)(?:/([0-9]+))?$};
416  
417 0 $edges->[0] += scalar keys %{$obj->{'tree'}[$q]};
418 0 $lists->[$q * 3] .= " " . keys %{$obj->{'tree'}[$q]};
419  
420 0 ▶ if (defined $2) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $2) { } 0 0 0
421 0 $edges->[1] += $1;
422 0 $edges->[2] += $2;
423 }
424 else {
425 0 ▶ $paths++ unless $1;
Branch: 1 branch
BranchTrueFalseTotal
unless $1 0 0 0
426 }
427  
428 0 ▶ $lists->[$q * 3 + 1] = $obj->{'bell'}[$q] eq "" ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$obj->{'bell'}[$q] eq '' ? : 0 0 0
429 0 $bells += $lists->[$q * 3 + 1];
430  
431 0 ▶ next if $q == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $q == 0 0 0 0
432  
433 0 $lists->[$obj->{'skip'}[$q] * 3 + 2]++;
434 0 ▶ $skips++ unless $obj->{'skip'}[$q] == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $obj->{"skip"}[$q] == 0 0 0 0
435 }
436 }
437 else {
438 0 ▶ for ($q = 0; $q < @{$obj->{'tree'}}; $q++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($q < @{$obj->{"tree"};}) 0 0 0
439 0 $lists->[$q * 3] = scalar %{$obj->{'tree'}[$q]};
440 0 $lists->[$q * 3] =~ m{^([0-9]+)(?:/([0-9]+))?$};
441  
442 0 $edges->[0] += scalar keys %{$obj->{'tree'}[$q]};
443 0 $lists->[$q * 3] .= " " . keys %{$obj->{'tree'}[$q]};
444  
445 0 ▶ if (defined $2) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $2) { } 0 0 0
446 0 $edges->[1] += $1;
447 0 $edges->[2] += $2;
448 }
449 else {
450 0 ▶ $paths++ unless $1;
Branch: 1 branch
BranchTrueFalseTotal
unless $1 0 0 0
451 }
452  
453 0 ▶ $lists->[$q * 3 + 1] = ref $obj->{'bell'}[$q] eq 'ARRAY' ? scalar @{$obj->{'bell'}[$q]} : 1;
Branch: 1 branch
BranchTrueFalseTotal
ref $obj->{'bell'}[$q] eq 'ARRAY' ? : 0 0 0
454 0 $bells += $lists->[$q * 3 + 1];
455  
456 0 ▶ next if $q == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $q == 0 0 0 0
457  
458 0 $lists->[$obj->{'skip'}[$q] * 3 + 2]++;
459 0 ▶ $skips++ unless $obj->{'skip'}[$q] == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $obj->{"skip"}[$q] == 0 0 0 0
460 }
461 }
462  
463 0 my $return = {'nodes' => $nodes, 'edges' => $edges->[0], 'slots' => $edges->[1] . "/" . $edges->[2],
464 'skips' => $skips, 'bells' => $bells, 'paths' => $paths, 'lists' => $lists};
465  
466 0 ▶ if (defined $ref) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ref) 0 0 0
467 0 ▶ $ref = *STDERR if ref $ref ne 'GLOB';
Branch: 1 branch
BranchTrueFalseTotal
if ref $ref ne "GLOB" 0 0 0
468 0 ▶ print $ref ( join ", ", map { ( defined $return->{$_} ? $return->{$_} : 'undef' ) . " " . $_ }
Branch: 1 branch
BranchTrueFalseTotal
defined $return->{$_} ? : 0 0 0
469 0 grep { $_ ne 'lists' } keys %{$return} ) . "\n";
470 }
471  
472 0 return $return;
473 }
474  
475  
476 6 ▶sub encode ($$$;$) {
Subroutine encode: called CC=14 SCAR=33.6
Pod documented
477 6 my ($cls, $text, $encoder, $enc) = @_;
478 6 my ($mapper, $join);
479  
480 6 ▶ local %option = exists $options{caller 0} ? %{$options{caller 0}} : ();
Branch: 1 branch
BranchTrueFalseTotal
exists $Encode::Mapper::options{caller 0} ? : 6 0 6
481 # options be local due to whisper
482  
483 6 require Encode;
484  
485 6 ▶ unless (Encode::is_utf8($text)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&Encode::is_utf8($text)) 0 6 6
486 0 whisper "The input text is not in Perl's internal utf8 .. note only, might be fine";
487 }
488  
489 6 ▶ if ($enc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($enc) 0 6 6
490 0 ▶ unless (Encode::resolve_alias($enc)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&Encode::resolve_alias($enc)) 0 0 0
491 0 carp "Cannot resolve the proposed '$enc' encoding";
492 0 return undef;
493 }
494  
495 0 $text = Encode::encode($enc, $text);
496 }
497  
498 6 ▶ if (not UNIVERSAL::isa($encoder, 'ARRAY') or grep { defined $_ and not $_->isa($cls) } @{$encoder}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not UNIVERSAL::isa($encoder, "ARRAY") or grep {defined $_ and not $_->isa($cls);} @{$encoder;}) 0 6 6
Condition50%3/6
not UNIVERSAL::isa($encoder, "ARRAY") or grep {defined $_ and not $_->isa($cls);} @{$encoder;}
l!l&&r!l&&!r
006
defined $_ && !$_->isa($cls)
!ll&&!rl&&r
2060
MC/DC0%0/2
not UNIVERSAL::isa($encoder, "ARRAY") or grep {defined $_ and not $_->isa($cls);} @{$encoder;}
not UNIVERSAL::isa($encoder, "ARRAY") grep {defined $_ and not $_->isa($cls);} @{$encoder;}
MC/DC0%0/2
defined $_ && !$_->isa($cls)
defined $_ !$_->isa($cls)
Truth table33%1/3
not(A) or B
A= not UNIVERSAL::isa($encoder, "ARRAY")B= grep {defined $_ and not $_->isa($cls);} @{$encoder;}
ABresult
1X1
011
000
Truth table66%2/3
A && not(B)
A= defined $_B= !$_->isa($cls)
ABresult
0X0
100
111
499 0 carp "Expecting a reference to an array of '$cls' objects";
500 0 return undef;
501 }
502  
503 6 foreach $mapper (@{$encoder}) {
504 10 ▶ last unless defined $mapper;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mapper 4 6 10
505  
506 $join = defined $mapper->{'join'} ? $mapper->{'join'} :
507 6 ▶ defined $option{'join'} ? $option{'join'} : "";
Branch: 2 branches
BranchTrueFalseTotal
defined $mapper->{'join'} ? : 0 6 6
defined $Encode::Mapper::option{'join'} ? : 4 2 6
508  
509 6 ▶ if ($mapper->{'no_code'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mapper->{'no_code'}) { } 6 0 6
510 6 $text = join $join, $mapper->process($text), $mapper->recover();
511 }
512 else {
513 $text = join $join, map {
514 0 ▶ UNIVERSAL::isa($_, 'CODE') ? $_->() : $_
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_, 'CODE') ? : 0 0 0
515 } $mapper->process($text), $mapper->recover();
516 }
517 }
518  
519 6 return $text;
520 }
521  
522  
523 3 ▶sub decode ($$$;$) {
Subroutine decode: called CC=14 SCAR=31.7
Pod documented
524 3 my ($cls, $text, $decoder, $enc) = @_;
525 3 my ($mapper, $join);
526  
527 3 ▶ local %option = exists $options{caller 0} ? %{$options{caller 0}} : ();
Branch: 1 branch
BranchTrueFalseTotal
exists $Encode::Mapper::options{caller 0} ? : 3 0 3
528 # options be local due to tradition ^^
529  
530 3 require Encode;
531  
532 3 ▶ $enc = 'utf8' unless $enc;
Branch: 1 branch
BranchTrueFalseTotal
unless $enc 0 3 3
533  
534 3 ▶ unless (Encode::resolve_alias($enc)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&Encode::resolve_alias($enc)) 0 3 3
535 0 carp "Cannot resolve the proposed '$enc' encoding";
536 0 return undef;
537 }
538  
539 3 ▶ if (not UNIVERSAL::isa($decoder, 'ARRAY') or grep { defined $_ and not $_->isa($cls) } @{$decoder}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not UNIVERSAL::isa($decoder, "ARRAY") or grep {defined $_ and not $_->isa($cls);} @{$decoder;}) 0 3 3
Condition50%3/6
not UNIVERSAL::isa($decoder, "ARRAY") or grep {defined $_ and not $_->isa($cls);} @{$decoder;}
l!l&&r!l&&!r
003
defined $_ && !$_->isa($cls)
!ll&&!rl&&r
690
MC/DC0%0/2
not UNIVERSAL::isa($decoder, "ARRAY") or grep {defined $_ and not $_->isa($cls);} @{$decoder;}
not UNIVERSAL::isa($decoder, "ARRAY") grep {defined $_ and not $_->isa($cls);} @{$decoder;}
MC/DC0%0/2
defined $_ && !$_->isa($cls)
defined $_ !$_->isa($cls)
Truth table33%1/3
not(A) or B
A= not UNIVERSAL::isa($decoder, "ARRAY")B= grep {defined $_ and not $_->isa($cls);} @{$decoder;}
ABresult
1X1
011
000
Truth table66%2/3
A && not(B)
A= defined $_B= !$_->isa($cls)
ABresult
0X0
100
111
540 0 carp "Expecting a reference to an array of $cls objects";
541 0 return undef;
542 }
543  
544 3 foreach $mapper (@{$decoder}) {
545 9 ▶ last unless defined $mapper;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mapper 2 7 9
546  
547 $join = defined $mapper->{'join'} ? $mapper->{'join'} :
548 7 ▶ defined $option{'join'} ? $option{'join'} : "";
Branch: 2 branches
BranchTrueFalseTotal
defined $mapper->{'join'} ? : 0 7 7
defined $Encode::Mapper::option{'join'} ? : 4 3 7
549  
550 7 ▶ if ($mapper->{'no_code'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mapper->{'no_code'}) { } 7 0 7
551 7 $text = join $join, $mapper->process($text), $mapper->recover();
552 }
553 else {
554 $text = join $join, map {
555 0 ▶ UNIVERSAL::isa($_, 'CODE') ? $_->() : $_
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_, 'CODE') ? : 0 0 0
556 } $mapper->process($text), $mapper->recover();
557 }
558 }
559  
560 3 ▶ return Encode::is_utf8($text) ? $text : Encode::decode($enc, $text);
Branch: 1 branch
BranchTrueFalseTotal
&Encode::is_utf8($text) ? : 0 3 3
561 }
562  
563  
564 8 1;
565  
566 __END__
« lib/Encode/Arabic/CGI.pm ↑ Summary lib/Encode/Arabic/ArabTeX/RE.pm »