lib/In/Korean/Numbers/SinoKorean.pm

filter: Statementstmt 100% 68 / 68 Branchbran 88.2% 30 / 34 Conditioncond 100% 9 / 9 MC/DCmcdc 100% 5 / 5 Subroutinesub 100% 10 / 10 Podpod 42.8% 3 / 7 totaltotal 93.9% 125 / 133 CC 23 SCAR 19.6
CC
23
Cov
96%
getHangul
11.0
getInt
9.0
get_args
3.0
CRAP 7.1
↑ Summary
1 package In::Korean::Numbers::SinoKorean;
2  
3 1 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '0.04'; # Also update POD version below
8  
9 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
10  
11 # Map Hangul to integer
12 1 my %int_to_char_map = (
13 0 => "\x{C601}",
14 1 => "\x{C77C}",
15 2 => "\x{C774}",
16 3 => "\x{C0BC}",
17 4 => "\x{C0AC}",
18 5 => "\x{C624}",
19 6 => "\x{C721}",
20 7 => "\x{CE60}",
21 8 => "\x{D314}",
22 9 => "\x{AD6C}",
23 10 => "\x{C2ED}",
24 100 => "\x{BC31}",
25 1000 =>"\x{CC9C}",
26 10000 => "\x{B9CC}",
27 );
28  
29 1 my %char_to_int_map; # Lazily created from %int_to_char_map
30  
31 # All numbers are expressed as a combination of the following units
32 1 my @units = (10000, 1000, 100, 10, 1);
33  
34 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
35 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
36 1 my $class = shift;
37 1 return bless {}, $class;
38 }
39  
40 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
41 114 ▶sub getHangul {
Subroutine getHangul: called CC=11 SCAR=24.0
Pod documented
42  
43 114 my( $self, $num ) = get_args( @_ );
44  
45 114 ▶ return undef if not defined $num;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $num 0 114 114
46  
47 # Must be positive integer
48 114 ▶ return undef if not is_positive_int_or_zero( $num );
Branch: 1 branch
BranchTrueFalseTotal
unless is_positive_int_or_zero($num) 3 111 114
49
50 111 my @hangul = ();
51  
52 111 my $remaining = $num;
53  
54 111 foreach my $unit ( @units ) {
55  
56 525 ▶ last if $remaining == 0; # Performance reasons only
Branch: 1 branch
BranchTrueFalseTotal
if $remaining == 0 34 491 525
57  
58 # Find the mutiple for the current unit.
59 # E.g., 502,217 for key=10,000, then multiple = 52
60 491 my $multiple = floor( $remaining / $unit );
61  
62 491 ▶ next if $multiple < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $multiple < 1 308 183 491
63  
64 183 $remaining %= $unit;
65  
66 # Recursively call to get value greater than 10
67 183 ▶ my $multiple_str = $multiple >= 10 ? getHangul( $self, $multiple ) : int_to_char( $multiple );
Branch: 1 branch
BranchTrueFalseTotal
$multiple >= 10 ? : 6 177 183
68  
69 # Don't push Hangul value for 1 unless currently handling 1 unit
70 183 ▶ push @hangul, $multiple_str unless $multiple == 1 && $unit != 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $multiple == 1 and $unit != 1 147 36 183
Condition100%3/3
$multiple == 1 and $unit != 1
!ll&&!rl&&r
1222536
MC/DC100%2/2
$multiple == 1 and $unit != 1
$multiple == 1 $unit != 1
Truth table100%3/3
A and B
A= $multiple == 1B= $unit != 1
ABresult
0X0
100
111
71 183 ▶ push @hangul, int_to_char( $unit ) if $unit != 1;
Branch: 1 branch
BranchTrueFalseTotal
if $unit != 1 106 77 183
72 }
73  
74 111 ▶ return join( '', @hangul ) if @hangul;
Branch: 1 branch
BranchTrueFalseTotal
if @hangul 109 2 111
75  
76 2 return "\x{C601}";
77 }
78  
79 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
80 104 ▶sub getInt {
Subroutine getInt: called CC=9 SCAR=22.0
Pod documented
81  
82 104 my( $self, $hangul ) = get_args( @_ );
83  
84 104 ▶ return undef if not defined $hangul;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hangul 0 104 104
85  
86 # Tokenize so process each character separately
87 104 my @tokens = split( //, $hangul );
88
89 104 my $total = 0;
90
91 104 ▶ while( @tokens ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tokens) 184 104 288
92 184 my $char_char = shift @tokens;
93 184 my $char_int = char_to_int( $char_char );
94  
95 # If invalid input, return undef
96 184 ▶ return undef if ! defined( $char_int );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char_int 0 184 184
97
98 # If 만, multiply everything by 10,000
99 184 ▶ if ( $char_char eq "\x{B9CC}") {
Branch: 2 branches
BranchTrueFalseTotal
if ($char_char eq "\x{b9cc}") { } 8 176 184
elsif ($char_int > 9 or not @tokens or $tokens[0] eq "\x{b9cc}") { } 112 64 176
100 8 ▶ if ( $total ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($total) { } 6 2 8
101 6 $total *= 10000;
102 } else {
103 2 $total = 10000;
104 }
105 8 next;
106 }
107  
108 # If:
109 # (1) char value is greater than 9,
110 # (2) no more characters left
111 # (3) next character is 만
112 # Then add value
113 ▶ elsif ( $char_int > 9 || ! @tokens || $tokens[0] eq "\x{B9CC}" ) {
Condition100%6/6
$char_int > 9 or not @tokens or $tokens[0] eq "\x{b9cc}"
l!l&&r!l&&!r
110264
$char_int > 9 or not @tokens
l!l&&r!l&&!r
347666
MC/DC100%3/3
$char_int > 9 or not @tokens or $tokens[0] eq "\x{b9cc}"
$char_int > 9 not @tokens $tokens[0] eq "\x{b9cc}"
Truth table100%4/4
A or not(B) or C
A= $char_int > 9B= not @tokensC= $tokens[0] eq "\x{b9cc}"
ABCresult
1XX1
01X1
0011
0000
114 112 $total += $char_int;
115 }
116  
117 # If char 0-9 and not final, get next char (units)
118 else {
119 64 my $unit_char = shift @tokens;
120 64 my $unit_int = char_to_int( $unit_char );
121 64 $total += ( $char_int * $unit_int );
122 }
123 }
124  
125 104 return $total;
126 }
127  
128 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
129 # Returns $self and $val values from arguments. Handles
130 # presence of $self (if o-o) and absence of $self (if
131 # procedural).
132 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
133 218 ▶sub get_args {
Subroutine get_args: called CC=3 SCAR=11.0
Pod undocumented
134 218 my( $self, $val );
135  
136 218 ▶ if ( @_ >= 2 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ >= 2) { } 113 105 218
elsif (@_ == 1) { } 105 0 105
137 113 ( $self, $val ) = @_;
138 } elsif ( @_ == 1 ) {
139 105 ( $val ) = @_;
140 }
141
142 218 return ( $self, $val );
143 }
144  
145 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
146 # Converts integer (e.g., 1) to Hangul block (e.g., 일) using
147 # %int_to_char_map.
148 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
149 283 ▶sub int_to_char {
Subroutine int_to_char: called CC=1 SCAR=0.0
Pod undocumented
150 283 my $int = shift;
151 283 return $int_to_char_map{ $int };
152 }
153  
154 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
155 # Converts hangul block (e.g., 일) to integer (e.g., 1) using
156 # %char_to_int_map.
157 #
158 # Note that %char_to_int_map is lazily created from
159 # %int_to_char_map.
160 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
161 248 ▶sub char_to_int {
Subroutine char_to_int: called CC=3 SCAR=11.0
Pod undocumented
162 248 my $char = shift;
163  
164 248 ▶ if (! %char_to_int_map ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%char_to_int_map) 1 247 248
165 1 %char_to_int_map = ();
166  
167 1 for my $int ( keys %int_to_char_map ) {
168 14 my $hangul = $int_to_char_map{ $int };
169 14 $char_to_int_map{ $hangul } = $int; # Inverse of int_to_char_map
170 }
171 }
172  
173 248 my $int = $char_to_int_map{ $char };
174  
175 248 return $int;
176 }
177  
178 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
179 # Returns true if value is a positive integer or zero.
180 #
181 # Source: http://www.perlmonks.org/?node_id=614452
182 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
183 114 ▶sub is_positive_int_or_zero {
Subroutine is_positive_int_or_zero: called CC=1 SCAR=0.0
Pod undocumented
184 114 my $val = shift;
185 114 $val =~ s/^\s+//; # leading whitespace
186 114 $val =~ s/\s+$//; # trailing whitespace
187 114 return $val =~ /^[+]?\d+$/;
188 }
189  
190 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
191 1 1;
192 __END__
↑ Summary