Data/Str2Num.pm

filter: Statementstmt 25.8% 32 / 124 Branchbran 0.0% 0 / 104 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 69.2% 9 / 13 Podpod 60.0% 3 / 5 totaltotal 14.9% 44 / 294 CC 65 SCAR 69.3
CC
65
Cov
12%
str2float
1640.0
str2integer
552.0
config
12.0
CRAP 1017.6
↑ Summary Docs/Site_SVD/Data_Str2Num.pm »
1 #!perl
2 #
3 # The copyright notice and plain old documentation (POD)
4 # are at the end of this file.
5 #
6 package Data::Str2Num;
7
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use 5.001;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use warnings::register;
Subroutine BEGIN: called CC=1 SCAR=0.0
12
13 #####
14 # Connect up with the event log.
15 #
16 1 ▶use vars qw( $VERSION $DATE $FILE);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 $VERSION = '0.08';
18 1 $DATE = '2004/05/22';
19 1 $FILE = __FILE__;
20
21 1 ▶use vars qw(@ISA @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 require Exporter;
23 1 @ISA=('Exporter');
24 1 @EXPORT_OK = qw(str2float str2int str2integer);
25
26 1 ▶use Data::Startup;
Subroutine BEGIN: called CC=1 SCAR=0.0
27
28 1 ▶use vars qw($default_options);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 $default_options = new();
30
31 ######
32 # Provide a way to module wide configure
33 #
34 sub config
35 0 ▶{
Subroutine config: not called CC=3 SCAR=24.8
Pod undocumented
36 0 ▶ shift if UNIVERSAL::isa($_[0],__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], __PACKAGE__) 0 0 0
37 0 ▶ $default_options = new() unless $default_options;
Branch: 1 branch
BranchTrueFalseTotal
unless $Data::Str2Num::default_options 0 0 0
38 0 $default_options->config(@_);
39 }
40
41
42 #######
43 # Object used to set default, startup, options values.
44 #
45 sub new
46 1 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
47 1 Data::Startup->new(
48
49 ######
50 # Make Test variables visible to tech_config
51 #
52 ascii_float => 0
53 );
54
55 }
56
57
58 ######
59 # Covert a string to floats.
60 #
61 sub str2float
62 0 ▶{
Subroutine str2float: not called CC=40 SCAR=74.0
Pod documented
63 0 ▶ shift if UNIVERSAL::isa($_[0],__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], __PACKAGE__) 0 0 0
64 0 ▶ return '',() unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
65
66 0 ▶ $default_options = Data::Str2Num->new() unless ref($default_options);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $Data::Str2Num::default_options 0 0 0
67 0 ▶ my $options = $default_options->override(pop @_) if ref($_[-1]);
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[-1] 0 0 0
68
69 #########
70 # Drop leading empty strings
71 #
72 0 my @strs = @_;
73 0 ▶ while (@strs && $strs[0] !~ /^\s*\S/) {
Branch: 1 branch
BranchTrueFalseTotal
if (@strs and not $strs[0] =~ /^\s*\S/) 0 0 0
Condition0%0/3
@strs and not $strs[0] =~ /^\s*\S/
!ll&&!rl&&r
000
MC/DC0%0/2
@strs and not $strs[0] =~ /^\s*\S/
@strs not $strs[0] =~ /^\s*\S/
Truth table0%0/3
A and not(B)
A= @strsB= not $strs[0] =~ /^\s*\S/
ABresult
0X0
100
111
74 0 shift @strs;
75 }
76 0 ▶ @strs = () unless(@strs); # do not shift @strs out of existance
Branch: 1 branch
BranchTrueFalseTotal
unless @strs 0 0 0
77
78 0 my @floats = ();
79 0 ▶ my $early_exit unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
80 0 my ($sign,$integer,$fraction,$exponent);
81 0 foreach (@strs) {
82 0 ▶ next unless defined $_;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 0 0
83 0 ▶ while ( length($_) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_) 0 0 0
84
85 0 ($sign, $integer,$fraction,$exponent) = ('',undef,undef,undef);
86
87 #######
88 # Parse the integer part
89 #
90 0 ▶ if($_ =~ s/^\s*(-?)\s*(0[0-7]+|0?b[0-1]+|0x[0-9A-Fa-f]+)\s*[,;\n]?//) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_ =~ s/^\s*(-?)\s*(0[0-7]+|0?b[0-1]+|0x[0-9A-Fa-f]+)\s*[,;\n]?//) { } 0 0 0
elsif ($_ =~ s/^\s*(-?)\s*([0-9]+)\s*[,;\n]?//) { } 0 0 0
91 0 $integer = 0+oct($1 . $2);
92 0 ▶ $sign = $1 if $integer =~ s/^\s*-//;
Branch: 1 branch
BranchTrueFalseTotal
if $integer =~ s/^\s*-// 0 0 0
93 }
94 elsif ($_ =~ s/^\s*(-?)\s*([0-9]+)\s*[,;\n]?//) {
95 0 ($sign,$integer) = ($1,$2);
96 }
97
98 ######
99 # Parse the decimal part
100 #
101 0 ▶ $fraction = $1 if $_ =~ s/^\.([0-9]+)\s*[,;\n]?// ;
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ s/^\.([0-9]+)\s*[,;\n]?// 0 0 0
102
103 ######
104 # Parse the exponent part
105 0 ▶ $exponent = $1 . $2 if $_ =~ s/^E(-?)([0-9]+)\s*[,;\n]?//;
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ s/^E(-?)([0-9]+)\s*[,;\n]?// 0 0 0
106
107 0 ▶ goto LAST unless defined($integer) || defined($fraction) || defined($exponent);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $integer or defined $fraction or defined $exponent 0 0 0
Condition0%0/6
defined $integer or defined $fraction or defined $exponent
l!l&&r!l&&!r
000
defined $integer or defined $fraction
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $integer or defined $fraction or defined $exponent
defined $integer defined $fraction defined $exponent
Truth table0%0/4
A or B or C
A= defined $integerB= defined $fractionC= defined $exponent
ABCresult
1XX1
01X1
0011
0000
108
109 0 ▶ $integer = '' unless defined($integer);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $integer 0 0 0
110 0 ▶ $fraction = '' unless defined($fraction);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fraction 0 0 0
111 0 ▶ $exponent = 0 unless defined($exponent);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $exponent 0 0 0
112
113 0 ▶ if($options->{ascii_float} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{'ascii_float'}) { } 0 0 0
114 0 ▶ $integer .= '.' . $fraction if( $fraction);
Branch: 1 branch
BranchTrueFalseTotal
if $fraction 0 0 0
115 0 ▶ $integer .= 'E' . $exponent if( $exponent);
Branch: 1 branch
BranchTrueFalseTotal
if $exponent 0 0 0
116 0 push @floats,$sign . $integer;
117 }
118 else {
119 ############
120 # Normalize decimal float so that there is only one digit to the
121 # left of the decimal point.
122 #
123 0 ▶ while($integer && substr($integer,0,1) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($integer and substr($integer, 0, 1) == 0) 0 0 0
Condition0%0/3
$integer and substr($integer, 0, 1) == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$integer and substr($integer, 0, 1) == 0
$integer substr($integer, 0, 1) == 0
Truth table0%0/3
A and B
A= $integerB= substr($integer, 0, 1) == 0
ABresult
0X0
100
111
124 0 $integer = substr($integer,1);
125 }
126 0 ▶ if( $integer ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($integer) { } 0 0 0
127 0 $exponent += length($integer) - 1;
128 }
129 else {
130 0 ▶ while($fraction && substr($fraction,0,1) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fraction and substr($fraction, 0, 1) == 0) 0 0 0
Condition0%0/3
$fraction and substr($fraction, 0, 1) == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$fraction and substr($fraction, 0, 1) == 0
$fraction substr($fraction, 0, 1) == 0
Truth table0%0/3
A and B
A= $fractionB= substr($fraction, 0, 1) == 0
ABresult
0X0
100
111
131 0 $fraction = substr($fraction,1);
132 0 $exponent--;
133 }
134 0 $exponent--;
135 }
136 0 $integer .= $fraction;
137 0 ▶ while($integer && substr($integer,0,1) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($integer and substr($integer, 0, 1) == 0) 0 0 0
Condition0%0/3
$integer and substr($integer, 0, 1) == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$integer and substr($integer, 0, 1) == 0
$integer substr($integer, 0, 1) == 0
Truth table0%0/3
A and B
A= $integerB= substr($integer, 0, 1) == 0
ABresult
0X0
100
111
138 0 $integer = substr($integer,1);
139 }
140 0 ▶ $integer = 0 unless $integer;
Branch: 1 branch
BranchTrueFalseTotal
unless $integer 0 0 0
141 0 push @floats,[$sign . $integer, $exponent];
142 }
143 0 ▶ goto LAST if $early_exit;
Branch: 1 branch
BranchTrueFalseTotal
if $early_exit 0 0 0
144 }
145 0 ▶ last if $early_exit;
Branch: 1 branch
BranchTrueFalseTotal
if $early_exit 0 0 0
146 }
147
148 LAST:
149 #########
150 # Drop leading empty strings
151 #
152 0 ▶ while (@strs && $strs[0] !~ /^\s*\S/) {
Branch: 1 branch
BranchTrueFalseTotal
if (@strs and not $strs[0] =~ /^\s*\S/) 0 0 0
Condition0%0/3
@strs and not $strs[0] =~ /^\s*\S/
!ll&&!rl&&r
000
MC/DC0%0/2
@strs and not $strs[0] =~ /^\s*\S/
@strs not $strs[0] =~ /^\s*\S/
Truth table0%0/3
A and not(B)
A= @strsB= not $strs[0] =~ /^\s*\S/
ABresult
0X0
100
111
153 0 shift @strs;
154 }
155 0 ▶ @strs = () unless(@strs); # do not shift @strs out of existance
Branch: 1 branch
BranchTrueFalseTotal
unless @strs 0 0 0
156
157 0 ▶ return (\@strs, @floats) unless $early_exit;
Branch: 1 branch
BranchTrueFalseTotal
unless $early_exit 0 0 0
158 0 ($integer,$fraction,$exponent) = @{$floats[0]};
159 0 "${integer}${fraction}E${exponent}"
160 }
161
162
163
164 ######
165 # Convert number (oct, bin, hex, decimal) to decimal
166 #
167 sub str2int
168 0 ▶{
Subroutine str2int: not called CC=2 SCAR=17.9
Pod documented
169 0 ▶ shift if UNIVERSAL::isa($_[0],__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], __PACKAGE__) 0 0 0
170 ####
171 # do no let the wantarray kink in
172 0 my $num = str2integer(@_);
173 0 $num;
174 }
175
176
177
178 ######
179 # Convert number (oct, bin, hex, decimal) to decimal
180 #
181 sub str2integer
182 0 ▶{
Subroutine str2integer: not called CC=23 SCAR=63.1
Pod documented
183 0 ▶ shift if UNIVERSAL::isa($_[0],__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], __PACKAGE__) 0 0 0
184 0 ▶ unless( wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (wantarray) 0 0 0
185 0 ▶ return undef unless(defined($_[0]));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 0 0
186 0 my $str = $_[0];
187 0 ▶ return 0+oct($1) if($str =~ /^\s*(-?\s*0[0-7]+|0?b[0-1]+|0x[0-9A-Fa-f]+)\s*[,;\n]?$/);
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ /^\s*(-?\s*0[0-7]+|0?b[0-1]+|0x[0-9A-Fa-f]+)\s*[,;\n]?$/ 0 0 0
188 0 ▶ return 0+$1 if ($str =~ /^\s*(-?\s*[0-9]+)\s*[,;:\n]?$/ );
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ /^\s*(-?\s*[0-9]+)\s*[,;:\n]?$/ 0 0 0
189 0 return undef;
190 }
191
192 #######
193 # Pick up input strings
194 #
195 0 ▶ return [],() unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
196
197 0 ▶ $default_options = Data::Str2num->new() unless ref($default_options);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $Data::Str2Num::default_options 0 0 0
198 0 ▶ my $options = $default_options->override(pop @_) if ref($_[-1]);
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[-1] 0 0 0
199 0 my @strs = @_;
200
201 #########
202 # Drop leading empty strings
203 #
204 0 ▶ while (@strs && $strs[0] !~ /^\s*\S/) {
Branch: 1 branch
BranchTrueFalseTotal
if (@strs and not $strs[0] =~ /^\s*\S/) 0 0 0
Condition0%0/3
@strs and not $strs[0] =~ /^\s*\S/
!ll&&!rl&&r
000
MC/DC0%0/2
@strs and not $strs[0] =~ /^\s*\S/
@strs not $strs[0] =~ /^\s*\S/
Truth table0%0/3
A and not(B)
A= @strsB= not $strs[0] =~ /^\s*\S/
ABresult
0X0
100
111
205 0 shift @strs;
206 }
207 0 ▶ @strs = () unless(@strs); # do not shift @strs out of existance
Branch: 1 branch
BranchTrueFalseTotal
unless @strs 0 0 0
208
209 0 my ($int,$num);
210 0 my @integers = ();
211 0 foreach $_ (@strs) {
212 0 ▶ next unless defined $_;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 0 0
213 0 ▶ while ( length($_) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_) 0 0 0
214 0 ▶ if($_ =~ s/^\s*(-?)\s*(0[0-7]+|0?b[0-1]+|0x[0-9A-Fa-f]+)\s*[,;\n]?//) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_ =~ s/^\s*(-?)\s*(0[0-7]+|0?b[0-1]+|0x[0-9A-Fa-f]+)\s*[,;\n]?//) { } 0 0 0
elsif ($_ =~ s/^\s*(-?)\s*([0-9]+)\s*[,;\n]?//) { } 0 0 0
215 0 $int = $1 . $2;
216 0 $num = 0+oct($int);
217 }
218 elsif ($_ =~ s/^\s*(-?)\s*([0-9]+)\s*[,;\n]?// ) {
219 0 $int = $1 . $2;
220 0 $num = 0+$int;
221
222 }
223 else {
224 0 goto LAST;
225 }
226
227 #######
228 # If the integer is so large that Perl converted it to a float,
229 # repair the str so that the large integer may be dealt as a string
230 # or converted to a float. The using routine may be using Math::BigInt
231 # instead of using the native Perl floats and this automatic conversion
232 # would cause major damage.
233 #
234 0 ▶ if($num =~ /\s*[\.E]\d+/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($num =~ /\s*[\.E]\d+/) { } 0 0 0
elsif ($_ =~ /^\s*[\.E]\d+/) { } 0 0 0
235 0 $_ = $int;
236 0 goto LAST;
237 }
238
239 #######
240 # If there is a string float instead of an int repair the str to
241 # perserve the float. The using routine may decide to use str2float
242 # to parse out the float.
243 #
244 elsif($_ =~ /^\s*[\.E]\d+/) {
245 0 $_ = $int . $_;
246 0 goto LAST;
247 }
248 0 push @integers,$num;
249 }
250 }
251
252 LAST:
253 #########
254 # Drop leading empty strings
255 #
256 0 ▶ while (@strs && (!defined($strs[0]) || $strs[0] !~ /^\s*\S/)) {
Branch: 1 branch
BranchTrueFalseTotal
if (@strs and not defined $strs[0] && $strs[0] =~ /^\s*\S/) 0 0 0
Condition0%0/6
@strs and not defined $strs[0] && $strs[0] =~ /^\s*\S/
!ll&&!rl&&r
000
defined $strs[0] && $strs[0] =~ /^\s*\S/
!ll&&!rl&&r
000
MC/DC0%0/3
@strs and not defined $strs[0] && $strs[0] =~ /^\s*\S/
@strs defined $strs[0] $strs[0] =~ /^\s*\S/
Truth table0%0/4
A and not(B && C)
A= @strsB= defined $strs[0]C= $strs[0] =~ /^\s*\S/
ABCresult
0XX0
1110
10X1
1101
257 0 shift @strs;
258 }
259 0 ▶ @strs = ('') unless(@strs); # do not shift @strs out of existance
Branch: 1 branch
BranchTrueFalseTotal
unless @strs 0 0 0
260
261 0 (\@strs, @integers);
262 }
263
264 1 1
265
266 __END__
↑ Summary Docs/Site_SVD/Data_Str2Num.pm »