blib/lib/Silly/StringMaths.pm

filter: Statementstmt 97.1% 68 / 70 Branchbran 89.2% 25 / 28 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 12 / 12 Podpod 100% 9 / 9 totaltotal 95.9% 119 / 124 CC 17 SCAR 12.0
CC
17
Cov
95%
add
5.0
exponentiate
4.1
divide
4.0
CRAP 3.3
↑ Summary
1 package Silly::StringMaths;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use vars qw($VERSION @ISA @EXPORT_OK @EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 @ISA=qw(Exporter);
7 1 @EXPORT=();
8 1 @EXPORT_OK=qw(add subtract multiply divide exponentiate
9 normalise sign negative invert);
10  
11 1 $VERSION = '0.13';
12  
13 =head1 NAME
14  
15 Silly::StringMaths - Perl extension for doing maths with strings
16  
17 =head1 SYNOPSIS
18  
19 use Silly::StringMaths qw(add subtract multiply divide exponentiate);
20  
21 # Add two positive numbers - returns ABFOOR
22 print add("FOO", "BAR");
23  
24 # Add a generally positive number and a negative number
25 # - returns ot
26 print add("FNoRD", "yncft");
27  
28 # Subtract several numbers from a rather large one
29 # - returns accdeiiiiloopssu
30 print subtract("Supercalifragilisticepsialidocious",
31 "stupid", "made", "up", "word");
32  
33 # Multiply two negative numbers - returns AAACCCCCCEEELLLNNN
34 print multiply("cancel", "out");
35  
36 # Divide two numbers - returns AAA
37 print divide("EuropeanCommission", "France");
38  
39 # Confirm Pythagorus' theorum - returns nothing
40 print subtract(exponentiate("FETLA", "PI"),
41 exponentiate("TLA", "PI"),
42 exponentiate("ETLA", "PI"));
43  
44 =head1 DESCRIPTION
45  
46 Silly::StringMaths provides support for basic integer mathematics, using
47 strings rather than numbers. Upper-case letters are positive,
48 lower-case letters are negative, so ABCDEF would be 6 (but
49 WOMBAT would also be 6), whereas C<positive> would actually be
50 -8. Mixed-case is also possible, so Compaq is actually -5.
51 Most methods return a canonicalised version of the string -
52 e.g. C<ampq> rather than C<Compaq> (mixed case removed,
53 the result sorted alphabetically).
54  
55 The behaviour of other characters is as yet undefined, but be
56 warned that non-alphabetical characters may be reserved for
57 floating point or imaginary numbers.
58  
59 Actual numbers (i.e. the characters 0 to 9) will I<never> be used
60 by this module.
61  
62 =head1 BASIC METHODS
63  
64 =head2 add
65  
66 Takes an array of strings, returns the sum.
67  
68 =cut
69  
70 18 ▶sub add {
Subroutine add: called CC=5 SCAR=16.1
Pod documented
71 18 my $base=shift;
72 # Go through our arguments in turn
73 18 ▶ while (my $addition=shift) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $addition = shift()) 22 18 40
74 # If there are any positive elements, add them on now
75 22 ▶ while ($addition =~ s/([A-Z])//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($addition =~ s/([A-Z])//) 89 22 111
76 89 $base.=$1;
77 }
78 # If there are any negative elements, subtract:
79 22 ▶ while ($addition =~ s/([a-z])//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($addition =~ s/([a-z])//) 84 22 106
80 # First take away any positive letters
81 84 ▶ if ($base =~ /[A-Z]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base =~ /[A-Z]/) { } 49 35 84
82 49 $base =~ s/[A-Z]//;
83 } else {
84 # Then add on negative ones
85 35 $base.=$1;
86 }
87 }
88 }
89 # Return a normalised (i.e. sorted and condensed) version of this
90 18 return Silly::StringMaths::normalise($base);
91 }
92  
93 =head2 subtract
94  
95 Takes a string, subtracts all other supplied strings from it and
96 returns the result.
97  
98 =cut
99  
100 2 ▶sub subtract {
Subroutine subtract: called CC=2 SCAR=6.9
Pod documented
101 2 my ($base, @others)=@_;
102 # Find our base, subtract all other numbers by adding negative
103 # versions of them
104 2 foreach (@others) {
105 6 Silly::StringMaths::invert(\$_);
106 }
107 2 return Silly::StringMaths::add($base, @others);
108 }
109  
110 =head2 multiply
111  
112 Takes a string and multiplies it by all the other strings,
113 returning the resulting product.
114  
115 =cut
116  
117 6 ▶sub multiply {
Subroutine multiply: called CC=4 SCAR=13.9
Pod documented
118 # Find our base number, normalise it
119 6 my $base=Silly::StringMaths::normalise(shift);
120 6 ▶ while (my $product=Silly::StringMaths::normalise(shift)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $product = normalise(shift())) 6 6 12
121 # If the argument is negative, invert the base number
122 6 ▶ if (Silly::StringMaths::negative($product)) {
Branch: 1 branch
BranchTrueFalseTotal
if (negative($product)) 2 4 6
123 2 Silly::StringMaths::invert(\$base);
124 }
125 # Now add on the base number as many times as we have extra letters
126 # (so remove one letter from the product first)
127 6 $product =~ s%.%%;
128 6 my $step=$base;
129 6 ▶ while ($product =~ s%.%%) {
Branch: 1 branch
BranchTrueFalseTotal
if ($product =~ s/.//) 11 6 17
130 11 $base=Silly::StringMaths::add($base, $step);
131 }
132 }
133 6 return $base;
134 }
135  
136 =head2 divide
137  
138 Takes a string, and divides it by all the other strings,
139 returning the result. Results are rounded down.
140  
141 =cut
142  
143 1 ▶sub divide {
Subroutine divide: called CC=4 SCAR=13.9
Pod documented
144 # Find our base number, normalise it
145 1 my $base=Silly::StringMaths::normalise(shift);
146 # Find the sign of this number, convert the base number to positive
147 1 my $sign=Silly::StringMaths::sign($base);
148 1 ▶ if (Silly::StringMaths::negative($sign)) {
Branch: 1 branch
BranchTrueFalseTotal
if (negative($sign)) 1 0 1
149 1 $base=Silly::StringMaths::multiply($base, $sign);
150 }
151 # Step through our divisors
152 1 ▶ while (my $divisor=Silly::StringMaths::normalise(shift)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $divisor = normalise(shift())) 1 1 2
153 # If this divisor is negative, invert our sign
154 1 ▶ if (Silly::StringMaths::negative(Silly::StringMaths::sign($divisor))) {
Branch: 1 branch
BranchTrueFalseTotal
if (negative(sign($divisor))) 1 0 1
155 1 Silly::StringMaths::invert(\$sign);
156 1 Silly::StringMaths::invert(\$divisor);
157 }
158 # Now find how many times we can remove our divisor
159 # First convert our divisor to a regexp that can remove itself from
160 # a number, then apply it as many times as possible, and insert
161 # that many As into the return value
162 1 $divisor =~ s%.%.%g;
163 1 $base="A"x($base =~ s%$divisor%%g);
164 }
165 # Multiply our (positive) base number by the stored sign, return it
166 1 return Silly::StringMaths::multiply($base, $sign);
167 }
168  
169 =head2 exponentiate
170  
171 Takes a number, raises it to the appropriate power, as specified by the
172 other arguments. Returns the result. (Note that some textual information
173 is lost here - the result will be either C<A>s or C<a>s).
174  
175 =cut
176  
177 3 ▶sub exponentiate {
Subroutine exponentiate: called CC=4 SCAR=14.1
Pod documented
178 3 my $base=Silly::StringMaths::normalise(shift);
179 3 ▶ while (my $power=Silly::StringMaths::normalise(shift)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $power = normalise(shift())) 3 3 6
180 # Don't allow negative powers
181 3 ▶ if (Silly::StringMaths::negative($power)) {
Branch: 1 branch
BranchTrueFalseTotal
if (negative($power)) 0 3 3
182 0 warn "Cannot use negative power $power";
183 0 return undef;
184 }
185 # Find the number we multiply by (the original base number)
186 3 my $multiply=$base;
187 # Remove one
188 3 $power =~ s%.%%;
189 # For every remaining digit, multiply base by its original value
190 3 ▶ while ($power =~ s%.%%) {
Branch: 1 branch
BranchTrueFalseTotal
if ($power =~ s/.//) 3 3 6
191 3 $base=Silly::StringMaths::multiply($base, $multiply);
192 }
193 }
194 3 return $base;
195 }
196  
197 =head1 USEFUL TOOLBOX METHODS
198  
199 =head2 normalise
200  
201 Takes a string with, potentially, a mix of upper-case and lower-case
202 letters, and returns a sorted string that is unmistakeably either
203 positive or negative.
204  
205 =cut
206  
207 71 ▶sub normalise {
Subroutine normalise: called CC=3 SCAR=11.0
Pod documented
208 71 my ($number)=@_;
209  
210 # If there's a mixture of upper and lower case, add this number to
211 # a null string to make the negatives and positives cancel out
212 71 ▶ if ($number =~ /[A-Z]/ && $number =~ /[a-z]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number =~ /[A-Z]/ and $number =~ /[a-z]/) 3 68 71
Condition100%3/3
$number =~ /[A-Z]/ and $number =~ /[a-z]/
!ll&&!rl&&r
33353
MC/DC100%2/2
$number =~ /[A-Z]/ and $number =~ /[a-z]/
$number =~ /[A-Z]/ $number =~ /[a-z]/
Truth table100%3/3
A and B
A= $number =~ /[A-Z]/B= $number =~ /[a-z]/
ABresult
0X0
100
111
213 3 $number=Silly::StringMaths::add(undef, $number);
214 }
215 # Return this string in sorted form
216 71 return join("", sort split("", $number));
217 }
218  
219 =head2 sign
220  
221 Returns the sign of a number as either 1, 0 or -1 (as a string,
222 obviously).
223  
224 =cut
225  
226 2 ▶sub sign {
Subroutine sign: called CC=1 SCAR=0.0
Pod documented
227 # Take the sign of a number by normalising it, then removing all but
228 # the first character, so we have 1 or -1
229 2 my $number=Silly::StringMaths::normalise(shift);
230 2 $number =~ s%^(.).*%$1%;
231 2 return $number;
232 }
233  
234 =head2 negative
235  
236 Returns whether the supplied string is negative or not
237  
238 =cut
239  
240 11 ▶sub negative {
Subroutine negative: called CC=1 SCAR=0.0
Pod documented
241 11 my ($number)=@_;
242 11 return (Silly::StringMaths::normalise($number) =~ /[a-z]/);
243 }
244  
245 =head2 invert
246  
247 Takes a I<reference> to a number, inverts it.
248  
249 =cut
250  
251 10 ▶sub invert {
Subroutine invert: called CC=1 SCAR=0.0
Pod documented
252 10 my ($number)=shift;
253 10 $$number = Silly::StringMaths::normalise($$number);
254 10 $$number =~ tr/A-Za-z/a-zA-Z/;
255 10 return $$number;
256 }
257  
258  
259 =head1 AUTHOR
260  
261 Sam Kington, sam@illuminated.co.uk
262  
263 =head1 SEE ALSO
264  
265 perl(1).
266  
267 =cut
268  
269  
270 1 1;
271  
↑ Summary