diff options
-rw-r--r-- | src/libutil/str_util.c | 49 | ||||
-rw-r--r-- | src/libutil/str_util.h | 7 |
2 files changed, 56 insertions, 0 deletions
diff --git a/src/libutil/str_util.c b/src/libutil/str_util.c index 2016808cf..e72962904 100644 --- a/src/libutil/str_util.c +++ b/src/libutil/str_util.c @@ -2751,4 +2751,53 @@ rspamd_str_make_utf_valid (const gchar *src, gsize slen, gsize *dstlen) g_string_free (dst, FALSE); return dchar; +} + +gsize +rspamd_gstring_strip (GString *s, const gchar *strip_chars) +{ + const gchar *p, *sc; + gsize strip_len = 0, total = 0; + + p = s->str + s->len - 1; + + while (p >= s->str) { + gboolean seen = FALSE; + + sc = strip_chars; + + while (*sc != '\0') { + if (*p == *sc) { + strip_len ++; + seen = TRUE; + break; + } + + sc ++; + } + + if (!seen) { + break; + } + + p --; + } + + if (strip_len > 0) { + s->len -= strip_len; + s->str[s->len] = '\0'; + total += strip_len; + } + + if (s->len > 0) { + strip_len = rspamd_memspn (s->str, strip_chars, s->len); + + if (strip_len > 0) { + memmove (s->str, s->str + strip_len, s->len - strip_len); + s->len -= strip_len; + total += strip_len; + } + } + + return total; }
\ No newline at end of file diff --git a/src/libutil/str_util.h b/src/libutil/str_util.h index 059665388..a2669d1a0 100644 --- a/src/libutil/str_util.h +++ b/src/libutil/str_util.h @@ -447,6 +447,13 @@ rspamd_str_regexp_escape (const gchar *pattern, gsize slen, */ gchar * rspamd_str_make_utf_valid (const gchar *src, gsize slen, gsize *dstlen); +/** + * Strips characters in `strip_chars` from start and end of the GString + * @param s + * @param strip_chars + */ +gsize rspamd_gstring_strip (GString *s, const gchar *strip_chars); + #define IS_ZERO_WIDTH_SPACE(uc) ((uc) == 0x200b || (uc) == 0x200c) #endif /* SRC_LIBUTIL_STR_UTIL_H_ */ |