More luci.cutil

This commit is contained in:
Steven Barth 2008-11-20 19:39:40 +00:00
parent 9c260769a5
commit a3cf596d37
2 changed files with 27 additions and 12 deletions

View file

@ -271,9 +271,12 @@ 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

View file

@ -153,23 +153,19 @@ static int luci_pcdata(lua_State *L) {
lua_pushnil(L); lua_pushnil(L);
return 1; return 1;
} }
luaL_checkstring(L, 1);
/* Discard anything else */ /* Discard anything else */
lua_settop(L, 1); lua_settop(L, 1);
/* tostring(obj) */
lua_pushvalue(L, lua_upvalueindex(1));
lua_insert(L, 1);
lua_call(L, 1, 1);
/* pattern */ /* pattern */
lua_pushvalue(L, lua_upvalueindex(2)); lua_pushvalue(L, lua_upvalueindex(1));
/* repl */ /* repl */
lua_pushvalue(L, lua_upvalueindex(3)); lua_pushvalue(L, lua_upvalueindex(2));
/* get gsub function */ /* get gsub function */
lua_getfield(L, -3, "gsub"); lua_getfield(L, 1, "gsub");
lua_insert(L, 1); lua_insert(L, 1);
/* tostring(obj):gsub(pattern, repl) */ /* tostring(obj):gsub(pattern, repl) */
@ -177,12 +173,27 @@ static int luci_pcdata(lua_State *L) {
return 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 */ /* Registration helper for luci.cutil.pcdata */
static void luci__register_pcdata(lua_State *L) { static void luci__register_pcdata(lua_State *L) {
/* tostring */
lua_getfield(L, LUA_GLOBALSINDEX, "tostring");
/* pattern */ /* pattern */
lua_pushliteral(L, "[&\"'<>]"); lua_pushliteral(L, "[&\"'<>]");
@ -201,7 +212,7 @@ static void luci__register_pcdata(lua_State *L) {
lua_setfield(L, -2, ">"); lua_setfield(L, -2, ">");
/* register function */ /* register function */
lua_pushcclosure(L, luci_pcdata, 3); lua_pushcclosure(L, luci_pcdata, 2);
lua_setfield(L, -2, "pcdata"); lua_setfield(L, -2, "pcdata");
} }
@ -209,6 +220,7 @@ static void luci__register_pcdata(lua_State *L) {
static const luaL_Reg registry[] = { static const luaL_Reg registry[] = {
{"class", luci_class}, {"class", luci_class},
{"instanceof", luci_instanceof}, {"instanceof", luci_instanceof},
{"trim", luci_trim},
{ NULL, NULL }, { NULL, NULL },
}; };