lkundrak / rpms / vim

Forked from rpms/vim 4 years ago
Clone
b856621
To: vim_dev@googlegroups.com
b856621
Subject: Patch 7.4.341
b856621
Fcc: outbox
b856621
From: Bram Moolenaar <Bram@moolenaar.net>
b856621
Mime-Version: 1.0
b856621
Content-Type: text/plain; charset=UTF-8
b856621
Content-Transfer-Encoding: 8bit
b856621
------------
b856621
b856621
Patch 7.4.341
b856621
Problem:    sort() doesn't handle numbers well.
b856621
Solution:   Add an argument to specify sorting on numbers. (Christian Brabandt)
b856621
Files:	    runtime/doc/eval.txt, src/eval.c, src/testdir/test55.in,
b856621
	    src/testdir/test55.ok
b856621
b856621
b856621
*** ../vim-7.4.340/runtime/doc/eval.txt	2014-06-25 14:39:35.094348583 +0200
b856621
--- runtime/doc/eval.txt	2014-06-25 17:05:50.606680574 +0200
b856621
***************
b856621
*** 5618,5628 ****
b856621
  		
b856621
  		If you want a list to remain unmodified make a copy first: >
b856621
  			:let sortedlist = sort(copy(mylist))
b856621
- <		Uses the string representation of each item to sort on.
b856621
- 		Numbers sort after Strings, |Lists| after Numbers.
b856621
- 		For sorting text in the current buffer use |:sort|.
b856621
  
b856621
! 		When {func} is given and it is one then case is ignored.
b856621
  		When {func} is a |Funcref| or a function name, this function
b856621
  		is called to compare items.  The function is invoked with two
b856621
  		items as argument and must return zero if they are equal, 1 or
b856621
--- 5628,5647 ----
b856621
  		
b856621
  		If you want a list to remain unmodified make a copy first: >
b856621
  			:let sortedlist = sort(copy(mylist))
b856621
  
b856621
! <		When {func} is omitted, is empty or zero, then sort() uses the
b856621
! 		string representation of each item to sort on.  Numbers sort
b856621
! 		after Strings, |Lists| after Numbers.  For sorting text in the
b856621
! 		current buffer use |:sort|.
b856621
! 
b856621
! 		When {func} is given and it is is '1' or 'i' then case is
b856621
! 		ignored.
b856621
! 		
b856621
! 		When {func} is given and it is 'n' then all items will be
b856621
! 		sorted numerical (Implementation detail: This uses the
b856621
! 		strtod() function to parse numbers, Strings, Lists, Dicts and
b856621
! 		Funcrefs will be considered as being 0).
b856621
! 
b856621
  		When {func} is a |Funcref| or a function name, this function
b856621
  		is called to compare items.  The function is invoked with two
b856621
  		items as argument and must return zero if they are equal, 1 or
b856621
*** ../vim-7.4.340/src/eval.c	2014-06-17 17:48:21.776628008 +0200
b856621
--- src/eval.c	2014-06-25 17:23:05.466719724 +0200
b856621
***************
b856621
*** 17330,17335 ****
b856621
--- 17330,17336 ----
b856621
  	item_compare2 __ARGS((const void *s1, const void *s2));
b856621
  
b856621
  static int	item_compare_ic;
b856621
+ static int	item_compare_numeric;
b856621
  static char_u	*item_compare_func;
b856621
  static dict_T	*item_compare_selfdict;
b856621
  static int	item_compare_func_err;
b856621
***************
b856621
*** 17359,17368 ****
b856621
  	p1 = (char_u *)"";
b856621
      if (p2 == NULL)
b856621
  	p2 = (char_u *)"";
b856621
!     if (item_compare_ic)
b856621
! 	res = STRICMP(p1, p2);
b856621
      else
b856621
! 	res = STRCMP(p1, p2);
b856621
      vim_free(tofree1);
b856621
      vim_free(tofree2);
b856621
      return res;
b856621
--- 17360,17379 ----
b856621
  	p1 = (char_u *)"";
b856621
      if (p2 == NULL)
b856621
  	p2 = (char_u *)"";
b856621
!     if (!item_compare_numeric)
b856621
!     {
b856621
! 	if (item_compare_ic)
b856621
! 	    res = STRICMP(p1, p2);
b856621
! 	else
b856621
! 	    res = STRCMP(p1, p2);
b856621
!     }
b856621
      else
