line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Format::CN; |
2
|
|
|
|
|
|
|
{ |
3
|
|
|
|
|
|
|
$Format::CN::VERSION = '0.01'; |
4
|
|
|
|
|
|
|
} |
5
|
3
|
|
|
3
|
|
45426
|
use strict; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
71
|
|
6
|
2
|
|
|
2
|
|
10
|
use warnings; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
64
|
|
7
|
2
|
|
|
2
|
|
3510
|
use Encode qw/encode decode is_utf8/; |
|
2
|
|
|
|
|
31742
|
|
|
2
|
|
|
|
|
202
|
|
8
|
2
|
|
|
2
|
|
3861
|
use Sub::Exporter -setup => {exports => ['f']}; |
|
2
|
|
|
|
|
32689
|
|
|
2
|
|
|
|
|
21
|
|
9
|
2
|
|
|
2
|
|
674
|
use Carp qw/carp croak/; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
871
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub f { |
12
|
4
|
|
|
4
|
0
|
1570
|
my %args = @_; |
13
|
4
|
|
|
|
|
9
|
my ($output, $perl_internal); |
14
|
0
|
|
|
|
|
0
|
my ($in, $out); |
15
|
4
|
|
100
|
|
|
26
|
my $en = $args{encoding} || 'utf8'; |
16
|
4
|
50
|
|
|
|
15
|
if ($args{output}) { |
17
|
2
|
50
|
|
2
|
|
29
|
open $out, ">:encoding($en)", $args{output} or die $!; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
17
|
|
|
4
|
|
|
|
|
97
|
|
18
|
|
|
|
|
|
|
} else { |
19
|
0
|
|
|
|
|
0
|
$out = *STDOUT; |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
|
22
|
4
|
100
|
|
|
|
3231
|
if ($args{content}) { |
|
|
50
|
|
|
|
|
|
23
|
3
|
50
|
|
|
|
9
|
if (!$args{encoding}) { |
24
|
3
|
50
|
|
|
|
24
|
if (is_utf8($args{encoding})) { |
25
|
0
|
0
|
|
|
|
0
|
open $in, "<", \$args{content} or die $!; |
26
|
|
|
|
|
|
|
} else { |
27
|
1
|
50
|
|
1
|
|
8
|
open $in, "<:encoding(utf8)", \$args{content} or die $!; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
45
|
|
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
} else { |
30
|
0
|
0
|
|
|
|
0
|
open $in, "<:encoding($args{encoding})", \$args{content} |
31
|
|
|
|
|
|
|
or die $!; |
32
|
|
|
|
|
|
|
} |
33
|
3
|
|
|
|
|
8226
|
return _format($in, $out); |
34
|
|
|
|
|
|
|
} elsif ($args{file}) { |
35
|
1
|
|
50
|
|
|
6
|
$args{encoding} ||= 'utf8'; |
36
|
1
|
50
|
|
|
|
32
|
open $in, "<:encoding($args{encoding})", $args{file} or die $!; |
37
|
1
|
|
|
|
|
35
|
return _format($in, $out); |
38
|
|
|
|
|
|
|
} else { |
39
|
0
|
|
|
|
|
0
|
return; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub _format($$) { |
44
|
4
|
|
|
4
|
|
8
|
my ($input_fh, $output_fh) = @_; |
45
|
4
|
|
|
|
|
6
|
my ($n_is_han, $p_is_han, $p_is_pun, $n_is_pun, $n_is_others); |
46
|
4
|
|
|
|
|
64
|
while (my $c = getc($input_fh)) { |
47
|
2
|
100
|
|
2
|
|
2326
|
if ($c =~ /\p{han}/) { |
|
2
|
100
|
|
|
|
23
|
|
|
2
|
100
|
|
|
|
30
|
|
|
65
|
|
|
|
|
323
|
|
48
|
19
|
|
|
|
|
20
|
$n_is_han = 1; |
49
|
19
|
|
|
|
|
22
|
$n_is_pun = 0; |
50
|
19
|
50
|
|
|
|
47
|
$ENV{DEBUG_MATCH} && print "get han $c\n"; |
51
|
|
|
|
|
|
|
} elsif ($c =~ /\p{p}/) { |
52
|
10
|
|
|
|
|
13
|
$n_is_han = 0; |
53
|
10
|
|
|
|
|
12
|
$n_is_pun = 1; |
54
|
10
|
50
|
|
|
|
25
|
$ENV{DEBUG_MATCH} && print "get punctuation $c\n"; |
55
|
|
|
|
|
|
|
} elsif ($c =~ /\s/s) { |
56
|
1
|
|
|
|
|
1
|
$n_is_others = 1; |
57
|
|
|
|
|
|
|
} else { |
58
|
35
|
|
|
|
|
40
|
$n_is_han = 0; |
59
|
35
|
|
|
|
|
34
|
$n_is_pun = 0; |
60
|
35
|
50
|
|
|
|
73
|
$ENV{DEBUG_MATCH} && print "get others" . ord($c) . "\n"; |
61
|
|
|
|
|
|
|
} |
62
|
65
|
100
|
100
|
|
|
453
|
( defined $p_is_han |
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
63
|
|
|
|
|
|
|
&& !$n_is_others |
64
|
|
|
|
|
|
|
&& !$n_is_pun |
65
|
|
|
|
|
|
|
&& ($n_is_han ^ $p_is_han) |
66
|
|
|
|
|
|
|
&& !$p_is_pun) |
67
|
|
|
|
|
|
|
&& print $output_fh " "; |
68
|
|
|
|
|
|
|
|
69
|
65
|
|
|
|
|
102
|
print $output_fh $c; |
70
|
65
|
|
|
|
|
63
|
$p_is_han = $n_is_han; |
71
|
65
|
|
|
|
|
185
|
$p_is_pun = $n_is_pun; |
72
|
|
|
|
|
|
|
} |
73
|
4
|
|
|
|
|
33
|
close $input_fh; |
74
|
4
|
|
|
|
|
126
|
close $output_fh; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
1; |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
__END__ |