# HG changeset patch # User Timo Sirainen # Date 1090859958 -10800 # Node ID 1ce02ffc7ec7c82adf3653ec72af81a6ebeabb9f # Parent 203938a7f45ebc88ea7906368069b0bfbbcdfd8c Added support for %offset.width format in variables. diff -r 203938a7f45e -r 1ce02ffc7ec7 doc/variables.txt --- a/doc/variables.txt Mon Jul 26 19:21:29 2004 +0300 +++ b/doc/variables.txt Mon Jul 26 19:39:18 2004 +0300 @@ -22,6 +22,7 @@ %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. diff -r 203938a7f45e -r 1ce02ffc7ec7 src/lib/var-expand.c --- a/src/lib/var-expand.c Mon Jul 26 19:21:29 2004 +0300 +++ b/src/lib/var-expand.c Mon Jul 26 19:39:18 2004 +0300 @@ -23,7 +23,7 @@ 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 @@ str_append_c(dest, *str); else { str++; + + /* [.][] */ 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 @@ } if (var != NULL) { + for (; *var != '\0' && offset > 0; offset--) + var++; if (modifier != NULL) var = modifier(var); if (width == 0)