mirror of
https://github.com/neovim/neovim.git
synced 2025-10-22 17:11:49 +00:00
refactor(vim.gsplit): remove "keepsep"
string.gmatch() is superior, use that instead.
This commit is contained in:
@@ -1659,12 +1659,18 @@ gsplit({s}, {sep}, {opts}) *vim.gsplit()*
|
|||||||
end
|
end
|
||||||
<
|
<
|
||||||
|
|
||||||
|
If you want to also inspect the separator itself (instead of discarding
|
||||||
|
it), use |string.gmatch()|. Example: >lua
|
||||||
|
|
||||||
|
for word, num in ('foo111bar222'):gmatch('([^0-9]*)(d*)') do
|
||||||
|
print(('word: s num: s'):format(word, num))
|
||||||
|
end
|
||||||
|
<
|
||||||
|
|
||||||
Parameters: ~
|
Parameters: ~
|
||||||
• {s} (string) String to split
|
• {s} string String to split
|
||||||
• {sep} (string) Separator or pattern
|
• {sep} string Separator or pattern
|
||||||
• {opts} (table|nil) Keyword arguments |kwargs|:
|
• {opts} (table|nil) Keyword arguments |kwargs|:
|
||||||
• keepsep: (boolean) Return segments matching `sep` instead of
|
|
||||||
discarding them.
|
|
||||||
• plain: (boolean) Use `sep` literally (as in string.find).
|
• plain: (boolean) Use `sep` literally (as in string.find).
|
||||||
• trimempty: (boolean) Discard empty segments at start and end
|
• trimempty: (boolean) Discard empty segments at start and end
|
||||||
of the sequence.
|
of the sequence.
|
||||||
@@ -1673,6 +1679,7 @@ gsplit({s}, {sep}, {opts}) *vim.gsplit()*
|
|||||||
(function) Iterator over the split components
|
(function) Iterator over the split components
|
||||||
|
|
||||||
See also: ~
|
See also: ~
|
||||||
|
• |string.gmatch()|
|
||||||
• |vim.split()|
|
• |vim.split()|
|
||||||
• |luaref-patterns|
|
• |luaref-patterns|
|
||||||
• https://www.lua.org/pil/20.2.html
|
• https://www.lua.org/pil/20.2.html
|
||||||
@@ -1749,7 +1756,6 @@ split({s}, {sep}, {opts}) *vim.split()*
|
|||||||
split("axaby", "ab?") --> {'','x','y'}
|
split("axaby", "ab?") --> {'','x','y'}
|
||||||
split("x*yz*o", "*", {plain=true}) --> {'x','yz','o'}
|
split("x*yz*o", "*", {plain=true}) --> {'x','yz','o'}
|
||||||
split("|x|y|z|", "|", {trimempty=true}) --> {'x', 'y', 'z'}
|
split("|x|y|z|", "|", {trimempty=true}) --> {'x', 'y', 'z'}
|
||||||
split("|x|y|z|", "|", {keepsep=true}) --> {'|', 'x', '|', 'y', '|', 'z', '|'}
|
|
||||||
<
|
<
|
||||||
|
|
||||||
Parameters: ~
|
Parameters: ~
|
||||||
@@ -1763,6 +1769,7 @@ split({s}, {sep}, {opts}) *vim.split()*
|
|||||||
|
|
||||||
See also: ~
|
See also: ~
|
||||||
• |vim.gsplit()|
|
• |vim.gsplit()|
|
||||||
|
• |string.gmatch()|
|
||||||
|
|
||||||
startswith({s}, {prefix}) *vim.startswith()*
|
startswith({s}, {prefix}) *vim.startswith()*
|
||||||
Tests if `s` starts with `prefix`.
|
Tests if `s` starts with `prefix`.
|
||||||
@@ -2625,7 +2632,7 @@ cmp({v1}, {v2}) *vim.version.cmp()*
|
|||||||
(integer) -1 if `v1 < v2`, 0 if `v1 == v2`, 1 if `v1 > v2`.
|
(integer) -1 if `v1 < v2`, 0 if `v1 == v2`, 1 if `v1 > v2`.
|
||||||
|
|
||||||
eq({v1}, {v2}) *vim.version.eq()*
|
eq({v1}, {v2}) *vim.version.eq()*
|
||||||
Returns `true` if the given versions are equal.
|
Returns `true` if the given versions are equal. See |vim.version.cmp()| for usage.
|
||||||
|
|
||||||
Parameters: ~
|
Parameters: ~
|
||||||
• {v1} Version|number[]
|
• {v1} Version|number[]
|
||||||
@@ -2635,7 +2642,7 @@ eq({v1}, {v2}) *vim.version.eq()*
|
|||||||
(boolean)
|
(boolean)
|
||||||
|
|
||||||
gt({v1}, {v2}) *vim.version.gt()*
|
gt({v1}, {v2}) *vim.version.gt()*
|
||||||
Returns `true` if `v1 > v2` .
|
Returns `true` if `v1 > v2` . See |vim.version.cmp()| for usage.
|
||||||
|
|
||||||
Parameters: ~
|
Parameters: ~
|
||||||
• {v1} Version|number[]
|
• {v1} Version|number[]
|
||||||
@@ -2654,7 +2661,7 @@ last({versions}) *vim.version.last()*
|
|||||||
Version ?|ni
|
Version ?|ni
|
||||||
|
|
||||||
lt({v1}, {v2}) *vim.version.lt()*
|
lt({v1}, {v2}) *vim.version.lt()*
|
||||||
Returns `true` if `v1 < v2` .
|
Returns `true` if `v1 < v2` . See |vim.version.cmp()| for usage.
|
||||||
|
|
||||||
Parameters: ~
|
Parameters: ~
|
||||||
• {v1} Version|number[]
|
• {v1} Version|number[]
|
||||||
|
@@ -66,6 +66,14 @@ end)()
|
|||||||
--- end
|
--- end
|
||||||
--- </pre>
|
--- </pre>
|
||||||
---
|
---
|
||||||
|
--- If you want to also inspect the separator itself (instead of discarding it), use
|
||||||
|
--- |string.gmatch()|. Example:
|
||||||
|
--- <pre>lua
|
||||||
|
--- for word, num in ('foo111bar222'):gmatch('([^0-9]*)(%d*)') do
|
||||||
|
--- print(('word: %s num: %s'):format(word, num))
|
||||||
|
--- end
|
||||||
|
--- </pre>
|
||||||
|
---
|
||||||
--- @see |string.gmatch()|
|
--- @see |string.gmatch()|
|
||||||
--- @see |vim.split()|
|
--- @see |vim.split()|
|
||||||
--- @see |luaref-patterns|
|
--- @see |luaref-patterns|
|
||||||
@@ -75,27 +83,22 @@ end)()
|
|||||||
--- @param s string String to split
|
--- @param s string String to split
|
||||||
--- @param sep string Separator or pattern
|
--- @param sep string Separator or pattern
|
||||||
--- @param opts (table|nil) Keyword arguments |kwargs|:
|
--- @param opts (table|nil) Keyword arguments |kwargs|:
|
||||||
--- - keepsep: (boolean) Include segments matching `sep` instead of discarding them.
|
|
||||||
--- - plain: (boolean) Use `sep` literally (as in string.find).
|
--- - plain: (boolean) Use `sep` literally (as in string.find).
|
||||||
--- - trimempty: (boolean) Discard empty segments at start and end of the sequence.
|
--- - trimempty: (boolean) Discard empty segments at start and end of the sequence.
|
||||||
---@return fun():string|nil (function) Iterator over the split components
|
---@return fun():string|nil (function) Iterator over the split components
|
||||||
function vim.gsplit(s, sep, opts)
|
function vim.gsplit(s, sep, opts)
|
||||||
local plain
|
local plain
|
||||||
local trimempty = false
|
local trimempty = false
|
||||||
local keepsep = false
|
|
||||||
if type(opts) == 'boolean' then
|
if type(opts) == 'boolean' then
|
||||||
plain = opts -- For backwards compatibility.
|
plain = opts -- For backwards compatibility.
|
||||||
else
|
else
|
||||||
vim.validate({ s = { s, 's' }, sep = { sep, 's' }, opts = { opts, 't', true } })
|
vim.validate({ s = { s, 's' }, sep = { sep, 's' }, opts = { opts, 't', true } })
|
||||||
opts = opts or {}
|
opts = opts or {}
|
||||||
plain, trimempty, keepsep = opts.plain, opts.trimempty, opts.keepsep
|
plain, trimempty = opts.plain, opts.trimempty
|
||||||
assert(not trimempty or not keepsep, 'keepsep+trimempty not supported')
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local start = 1
|
local start = 1
|
||||||
local done = false
|
local done = false
|
||||||
local sepseg = nil -- Last matched `sep` segment.
|
|
||||||
local sepesc = plain and vim.pesc(sep) or sep
|
|
||||||
|
|
||||||
-- For `trimempty`:
|
-- For `trimempty`:
|
||||||
local empty_start = true -- Only empty segments seen so far.
|
local empty_start = true -- Only empty segments seen so far.
|
||||||
@@ -105,9 +108,6 @@ function vim.gsplit(s, sep, opts)
|
|||||||
local function _pass(i, j, ...)
|
local function _pass(i, j, ...)
|
||||||
if i then
|
if i then
|
||||||
assert(j + 1 > start, 'Infinite loop detected')
|
assert(j + 1 > start, 'Infinite loop detected')
|
||||||
if keepsep then
|
|
||||||
sepseg = s:match(sepesc, start)
|
|
||||||
end
|
|
||||||
local seg = s:sub(start, i - 1)
|
local seg = s:sub(start, i - 1)
|
||||||
start = j + 1
|
start = j + 1
|
||||||
return seg, ...
|
return seg, ...
|
||||||
@@ -126,10 +126,6 @@ function vim.gsplit(s, sep, opts)
|
|||||||
local seg = nonemptyseg
|
local seg = nonemptyseg
|
||||||
nonemptyseg = nil
|
nonemptyseg = nil
|
||||||
return seg
|
return seg
|
||||||
elseif keepsep and sepseg then
|
|
||||||
local seg = sepseg
|
|
||||||
sepseg = nil
|
|
||||||
return seg
|
|
||||||
elseif done or (s == '' and sep == '') then
|
elseif done or (s == '' and sep == '') then
|
||||||
return nil
|
return nil
|
||||||
elseif sep == '' then
|
elseif sep == '' then
|
||||||
@@ -171,17 +167,16 @@ end
|
|||||||
--- split("axaby", "ab?") --> {'','x','y'}
|
--- split("axaby", "ab?") --> {'','x','y'}
|
||||||
--- split("x*yz*o", "*", {plain=true}) --> {'x','yz','o'}
|
--- split("x*yz*o", "*", {plain=true}) --> {'x','yz','o'}
|
||||||
--- split("|x|y|z|", "|", {trimempty=true}) --> {'x', 'y', 'z'}
|
--- split("|x|y|z|", "|", {trimempty=true}) --> {'x', 'y', 'z'}
|
||||||
--- split("|x|y|z|", "|", {keepsep=true}) --> {'|', 'x', '|', 'y', '|', 'z', '|'}
|
|
||||||
--- </pre>
|
--- </pre>
|
||||||
---
|
---
|
||||||
---@see |vim.gsplit()|
|
---@see |vim.gsplit()|
|
||||||
|
---@see |string.gmatch()|
|
||||||
---
|
---
|
||||||
---@param s string String to split
|
---@param s string String to split
|
||||||
---@param sep string Separator or pattern
|
---@param sep string Separator or pattern
|
||||||
---@param opts (table|nil) Keyword arguments |kwargs| accepted by |vim.gsplit()|
|
---@param opts (table|nil) Keyword arguments |kwargs| accepted by |vim.gsplit()|
|
||||||
---@return string[] List of split components
|
---@return string[] List of split components
|
||||||
function vim.split(s, sep, opts)
|
function vim.split(s, sep, opts)
|
||||||
-- TODO(justinmk): deprecate vim.split in favor of vim.totable(vim.gsplit())
|
|
||||||
local t = {}
|
local t = {}
|
||||||
for c in vim.gsplit(s, sep, opts) do
|
for c in vim.gsplit(s, sep, opts) do
|
||||||
table.insert(t, c)
|
table.insert(t, c)
|
||||||
|
@@ -65,6 +65,8 @@ local M = {}
|
|||||||
local Version = {}
|
local Version = {}
|
||||||
Version.__index = Version
|
Version.__index = Version
|
||||||
|
|
||||||
|
--- @private
|
||||||
|
---
|
||||||
--- Compares prerelease strings: per semver, number parts must be must be treated as numbers:
|
--- Compares prerelease strings: per semver, number parts must be must be treated as numbers:
|
||||||
--- "pre1.10" is greater than "pre1.2". https://semver.org/#spec-item-11
|
--- "pre1.10" is greater than "pre1.2". https://semver.org/#spec-item-11
|
||||||
local function cmp_prerel(prerel1, prerel2)
|
local function cmp_prerel(prerel1, prerel2)
|
||||||
|
@@ -329,14 +329,6 @@ describe('lua stdlib', function()
|
|||||||
matches("Infinite loop detected", pcall_err(vim.split, t[1], t[2]))
|
matches("Infinite loop detected", pcall_err(vim.split, t[1], t[2]))
|
||||||
end
|
end
|
||||||
|
|
||||||
-- `keepsep`
|
|
||||||
eq({ '', '.', '', '.', 'aa', '.', 'bb', '.', 'cc', '.', 'dd', '.', 'ee', '.', '', },
|
|
||||||
vim.split('..aa.bb.cc.dd.ee.', '%.', {keepsep=true}))
|
|
||||||
eq({ '..aa', '1', '.bb', '2', '', '2', '.cc.', '9', '', },
|
|
||||||
vim.split('..aa1.bb22.cc.9', '%d', {keepsep=true}))
|
|
||||||
eq({ '..aa', '1', '.bb', '22', '.cc.', '9', '', },
|
|
||||||
vim.split('..aa1.bb22.cc.9', '%d+', {keepsep=true}))
|
|
||||||
|
|
||||||
-- Validates args.
|
-- Validates args.
|
||||||
eq(true, pcall(vim.split, 'string', 'string'))
|
eq(true, pcall(vim.split, 'string', 'string'))
|
||||||
matches('s: expected string, got number',
|
matches('s: expected string, got number',
|
||||||
@@ -345,9 +337,6 @@ describe('lua stdlib', function()
|
|||||||
pcall_err(vim.split, 'string', 1))
|
pcall_err(vim.split, 'string', 1))
|
||||||
matches('opts: expected table, got number',
|
matches('opts: expected table, got number',
|
||||||
pcall_err(vim.split, 'string', 'string', 1))
|
pcall_err(vim.split, 'string', 'string', 1))
|
||||||
-- Not supported (yet).
|
|
||||||
matches('keepsep%+trimempty not supported',
|
|
||||||
pcall_err(vim.split, 'foo bar', ' ', {keepsep=true, trimempty=true}))
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it('vim.trim', function()
|
it('vim.trim', function()
|
||||||
|
Reference in New Issue
Block a user