Moved shared code of lume.split/trim() to local func

This commit is contained in:
rxi 2014-03-19 21:05:34 +00:00
parent 617729e261
commit 9ff4637201

View File

@ -23,6 +23,10 @@ local math_min = math.min
local math_max = math.max local math_max = math.max
local math_pi = math.pi local math_pi = math.pi
local patternescape = function(str)
return str:gsub("[%(%)%.%%%+%-%*%?%[%]%^%$]", "%%%1")
end
function lume.clamp(x, min, max) function lume.clamp(x, min, max)
return x < min and min or (x > max and max or x) return x < min and min or (x > max and max or x)
@ -281,15 +285,15 @@ function lume.split(str, sep)
return lume.array(str:gmatch("([%S]+)")) return lume.array(str:gmatch("([%S]+)"))
else else
assert(sep ~= "", "empty separator") assert(sep ~= "", "empty separator")
local ssep = sep:gsub("[%(%)%.%%%+%-%*%?%[%^%$]", "%%%1") local psep = patternescape(sep)
return lume.array((str..sep):gmatch("(.-)("..ssep..")")) return lume.array((str..sep):gmatch("(.-)("..psep..")"))
end end
end end
function lume.trim(str, chars) function lume.trim(str, chars)
if not chars then return str:match("^[%s]*(.-)[%s]*$") end if not chars then return str:match("^[%s]*(.-)[%s]*$") end
chars = chars:gsub("[%(%)%.%%%+%-%*%?%[%]%^%$]", "%%%1") chars = patternescape(chars)
return str:match("^[" .. chars .. "]*(.-)[" .. chars .. "]*$") return str:match("^[" .. chars .. "]*(.-)[" .. chars .. "]*$")
end end