]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
Added support for %offset.width format in variables.
authorTimo Sirainen <tss@iki.fi>
Mon, 26 Jul 2004 16:39:18 +0000 (19:39 +0300)
committerTimo Sirainen <tss@iki.fi>
Mon, 26 Jul 2004 16:39:18 +0000 (19:39 +0300)
--HG--
branch : HEAD

doc/variables.txt
src/lib/var-expand.c

index 105e4886f01a8be2cd0bc93d7c200371c1f9e4dc..9af2d633e80373f354764d122a54890248d6eb76 100644 (file)
@@ -22,6 +22,7 @@ You can apply a modifiers for each variable (eg. %Lp = pop3):
  %U - uppercase
  %E - escape '"', "'" and '\' characters by inserting '\' before them.
 
-You can also limit a width of string by giving the number of max. characters
-after the '%' character. For example %1u gives the first character of
-username.
+You can take a substring of the variable by giving optional offset followed
+by '.' and width after the '%' character. For example %2u gives first two
+characters of the username. %2.1u gives third character of the username. If
+offset points outside the value, empty string is returned.
index 996bc313822f13ed2dd59ad1389bcbce89300015..ed4e0e1bff70d585397b8a35f5bb7ed1838b6aba 100644 (file)
@@ -23,7 +23,7 @@ void var_expand(string_t *dest, const char *str,
         const struct var_expand_modifier *m;
         const struct var_expand_table *t;
        const char *var;
-       unsigned int width;
+       unsigned int offset, width;
        const char *(*modifier)(const char *);
 
        for (; *str != '\0'; str++) {
@@ -31,12 +31,26 @@ void var_expand(string_t *dest, const char *str,
                        str_append_c(dest, *str);
                else {
                        str++;
+
+                       /* [<offset>.]<width>[<modifier>]<variable> */
                        width = 0;
                        while (*str >= '0' && *str <= '9') {
                                width = width*10 + (*str - '0');
                                str++;
                        }
 
+                       if (*str != '.')
+                               offset = 0;
+                       else {
+                               offset = width;
+                               width = 0;
+                               str++;
+                               while (*str >= '0' && *str <= '9') {
+                                       width = width*10 + (*str - '0');
+                                       str++;
+                               }
+                       }
+
                        modifier = NULL;
                        for (m = modifiers; m->key != '\0'; m++) {
                                if (m->key == *str) {
@@ -64,6 +78,8 @@ void var_expand(string_t *dest, const char *str,
                        }
 
                        if (var != NULL) {
+                               for (; *var != '\0' && offset > 0; offset--)
+                                       var++;
                                if (modifier != NULL)
                                        var = modifier(var);
                                if (width == 0)