mirror of
https://github.com/vim/vim.git
synced 2025-09-24 03:44:06 -04:00
patch 8.1.0959: sorting large numbers is not tested
Problem: Sorting large numbers is not tested and does not work properly. Solution: Add test. Fix comparing lines with and without a number. (Dominique Pelle, closes #4017)
This commit is contained in:
@@ -296,16 +296,20 @@ static int sort_abort; /* flag to indicate if sorting has been interrupted */
|
|||||||
/* Struct to store info to be sorted. */
|
/* Struct to store info to be sorted. */
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
linenr_T lnum; /* line number */
|
linenr_T lnum; // line number
|
||||||
union {
|
union {
|
||||||
struct
|
struct
|
||||||
{
|
{
|
||||||
varnumber_T start_col_nr; /* starting column number */
|
varnumber_T start_col_nr; // starting column number
|
||||||
varnumber_T end_col_nr; /* ending column number */
|
varnumber_T end_col_nr; // ending column number
|
||||||
} line;
|
} line;
|
||||||
varnumber_T value; /* value if sorting by integer */
|
struct
|
||||||
|
{
|
||||||
|
varnumber_T value; // value if sorting by integer
|
||||||
|
int is_number; // TRUE when line contains a number
|
||||||
|
} num;
|
||||||
#ifdef FEAT_FLOAT
|
#ifdef FEAT_FLOAT
|
||||||
float_T value_flt; /* value if sorting by float */
|
float_T value_flt; // value if sorting by float
|
||||||
#endif
|
#endif
|
||||||
} st_u;
|
} st_u;
|
||||||
} sorti_T;
|
} sorti_T;
|
||||||
@@ -335,11 +339,14 @@ sort_compare(const void *s1, const void *s2)
|
|||||||
if (got_int)
|
if (got_int)
|
||||||
sort_abort = TRUE;
|
sort_abort = TRUE;
|
||||||
|
|
||||||
/* When sorting numbers "start_col_nr" is the number, not the column
|
|
||||||
* number. */
|
|
||||||
if (sort_nr)
|
if (sort_nr)
|
||||||
result = l1.st_u.value == l2.st_u.value ? 0
|
{
|
||||||
: l1.st_u.value > l2.st_u.value ? 1 : -1;
|
if (l1.st_u.num.is_number != l2.st_u.num.is_number)
|
||||||
|
result = l1.st_u.num.is_number - l2.st_u.num.is_number;
|
||||||
|
else
|
||||||
|
result = l1.st_u.num.value == l2.st_u.num.value ? 0
|
||||||
|
: l1.st_u.num.value > l2.st_u.num.value ? 1 : -1;
|
||||||
|
}
|
||||||
#ifdef FEAT_FLOAT
|
#ifdef FEAT_FLOAT
|
||||||
else if (sort_flt)
|
else if (sort_flt)
|
||||||
result = l1.st_u.value_flt == l2.st_u.value_flt ? 0
|
result = l1.st_u.value_flt == l2.st_u.value_flt ? 0
|
||||||
@@ -553,11 +560,17 @@ ex_sort(exarg_T *eap)
|
|||||||
if (s > p && s[-1] == '-')
|
if (s > p && s[-1] == '-')
|
||||||
--s; /* include preceding negative sign */
|
--s; /* include preceding negative sign */
|
||||||
if (*s == NUL)
|
if (*s == NUL)
|
||||||
/* empty line should sort before any number */
|
{
|
||||||
nrs[lnum - eap->line1].st_u.value = -MAXLNUM;
|
/* line without number should sort before any number */
|
||||||
|
nrs[lnum - eap->line1].st_u.num.is_number = FALSE;
|
||||||
|
nrs[lnum - eap->line1].st_u.num.value = 0;
|
||||||
|
}
|
||||||
else
|
else
|
||||||
|
{
|
||||||
|
nrs[lnum - eap->line1].st_u.num.is_number = TRUE;
|
||||||
vim_str2nr(s, NULL, NULL, sort_what,
|
vim_str2nr(s, NULL, NULL, sort_what,
|
||||||
&nrs[lnum - eap->line1].st_u.value, NULL, 0);
|
&nrs[lnum - eap->line1].st_u.num.value, NULL, 0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#ifdef FEAT_FLOAT
|
#ifdef FEAT_FLOAT
|
||||||
else
|
else
|
||||||
|
@@ -1222,6 +1222,77 @@ func Test_sort_cmd()
|
|||||||
enew!
|
enew!
|
||||||
endfunc
|
endfunc
|
||||||
|
|
||||||
|
func Test_sort_large_num()
|
||||||
|
new
|
||||||
|
a
|
||||||
|
-2147483648
|
||||||
|
-2147483647
|
||||||
|
|
||||||
|
-1
|
||||||
|
0
|
||||||
|
1
|
||||||
|
-2147483646
|
||||||
|
2147483646
|
||||||
|
2147483647
|
||||||
|
2147483647
|
||||||
|
-2147483648
|
||||||
|
abc
|
||||||
|
|
||||||
|
.
|
||||||
|
" Numerical sort. Non-numeric lines are ordered before numerical lines.
|
||||||
|
" Ordering of non-numerical is stable.
|
||||||
|
sort n
|
||||||
|
call assert_equal(['',
|
||||||
|
\ 'abc',
|
||||||
|
\ '',
|
||||||
|
\ '-2147483648',
|
||||||
|
\ '-2147483648',
|
||||||
|
\ '-2147483647',
|
||||||
|
\ '-2147483646',
|
||||||
|
\ '-1',
|
||||||
|
\ '0',
|
||||||
|
\ '1',
|
||||||
|
\ '2147483646',
|
||||||
|
\ '2147483647',
|
||||||
|
\ '2147483647'], getline(1, '$'))
|
||||||
|
bwipe!
|
||||||
|
|
||||||
|
if has('num64')
|
||||||
|
new
|
||||||
|
a
|
||||||
|
-9223372036854775808
|
||||||
|
-9223372036854775807
|
||||||
|
|
||||||
|
-1
|
||||||
|
0
|
||||||
|
1
|
||||||
|
-9223372036854775806
|
||||||
|
9223372036854775806
|
||||||
|
9223372036854775807
|
||||||
|
9223372036854775807
|
||||||
|
-9223372036854775808
|
||||||
|
abc
|
||||||
|
|
||||||
|
.
|
||||||
|
sort n
|
||||||
|
call assert_equal(['',
|
||||||
|
\ 'abc',
|
||||||
|
\ '',
|
||||||
|
\ '-9223372036854775808',
|
||||||
|
\ '-9223372036854775808',
|
||||||
|
\ '-9223372036854775807',
|
||||||
|
\ '-9223372036854775806',
|
||||||
|
\ '-1',
|
||||||
|
\ '0',
|
||||||
|
\ '1',
|
||||||
|
\ '9223372036854775806',
|
||||||
|
\ '9223372036854775807',
|
||||||
|
\ '9223372036854775807'], getline(1, '$'))
|
||||||
|
bwipe!
|
||||||
|
endif
|
||||||
|
endfunc
|
||||||
|
|
||||||
|
|
||||||
func Test_sort_cmd_report()
|
func Test_sort_cmd_report()
|
||||||
enew!
|
enew!
|
||||||
call append(0, repeat([1], 3) + repeat([2], 3) + repeat([3], 3))
|
call append(0, repeat([1], 3) + repeat([2], 3) + repeat([3], 3))
|
||||||
|
@@ -779,6 +779,8 @@ static char *(features[]) =
|
|||||||
|
|
||||||
static int included_patches[] =
|
static int included_patches[] =
|
||||||
{ /* Add new patch number below this line */
|
{ /* Add new patch number below this line */
|
||||||
|
/**/
|
||||||
|
959,
|
||||||
/**/
|
/**/
|
||||||
958,
|
958,
|
||||||
/**/
|
/**/
|
||||||
|
Reference in New Issue
Block a user