[dovecot-cvs] dovecot/src/lib var-expand.c,1.13,1.14

tss at dovecot.org tss at dovecot.org
Sun Nov 12 12:06:38 UTC 2006


Update of /var/lib/cvs/dovecot/src/lib
In directory talvi:/tmp/cvs-serv13761/src/lib

Modified Files:
	var-expand.c 
Log Message:
Fixed zero padding handling and documented it. %0.1n shouldn't enable it,
and it really shouldn't stay for the next %variable. -sign also shouldn't
stay for the next variable.



Index: var-expand.c
===================================================================
RCS file: /var/lib/cvs/dovecot/src/lib/var-expand.c,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -d -r1.13 -r1.14
--- var-expand.c	15 Oct 2006 21:03:41 -0000	1.13
+++ var-expand.c	12 Nov 2006 12:06:36 -0000	1.14
@@ -13,6 +13,7 @@
 struct var_expand_context {
 	int offset;
 	unsigned int width;
+	bool zero_padding;
 };
 
 struct var_expand_modifier {
@@ -126,24 +127,24 @@
 	const char *(*modifier[MAX_MODIFIER_COUNT])
 		(const char *, struct var_expand_context *);
 	unsigned int i, modifier_count;
-	int sign = 1;
-	bool zero_padding = FALSE;
 
 	memset(&ctx, 0, sizeof(ctx));
 	for (; *str != '\0'; str++) {
 		if (*str != '%')
 			str_append_c(dest, *str);
 		else {
+			int sign = 1;
+
 			str++;
+			memset(&ctx, 0, sizeof(ctx));
 
 			/* [<offset>.]<width>[<modifiers>]<variable> */
-			ctx.width = 0;
 			if (*str == '-') {
 				sign = -1;
 				str++;
 			}
 			if (*str == '0') {
-				zero_padding = TRUE;
+				ctx.zero_padding = TRUE;
 				str++;
 			}
 			while (*str >= '0' && *str <= '9') {
@@ -151,12 +152,20 @@
 				str++;
 			}
 
-			if (*str != '.')
-				ctx.offset = 0;
-			else {
+			if (*str == '.') {
 				ctx.offset = sign * (int)ctx.width;
 				ctx.width = 0;
 				str++;
+
+				/* if offset was prefixed with zero (or it was
+				   plain zero), just ignore that. zero padding
+				   is done with the width. */
+				ctx.zero_padding = FALSE;
+				if (*str == '0') {
+					ctx.zero_padding = TRUE;
+					str++;
+				}
+
 				while (*str >= '0' && *str <= '9') {
 					ctx.width = ctx.width*10 + (*str - '0');
 					str++;
@@ -216,10 +225,10 @@
 				}
 				if (ctx.width == 0)
 					str_append(dest, var);
-				else if (!zero_padding)
+				else if (!ctx.zero_padding)
 					str_append_n(dest, var, ctx.width);
 				else {
-					/* %05d -like padding */
+					/* %05d -like padding. no truncation. */
 					size_t len = strlen(var);
 					while (len < ctx.width) {
 						str_append_c(dest, '0');



More information about the dovecot-cvs mailing list