forked from blitmap/lua-snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
string-trim.lua
executable file
·52 lines (38 loc) · 827 Bytes
/
string-trim.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env lua
local ssub = string.sub
local sfind = string.find
module('trim')
-- Each of these functions return a 2nd value letting
-- the caller know if the string was changed at all
ltrim =
function (s)
local res = s
local tmp = sfind(res, '%S')
-- string.sub() will create a duplicate if
-- called with the first and last index
-- (str_sub() in lstrlib.c)
if not tmp then
res = ''
elseif tmp ~= 1 then
res = ssub(res, tmp)
end
return res, res ~= s
end
rtrim =
function (s)
local res = s
local tmp = sfind(res, '%S%s*$')
if not tmp then
res = ''
elseif tmp ~= #res then
res = ssub(res, 1, tmp)
end
return res, res ~= s
end
trim =
function (s)
local res1, stat1 = ltrim(s)
local res2, stat2 = rtrim(res1)
return res2, stat1 or stat2
end
return _M