Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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

In reply to Re: Reliably parsing an integer by haukex
in thread Reliably parsing an integer by rdiez

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (4)
    As of 2020-10-22 07:35 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      My favourite web site is:












      Results (225 votes). Check out past polls.

      Notices?