Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

PATCH: more things I screwed up



I've never realised that the delimiters for arguments to parameter
padding flags had to be the same for each pair, although a moment's
thought will show it's obvious since that's the only way you can tell if
the optional arguments are present.

Consequently I got this wrong when I extended the delimiters to allow
multibyte characters.  This means the simple form ${(l.8.)foo} with
no optional arguments is broken.  The next form ${(l.8..X.)foo} is
semi-broken, because I didn't alter the test to be multibyte.  So all in
all this wasn't very competent.

Index: Doc/Zsh/expn.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/expn.yo,v
retrieving revision 1.73
diff -u -r1.73 expn.yo
--- Doc/Zsh/expn.yo	1 Nov 2006 12:25:22 -0000	1.73
+++ Doc/Zsh/expn.yo	7 Nov 2006 22:39:37 -0000
@@ -899,11 +899,15 @@
 )
 item(tt(l:)var(expr)tt(::)var(string1)tt(::)var(string2)tt(:))(
 Pad the resulting words on the left.  Each word will be truncated if
-required and placed in a field var(expr) characters wide.  The space
-to the left will be filled with var(string1) (concatenated as often
-as needed) or spaces if var(string1) is not given.  If both
-var(string1) and var(string2) are given, tt(string2) is inserted
-once directly to the left of each word, truncated if necessary, before
+required and placed in a field var(expr) characters wide.
+
+The arguments tt(:)var(string1)tt(:) and tt(:)var(string2)tt(:) are
+optional; neither, the first, or both may be given.  Note that the same
+pairs of delimiters must be used for each of the three arguments.  The
+space to the left will be filled with var(string1) (concatenated as
+often as needed) or spaces if var(string1) is not given.  If both
+var(string1) and var(string2) are given, tt(string2) is inserted once
+directly to the left of each word, truncated if necessary, before
 var(string1) is used to produce any remaining padding.
 
 If the tt(MULTIBYTE) option is in effect, the flag tt(m) may also
Index: Src/subst.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/subst.c,v
retrieving revision 1.66
diff -u -r1.66 subst.c
--- Src/subst.c	2 Nov 2006 18:43:20 -0000	1.66
+++ Src/subst.c	7 Nov 2006 22:39:39 -0000
@@ -1224,8 +1224,9 @@
 
 /*
  * Get an integer argument; update *s to the end of the
- * final delimiter.  *delmatchp is set to 1 if we have matching
- * delimiters and there was no error in the evaluation, else 0.
+ * final delimiter.  *delmatchp is set to the length of the
+ * matched delimiter if we have matching, delimiters and there was no error in
+ * the evaluation, else 0.
  */
 
 /**/
@@ -1255,7 +1256,7 @@
 	return -1;
     if (ret < 0)
 	ret = -ret;
-    *delmatchp = 1;
+    *delmatchp = arglen;
     return ret < 0 ? -ret : ret;
 }
 
@@ -1602,7 +1603,8 @@
 
 	    for (s++; (c = *s) != ')' && c != Outpar; s++, tt = 0) {
 		int arglen;	/* length of modifier argument */
-		int delmatch;	/* integer delimiters matched OK */
+		int dellen;	/* length of matched delimiter, 0 if not */
+		char *del0;	/* pointer to initial delimiter */
 
 		switch (c) {
 		case ')':
@@ -1634,7 +1636,7 @@
 		    break;
 		case 'I':
 		    s++;
-		    flnum = get_intarg(&s, &delmatch);
+		    flnum = get_intarg(&s, &dellen);
 		    if (flnum < 0)
 			goto flagerr;
 		    s--;
@@ -1734,15 +1736,21 @@
 		/* fall through */
 		case 'r':
 		    s++;
-		    num = get_intarg(&s, &delmatch);
+		    /* delimiter position */
+		    del0 = s;
+		    num = get_intarg(&s, &dellen);
 		    if (num < 0)
 			goto flagerr;
 		    if (tt)
 			prenum = num;
 		    else
 			postnum = num;
-		    if (!delmatch)
+		    /* must have same delimiter if more arguments */
+		    if (!dellen || memcmp(del0, s, dellen)) {
+			/* decrement since loop will increment */
+			s--;
 			break;
+		    }
 		    t = get_strarg(s, &arglen);
 		    if (!*t)
 			goto flagerr;
@@ -1755,7 +1763,9 @@
 		    *t = sav;
 		    sav = *s;
 		    s = t + arglen;
-		    if (UNTOK(*s) != UNTOK(sav)) {
+		    /* again, continue only if another start delimiter */
+		    if (memcmp(del0, s, dellen)) {
+			/* decrement since loop will increment */
 			s--;
 			break;
 		    }
@@ -1769,6 +1779,7 @@
 		    else
 			UNTOK_AND_ESCAPE(postone, s + arglen)
 		    *t = sav;
+		    /* -1 since loop will increment */
 		    s = t + arglen - 1;
 		    break;
 
@@ -3310,7 +3321,7 @@
 {
     char *ptr1, *ptr2, *ptr3, *lptr, c, *test, *sep, *t, *tt, tc, *e;
     char *copy, *all, *tmp, sav, sav1, *ptr1end;
-    int gbal, wall, rec, al, nl, charlen, delmatch;
+    int gbal, wall, rec, al, nl, charlen, dellen;
     convchar_t del;
 
     test = NULL;
@@ -3436,7 +3447,7 @@
 		break;
 	    case 'F':
 		(*ptr)++;
-		rec = get_intarg(ptr, &delmatch);
+		rec = get_intarg(ptr, &dellen);
 		break;
 	    default:
 		*ptr = lptr;
Index: Test/D04parameter.ztst
===================================================================
RCS file: /cvsroot/zsh/zsh/Test/D04parameter.ztst,v
retrieving revision 1.22
diff -u -r1.22 D04parameter.ztst
--- Test/D04parameter.ztst	2 Nov 2006 18:43:20 -0000	1.22
+++ Test/D04parameter.ztst	7 Nov 2006 22:39:45 -0000
@@ -361,6 +361,11 @@
 >val1 val2
 >key1 key2 val1 val2
 
+  word="obfuscatory"
+  print !${(l.16.)word}! +${(r.16.)word}+
+0:simple padding
+>!     obfuscatory! +obfuscatory     +
+
   foo=(resulting words uproariously padded)
   print ${(pl.10..\x22..X.)foo}
 0:${(pl...)...}
Index: Test/D07multibyte.ztst
===================================================================
RCS file: /cvsroot/zsh/zsh/Test/D07multibyte.ztst,v
retrieving revision 1.11
diff -u -r1.11 D07multibyte.ztst
--- Test/D07multibyte.ztst	2 Nov 2006 18:43:20 -0000	1.11
+++ Test/D07multibyte.ztst	7 Nov 2006 22:39:45 -0000
@@ -306,8 +306,8 @@
 # TODO: if we get paired multibyte bracket delimiters to work
 # (as Emacs does, the smug so-and-so), the following should change.
   foo=bar
-  print ${(r£5£¥X¥)foo}
-  print ${(l«10«»Y»£HI£)foo}
+  print ${(r£5££X£)foo}
+  print ${(l«10««Y««HI«)foo}
 0:Delimiters in parameter flags
 >barXX
 >YYYYYHIbar

-- 
Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/



Messages sorted by: Reverse Date, Date, Thread, Author