Skip to content

Commit

Permalink
bugfix: pay attention to the IsUV flag for numified strings
Browse files Browse the repository at this point in the history
  • Loading branch information
DrHyde committed Jun 22, 2021
1 parent 46e5c5b commit 33a7300
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
0.1.2 2021-06-22

- bugfix: pay attention to the IsUV flag for numified strings

0.1.1 2021-06-03

- bugfix: all functions require an argument; to not supply one is a fatal error.
Expand Down
6 changes: 5 additions & 1 deletion Type.xs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ SV* _scalar_type(SV* argument) {
if(SvIOK(argument)) {
if(SvPOK(argument)) {
/* int is also a string, better see if it's not int-ified 007 */
sprintf(num_as_str, "%" IVdf, SvIVX(argument));
sprintf(
num_as_str,
(SvIsUV(argument) ? "%" UVuf : "%" IVdf),
(SvIsUV(argument) ? SvUVX(argument) : SvIVX(argument))
);
rval = (
(strcmp(SvPVX(argument), num_as_str)) == 0
? newSVpv("INTEGER", 7)
Expand Down
11 changes: 10 additions & 1 deletion lib/Scalar/Type.pm
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use warnings;

use Carp qw(croak);

our $VERSION = '0.1.1';
our $VERSION = '0.1.2';

require XSLoader;
XSLoader::load(__PACKAGE__, $VERSION);
Expand Down Expand Up @@ -141,6 +141,10 @@ for our purposes are:
an integer value
=item UV
an unsigned integer value, only used for ints > MAXINT / 2.
=item NV
a numeric value (ie a float)
Expand Down Expand Up @@ -267,6 +271,11 @@ being careful about strings like C<"007.5">.
If neither C<IOK> nor C<NOK> is set then we return C<SCALAR>.
And what about C<UV>s? They are treated exactly the same as C<IV>s, and a
variable with a valid C<UV> slot will have the B<C<IOK>> flag set. It will
also have the C<IsUV> flag set, which we use to determine how to stringify
the number.
=head1 SEE ALSO
L<Scalar::Util> in particular its C<blessed> function.
Expand Down
7 changes: 7 additions & 0 deletions t/all.t
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@ subtest "string subsequently used as an int or float" => sub {
is(type($foo), 'NUMBER', "this does become a float after a numeric operation");
note(capture_stderr { Dump($foo) });
};

subtest "'[MAXINT]'" => sub {
my $foo = ''.~0;
$foo + 0;
is(type($foo), 'INTEGER', "this becomes an integer after a numeric operation, even though the value is a UV (unsigned int), not an IV");
note(capture_stderr { Dump($foo) });
};
};

subtest "int subsequently used as a float" => sub {
Expand Down

0 comments on commit 33a7300

Please sign in to comment.