Remove luci.cutil: does not affect performance
This commit is contained in:
parent
a3cf596d37
commit
8d2c8c131d
3 changed files with 9 additions and 282 deletions
|
@ -1,12 +1,2 @@
|
||||||
include ../../build/config.mk
|
include ../../build/config.mk
|
||||||
include ../../build/module.mk
|
include ../../build/module.mk
|
||||||
|
|
||||||
%.o: %.c
|
|
||||||
$(COMPILE) $(LUA_CFLAGS) $(FPIC) -c -o $@ $<
|
|
||||||
|
|
||||||
compile: src/luci_cutil.o
|
|
||||||
mkdir -p dist$(LUCI_LIBRARYDIR)
|
|
||||||
$(LINK) $(SHLIB_FLAGS) -o dist$(LUCI_LIBRARYDIR)/cutil.so src/luci_cutil.o
|
|
||||||
|
|
||||||
clean: luaclean
|
|
||||||
rm -f src/*.o
|
|
||||||
|
|
|
@ -45,7 +45,6 @@ module "luci.util"
|
||||||
--
|
--
|
||||||
-- Pythonic string formatting extension
|
-- Pythonic string formatting extension
|
||||||
--
|
--
|
||||||
--[[
|
|
||||||
getmetatable("").__mod = function(a, b)
|
getmetatable("").__mod = function(a, b)
|
||||||
if not b then
|
if not b then
|
||||||
return a
|
return a
|
||||||
|
@ -55,7 +54,6 @@ getmetatable("").__mod = function(a, b)
|
||||||
return a:format(b)
|
return a:format(b)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
]]--
|
|
||||||
|
|
||||||
|
|
||||||
--
|
--
|
||||||
|
@ -63,7 +61,6 @@ end
|
||||||
--
|
--
|
||||||
|
|
||||||
-- Instantiates a class
|
-- Instantiates a class
|
||||||
--[[
|
|
||||||
local function _instantiate(class, ...)
|
local function _instantiate(class, ...)
|
||||||
local inst = setmetatable({}, {__index = class})
|
local inst = setmetatable({}, {__index = class})
|
||||||
|
|
||||||
|
@ -73,7 +70,6 @@ local function _instantiate(class, ...)
|
||||||
|
|
||||||
return inst
|
return inst
|
||||||
end
|
end
|
||||||
]]--
|
|
||||||
|
|
||||||
--- Create a Class object (Python-style object model).
|
--- Create a Class object (Python-style object model).
|
||||||
-- The class object can be instantiated by calling itself.
|
-- The class object can be instantiated by calling itself.
|
||||||
|
@ -89,15 +85,12 @@ end
|
||||||
-- @return A class object
|
-- @return A class object
|
||||||
-- @see instanceof
|
-- @see instanceof
|
||||||
-- @see clone
|
-- @see clone
|
||||||
--[[
|
|
||||||
function class(base)
|
function class(base)
|
||||||
return setmetatable({}, {
|
return setmetatable({}, {
|
||||||
__call = _instantiate,
|
__call = _instantiate,
|
||||||
__index = base
|
__index = base
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
]]--
|
|
||||||
class = cutil.class
|
|
||||||
|
|
||||||
--- Test whether the given object is an instance of the given class.
|
--- Test whether the given object is an instance of the given class.
|
||||||
-- @param object Object instance
|
-- @param object Object instance
|
||||||
|
@ -105,7 +98,6 @@ class = cutil.class
|
||||||
-- @return Boolean indicating whether the object is an instance
|
-- @return Boolean indicating whether the object is an instance
|
||||||
-- @see class
|
-- @see class
|
||||||
-- @see clone
|
-- @see clone
|
||||||
--[[
|
|
||||||
function instanceof(object, class)
|
function instanceof(object, class)
|
||||||
local meta = getmetatable(object)
|
local meta = getmetatable(object)
|
||||||
while meta and meta.__index do
|
while meta and meta.__index do
|
||||||
|
@ -116,8 +108,6 @@ function instanceof(object, class)
|
||||||
end
|
end
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
]]--
|
|
||||||
instanceof = cutil.instanceof
|
|
||||||
|
|
||||||
|
|
||||||
--
|
--
|
||||||
|
@ -204,18 +194,17 @@ end
|
||||||
--- Create valid XML PCDATA from given string.
|
--- Create valid XML PCDATA from given string.
|
||||||
-- @param value String value containing the data to escape
|
-- @param value String value containing the data to escape
|
||||||
-- @return String value containing the escaped data
|
-- @return String value containing the escaped data
|
||||||
--[[
|
local _pcdata_repl = {
|
||||||
|
["&"] = "&",
|
||||||
|
['"'] = """,
|
||||||
|
["'"] = "'",
|
||||||
|
["<"] = "<",
|
||||||
|
[">"] = ">"
|
||||||
|
}
|
||||||
|
|
||||||
function pcdata(value)
|
function pcdata(value)
|
||||||
return value and tostring(value):gsub("[&\"'<>]", {
|
return value and tostring(value):gsub("[&\"'<>]", _pcdata_repl)
|
||||||
["&"] = "&",
|
|
||||||
['"'] = """,
|
|
||||||
["'"] = "'",
|
|
||||||
["<"] = "<",
|
|
||||||
[">"] = ">"
|
|
||||||
})
|
|
||||||
end
|
end
|
||||||
]]--
|
|
||||||
pcdata = cutil.pcdata
|
|
||||||
|
|
||||||
--- Strip HTML tags from given string.
|
--- Strip HTML tags from given string.
|
||||||
-- @param value String containing the HTML text
|
-- @param value String containing the HTML text
|
||||||
|
@ -271,12 +260,9 @@ end
|
||||||
--- Remove leading and trailing whitespace from given string value.
|
--- Remove leading and trailing whitespace from given string value.
|
||||||
-- @param str String value containing whitespace padded data
|
-- @param str String value containing whitespace padded data
|
||||||
-- @return String value with leading and trailing space removed
|
-- @return String value with leading and trailing space removed
|
||||||
--[[
|
|
||||||
function trim(str)
|
function trim(str)
|
||||||
return (str:gsub("^%s*(.-)%s*$", "%1"))
|
return (str:gsub("^%s*(.-)%s*$", "%1"))
|
||||||
end
|
end
|
||||||
]]--
|
|
||||||
trim = cutil.trim
|
|
||||||
|
|
||||||
--- Count the occurences of given substring in given string.
|
--- Count the occurences of given substring in given string.
|
||||||
-- @param str String to search in
|
-- @param str String to search in
|
||||||
|
|
|
@ -1,249 +0,0 @@
|
||||||
/**
|
|
||||||
* LuCI Core - Utility library
|
|
||||||
* Copyright (C) 2008 Steven Barth <steven@midlink.org>
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "lauxlib.h"
|
|
||||||
|
|
||||||
#define LUCI_MODNAME "luci.cutil"
|
|
||||||
#define LUCI_MODDESC "LuCI Core Utility Library"
|
|
||||||
#define LUCI_MODCOPY "2008 Steven Barth"
|
|
||||||
|
|
||||||
|
|
||||||
/* Pythonic overloaded MOD operator */
|
|
||||||
static int luci__string_mod(lua_State *L) {
|
|
||||||
int i, n=1;
|
|
||||||
|
|
||||||
luaL_checkstring(L, 1);
|
|
||||||
luaL_checkany(L, 2);
|
|
||||||
|
|
||||||
/* Discard further arguments */
|
|
||||||
lua_settop(L, 2);
|
|
||||||
|
|
||||||
/* Get format and push it to the bottom of the stack */
|
|
||||||
lua_getfield(L, 1, "format");
|
|
||||||
lua_insert(L, 1);
|
|
||||||
|
|
||||||
/* If second argument is a table, unpack it */
|
|
||||||
if (lua_istable(L, 3)) {
|
|
||||||
n = lua_objlen(L, 3);
|
|
||||||
if (n > 0) {
|
|
||||||
luaL_checkstack(L, n, "too many results to unpack");
|
|
||||||
for (i=1; i<=n; i++) {
|
|
||||||
lua_rawgeti(L, 3, i);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
n = 0;
|
|
||||||
}
|
|
||||||
lua_remove(L, 3);
|
|
||||||
}
|
|
||||||
|
|
||||||
lua_call(L, n+1, 1);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Instantiate a class */
|
|
||||||
static int luci__instantiate(lua_State *L) {
|
|
||||||
luaL_checktype(L, 1, LUA_TTABLE);
|
|
||||||
|
|
||||||
/* Create the object */
|
|
||||||
lua_newtable(L);
|
|
||||||
|
|
||||||
/* Create the metatable */
|
|
||||||
lua_createtable(L, 0, 1);
|
|
||||||
lua_pushvalue(L, 1);
|
|
||||||
lua_setfield(L, -2, "__index");
|
|
||||||
lua_setmetatable(L, -2);
|
|
||||||
|
|
||||||
/* Move instance at the bottom of the stack */
|
|
||||||
lua_replace(L, 1);
|
|
||||||
|
|
||||||
/* Invoke constructor if it exists */
|
|
||||||
lua_getfield(L, 1, "__init__");
|
|
||||||
if (lua_isfunction(L, -1)) {
|
|
||||||
/* Put instance at the bottom for the 2nd time */
|
|
||||||
lua_pushvalue(L, 1);
|
|
||||||
lua_insert(L, 1);
|
|
||||||
|
|
||||||
/* Call constructor */
|
|
||||||
lua_insert(L, 2);
|
|
||||||
lua_call(L, lua_gettop(L)-2, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
lua_settop(L, 1);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* luci.cutil.class(baseclass) */
|
|
||||||
static int luci_class(lua_State *L) {
|
|
||||||
/* Create class */
|
|
||||||
lua_newtable(L);
|
|
||||||
|
|
||||||
/* Create metatable and register parent class if any */
|
|
||||||
if (lua_gettop(L) > 1 && lua_istable(L, 1)) {
|
|
||||||
lua_createtable(L, 0, 2);
|
|
||||||
lua_pushvalue(L, 1);
|
|
||||||
lua_setfield(L, -2, "__index");
|
|
||||||
} else {
|
|
||||||
lua_createtable(L, 0, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Set instantiator */
|
|
||||||
lua_pushcfunction(L, luci__instantiate);
|
|
||||||
lua_setfield(L, -2, "__call");
|
|
||||||
|
|
||||||
lua_setmetatable(L, -2);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* luci.cutil.instanceof(object, class) */
|
|
||||||
static int luci_instanceof(lua_State *L) {
|
|
||||||
int stat = 0;
|
|
||||||
|
|
||||||
luaL_checkany(L, 1);
|
|
||||||
luaL_checkany(L, 2);
|
|
||||||
|
|
||||||
if (lua_getmetatable(L, 1)) {
|
|
||||||
/* get parent class */
|
|
||||||
lua_getfield(L, -1, "__index");
|
|
||||||
while (lua_istable(L, -1)) {
|
|
||||||
/* parent class == class */
|
|
||||||
if (lua_equal(L, -1, 2)) {
|
|
||||||
stat = 1;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* remove last metatable */
|
|
||||||
lua_remove(L, -2);
|
|
||||||
|
|
||||||
/* get metatable of parent class */
|
|
||||||
if (lua_getmetatable(L, -1)) {
|
|
||||||
/* remove last parent class */
|
|
||||||
lua_remove(L, -2);
|
|
||||||
|
|
||||||
/* get next parent class */
|
|
||||||
lua_getfield(L, -1, "__index");
|
|
||||||
} else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
lua_pushboolean(L, stat);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* luci.cutil.pcdata(obj) */
|
|
||||||
static int luci_pcdata(lua_State *L) {
|
|
||||||
if (lua_isnone(L, 1)) {
|
|
||||||
lua_pushnil(L);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
luaL_checkstring(L, 1);
|
|
||||||
|
|
||||||
/* Discard anything else */
|
|
||||||
lua_settop(L, 1);
|
|
||||||
|
|
||||||
/* pattern */
|
|
||||||
lua_pushvalue(L, lua_upvalueindex(1));
|
|
||||||
|
|
||||||
/* repl */
|
|
||||||
lua_pushvalue(L, lua_upvalueindex(2));
|
|
||||||
|
|
||||||
/* get gsub function */
|
|
||||||
lua_getfield(L, 1, "gsub");
|
|
||||||
lua_insert(L, 1);
|
|
||||||
|
|
||||||
/* tostring(obj):gsub(pattern, repl) */
|
|
||||||
lua_call(L, 3, 1);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* luci.cutil.trim(str) */
|
|
||||||
static int luci_trim(lua_State *L) {
|
|
||||||
luaL_checkstring(L, 1);
|
|
||||||
lua_settop(L, 1);
|
|
||||||
|
|
||||||
/* pattern and repl */
|
|
||||||
lua_pushliteral(L, "^%s*(.-)%s*$");
|
|
||||||
lua_pushliteral(L, "%1");
|
|
||||||
|
|
||||||
/* get str.gsub */
|
|
||||||
lua_getfield(L, 1, "gsub");
|
|
||||||
lua_insert(L, 1);
|
|
||||||
|
|
||||||
/* str.gsub(str, pattern, repl) */
|
|
||||||
lua_call(L, 3, 1);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* Registration helper for luci.cutil.pcdata */
|
|
||||||
static void luci__register_pcdata(lua_State *L) {
|
|
||||||
/* pattern */
|
|
||||||
lua_pushliteral(L, "[&\"'<>]");
|
|
||||||
|
|
||||||
/* repl */
|
|
||||||
lua_createtable(L, 0, 5);
|
|
||||||
|
|
||||||
lua_pushliteral(L, "&");
|
|
||||||
lua_setfield(L, -2, "&");
|
|
||||||
lua_pushliteral(L, """);
|
|
||||||
lua_setfield(L, -2, "\"");
|
|
||||||
lua_pushliteral(L, "'");
|
|
||||||
lua_setfield(L, -2, "'");
|
|
||||||
lua_pushliteral(L, "<");
|
|
||||||
lua_setfield(L, -2, "<");
|
|
||||||
lua_pushliteral(L, ">");
|
|
||||||
lua_setfield(L, -2, ">");
|
|
||||||
|
|
||||||
/* register function */
|
|
||||||
lua_pushcclosure(L, luci_pcdata, 2);
|
|
||||||
lua_setfield(L, -2, "pcdata");
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Registry */
|
|
||||||
static const luaL_Reg registry[] = {
|
|
||||||
{"class", luci_class},
|
|
||||||
{"instanceof", luci_instanceof},
|
|
||||||
{"trim", luci_trim},
|
|
||||||
{ NULL, NULL },
|
|
||||||
};
|
|
||||||
|
|
||||||
/* Registrator */
|
|
||||||
LUALIB_API int luaopen_luci_cutil(lua_State *L) {
|
|
||||||
luaL_register(L, LUCI_MODNAME, registry);
|
|
||||||
|
|
||||||
lua_pushliteral(L, LUCI_MODDESC);
|
|
||||||
lua_setfield(L, -2, "_DESCRIPTION");
|
|
||||||
|
|
||||||
lua_pushliteral(L, LUCI_MODCOPY);
|
|
||||||
lua_setfield(L, -2, "_COPYRIGHT");
|
|
||||||
|
|
||||||
/* Additional registrations */
|
|
||||||
luci__register_pcdata(L);
|
|
||||||
|
|
||||||
|
|
||||||
/* Register pythonic printf string operator */
|
|
||||||
lua_pushliteral(L, "");
|
|
||||||
lua_getmetatable(L, -1);
|
|
||||||
lua_pushcfunction(L, luci__string_mod);
|
|
||||||
lua_setfield(L, -2, "__mod");
|
|
||||||
lua_pop(L, 2);
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
Loading…
Reference in a new issue