Make srand treat "-1" as -1
authorFather Chrysostomos <sprout@cpan.org>
Wed, 27 Jun 2012 01:04:02 +0000 (18:04 -0700)
committerFather Chrysostomos <sprout@cpan.org>
Wed, 27 Jun 2012 07:51:44 +0000 (00:51 -0700)
It was returning U+FFFD for negative numbers, unless they
were strings.

The SvOK is to avoid double uninit warnings.  The !SvIsUV is for
efficiency.  We don’t need to coerce it to an NV if it is a UV
already, because we know it won’t pass that test.

pp.c
t/op/chr.t

diff --git a/pp.c b/pp.c
index f4c5693..c3221d5 100644 (file)
--- a/pp.c
+++ b/pp.c
@@ -3259,7 +3259,8 @@ PP(pp_chr)
     SvGETMAGIC(TOPs);
     if (((SvIOKp(TOPs) && !SvIsUV(TOPs) && SvIV_nomg(TOPs) < 0)
         ||
-        (SvNOKp(TOPs) && SvNV_nomg(TOPs) < 0.0))) {
+        ((SvNOKp(TOPs) || (SvOK(TOPs) && !SvIsUV(TOPs)))
+         && SvNV_nomg(TOPs) < 0.0))) {
        if (IN_BYTES) {
            value = SvUV_nomg(TOPs); /* chr(-1) eq chr(0xff), etc. */
            (void)POPs;
index 510492e..57b4ade 100644 (file)
@@ -6,7 +6,7 @@ BEGIN {
     require "test.pl";
 }
 
-plan tests => 38;
+plan tests => 42;
 
 # Note that t/op/ord.t already tests for chr() <-> ord() rountripping.
 
@@ -30,6 +30,7 @@ is(chr(-3.0), "\x{FFFD}");
     is(chr(-2  ), "\xFE");
     is(chr(-3.0), "\xFD");
 }
+
 # Make sure -1 is treated the same way when coming from a tied variable
 sub TIESCALAR {bless[]}
 sub STORE { $_[0][0] = $_[1] }
@@ -40,6 +41,12 @@ $t = -2; is chr $t, chr -2, 'chr $tied when $tied is -2';
 $t = -1.1; is chr $t, chr -1.1, 'chr $tied when $tied is -1.1';
 $t = -2.2; is chr $t, chr -2.2, 'chr $tied when $tied is -2.2';
 
+# And that stringy scalars are treated likewise
+is chr "-1", chr -1, 'chr "-1" eq chr -1';
+is chr "-2", chr -2, 'chr "-2" eq chr -2';
+is chr "-1.1", chr -1.1, 'chr "-1.1" eq chr -1.1';
+is chr "-2.2", chr -2.2, 'chr "-2.2" eq chr -2.2';
+
 # Check UTF-8 (not UTF-EBCDIC).
 SKIP: {
     skip "no UTF-8 on EBCDIC", 21 if chr(193) eq 'A';