5 Several common useful Lua functions
11 Copyright 2008 Steven Barth <steven@midlink.org>
13 Licensed under the Apache License, Version 2.0 (the "License");
14 you may not use this file except in compliance with the License.
15 You may obtain a copy of the License at
17 http://www.apache.org/licenses/LICENSE-2.0
19 Unless required by applicable law or agreed to in writing, software
20 distributed under the License is distributed on an "AS IS" BASIS,
21 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 See the License for the specific language governing permissions and
23 limitations under the License.
27 module("luci.util", package.seeall)
30 -- Lua simplified Python-style OO class support emulation
34 local create = function(class, ...)
36 setmetatable(inst, {__index = class})
39 local stat, err = pcall(inst.__init__, inst, ...)
48 local classmeta = {__call = create}
51 classmeta.__index = base
54 setmetatable(class, classmeta)
59 -- Clones an object (deep on-demand)
60 function clone(object, deep)
63 for k, v in pairs(object) do
64 if deep and type(v) == "table" then
70 setmetatable(copy, getmetatable(object))
76 -- Combines two or more numerically indexed tables into one
79 for i, a in ipairs(arg) do
80 for j, v in ipairs(a) do
81 table.insert(result, v)
88 -- Checks whether a table has an object "value" in it
89 function contains(table, value)
90 for k,v in pairs(table) do
99 -- Dumps and strips a Lua-Function
101 local d = string.dump(f)
102 return d and strip_bytecode(d)
106 -- Dumps a table to stdout (useful for testing and debugging)
107 function dumptable(t, i)
109 for k,v in pairs(t) do
110 print(string.rep("\t", i) .. k, v)
111 if type(v) == "table" then
118 -- Escapes all occurences of c in s
119 function escape(s, c)
121 return s:gsub(c, "\\" .. c)
125 -- Populate obj in the scope of f as key
126 function extfenv(f, key, obj)
127 local scope = getfenv(f)
132 -- Checks whether an object is an instanceof class
133 function instanceof(object, class)
134 local meta = getmetatable(object)
135 while meta and meta.__index do
136 if meta.__index == class then
139 meta = getmetatable(meta.__index)
145 -- Creates valid XML PCDATA from a string
146 function pcdata(value)
147 value = value:gsub("&", "&")
148 value = value:gsub('"', """)
149 value = value:gsub("'", "'")
150 value = value:gsub("<", "<")
151 return value:gsub(">", ">")
155 -- Resets the scope of f doing a shallow copy of its scope into a new table
157 setfenv(f, clone(getfenv(f)))
161 -- Splits a string into an array
162 function split(str, pat, max, regex)
182 local s, e = str:find(pat, c, not regex)
183 table.insert(t, str:sub(c, s and s - 1))
185 c = e and e + 1 or #str + 1
186 until not s or max < 0
192 -- Bytecode stripping function by Peter Cawley from http://lua-users.org/lists/lua-l/2008-02/msg01158.html
193 function strip_bytecode(dump)
194 local version, format, endian, int, size, ins, num = dump:byte(5, 11)
197 subint = function(dump, i, l)
200 val = val * 256 + dump:byte(i + n - 1)
205 subint = function(dump, i, l)
208 val = val * 256 + dump:byte(i + n - 1)
215 strip_function = function(dump)
216 local count, offset = subint(dump, 1, size)
217 local stripped, dirty = string.rep("\0", size), offset + count
218 offset = offset + count + int * 2 + 4
219 offset = offset + int + subint(dump, offset, int) * ins
220 count, offset = subint(dump, offset, int)
223 t, offset = subint(dump, offset, 1)
227 offset = offset + size + subint(dump, offset, size)
229 offset = offset + num
232 count, offset = subint(dump, offset, int)
233 stripped = stripped .. dump:sub(dirty, offset - 1)
235 local proto, off = strip_function(dump:sub(offset, -1))
236 stripped, offset = stripped .. proto, offset + off - 1
238 offset = offset + subint(dump, offset, int) * int + int
239 count, offset = subint(dump, offset, int)
241 offset = offset + subint(dump, offset, size) + size + int * 2
243 count, offset = subint(dump, offset, int)
245 offset = offset + subint(dump, offset, size) + size
247 stripped = stripped .. string.rep("\0", int * 3)
248 return stripped, offset
251 return dump:sub(1,12) .. strip_function(dump:sub(13,-1))
255 -- Removes whitespace from beginning and end of a string
257 local s = str:gsub("^%s*(.-)%s*$", "%1")
262 -- Updates given table with new values
263 function update(t, updates)
264 for k, v in pairs(updates) do
270 -- Updates the scope of f with "extscope"
271 function updfenv(f, extscope)
272 update(getfenv(f), extscope)
276 -- Validates a variable
277 function validate(value, cast_number, cast_int)
278 if cast_number or cast_int then
279 value = tonumber(value)
282 if cast_int and value and not(value % 1 == 0) then