blib/lib/Text/Fy/Utils.pm

filter: Statementstmt 100% 71 / 71 Branchbran 92.8% 13 / 14 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 13 / 13 Podpod 0.0% 0 / 6 totaltotal 93.4% 100 / 107 CC 10 SCAR 10.0
CC
10
Cov
99%
_aconvert
5.0
commify
4.0
_make_tr
3.0
CRAP 2.7
↑ Summary
1 package Text::Fy::Utils;
2 1 $Text::Fy::Utils::VERSION = '0.10';
3 1 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Unicode::Normalize;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Encode qw(encode decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 require Exporter;
11 1 our @ISA = qw(Exporter);
12 1 our @EXPORT = qw();
13 1 our @EXPORT_OK = qw(asciify isoify simplify commify cv_to_win cv_from_win);
14  
15 1 my %cp1252_to_uni;
16  
17 1 for (128..159) {
18 32 $cp1252_to_uni{chr($_)} = decode('cp1252', chr($_));
19 }
20  
21 1 my %uni_to_ascii = (
22 "\x{20ac}" => q{E},
23 "\x{201a}" => q{,},
24 "\x{0192}" => q{f},
25 "\x{2026}" => q{_},
26 "\x{2020}" => q{+},
27 "\x{02c6}" => q{^},
28 "\x{2030}" => q{%},
29 # "\x{0160}" => q{S},
30 "\x{2039}" => q{<},
31 "\x{0152}" => q{O},
32 # "\x{017d}" => q{Z},
33 "\x{2022}" => q{.},
34 "\x{20dc}" => q{~},
35 # "\x{0161}" => q{s},
36 "\x{203a}" => q{>},
37 "\x{203a}" => q{>},
38 "\x{0153}" => q{o},
39 # "\x{017e}" => q{z},
40 # "\x{017e}" => q{Y},
41 "\x{0080}" => q{e},
42 "\x{0082}" => q{,},
43 "\x{0083}" => q{f},
44 "\x{0085}" => q{_},
45 "\x{0088}" => q{^},
46 "\x{0089}" => q{%},
47 "\x{008b}" => q{<},
48 "\x{008c}" => q{O},
49 "\x{0095}" => q{.},
50 "\x{0098}" => q{~},
51 "\x{0099}" => q{T},
52 "\x{009b}" => q{>},
53 "\x{009c}" => q{o},
54 "\x{00a1}" => q{!},
55 "\x{00a2}" => q{c},
56 "\x{00a3}" => q{L},
57 "\x{00a5}" => q{Y},
58 "\x{00a6}" => q{|},
59 "\x{00a9}" => q{C},
60 "\x{00aa}" => q{a},
61 "\x{00ae}" => q{R},
62 "\x{00b2}" => q{2},
63 "\x{00b3}" => q{3},
64 "\x{00b7}" => q{.},
65 "\x{00b9}" => q{1},
66 "\x{00ba}" => q{0},
67 "\x{00bf}" => q{?},
68 "\x{00c6}" => q{A},
69 "\x{00d7}" => q{x},
70 "\x{00d8}" => q{O},
71 "\x{00df}" => q{s},
72 "\x{00e6}" => q{a},
73 "\x{00f0}" => q{d},
74 "\x{00f8}" => q{o},
75 );
76  
77 1 my %uni_to_iso = (
78 "\x{201c}" => q{"},
79 "\x{201d}" => q{"},
80 "\x{201e}" => q{"},
81  
82 "\x{2018}" => q{'},
83 "\x{2019}" => q{'},
84  
85 "\x{2013}" => q{-},
86 "\x{2014}" => q{-},
87 );
88  
89 1 my %iso_to_ascii = (
90 "\x{008a}" => q{S},
91 "\x{008e}" => q{Z},
92 "\x{009a}" => q{s},
93 "\x{009e}" => q{z},
94 "\x{009f}" => q{Y},
95 "\x{00ba}" => q{o},
96  
97 "\x{0084}" => q{"},
98 "\x{0093}" => q{"},
99 "\x{0094}" => q{"},
100 "\x{00ab}" => q{"},
101 "\x{00bb}" => q{"},
102  
103 "\x{0091}" => q{'},
104 "\x{0092}" => q{'},
105 "\x{00b4}" => q{'},
106  
107 "\x{0096}" => q{-},
108 "\x{0097}" => q{-},
109 "\x{00ac}" => q{-},
110 "\x{00ad}" => q{-},
111 );
112  
113 1 my $convert_c2u = _make_tr(\%cp1252_to_uni);
114 1 my $convert_u2c = _make_tr(\%cp1252_to_uni, 'R');
115 1 my $convert_u2a = _make_tr(\%uni_to_ascii);
116 1 my $convert_u2i = _make_tr(\%uni_to_iso);
117 1 my $convert_i2a = _make_tr(\%iso_to_ascii);
118  
119 5 ▶sub _make_tr {
Subroutine _make_tr: called CC=3 SCAR=11.0
120 5 my ($href, $rev) = @_;
121  
122 5 my $from = join '', map { sprintf '\x{%04x}', ord($_) } sort keys %$href;
123 5 my $to = join '', map { sprintf '\x{%04x}', ord($href->{$_}) } sort keys %$href;
124  
125 5 ▶ my $code = 'sub { $_[0] =~ '.($rev ? "tr/$to/$from/" : "tr/$from/$to/").'; }';
Branch: 1 branch
BranchTrueFalseTotal
$rev ? : 1 4 5
126  
127 5 ▶ eval $code or die "Can't compile >$code< because $@";
Branch: 1 branch
BranchTrueFalseTotal
eval $code or die "Can't compile >$code< because $@" 5 0 5
128 }
129  
130 1 ▶sub asciify {
Subroutine asciify: called CC=1 SCAR=0.0
Pod undocumented
131 1 _aconvert($_[0], 0, 0);
132 }
133  
134 1 ▶sub isoify {
Subroutine isoify: called CC=1 SCAR=0.0
Pod undocumented
135 1 _aconvert($_[0], 1, 0);
136 }
137  
138 1 ▶sub simplify {
Subroutine simplify: called CC=1 SCAR=0.0
Pod undocumented
139 1 _aconvert($_[0], 2, 0);
140 }
141  
142 9 ▶sub _aconvert {
Subroutine _aconvert: called CC=5 SCAR=16.1
143 9 my ($text, $loc_m, $loc_w) = @_;
144  
145 9 $convert_u2i->($text);
146  
147 9 ▶ if ($loc_w) { # windows cp1252
Branch: 1 branch
BranchTrueFalseTotal
if ($loc_w) 3 6 9
148 3 $convert_c2u->($text);
149 }
150  
151 9 ▶ if ($loc_m == 1) { # iso
Branch: 1 branch
BranchTrueFalseTotal
if ($loc_m == 1) { } 3 6 9
152 3 $text = NFC($text) =~ s{\p{Diacriticals}}''xmsgr;
153  
154 3 ▶ if ($loc_w) { # windows cp1252
Branch: 1 branch
BranchTrueFalseTotal
if ($loc_w) 1 2 3
155 1 $convert_u2c->($text);
156 }
157  
158 3 $text =~ s{([^\x00-\xff])}{NFD($1)}xmsge;
159  
160 3 $text =~ s{\p{Diacriticals}}''xmsg;
161  
162 3 $text = encode('iso-8859-1', $text);
163 }
164 else { # pure or brutal
165 6 $convert_i2a->($text);
166 6 ▶ $convert_u2a->($text) if $loc_m == 2; # brutal
Branch: 1 branch
BranchTrueFalseTotal
if $loc_m == 2 3 3 6
167  
168 6 $text = encode('iso-8859-1', NFD($text) =~ s{\p{Diacriticals}}''xmsgr);
169 6 $text =~ s{\P{ASCII}}'?'xmsg;
170 }
171  
172 9 return $text;
173 }
174  
175 1 ▶sub cv_from_win {
Subroutine cv_from_win: called CC=1 SCAR=0.0
Pod undocumented
176 1 my ($buf) = @_;
177  
178 1 $convert_c2u->($buf);
179  
180 1 return $buf;
181 }
182  
183 1 ▶sub cv_to_win {
Subroutine cv_to_win: called CC=1 SCAR=0.0
Pod undocumented
184 1 my ($buf) = @_;
185  
186 1 $convert_u2c->($buf);
187  
188 1 return $buf;
189 }
190  
191 2 ▶sub commify {
Subroutine commify: called CC=4 SCAR=13.9
Pod undocumented
192 2 local $_ = shift;
193 2 my ($sep) = @_;
194  
195 2 ▶ $sep //= '_';
Condition100%2/2
$sep //= "_"
l!l
11
MC/DC100%1/1
$sep //= "_"
$sep
Truth table100%2/2
A
A= $sep
Aresult
11
00
196  
197 2 my $len = length($_);
198 2 for my $i (1..$len) {
199 6 ▶ last unless s/^([-+]?\d+)(\d{3})/$1$sep$2/;
Branch: 1 branch
BranchTrueFalseTotal
unless s/^([-+]?\d+)(\d{3})/$1$sep$2/u 2 4 6
200 }
201  
202 2 return $_;
203 }
204  
205 1 1;
206  
207 __END__
↑ Summary