From: Vincent Pit Date: Sat, 7 Feb 2009 12:10:52 +0000 (+0100) Subject: Set $_[2] to the default length for len magic on scalars X-Git-Tag: v0.29~3 X-Git-Url: http://git.vpit.fr/?a=commitdiff_plain;h=9cad66dcbed4359e8d70b1bc1a26000b475a400c;p=perl%2Fmodules%2FVariable-Magic.git Set $_[2] to the default length for len magic on scalars --- diff --git a/Magic.xs b/Magic.xs index 5b4576a..6585817 100644 --- a/Magic.xs +++ b/Magic.xs @@ -546,13 +546,11 @@ STATIC int vmg_svt_set(pTHX_ SV *sv, MAGIC *mg) { STATIC U32 vmg_svt_len(pTHX_ SV *sv, MAGIC *mg) { SV *svr; - I32 len, has_array; - U32 ret; + U32 len, ret; + svtype t = SvTYPE(sv); dSP; - has_array = SvTYPE(sv) == SVt_PVAV; - ENTER; SAVETMPS; @@ -560,9 +558,17 @@ STATIC U32 vmg_svt_len(pTHX_ SV *sv, MAGIC *mg) { EXTEND(SP, 3); PUSHs(sv_2mortal(newRV_inc(sv))); PUSHs(mg->mg_obj ? mg->mg_obj : &PL_sv_undef); - if (has_array) { + if (t < SVt_PVAV) { + STRLEN l; + U8 *s = (U8 *) SvPV_const(sv, l); + if (DO_UTF8(sv)) + len = utf8_length(s, s + l); + else + len = l; + mPUSHu(len); + } else if (t == SVt_PVAV) { len = av_len((AV *) sv) + 1; - mPUSHi(len); + mPUSHu(len); } else { len = 0; PUSHs(&PL_sv_undef); @@ -579,7 +585,7 @@ STATIC U32 vmg_svt_len(pTHX_ SV *sv, MAGIC *mg) { FREETMPS; LEAVE; - return has_array ? ret - 1 : ret; + return t == SVt_PVAV ? ret - 1 : ret; } STATIC int vmg_svt_clear(pTHX_ SV *sv, MAGIC *mg) { diff --git a/lib/Variable/Magic.pm b/lib/Variable/Magic.pm index aa7ee22..2700f71 100644 --- a/lib/Variable/Magic.pm +++ b/lib/Variable/Magic.pm @@ -329,8 +329,8 @@ Other arguments are specific to the magic hooked : C -When the variable is an array, C<$_[2]> contains the normal length. -The callback is also expected to return the new scalar or array length. +When the variable is an array or a scalar, C<$_[2]> contains the non-magical length. +The callback can return the new scalar or array length to use, or C to default to the normal length. =item * diff --git a/t/22-len.t b/t/22-len.t index 5d3ae92..3b8039e 100644 --- a/t/22-len.t +++ b/t/22-len.t @@ -3,14 +3,15 @@ use strict; use warnings; -use Test::More tests => 13; +use Test::More tests => 33; use Variable::Magic qw/wizard cast VMG_COMPAT_SCALAR_LENGTH_NOLEN/; my $c = 0; my $n = 1 + int rand 1000; -my $wiz = wizard len => sub { ++$c; return $n }; +my $d; +my $wiz = wizard len => sub { $d = $_[2]; ++$c; return $n }; is $c, 0, 'len: wizard() doesn\'t trigger magic'; my @a = qw/a b c/; @@ -20,46 +21,106 @@ cast @a, $wiz; is $c, 0, 'len: cast on array doesn\'t trigger magic'; $c = 0; +$d = undef; my $b = scalar @a; is $c, 1, 'len: get array length triggers magic correctly'; +is $d, 3, 'len: get array length have correct default length'; is $b, $n, 'len: get array length correctly'; $c = 0; +$d = undef; $b = $#a; is $c, 1, 'len: get last array index triggers magic correctly'; +is $d, 3, 'len: get last array index have correct default length'; is $b, $n - 1, 'len: get last array index correctly'; $n = 0; $c = 0; +$d = undef; $b = scalar @a; is $c, 1, 'len: get array length 0 triggers magic correctly'; +is $d, 3, 'len: get array length 0 have correct default length'; is $b, 0, 'len: get array length 0 correctly'; +$n = undef; +@a = (); +cast @a, $wiz; + +$c = 0; +$d = undef; +$b = scalar @a; +is $c, 1, 'len: get empty array length triggers magic correctly'; +is $d, 0, 'len: get empty array length have correct default length'; +is $b, 0, 'len: get empty array length correctly'; + +$c = 0; +$d = undef; +$b = $#a; +is $c, 1, 'len: get last empty array index triggers magic correctly'; +is $d, 0, 'len: get last empty array index have correct default length'; +is $b, -1, 'len: get last empty array index correctly'; + SKIP: { - skip 'length() no longer calls mg_len magic' => 5 if VMG_COMPAT_SCALAR_LENGTH_NOLEN; + skip 'length() no longer calls mg_len magic' => 16 if VMG_COMPAT_SCALAR_LENGTH_NOLEN; $c = 0; $n = 1 + int rand 1000; # length magic on scalars needs also get magic to be triggered. $wiz = wizard get => sub { return 'anything' }, - len => sub { ++$c; return $n }; + len => sub { $d = $_[2]; ++$c; return $n }; - my $x = int rand 1000; + my $x = 6789; $c = 0; cast $x, $wiz; is $c, 0, 'len: cast on scalar doesn\'t trigger magic'; $c = 0; + $d = undef; $b = length $x; is $c, 1, 'len: get scalar length triggers magic correctly'; + is $d, 4, 'len: get scalar length have correct default length'; is $b, $n, 'len: get scalar length correctly'; $n = 0; $c = 0; + $d = undef; $b = length $x; is $c, 1, 'len: get scalar length 0 triggers magic correctly'; + is $d, 4, 'len: get scalar length 0 have correct default length'; is $b, $n, 'len: get scalar length 0 correctly'; + + $n = undef; + $x = ''; + cast $x, $wiz; + + $c = 0; + $d = undef; + $b = length $x; + is $c, 1, 'len: get empty scalar length triggers magic correctly'; + is $d, 0, 'len: get empty scalar length have correct default length'; + is $b, 0, 'len: get empty scalar length correctly'; + + $x = "\x{20AB}ongs"; + cast $x, $wiz; + + { + use bytes; + + $c = 0; + $d = undef; + $b = length $x; + is $c, 1, 'len: get utf8 scalar length in bytes triggers magic correctly'; + is $d, 7, 'len: get utf8 scalar length in bytes have correct default length'; + is $b, $d, 'len: get utf8 scalar length in bytes correctly'; + } + + $c = 0; + $d = undef; + $b = length $x; + is $c, 1, 'len: get utf8 scalar length triggers magic correctly'; + is $d, 5, 'len: get utf8 scalar length have correct default length'; + is $b, $d, 'len: get utf8 scalar length correctly'; }