perf(lsp): use faster version of str_byteindex

This commit is contained in:
Lewis Russell
2024-11-11 14:23:38 +00:00
committed by GitHub
parent ff575b3886
commit 3e855d533f
3 changed files with 34 additions and 20 deletions

View File

@@ -764,8 +764,11 @@ function vim.str_byteindex(s, encoding, index, strict_indexing)
return vim._str_byteindex(s, old_index, use_utf16) or error('index out of range')
end
vim.validate('s', s, 'string')
vim.validate('index', index, 'number')
-- Avoid vim.validate for performance.
if type(s) ~= 'string' or type(index) ~= 'number' then
vim.validate('s', s, 'string')
vim.validate('index', index, 'number')
end
local len = #s
@@ -773,11 +776,15 @@ function vim.str_byteindex(s, encoding, index, strict_indexing)
return 0
end
vim.validate('encoding', encoding, function(v)
return utfs[v], 'invalid encoding'
end)
if not utfs[encoding] then
vim.validate('encoding', encoding, function(v)
return utfs[v], 'invalid encoding'
end)
end
vim.validate('strict_indexing', strict_indexing, 'boolean', true)
if strict_indexing ~= nil and type(strict_indexing) ~= 'boolean' then
vim.validate('strict_indexing', strict_indexing, 'boolean', true)
end
if strict_indexing == nil then
strict_indexing = true
end
@@ -828,8 +835,11 @@ function vim.str_utfindex(s, encoding, index, strict_indexing)
return col32, col16
end
vim.validate('s', s, 'string')
vim.validate('index', index, 'number', true)
if type(s) ~= 'string' or (index ~= nil and type(index) ~= 'number') then
vim.validate('s', s, 'string')
vim.validate('index', index, 'number', true)
end
if not index then
index = math.huge
strict_indexing = false
@@ -839,11 +849,15 @@ function vim.str_utfindex(s, encoding, index, strict_indexing)
return 0
end
vim.validate('encoding', encoding, function(v)
return utfs[v], 'invalid encoding'
end)
if not utfs[encoding] then
vim.validate('encoding', encoding, function(v)
return utfs[v], 'invalid encoding'
end)
end
vim.validate('strict_indexing', strict_indexing, 'boolean', true)
if strict_indexing ~= nil and type(strict_indexing) ~= 'boolean' then
vim.validate('strict_indexing', strict_indexing, 'boolean', true)
end
if strict_indexing == nil then
strict_indexing = true
end