From: Vsevolod Stakhov Date: Sat, 7 Sep 2019 12:35:19 +0000 (+0100) Subject: [Minor] Allow to index lua text X-Git-Tag: 2.0~260 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=9be69321212f756a97643e417bf9ed563823f1b4;p=rspamd.git [Minor] Allow to index lua text --- diff --git a/src/lua/lua_text.c b/src/lua/lua_text.c index a41775230..e095b8c43 100644 --- a/src/lua/lua_text.c +++ b/src/lua/lua_text.c @@ -74,6 +74,13 @@ LUA_FUNCTION_DEF (text, save_in_file); * @return {rspamd_text} new rspamd_text with span (must be careful when using with owned texts...) */ LUA_FUNCTION_DEF (text, span); +/*** + * @method rspamd_text:at(pos) + * Returns a byte at the position `pos` + * @param {integer} pos index + * @return {integer} byte at the position `pos` or nil if pos out of bound + */ +LUA_FUNCTION_DEF (text, at); LUA_FUNCTION_DEF (text, take_ownership); LUA_FUNCTION_DEF (text, gc); LUA_FUNCTION_DEF (text, eq); @@ -341,6 +348,28 @@ lua_text_span (lua_State *L) return 1; } +static gint +lua_text_at (lua_State *L) +{ + LUA_TRACE_POINT; + struct rspamd_lua_text *t = lua_check_text (L, 1); + gint pos = lua_tointeger (L, 2); + + if (t) { + if (pos > 0 && pos <= t->len) { + lua_pushinteger (L, t->start[pos - 1]); + } + else { + lua_pushnil (L); + } + } + else { + return luaL_error (L, "invalid arguments"); + } + + return 1; +} + static gint lua_text_save_in_file (lua_State *L) {