TIMTOWTDI (and d'oh): For integers matching /^[0-9]+$/, Perl's string comparisons are perfectly fine, provided that one pads the shorter number with zeros.
sub validate_int {
my $str = shift;
state $max = int(eval $Config{nv_overflows_integers_at} or die);
croak "not an integer" unless defined $str && $str=~/\A[0-9]+\z/;
$str =~ s/\A0+(?=[1-9])//;
croak "integer too big" if length $str > length $max
|| sprintf("%0*s", length $max, $str) gt $max;
return 0+$str;
}
Update: A quick test shows that this is faster than the regex version even if I use string comparisons ((?{ $^N gt $max })) instead of Math::BigInt. /Update
Tests
use warnings;
use strict;
use feature 'state';
use Carp;
use Config;
sub validate_int {
my $str = shift;
state $max = int(eval $Config{nv_overflows_integers_at} or die);
croak "not an integer" unless defined $str && $str=~/\A[0-9]+\z/;
$str =~ s/\A0+(?=[1-9])//;
croak "integer too big" if length $str > length $max
|| sprintf("%0*s", length $max, $str) gt $max;
return 0+$str;
}
use Math::BigInt;
use Test::More;
sub exception (&) { eval { shift->(); 1 } ? undef : ($@ || die) }
is validate_int(0), 0;
is validate_int(1), 1;
is validate_int(3), 3;
is validate_int("000000000000000000000000000000000000000001"), 1;
ok exception { validate_int(undef) };
ok exception { validate_int("") };
ok exception { validate_int("x") };
ok exception { validate_int("123y") };
ok exception { validate_int(-1) };
ok exception { validate_int("-9999999999999999999999999999999") };
my $x = Math::BigInt->new(eval $Config{nv_overflows_integers_at})-1;
is validate_int("$x"), 0+$x->numify, "'$x' works (max-1)";
$x++;
is validate_int("$x"), 0+$x->numify, "'$x' works (max)";
$x++;
ok exception { validate_int("$x") }, "'$x' fails (max+1)";
ok exception { validate_int("999999999999999999999999999999999999") };
done_testing;
Benchmark (updated version of this):
#!/usr/bin/env perl
use warnings;
use strict;
use Benchmark qw/cmpthese/;
use feature 'state';
use Carp;
use Math::BigInt;
use Config;
use Regexp::Common qw/number/;
sub validate_int {
my $str = shift;
state $max = Math::BigInt->new(
eval $Config{nv_overflows_integers_at} );
croak "not an integer"
unless defined $str && $str=~/\A$RE{num}{int}\z/;
my $num = Math::BigInt->new($str);
croak "integer to small" if $num < 0;
croak "integer too big" if $num > $max;
return $num->numify;
}
sub validate_int2 {
my $str = shift;
state $regex = do {
my $max = eval $Config{nv_overflows_integers_at} or die;
my $len1 = length($max) - 1;
my $range = substr $max, 0, 1;
$range = $range eq "1" ? "1" : "1-$range";
qr{ \A (?: (?!0) [0-9]{1,$len1}
| ( [$range] [0-9]{$len1} )
(?(?{ Math::BigInt->new($^N)->bgt($max) })(*F))
) \z }x };
croak "bad integer" unless $str =~ $regex;
return 0+$str;
}
sub validate_int3 {
my $str = shift;
state $max = int(eval $Config{nv_overflows_integers_at} or die);
croak "not an integer" unless defined $str && $str=~/\A[0-9]+\z/;
$str =~ s/\A0+(?=[1-9])//;
croak "integer too big" if length $str > length $max
|| sprintf("%0*s", length $max, $str) gt $max;
return 0+$str;
}
my $max = eval($Config{nv_overflows_integers_at});
cmpthese(-2, {
first => sub {
validate_int(1)==1 or die;
validate_int(999)==999 or die;
validate_int($max-1)==$max-1 or die;
validate_int($max)==$max or die;
defined eval { validate_int($max+1) } and die;
},
second => sub {
validate_int2(1)==1 or die;
validate_int2(999)==999 or die;
validate_int2($max-1)==$max-1 or die;
validate_int2($max)==$max or die;
defined eval { validate_int2($max+1) } and die;
},
third => sub {
validate_int3(1)==1 or die;
validate_int3(999)==999 or die;
validate_int3($max-1)==$max-1 or die;
validate_int3($max)==$max or die;
defined eval { validate_int3($max+1) } and die;
},
});
__END__
Rate first second third
first 4544/s -- -70% -90%
second 15386/s 239% -- -67%
third 46631/s 926% 203% --
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.