changeset 2368:1ce02ffc7ec7 HEAD

Added support for %offset.width format in variables.
author Timo Sirainen <tss@iki.fi>
date Mon, 26 Jul 2004 19:39:18 +0300
parents 203938a7f45e
children 189a6de6b166
files doc/variables.txt src/lib/var-expand.c
diffstat 2 files changed, 21 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- 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.
--- 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++;
+
+			/* [<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 @@
 			}
 
 			if (var != NULL) {
+				for (; *var != '\0' && offset > 0; offset--)
+					var++;
 				if (modifier != NULL)
 					var = modifier(var);
 				if (width == 0)