diff options
author | Justin M. Keyes <justinkz@gmail.com> | 2014-09-15 14:08:55 -0400 |
---|---|---|
committer | Justin M. Keyes <justinkz@gmail.com> | 2014-09-15 14:08:55 -0400 |
commit | 3b7b79e37d3479633701ad1d2ea4912d5c17c93b (patch) | |
tree | 4ac491dab0f51bd8f2560f005fa02e23206494a9 /src | |
parent | 874296331b70f233e0fbd292e459b84d81d8349c (diff) | |
parent | eeef120c86bca2dd69a8392f6aee73ba438bf392 (diff) | |
download | rneovim-3b7b79e37d3479633701ad1d2ea4912d5c17c93b.tar.gz rneovim-3b7b79e37d3479633701ad1d2ea4912d5c17c93b.tar.bz2 rneovim-3b7b79e37d3479633701ad1d2ea4912d5c17c93b.zip |
Merge pull request #1140 from atwupack/vp-7.4.341
vim-patch:7.4.341, 7.4.347, 7.4.351, 7.4.358
Diffstat (limited to 'src')
-rw-r--r-- | src/nvim/eval.c | 90 | ||||
-rw-r--r-- | src/nvim/testdir/test55.in | 6 | ||||
-rw-r--r-- | src/nvim/testdir/test55.ok | 4 | ||||
-rw-r--r-- | src/nvim/version.c | 8 |
4 files changed, 84 insertions, 24 deletions
diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 00033e3866..7793f5040c 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -13386,11 +13386,18 @@ static void f_sinh(typval_T *argvars, typval_T *rettv) rettv->vval.v_float = 0.0; } +/// struct used in the array that's given to qsort() +typedef struct { + listitem_T *item; + int idx; +} sortItem_T; static int item_compare_ic; +static bool item_compare_numeric; static char_u *item_compare_func; static dict_T *item_compare_selfdict; static int item_compare_func_err; +static bool item_compare_keep_zero; #define ITEM_COMPARE_FAIL 999 /* @@ -13398,22 +13405,40 @@ static int item_compare_func_err; */ static int item_compare(const void *s1, const void *s2) { + sortItem_T *si1, *si2; char_u *p1, *p2; char_u *tofree1, *tofree2; int res; char_u numbuf1[NUMBUFLEN]; char_u numbuf2[NUMBUFLEN]; - p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0); - p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0); + si1 = (sortItem_T *)s1; + si2 = (sortItem_T *)s2; + p1 = tv2string(&si1->item->li_tv, &tofree1, numbuf1, 0); + p2 = tv2string(&si2->item->li_tv, &tofree2, numbuf2, 0); if (p1 == NULL) p1 = (char_u *)""; if (p2 == NULL) p2 = (char_u *)""; - if (item_compare_ic) - res = STRICMP(p1, p2); - else - res = STRCMP(p1, p2); + if (!item_compare_numeric) { + if (item_compare_ic) { + res = STRICMP(p1, p2); + } else { + res = STRCMP(p1, p2); + } + } else { + double n1, n2; + n1 = strtod((char *)p1, (char **)&p1); + n2 = strtod((char *)p2, (char **)&p2); + res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1; + } + + // When the result would be zero, compare the pointers themselves. Makes + // the sort stable. + if (res == 0 && !item_compare_keep_zero) { + res = si1->idx > si2->idx ? 1 : -1; + } + free(tofree1); free(tofree2); return res; @@ -13421,6 +13446,7 @@ static int item_compare(const void *s1, const void *s2) static int item_compare2(const void *s1, const void *s2) { + sortItem_T *si1, *si2; int res; typval_T rettv; typval_T argv[3]; @@ -13430,10 +13456,13 @@ static int item_compare2(const void *s1, const void *s2) if (item_compare_func_err) return 0; - /* copy the values. This is needed to be able to set v_lock to VAR_FIXED - * in the copy without changing the original list items. */ - copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]); - copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]); + si1 = (sortItem_T *)s1; + si2 = (sortItem_T *)s2; + + // Copy the values. This is needed to be able to set v_lock to VAR_FIXED + // in the copy without changing the original list items. + copy_tv(&si1->item->li_tv, &argv[0]); + copy_tv(&si2->item->li_tv, &argv[1]); rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */ res = call_func(item_compare_func, (int)STRLEN(item_compare_func), @@ -13449,6 +13478,13 @@ static int item_compare2(const void *s1, const void *s2) if (item_compare_func_err) res = ITEM_COMPARE_FAIL; /* return value has wrong type */ clear_tv(&rettv); + + // When the result would be zero, compare the pointers themselves. Makes + // the sort stable. + if (res == 0 && !item_compare_keep_zero) { + res = si1->idx > si2->idx ? 1 : -1; + } + return res; } @@ -13459,7 +13495,7 @@ static void do_sort_uniq(typval_T *argvars, typval_T *rettv, bool sort) { list_T *l; listitem_T *li; - listitem_T **ptrs; + sortItem_T *ptrs; long len; long i; @@ -13480,6 +13516,7 @@ static void do_sort_uniq(typval_T *argvars, typval_T *rettv, bool sort) return; /* short list sorts pretty quickly */ item_compare_ic = FALSE; + item_compare_numeric = false; item_compare_func = NULL; item_compare_selfdict = NULL; @@ -13497,6 +13534,15 @@ static void do_sort_uniq(typval_T *argvars, typval_T *rettv, bool sort) item_compare_ic = TRUE; else item_compare_func = get_tv_string(&argvars[1]); + if (item_compare_func != NULL) { + if (STRCMP(item_compare_func, "n") == 0) { + item_compare_func = NULL; + item_compare_numeric = true; + } else if (STRCMP(item_compare_func, "i") == 0) { + item_compare_func = NULL; + item_compare_ic = TRUE; + } + } } if (argvars[2].v_type != VAR_UNKNOWN) { @@ -13510,23 +13556,26 @@ static void do_sort_uniq(typval_T *argvars, typval_T *rettv, bool sort) } /* Make an array with each entry pointing to an item in the List. */ - ptrs = xmalloc((size_t)(len * sizeof (listitem_T *))); + ptrs = xmalloc((size_t)(len * sizeof (sortItem_T))); i = 0; if (sort) { // sort(): ptrs will be the list to sort. for (li = l->lv_first; li != NULL; li = li->li_next) { - ptrs[i++] = li; + ptrs[i].item = li; + ptrs[i].idx = i; + i++; } item_compare_func_err = FALSE; + item_compare_keep_zero = false; // Test the compare function. if (item_compare_func != NULL && item_compare2(&ptrs[0], &ptrs[1]) == ITEM_COMPARE_FAIL) { EMSG(_("E702: Sort compare function failed")); } else { // Sort the array with item pointers. - qsort(ptrs, (size_t)len, sizeof (listitem_T *), + qsort(ptrs, (size_t)len, sizeof (sortItem_T), item_compare_func == NULL ? item_compare : item_compare2); if (!item_compare_func_err) { @@ -13537,7 +13586,7 @@ static void do_sort_uniq(typval_T *argvars, typval_T *rettv, bool sort) l->lv_len = 0; for (i = 0; i < len; i++) { - list_append(l, ptrs[i]); + list_append(l, ptrs[i].item); } } } @@ -13546,11 +13595,12 @@ static void do_sort_uniq(typval_T *argvars, typval_T *rettv, bool sort) // f_uniq(): ptrs will be a stack of items to remove. item_compare_func_err = FALSE; + item_compare_keep_zero = true; item_compare_func_ptr = item_compare_func ? item_compare2 : item_compare; for (li = l->lv_first; li != NULL && li->li_next != NULL; li = li->li_next) { if (item_compare_func_ptr(&li, &li->li_next) == 0) { - ptrs[i++] = li; + ptrs[i++].item = li; } if (item_compare_func_err) { EMSG(_("E882: Uniq compare function failed")); @@ -13560,12 +13610,12 @@ static void do_sort_uniq(typval_T *argvars, typval_T *rettv, bool sort) if (!item_compare_func_err) { while (--i >= 0) { - li = ptrs[i]->li_next; - ptrs[i]->li_next = li->li_next; + li = ptrs[i].item->li_next; + ptrs[i].item->li_next = li->li_next; if (li->li_next != NULL) { - li->li_next->li_prev = ptrs[i]; + li->li_next->li_prev = ptrs[i].item; } else { - l->lv_last = ptrs[i]; + l->lv_last = ptrs[i].item; } list_fix_watch(l, li); listitem_free(li); diff --git a/src/nvim/testdir/test55.in b/src/nvim/testdir/test55.in index 85a8063774..8e073f30f2 100644 --- a/src/nvim/testdir/test55.in +++ b/src/nvim/testdir/test55.in @@ -332,6 +332,12 @@ let l = [0, 1, 2, 3] :$put =string(reverse(sort(l))) :$put =string(sort(reverse(sort(l)))) :$put =string(uniq(sort(l))) +:let l=[7, 9, 'one', 18, 12, 22, 'two', 10.0e-16, -1, 'three', 0xff, 0.22, 'four'] +:$put =string(sort(copy(l), 'n')) +:let l=[7, 9, 18, 12, 22, 10.0e-16, -1, 0xff, 0, -0, 0.22, 'bar', 'BAR', 'Bar', 'Foo', 'FOO', 'foo', 'FOOBAR', {}, []] +:$put =string(sort(copy(l), 1)) +:$put =string(sort(copy(l), 'i')) +:$put =string(sort(copy(l))) :" :" splitting a string to a List :$put =string(split(' aa bb ')) diff --git a/src/nvim/testdir/test55.ok b/src/nvim/testdir/test55.ok index be476e8e93..dfd8060db7 100644 --- a/src/nvim/testdir/test55.ok +++ b/src/nvim/testdir/test55.ok @@ -101,6 +101,10 @@ caught a:000[3] [[0, 1, 2], [0, 1, 2], 4, 2, 2, 1.5, 'xaaa', 'x8', 'foo6', 'foo', 'foo', 'A11', '-0'] ['-0', 'A11', 'foo', 'foo', 'foo6', 'x8', 'xaaa', 1.5, 2, 2, 4, [0, 1, 2], [0, 1, 2]] ['-0', 'A11', 'foo', 'foo6', 'x8', 'xaaa', 1.5, 2, 4, [0, 1, 2]] +[-1, 'one', 'two', 'three', 'four', 1.0e-15, 0.22, 7, 9, 12, 18, 22, 255] +['bar', 'BAR', 'Bar', 'Foo', 'FOO', 'foo', 'FOOBAR', -1, 0, 0, 0.22, 1.0e-15, 12, 18, 22, 255, 7, 9, [], {}] +['bar', 'BAR', 'Bar', 'Foo', 'FOO', 'foo', 'FOOBAR', -1, 0, 0, 0.22, 1.0e-15, 12, 18, 22, 255, 7, 9, [], {}] +['BAR', 'Bar', 'FOO', 'FOOBAR', 'Foo', 'bar', 'foo', -1, 0, 0, 0.22, 1.0e-15, 12, 18, 22, 255, 7, 9, [], {}] ['aa', 'bb'] ['aa', 'bb'] ['', 'aa', 'bb', ''] diff --git a/src/nvim/version.c b/src/nvim/version.c index 34dd04ee13..b32da8937e 100644 --- a/src/nvim/version.c +++ b/src/nvim/version.c @@ -237,24 +237,24 @@ static int included_patches[] = { //361, //360, //359, - //358, + 358, 357, //356 NA //355, //354 NA 353, 352, - //351, + 351, //350, 349, 348, - //347, + 347, 346, 345, 344, 343, //342 NA - //341, + 341, //340 NA 339, 338, |