comparison src/eval.c @ 7291:6ffc75d807bd v7.4.951

commit https://github.com/vim/vim/commit/b00da1d6d1655cb6e415f84ecc3be5ff3b790811 Author: Bram Moolenaar <Bram@vim.org> Date: Thu Dec 3 16:33:12 2015 +0100 patch 7.4.951 Problem: Sorting number strings does not work as expected. (Luc Hermitte) Solution: Add the 'N" argument to sort()
author Christian Brabandt <cb@256bit.org>
date Thu, 03 Dec 2015 16:45:05 +0100
parents 32efe489afc5
children 38e4f3a246eb
comparison
equal deleted inserted replaced
7290:e35d237ef46c 7291:6ffc75d807bd
17926 int idx; 17926 int idx;
17927 } sortItem_T; 17927 } sortItem_T;
17928 17928
17929 static int item_compare_ic; 17929 static int item_compare_ic;
17930 static int item_compare_numeric; 17930 static int item_compare_numeric;
17931 static int item_compare_numbers;
17931 static char_u *item_compare_func; 17932 static char_u *item_compare_func;
17932 static dict_T *item_compare_selfdict; 17933 static dict_T *item_compare_selfdict;
17933 static int item_compare_func_err; 17934 static int item_compare_func_err;
17934 static int item_compare_keep_zero; 17935 static int item_compare_keep_zero;
17935 static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort)); 17936 static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
17956 17957
17957 si1 = (sortItem_T *)s1; 17958 si1 = (sortItem_T *)s1;
17958 si2 = (sortItem_T *)s2; 17959 si2 = (sortItem_T *)s2;
17959 tv1 = &si1->item->li_tv; 17960 tv1 = &si1->item->li_tv;
17960 tv2 = &si2->item->li_tv; 17961 tv2 = &si2->item->li_tv;
17962
17963 if (item_compare_numbers)
17964 {
17965 long v1 = get_tv_number(tv1);
17966 long v2 = get_tv_number(tv2);
17967
17968 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
17969 }
17970
17961 /* tv2string() puts quotes around a string and allocates memory. Don't do 17971 /* tv2string() puts quotes around a string and allocates memory. Don't do
17962 * that for string variables. Use a single quote when comparing with a 17972 * that for string variables. Use a single quote when comparing with a
17963 * non-string to do what the docs promise. */ 17973 * non-string to do what the docs promise. */
17964 if (tv1->v_type == VAR_STRING) 17974 if (tv1->v_type == VAR_STRING)
17965 { 17975 {
18089 if (len <= 1) 18099 if (len <= 1)
18090 return; /* short list sorts pretty quickly */ 18100 return; /* short list sorts pretty quickly */
18091 18101
18092 item_compare_ic = FALSE; 18102 item_compare_ic = FALSE;
18093 item_compare_numeric = FALSE; 18103 item_compare_numeric = FALSE;
18104 item_compare_numbers = FALSE;
18094 item_compare_func = NULL; 18105 item_compare_func = NULL;
18095 item_compare_selfdict = NULL; 18106 item_compare_selfdict = NULL;
18096 if (argvars[1].v_type != VAR_UNKNOWN) 18107 if (argvars[1].v_type != VAR_UNKNOWN)
18097 { 18108 {
18098 /* optional second argument: {func} */ 18109 /* optional second argument: {func} */
18113 { 18124 {
18114 if (STRCMP(item_compare_func, "n") == 0) 18125 if (STRCMP(item_compare_func, "n") == 0)
18115 { 18126 {
18116 item_compare_func = NULL; 18127 item_compare_func = NULL;
18117 item_compare_numeric = TRUE; 18128 item_compare_numeric = TRUE;
18129 }
18130 else if (STRCMP(item_compare_func, "N") == 0)
18131 {
18132 item_compare_func = NULL;
18133 item_compare_numbers = TRUE;
18118 } 18134 }
18119 else if (STRCMP(item_compare_func, "i") == 0) 18135 else if (STRCMP(item_compare_func, "i") == 0)
18120 { 18136 {
18121 item_compare_func = NULL; 18137 item_compare_func = NULL;
18122 item_compare_ic = TRUE; 18138 item_compare_ic = TRUE;