]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Allow to index lua text
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Sat, 7 Sep 2019 12:35:19 +0000 (13:35 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Sat, 7 Sep 2019 12:35:19 +0000 (13:35 +0100)
src/lua/lua_text.c

index a41775230d6a18580caa2c128aacb186fdd5be7b..e095b8c4333cd98df1f27fed8f7391e64f95bd60 100644 (file)
@@ -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)
 {