Encode.pm

filter: Statementstmt 80.9% 106 / 131 Branchbran 68.6% 59 / 86 Conditioncond 58.3% 7 / 12 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 82.6% 19 / 23 Podpod 100% 3 / 3 totaltotal 75.2% 198 / 263 CC 54 SCAR 27.6
CC
54
Cov
75%
_decode_bracket
72.0
encode
18.8
_decode
12.2
CRAP 15.9
« Encode/BibTeX.pm ↑ Summary Encode/charmap.pm »
1 package TeX::Encode;
2  
3 6 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 #use AutoLoader qw(AUTOLOAD);
7  
8 6 ▶use Encode::Encoding 0.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 6 ▶use TeX::Encode::charmap;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use TeX::Encode::BibTeX;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 6 our @ISA = qw(Encode::Encoding);
15  
16 6 our $VERSION = '2.010'; # VERSION
17  
18 6 __PACKAGE__->Define(qw(LaTeX latex));
19  
20 sub _bad_cp
21 1 ▶{
Subroutine _bad_cp: called CC=1 SCAR=0.0
22 1 return sprintf("Unsupported character code point 0x%04x\n", ord($_[0]));
23 }
24  
25 sub encode
26 19 ▶{
Subroutine encode: called CC=17 SCAR=29.3
Pod documented
27 19 my( undef, $string, $check ) = @_;
28  
29 19 my $bad_cp = 0;
30  
31 # set up a "check" sub that will determine how we handle unsupported code
32 # points
33 19 ▶ $check = Encode::FB_DEFAULT if !defined $check;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $check 0 19 19
34 19 ▶ if( $check eq Encode::FB_DEFAULT or $check eq "")
Branch: 4 branches
BranchTrueFalseTotal
if ($check eq 0 or $check eq '') { } 17 2 19
elsif ($check eq 1) { } 1 1 2
elsif ($check eq 4) { } 1 0 1
elsif ($check eq 6) { } 0 0 0
Condition100%3/3
$check eq 0 or $check eq ""
l!l&&r!l&&!r
1162
MC/DC100%2/2
$check eq 0 or $check eq ""
$check eq 0 $check eq ""
Truth table100%3/3
A or B
A= $check eq 0B= $check eq ""
ABresult
1X1
011
000
35 {
36 17 ▶ $check = sub { '?' };
Subroutine __ANON__: called CC=1 SCAR=0.0
37 }
38 elsif( $check eq Encode::FB_CROAK )
39 {
40 1 ▶ $check = sub { Carp::croak(&_bad_cp(@_)) };
Subroutine __ANON__: called CC=1 SCAR=0.0
41 }
42 elsif( $check eq Encode::FB_QUIET )
43 {
44 1 ▶ $check = sub { $bad_cp = 1; '' };
Subroutine __ANON__: called CC=1 SCAR=0.0
45 }
46 elsif( $check eq Encode::FB_WARN )
47 {
48 0 ▶ $check = sub { Carp::carp(&_bad_cp(@_)); $bad_cp = 1; '' };
Subroutine __ANON__: not called CC=1 SCAR=6.9
49 }
50 else
51 {
52 0 Carp::confess( "Unknown check argument: expected one of undef, FB_DEFAULT, FB_CROAK, FB_QUIET or FB_WARN" );
53 }
54  
55 19 my $tex = "";
56  
57 19 pos($string) = 0;
58  
59 19 for($string)
60 {
61 19 ▶ while(!$bad_cp) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($bad_cp) 53 1 54
62 53 ▶ last if pos($_) == length($_);
Branch: 1 branch
BranchTrueFalseTotal
if pos $_ == length $_ 17 36 53
63  
64 # escape reserved characters
65 36 ▶ /\G($TeX::Encode::charmap::RESERVED_RE)/gc and ($tex .= $TeX::Encode::charmap::RESERVED{$1}, next);
Branch: 1 branch
BranchTrueFalseTotal
/\G($TeX::Encode::charmap::RESERVED_RE)/cg and $tex .= $TeX::Encode::charmap::RESERVED{$1}, next 9 27 36
66  
67 # escape all characters supported by tex
68 27 ▶ if( /\G($TeX::Encode::charmap::CHAR_MAP_RE)/gc )
Branch: 1 branch
BranchTrueFalseTotal
if (/\G($TeX::Encode::charmap::CHAR_MAP_RE)/cg) 9 18 27
69 {
70 9 $tex .= $TeX::Encode::charmap::CHAR_MAP{$1};
71 9 ▶ if( /\G[a-zA-Z_]/gc )
Branch: 1 branch
BranchTrueFalseTotal
if (/\G[a-zA-Z_]/cg) 2 7 9
72 {
73 2 --pos($_);
74 2 ▶ $tex =~ /[a-zA-Z_]$/ and $tex .= '{}';
Branch: 1 branch
BranchTrueFalseTotal
$tex =~ /[a-zA-Z_]$/ and $tex .= '{}' 2 0 2
75 }
76 9 next;
77 }
78  
79 # basic unreserved characters
80 18 ▶ /\G([\sa-zA-Z0-9\.,:;'"\(\)=\-\/\[\]\*\+!]+)/gc and ($tex .= $1, next);
Branch: 1 branch
BranchTrueFalseTotal
m<\G([\sa-zA-Z0-9\.,:;'"\(\)=\-/\[\]\*\+!]+)>cg and $tex .= $1, next 15 3 18
81  
82 # unsupported code point (may set $bad_cp)
83 3 ▶ /\G(.)/gc and ($tex .= &$check(ord($1)), next);
Branch: 1 branch
BranchTrueFalseTotal
/\G(.)/cg and $tex .= &$check(ord $1), next 3 0 3
84  
85 0 Carp::confess "Shouldn't happen";
86 }
87 }
88  
89 18 ▶ if( $bad_cp )
Branch: 1 branch
BranchTrueFalseTotal
if ($bad_cp) 1 17 18
90 {
91 1 $_[1] = substr($string,pos($string)-1);
92 }
93  
94 18 return $tex;
95 }
96  
97 # decode($octets [,$check])
98 sub decode
99 42 ▶{
Subroutine decode: called CC=2 SCAR=6.9
Pod documented
100 42 my( undef, $tex, $check ) = @_;
101  
102 42 pos($tex) = 0;
103  
104 42 my $str = "";
105  
106 42 ▶ while(pos($tex) < length($tex))
Branch: 1 branch
BranchTrueFalseTotal
if (pos $tex < length $tex) 233 42 275
107 {
108 233 $str .= _decode( $tex, $check );
109 }
110  
111 42 return $str;
112 }
113  
114 sub _decode
115 238 ▶{
Subroutine _decode: called CC=12 SCAR=25.0
116 238 my $str = Encode::decode_utf8( "" );
117  
118 238 for($_[0])
119 {
120 238 ▶ /\G\%([^\n]+\n)?/gc and next; # comment
Branch: 1 branch
BranchTrueFalseTotal
/\G\%([^\n]+\n)?/cg and next 0 238 238
121 # not sure about this:
122 # /\G\\ensuremath/gc and ($str .= _decode_mathmode(_decode_bracket($_)), next); # mathmode
123 238 ▶ /\G\$/gc and ($str .= _decode_mathmode($_), next); # mathmode
Branch: 1 branch
BranchTrueFalseTotal
/\G\$/cg and $str .= _decode_mathmode($_), next 9 229 238
124 229 ▶ /\G($TeX::Encode::charmap::MACROS_RE)/gc and ($str .= $TeX::Encode::charmap::MACROS{$1}, next); # macro
Branch: 1 branch
BranchTrueFalseTotal
/\G($TeX::Encode::charmap::MACROS_RE)/cg and $str .= $TeX::Encode::charmap::MACROS{$1}, next 156 73 229
125 73 ▶ /\G\\(.)/gc and ($str .= _decode_macro($1,$_), next); # unknown macro
Branch: 1 branch
BranchTrueFalseTotal
/\G\\(.)/cg and $str .= _decode_macro($1, $_), next 1 72 73
126 72 ▶ /\G\{/gc and ($str .= _decode_brace($_), next); # {foo}
Branch: 1 branch
BranchTrueFalseTotal
/\G\{/cg and $str .= _decode_brace($_), next 10 62 72
127 62 ▶ /\G\[/gc and ($str .= _decode_bracket($_), next); # [foo]
Branch: 1 branch
BranchTrueFalseTotal
/\G\[/cg and $str .= _decode_bracket($_), next 0 62 62
128 62 ▶ /\G_/gc and ($str .= _subscript(&_decode), next); # _ (subscript)
Branch: 1 branch
BranchTrueFalseTotal
/\G_/cg and $str .= _subscript(&_decode), next 2 60 62
129 60 ▶ /\G\^/gc and ($str .= _superscript(&_decode), next); # ^ (superscript)
Branch: 1 branch
BranchTrueFalseTotal
/\G\^/cg and $str .= _superscript(&_decode), next 3 57 60
130 57 ▶ /\G([^_\^\%\$\\\{\[ \t\n\r]+)/gc and $str .= $1, next;
Branch: 1 branch
BranchTrueFalseTotal
/\G([^_\^\%\$\\\{\[ \t\n\r]+)/cg and $str .= $1, next 27 30 57
131 30 ▶ /\G([ \t\n\r])+/gc and $str .= $1, next;
Branch: 1 branch
BranchTrueFalseTotal
/\G([ \t\n\r])+/cg and $str .= $1, next 30 0 30
132  
133 0 Carp::confess "Shouldn't happen: ".substr($_,0,10)." ...".substr($_,pos($_),10)." [".pos($_)."/".length($_)."]";
134 }
135  
136 238 return $str;
137 }
138  
139 sub _subscript
140 2 ▶{
Subroutine _subscript: called CC=2 SCAR=8.1
141 2 my( $tex ) = @_;
142 2 ▶ return $tex if $tex =~ /[^0-9+\-]/;
Branch: 1 branch
BranchTrueFalseTotal
if $tex =~ /[^0-9+\-]/ 2 0 2
143 0 return _subscript_digits( $tex );
144 }
145  
146 sub _superscript
147 3 ▶{
Subroutine _superscript: called CC=2 SCAR=7.1
148 3 my( $tex ) = @_;
149 3 ▶ return $tex if $tex =~ /[^0-9+\-]/;
Branch: 1 branch
BranchTrueFalseTotal
if $tex =~ /[^0-9+\-]/ 0 3 3
150 3 return _superscript_digits( $tex );
151 }
152  
153 6 my %SUBSCRIPTS = (
154 '+' => chr(0x208a),
155 '-' => chr(0x208b),
156 );
157 6 $SUBSCRIPTS{''.$_} = chr(0x2080+$_) for 0..9;
158 sub _subscript_digits
159 0 ▶{
Subroutine _subscript_digits: not called CC=1 SCAR=6.9
160 0 my( $tex ) = @_;
161 0 $tex =~ s/(.)/$SUBSCRIPTS{$1}/g;
162 0 return $tex;
163 }
164  
165 6 my %SUPERSCRIPTS = (
166 '0' => chr(0x2070),
167 '1' => chr(0xb9),
168 '2' => chr(0xb2),
169 '3' => chr(0xb3),
170 '4' => chr(0x2074),
171 '5' => chr(0x2075),
172 '6' => chr(0x2076),
173 '7' => chr(0x2077),
174 '8' => chr(0x2078),
175 '9' => chr(0x2079),
176 '+' => chr(0x207a),
177 '-' => chr(0x207b),
178 );
179 sub _superscript_digits
180 3 ▶{
Subroutine _superscript_digits: called CC=1 SCAR=0.0
181 3 my( $tex ) = @_;
182 3 $tex =~ s/(.)/$SUPERSCRIPTS{$1}/g;
183 3 return $tex;
184 }
185  
186 0 ▶sub perlio_ok { 0 }
Subroutine perlio_ok: not called CC=1 SCAR=6.9
Pod documented
187  
188 sub _decode_mathmode
189 9 ▶{
Subroutine _decode_mathmode: called CC=7 SCAR=20.0
190 9 my $str = "";
191  
192 9 for($_[0])
193 {
194 9 while(1) {
195 30 ▶ last if pos($_) == length($_);
Branch: 1 branch
BranchTrueFalseTotal
if pos $_ == length $_ 0 30 30
196
197 30 ▶ /\G(\\.)/gc and ($str .= $1, next);
Branch: 1 branch
BranchTrueFalseTotal
/\G(\\.)/cg and $str .= $1, next 8 22 30
198 22 ▶ /\G\$/gc and last;
Branch: 1 branch
BranchTrueFalseTotal
/\G\$/cg and last 9 13 22
199 13 ▶ /\G($TeX::Encode::charmap::MATH_CHARS_RE)/gc and ($str .= $TeX::Encode::charmap::MATH_CHARS{$1}, next);
Branch: 1 branch
BranchTrueFalseTotal
/\G($TeX::Encode::charmap::MATH_CHARS_RE)/cg and $str .= $TeX::Encode::charmap::MATH_CHARS{$1}, next 0 13 13
200 13 ▶ /\G([^\\\$]+)/gc and ($str .= $1, next);
Branch: 1 branch
BranchTrueFalseTotal
/\G([^\\\$]+)/cg and $str .= $1, next 13 0 13
201  
202 0 Carp::confess "Shouldn't happen";
203 }
204 }
205  
206 9 return decode(undef, $str);
207 }
208  
209 # try again to expand a macro
210 sub _decode_macro
211 1 ▶{
Subroutine _decode_macro: called CC=4 SCAR=14.6
212 1 my( $c ) = @_;
213  
214 1 my $str = "\\$c";
215  
216 1 for($_[1])
217 {
218 # expand \'{e} to \'e
219 1 ▶ /\G\{/ and ($str .= _decode_brace( $_ ), next);
Branch: 1 branch
BranchTrueFalseTotal
/\G\{/ and $str .= _decode_brace($_), next 0 1 1
220 1 last;
221 }
222  
223 1 ▶ return $TeX::Encode::charmap::MACROS{$str} || $str;
Condition33%1/3
$TeX::Encode::charmap::MACROS{$str} || $str
l!l&&r!l&&!r
010
MC/DC0%0/2
$TeX::Encode::charmap::MACROS{$str} || $str
$TeX::Encode::charmap::MACROS{$str} $str
Truth table33%1/3
A || B
A= $TeX::Encode::charmap::MACROS{$str}B= $str
ABresult
1X1
011
000
224 }
225  
226 sub _decode_bracket
227 0 ▶{
Subroutine _decode_bracket: not called CC=8 SCAR=42.8
228 0 my $str = "";
229  
230 0 my $depth = 1;
231 0 for($_[0])
232 {
233 0 while(1) {
234 0 ▶ last if pos($_) == length($_) or $depth == 0;
Branch: 1 branch
BranchTrueFalseTotal
if pos $_ == length $_ or $depth == 0 0 0 0
Condition0%0/3
pos $_ == length $_ or $depth == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
pos $_ == length $_ or $depth == 0
pos $_ == length $_ $depth == 0
Truth table0%0/3
A or B
A= pos $_ == length $_B= $depth == 0
ABresult
1X1
011
000
235  
236 0 ▶ /\G(\\.)/gc and ($str .= $1, next);
Branch: 1 branch
BranchTrueFalseTotal
/\G(\\.)/cg and $str .= $1, next 0 0 0
237 0 ▶ /\G\[/gc and (--$depth, next);
Branch: 1 branch
BranchTrueFalseTotal
/\G\[/cg and --$depth, next 0 0 0
238 0 ▶ /\G\]/gc and (++$depth, next);
Branch: 1 branch
BranchTrueFalseTotal
/\G\]/cg and ++$depth, next 0 0 0
239 0 ▶ /\G([^\\\[\]]+)/gc and ($str .= $1, next);
Branch: 1 branch
BranchTrueFalseTotal
/\G([^\\\[\]]+)/cg and $str .= $1, next 0 0 0
240  
241 0 Carp::confess "Shouldn't happen";
242 }
243 }
244  
245 0 return $str;
246 }
247  
248 sub _decode_brace
249 10 ▶{
Subroutine _decode_brace: called CC=8 SCAR=20.9
250 10 my $str = "";
251  
252 10 my $depth = 1;
253 10 for($_[0])
254 {
255 10 while(1) {
256 25 ▶ last if pos($_) == length($_) or $depth == 0;
Branch: 1 branch
BranchTrueFalseTotal
if pos $_ == length $_ or $depth == 0 10 15 25
Condition100%3/3
pos $_ == length $_ or $depth == 0
l!l&&r!l&&!r
5515
MC/DC100%2/2
pos $_ == length $_ or $depth == 0
pos $_ == length $_ $depth == 0
Truth table100%3/3
A or B
A= pos $_ == length $_B= $depth == 0
ABresult
1X1
011
000
257  
258 15 ▶ /\G(\\.)/gc and ($str .= $1, next);
Branch: 1 branch
BranchTrueFalseTotal
/\G(\\.)/cg and $str .= $1, next 1 14 15
259 14 ▶ /\G\}/gc and ($str .= '}', --$depth, next);
Branch: 1 branch
BranchTrueFalseTotal
/\G\}/cg and $str .= '}', --$depth, next 10 4 14
260 4 ▶ /\G\{/gc and ($str .= '{', ++$depth, next);
Branch: 1 branch
BranchTrueFalseTotal
/\G\{/cg and $str .= '{', ++$depth, next 0 4 4
261 4 ▶ /\G([^\\\{\}]+)/gc and ($str .= $1, next);
Branch: 1 branch
BranchTrueFalseTotal
/\G([^\\\{\}]+)/cg and $str .= $1, next 4 0 4
262  
263 0 Carp::confess "Shouldn't happen";
264 }
265 }
266  
267 10 chop($str); # remove trailing '}'
268  
269 10 return decode(undef, $str);
270 }
271  
272 6 1;
273 __END__
« Encode/BibTeX.pm ↑ Summary Encode/charmap.pm »