rewritten. */
#include "lib.h"
+#include "array.h"
#include "imap-match.h"
#include <ctype.h>
*glob = NULL;
}
+static struct imap_match_glob *
+imap_match_dup_real(pool_t pool, const struct imap_match_glob *glob)
+{
+ ARRAY_TYPE(const_string) patterns;
+ const struct imap_match_pattern *p;
+ bool inboxcase = FALSE;
+
+ t_array_init(&patterns, 8);
+ for (p = glob->patterns; p->pattern != NULL; p++) {
+ if (p->inboxcase)
+ inboxcase = TRUE;
+ array_append(&patterns, &p->pattern, 1);
+ }
+ (void)array_append_space(&patterns);
+ return imap_match_init_multiple_real(pool, array_idx(&patterns, 0),
+ inboxcase, glob->sep);
+};
+
+struct imap_match_glob *
+imap_match_dup(pool_t pool, const struct imap_match_glob *glob)
+{
+ struct imap_match_glob *new_glob;
+
+ if (pool->datastack_pool) {
+ return imap_match_dup_real(pool, glob);
+ } else {
+ T_BEGIN {
+ new_glob = imap_match_dup_real(pool, glob);
+ } T_END;
+ return new_glob;
+ }
+};
+
#define CMP_CUR_CHR(ctx, data, pattern) \
(*(data) == *(pattern) || \
(i_toupper(*(data)) == i_toupper(*(pattern)) && \
struct imap_match_glob *
imap_match_init_multiple(pool_t pool, const char *const *patterns,
bool inboxcase, char separator);
-
void imap_match_deinit(struct imap_match_glob **glob);
+struct imap_match_glob *
+imap_match_dup(pool_t pool, const struct imap_match_glob *glob);
+
enum imap_match_result
imap_match(struct imap_match_glob *glob, const char *data);
{ "%I%N%B%O%X%/foo", "inbox/foo", IMAP_MATCH_YES },
{ "i%X/foo", "inbx/foo", IMAP_MATCH_NO }
};
- struct imap_match_glob *glob;
+ struct imap_match_glob *glob, *glob2;
unsigned int i;
- enum imap_match_result result;
+ pool_t pool;
+
+ pool = pool_alloconly_create_clean("test", 1024);
/* first try tests without inboxcasing */
+ test_begin("imap match");
for (i = 0; i < N_ELEMENTS(test); i++) {
- glob = imap_match_init(default_pool, test[i].pattern,
+ glob = imap_match_init(pool, test[i].pattern,
FALSE, '/');
- result = imap_match(glob, test[i].input);
- imap_match_deinit(&glob);
+ test_assert(imap_match(glob, test[i].input) == test[i].result);
- test_out(t_strdup_printf("imap_match(%d)", i),
- result == test[i].result);
+ glob2 = imap_match_dup(default_pool, glob);
+ p_clear(pool);
+
+ /* test the dup after clearing first one's memory */
+ test_assert(imap_match(glob2, test[i].input) == test[i].result);
+ imap_match_deinit(&glob2);
}
/* inboxcasing tests */
for (i = 0; i < N_ELEMENTS(inbox_test); i++) {
- glob = imap_match_init(default_pool, inbox_test[i].pattern,
+ glob = imap_match_init(pool, inbox_test[i].pattern,
TRUE, '/');
- result = imap_match(glob, inbox_test[i].input);
- imap_match_deinit(&glob);
+ test_assert(imap_match(glob, inbox_test[i].input) == inbox_test[i].result);
+
+ glob2 = imap_match_dup(default_pool, glob);
+ p_clear(pool);
- test_out(t_strdup_printf("imap_match(inboxcase, %d)", i),
- result == inbox_test[i].result);
+ /* test the dup after clearing first one's memory */
+ test_assert(imap_match(glob2, inbox_test[i].input) == inbox_test[i].result);
+ imap_match_deinit(&glob2);
}
+ pool_unref(&pool);
+ test_end();
}
int main(void)