commit ae73d81: Merge pull request #3553 from fatalbanana/byte
GitHub
noreply at github.com
Mon Nov 16 19:14:07 UTC 2020
Author: Vsevolod Stakhov
Date: 2020-11-16 19:12:59 +0000
URL: https://github.com/rspamd/rspamd/commit/ae73d813970522f5f3de5399ddde0f1f367c9479 (HEAD -> master)
Merge pull request #3553 from fatalbanana/byte
[Minor] rspamd_text:byte() metamethod
contrib/lua-fun/fun.lua | 2 +-
lualib/lua_content/pdf.lua | 2 +-
lualib/lua_magic/heuristics.lua | 2 +-
src/lua/lua_text.c | 40 ++++++++++++++++++++++++++++------------
test/lua/unit/rspamd_text.lua | 30 ++++++++++++++++++++++++++++++
5 files changed, 61 insertions(+), 15 deletions(-)
diff --combined src/lua/lua_text.c
index feef9111f,37e1752c1..99b8b8151
--- a/src/lua/lua_text.c
+++ b/src/lua/lua_text.c
@@@ -58,6 -58,14 +58,14 @@@ LUA_FUNCTION_DEF (text, randombytes)
* @return {rspamd_text} resulting text
*/
LUA_FUNCTION_DEF (text, fromtable);
+ /***
+ * @method rspamd_text:byte(pos[, pos2])
+ * Returns a byte at the position `pos` or bytes from `pos` to `pos2` if specified
+ * @param {integer} pos index
+ * @param {integer} pos2 index
+ * @return {integer} byte at the position `pos` or varargs of bytes
+ */
+ LUA_FUNCTION_DEF (text, byte);
/***
* @method rspamd_text:len()
* Returns length of a string
@@@ -139,7 -147,7 +147,7 @@@ LUA_FUNCTION_DEF (text, bytes)
* Return a new text with lowercased characters, if is_utf is true then Rspamd applies utf8 lowercase
* @param {boolean} is_utf apply utf8 lowercase
* @param {boolean} inplace lowercase the original text
- * @return rspamd_text} new rspamd_text (or the original text if inplace) with lowercased letters
+ * @return {rspamd_text} new rspamd_text (or the original text if inplace) with lowercased letters
*/
LUA_FUNCTION_DEF (text, lower);
LUA_FUNCTION_DEF (text, take_ownership);
@@@ -157,7 -165,7 +165,7 @@@
*
* @param {string} set_to_exclude characters to exclude
* @param {boolean} always_copy always copy the source text
- * @return {tspamd_text} modified or copied text
+ * @return {rspamd_text} modified or copied text
*/
LUA_FUNCTION_DEF (text, exclude_chars);
/***
@@@ -171,7 -179,7 +179,7 @@@
* - UTF8 sequences are normalised
*
* @param {boolean} always_copy always copy the source text
- * @return {tspamd_text} modified or copied text
+ * @return {rspamd_text} modified or copied text
*/
LUA_FUNCTION_DEF (text, oneline);
/***
@@@ -179,7 -187,7 +187,7 @@@
* Returns a text encoded in base32 (new rspamd_text is allocated)
*
* @param {string} b32type base32 type (default, bleach, rfc)
- * @return {tspamd_text} new text encoded in base32
+ * @return {rspamd_text} new text encoded in base32
*/
LUA_FUNCTION_DEF (text, base32);
/***
@@@ -189,30 -197,16 +197,30 @@@
* @param {number} line_length return text splited with newlines up to this attribute
* @param {string} nline newline type: `cr`, `lf`, `crlf`
* @param {boolean} fold use folding when splitting into lines (false by default)
- * @return {tspamd_text} new text encoded in base64
+ * @return {rspamd_text} new text encoded in base64
*/
LUA_FUNCTION_DEF (text, base64);
/***
* @method rspamd_text:hex()
* Returns a text encoded in hex (new rspamd_text is allocated)
*
- * @return {tspamd_text} new text encoded in hex
+ * @return {rspamd_text} new text encoded in hex
*/
LUA_FUNCTION_DEF (text, hex);
+/***
+ * @method rspamd_text:find(pattern [, init])
+ * Looks for the first match of pattern in the string s.
+ * If it finds a match, then find returns the indices of s where this occurrence
+ * starts and ends; otherwise, it returns nil. A third,
+ * optional numerical argument init specifies where to start the search;
+ * its default value is 1 and can be negative.
+ * This method currently supports merely a plain search, no patterns.
+ *
+ * @param {string} pattern pattern to find
+ * @param {number} init specifies where to start the search (1 default)
+ * @return {number,number/nil} If it finds a match, then find returns the indices of s where this occurrence starts and ends; otherwise, it returns nil
+ */
+LUA_FUNCTION_DEF (text, find);
LUA_FUNCTION_DEF (text, gc);
LUA_FUNCTION_DEF (text, eq);
LUA_FUNCTION_DEF (text, lt);
@@@ -240,6 -234,7 +248,7 @@@ static const struct luaL_reg textlib_m[
LUA_INTERFACE_DEF (text, split),
LUA_INTERFACE_DEF (text, at),
LUA_INTERFACE_DEF (text, memchr),
+ LUA_INTERFACE_DEF (text, byte),
LUA_INTERFACE_DEF (text, bytes),
LUA_INTERFACE_DEF (text, lower),
LUA_INTERFACE_DEF (text, exclude_chars),
@@@ -247,7 -242,6 +256,7 @@@
LUA_INTERFACE_DEF (text, base32),
LUA_INTERFACE_DEF (text, base64),
LUA_INTERFACE_DEF (text, hex),
+ LUA_INTERFACE_DEF (text, find),
{"write", lua_text_save_in_file},
{"__len", lua_text_len},
{"__tostring", lua_text_str},
@@@ -961,24 -955,31 +970,31 @@@ lua_text_split (lua_State *L
static gint
lua_text_at (lua_State *L)
+ {
+ return lua_text_byte(L);
+ }
+
+ static gint
+ lua_text_byte (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 {
+ if (!t) {
return luaL_error (L, "invalid arguments");
}
- return 1;
+ gsize start = relative_pos_start (luaL_optinteger (L, 2, 1), t->len);
+ gsize end = relative_pos_end (luaL_optinteger (L, 3, start), t->len);
+ start--;
+
+ if (start >= end) {
+ return 0;
+ }
+
+ for (gsize i = start; i < end; i++) {
+ lua_pushinteger (L, t->start[i]);
+ }
+ return end - start;
}
static gint
@@@ -1328,44 -1329,6 +1344,44 @@@ lua_text_hex (lua_State *L
return 1;
}
+static gint
+lua_text_find (lua_State *L)
+{
+ LUA_TRACE_POINT;
+ struct rspamd_lua_text *t = lua_check_text (L, 1);
+ gsize patlen, init = 1;
+ const gchar *pat = luaL_checklstring (L, 2, &patlen);
+
+ if (t != NULL && pat != NULL) {
+
+ if (lua_isnumber (L, 3)) {
+ init = relative_pos_start (lua_tointeger (L, 3), t->len);
+ }
+
+ if (init >= t->len) {
+ return luaL_error (L, "invalid arguments to find: init too large");
+ }
+
+ goffset pos = rspamd_substring_search (t->start + init - 1,
+ t->len - (init - 1),
+ pat, patlen);
+
+ if (pos == -1) {
+ lua_pushnil (L);
+
+ return 1;
+ }
+
+ lua_pushinteger (L, pos + 1);
+ lua_pushinteger (L, pos + patlen);
+ }
+ else {
+ return luaL_error (L, "invalid arguments");
+ }
+
+ return 2;
+}
+
#define BITOP(a,b,op) \
((a)[(gsize)(b)/(8*sizeof *(a))] op (gsize)1<<((gsize)(b)%(8*sizeof *(a))))
More information about the Commits
mailing list