This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
pp_iter: factor out some common code in CXt_LOOP_FOR
authorDavid Mitchell <davem@iabyn.com>
Tue, 13 Nov 2012 11:55:19 +0000 (11:55 +0000)
committerDavid Mitchell <davem@iabyn.com>
Thu, 15 Nov 2012 22:41:07 +0000 (22:41 +0000)
pp_hot.c

index e02558c..38a5d4f 100644 (file)
--- a/pp_hot.c
+++ b/pp_hot.c
@@ -1971,36 +1971,29 @@ PP(pp_iter)
         AV *av = cx->blk_loop.state_u.ary.ary;
         SV *sv;
         bool av_is_stack = FALSE;
+        IV ix;
 
         if (!av) {
             av_is_stack = TRUE;
             av = PL_curstack;
         }
         if (PL_op->op_private & OPpITER_REVERSED) {
-            if (cx->blk_loop.state_u.ary.ix <= (av_is_stack
-                                        ? cx->blk_loop.resetsp + 1 : 0))
+            ix = --cx->blk_loop.state_u.ary.ix;
+            if (ix <= (av_is_stack ? cx->blk_loop.resetsp : -1))
                 RETPUSHNO;
-
-            if (SvMAGICAL(av) || AvREIFY(av)) {
-                SV * const * const svp = av_fetch(av, --cx->blk_loop.state_u.ary.ix, FALSE);
-                sv = svp ? *svp : NULL;
-            }
-            else {
-                sv = AvARRAY(av)[--cx->blk_loop.state_u.ary.ix];
-            }
         }
         else {
-            if (cx->blk_loop.state_u.ary.ix >= (av_is_stack ? cx->blk_oldsp :
-                                        AvFILL(av)))
+            ix = ++cx->blk_loop.state_u.ary.ix;
+            if (ix > (av_is_stack ? cx->blk_oldsp : AvFILL(av)))
                 RETPUSHNO;
+        }
 
-            if (SvMAGICAL(av) || AvREIFY(av)) {
-                SV * const * const svp = av_fetch(av, ++cx->blk_loop.state_u.ary.ix, FALSE);
-                sv = svp ? *svp : NULL;
-            }
-            else {
-                sv = AvARRAY(av)[++cx->blk_loop.state_u.ary.ix];
-            }
+        if (SvMAGICAL(av) || AvREIFY(av)) {
+            SV * const * const svp = av_fetch(av, ix, FALSE);
+            sv = svp ? *svp : NULL;
+        }
+        else {
+            sv = AvARRAY(av)[ix];
         }
 
         if (sv && SvIS_FREED(sv)) {