aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2018-06-21 14:20:13 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2018-06-21 14:20:13 +0100
commit424bf837f936bb2d02c06c839d08139a871f36d6 (patch)
treedd7ed137d5455a917f2c10f153ba11abd640355e /src
parentcf88a6243a093b71340c17920b0ce8d225cd9466 (diff)
downloadrspamd-424bf837f936bb2d02c06c839d08139a871f36d6.tar.gz
rspamd-424bf837f936bb2d02c06c839d08139a871f36d6.zip
[Minor] Add rspamd_memspn function
Diffstat (limited to 'src')
-rw-r--r--src/libutil/str_util.c26
-rw-r--r--src/libutil/str_util.h8
2 files changed, 34 insertions, 0 deletions
diff --git a/src/libutil/str_util.c b/src/libutil/str_util.c
index 026e331fe..cfacf514f 100644
--- a/src/libutil/str_util.c
+++ b/src/libutil/str_util.c
@@ -1738,12 +1738,19 @@ decode:
#define BITOP(a,b,op) \
((a)[(gsize)(b)/(8*sizeof *(a))] op (gsize)1<<((gsize)(b)%(8*sizeof *(a))))
+
+
gsize
rspamd_memcspn (const gchar *s, const gchar *e, gsize len)
{
gsize byteset[32 / sizeof(gsize)];
const gchar *p = s, *end = s + len;
+ if (!e[1]) {
+ for (; *p != *e; p++);
+ return p - s;
+ }
+
memset (byteset, 0, sizeof byteset);
for (; *e && BITOP (byteset, *(guchar *)e, |=); e++);
@@ -1752,6 +1759,25 @@ rspamd_memcspn (const gchar *s, const gchar *e, gsize len)
return p - s;
}
+gsize
+rspamd_memspn (const gchar *s, const gchar *e, gsize len)
+{
+ gsize byteset[32 / sizeof(gsize)];
+ const gchar *p = s, *end = s + len;
+
+ if (!e[1]) {
+ for (; *p == *e; p++);
+ return p - s;
+ }
+
+ memset (byteset, 0, sizeof byteset);
+
+ for (; *e && BITOP (byteset, *(guchar *)e, |=); e++);
+ for (; p < end && BITOP (byteset, *(guchar *)p, &); p++);
+
+ return p - s;
+}
+
gssize
rspamd_decode_qp2047_buf (const gchar *in, gsize inlen,
gchar *out, gsize outlen)
diff --git a/src/libutil/str_util.h b/src/libutil/str_util.h
index 45507e2be..9fa914292 100644
--- a/src/libutil/str_util.h
+++ b/src/libutil/str_util.h
@@ -334,6 +334,14 @@ const void *rspamd_memrchr (const void *m, gint c, gsize len);
*/
gsize rspamd_memcspn (const gchar *s, const gchar *e, gsize len);
+/**
+ * Return length of memory segment starting in `s` that contains only chars from `e`
+ * @param s any input
+ * @param e zero terminated string of inclusions
+ * @param len length of `s`
+ * @return segment size
+ */
+gsize rspamd_memspn (const gchar *s, const gchar *e, gsize len);
/* https://graphics.stanford.edu/~seander/bithacks.html#HasMoreInWord */
#define rspamd_str_hasmore(x,n) ((((x)+~0UL/255*(127-(n)))|(x))&~0UL/255*128)