commit 9cd6c30: [Minor] Core: Add function to strip characters in gstring

Vsevolod Stakhov vsevolod at highsecure.ru
Thu Jan 17 15:00:09 UTC 2019


Author: Vsevolod Stakhov
Date: 2019-01-17 14:57:24 +0000
URL: https://github.com/rspamd/rspamd/commit/9cd6c30cd0fb6c8c1189d1cddeb1d52d4e687251

[Minor] Core: Add function to strip characters in gstring

---
 src/libutil/str_util.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
 src/libutil/str_util.h |  7 +++++++
 2 files changed, 56 insertions(+)

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_ */


More information about the Commits mailing list