]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Add function to strip characters
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 6 Feb 2019 14:17:43 +0000 (14:17 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 6 Feb 2019 14:17:43 +0000 (14:17 +0000)
src/libutil/str_util.c
src/libutil/str_util.h

index 2e09e48f3e2f1fd324a0821f9034aaaf17600119..7078415d32b9e2f3ea9c788f42e0e7bf91cff624 100644 (file)
@@ -2826,4 +2826,56 @@ rspamd_gstring_strip (GString *s, const gchar *strip_chars)
        }
 
        return total;
+}
+
+const gchar* rspamd_string_len_strip (const gchar *in,
+                                                                         gsize *len,
+                                                                         const gchar *strip_chars)
+{
+       const gchar *p, *sc;
+       gsize strip_len = 0, old_len = *len;
+
+       p = in + old_len - 1;
+
+       /* Trail */
+       while (p >= in) {
+               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) {
+               *len -= strip_len;
+       }
+
+       /* Head */
+       old_len = *len;
+
+       if (old_len > 0) {
+               strip_len = rspamd_memspn (in, strip_chars, old_len);
+
+               if (strip_len > 0) {
+                       *len -= strip_len;
+
+                       return in + strip_len;
+               }
+       }
+
+       return in;
 }
\ No newline at end of file
index 5e3d7a201b51c0f29c8634647cbb1aadd4991966..72e0ab56f3da40ae3824875bf3babafe4b22e9a8 100644 (file)
@@ -454,6 +454,14 @@ gchar * rspamd_str_make_utf_valid (const gchar *src, gsize slen, gsize *dstlen);
  */
 gsize rspamd_gstring_strip (GString *s, const gchar *strip_chars);
 
+/**
+ * Strips characters in `strip_chars` from start and end of the sized string
+ * @param s
+ * @param strip_chars
+ */
+const gchar* rspamd_string_len_strip (const gchar *in,
+               gsize *len, const gchar *strip_chars);
+
 #define IS_ZERO_WIDTH_SPACE(uc) ((uc) == 0x200B || \
                                                                (uc) == 0x200C || \
                                                                (uc) == 0x200D || \