From 9957a53c3d6cad360297306214c13d804d99bdbf Mon Sep 17 00:00:00 2001 From: Alexander Lantsev Date: Sun, 17 Apr 2016 17:29:53 +0000 Subject: 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 --- plugins/MirLua/src/mlua_utils.cpp | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) (limited to 'plugins/MirLua/src/mlua_utils.cpp') 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); -- cgit v1.2.3