mirror of
https://github.com/neovim/neovim
synced 2025-07-16 01:01:49 +00:00
fix(lsp): str_byteindex_enc bounds checking #30747
Problem:
Previously the index was only checked against the UTF8 length. This
could cause unexpected behaviours for strings containing multibyte chars
Solution:
Check indicies correctly against their max value before returning the
fallback length
(cherry picked from commit 80e37aa533
)
This commit is contained in:
committed by
github-actions[bot]
parent
bbefbc995e
commit
84bbbd9fbe
@ -155,25 +155,24 @@ end
|
|||||||
---@param encoding string utf-8|utf-16|utf-32| defaults to utf-16
|
---@param encoding string utf-8|utf-16|utf-32| defaults to utf-16
|
||||||
---@return integer byte (utf-8) index of `encoding` index `index` in `line`
|
---@return integer byte (utf-8) index of `encoding` index `index` in `line`
|
||||||
function M._str_byteindex_enc(line, index, encoding)
|
function M._str_byteindex_enc(line, index, encoding)
|
||||||
local len = #line
|
-- LSP spec: if character > line length, default to the line length.
|
||||||
if index > len then
|
-- https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#position
|
||||||
-- LSP spec: if character > line length, default to the line length.
|
local len8 = #line
|
||||||
-- https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#position
|
|
||||||
return len
|
|
||||||
end
|
|
||||||
if not encoding then
|
if not encoding then
|
||||||
encoding = 'utf-16'
|
encoding = 'utf-16'
|
||||||
end
|
end
|
||||||
if encoding == 'utf-8' then
|
if encoding == 'utf-8' then
|
||||||
if index then
|
if index and index <= len8 then
|
||||||
return index
|
return index
|
||||||
else
|
else
|
||||||
return len
|
return len8
|
||||||
end
|
end
|
||||||
elseif encoding == 'utf-16' then
|
end
|
||||||
return vim.str_byteindex(line, index, true)
|
local len32, len16 = vim.str_utfindex(line)
|
||||||
|
if encoding == 'utf-16' then
|
||||||
|
return index <= len16 and vim.str_byteindex(line, index, true) or len8
|
||||||
elseif encoding == 'utf-32' then
|
elseif encoding == 'utf-32' then
|
||||||
return vim.str_byteindex(line, index)
|
return index <= len32 and vim.str_byteindex(line, index) or len8
|
||||||
else
|
else
|
||||||
error('Invalid encoding: ' .. vim.inspect(encoding))
|
error('Invalid encoding: ' .. vim.inspect(encoding))
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user