changeset 20550:ba37ec73c6aa

lib: var_get_key() didn't handle %{long_variables} correctly This also fixes var_has_key()'s long_key handling.
author Timo Sirainen <timo.sirainen@dovecot.fi>
date Tue, 02 Aug 2016 13:38:25 +0300
parents c83ca52a7f65
children 64f547f5bd86
files src/lib/test-var-expand.c src/lib/var-expand.c src/lib/var-expand.h
diffstat 3 files changed, 42 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib/test-var-expand.c	Tue Aug 02 01:04:57 2016 +0300
+++ b/src/lib/test-var-expand.c	Tue Aug 02 13:38:25 2016 +0300
@@ -151,10 +151,31 @@
 	test_end();
 }
 
+static void test_var_get_key(void)
+{
+	static struct {
+		const char *str;
+		char key;
+	} tests[] = {
+		{ "x", 'x' },
+		{ "2.5Mx", 'x' },
+		{ "200MDx", 'x' },
+		{ "200MD{foo}", '{' },
+		{ "{foo}", '{' },
+		{ "", '\0' },
+	};
+
+	test_begin("var_get_key");
+	for (unsigned int i = 0; i < N_ELEMENTS(tests); i++)
+		test_assert_idx(var_get_key(tests[i].str) == tests[i].key, i);
+	test_end();
+}
+
 void test_var_expand(void)
 {
 	test_var_expand_ranges();
 	test_var_expand_builtin();
 	test_var_get_key_range();
 	test_var_expand_with_funcs();
+	test_var_get_key();
 }
--- a/src/lib/var-expand.c	Tue Aug 02 01:04:57 2016 +0300
+++ b/src/lib/var-expand.c	Tue Aug 02 13:38:25 2016 +0300
@@ -388,15 +388,8 @@
 	var_expand_with_funcs(dest, str, table, NULL, NULL);
 }
 
-char var_get_key(const char *str)
-{
-	unsigned int idx, size;
-
-	var_get_key_range(str, &idx, &size);
-	return str[idx];
-}
-
-void var_get_key_range(const char *str, unsigned int *idx_r,
+static bool
+var_get_key_range_full(const char *str, unsigned int *idx_r,
 		       unsigned int *size_r)
 {
 	const struct var_expand_modifier *m;
@@ -425,6 +418,7 @@
 		/* short key */
 		*idx_r = i;
 		*size_r = str[i] == '\0' ? 0 : 1;
+		return FALSE;
 	} else {
 		/* long key */
 		*idx_r = ++i;
@@ -433,9 +427,25 @@
 				break;
 		}
 		*size_r = i - *idx_r;
+		return TRUE;
 	}
 }
 
+char var_get_key(const char *str)
+{
+	unsigned int idx, size;
+
+	if (var_get_key_range_full(str, &idx, &size))
+		return '{';
+	return str[idx];
+}
+
+void var_get_key_range(const char *str, unsigned int *idx_r,
+		       unsigned int *size_r)
+{
+	(void)var_get_key_range_full(str, idx_r, size_r);
+}
+
 static bool var_has_long_key(const char **str, const char *long_key)
 {
 	const char *start, *end;
--- a/src/lib/var-expand.h	Tue Aug 02 01:04:57 2016 +0300
+++ b/src/lib/var-expand.h	Tue Aug 02 13:38:25 2016 +0300
@@ -25,7 +25,8 @@
 			   void *func_context) ATTR_NULL(3, 4, 5);
 
 /* Returns the actual key character for given string, ie. skip any modifiers
-   that are before it. The string should be the data after the '%' character. */
+   that are before it. The string should be the data after the '%' character.
+   For %{long_variable}, '{' is returned. */
 char var_get_key(const char *str) ATTR_PURE;
 /* Similar to var_get_key(), but works for long keys as well. For single char
    keys size=1, while for e.g. %{key} size=3 and idx points to 'k'. */