]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: Created t_str_trim() functions to trim characters from beginning and end of...
authorStephan Bosch <stephan@rename-it.nl>
Sat, 28 Nov 2015 21:50:14 +0000 (23:50 +0200)
committerStephan Bosch <stephan@rename-it.nl>
Sat, 28 Nov 2015 21:50:14 +0000 (23:50 +0200)
src/lib/strfuncs.c
src/lib/strfuncs.h
src/lib/test-strfuncs.c

index 72b2758d862315fafba59c7b95ea6bcff0c7828c..20df783c5715b0d80d5a66bccf2150c27f362515 100644 (file)
@@ -364,6 +364,63 @@ const char *t_str_ucase(const char *str)
        return str_ucase(t_strdup_noconst(str));
 }
 
+const char *t_str_trim(const char *str, const char *chars)
+{
+       const char *p, *pend, *begin;
+
+       pend = str + strlen(str);
+       if (pend == str)
+               return "";
+
+       p = str;
+       while (p < pend && strchr(chars, *p) != NULL)
+               p++;
+       begin = p;
+
+       p = pend - 1;
+       while (p > begin && strchr(chars, *p) != NULL)
+               p--;
+
+       if (p <= begin)
+               return "";
+       return t_strdup_until(begin, p+1);
+}
+
+const char *str_ltrim(const char *str, const char *chars)
+{
+       const char *p;
+
+       if (*str == '\0')
+               return "";
+
+       p = str;
+       while (*p != '\0' && strchr(chars, *p) != NULL)
+               p++;
+
+       return p;
+}
+
+const char *t_str_ltrim(const char *str, const char *chars)
+{
+       return t_strdup(str_ltrim(str, chars));
+}
+
+const char *t_str_rtrim(const char *str, const char *chars)
+{
+       const char *p, *pend;
+
+       pend = str + strlen(str);
+       if (pend == str)
+               return "";
+
+       p = pend - 1;
+       while (p > str && strchr(chars, *p) != NULL)
+               p--;
+       if (p <= str)
+               return "";
+       return t_strdup_until(str, p+1);
+}
+
 int null_strcmp(const char *s1, const char *s2)
 {
        if (s1 == NULL)
index 3a98e1370efe778f98f964a22c6e57175628c11d..8826bc227e4598f526cc510b09339c3ebd240089 100644 (file)
@@ -53,6 +53,12 @@ char *str_lcase(char *str);
 const char *t_str_lcase(const char *str);
 const char *t_str_ucase(const char *str);
 
+/* Trim matching chars from either side of the string */
+const char *str_ltrim(const char *str, const char *chars);
+const char *t_str_ltrim(const char *str, const char *chars);
+const char *t_str_rtrim(const char *str, const char *chars);
+const char *t_str_trim(const char *str, const char *chars);
+
 int null_strcmp(const char *s1, const char *s2) ATTR_PURE;
 int bsearch_strcmp(const char *key, const char *const *member) ATTR_PURE;
 int bsearch_strcasecmp(const char *key, const char *const *member) ATTR_PURE;
index 9e80421b75576c9b7fb433aae7e992044e3c2f56..f7095268ce3773340e628f5fe1df7282e21b5756 100644 (file)
@@ -109,10 +109,61 @@ static void test_t_str_replace(void)
        test_end();
 }
 
+static void test_t_str_trim(void)
+{
+       test_begin("t_str_trim");
+       test_assert(strcmp(t_str_trim("foo", ""), "foo") == 0);
+       test_assert(strcmp(t_str_trim("foo", " "), "foo") == 0);
+       test_assert(strcmp(t_str_trim("foo ", " "), "foo") == 0);
+       test_assert(strcmp(t_str_trim(" foo", " "), "foo") == 0);
+       test_assert(strcmp(t_str_trim(" foo ", " "), "foo") == 0);
+       test_assert(strcmp(t_str_trim("\tfoo ", "\t "), "foo") == 0);
+       test_assert(strcmp(t_str_trim(" \tfoo\t ", "\t "), "foo") == 0);
+       test_assert(strcmp(t_str_trim("\r \tfoo\t \r", "\t \r"), "foo") == 0);
+       test_assert(strcmp(t_str_trim("\r \tfoo foo\t \r", "\t \r"), "foo foo") == 0);
+       test_assert(strcmp(t_str_trim("\tfoo\tfoo\t", "\t \r"), "foo\tfoo") == 0);
+       test_end();
+}
+
+static void test_t_str_ltrim(void)
+{
+       test_begin("t_str_ltrim");
+       test_assert(strcmp(t_str_ltrim("foo", ""), "foo") == 0);
+       test_assert(strcmp(t_str_ltrim("foo", " "), "foo") == 0);
+       test_assert(strcmp(t_str_ltrim("foo ", " "), "foo ") == 0);
+       test_assert(strcmp(t_str_ltrim(" foo", " "), "foo") == 0);
+       test_assert(strcmp(t_str_ltrim(" foo ", " "), "foo ") == 0);
+       test_assert(strcmp(t_str_ltrim("\tfoo ", "\t "), "foo ") == 0);
+       test_assert(strcmp(t_str_ltrim(" \tfoo\t ", "\t "), "foo\t ") == 0);
+       test_assert(strcmp(t_str_ltrim("\r \tfoo\t \r", "\t \r"), "foo\t \r") == 0);
+       test_assert(strcmp(t_str_ltrim("\r \tfoo foo\t \r", "\t \r"), "foo foo\t \r") == 0);
+       test_assert(strcmp(t_str_ltrim("\tfoo\tfoo\t", "\t \r"), "foo\tfoo\t") == 0);
+       test_end();
+}
+
+static void test_t_str_rtrim(void)
+{
+       test_begin("t_str_rtrim");
+       test_assert(strcmp(t_str_rtrim("foo", ""), "foo") == 0);
+       test_assert(strcmp(t_str_rtrim("foo", " "), "foo") == 0);
+       test_assert(strcmp(t_str_rtrim("foo ", " "), "foo") == 0);
+       test_assert(strcmp(t_str_rtrim(" foo", " "), " foo") == 0);
+       test_assert(strcmp(t_str_rtrim(" foo ", " "), " foo") == 0);
+       test_assert(strcmp(t_str_rtrim("\tfoo ", "\t "), "\tfoo") == 0);
+       test_assert(strcmp(t_str_rtrim(" \tfoo\t ", "\t "), " \tfoo") == 0);
+       test_assert(strcmp(t_str_rtrim("\r \tfoo\t \r", "\t \r"), "\r \tfoo") == 0);
+       test_assert(strcmp(t_str_rtrim("\r \tfoo foo\t \r", "\t \r"), "\r \tfoo foo") == 0);
+       test_assert(strcmp(t_str_rtrim("\tfoo\tfoo\t", "\t \r"), "\tfoo\tfoo") == 0);
+       test_end();
+}
+
 void test_strfuncs(void)
 {
        test_p_strarray_dup();
        test_t_strsplit();
        test_t_strsplit_tab();
        test_t_str_replace();
+       test_t_str_trim();
+       test_t_str_ltrim();
+       test_t_str_rtrim();
 }