PATCH for [perl #36043] '@foo = sort { $a <=> $b } @bar' uses too much memory
Salvador FandiƱo [Sun, 5 Jun 2005 04:25:00 +0000 (04:25 +0000)]
Message-ID: <20050605022436.21982.qmail@lists.develooper.com>

p4raw-id: //depot/perl@24708

pp_sort.c

index 349944e..203b55d 100644 (file)
--- a/pp_sort.c
+++ b/pp_sort.c
@@ -1593,17 +1593,40 @@ PP(pp_sort)
        sortsvp = S_sortsv_desc;
     }
 
-    /* shuffle stack down, removing optional initial cv (p1!=p2), plus any
-     * nulls; also stringify any args */
+    /* shuffle stack down, removing optional initial cv (p1!=p2), plus
+     * any nulls; also stringify or converting to integer or number as
+     * required any args */
     for (i=max; i > 0 ; i--) {
        if ((*p1 = *p2++)) {                    /* Weed out nulls. */
            SvTEMP_off(*p1);
-           if (!PL_sortcop && !SvPOK(*p1)) {
-               STRLEN n_a;
-               if (SvAMAGIC(*p1))
-                   overloading = 1;
-               else
-                   (void)sv_2pv(*p1, &n_a);
+           if (!PL_sortcop) {
+               if (priv & OPpSORT_NUMERIC) {
+                   if (priv & OPpSORT_INTEGER) {
+                       if (!SvIOK(*p1)) {
+                           if (SvAMAGIC(*p1))
+                               overloading = 1;
+                           else
+                               (void)sv_2iv(*p1);
+                       }
+                   }
+                   else {
+                       if (!SvNOK(*p1)) {
+                           if (SvAMAGIC(*p1))
+                               overloading = 1;
+                           else
+                               (void)sv_2nv(*p1);
+                       }
+                   }
+               }
+               else {
+                   if (!SvPOK(*p1)) {
+                       STRLEN n_a;
+                       if (SvAMAGIC(*p1))
+                           overloading = 1;
+                       else
+                           (void)sv_2pv(*p1, &n_a);
+                   }
+               }
            }
            p1++;
        }