Mercurial > vim
comparison src/eval.c @ 7689:20dc2763a3b9 v7.4.1143
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Author: Bram Moolenaar <Bram@vim.org>
Date: Tue Jan 19 23:36:15 2016 +0100
patch 7.4.1143
Problem: Can't sort on floating point numbers.
Solution: Add the "f" flag to ":sort". (Alex Jakushev) Also add the "f"
flag to sort().
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Tue, 19 Jan 2016 23:45:05 +0100 |
parents | ec434c82f72c |
children | 1b9a1c10806b |
comparison
equal
deleted
inserted
replaced
7688:f56cebad5ba2 | 7689:20dc2763a3b9 |
---|---|
807 static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose)); | 807 static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose)); |
808 static typval_T *alloc_tv __ARGS((void)); | 808 static typval_T *alloc_tv __ARGS((void)); |
809 static typval_T *alloc_string_tv __ARGS((char_u *string)); | 809 static typval_T *alloc_string_tv __ARGS((char_u *string)); |
810 static void init_tv __ARGS((typval_T *varp)); | 810 static void init_tv __ARGS((typval_T *varp)); |
811 static long get_tv_number __ARGS((typval_T *varp)); | 811 static long get_tv_number __ARGS((typval_T *varp)); |
812 #ifdef FEAT_FLOAT | |
813 static float_T get_tv_float(typval_T *varp); | |
814 #endif | |
812 static linenr_T get_tv_lnum __ARGS((typval_T *argvars)); | 815 static linenr_T get_tv_lnum __ARGS((typval_T *argvars)); |
813 static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf)); | 816 static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf)); |
814 static char_u *get_tv_string __ARGS((typval_T *varp)); | 817 static char_u *get_tv_string __ARGS((typval_T *varp)); |
815 static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf)); | 818 static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf)); |
816 static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf)); | 819 static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf)); |
18141 } sortItem_T; | 18144 } sortItem_T; |
18142 | 18145 |
18143 static int item_compare_ic; | 18146 static int item_compare_ic; |
18144 static int item_compare_numeric; | 18147 static int item_compare_numeric; |
18145 static int item_compare_numbers; | 18148 static int item_compare_numbers; |
18149 #ifdef FEAT_FLOAT | |
18150 static int item_compare_float; | |
18151 #endif | |
18146 static char_u *item_compare_func; | 18152 static char_u *item_compare_func; |
18147 static dict_T *item_compare_selfdict; | 18153 static dict_T *item_compare_selfdict; |
18148 static int item_compare_func_err; | 18154 static int item_compare_func_err; |
18149 static int item_compare_keep_zero; | 18155 static int item_compare_keep_zero; |
18150 static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort)); | 18156 static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort)); |
18179 long v1 = get_tv_number(tv1); | 18185 long v1 = get_tv_number(tv1); |
18180 long v2 = get_tv_number(tv2); | 18186 long v2 = get_tv_number(tv2); |
18181 | 18187 |
18182 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1; | 18188 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1; |
18183 } | 18189 } |
18190 | |
18191 #ifdef FEAT_FLOAT | |
18192 if (item_compare_float) | |
18193 { | |
18194 float_T v1 = get_tv_float(tv1); | |
18195 float_T v2 = get_tv_float(tv2); | |
18196 | |
18197 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1; | |
18198 } | |
18199 #endif | |
18184 | 18200 |
18185 /* tv2string() puts quotes around a string and allocates memory. Don't do | 18201 /* tv2string() puts quotes around a string and allocates memory. Don't do |
18186 * that for string variables. Use a single quote when comparing with a | 18202 * that for string variables. Use a single quote when comparing with a |
18187 * non-string to do what the docs promise. */ | 18203 * non-string to do what the docs promise. */ |
18188 if (tv1->v_type == VAR_STRING) | 18204 if (tv1->v_type == VAR_STRING) |
18314 return; /* short list sorts pretty quickly */ | 18330 return; /* short list sorts pretty quickly */ |
18315 | 18331 |
18316 item_compare_ic = FALSE; | 18332 item_compare_ic = FALSE; |
18317 item_compare_numeric = FALSE; | 18333 item_compare_numeric = FALSE; |
18318 item_compare_numbers = FALSE; | 18334 item_compare_numbers = FALSE; |
18335 #ifdef FEAT_FLOAT | |
18336 item_compare_float = FALSE; | |
18337 #endif | |
18319 item_compare_func = NULL; | 18338 item_compare_func = NULL; |
18320 item_compare_selfdict = NULL; | 18339 item_compare_selfdict = NULL; |
18321 if (argvars[1].v_type != VAR_UNKNOWN) | 18340 if (argvars[1].v_type != VAR_UNKNOWN) |
18322 { | 18341 { |
18323 /* optional second argument: {func} */ | 18342 /* optional second argument: {func} */ |
18344 else if (STRCMP(item_compare_func, "N") == 0) | 18363 else if (STRCMP(item_compare_func, "N") == 0) |
18345 { | 18364 { |
18346 item_compare_func = NULL; | 18365 item_compare_func = NULL; |
18347 item_compare_numbers = TRUE; | 18366 item_compare_numbers = TRUE; |
18348 } | 18367 } |
18368 #ifdef FEAT_FLOAT | |
18369 else if (STRCMP(item_compare_func, "f") == 0) | |
18370 { | |
18371 item_compare_func = NULL; | |
18372 item_compare_float = TRUE; | |
18373 } | |
18374 #endif | |
18349 else if (STRCMP(item_compare_func, "i") == 0) | 18375 else if (STRCMP(item_compare_func, "i") == 0) |
18350 { | 18376 { |
18351 item_compare_func = NULL; | 18377 item_compare_func = NULL; |
18352 item_compare_ic = TRUE; | 18378 item_compare_ic = TRUE; |
18353 } | 18379 } |
21610 n = -1; | 21636 n = -1; |
21611 else | 21637 else |
21612 *denote = TRUE; | 21638 *denote = TRUE; |
21613 return n; | 21639 return n; |
21614 } | 21640 } |
21641 | |
21642 #ifdef FEAT_FLOAT | |
21643 static float_T | |
21644 get_tv_float(varp) | |
21645 typval_T *varp; | |
21646 { | |
21647 switch (varp->v_type) | |
21648 { | |
21649 case VAR_NUMBER: | |
21650 return (float_T)(varp->vval.v_number); | |
21651 #ifdef FEAT_FLOAT | |
21652 case VAR_FLOAT: | |
21653 return varp->vval.v_float; | |
21654 break; | |
21655 #endif | |
21656 case VAR_FUNC: | |
21657 EMSG(_("E891: Using a Funcref as a Float")); | |
21658 break; | |
21659 case VAR_STRING: | |
21660 EMSG(_("E892: Using a String as a Float")); | |
21661 break; | |
21662 case VAR_LIST: | |
21663 EMSG(_("E893: Using a List as a Float")); | |
21664 break; | |
21665 case VAR_DICT: | |
21666 EMSG(_("E894: Using a Dictionary as a Float")); | |
21667 break; | |
21668 default: | |
21669 EMSG2(_(e_intern2), "get_tv_float()"); | |
21670 break; | |
21671 } | |
21672 return 0; | |
21673 } | |
21674 #endif | |
21615 | 21675 |
21616 /* | 21676 /* |
21617 * Get the lnum from the first argument. | 21677 * Get the lnum from the first argument. |
21618 * Also accepts ".", "$", etc., but that only works for the current buffer. | 21678 * Also accepts ".", "$", etc., but that only works for the current buffer. |
21619 * Returns -1 on error. | 21679 * Returns -1 on error. |