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 text The text to split.
|
||||||
-- @param split The split value.
|
-- @param split The split value.
|
||||||
|
-- @param trim_values Optional. If the values should be trimmed, defaults to
|
||||||
|
-- true.
|
||||||
-- @return The list of splitted values.
|
-- @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()
|
local splitted = List:new()
|
||||||
|
|
||||||
if string ~= nil and #text > 0 then
|
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)
|
local starts, ends = string.find(text, split, 0, true)
|
||||||
|
|
||||||
while ends ~= nil do
|
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_starts = starts
|
||||||
previous_ends = ends
|
previous_ends = ends
|
||||||
@ -117,7 +129,13 @@ function stringutil.split(text, split)
|
|||||||
end
|
end
|
||||||
|
|
||||||
if previous_ends > 0 or splitted:size() == 0 then
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -71,6 +71,12 @@ test.run("split", function()
|
|||||||
test.equals("-100", params:get(1))
|
test.equals("-100", params:get(1))
|
||||||
test.equals("100", params:get(2))
|
test.equals("100", params:get(2))
|
||||||
test.equals("/some/path/", params:get(3))
|
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)
|
end)
|
||||||
|
|
||||||
test.run("startswith", function()
|
test.run("startswith", function()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user