Merge #15218 from gpanders/split-trimempty

feat(lua): add "noempty" param to vim.split()
This commit is contained in:
Justin M. Keyes
2021-10-03 17:33:12 -07:00
committed by GitHub
3 changed files with 75 additions and 30 deletions

View File

@@ -1373,20 +1373,25 @@ pesc({s}) *vim.pesc()*
See also: ~
https://github.com/rxi/lume
split({s}, {sep}, {plain}) *vim.split()*
split({s}, {sep}, {kwargs}) *vim.split()*
Splits a string at each instance of a separator.
Examples: >
split(":aa::b:", ":") --> {'','aa','','b',''}
split("axaby", "ab?") --> {'','x','y'}
split(x*yz*o, "*", true) --> {'x','yz','o'}
split(":aa::b:", ":") --> {'','aa','','b',''}
split("axaby", "ab?") --> {'','x','y'}
split("x*yz*o", "*", {plain=true}) --> {'x','yz','o'}
split("|x|y|z|", "|", {trimempty=true}) --> {'x', 'y', 'z'}
<
Parameters: ~
{s} String to split
{sep} Separator string or pattern
{plain} If `true` use `sep` literally (passed to
String.find)
{s} String to split
{sep} Separator string or pattern
{kwargs} Keyword arguments:
• plain: (boolean) If `true` use `sep` literally
(passed to string.find)
• trimempty: (boolean) If `true` remove empty
items from the front and back of the list
Return: ~
List-like table of the split components.

View File

@@ -98,17 +98,53 @@ end
--- <pre>
--- split(":aa::b:", ":") --> {'','aa','','b',''}
--- split("axaby", "ab?") --> {'','x','y'}
--- split(x*yz*o, "*", true) --> {'x','yz','o'}
--- split("x*yz*o", "*", {plain=true}) --> {'x','yz','o'}
--- split("|x|y|z|", "|", {trimempty=true}) --> {'x', 'y', 'z'}
--- </pre>
--
---
---@see |vim.gsplit()|
---
---@param s String to split
---@param sep Separator string or pattern
---@param plain If `true` use `sep` literally (passed to String.find)
---@param kwargs Keyword arguments:
--- - plain: (boolean) If `true` use `sep` literally (passed to string.find)
--- - trimempty: (boolean) If `true` remove empty items from the front
--- and back of the list
---@returns List-like table of the split components.
function vim.split(s,sep,plain)
local t={} for c in vim.gsplit(s, sep, plain) do table.insert(t,c) end
function vim.split(s, sep, kwargs)
local plain
local trimempty = false
if type(kwargs) == 'boolean' then
-- Support old signature for backward compatibility
plain = kwargs
else
vim.validate { kwargs = {kwargs, 't', true} }
kwargs = kwargs or {}
plain = kwargs.plain
trimempty = kwargs.trimempty
end
local t = {}
local skip = trimempty
for c in vim.gsplit(s, sep, plain) do
if c ~= "" then
skip = false
end
if not skip then
table.insert(t, c)
end
end
if trimempty then
for i = #t, 1, -1 do
if t[i] ~= "" then
break
end
table.remove(t, i)
end
end
return t
end

View File

@@ -237,29 +237,34 @@ describe('lua stdlib', function()
end)
it("vim.split", function()
local split = function(str, sep, plain)
return exec_lua('return vim.split(...)', str, sep, plain)
local split = function(str, sep, kwargs)
return exec_lua('return vim.split(...)', str, sep, kwargs)
end
local tests = {
{ "a,b", ",", false, { 'a', 'b' } },
{ ":aa::bb:", ":", false, { '', 'aa', '', 'bb', '' } },
{ "::ee::ff:", ":", false, { '', '', 'ee', '', 'ff', '' } },
{ "ab", ".", false, { '', '', '' } },
{ "a1b2c", "[0-9]", false, { 'a', 'b', 'c' } },
{ "xy", "", false, { 'x', 'y' } },
{ "here be dragons", " ", false, { "here", "be", "dragons"} },
{ "axaby", "ab?", false, { '', 'x', 'y' } },
{ "f v2v v3v w2w ", "([vw])2%1", false, { 'f ', ' v3v ', ' ' } },
{ "", "", false, {} },
{ "", "a", false, { '' } },
{ "x*yz*oo*l", "*", true, { 'x', 'yz', 'oo', 'l' } },
{ "a,b", ",", false, false, { 'a', 'b' } },
{ ":aa::bb:", ":", false, false, { '', 'aa', '', 'bb', '' } },
{ ":aa::bb:", ":", false, true, { 'aa', '', 'bb' } },
{ "::ee::ff:", ":", false, false, { '', '', 'ee', '', 'ff', '' } },
{ "::ee::ff:", ":", false, true, { 'ee', '', 'ff' } },
{ "ab", ".", false, false, { '', '', '' } },
{ "a1b2c", "[0-9]", false, false, { 'a', 'b', 'c' } },
{ "xy", "", false, false, { 'x', 'y' } },
{ "here be dragons", " ", false, false, { "here", "be", "dragons"} },
{ "axaby", "ab?", false, false, { '', 'x', 'y' } },
{ "f v2v v3v w2w ", "([vw])2%1", false, false, { 'f ', ' v3v ', ' ' } },
{ "", "", false, false, {} },
{ "", "a", false, false, { '' } },
{ "x*yz*oo*l", "*", true, false, { 'x', 'yz', 'oo', 'l' } },
}
for _, t in ipairs(tests) do
eq(t[4], split(t[1], t[2], t[3]))
eq(t[5], split(t[1], t[2], {plain=t[3], trimempty=t[4]}))
end
-- Test old signature
eq({'x', 'yz', 'oo', 'l'}, split("x*yz*oo*l", "*", true))
local loops = {
{ "abc", ".-" },
}
@@ -283,9 +288,8 @@ describe('lua stdlib', function()
vim/shared.lua:0: in function <vim/shared.lua:0>]]),
pcall_err(split, 'string', 1))
eq(dedent([[
Error executing lua: vim/shared.lua:0: plain: expected boolean, got number
Error executing lua: vim/shared.lua:0: kwargs: expected table, got number
stack traceback:
vim/shared.lua:0: in function 'gsplit'
vim/shared.lua:0: in function <vim/shared.lua:0>]]),
pcall_err(split, 'string', 'string', 1))
end)