blib/lib/Search/Tools/UTF8.pm

filter: Statementstmt 80.6% 75 / 93 Branchbran 57.5% 23 / 40 Conditioncond 72.7% 16 / 22 MC/DCmcdc 54.5% 6 / 11 Subroutinesub 100% 15 / 15 Podpod 100% 7 / 7 totaltotal 75.5% 142 / 188 CC 27 SCAR 21.8
CC
27
Cov
74%
is_sane_utf8
21.5
to_utf8
13.0
looks_like_cp1252
4.3
CRAP 8.9
« blib/lib/Search/Tools/HiLiter.pm ↑ Summary blib/lib/Search/Tools/Tokenizer.pm »
1 package Search::Tools::UTF8;
2 39 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 39 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 39 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 39 ▶use Search::Tools; # XS stuff
Subroutine BEGIN: called CC=1 SCAR=0.0
6 39 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 39 ▶use charnames ':full';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 39 ▶use Data::Dump qw( dump );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 39 ▶use base qw( Exporter );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 39 our @EXPORT = qw(
11 to_utf8
12 is_valid_utf8
13 is_flagged_utf8
14 is_perl_utf8_string
15 is_ascii
16 is_latin1
17 is_sane_utf8
18 find_bad_utf8
19 find_bad_ascii
20 find_bad_latin1
21 find_bad_latin1_report
22 byte_length
23 looks_like_cp1252
24 fix_cp1252_codepoints_in_utf8
25 debug_bytes
26 );
27  
28 39 ▶our $Debug = ( $ENV{PERL_DEBUG} && $ENV{PERL_DEBUG} > 2 ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'PERL_DEBUG'} && $ENV{'PERL_DEBUG'} > 2 ? : 0 39 39
Condition33%1/3
$ENV{"PERL_DEBUG"} and $ENV{"PERL_DEBUG"} > 2
!ll&&!rl&&r
3900
MC/DC0%0/2
$ENV{"PERL_DEBUG"} and $ENV{"PERL_DEBUG"} > 2
$ENV{"PERL_DEBUG"} $ENV{"PERL_DEBUG"} > 2
Truth table33%1/3
A and B
A= $ENV{"PERL_DEBUG"}B= $ENV{"PERL_DEBUG"} > 2
ABresult
0X0
100
111
29  
30 39 our $VERSION = '1.007';
31  
32 604 ▶sub to_utf8 {
Subroutine to_utf8: called CC=12 SCAR=25.6
Pod documented
33 604 my $str = shift;
34 604 ▶ Carp::cluck("\$str is undefined") unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 0 604 604
35  
36 604 ▶ my $charset = shift || 'iso-8859-1';
Condition100%2/2
shift() || 'iso-8859-1'
l!l
496108
MC/DC100%1/1
shift() || 'iso-8859-1'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
37  
38 # checks first
39 604 ▶ if ( is_flagged_utf8($str) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_flagged_utf8($str)) 393 211 604
40 393 ▶ $Debug and carp "string '$str' is flagged utf8 already";
Branch: 1 branch
BranchTrueFalseTotal
$Search::Tools::UTF8::Debug and carp("string '${str}' is flagged utf8 already") 0 393 393
41 393 return $str;
42 }
43 211 ▶ if ( is_ascii($str) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_ascii($str)) 193 18 211
44 193 Encode::_utf8_on($str);
45 193 ▶ $Debug and carp "string '$str' is ascii; utf8 flag turned on";
Branch: 1 branch
BranchTrueFalseTotal
$Search::Tools::UTF8::Debug and carp("string '${str}' is ascii; utf8 flag turned on") 0 193 193
46 193 return $str;
47 }
48 18 ▶ if ( is_valid_utf8($str) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_valid_utf8($str)) 6 12 18
49  
50 # we got here only because the flag was off and it wasn't ascii.
51 # however, is_valid_utf8() claims that it is valid internal UTF8,
52 # so just turn the flag on.
53 6 Encode::_utf8_on($str);
54 6 ▶ $Debug and carp "string '$str' is valid utf8; utf8 flag turned on";
Branch: 1 branch
BranchTrueFalseTotal
$Search::Tools::UTF8::Debug and carp("string '${str}' is valid utf8; utf8 flag turned on") 0 6 6
55 6 return $str;
56 }
57  
58 $Debug
59 12 ▶ and carp "converting $str from $charset -> utf8";
Branch: 1 branch
BranchTrueFalseTotal
$Search::Tools::UTF8::Debug and carp("converting $str from $charset -> utf8") 0 12 12
60 12 my $c = Encode::decode( $charset, $str );
61 12 ▶ $Debug and carp "converted $c";
Branch: 1 branch
BranchTrueFalseTotal
$Search::Tools::UTF8::Debug and carp("converted $c") 0 12 12
62  
63 12 ▶ unless ( is_sane_utf8( $c, 1 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (is_sane_utf8($c, 1)) 0 12 12
64 0 carp "not sane: $c";
65 }
66  
67 12 return $c;
68 }
69  
70 611 ▶sub is_flagged_utf8 {
Subroutine is_flagged_utf8: called CC=1 SCAR=0.0
Pod documented
71 611 return Encode::is_utf8( $_[0] );
72 }
73  
74 my $re_bit = join "|",
75 39 map { Encode::encode( "utf8", chr($_) ) } ( 127 .. 255 );
76  
77 #binmode STDERR, ":utf8";
78 #print STDERR $re_bit;
79  
80 15 ▶sub is_sane_utf8 {
Subroutine is_sane_utf8: called CC=7 SCAR=30.7
Pod documented
81 15 my $string = shift;
82 15 ▶ my $warnings = shift || $Debug || 0;
Condition80%4/5
shift() || $Search::Tools::UTF8::Debug || 0
l!l
141
shift() or $Search::Tools::UTF8::Debug
l!l&&r!l&&!r
1401
MC/DC50%1/2
shift() || $Search::Tools::UTF8::Debug || 0
shift() $Search::Tools::UTF8::Debug
Truth table66%2/3
A or B
A= shift()B= $Search::Tools::UTF8::Debug
ABresult
1X1
011
000
83  
84 15 my $is_insane = 0;
85 15 ▶ while ( $string =~ /($re_bit)/go ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /($re_bit)/go) 0 15 15
86  
87 # work out what the double encoded string was
88 0 my $bytes = $1;
89  
90 0 my $index = $+[0] - length($bytes);
91 0 my $codes = join '', map { sprintf '<%00x>', ord($_) } split //,
92 $bytes;
93  
94 # what character does that represent?
95 0 my $char = Encode::decode( "utf8", $bytes );
96 0 my $ord = ord($char);
97 0 my $hex = sprintf '%00x', $ord;
98 0 $char = charnames::viacode($ord);
99  
100 # print out diagnostic messages
101 0 ▶ if ($warnings) {
Branch: 1 branch
BranchTrueFalseTotal
if ($warnings) 0 0 0
102  
103 0 warn(qq{Found dodgy chars "$codes" at char $index\n});
104 0 ▶ if ( Encode::is_utf8($string) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Encode::is_utf8($string)) { } 0 0 0
105 0 warn("Chars in utf8 string look like utf8 byte sequence.");
106 }
107 else {
108 0 warn("String not flagged as utf8...was it meant to be?\n");
109 }
110 0 warn(
111 "Probably originally a $char char - codepoint $ord (dec), $hex (hex)\n"
112 );
113  
114 }
115 0 $is_insane++;
116 }
117  
118 15 ▶ return $is_insane ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$is_insane ? : 0 15 15
119 }
120  
121 39 ▶sub is_valid_utf8 {
Subroutine is_valid_utf8: called CC=4 SCAR=13.9
Pod documented
122 39 ▶ if ( is_latin1( $_[0] )
Branch: 1 branch
BranchTrueFalseTotal
if (is_latin1($_[0]) and not is_ascii($_[0]) and not is_perl_utf8_string($_[0])) 8 31 39
Condition100%6/6
is_latin1($_[0]) and not is_ascii($_[0]) and not is_perl_utf8_string($_[0])
!ll&&!rl&&r
2658
is_latin1($_[0]) and not is_ascii($_[0])
!ll&&!rl&&r
24213
MC/DC100%3/3
is_latin1($_[0]) and not is_ascii($_[0]) and not is_perl_utf8_string($_[0])
is_latin1($_[0]) not is_ascii($_[0]) not is_perl_utf8_string($_[0])
Truth table100%4/4
A and not(B) and not(C)
A= is_latin1($_[0])B= not is_ascii($_[0])C= not is_perl_utf8_string($_[0])
ABCresult
0XX0
10X0
1100
1111
123 && !is_ascii( $_[0] )
124 && !is_perl_utf8_string( $_[0] ) )
125 {
126 8 return 0;
127 }
128 31 return is_perl_utf8_string( $_[0] );
129 }
130  
131 2 ▶sub find_bad_latin1_report {
Subroutine find_bad_latin1_report: called CC=2 SCAR=7.0
Pod documented
132 2 my $bad = find_bad_latin1(@_);
133 2 ▶ if ($bad) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bad) 2 0 2
134  
135 # explain why we failed
136 2 my $char = substr( $_[0], $bad, 1 );
137 2 my $dec = ord($char);
138 2 my $hex = sprintf '%x', $dec;
139 2 carp("byte $bad ($char) is not Latin1 (it's $dec dec / $hex hex)");
140 }
141 2 return $bad;
142 }
143  
144 6 ▶sub looks_like_cp1252 {
Subroutine looks_like_cp1252: called CC=4 SCAR=14.6
Pod documented
145 6 ▶ if ( !is_latin1( $_[0] )
Branch: 1 branch
BranchTrueFalseTotal
if (not is_latin1($_[0]) || is_ascii($_[0]) and $_[0] =~ /[\x80-\x9f]/) 5 1 6
Condition50%3/6
not is_latin1($_[0]) || is_ascii($_[0]) and $_[0] =~ /[\x80-\x9f]/
!ll&&!rl&&r
015
is_latin1($_[0]) || is_ascii($_[0])
l!l&&r!l&&!r
006
MC/DC33%1/3
not is_latin1($_[0]) || is_ascii($_[0]) and $_[0] =~ /[\x80-\x9f]/
is_latin1($_[0]) is_ascii($_[0]) $_[0] =~ /[\x80-\x9f]/
Truth table50%2/4
not(A || B) and C
A= is_latin1($_[0])B= is_ascii($_[0])C= $_[0] =~ /[\x80-\x9f]/
ABCresult
1XX0
01X0
0000
0011
146 && !is_ascii( $_[0] )
147 && $_[0] =~ m/[\x80-\x9f]/ )
148 {
149 5 return 1;
150 }
151 1 return 0;
152 }
153  
154 39 my %win1252 = (
155 "\x80" => "\x{20AC}", #EURO SIGN
156 "\x81" => '', #UNDEFINED
157 "\x82" => "\x{201A}", #SINGLE LOW-9 QUOTATION MARK
158 "\x83" => "\x{0192}", #LATIN SMALL LETTER F WITH HOOK
159 "\x84" => "\x{201E}", #DOUBLE LOW-9 QUOTATION MARK
160 "\x85" => "\x{2026}", #HORIZONTAL ELLIPSIS
161 "\x86" => "\x{2020}", #DAGGER
162 "\x87" => "\x{2021}", #DOUBLE DAGGER
163 "\x88" => "\x{02C6}", #MODIFIER LETTER CIRCUMFLEX ACCENT
164 "\x89" => "\x{2030}", #PER MILLE SIGN
165 "\x8A" => "\x{0160}", #LATIN CAPITAL LETTER S WITH CARON
166 "\x8B" => "\x{2039}", #SINGLE LEFT-POINTING ANGLE QUOTATION MARK
167 "\x8C" => "\x{0152}", #LATIN CAPITAL LIGATURE OE
168 "\x8D" => '', #UNDEFINED
169 "\x8E" => "\x{017D}", #LATIN CAPITAL LETTER Z WITH CARON
170 "\x8F" => '', #UNDEFINED
171 "\x90" => '', #UNDEFINED
172 "\x91" => "\x{2018}", #LEFT SINGLE QUOTATION MARK
173 "\x92" => "\x{2019}", #RIGHT SINGLE QUOTATION MARK
174 "\x93" => "\x{201C}", #LEFT DOUBLE QUOTATION MARK
175 "\x94" => "\x{201D}", #RIGHT DOUBLE QUOTATION MARK
176 "\x95" => "\x{2022}", #BULLET
177 "\x96" => "\x{2013}", #EN DASH
178 "\x97" => "\x{2014}", #EM DASH
179 "\x98" => "\x{02DC}", #SMALL TILDE
180 "\x99" => "\x{2122}", #TRADE MARK SIGN
181 "\x9A" => "\x{0161}", #LATIN SMALL LETTER S WITH CARON
182 "\x9B" => "\x{203A}", #SINGLE RIGHT-POINTING ANGLE QUOTATION MARK
183 "\x9C" => "\x{0153}", #LATIN SMALL LIGATURE OE
184 "\x9D" => '', #UNDEFINED
185 "\x9E" => "\x{017E}", #LATIN SMALL LETTER Z WITH CARON
186 "\x9F" => "\x{0178}", #LATIN CAPITAL LETTER Y WITH DIAERESIS
187  
188 );
189  
190 # fix_latin (used in Transliterate) lacks the check for the
191 # prefixed \xc2 byte, but the UTF-8 encoding for these
192 # Windows codepoints has the leading \xc2 byte.
193 1 ▶sub fix_cp1252_codepoints_in_utf8 {
Subroutine fix_cp1252_codepoints_in_utf8: called CC=3 SCAR=12.0
Pod documented
194 1 my $buf = shift;
195 1 ▶ unless ( is_valid_utf8($buf) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (is_valid_utf8($buf)) 0 1 1
196 0 my $badbyte = find_bad_utf8($buf);
197 0 croak "bad UTF-8 byte(s) at $badbyte [ " . dump($buf) . " ]";
198 }
199 1 ▶ $Debug and warn "converting $buf\n";
Branch: 1 branch
BranchTrueFalseTotal
$Search::Tools::UTF8::Debug and warn "converting $buf\n" 0 1 1
200 1 my $bytes = Encode::encode_utf8( to_utf8($buf) );
201 1 $bytes =~ s/\xc2([\x80-\x9f])/$win1252{$1}/g;
202 1 return to_utf8($bytes);
203 }
204  
205 39 1;
206  
207 __END__
« blib/lib/Search/Tools/HiLiter.pm ↑ Summary blib/lib/Search/Tools/Tokenizer.pm »