-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.lua
More file actions
44 lines (41 loc) · 1.16 KB
/
util.lua
File metadata and controls
44 lines (41 loc) · 1.16 KB
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
local match = string.match
function trim(s)
return match(s,'^()%s*$') and '' or match(s,'^%s*(.*%S)')
end
function readonly(t)
local mt = {
__newindex = function(t, k, v)
error("Tried to modify read-only table ["..tostring(k).."] = "..tostring(v))
end,
__index = function(t, k)
local a = rawget(t, k)
if type(a) ~= "function" then error("Cannot access private members") end
end,
__metatable = nil
}
setmetatable(t, mt)
return t
end
-- Matches string against pattern with return values:
-- string - the string with pattern removed if found
-- string - value of the matched string
-- int - offset to move cursor
-- int - number of whitespaces before pattern
function match_pattern(str, pat)
--dbg()
local a, b = string.match(str, "^()"..pat.."()")
if a == nil then return str, nil, 0, 0 end
return string.sub(str, b), string.sub(str, a, b-1), b-1, a-1
end
-- Counts tabs in string until limit count is hit
function count_tabs(s, lim)
local count = 0
if lim == nil or lim > #s then lim = #s end
for i = 1, lim do
local c = string.sub(s,i,i)
if string.byte(c) == 9 then
count = count + 1
end
end
return count
end