diff options
author | Steven Barth <steven@midlink.org> | 2008-11-20 19:39:40 +0000 |
---|---|---|
committer | Steven Barth <steven@midlink.org> | 2008-11-20 19:39:40 +0000 |
commit | a3cf596d37e4116a7068daaa724270a0898cb7da (patch) | |
tree | c064825fc434a23903d29479bf26940224dc878b | |
parent | 9c260769a50956e9ece40a10e226da8e7191158c (diff) |
More luci.cutil
-rw-r--r-- | libs/core/luasrc/util.lua | 3 | ||||
-rw-r--r-- | libs/core/src/luci_cutil.c | 36 |
2 files changed, 27 insertions, 12 deletions
diff --git a/libs/core/luasrc/util.lua b/libs/core/luasrc/util.lua index e493811256..53e46a99dc 100644 --- a/libs/core/luasrc/util.lua +++ b/libs/core/luasrc/util.lua @@ -271,9 +271,12 @@ end --- Remove leading and trailing whitespace from given string value. -- @param str String value containing whitespace padded data -- @return String value with leading and trailing space removed +--[[ function trim(str) return (str:gsub("^%s*(.-)%s*$", "%1")) end +]]-- +trim = cutil.trim --- Count the occurences of given substring in given string. -- @param str String to search in diff --git a/libs/core/src/luci_cutil.c b/libs/core/src/luci_cutil.c index 31bec041d0..49a33ac940 100644 --- a/libs/core/src/luci_cutil.c +++ b/libs/core/src/luci_cutil.c @@ -153,23 +153,19 @@ static int luci_pcdata(lua_State *L) { lua_pushnil(L); return 1; } + luaL_checkstring(L, 1); /* Discard anything else */ lua_settop(L, 1); - /* tostring(obj) */ - lua_pushvalue(L, lua_upvalueindex(1)); - lua_insert(L, 1); - lua_call(L, 1, 1); - /* pattern */ - lua_pushvalue(L, lua_upvalueindex(2)); + lua_pushvalue(L, lua_upvalueindex(1)); /* repl */ - lua_pushvalue(L, lua_upvalueindex(3)); + lua_pushvalue(L, lua_upvalueindex(2)); /* get gsub function */ - lua_getfield(L, -3, "gsub"); + lua_getfield(L, 1, "gsub"); lua_insert(L, 1); /* tostring(obj):gsub(pattern, repl) */ @@ -177,12 +173,27 @@ static int luci_pcdata(lua_State *L) { 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) { - /* tostring */ - lua_getfield(L, LUA_GLOBALSINDEX, "tostring"); - /* pattern */ lua_pushliteral(L, "[&\"'<>]"); @@ -201,7 +212,7 @@ static void luci__register_pcdata(lua_State *L) { lua_setfield(L, -2, ">"); /* register function */ - lua_pushcclosure(L, luci_pcdata, 3); + lua_pushcclosure(L, luci_pcdata, 2); lua_setfield(L, -2, "pcdata"); } @@ -209,6 +220,7 @@ static void luci__register_pcdata(lua_State *L) { static const luaL_Reg registry[] = { {"class", luci_class}, {"instanceof", luci_instanceof}, + {"trim", luci_trim}, { NULL, NULL }, }; |