Block/Item.pm

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