lib/Convert/Number/Ethiopic.pm

filter: Statementstmt 83.1% 84 / 101 Branchbran 89.4% 34 / 38 Conditioncond 70.5% 12 / 17 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 3 totaltotal 80.5% 145 / 180 CC 25 SCAR 20.1
CC
25
Cov
83%
_toEthiopic
14.0
_setArgs
5.3
new
3.3
CRAP 7.4
↑ Summary
1 package Convert::Number::Ethiopic;
2  
3 1 ▶use utf8; # can't find a way to conditionally load this with
Subroutine BEGIN: called CC=1 SCAR=0.0
4 # the scope applying throughout
5  
6 BEGIN
7 1 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶ use vars qw($VERSION @ENumbers %ENumbers);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 $VERSION = "0.21";
13  
14 1 require 5.000;
15  
16 1 @ENumbers =(
17 "፩", "፪", "፫", "፬", "፭", "፮", "፯", "፰", "፱",
18 "፲", "፳", "፴", "፵", "፶", "፷", "፸", "፹", "፺",
19 "፻", "፼"
20 );
21 1 %ENumbers =(
22 '፩' => 1,
23 '፪' => 2,
24 '፫' => 3,
25 '፬' => 4,
26 '፭' => 5,
27 '፮' => 6,
28 '፯' => 7,
29 '፰' => 8,
30 '፱' => 9,
31 '፲' => 10,
32 '፳' => 20,
33 '፴' => 30,
34 '፵' => 40,
35 '፶' => 50,
36 '፷' => 60,
37 '፸' => 70,
38 '፹' => 80,
39 '፺' => 90,
40 '፻' => 100,
41 '፼' => 10000
42 );
43  
44 }
45  
46  
47 sub _setArgs
48 190 ▶{
Subroutine _setArgs: called CC=4 SCAR=16.8
49 190 my ($self, $number) = @_;
50  
51 190 ▶ if ( $#_ > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ > 1) 0 190 190
52 0 warn ( "too many arguments." );
53 0 return;
54 }
55 190 ▶ unless ( $number =~ /^\d+$/ || $number =~ /^[፩-፼]+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($number =~ /^\d+$/ or $number =~ /^[\x{1369}-\x{137c}]+$/) 0 190 190
Condition66%2/3
$number =~ /^\d+$/ or $number =~ /^[\x{1369}-\x{137c}]+$/
l!l&&r!l&&!r
95950
MC/DC0%0/2
$number =~ /^\d+$/ or $number =~ /^[\x{1369}-\x{137c}]+$/
$number =~ /^\d+$/ $number =~ /^[\x{1369}-\x{137c}]+$/
Truth table66%2/3
A or B
A= $number =~ /^\d+$/B= $number =~ /^[\x{1369}-\x{137c}]+$/
ABresult
1X1
011
000
56 0 warn ( "'$number' is not a number." );
57 0 return;
58 }
59  
60 190 $self->{number} = $number;
61  
62 190 1;
63 }
64  
65  
66 sub new
67 1 ▶{
Subroutine new: called CC=3 SCAR=12.0
Pod undocumented
68 1 my $class = shift;
69 1 my $self = {};
70  
71  
72 1 my $blessing = bless ( $self, $class );
73  
74 1 $self->{number} = undef;
75  
76 1 ▶ $self->_setArgs ( @_ ) || return if ( @_ );
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 1 1
Condition25%1/4
@_ && ($self->_setArgs(@_) || (return))
!ll
10
$self->_setArgs(@_) or return
l!l
00
MC/DC0%0/2
@_ && ($self->_setArgs(@_) || (return))
@_ $self->_setArgs(@_)
Truth table33%1/3
A && B
A= @_B= $self->_setArgs(@_)
ABresult
0X0
100
111
77  
78 1 $blessing;
79 }
80  
81  
82 sub _fromEthiopic
83 95 ▶{
Subroutine _fromEthiopic: called CC=3 SCAR=12.0
84  
85 #
86 # just return if its a single char
87 #
88 95 ▶ return ( $ENumbers{$_[0]->{number}} ) if ( length($_[0]->{number}) == 1);
Branch: 1 branch
BranchTrueFalseTotal
if length $_[0]{"number"} == 1 9 86 95
89  
90  
91 86 $_ = $_[0]->{number};
92  
93 #
94 # tack on a ፩ to avoid special condition check
95 #
96 86 s/^([፻፼])/፩$1/o;
97 86 s/፼፻/፼፩፻/og;
98  
99 # what we do now is pad 0s around ፻ and ፼, these regexi try to kill
100 # two birds with one stone but could be split and simplified
101  
102 #
103 # pad 0 around ones and tens
104 #
105 86 s/([፻፼])([፩-፱])/$1."0$2"/oge; # add 0 if tens place empty
106 86 s/([፲-፺])([^፩-፱])/$1."0$2"/oge; # add 0 if ones place empty
107 86 ▶ if ( $] >= 5.012 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= 5.012) { } 86 0 86
108 86 s/([፲-፺])$/$1."0"/oe; # repeat at end of string
109  
110 # pad 0s for meto
111 #
112 # s/(፻)$/$1."00"/e; # this is stupid but tricks perl 5.6 into working
113 86 s/፻$/፻00/o;
114  
115 # pad 0s for ilf
116 #
117 86 s/፼$/፼0000/o;
118  
119 # pad 0s for ilf
120 #
121 86 s/፼፼/፼0000፼/og; # since /g doesn't work the first time..
122 86 s/፼፼/፼0000፼/og; # ...we do it again!
123 86 s/፻፼/፼00፼/og;
124 86 s/፼0([፩-፱])፼/፼000$1፼/og;
125  
126 86 s/፼0([፩-፱])$/፼000$1/o; # repeat at end of string
127  
128 86 s/፼([፲-፺]0)፼/፼00$1፼/og;
129  
130 86 s/፼([፲-፺]0)$/፼00$1/o; # repeat at end of string
131  
132 86 s/፼([፩-፺]{2})፼/፼00$1፼/og;
133  
134 86 s/፼([፩-፺]{2})$/፼00$1/o; # repeat at end of string
135 }
136 else { ##
137 0 s/([፲-፺])\b/$1."0"/oe; # repeat at end of string
138  
139 # pad 0s for meto
140 #
141 # s/(፻)$/$1."00"/e; # this is stupid but tricks perl 5.6 into working
142 0 s/፻\b$/፻00/o;
143  
144 # pad 0s for ilf
145 #
146 0 s/፼\b/፼0000/o;
147 0 s/፼፼/፼0000፼/og; # since /g doesn't work the first time..
148 0 s/፼፼/፼0000፼/og; # ...we do it again!
149 0 s/፻፼/፼00፼/og;
150 0 s/፼0([፩-፱])፼/፼000$1፼/og;
151  
152 0 s/፼0([፩-፱])\b/፼000$1/o; # repeat at end of string
153  
154 0 s/፼([፲-፺]0)፼/፼00$1፼/og;
155  
156 0 s/፼([፲-፺]0)\b/፼00$1/o; # repeat at end of string
157  
158 0 s/፼([፩-፺]{2})፼/፼00$1፼/og;
159  
160 0 s/፼([፩-፺]{2})\b/፼00$1/o; # repeat at end of string
161 }
162  
163 86 s/[፻፼]//og;
164  
165 # fold tens:
166 #
167 86 tr/፲-፺/፩-፱/;
168  
169 # translit digits:
170 #
171 86 tr/፩-፱/1-9/;
172  
173 86 int $_;
174 }
175  
176  
177 sub _toEthiopic
178 95 ▶{
Subroutine _toEthiopic: called CC=14 SCAR=26.4
179 95 my $number = $_[0]->{number};
180  
181 95 my $n = length ( $number ) - 1;
182  
183 # map and return a single digit number
184 # don't waste time with the loop:
185 95 ▶ return ( $ENumbers[$number-1] ) unless ( $n );
Branch: 1 branch
BranchTrueFalseTotal
unless $n 4 91 95
186  
187  
188 91 ▶ unless ( $n % 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($n % 2) 50 41 91
189 #
190 # Add dummy leading 0 to precondition the number for
191 # the algorithm and reduce one logic test within the
192 # for loop
193 #
194 50 $number = "0$number";
195 50 $n++;
196 }
197  
198 91 my @aNumberString = split ( //, $number );
199 91 my $eNumberString = "";
200  
201  
202 #
203 # read number from most to least significant digits:
204 #
205 91 ▶ for ( my $place = $n; $place >= 0; $place-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($place >= 0) 316 91 407
206 #
207 # initialize values to emptiness:
208 #
209 316 my ($aTen, $aOne) = ( 0, 0); # ascii ten's and one's place
210 316 my ($eTen, $eOne) = ('',''); # ethiopic ten's and one's place
211  
212  
213 #
214 # populate our tens and ones places from the number string:
215 #
216 316 $aTen = $aNumberString[$n-$place]; $place--;
217 316 $aOne = $aNumberString[$n-$place];
218 316 ▶ $eTen = $ENumbers[$aTen-1+9] if ( $aTen );
Branch: 1 branch
BranchTrueFalseTotal
if $aTen 120 196 316
219 316 ▶ $eOne = $ENumbers[$aOne-1] if ( $aOne );
Branch: 1 branch
BranchTrueFalseTotal
if $aOne 185 131 316
220  
221  
222 #
223 # pos tracks our 'pos'ition in a sequence of 4 digits
224 # to help determine what separator we need between
225 # a grouping of tens and ones.
226 #
227 316 my $pos = int ( $place % 4 ) / 2; # make even/odd
228  
229  
230 #
231 # find a separator, if any, to follow ethiopic ten and one:
232 #
233 316 ▶ my $sep
Branch: 3 branches
BranchTrueFalseTotal
$place ? : 225 91 316
$pos ? : 136 89 225
$eTen ne '' || $eOne ne '' ? : 90 46 136
Condition100%3/3
$eTen ne "" or $eOne ne ""
l!l&&r!l&&!r
494146
MC/DC100%2/2
$eTen ne "" or $eOne ne ""
$eTen ne "" $eOne ne ""
Truth table100%3/3
A or B
A= $eTen ne ""B= $eOne ne ""
ABresult
1X1
011
000
234 = ( $place )
235 ? ( $pos ) # odd
236 ? ( ($eTen ne '') || ($eOne ne '') )
237 ? '፻'
238 : ''
239 : '፼'
240 : ''
241 ;
242  
243  
244 #
245 # if $eOne is an Ethiopic '፩' we want to clear it under
246 # under special conditions. These ellision rules could be
247 # combined into a single big test but gets harder to read
248 # and manage:
249 #
250 # if ( ( $eOne eq '፩' ) && ( $eTen eq '' ) && ( $n > 1 ) ) {
251 316 ▶ if ( ( $eOne eq '፩' ) && ( $eTen eq '' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($eOne eq "\x{1369}" and $eTen eq "") 72 244 316
Condition100%3/3
$eOne eq "\x{1369}" and $eTen eq ""
!ll&&!rl&&r
1875772
MC/DC100%2/2
$eOne eq "\x{1369}" and $eTen eq ""
$eOne eq "\x{1369}" $eTen eq ""
Truth table100%3/3
A and B
A= $eOne eq "\x{1369}"B= $eTen eq ""
ABresult
0X0
100
111
252 72 ▶ if ( $sep eq '፻' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($sep eq "\x{137b}") { } 29 43 72
elsif ($place + 1 == $n) { } 23 20 43
253 #
254 # A superflous implied ፩ before ፻
255 #
256 29 $eOne = '';
257 }
258 elsif ( ($place+1) == $n ) { # recover from initial $place--
259 #
260 # ፩ is the leading digit.
261 #
262 23 $eOne = '';
263 }
264 }
265  
266  
267 #
268 # put it all together and append to our output number:
269 #
270 316 $eNumberString .= "$eTen$eOne$sep";
271 }
272  
273 91 $eNumberString;
274 }
275  
276  
277 sub convert
278 190 ▶{
Subroutine convert: called CC=3 SCAR=11.0
Pod undocumented
279 190 my $self = shift;
280  
281  
282 #
283 # reset string if we've been passed one:
284 #
285 190 ▶ $self->number ( @_ ) if ( @_ );
Branch: 1 branch
BranchTrueFalseTotal
if @_ 95 95 190
286  
287 190 ▶ ( $self->number =~ /^[0-9]+$/ )
Branch: 1 branch
BranchTrueFalseTotal
$self->number =~ /^[0-9]+$/ ? : 95 95 190
288 ? $self->_toEthiopic
289 : $self->_fromEthiopic
290 ;
291 }
292  
293  
294 sub number
295 380 ▶{
Subroutine number: called CC=3 SCAR=11.0
Pod undocumented
296 380 my $self = shift;
297  
298 380 ▶ $self->_setArgs ( @_ ) || return
Branch: 1 branch
BranchTrueFalseTotal
if @_ 190 190 380
Condition75%3/4
@_ && ($self->_setArgs(@_) || (return))
!ll
190190
$self->_setArgs(@_) or return
l!l
1900
MC/DC0%0/2
@_ && ($self->_setArgs(@_) || (return))
@_ $self->_setArgs(@_)
Truth table33%1/3
A && B
A= @_B= $self->_setArgs(@_)
ABresult
0X0
100
111
299 if ( @_ );
300  
301 380 $self->{number};
302 }
303  
304  
305 #########################################################
306 # Do not change this, Do not put anything below this.
307 # File must return "true" value at termination
308 1 1;
309 ##########################################################
310  
311  
312 __END__
↑ Summary