mirror of
https://github.com/1bardesign/batteries.git
synced 2024-11-22 22:24:35 +00:00
Refactored rtrim and ltrim to use _whitespace_bytes table
This commit is contained in:
parent
6042e3a705
commit
dbadd9af83
20
stringx.lua
20
stringx.lua
@ -216,31 +216,33 @@ function stringx.trim(s)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function stringx.ltrim(s)
|
function stringx.ltrim(s)
|
||||||
if s == "" or s == string.rep(" ", s:len()) then return "" end
|
if s == "" or s == string.rep(" ", s:len()) then
|
||||||
|
return ""
|
||||||
|
end
|
||||||
|
|
||||||
local head = 1
|
local head = 1
|
||||||
for i = 1, #s do
|
for i = 1, #s do
|
||||||
local c = s:sub(i, i)
|
if not _whitespace_bytes[s:byte(i)] then
|
||||||
if c ~= " " then
|
|
||||||
head = i
|
head = i
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return s:sub(head)
|
return s:sub(head)
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function stringx.rtrim(s)
|
function stringx.rtrim(s)
|
||||||
if s == "" or s == string.rep(" ", s:len()) then return "" end
|
if s == "" or s == string.rep(" ", s:len()) then
|
||||||
|
return ""
|
||||||
|
end
|
||||||
local tail = #s
|
local tail = #s
|
||||||
for i=#s, 1 do
|
|
||||||
local c = s:sub(i, i)
|
for i = #s, 1, -1 do
|
||||||
if c ~= " " then
|
if not _whitespace_bytes[s:byte(i)] then
|
||||||
tail = i
|
tail = i
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return s:sub(1, tail)
|
return s:sub(1, tail)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user