blib/lib/Unicode/CharWidth.pm

filter: Statementstmt 63.4% 73 / 115 Branchbran 22.7% 5 / 22 Conditioncond 23.5% 4 / 17 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 75.0% 21 / 28 Podpod n/a totaltotal 53.9% 103 / 191 SCAR 41.9
CC
21
Cov
53%
_effective_locale
20.0
_gen_proptab
12.0
_write_startup
12.0
CRAP 66.1
↑ Summary
1 package Unicode::CharWidth;
2  
3 3 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 Unicode::CharWidth - Character Width properties
10  
11 =head1 VERSION
12  
13 Version 1.04
14  
15 =cut
16  
17 3 our $VERSION = '1.04';
18  
19 # the names of the character classes we'll define
20 # we arrange them so, that in an array of 4 elements the mbwidth value
21 # indexes the corresponding element, -1 being equivalent to 3
22  
23 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 our @CARP_NOT = qw(utf8); # otherwise we see errors from unicode_heavy.pl
25  
26 3 use constant CLASS_NAMES => (
27 'InZerowidth', # mbwidth == 0
28 'InSinglewidth', # mbwidth == 1
29 'InDoublewidth', # mbwidth == 2
30 'InNowidth', # mbwidth == -1
31 3 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 3 ▶use constant WIDTH_VALUES => (0 .. 2, -1); # order corresponds to CLASS_NAMES
Subroutine BEGIN: called CC=1 SCAR=0.0
34 3 ▶use constant STD_QUICKSTART => 'UCW_startup';
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 sub import {
37 2 ▶ my $class = shift;
Subroutine import: called CC=4 SCAR=16.3
38 2 my ($arg) = @_;
39 2 ▶ if ( $arg and $arg eq '-gen' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg and $arg eq "-gen") 0 2 2
Condition33%1/3
$arg and $arg eq "-gen"
!ll&&!rl&&r
200
MC/DC0%0/2
$arg and $arg eq "-gen"
$arg $arg eq "-gen"
Truth table33%1/3
A and B
A= $argB= $arg eq "-gen"
ABresult
0X0
100
111
40 0 _gen_and_save_proptab(_startup_path());
41 0 carp 'Exiting';
42 0 exit 0; # so no useful program runs with this option
43 }
44 2 _compile_functions();
45 2 @_ = ($class);
46 2 require Exporter;
47 2 ▶ goto(Exporter->can('import') or die q(Exporter can't import?));
Condition50%1/2
'Exporter'->can('import') || die(q[Exporter can't import?])
l!l
20
MC/DC0%0/1
'Exporter'->can('import') || die(q[Exporter can't import?])
'Exporter'->can('import')
Truth table50%1/2
A
A= 'Exporter'->can('import')
Aresult
11
00
48 }
49  
50 3 our @EXPORT = CLASS_NAMES;
51  
52 # compile the four exported functions
53 sub _compile_functions {
54 2 ▶ my $tabs = _get_proptab(_startup_path());
Subroutine _compile_functions: called CC=2 SCAR=6.9
55 2 for my $name ( CLASS_NAMES ) {
56 8 my $tab = $tabs->{$name};
57 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
58 8 ▶ *$name = sub { $tab };
Subroutine __ANON__: called CC=1 SCAR=0.0
59 }
60 }
61  
62 3 ▶use Dir::Self;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 3 ▶use File::Spec::Functions ();
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 sub _startup_path {
66 4 ▶ File::Spec::Functions::catfile(
Subroutine _startup_path: called CC=1 SCAR=0.0
67 __DIR__, STD_QUICKSTART()
68 )
69 }
70  
71 sub _get_proptab {
72 2 ▶ my $file = _startup_path();
Subroutine _get_proptab: called CC=2 SCAR=7.2
73 2 ▶ _read_startup($file) || croak(
Branch: 1 branch
BranchTrueFalseTotal
_read_startup($file) or croak("Missing $file in distribution " . 'Unicode::CharWidth') 0 2 2
74 "Missing $file in distribution " . __PACKAGE__
75 )
76 }
77  
78 sub _gen_and_save_proptab {
79 0 ▶ unless ( _effective_locale() =~ /\.UTF-8$/ ) {
Subroutine _gen_and_save_proptab: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless (_effective_locale() =~ /\.UTF-8$/) 0 0 0
80 0 croak "Generation must be under a UTF-8 locale"
81 }
82 0 _write_startup(_gen_proptab(), _startup_path());
83 }
84  
85 sub _effective_locale {
86 0 ▶ $ENV{LC_CTYPE} || $ENV{LANG} || $ENV{LC_ALL} || ''
Subroutine _effective_locale: not called CC=4 SCAR=30.0
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"LC_CTYPE"} or $ENV{"LANG"} or $ENV{"LC_ALL"} or "" 0 0 0
Condition0%0/6
$ENV{"LC_CTYPE"} or $ENV{"LANG"} or $ENV{"LC_ALL"}
l!l&&r!l&&!r
000
$ENV{"LC_CTYPE"} or $ENV{"LANG"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$ENV{"LC_CTYPE"} or $ENV{"LANG"} or $ENV{"LC_ALL"}
$ENV{"LC_CTYPE"} $ENV{"LANG"} $ENV{"LC_ALL"}
Truth table0%0/4
A or B or C
A= $ENV{"LC_CTYPE"}B= $ENV{"LANG"}C= $ENV{"LC_ALL"}
ABCresult
1XX1
01X1
0011
0000
87 }
88  
89 3 ▶use constant MAX_UNICODE => 0x10FFFF;
Subroutine BEGIN: called CC=1 SCAR=0.0
90  
91 sub _gen_proptab {
92 0 ▶ require Text::CharWidth;
Subroutine _gen_proptab: not called CC=3 SCAR=24.8
93 0 my @proptab; # we'll make it a hash later (_reform_proptab)
94 # make room for as many elements as we have class names
95 # so index -1 is index 3 (InNowidth)
96 0 $#proptab = $#{ [CLASS_NAMES] };
97 0 my $last_width = 99; # won't occur
98 0 for my $code ( 0 .. MAX_UNICODE ) {
99 0 my $width = Text::CharWidth::mbwidth(chr $code);
100 0 ▶ if ( $width == $last_width ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($width == $last_width) { } 0 0 0
101 # continue current interval
102 0 $proptab[$width]->[-1]->[1] = $code;
103 } else {
104 # start new interval (pair) for current length
105 0 push @{ $proptab[$width] }, [$code, $code];
106 }
107 0 $last_width = $width;
108 }
109 0 _reform_proptab(@proptab) # make a hash of strings, keyed by class name
110 }
111  
112 sub _reform_proptab {
113 0 ▶ my @proptab = @_;
Subroutine _reform_proptab: not called CC=2 SCAR=17.9
114 0 for my $tab ( @proptab ) {
115 0 $tab = join "\n", map _one_or_two(@$_), @$tab;
116 }
117 0 my %proptab;
118 0 @proptab{CLASS_NAMES()} = @proptab;
119 0 \ %proptab
120 }
121  
122 3 ▶use constant CODEPOINT_FMT => '%04X';
Subroutine BEGIN: called CC=1 SCAR=0.0
123  
124 sub _one_or_two {
125 0 ▶ my ($from, $to) = @_;
Subroutine _one_or_two: not called CC=2 SCAR=17.9
126 0 my $fmt = CODEPOINT_FMT; # print only first element if second is equal
127 0 ▶ $fmt .= " $fmt" if $from != $to; # ... or both elements
Branch: 1 branch
BranchTrueFalseTotal
if $from != $to 0 0 0
128 0 sprintf $fmt, $from, $to
129 }
130  
131 3 ▶use Storable ();
Subroutine BEGIN: called CC=1 SCAR=0.0
132  
133 sub _read_startup {
134 2 ▶ my ($file) = @_;
Subroutine _read_startup: called CC=3 SCAR=11.8
135 2 ▶ my $tab = eval { Storable::retrieve($file) } or croak(
Branch: 1 branch
BranchTrueFalseTotal
my $tab = eval { do { Storable::retrieve($file) } } or croak(_strip_error($@)) 0 2 2
136 _strip_error($@)
137 );
138 2 ▶ unless ( _validate_proptab($tab) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_validate_proptab($tab)) 0 2 2
139 0 croak("File '$file' wasn't created by " . __PACKAGE__);
140 }
141 2 $tab;
142 }
143  
144 sub _write_startup {
145 0 ▶ my ($proptab, $file) = @_;
Subroutine _write_startup: not called CC=3 SCAR=24.8
146 # only write validated $proptab
147 0 ▶ die "Failing our own validation" unless _validate_proptab($proptab);
Branch: 1 branch
BranchTrueFalseTotal
unless _validate_proptab($proptab) 0 0 0
148 0 ▶ if ( eval { Storable::nstore($proptab, $file); 1 } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { Storable::nstore($proptab, $file); 1 } }) { } 0 0 0
149 0 carp "Created startup file $file";
150 } else {
151 # remove file/line from message and re-croak
152 0 croak _strip_error($@);
153 }
154 return # nothing in particular, no-one cares
155 0 }
156  
157 sub _strip_error {
158 0 ▶ my ($error) = @_;
Subroutine _strip_error: not called CC=1 SCAR=6.9
159 0 $error =~ s/at .* line \d+.*//s;
160 0 ucfirst $error
161 }
162  
163 3 $@ =~ s/at .* line \d+.*//s;
164 3 ▶use List::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
165  
166 sub _validate_proptab {
167 2 ▶ my ($tab) = @_;
Subroutine _validate_proptab: called CC=4 SCAR=15.2
168 2 my $ncn = @{ [CLASS_NAMES] }; # number of class names
169 ref $tab eq 'HASH' and
170 8 $ncn == grep { exists $tab->{$_} } CLASS_NAMES and
171 8 $ncn == grep { defined $tab->{$_} } CLASS_NAMES and
172 2 ▶ $ncn == grep { $tab->{$_} =~ /^[[:xdigit:]\s]*$/ } CLASS_NAMES
Branch: 1 branch
BranchTrueFalseTotal
ref $tab eq "HASH" and $ncn == grep({exists $tab->{$_};} ("InZerowidth", "InSinglewidth", "InDoublewidth", "InNowidth")) and $ncn == grep({defined $tab->{$_};} ("InZerowidth", "InSinglewidth", "InDoublewidth", "InNowidth")) and $ncn == grep({$tab->{$_} =~ /^[[:xdigit:]\s]*$/;} ("InZerowidth", "InSinglewidth", "InDoublewidth", "InNowidth")) 2 0 2
Condition33%2/6
ref $tab eq "HASH" and $ncn == grep({exists $tab->{$_};} ("InZerowidth", "InSinglewidth", "InDoublewidth", "InNowidth")) and $ncn == grep({defined $tab->{$_};} ("InZerowidth", "InSinglewidth", "InDoublewidth", "InNowidth"))
!ll&&!rl&&r
002
ref $tab eq "HASH" and $ncn == grep({exists $tab->{$_};} ("InZerowidth", "InSinglewidth", "InDoublewidth", "InNowidth"))
!ll&&!rl&&r
002
MC/DC0%0/3
ref $tab eq "HASH" and $ncn == grep({exists $tab->{$_};} ("InZerowidth", "InSinglewidth", "InDoublewidth", "InNowidth")) and $ncn == grep({defined $tab->{$_};} ("InZerowidth", "InSinglewidth", "InDoublewidth", "InNowidth"))
ref $tab eq "HASH" $ncn == grep({exists $tab->{$_};} ("InZerowidth", "InSinglewidth", "InDoublewidth", "InNowidth")) $ncn == grep({defined $tab->{$_};} ("InZerowidth", "InSinglewidth", "InDoublewidth", "InNowidth"))
Truth table25%1/4
A and B and C
A= ref $tab eq "HASH"B= $ncn == grep({exists $tab->{$_};} ("InZerowidth", "InSinglewidth", "InDoublewidth", "InNowidth"))C= $ncn == grep({defined $tab->{$_};} ("InZerowidth", "InSinglewidth", "InDoublewidth", "InNowidth"))
ABCresult
0XX0
10X0
1100
1111
173 }
174  
175 3 __PACKAGE__
176 __END__
↑ Summary