File Coverage

blib/lib/Math/Inequalities/Parser.pm
Criterion Covered Total %
statement 22 23 95.6
branch 21 22 95.4
condition 4 5 80.0
subroutine 5 5 100.0
pod 1 1 100.0
total 53 56 94.6


line stmt bran cond sub pod time code
1             package Math::Inequalities::Parser;
2 2     2   52118 use strict;
  2         6  
  2         99  
3 2     2   15 use warnings;
  2         5  
  2         83  
4 2     2   21 use Carp qw/ croak /;
  2         4  
  2         158  
5 2     2   13 use Exporter qw/ import /;
  2         5  
  2         2166  
6              
7             our @EXPORT = qw/ parse_inequality /;
8              
9             our $VERSION = '0.002';
10              
11             sub parse_inequality {
12 47     47 1 31353 my ($string) = @_;
13 47   100     117 $string ||= '';
14 47 100 66     471 if ($string =~ /^\s*(\d+)\s*<\s*n\s*<\s*(\d+)\s*$/ ) {
    100          
    100          
    100          
    100          
    100          
    50          
15 4         32 return ($1+1, $2-1);
16             }
17             elsif ($string =~ /^\s*(\d+)\s*<(=)?\s*n/ ) {
18 8 100       72 return ($1 + ($2 ? 0 : 1), undef);
19             }
20             elsif ($string =~ /^\s*(\d+)\s*>(=)?\s*n\s*$/ ) {
21 8 100       70 return (undef, $1 - ($2 ? 0 : 1));
22             }
23             elsif ($string =~ /^\s*n\s*>(=)?\s*(\d+)\s*$/ ) {
24 8 100       72 return ($2 + ($1 ? 0 : 1), undef);
25             }
26             elsif ($string =~ /^\s*n\s*<(=)?\s*(\d+)\s*$/ ) {
27 8 100       74 return (undef, $2 - ($1 ? 0 : 1));
28             }
29             elsif ($string =~ /^\s*(\d+)\s*$/ ) {
30 4         33 return ($1, $1);
31             }
32             elsif (length $string && $string !~ /^\s+$/) {
33 0         0 croak "Cannot parse '$string' as an inequality.";
34             }
35 7         45 return (undef, undef);
36             }
37              
38             1;
39              
40             =head1 NAME
41              
42             Math::Inequalities::Parser - Minimum and maximum values allowed by an inequality.
43              
44             =head1 SYNOPSIS
45              
46             use Math::Inequalities::Parser;
47            
48             my ($min, $max) = parse_inequality( ' 10 < n < 20 ' );
49             # $min = 11
50             # $max = 19
51              
52             =head1 DESCRIPTION
53              
54             Tiny library for parsing integer maximum and minimum out when given an arbitrary inequality.
55              
56             Because getting this simple thing right was far harder
57             than it looked, and I never want to have to think about it again.
58              
59             =head1 FUNCTIONS
60              
61             =head2 parse_inequality
62              
63             Parses an inequality string and returns a list of two values, the minimum and the maxium value
64             that string will allow.
65              
66             =head1 TYPES OF INEQUALITY
67              
68             =head2 VALUE
69              
70             The simplest type, a single value, e.g. C<< 42 = Min 42, Max 42 >>.
71              
72             =head2 n < VALUE
73              
74             Maximum is VALUE - 1, Minimum is undefined, e.g. C<< n < 42 = Min undef, Max 41 >>.
75              
76             =head2 n > VALUE
77              
78             Minimum is VALUE +1, Maximum is undefined, e.g. C<< n > 42 = Min 43, Max undef >>.
79              
80             =head2 n <= VALUE
81              
82             Maximum is VALUE, Minimum is undefined, e.g. C<< n < 42 = Min undef, Max 42 >>.
83              
84             =head2 n >= VALUE
85              
86             Minimum is VALUE, Maximum is undefined, e.g. C<< n > 42 = Min 42, Max undef >>.
87              
88             =head2 Cases with VALUE, followed by N.
89              
90             Handled as above, but with minimum and maximum reversed as expected.
91              
92             =head2 VALUE1 < n < VALUE2
93              
94             Minimum is VALUE1 + 1, maximum is VALUE2 - 1, e.g C<< 42 < n < 200 = Min 43, Max 199 >>.
95              
96             =head1 BUGS
97              
98             =over
99              
100             =item Does not handle C<< VALUE1 <= n <= VALUE2 >> or similar. Patches welcome.
101              
102             =item Does not complain at impossible C<> combinations (e.g. C<< 5 < n < 4 >>) which result in a higher minumum than the maxiumum. Patches welcome.
103              
104             =item Does not work with negative numbers. Patches welcome.
105              
106             =item Always uses C<< n >> as the number identifier, this should be configureable at import time.
107              
108             =item Uses Exporter (should use Sub::Exporter)
109              
110             =item B work with floating point numbers. I consider this a feature.
111              
112             =back
113              
114             =head1 AUTHORS
115              
116             Tomas Doran (t0m) C<< >>
117              
118             Dave Lambley
119              
120             =head1 COPYRIGHT & LICENSE
121              
122             Copyright 2011 the above author(s).
123              
124             This sofware is free software, and is licensed under the same terms as perl itself.
125              
126             =cut
127