From 5c03061a16ec9af06e3d76d3757f9d3044d58d8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20P=C3=ADsa=C5=99?= Date: Wed, 11 Jan 2017 09:24:21 +0100 Subject: [PATCH] Ignore leading and trailing white spaces for non-decimal input MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Documentation reads that surrounding white space is ignored. But this does not work for non-decimal numbers since Math-BigInt-1.999712: $ perl -MMath::BigInt -e 'print Math::BigInt->new(" 0x1 "), qq{\n}' NaN This patch fixes it. CPAN RT#119805 Signed-off-by: Petr Písař --- lib/Math/BigFloat.pm | 6 ++++++ lib/Math/BigInt.pm | 6 ++++++ t/from_bin-mbf.t | 15 ++++++++++++++- t/from_hex-mbf.t | 15 ++++++++++++++- t/from_oct-mbf.t | 15 ++++++++++++++- 5 files changed, 54 insertions(+), 3 deletions(-) diff --git a/lib/Math/BigFloat.pm b/lib/Math/BigFloat.pm index 190e2ee..1030c46 100644 --- a/lib/Math/BigFloat.pm +++ b/lib/Math/BigFloat.pm @@ -526,6 +526,7 @@ sub from_hex { if ($str =~ s/ ^ + \s* # sign ( [+-]? ) @@ -552,6 +553,7 @@ sub from_hex { ( \d+ (?: _ \d+ )* ) )? + \s* $ //x) { @@ -611,6 +613,7 @@ sub from_oct { if ($str =~ s/ ^ + \s* # sign ( [+-]? ) @@ -634,6 +637,7 @@ sub from_oct { ( \d+ (?: _ \d+ )* ) )? + \s* $ //x) { @@ -693,6 +697,7 @@ sub from_bin { if ($str =~ s/ ^ + \s* # sign ( [+-]? ) @@ -719,6 +724,7 @@ sub from_bin { ( \d+ (?: _ \d+ )* ) )? + \s* $ //x) { diff --git a/lib/Math/BigInt.pm b/lib/Math/BigInt.pm index 24b14c4..b0782ac 100644 --- a/lib/Math/BigInt.pm +++ b/lib/Math/BigInt.pm @@ -729,12 +729,14 @@ sub from_hex { if ($str =~ s/ ^ + \s* ( [+-]? ) (0?x)? ( [0-9a-fA-F]* ( _ [0-9a-fA-F]+ )* ) + \s* $ //x) { @@ -780,11 +782,13 @@ sub from_oct { if ($str =~ s/ ^ + \s* ( [+-]? ) ( [0-7]* ( _ [0-7]+ )* ) + \s* $ //x) { @@ -830,12 +834,14 @@ sub from_bin { if ($str =~ s/ ^ + \s* ( [+-]? ) (0?b)? ( [01]* ( _ [01]+ )* ) + \s* $ //x) { diff --git a/t/from_bin-mbf.t b/t/from_bin-mbf.t index a8c7527..caaaa2f 100644 --- a/t/from_bin-mbf.t +++ b/t/from_bin-mbf.t @@ -3,19 +3,32 @@ use strict; use warnings; -use Test::More tests => 27; +use Test::More tests => 1 + 26 * 4; my $class; BEGIN { $class = 'Math::BigFloat'; } BEGIN { use_ok($class, '1.999710'); } +my @data; +my $space = "\t\r\n "; + while () { s/#.*$//; # remove comments s/\s+$//; # remove trailing whitespace next unless length; # skip empty lines my ($in0, $out0) = split /:/; + + push @data, [ $in0, $out0 ], + [ $in0 . $space, $out0 ], + [ $space . $in0, $out0 ], + [ $space . $in0 . $space, $out0 ]; +} + +for my $entry (@data) { + my ($in0, $out0) = @$entry; + my $x; my $test = qq|\$x = $class -> from_bin("$in0");|; diff --git a/t/from_hex-mbf.t b/t/from_hex-mbf.t index b45917a..8fa8a0b 100644 --- a/t/from_hex-mbf.t +++ b/t/from_hex-mbf.t @@ -3,19 +3,32 @@ use strict; use warnings; -use Test::More tests => 27; +use Test::More tests => 1 + 26 * 4; my $class; BEGIN { $class = 'Math::BigFloat'; } BEGIN { use_ok($class, '1.999710'); } +my @data; +my $space = "\t\r\n "; + while () { s/#.*$//; # remove comments s/\s+$//; # remove trailing whitespace next unless length; # skip empty lines my ($in0, $out0) = split /:/; + + push @data, [ $in0, $out0 ], + [ $in0 . $space, $out0 ], + [ $space . $in0, $out0 ], + [ $space . $in0 . $space, $out0 ]; +} + +for my $entry (@data) { + my ($in0, $out0) = @$entry; + my $x; my $test = qq|\$x = $class -> from_hex("$in0");|; diff --git a/t/from_oct-mbf.t b/t/from_oct-mbf.t index 7e58454..19392f2 100644 --- a/t/from_oct-mbf.t +++ b/t/from_oct-mbf.t @@ -3,19 +3,32 @@ use strict; use warnings; -use Test::More tests => 27; +use Test::More tests => 1 + 26 * 4; my $class; BEGIN { $class = 'Math::BigFloat'; } BEGIN { use_ok($class, '1.999710'); } +my @data; +my $space = "\t\r\n "; + while () { s/#.*$//; # remove comments s/\s+$//; # remove trailing whitespace next unless length; # skip empty lines my ($in0, $out0) = split /:/; + + push @data, [ $in0, $out0 ], + [ $in0 . $space, $out0 ], + [ $space . $in0, $out0 ], + [ $space . $in0 . $space, $out0 ]; +} + +for my $entry (@data) { + my ($in0, $out0) = @$entry; + my $x; my $test = qq|\$x = $class -> from_oct("$in0");|; -- 2.7.4