lib/IOas/CP932X.pm

filter: Statementstmt 93.5% 73 / 78 Branchbran 79.4% 27 / 34 Conditioncond 66.6% 8 / 12 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 100% 24 / 24 Podpod 0.0% 0 / 15 totaltotal 79.5% 136 / 171 CC 22 SCAR 12.3
CC
22
Cov
87%
import
9.8
getc
6.1
printf
4.1
CRAP 3.4
↑ Summary
1 package IOas::CP932X;
2 ######################################################################
3 #
4 # IOas::CP932X - provides CP932X I/O subroutines for UTF-8 script
5 #
6 # http://search.cpan.org/dist/IOas-CP932X/
7 #
8 # Copyright (c) 2019 INABA Hitoshi <ina@cpan.org> in a CPAN
9 ######################################################################
10  
11 12 ▶use 5.00503; # Universal Consensus 1998 for primetools
Subroutine BEGIN: called CC=1 SCAR=0.0
12 # use 5.008001; # Lancaster Consensus 2013 for toolchains
13  
14 12 $VERSION = '0.10';
15 12 $VERSION = $VERSION;
16  
17 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 12 ▶BEGIN { $INC{'warnings.pm'} = '' if $] < 5.006 }; use warnings; $^W=1;
Subroutine BEGIN: called CC=2 SCAR=7.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if $] < "5.006" 0 12 12
19 12 ▶use Symbol ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20 12 ▶use Jacode4e::RoundTrip; # pmake.bat makes META.yml, META.json and Makefile.PL by /^use /
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 #-----------------------------------------------------------------------------
23 # import
24 #-----------------------------------------------------------------------------
25  
26 12 ▶sub import {
Subroutine import: called CC=4 SCAR=22.9
27 12 my $self = shift @_;
28 12 ▶ if (defined($_[0]) and ($_[0] =~ /\A[0123456789]/)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0] and $_[0] =~ /\A[0123456789]/) 0 12 12
Condition33%1/3
defined $_[0] and $_[0] =~ /\A[0123456789]/
!ll&&!rl&&r
1200
MC/DC0%0/2
defined $_[0] and $_[0] =~ /\A[0123456789]/
defined $_[0] $_[0] =~ /\A[0123456789]/
Truth table33%1/3
A and B
A= defined $_[0]B= $_[0] =~ /\A[0123456789]/
ABresult
0X0
100
111
29 0 ▶ if ($_[0] != $IOas::CP932X::VERSION) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] != $IOas::CP932X::VERSION) 0 0 0
30 0 my($package,$filename,$line) = caller;
31 0 die "$filename requires @{[__PACKAGE__]} $_[0], this is version $IOas::CP932X::VERSION, stopped at $filename line $line.\n";
32 }
33 0 shift @_;
34 }
35 }
36  
37 #-----------------------------------------------------------------------------
38 # autodetect I/O encoding from package name
39 #-----------------------------------------------------------------------------
40  
41 12 (my $__package__ = __PACKAGE__) =~ s/utf81/utf8.1/i;
42 12 my $io_encoding = lc((split /::/, $__package__)[-1]);
43  
44 58 ▶sub _io_input ($) {
Subroutine _io_input: called CC=2 SCAR=6.9
45 58 my($s) = @_;
46 58 ▶ if (defined $s) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $s) 56 2 58
47 56 Jacode4e::RoundTrip::convert(\$s, 'utf8.1', $io_encoding);
48 }
49 58 return $s;
50 };
51  
52 262 ▶sub _io_output ($) {
Subroutine _io_output: called CC=1 SCAR=0.0
53 262 my($s) = @_;
54 262 Jacode4e::RoundTrip::convert(\$s, $io_encoding, 'utf8.1', {
55 'OVERRIDE_MAPPING' => {
56  
57 # UTF-8.0 was popular in old days
58 "\xE2\x80\x95" => "\x81\x5C", # U+2015 HORIZONTAL BAR
59 "\xE2\x88\xA5" => "\x81\x61", # U+2225 PARALLEL TO
60 "\xEF\xBC\x8D" => "\x81\x7C", # U+FF0D FULLWIDTH HYPHEN-MINUS
61  
62 # UTF-8.1 will be popular in someday
63 "\xE2\x80\x94" => "\x81\x5C", # U+2014 EM DASH
64 "\xE2\x80\x96" => "\x81\x61", # U+2016 DOUBLE VERTICAL LINE
65 "\xE2\x88\x92" => "\x81\x7C", # U+2212 MINUS SIGN
66 },
67 });
68 262 return $s;
69 };
70  
71 #-----------------------------------------------------------------------------
72 # Octet Length as I/O Encoding
73 #-----------------------------------------------------------------------------
74  
75 18 ▶sub length (;$) {
Subroutine length: called CC=2 SCAR=6.9
Pod undocumented
76 18 ▶ return CORE::length _io_output(@_ ? $_[0] : $_);
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 9 9 18
77 }
78  
79 10 ▶sub sprintf ($@) {
Subroutine sprintf: called CC=1 SCAR=0.0
Pod undocumented
80 10 my($format, @list) = map { _io_output($_) } @_;
81 10 return _io_input(CORE::sprintf($format, @list));
82 }
83  
84 6 ▶sub substr ($$;$$) {
Subroutine substr: called CC=3 SCAR=11.0
Pod undocumented
85 6 ▶ if (@_ == 4) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 4) { } 2 4 6
elsif (@_ == 3) { } 2 2 4
86 2 my $expr = _io_output($_[0]);
87 2 my $substr = CORE::substr($expr, $_[1], $_[2], _io_output($_[3]));
88 2 $_[0] = _io_input($expr);
89 2 return _io_input($substr);
90 }
91 elsif (@_ == 3) {
92 2 return _io_input(CORE::substr(_io_output($_[0]), $_[1], $_[2]));
93 }
94 else {
95 2 return _io_input(CORE::substr(_io_output($_[0]), $_[1]));
96 }
97 }
98  
99 #-----------------------------------------------------------------------------
100 # String Comparison as I/O Encoding
101 #-----------------------------------------------------------------------------
102  
103 20 ▶sub cmp ($$) { _io_output($_[0]) cmp _io_output($_[1]) }
Subroutine cmp: called CC=1 SCAR=0.0
Pod undocumented
104 10 ▶sub eq ($$) { _io_output($_[0]) eq _io_output($_[1]) } # must not optimize like "$_[0] eq $_[1]"
Subroutine eq: called CC=1 SCAR=0.0
Pod undocumented
105 10 ▶sub ne ($$) { _io_output($_[0]) ne _io_output($_[1]) } # must not optimize like "$_[0] ne $_[1]"
Subroutine ne: called CC=1 SCAR=0.0
Pod undocumented
106 10 ▶sub ge ($$) { _io_output($_[0]) ge _io_output($_[1]) }
Subroutine ge: called CC=1 SCAR=0.0
Pod undocumented
107 10 ▶sub gt ($$) { _io_output($_[0]) gt _io_output($_[1]) }
Subroutine gt: called CC=1 SCAR=0.0
Pod undocumented
108 10 ▶sub le ($$) { _io_output($_[0]) le _io_output($_[1]) }
Subroutine le: called CC=1 SCAR=0.0
Pod undocumented
109 10 ▶sub lt ($$) { _io_output($_[0]) lt _io_output($_[1]) }
Subroutine lt: called CC=1 SCAR=0.0
Pod undocumented
110 1 ▶sub sort (@) {
Subroutine sort: called CC=1 SCAR=0.0
Pod undocumented
111 9 map { $_->[0] }
112 21 CORE::sort { $a->[1] cmp $b->[1] }
113 1 map { [ $_, _io_output($_) ] }
114 @_;
115 }
116  
117 #-----------------------------------------------------------------------------
118 # Encoding Convert on I/O Operations
119 #-----------------------------------------------------------------------------
120  
121 4 ▶sub getc (;*) {
Subroutine getc: called CC=6 SCAR=18.1
Pod undocumented
122 4 ▶ my $fh = @_ ? Symbol::qualify_to_ref($_[0],caller()) : \*STDIN;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 2 2 4
123 4 my $octet = CORE::getc($fh);
124 4 ▶ if ($io_encoding =~ /^(?:cp932x|cp932|cp932ibm|cp932nec|sjis2004)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($io_encoding =~ /^(?:cp932x|cp932|cp932ibm|cp932nec|sjis2004)$/) 4 0 4
125 4 ▶ if ($octet =~ /\A[\x81-\x9F\xE0-\xFC]\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($octet =~ /\A[\x81-\x9F\xE0-\xFC]\z/) 4 0 4
126 4 $octet .= CORE::getc($fh);
127  
128 # ('cp932'.'x') to escape from build system
129 4 ▶ if (($io_encoding eq ('cp932'.'x')) and ($octet eq "\x9C\x5A")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($io_encoding eq "cp932x" and $octet eq "\234Z") 2 2 4
Condition66%2/3
$io_encoding eq "cp932x" and $octet eq "\234Z"
!ll&&!rl&&r
022
MC/DC50%1/2
$io_encoding eq "cp932x" and $octet eq "\234Z"
$io_encoding eq "cp932x" $octet eq "\234Z"
Truth table66%2/3
A and B
A= $io_encoding eq "cp932x"B= $octet eq "\234Z"
ABresult
0X0
100
111
130 2 $octet .= CORE::getc($fh);
131 2 $octet .= CORE::getc($fh);
132 }
133 }
134 }
135 4 return _io_input($octet);
136 }
137  
138 32 ▶sub readline (;*) {
Subroutine readline: called CC=3 SCAR=11.0
Pod undocumented
139 32 ▶ my $fh = @_ ? Symbol::qualify_to_ref($_[0],caller()) : \*ARGV;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 26 6 32
140 32 ▶ return wantarray ? map { _io_input($_) } <$fh> : _io_input(scalar <$fh>);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 30 32
141 }
142  
143 10 ▶sub print (;*@) {
Subroutine print: called CC=4 SCAR=13.9
Pod undocumented
144 10 ▶ my $fh = ((@_ >= 1) and defined(fileno(Symbol::qualify_to_ref($_[0],caller())))) ? Symbol::qualify_to_ref(shift,caller()) : Symbol::qualify_to_ref(select,caller());
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 1 && defined fileno &Symbol::qualify_to_ref($_[0], scalar caller()) ? : 8 2 10
Condition100%3/3
@_ >= 1 and defined fileno &Symbol::qualify_to_ref($_[0], scalar caller())
!ll&&!rl&&r
118
MC/DC100%2/2
@_ >= 1 and defined fileno &Symbol::qualify_to_ref($_[0], scalar caller())
@_ >= 1 defined fileno &Symbol::qualify_to_ref($_[0], scalar caller())
Truth table100%3/3
A and B
A= @_ >= 1B= defined fileno &Symbol::qualify_to_ref($_[0], scalar caller())
ABresult
0X0
100
111
145 10 ▶ return CORE::print {$fh} (map { _io_output($_) } (@_ ? @_ : $_));
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 8 2 10
146 }
147  
148 20 ▶sub printf (;*@) {
Subroutine printf: called CC=4 SCAR=14.0
Pod undocumented
149 20 ▶ my $fh = ((@_ >= 1) and defined(fileno(Symbol::qualify_to_ref($_[0],caller())))) ? Symbol::qualify_to_ref(shift,caller()) : Symbol::qualify_to_ref(select,caller());
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 1 && defined fileno &Symbol::qualify_to_ref($_[0], scalar caller()) ? : 10 10 20
Condition66%2/3
@_ >= 1 and defined fileno &Symbol::qualify_to_ref($_[0], scalar caller())
!ll&&!rl&&r
01010
MC/DC50%1/2
@_ >= 1 and defined fileno &Symbol::qualify_to_ref($_[0], scalar caller())
@_ >= 1 defined fileno &Symbol::qualify_to_ref($_[0], scalar caller())
Truth table66%2/3
A and B
A= @_ >= 1B= defined fileno &Symbol::qualify_to_ref($_[0], scalar caller())
ABresult
0X0
100
111
150 20 ▶ my($format, @list) = map { _io_output($_) } (@_ ? @_ : $_);
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 20 0 20
151 20 return CORE::printf {$fh} ($format, @list);
152 }
153  
154 12 1;
155  
156 __END__
↑ Summary