Mercurial > vim
comparison src/eval.c @ 6001:adc4a84f72eb v7.4.341
updated for version 7.4.341
Problem: sort() doesn't handle numbers well.
Solution: Add an argument to specify sorting on numbers. (Christian Brabandt)
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Wed, 25 Jun 2014 17:31:09 +0200 |
parents | f9fa2e506b9f |
children | f9ec944e4474 |
comparison
equal
deleted
inserted
replaced
6000:0cae257f69ff | 6001:adc4a84f72eb |
---|---|
17328 _RTLENTRYF | 17328 _RTLENTRYF |
17329 #endif | 17329 #endif |
17330 item_compare2 __ARGS((const void *s1, const void *s2)); | 17330 item_compare2 __ARGS((const void *s1, const void *s2)); |
17331 | 17331 |
17332 static int item_compare_ic; | 17332 static int item_compare_ic; |
17333 static int item_compare_numeric; | |
17333 static char_u *item_compare_func; | 17334 static char_u *item_compare_func; |
17334 static dict_T *item_compare_selfdict; | 17335 static dict_T *item_compare_selfdict; |
17335 static int item_compare_func_err; | 17336 static int item_compare_func_err; |
17336 static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort)); | 17337 static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort)); |
17337 #define ITEM_COMPARE_FAIL 999 | 17338 #define ITEM_COMPARE_FAIL 999 |
17357 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0); | 17358 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0); |
17358 if (p1 == NULL) | 17359 if (p1 == NULL) |
17359 p1 = (char_u *)""; | 17360 p1 = (char_u *)""; |
17360 if (p2 == NULL) | 17361 if (p2 == NULL) |
17361 p2 = (char_u *)""; | 17362 p2 = (char_u *)""; |
17362 if (item_compare_ic) | 17363 if (!item_compare_numeric) |
17363 res = STRICMP(p1, p2); | 17364 { |
17365 if (item_compare_ic) | |
17366 res = STRICMP(p1, p2); | |
17367 else | |
17368 res = STRCMP(p1, p2); | |
17369 } | |
17364 else | 17370 else |
17365 res = STRCMP(p1, p2); | 17371 { |
17372 double n1, n2; | |
17373 n1 = strtod((char *)p1, (char **)&p1); | |
17374 n2 = strtod((char *)p2, (char **)&p2); | |
17375 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1; | |
17376 } | |
17366 vim_free(tofree1); | 17377 vim_free(tofree1); |
17367 vim_free(tofree2); | 17378 vim_free(tofree2); |
17368 return res; | 17379 return res; |
17369 } | 17380 } |
17370 | 17381 |
17437 len = list_len(l); | 17448 len = list_len(l); |
17438 if (len <= 1) | 17449 if (len <= 1) |
17439 return; /* short list sorts pretty quickly */ | 17450 return; /* short list sorts pretty quickly */ |
17440 | 17451 |
17441 item_compare_ic = FALSE; | 17452 item_compare_ic = FALSE; |
17453 item_compare_numeric = FALSE; | |
17442 item_compare_func = NULL; | 17454 item_compare_func = NULL; |
17443 item_compare_selfdict = NULL; | 17455 item_compare_selfdict = NULL; |
17444 if (argvars[1].v_type != VAR_UNKNOWN) | 17456 if (argvars[1].v_type != VAR_UNKNOWN) |
17445 { | 17457 { |
17446 /* optional second argument: {func} */ | 17458 /* optional second argument: {func} */ |
17455 return; /* type error; errmsg already given */ | 17467 return; /* type error; errmsg already given */ |
17456 if (i == 1) | 17468 if (i == 1) |
17457 item_compare_ic = TRUE; | 17469 item_compare_ic = TRUE; |
17458 else | 17470 else |
17459 item_compare_func = get_tv_string(&argvars[1]); | 17471 item_compare_func = get_tv_string(&argvars[1]); |
17472 if (item_compare_func != NULL) | |
17473 { | |
17474 if (STRCMP(item_compare_func, "n") == 0) | |
17475 { | |
17476 item_compare_func = NULL; | |
17477 item_compare_numeric = TRUE; | |
17478 } | |
17479 else if (STRCMP(item_compare_func, "i") == 0) | |
17480 { | |
17481 item_compare_func = NULL; | |
17482 item_compare_ic = TRUE; | |
17483 } | |
17484 } | |
17460 } | 17485 } |
17461 | 17486 |
17462 if (argvars[2].v_type != VAR_UNKNOWN) | 17487 if (argvars[2].v_type != VAR_UNKNOWN) |
17463 { | 17488 { |
17464 /* optional third argument: {dict} */ | 17489 /* optional third argument: {dict} */ |