Patch 8.2.4627
Problem:    flatten() does not use maxdepth correctly.
Solution:   Use a recursive implementation. (closes #10020)
Files:      src/list.c, src/testdir/test_flatten.vim


*** ../vim-8.2.4626/src/list.c  2022-02-23 13:16:55.761541147 +0000
--- src/list.c  2022-03-25 19:49:18.105329739 +0000
***************
*** 916,950 ****
  }
  
  /*
!  * Flatten "list" to depth "maxdepth".
   * It does nothing if "maxdepth" is 0.
   * Returns FAIL when out of memory.
   */
      static void
! list_flatten(list_T *list, long maxdepth)
  {
      listitem_T        *item;
      listitem_T        *tofree;
!     int               n;
  
      if (maxdepth == 0)
        return;
      CHECK_LIST_MATERIALIZE(list);
  
!     n = 0;
!     item = list->lv_first;
!     while (item != NULL)
      {
        fast_breakcheck();
        if (got_int)
            return;
  
        if (item->li_tv.v_type == VAR_LIST)
        {
!           listitem_T *next = item->li_next;
  
            vimlist_remove(list, item, item);
!           if (list_extend(list, item->li_tv.vval.v_list, next) == FAIL)
            {
                list_free_item(list, item);
                return;
--- 916,955 ----
  }
  
  /*
!  * Flatten up to "maxitems" in "list", starting at "first" to depth 
"maxdepth".
!  * When "first" is NULL use the first item.
   * It does nothing if "maxdepth" is 0.
   * Returns FAIL when out of memory.
   */
      static void
! list_flatten(list_T *list, listitem_T *first, long maxitems, long maxdepth)
  {
      listitem_T        *item;
      listitem_T        *tofree;
!     int               done = 0;
  
      if (maxdepth == 0)
        return;
      CHECK_LIST_MATERIALIZE(list);
+     if (first == NULL)
+       item = list->lv_first;
+     else
+       item = first;
  
!     while (item != NULL && done < maxitems)
      {
+       listitem_T      *next = item->li_next;
+ 
        fast_breakcheck();
        if (got_int)
            return;
  
        if (item->li_tv.v_type == VAR_LIST)
        {
!           list_T      *itemlist = item->li_tv.vval.v_list;
  
            vimlist_remove(list, item, item);
!           if (list_extend(list, itemlist, next) == FAIL)
            {
                list_free_item(list, item);
                return;
***************
*** 952,974 ****
            clear_tv(&item->li_tv);
            tofree = item;
  
!           if (item->li_prev == NULL)
!               item = list->lv_first;
!           else
!               item = item->li_prev->li_next;
            list_free_item(list, tofree);
- 
-           if (++n >= maxdepth)
-           {
-               n = 0;
-               item = next;
-           }
-       }
-       else
-       {
-           n = 0;
-           item = item->li_next;
        }
      }
  }
  
--- 957,971 ----
            clear_tv(&item->li_tv);
            tofree = item;
  
!           if (maxdepth > 0)
!               list_flatten(list, item->li_prev == NULL
!                                    ? list->lv_first : item->li_prev->li_next,
!                               itemlist->lv_len, maxdepth - 1);
            list_free_item(list, tofree);
        }
+ 
+       ++done;
+       item = next;
      }
  }
  
***************
*** 1031,1037 ****
        ++l->lv_refcount;
      }
  
!     list_flatten(l, maxdepth);
  }
  
  /*
--- 1028,1034 ----
        ++l->lv_refcount;
      }
  
!     list_flatten(l, NULL, l->lv_len, maxdepth);
  }
  
  /*
*** ../vim-8.2.4626/src/testdir/test_flatten.vim        2021-11-21 
11:35:59.456938797 +0000
--- src/testdir/test_flatten.vim        2022-03-25 19:46:41.937658354 +0000
***************
*** 79,84 ****
--- 79,92 ----
    call add(y, x) " l:y = [2, [1, [...]]]
    call assert_equal([1, 2, 1, 2], flatten(l:x, 2))
    call assert_equal([2, l:x], l:y)
+ 
+   let l4 = [ 1, [ 11, [ 101, [ 1001 ] ] ] ]
+   call assert_equal(l4, flatten(deepcopy(l4), 0))
+   call assert_equal([1, 11, [101, [1001]]], flatten(deepcopy(l4), 1))
+   call assert_equal([1, 11, 101, [1001]], flatten(deepcopy(l4), 2))
+   call assert_equal([1, 11, 101, 1001], flatten(deepcopy(l4), 3))
+   call assert_equal([1, 11, 101, 1001], flatten(deepcopy(l4), 4))
+   call assert_equal([1, 11, 101, 1001], flatten(deepcopy(l4)))
  endfunc
  
  func Test_flattennew()
***************
*** 88,93 ****
--- 96,109 ----
  
    call assert_equal([1, 2, [3, 4], 5], flattennew(l, 1))
    call assert_equal([1, [2, [3, 4]], 5], l)
+ 
+   let l4 = [ 1, [ 11, [ 101, [ 1001 ] ] ] ]
+   call assert_equal(l4, flatten(deepcopy(l4), 0))
+   call assert_equal([1, 11, [101, [1001]]], flattennew(l4, 1))
+   call assert_equal([1, 11, 101, [1001]], flattennew(l4, 2))
+   call assert_equal([1, 11, 101, 1001], flattennew(l4, 3))
+   call assert_equal([1, 11, 101, 1001], flattennew(l4, 4))
+   call assert_equal([1, 11, 101, 1001], flattennew(l4))
  endfunc
  
  " vim: shiftwidth=2 sts=2 expandtab
*** ../vim-8.2.4626/src/version.c       2022-03-25 15:42:07.311639049 +0000
--- src/version.c       2022-03-25 19:49:35.193294695 +0000
***************
*** 752,753 ****
--- 752,755 ----
  {   /* Add new patch number below this line */
+ /**/
+     4627,
  /**/

-- 
The question is:  What do you do with your life?
The wrong answer is: Become the richest guy in the graveyard.
                                (billionaire and Oracle founder Larry Ellison)

 /// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net   \\\
///                                                                      \\\
\\\        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/20220325195134.E00741C0C1F%40moolenaar.net.

Raspunde prin e-mail lui