1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-02-03 15:10:04 +08:00
SpaceVim/bundle/cmp-dictionary/lua/cmp_dictionary/kit/Vim/RegExp.lua
2023-06-11 21:41:39 +08:00

43 lines
1.2 KiB
Lua

local RegExp = {}
---@type table<string, { match_str: fun(self, text: string) }>
RegExp._cache = {}
---Create a RegExp object.
---@param pattern string
---@return { match_str: fun(self, text: string) }
function RegExp.get(pattern)
if not RegExp._cache[pattern] then
RegExp._cache[pattern] = vim.regex(pattern)
end
return RegExp._cache[pattern]
end
---Grep and substitute text.
---@param text string
---@param pattern string
---@param replacement string
---@return string
function RegExp.gsub(text, pattern, replacement)
return vim.fn.substitute(text, pattern, replacement, 'g')
end
---Match pattern in text for specified position.
---@param text string
---@param pattern string
---@param pos number 1-origin index
---@return string?, integer?, integer? 1-origin-index
function RegExp.extract_at(text, pattern, pos)
local before_text = text:sub(1, pos - 1)
local after_text = text:sub(pos)
local b_s, _ = RegExp.get(pattern .. '$'):match_str(before_text)
local _, a_e = RegExp.get('^' .. pattern):match_str(after_text)
if b_s or a_e then
b_s = b_s or #before_text
a_e = #before_text + (a_e or 0)
return text:sub(b_s + 1, a_e), b_s + 1, a_e + 1
end
end
return RegExp