From 9be69321212f756a97643e417bf9ed563823f1b4 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Sat, 7 Sep 2019 13:35:19 +0100 Subject: [PATCH] [Minor] Allow to index lua text --- src/lua/lua_text.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) 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) { -- 2.39.5