line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Text::Fy::Utils; |
2
|
|
|
|
|
|
|
$Text::Fy::Utils::VERSION = '0.09'; |
3
|
1
|
|
|
1
|
|
679
|
use 5.020; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
30
|
|
4
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
29
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
11
|
use Carp; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
63
|
|
7
|
1
|
|
|
1
|
|
3214
|
use Unicode::Normalize; |
|
1
|
|
|
|
|
3141
|
|
|
1
|
|
|
|
|
88
|
|
8
|
1
|
|
|
1
|
|
695
|
use Encode qw(encode decode); |
|
1
|
|
|
|
|
9408
|
|
|
1
|
|
|
|
|
775
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
require Exporter; |
11
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
12
|
|
|
|
|
|
|
our @EXPORT = qw(); |
13
|
|
|
|
|
|
|
our @EXPORT_OK = qw(asciify isoify simplify commify cv_to_win cv_from_win); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
my %cp1252_to_uni; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
for (128..159) { |
18
|
|
|
|
|
|
|
$cp1252_to_uni{chr($_)} = decode('cp1252', chr($_)); |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
my %uni_to_ascii = ( |
22
|
|
|
|
|
|
|
"\x{20ac}" => q{E}, |
23
|
|
|
|
|
|
|
"\x{201a}" => q{,}, |
24
|
|
|
|
|
|
|
"\x{0192}" => q{f}, |
25
|
|
|
|
|
|
|
"\x{201e}" => q{"}, |
26
|
|
|
|
|
|
|
"\x{2026}" => q{_}, |
27
|
|
|
|
|
|
|
"\x{2020}" => q{+}, |
28
|
|
|
|
|
|
|
"\x{02c6}" => q{^}, |
29
|
|
|
|
|
|
|
"\x{2030}" => q{%}, |
30
|
|
|
|
|
|
|
# "\x{0160}" => q{S}, |
31
|
|
|
|
|
|
|
"\x{2039}" => q{<}, |
32
|
|
|
|
|
|
|
"\x{0152}" => q{O}, |
33
|
|
|
|
|
|
|
# "\x{017d}" => q{Z}, |
34
|
|
|
|
|
|
|
"\x{2018}" => q{'}, |
35
|
|
|
|
|
|
|
"\x{2019}" => q{'}, |
36
|
|
|
|
|
|
|
"\x{201c}" => q{"}, |
37
|
|
|
|
|
|
|
"\x{201d}" => q{"}, |
38
|
|
|
|
|
|
|
"\x{2022}" => q{.}, |
39
|
|
|
|
|
|
|
"\x{2013}" => q{-}, |
40
|
|
|
|
|
|
|
"\x{2014}" => q{-}, |
41
|
|
|
|
|
|
|
"\x{20dc}" => q{~}, |
42
|
|
|
|
|
|
|
# "\x{0161}" => q{s}, |
43
|
|
|
|
|
|
|
"\x{203a}" => q{>}, |
44
|
|
|
|
|
|
|
"\x{203a}" => q{>}, |
45
|
|
|
|
|
|
|
"\x{0153}" => q{o}, |
46
|
|
|
|
|
|
|
# "\x{017e}" => q{z}, |
47
|
|
|
|
|
|
|
# "\x{017e}" => q{Y}, |
48
|
|
|
|
|
|
|
"\x{00a1}" => q{!}, |
49
|
|
|
|
|
|
|
"\x{00a2}" => q{c}, |
50
|
|
|
|
|
|
|
"\x{00a3}" => q{L}, |
51
|
|
|
|
|
|
|
"\x{00a5}" => q{Y}, |
52
|
|
|
|
|
|
|
"\x{00a6}" => q{|}, |
53
|
|
|
|
|
|
|
"\x{00a9}" => q{C}, |
54
|
|
|
|
|
|
|
"\x{00aa}" => q{a}, |
55
|
|
|
|
|
|
|
"\x{00ab}" => q{"}, |
56
|
|
|
|
|
|
|
"\x{00ac}" => q{-}, |
57
|
|
|
|
|
|
|
"\x{00ad}" => q{-}, |
58
|
|
|
|
|
|
|
"\x{00ae}" => q{R}, |
59
|
|
|
|
|
|
|
"\x{00b2}" => q{2}, |
60
|
|
|
|
|
|
|
"\x{00b3}" => q{3}, |
61
|
|
|
|
|
|
|
"\x{00b4}" => q{'}, |
62
|
|
|
|
|
|
|
"\x{00b7}" => q{.}, |
63
|
|
|
|
|
|
|
"\x{00b9}" => q{1}, |
64
|
|
|
|
|
|
|
"\x{00ba}" => q{0}, |
65
|
|
|
|
|
|
|
"\x{00bb}" => q{"}, |
66
|
|
|
|
|
|
|
"\x{00bf}" => q{?}, |
67
|
|
|
|
|
|
|
"\x{00c6}" => q{A}, |
68
|
|
|
|
|
|
|
"\x{00d7}" => q{x}, |
69
|
|
|
|
|
|
|
"\x{00d8}" => q{O}, |
70
|
|
|
|
|
|
|
"\x{00df}" => q{s}, |
71
|
|
|
|
|
|
|
"\x{00e6}" => q{a}, |
72
|
|
|
|
|
|
|
"\x{00f0}" => q{d}, |
73
|
|
|
|
|
|
|
"\x{00f8}" => q{o}, |
74
|
|
|
|
|
|
|
); |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
my %uni_to_iso = ( |
77
|
|
|
|
|
|
|
"\x{2013}" => q{-}, |
78
|
|
|
|
|
|
|
"\x{2014}" => q{-}, |
79
|
|
|
|
|
|
|
"\x{2018}" => q{'}, |
80
|
|
|
|
|
|
|
"\x{2019}" => q{'}, |
81
|
|
|
|
|
|
|
"\x{201c}" => q{"}, |
82
|
|
|
|
|
|
|
"\x{201d}" => q{"}, |
83
|
|
|
|
|
|
|
"\x{2026}" => q{_}, |
84
|
|
|
|
|
|
|
); |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
my $convert_c2u = _make_tr(\%cp1252_to_uni); |
87
|
|
|
|
|
|
|
my $convert_u2c = _make_tr(\%cp1252_to_uni, 'R'); |
88
|
|
|
|
|
|
|
my $convert_u2a = _make_tr(\%uni_to_ascii); |
89
|
|
|
|
|
|
|
my $convert_u2i = _make_tr(\%uni_to_iso); |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub _make_tr { |
92
|
4
|
|
|
4
|
|
7
|
my ($href, $rev) = @_; |
93
|
|
|
|
|
|
|
|
94
|
4
|
|
|
|
|
44
|
my $from = join '', map { sprintf '\x{%04x}', ord($_) } sort keys %$href; |
|
117
|
|
|
|
|
187
|
|
95
|
4
|
|
|
|
|
48
|
my $to = join '', map { sprintf '\x{%04x}', ord($href->{$_}) } sort keys %$href; |
|
117
|
|
|
|
|
205
|
|
96
|
|
|
|
|
|
|
|
97
|
4
|
100
|
|
|
|
33
|
my $code = 'sub { $_[0] =~ '.($rev ? "tr/$to/$from/" : "tr/$from/$to/").'; }'; |
98
|
|
|
|
|
|
|
|
99
|
4
|
50
|
|
|
|
244
|
eval $code or die "Can't compile >$code< because $@"; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub asciify { |
103
|
1
|
|
|
1
|
0
|
21
|
_aconvert($_[0], 0, 0); |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub isoify { |
107
|
1
|
|
|
1
|
0
|
4
|
_aconvert($_[0], 1, 0); |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
sub simplify { |
111
|
1
|
|
|
1
|
0
|
5
|
_aconvert($_[0], 2, 0); |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
sub _aconvert { |
115
|
9
|
|
|
9
|
|
446
|
my ($text, $loc_m, $loc_w) = @_; |
116
|
|
|
|
|
|
|
|
117
|
9
|
|
|
|
|
210
|
$convert_u2i->($text); |
118
|
|
|
|
|
|
|
|
119
|
9
|
100
|
|
|
|
20
|
if ($loc_w) { # windows cp1252 |
120
|
3
|
|
|
|
|
56
|
$convert_c2u->($text); |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
9
|
100
|
|
|
|
19
|
if ($loc_m == 1) { # iso |
124
|
1
|
|
|
1
|
|
651
|
$text = NFC($text) =~ s{\p{Diacriticals}}''xmsgr; |
|
1
|
|
|
|
|
8
|
|
|
1
|
|
|
|
|
11
|
|
|
3
|
|
|
|
|
64
|
|
125
|
|
|
|
|
|
|
|
126
|
3
|
100
|
|
|
|
9
|
if ($loc_w) { # windows cp1252 |
127
|
1
|
|
|
|
|
20
|
$convert_u2c->($text); |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
3
|
|
|
|
|
16
|
$text =~ s{([^\x00-\xff])}{NFD($1)}xmsge; |
|
29
|
|
|
|
|
90
|
|
131
|
|
|
|
|
|
|
|
132
|
3
|
|
|
|
|
32
|
$text =~ s{\p{Diacriticals}}''xmsg; |
133
|
|
|
|
|
|
|
|
134
|
3
|
|
|
|
|
11
|
$text = encode('iso-8859-1', $text); |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
else { # pure or brutal |
137
|
6
|
100
|
|
|
|
63
|
$convert_u2a->($text) if $loc_m == 2; # brutal |
138
|
|
|
|
|
|
|
|
139
|
6
|
|
|
|
|
142
|
$text = encode('iso-8859-1', NFD($text) =~ s{\p{Diacriticals}}''xmsgr); |
140
|
6
|
|
|
|
|
155
|
$text =~ s{\P{ASCII}}'?'xmsg; |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
9
|
|
|
|
|
108
|
return $text; |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
sub cv_from_win { |
147
|
1
|
|
|
1
|
0
|
2
|
my ($buf) = @_; |
148
|
|
|
|
|
|
|
|
149
|
1
|
|
|
|
|
23
|
$convert_c2u->($buf); |
150
|
|
|
|
|
|
|
|
151
|
1
|
|
|
|
|
4
|
return $buf; |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
sub cv_to_win { |
155
|
1
|
|
|
1
|
0
|
3
|
my ($buf) = @_; |
156
|
|
|
|
|
|
|
|
157
|
1
|
|
|
|
|
28
|
$convert_u2c->($buf); |
158
|
|
|
|
|
|
|
|
159
|
1
|
|
|
|
|
4
|
return $buf; |
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
sub commify { |
163
|
2
|
|
|
2
|
0
|
4
|
local $_ = shift; |
164
|
2
|
|
|
|
|
2
|
my ($sep) = @_; |
165
|
|
|
|
|
|
|
|
166
|
2
|
|
100
|
|
|
8
|
$sep //= '_'; |
167
|
|
|
|
|
|
|
|
168
|
2
|
|
|
|
|
3
|
my $len = length($_); |
169
|
2
|
|
|
|
|
5
|
for my $i (1..$len) { |
170
|
6
|
100
|
|
|
|
32
|
last unless s/^([-+]?\d+)(\d{3})/$1$sep$2/; |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
2
|
|
|
|
|
8
|
return $_; |
174
|
|
|
|
|
|
|
} |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
1; |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
__END__ |