diff options
author | Alexander Lantsev <aunsane@gmail.com> | 2016-04-17 17:29:53 +0000 |
---|---|---|
committer | Alexander Lantsev <aunsane@gmail.com> | 2016-04-17 17:29:53 +0000 |
commit | 9957a53c3d6cad360297306214c13d804d99bdbf (patch) | |
tree | cfb07495cf6fc76a7b314c25d21cc2c2179e6ab4 | |
parent | 563a5c7cdbdafd3991d4a542bb32224a3ab0816d (diff) |
MirLua: added function interpolate into string module: print(string.interpolate('some {1} string', 'cool'))
git-svn-id: http://svn.miranda-ng.org/main/trunk@16704 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
-rw-r--r-- | plugins/MirLua/src/mlua.cpp | 6 | ||||
-rw-r--r-- | plugins/MirLua/src/mlua_utils.cpp | 35 |
2 files changed, 30 insertions, 11 deletions
diff --git a/plugins/MirLua/src/mlua.cpp b/plugins/MirLua/src/mlua.cpp index 8a9b0ce87c..afa7d7b04c 100644 --- a/plugins/MirLua/src/mlua.cpp +++ b/plugins/MirLua/src/mlua.cpp @@ -61,7 +61,11 @@ void CMLua::Load() lua_pushstring(L, "__mod");
lua_pushcfunction(L, luaM_interpolate);
lua_rawset(L, -3);
- lua_pop(L, 2);
+ lua_pushstring(L, "__index");
+ lua_rawget(L, -2);
+ lua_pushcfunction(L, luaM_interpolate);
+ lua_setfield(L, -2, "interpolate");
+ lua_pop(L, 3);
lua_atpanic(L, luaM_atpanic);
diff --git a/plugins/MirLua/src/mlua_utils.cpp b/plugins/MirLua/src/mlua_utils.cpp index c0b6629ffc..d5ca5ed0bb 100644 --- a/plugins/MirLua/src/mlua_utils.cpp +++ b/plugins/MirLua/src/mlua_utils.cpp @@ -168,18 +168,33 @@ int luaM_tonumber(lua_State *L) int luaM_interpolate(lua_State *L)
{
const char *string = luaL_checkstring(L, 1);
- luaL_checktype(L, 2, LUA_TTABLE);
- for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 2))
+ if (lua_istable(L, 2))
{
- lua_pushvalue(L, -2);
- const char *key = lua_tostring(L, -1);
- const char *val = lua_tostring(L, -2);
-
- char pattern[32];
- mir_snprintf(pattern, "{%s}", key);
- string = luaL_gsub(L, string, pattern, val);
- lua_pop(L, 1);
+ for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 2))
+ {
+ lua_pushvalue(L, -2);
+ const char *key = lua_tostring(L, -1);
+ const char *val = lua_tostring(L, -2);
+
+ char pattern[32];
+ mir_snprintf(pattern, "{%s}", key);
+ string = luaL_gsub(L, string, pattern, val);
+ lua_pop(L, 1);
+ }
+ }
+ else
+ {
+ int nargs = lua_gettop(L);
+ for (int i = 2; i <= nargs; i++)
+ {
+ const char *val = lua_tostring(L, i);
+
+ char pattern[32];
+ mir_snprintf(pattern, "{%d}", i - 1);
+ string = luaL_gsub(L, string, pattern, val);
+ lua_pop(L, 1);
+ }
}
lua_pushstring(L, string);
|