b856621
!     {
b856621
! 	double n1, n2;
b856621
! 	n1 = strtod((char *)p1, (char **)&p1;;
b856621
! 	n2 = strtod((char *)p2, (char **)&p2;;
b856621
! 	res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
b856621
!     }
b856621
      vim_free(tofree1);
b856621
      vim_free(tofree2);
b856621
      return res;
b856621
***************
b856621
*** 17439,17444 ****
b856621
--- 17450,17456 ----
b856621
  	    return;	/* short list sorts pretty quickly */
b856621
  
b856621
  	item_compare_ic = FALSE;
b856621
+ 	item_compare_numeric = FALSE;
b856621
  	item_compare_func = NULL;
b856621
  	item_compare_selfdict = NULL;
b856621
  	if (argvars[1].v_type != VAR_UNKNOWN)
b856621
***************
b856621
*** 17457,17462 ****
b856621
--- 17469,17487 ----
b856621
  		    item_compare_ic = TRUE;
b856621
  		else
b856621
  		    item_compare_func = get_tv_string(&argvars[1]);
b856621
+ 		if (item_compare_func != NULL)
b856621
+ 		{
b856621
+ 		    if (STRCMP(item_compare_func, "n") == 0)
b856621
+ 		    {
b856621
+ 			item_compare_func = NULL;
b856621
+ 			item_compare_numeric = TRUE;
b856621
+ 		    }
b856621
+ 		    else if (STRCMP(item_compare_func, "i") == 0)
b856621
+ 		    {
b856621
+ 			item_compare_func = NULL;
b856621
+ 			item_compare_ic = TRUE;
b856621
+ 		    }
b856621
+ 		}
b856621
  	    }
b856621
  
b856621
  	    if (argvars[2].v_type != VAR_UNKNOWN)
b856621
*** ../vim-7.4.340/src/testdir/test55.in	2014-03-25 18:23:27.062087691 +0100
b856621
--- src/testdir/test55.in	2014-06-25 17:20:47.006714486 +0200
b856621
***************
b856621
*** 332,337 ****
b856621
--- 332,342 ----
b856621
  :$put =string(reverse(sort(l)))
b856621
  :$put =string(sort(reverse(sort(l))))
b856621
  :$put =string(uniq(sort(l)))
b856621
+ :let l=[7, 9, 18, 12, 22, 10.0e-16, -1, 0xff, 0, -0, 0.22, 'foo', 'FOOBAR',{}, []]
b856621
+ :$put =string(sort(copy(l), 'n'))
b856621
+ :$put =string(sort(copy(l), 1))
b856621
+ :$put =string(sort(copy(l), 'i'))
b856621
+ :$put =string(sort(copy(l)))
b856621
  :"
b856621
  :" splitting a string to a List
b856621
  :$put =string(split('  aa  bb '))
b856621
*** ../vim-7.4.340/src/testdir/test55.ok	2014-03-25 18:23:27.062087691 +0100
b856621
--- src/testdir/test55.ok	2014-06-25 17:23:31.382720704 +0200
b856621
***************
b856621
*** 101,106 ****
b856621
--- 101,110 ----
b856621
  [[0, 1, 2], [0, 1, 2], 4, 2, 2, 1.5, 'xaaa', 'x8', 'foo6', 'foo', 'foo', 'A11', '-0']
b856621
  ['-0', 'A11', 'foo', 'foo', 'foo6', 'x8', 'xaaa', 1.5, 2, 2, 4, [0, 1, 2], [0, 1, 2]]
b856621
  ['-0', 'A11', 'foo', 'foo6', 'x8', 'xaaa', 1.5, 2, 4, [0, 1, 2]]
b856621
+ [-1, 0, 0, 'foo', 'FOOBAR', {}, [], 1.0e-15, 0.22, 7, 9, 12, 18, 22, 255]
b856621
+ ['foo', 'FOOBAR', -1, 0, 0, 0.22, 1.0e-15, 12, 18, 22, 255, 7, 9, [], {}]
b856621
+ ['foo', 'FOOBAR', -1, 0, 0, 0.22, 1.0e-15, 12, 18, 22, 255, 7, 9, [], {}]
b856621
+ ['FOOBAR', 'foo', -1, 0, 0, 0.22, 1.0e-15, 12, 18, 22, 255, 7, 9, [], {}]
b856621
  ['aa', 'bb']
b856621
  ['aa', 'bb']
b856621
  ['', 'aa', 'bb', '']
b856621
*** ../vim-7.4.340/src/version.c	2014-06-25 15:02:29.250400570 +0200
b856621
--- src/version.c	2014-06-25 16:46:45.438637250 +0200
b856621
***************
b856621
*** 736,737 ****
b856621
--- 736,739 ----
b856621
  {   /* Add new patch number below this line */
b856621
+ /**/
b856621
+     341,
b856621
  /**/
b856621
b856621
-- 
b856621
We do not stumble over mountains, but over molehills.
b856621
				Confucius
b856621
b856621
 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
b856621
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
b856621
\\\  an exciting new programming language -- http://www.Zimbu.org        ///
b856621
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///