Block/Item.pm

filter: Statementstmt 97.1% 68 / 70 Branchbran 91.6% 22 / 24 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 14 / 14 Podpod 100% 6 / 6 totaltotal 96.6% 115 / 119 CC 14 SCAR 12.0
CC
14
Cov
96%
char
6.0
_is_hex
5.2
new
3.0
CRAP 3.3
↑ Summary Block.pm »
1 package Unicode::Block::Item;
2  
3 # Pragmas.
4 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # Modules.
8 14 ▶use Class::Utils qw(set_params);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 14 ▶use Error::Pure qw(err);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 14 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 14 ▶use Text::CharWidth qw(mbwidth);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 14 ▶use Unicode::Char;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # Constants.
15 14 Readonly::Scalar our $SPACE => q{ };
16  
17 # Version.
18 14 our $VERSION = 0.03;
19  
20 # Constructor.
21 sub new {
22 23 ▶ my ($class, @params) = @_;
Subroutine new: called CC=3 SCAR=11.0
Pod documented
23  
24 # Create object.
25 23 my $self = bless {}, $class;
26  
27 # Hexadecimal number.
28 23 $self->{'hex'} = undef;
29  
30 # Length of hex number.
31 23 $self->{'hex_length'} = 4;
32  
33 # Process parameters.
34 23 set_params($self, @params);
35  
36 # Check hex number.
37 21 ▶ if (! defined $self->{'hex'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"hex"}) 1 20 21
38 1 err "Parameter 'hex' is required.";
39 }
40 20 ▶ if (! $self->_is_hex) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_hex) 1 19 20
41 1 err "Parameter 'hex' isn't hexadecimal number.";
42 }
43  
44 # Object.
45 19 return $self;
46 }
47  
48 # Get hex base number.
49 sub base {
50 1 ▶ my $self = shift;
Subroutine base: called CC=1 SCAR=0.0
Pod documented
51 1 my $base = substr $self->hex, 0, -1;
52 1 return 'U+'.$base.'x';
53 }
54  
55 # Get character.
56 sub char {
57 12 ▶ my $self = shift;
Subroutine char: called CC=6 SCAR=17.9
Pod documented
58  
59 # Create object.
60 12 ▶ if (! exists $self->{'u'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"u"}) 11 1 12
61 11 $self->{'u'} = Unicode::Char->new;
62 }
63  
64 # Get char.
65 12 my $char = $self->{'u'}->u($self->{'hex'});
66  
67 # 'Non-Spacing Mark' and 'Enclosing Mark'.
68 12 ▶ if ($char =~ m/\p{Mn}/ms || $char =~ m/\p{Me}/ms) {
Branch: 3 branches
BranchTrueFalseTotal
if ($char =~ /\p{Mn}/ms or $char =~ /\p{Me}/ms) { } 4 8 12
elsif ($char =~ /\p{Cc}/ms) { } 1 7 8
elsif ($char =~ /\p{Cn}/ms) { } 2 5 7
Condition100%3/3
$char =~ /\p{Mn}/ms or $char =~ /\p{Me}/ms
l!l&&r!l&&!r
228
MC/DC100%2/2
$char =~ /\p{Mn}/ms or $char =~ /\p{Me}/ms
$char =~ /\p{Mn}/ms $char =~ /\p{Me}/ms
Truth table100%3/3
A or B
A= $char =~ /\p{Mn}/msB= $char =~ /\p{Me}/ms
ABresult
1X1
011
000
69 4 $char = $SPACE.$char;
70  
71 # Control.
72 } elsif ($char =~ m/\p{Cc}/ms) {
73 1 $char = $SPACE;
74  
75 # Not Assigned.
76 } elsif ($char =~ m/\p{Cn}/ms) {
77 2 $char = $SPACE;
78 }
79  
80 12 return $char;
81 }
82  
83 # Get hex number.
84 sub hex {
85 4 ▶ my $self = shift;
Subroutine hex: called CC=1 SCAR=0.0
Pod documented
86 return sprintf '%0'.$self->{'hex_length'}.'x',
87 4 CORE::hex $self->{'hex'};
88 }
89  
90 # Get last hex number.
91 sub last_hex {
92 1 ▶ my $self = shift;
Subroutine last_hex: called CC=1 SCAR=0.0
Pod documented
93 1 return substr $self->{'hex'}, -1;
94 }
95  
96 # Get character width.
97 sub width {
98 7 ▶ my $self = shift;
Subroutine width: called CC=3 SCAR=11.0
Pod documented
99 7 ▶ if (! exists $self->{'_width'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_width"}) 6 1 7
100 6 $self->{'_width'} = mbwidth($self->char);
101 6 ▶ if ($self->{'_width'} == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_width"} == -1) 2 4 6
102 2 $self->{'_width'} = 1;
103 }
104 }
105 7 return $self->{'_width'};
106 }
107  
108 # Check for hex number.
109 sub _is_hex {
110 20 ▶ my $self = shift;
Subroutine _is_hex: called CC=5 SCAR=16.4
111 20 ▶ if ($self->{'hex'} !~ m/^[0-9a-fA-F]+$/ims) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"hex"} =~ /^[0-9a-fA-F]+$/ism) 1 19 20
112 1 return 0;
113 }
114 19 my $int = CORE::hex $self->{'hex'};
115 19 ▶ if (! defined $int) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $int) 0 19 19
116 0 return 0;
117 }
118 19 my $hex = sprintf '%x', $int;
119 19 my $value = lc $self->{'hex'};
120 19 $value =~ s/^0*//ms;
121 19 ▶ if ($value eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq "") 2 17 19
122 2 $value = 0;
123 }
124 19 ▶ if ($hex ne $value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hex ne $value) 0 19 19
125 0 return 0;
126 }
127 19 return 1;
128 }
129  
130 14 1;
131  
132 __END__
↑ Summary Block.pm »