This commit is contained in:
hrsh7th
2021-08-27 11:56:12 +09:00
parent b9c494a53e
commit 359b576063
3 changed files with 30 additions and 2 deletions

View File

@@ -156,6 +156,26 @@ str.oneline = function(text)
return text
end
---Escape special chars
---@param text string
---@param chars string[]
---@return string
str.escape = function(text, chars)
table.insert(chars, '\\')
local escaped = {}
local i = 1
while i <= #text do
local c = string.sub(text, i, i)
if vim.tbl_contains(chars, c) then
table.insert(escaped, '\\')
table.insert(escaped, c)
else
table.insert(escaped, c)
end
i = i + 1
end
return table.concat(escaped, '')
end
return str