Regex/Util/inf.pm

filter: Statementstmt 100% 46 / 46 Branchbran 87.5% 14 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 14 / 14 Podpod 0.0% 0 / 3 totaltotal 93.6% 74 / 79 CC 9 SCAR 6.5
CC
9
Cov
97%
_pad
3.0
inf_type
3.0
_add
2.0
CRAP 1.9
« Regex/Range.pm ↑ Summary Regex/EmptyRange.pm »
1 # Number::Range::Regex::Util::inf
2 #
3 # Copyright 2012 Brian Szymanski. All rights reserved. This module is
4 # free software; you can redistribute it and/or modify it under the same
5 # terms as Perl itself.
6  
7 package Number::Range::Regex::Util::inf;
8  
9 # why don't we use perl's "support" for inf?
10 # 1) behaves in various different ways between 5.6.X where X <= 1
11 # 5.6.Y where Y >= 2, 5.8.X where X <= 7, and 5.8.Y where Y >= 8
12 # 2) it's annoying - you can't implement a function inf() because of
13 # perl's desire to look like a shell script. because of this,
14 # -inf is interpreted as a bareword so you can say dumb(-foo => bar);
15 # but +inf and inf generate errors about barewords. you can't simply
16 # "fix" that by adding a sub inf { return 'inf' }; because that
17 # generates warnings about -inf being ambiguous between the literal
18 # '-inf' and -&inf(); in caller context.
19 # 3) it is only supported if the underlying libc supports it
20 # 4) it depends on the underlying libc's definition of the string
21 # version of infinity, which on win32 is '1.#INF', solaris
22 # 'Infinity', and libc 'inf'
23  
24 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 15 ▶use vars qw ( @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
26 15 eval { require warnings; }; #it's ok if we can't load warnings
27  
28 15 require Exporter;
29 15 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
30 15 @ISA = qw( Exporter );
31 15 @EXPORT = qw ( pos_inf neg_inf );
32 15 @EXPORT_OK = qw ( inf_type _cmp _is_negative _pad );
33 15 %EXPORT_TAGS = ( all => [ @EXPORT, @EXPORT_OK ] );
34  
35 15 $VERSION = '0.32';
36  
37 15 ▶use overload '<=>' => \&_cmp, # also defines <, <=, ==, !=, >=, >
Subroutine BEGIN: called CC=1 SCAR=0.0
38 '+' => \&_add, # with neg, also defines non-unary -
39 'neg' => \&_neg, # unary minus
40 'eq' => \&_eq, # string equality check, always returns false
41 15 ▶ '""' => sub { my $self = shift; return $$self };
Subroutine __ANON__: called CC=1 SCAR=0.0
42  
43 16873 ▶sub pos_inf { my $v = '+inf'; return bless \$v, __PACKAGE__; }
Subroutine pos_inf: called CC=1 SCAR=0.0
Pod undocumented
44 711 ▶sub neg_inf { my $v = '-inf'; return bless \$v, __PACKAGE__; }
Subroutine neg_inf: called CC=1 SCAR=0.0
Pod undocumented
45  
46 # returns -1 if this is neg_inf, 0 if this is non-infinite, 1 if pos_inf
47 50183 ▶sub inf_type {
Subroutine inf_type: called CC=3 SCAR=11.0
Pod undocumented
48 50183 my ($val) = @_;
49 50183 my $str_val = "$val";
50 50183 ▶ return $str_val eq '-inf' ? -1 : $str_val eq '+inf' ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
$str_val eq '-inf' ? : 1908 48275 50183
$str_val eq '+inf' ? : 17783 30492 48275
51 }
52  
53 24 ▶sub _neg {
Subroutine _neg: called CC=2 SCAR=6.9
54 24 my ($val) = @_;
55 24 ▶ return pos_inf if inf_type($val) == -1;
Branch: 1 branch
BranchTrueFalseTotal
if inf_type($val) == -1 12 12 24
56 12 return neg_inf; # inf_type($val) == 1 # if we're not -inf, we're +inf
57 }
58  
59 # we can't do numberic comparisons because of non-base-10 support,
60 # and we don't want to stringify when we can avoid it
61 10465 ▶sub _is_negative {
Subroutine _is_negative: called CC=2 SCAR=6.9
62 10465 my ($val) = @_;
63 10465 my $inf_type = inf_type($val);
64 10465 ▶ return $inf_type ? $inf_type == -1 : $val =~ /^-/;
Branch: 1 branch
BranchTrueFalseTotal
$inf_type ? : 99 10366 10465
65 }
66  
67 # usage: _pad( $value, $num_extra_leading_zeroes );
68 142 ▶sub _pad {
Subroutine _pad: called CC=3 SCAR=11.1
69 142 my ($val, $extra) = @_;
70 142 ▶ return $val if inf_type($val);
Branch: 1 branch
BranchTrueFalseTotal
if inf_type($val) 0 142 142
71 142 ▶ return $val =~ s/^-// ? '-'.(0 x $extra).$val : (0 x $extra).$val;
Branch: 1 branch
BranchTrueFalseTotal
$val =~ s/^-// ? : 9 133 142
72 }
73  
74 # for our purposes, -inf!=-inf, and +inf!=+inf
75 392 ▶sub _eq { return }
Subroutine _eq: called CC=1 SCAR=0.0
76  
77 52 ▶sub _add {
Subroutine _add: called CC=2 SCAR=7.1
78 52 my ($a, $b, $swapped) = @_;
79 # note: the case of 2 non-infinite numbers never gets here, so the
80 # below is accurate
81 52 ▶ die "neg_inf + pos_inf is undefined" if 0 == inf_type($a) + inf_type($b);
Branch: 1 branch
BranchTrueFalseTotal
if 0 == inf_type($a) + inf_type($b) 0 52 52
82 # some infinite value k + any value that is not the opposite of itself = k
83 52 return $a;
84 }
85  
86 18619 ▶sub _cmp {
Subroutine _cmp: called CC=2 SCAR=6.9
87 18619 my ($l, $r, $swapped) = @_;
88 18619 ▶ ($l, $r) = ($r, $l) if $swapped;
Branch: 1 branch
BranchTrueFalseTotal
if $swapped 17543 1076 18619
89 # note: the below would be wrong for the case of 2 non-infinite numbers,
90 # (inf_type($l) == inf_type($r) == 0), but we never check the overloaded
91 # _cmp() in that case, as both $l and $r are non-overloaded & non-infinite
92 18619 return inf_type($l) <=> inf_type($r);
93 }
94  
95 15 1;
« Regex/Range.pm ↑ Summary Regex/EmptyRange.pm »