patch 9.1.1623: Buffer menu does not handle unicode names correctly
Problem: Buffer menu does not handle unicode names correctly
(after v9.1.1622)
Solution: Fix the BMHash() function (Yee Cheng Chin)
The Buffers menu uses a BMHash() function to generate a sortable number
to be used for the menu index. It used a naive (and incorrect) way of
encoding multiple ASCII values into a single integer, but assumes each
character to be only in the ASCII 32-96 range. This means if we use
non-ASCII file names (e.g. Unicode values like CJK or emojis) we get
integer underflow and overflow, causing the menu index to wrap around.
Vim's GUI implementations internally use a signed 32-bit integer for the
`gui_mch_add_menu_item()` function and so we need to make sure the menu
index is in the (0, 2^31-1) range.
To do this, if the file name starts with a non-ASCII value, we just use
the first character's value and set the high bit so it sorts after the
other ASCII ones. Otherwise, we just take the first 5 characters, and
use 5 bit for each character to encode a 30-bit number that can be
sorted.
This means Unicode file names won't be sorted beyond the first
character. This is likely going to be fine as there are lots of ways to
query buffers.
related: #17403
closes: #17928
Signed-off-by: Yee Cheng Chin <ychin.git@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
committed by
Christian Brabandt
parent
cda0d17f59
commit
8f9de4991e
@ -1767,4 +1767,37 @@ func Test_CursorHold_not_triggered_at_startup()
|
||||
call assert_equal(['g:cursorhold_triggered=0'], found)
|
||||
endfunc
|
||||
|
||||
" Test that Buffers menu generates the correct index for different buffer
|
||||
" names for sorting.
|
||||
func Test_Buffers_Menu()
|
||||
doautocmd LoadBufferMenu VimEnter
|
||||
|
||||
" Non-ASCII characters only use the first character as idx
|
||||
let idx_emoji = or(char2nr('😑'), 0x40000000)
|
||||
|
||||
" Only first five letters are used for alphanumeric:
|
||||
" ('a'-32) << 24 + ('b'-32) << 18 + ('c'-32) << 12 + ('d'-32) << 6 + ('e'-32)
|
||||
let idx_abcde = 0x218A3925
|
||||
" ('a'-32) << 24 + ('b'-32) << 18 + ('c'-32) << 12 + ('d'-32) << 6 + ('f'-32)
|
||||
let idx_abcdf = 0x218A3926
|
||||
" ('a'-32) << 24 + 63 (clamped) << 18 + ('c'-32) << 12 + ('d'-32) << 6 + ('e'-32)
|
||||
let idx_a_emoji_cde = 0x21FE3925
|
||||
|
||||
let names = ['😑', '😑1', '😑2', 'abcde', 'abcdefghi', 'abcdf', 'a😑cde']
|
||||
let indices = [idx_emoji, idx_emoji, idx_emoji, idx_abcde, idx_abcde, idx_abcdf, idx_a_emoji_cde]
|
||||
for i in range(len(names))
|
||||
let name = names[i]
|
||||
let idx = indices[i]
|
||||
exe ':badd ' .. name
|
||||
let nr = bufnr('$')
|
||||
|
||||
let cmd = printf(':amenu Buffers.%s\ (%d)', name, nr)
|
||||
let menu = split(execute(cmd), '\n')[1]
|
||||
call assert_inrange(0, 0x7FFFFFFF, idx)
|
||||
call assert_match('^' .. idx .. ' '.. name, menu)
|
||||
endfor
|
||||
|
||||
%bw!
|
||||
endfunc
|
||||
|
||||
" vim: shiftwidth=2 sts=2 expandtab
|
||||
|
||||
@ -719,6 +719,8 @@ static char *(features[]) =
|
||||
|
||||
static int included_patches[] =
|
||||
{ /* Add new patch number below this line */
|
||||
/**/
|
||||
1623,
|
||||
/**/
|
||||
1622,
|
||||
/**/
|
||||
|
||||
Reference in New Issue
Block a user