Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^4: Reliably parsing an integer (updated)

by haukex (Bishop)
on Jun 29, 2020 at 21:35 UTC ( #11118681=note: print w/replies, xml ) Need Help??


in reply to Re^3: Reliably parsing an integer
in thread Reliably parsing an integer

It's quite often possible to construct a regex to discriminate a number range, but this is usually more of an academic exercise than a practical solution.

Turns out to not be too difficult if you cheat a little with some embedded code ;-) (of course hippo's suggestion is a lot more elegant Update: if that's the limit one wants to implement)

use Config; use Math::BigInt; my $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 };

One difference to my first version is that this regex doesn't allow zero (0).

Update: Updated benchmark and faster version of the code here.

Tests

use warnings; use strict; use Config; use Math::BigInt; my $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 }; use Test::More; diag explain $regex; unlike 0, $regex; like 1, $regex; like 3, $regex; unlike "", $regex; unlike "x", $regex; unlike "123y", $regex; unlike -1, $regex; unlike "-9999999999999999999999999999999", $regex; my $x = eval($Config{nv_overflows_integers_at})-1; like "$x", $regex, "max-1 ($x)"; $x++; like "$x", $regex, "max works ($x)"; $x++; unlike "$x", $regex, "max+1 fails ($x)"; $x+=1_000; unlike "$x", $regex; unlike "999999999999999999999999999999999999", $regex; done_testing;

Benchmark (vs. my first version):

#!/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; } my $max = eval($Config{nv_overflows_integers_at}); cmpthese(-1, { 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; }, }); __END__ Rate first second first 4403/s -- -70% second 14902/s 238% --

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2020-11-25 15:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?