stringutil.split(text, split, trim) does now support automatic trimming.
This commit is contained in:
parent
89fecc8cbf
commit
9aaa9268fd
@ -99,8 +99,14 @@ end
|
||||
--
|
||||
-- @param text The text to split.
|
||||
-- @param split The split value.
|
||||
-- @param trim_values Optional. If the values should be trimmed, defaults to
|
||||
-- true.
|
||||
-- @return The list of splitted values.
|
||||
function stringutil.split(text, split)
|
||||
function stringutil.split(text, split, trim_values)
|
||||
if trim_values == nil then
|
||||
trim_values = true
|
||||
end
|
||||
|
||||
local splitted = List:new()
|
||||
|
||||
if string ~= nil and #text > 0 then
|
||||
@ -109,7 +115,13 @@ function stringutil.split(text, split)
|
||||
local starts, ends = string.find(text, split, 0, true)
|
||||
|
||||
while ends ~= nil do
|
||||
splitted:add(string.sub(text, previous_ends + 1, starts - 1))
|
||||
local value = string.sub(text, previous_ends + 1, starts - 1)
|
||||
|
||||
if trim_values then
|
||||
value = stringutil.trim(value)
|
||||
end
|
||||
|
||||
splitted:add(value)
|
||||
|
||||
previous_starts = starts
|
||||
previous_ends = ends
|
||||
@ -117,7 +129,13 @@ function stringutil.split(text, split)
|
||||
end
|
||||
|
||||
if previous_ends > 0 or splitted:size() == 0 then
|
||||
splitted:add(string.sub(text, previous_ends + 1))
|
||||
local value = string.sub(text, previous_ends + 1)
|
||||
|
||||
if trim_values then
|
||||
value = stringutil.trim(value)
|
||||
end
|
||||
|
||||
splitted:add(value)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -51,15 +51,15 @@ test.run("split", function()
|
||||
test.equals(1, single:size())
|
||||
test.equals("value", single:get(1))
|
||||
|
||||
local empty = stringutil.split("", ", ")
|
||||
local empty = stringutil.split("", ",")
|
||||
test.equals(0, empty:size())
|
||||
test.equals(nil, empty:get(1))
|
||||
|
||||
local only_split = stringutil.split(", ", ", ")
|
||||
local only_split = stringutil.split(", ", ",")
|
||||
test.equals(2, only_split:size())
|
||||
test.equals("", only_split:get(1))
|
||||
|
||||
local splitted = stringutil.split("test, something, value, to", ", ")
|
||||
local splitted = stringutil.split("test, something, value, to", ",")
|
||||
test.equals(4, splitted:size())
|
||||
test.equals("test", splitted:get(1))
|
||||
test.equals("something", splitted:get(2))
|
||||
@ -71,6 +71,12 @@ test.run("split", function()
|
||||
test.equals("-100", params:get(1))
|
||||
test.equals("100", params:get(2))
|
||||
test.equals("/some/path/", params:get(3))
|
||||
|
||||
local not_trimmed = stringutil.split("no, trimming , required", ",", false)
|
||||
test.equals(3, not_trimmed:size())
|
||||
test.equals("no", not_trimmed:get(1))
|
||||
test.equals(" trimming ", not_trimmed:get(2))
|
||||
test.equals(" required", not_trimmed:get(3))
|
||||
end)
|
||||
|
||||
test.run("startswith", function()
|
||||
|
Loading…
x
Reference in New Issue
Block a user