diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2019-09-07 13:35:19 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2019-09-07 13:35:19 +0100 |
commit | 9be69321212f756a97643e417bf9ed563823f1b4 (patch) | |
tree | a8a2fa24bd482154b4eed9fea6b68e2d509aecc0 | |
parent | 859483618be3602ea3c405f944595f8c0d06e720 (diff) | |
download | rspamd-9be69321212f756a97643e417bf9ed563823f1b4.tar.gz rspamd-9be69321212f756a97643e417bf9ed563823f1b4.zip |
[Minor] Allow to index lua text
-rw-r--r-- | src/lua/lua_text.c | 29 |
1 files changed, 29 insertions, 0 deletions
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); @@ -342,6 +349,28 @@ lua_text_span (lua_State *L) } 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) { LUA_TRACE_POINT; |