mirror of
https://github.com/neovim/neovim
synced 2025-07-16 01:01:49 +00:00
fix(vim.text): remove assert from vim.text.hexdecode
Instead, return nil plus an error message if the input is invalid.
This commit is contained in:
@ -4385,8 +4385,9 @@ vim.text.hexdecode({enc}) *vim.text.hexdecode()*
|
|||||||
Parameters: ~
|
Parameters: ~
|
||||||
• {enc} (`string`) String to decode
|
• {enc} (`string`) String to decode
|
||||||
|
|
||||||
Return: ~
|
Return (multiple): ~
|
||||||
(`string`) Decoded string
|
(`string?`) Decoded string
|
||||||
|
(`string?`) Error message, if any
|
||||||
|
|
||||||
vim.text.hexencode({str}) *vim.text.hexencode()*
|
vim.text.hexencode({str}) *vim.text.hexencode()*
|
||||||
Hex encode a string.
|
Hex encode a string.
|
||||||
|
@ -18,15 +18,19 @@ end
|
|||||||
--- Hex decode a string.
|
--- Hex decode a string.
|
||||||
---
|
---
|
||||||
--- @param enc string String to decode
|
--- @param enc string String to decode
|
||||||
--- @return string : Decoded string
|
--- @return string? : Decoded string
|
||||||
|
--- @return string? : Error message, if any
|
||||||
function M.hexdecode(enc)
|
function M.hexdecode(enc)
|
||||||
assert(#enc % 2 == 0, 'string must have an even number of hex characters')
|
if #enc % 2 ~= 0 then
|
||||||
|
return nil, 'string must have an even number of hex characters'
|
||||||
|
end
|
||||||
|
|
||||||
local str = {} ---@type string[]
|
local str = {} ---@type string[]
|
||||||
for i = 1, #enc, 2 do
|
for i = 1, #enc, 2 do
|
||||||
local n = assert(tonumber(enc:sub(i, i + 1), 16))
|
local n = assert(tonumber(enc:sub(i, i + 1), 16))
|
||||||
str[#str + 1] = string.char(n)
|
str[#str + 1] = string.char(n)
|
||||||
end
|
end
|
||||||
return table.concat(str)
|
return table.concat(str), nil
|
||||||
end
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
Reference in New Issue
Block a user