From cffb2b2c6934428fc5f677b2330eaf9cb438909e Mon Sep 17 00:00:00 2001 From: Alexander Lantsev Date: Sun, 17 Apr 2016 16:38:49 +0000 Subject: MirLua: - added string interpolation: print('some {val} string' % { val = 'cool' }) - version bump git-svn-id: http://svn.miranda-ng.org/main/trunk@16699 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/MirLua/src/mlua_utils.cpp | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 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 ea032b052e..3bb82e8fb3 100644 --- a/plugins/MirLua/src/mlua_utils.cpp +++ b/plugins/MirLua/src/mlua_utils.cpp @@ -126,15 +126,15 @@ int luaM_topointer(lua_State *L) if (lua_isinteger(L, 1)) { lua_Integer value = lua_tointeger(L, 1); - if (value > INT_MAX) + if (value > INTPTR_MAX) { - const char *msg = lua_pushfstring(L, "%f is larger than %d", value, INT_MAX); + const char *msg = lua_pushfstring(L, "%f is larger than %d", value, INTPTR_MAX); return luaL_argerror(L, 1, msg); } lua_pushlightuserdata(L, (void*)value); } } - break; + break; case LUA_TSTRING: lua_pushlightuserdata(L, (void*)lua_tostring(L, 1)); break; @@ -165,6 +165,30 @@ int luaM_tonumber(lua_State *L) return 1; } +int luaM_interpolate(lua_State *L) +{ + const char *string = luaL_checkstring(L, 1); + luaL_checktype(L, 2, LUA_TTABLE); + + lua_pushnil(L); + while (lua_next(L, -2) != 0) + { + const char *key = luaL_checkstring(L, -2); + const char *val = lua_tostring(L, -1); + + char pattern[32]; + mir_snprintf(pattern, "{%s}", key); + string = luaL_gsub(L, string, pattern, val); + lua_pop(L, 1); + + lua_pop(L, 1); + } + + lua_pushstring(L, string); + + return 1; +} + WPARAM luaM_towparam(lua_State *L, int idx) { if (lua_islightuserdata(L, idx)) -- cgit v1.2.3