Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Reliably parsing an integer

by haukex (Bishop)
on Jun 30, 2020 at 08:46 UTC ( #11118708=note: print w/replies, xml ) Need Help??


in reply to Reliably parsing an integer

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% --

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11118708]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2020-09-28 00:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If at first I donít succeed, I Ö










    Results (143 votes). Check out past polls.

    Notices?