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.cpp | 7 +++++++ plugins/MirLua/src/mlua_utils.cpp | 30 +++++++++++++++++++++++++++--- plugins/MirLua/src/stdafx.h | 2 ++ plugins/MirLua/src/version.h | 2 +- 4 files changed, 37 insertions(+), 4 deletions(-) (limited to 'plugins') diff --git a/plugins/MirLua/src/mlua.cpp b/plugins/MirLua/src/mlua.cpp index f2cdafb41b..8a9b0ce87c 100644 --- a/plugins/MirLua/src/mlua.cpp +++ b/plugins/MirLua/src/mlua.cpp @@ -56,6 +56,13 @@ void CMLua::Load() lua_setglobal(L, "_tonumber"); lua_register(L, "tonumber", luaM_tonumber); + lua_pushstring(L, ""); + lua_getmetatable(L, -1); + lua_pushstring(L, "__mod"); + lua_pushcfunction(L, luaM_interpolate); + lua_rawset(L, -3); + lua_pop(L, 2); + lua_atpanic(L, luaM_atpanic); Log("Loading miranda modules"); 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)) diff --git a/plugins/MirLua/src/stdafx.h b/plugins/MirLua/src/stdafx.h index 48218e8651..921d2c70c4 100644 --- a/plugins/MirLua/src/stdafx.h +++ b/plugins/MirLua/src/stdafx.h @@ -115,6 +115,8 @@ int luaM_toucs2(lua_State *L); int luaM_topointer(lua_State *L); int luaM_tonumber(lua_State *L); +int luaM_interpolate(lua_State *L); + WPARAM luaM_towparam(lua_State *L, int idx); LPARAM luaM_tolparam(lua_State *L, int idx); diff --git a/plugins/MirLua/src/version.h b/plugins/MirLua/src/version.h index c410df9718..b9b7e3011f 100644 --- a/plugins/MirLua/src/version.h +++ b/plugins/MirLua/src/version.h @@ -1,7 +1,7 @@ #define __MAJOR_VERSION 0 #define __MINOR_VERSION 11 #define __RELEASE_NUM 7 -#define __BUILD_NUM 0 +#define __BUILD_NUM 1 #include -- cgit v1.2.3