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

Re: Change in FIGNORE behavior



On Wed, 30 May 2007 12:27:24 +0100
Peter Stephenson <pws@xxxxxxx> wrote:
> It would be nice to be able to detect if the index is going to be used
> for something other than extracting a single element and use the first
> index if so, but the current interface doesn't make that easy.

This would do it, but we're now disappearing back down the hole we've
supposedly dug ourselves out of.

Index: Doc/Zsh/params.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/params.yo,v
retrieving revision 1.37
diff -u -r1.37 params.yo
--- Doc/Zsh/params.yo	21 May 2007 09:30:25 -0000	1.37
+++ Doc/Zsh/params.yo	30 May 2007 12:50:00 -0000
@@ -234,7 +234,10 @@
 Like `tt(r)', but gives the last match.  For associative arrays, gives
 all possible matches. May be used for assigning to ordinary array
 elements, but not for assigning to associative arrays.
-On failure the empty string is returned.
+On failure the empty string is returned for a single match; any
+time a valid subscript is needed (for example, on an assignment
+to a failed element, or in a subscript range) the subscript is
+treated as the location of the first element.
 )
 item(tt(i))(
 Like `tt(r)', but gives the index of the match instead; this may not be
Index: Src/params.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/params.c,v
retrieving revision 1.129
diff -u -r1.129 params.c
--- Src/params.c	29 May 2007 14:16:03 -0000	1.129
+++ Src/params.c	30 May 2007 12:50:07 -0000
@@ -976,6 +976,9 @@
  * *w is only set if we need to find the end of a word (input; should
  *  be set to 0 by the caller).
  *
+ * flags is the current set of SCANPM_ flags passed down from
+ * the substitution being done.
+ *
  * The final two arguments are to support multibyte characters.
  * If supplied they are set to the length of the character before
  * the index position and the one at the index position.  If
@@ -991,7 +994,7 @@
 
 /**/
 static zlong
-getarg(char **str, int *inv, Value v, int a2, zlong *w,
+getarg(char **str, int *inv, Value v, int a2, zlong *w, int flags,
        int *prevcharlen, int *nextcharlen)
 {
     int hasbeg = 0, word = 0, rev = 0, ind = 0, down = 0, l, i, ishash;
@@ -1324,11 +1327,16 @@
 		     * is ambiguous with KSH_ARRAYS set, but we're
 		     * stuck with that now.
 		     *
-		     * If the index is to be turned into an element,
-		     * return an index that does not point to a valid
+		     * If
+		     * - the index is to be turned into an element
+		     * - we are not in a range of indices, i.e. neither
+		     *   a comma follows nor SCANPM_ENDRANGE is set,
+		     * - we are not assigning,
+		     * then return an index that does not point to a valid
 		     * element (since 0 is treated the same as 1).
 		     */
-		    if (!ind)
+		    if (!ind && *t != ',' &&
+			!(flags & (SCANPM_ENDRANGE|SCANPM_ASSIGNING)))
 			r = len + 1;
 		} else
 		    for (r = 1 + beg, p = ta + beg; *p; r++, p++)
@@ -1555,7 +1563,9 @@
 	     * its index, ensure the element is empty.
 	     * See comments on the array case above.
 	     */
-	    return (down && ind) ? 0 : slen + 1;
+	    return (down && (ind || *t == '.' ||
+			     (flags & (SCANPM_ENDRANGE|SCANPM_ASSIGNING))))
+		? 0 : slen + 1;
 	}
     }
     return r;
@@ -1563,13 +1573,14 @@
 
 /**/
 int
-getindex(char **pptr, Value v, int dq)
+getindex(char **pptr, Value v, int flags)
 {
     int start, end, inv = 0;
     char *s = *pptr, *tbrack;
 
     *s++ = '[';
-    s = parse_subscript(s, dq);	/* Error handled after untokenizing */
+    /* Error in parsing handled after untokenizing */
+    s = parse_subscript(s, flags & SCANPM_DQUOTED);
     /* Now we untokenize everything except inull() markers so we can check *
      * for the '*' and '@' special subscripts.  The inull()s are removed  *
      * in getarg() after we know whether we're doing reverse indexing.    */
@@ -1598,7 +1609,8 @@
 	zlong we = 0, dummy;
 	int startprevlen, startnextlen;
 
-	start = getarg(&s, &inv, v, 0, &we, &startprevlen, &startnextlen);
+	start = getarg(&s, &inv, v, 0, &we, flags,
+		       &startprevlen, &startnextlen);
 
 	if (inv) {
 	    if (!v->isarr && start != 0) {
@@ -1672,7 +1684,8 @@
 
 	    if ((com = (*s == ','))) {
 		s++;
-		end = getarg(&s, &inv, v, 1, &dummy, NULL, NULL);
+		end = getarg(&s, &inv, v, 1, &dummy,
+			     flags | SCANPM_ENDRANGE, NULL, NULL);
 	    } else {
 		end = we ? we : start;
 	    }
@@ -1790,7 +1803,7 @@
 	v->start = 0;
 	v->end = -1;
 	if (bracks > 0 && (*s == '[' || *s == Inbrack)) {
-	    if (getindex(&s, v, (flags & SCANPM_DQUOTED))) {
+	    if (getindex(&s, v, flags)) {
 		*pptr = s;
 		return v;
 	    }
Index: Src/subst.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/subst.c,v
retrieving revision 1.77
diff -u -r1.77 subst.c
--- Src/subst.c	2 Apr 2007 11:00:43 -0000	1.77
+++ Src/subst.c	30 May 2007 12:50:10 -0000
@@ -2008,7 +2008,7 @@
 	    v->isarr = isarr;
 	    v->pm = pm;
 	    v->end = -1;
-	    if (getindex(&s, v, qt) || s == os)
+	    if (getindex(&s, v, qt ? SCANPM_DQUOTED : 0) || s == os)
 		break;
 	}
 	/*
Index: Src/zsh.h
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/zsh.h,v
retrieving revision 1.115
diff -u -r1.115 zsh.h
--- Src/zsh.h	28 May 2007 22:57:41 -0000	1.115
+++ Src/zsh.h	30 May 2007 12:50:12 -0000
@@ -1422,6 +1422,7 @@
 #define SCANPM_ASSIGNING  (1<<6)
 #define SCANPM_KEYMATCH   (1<<7)
 #define SCANPM_DQUOTED    (1<<8)
+#define SCANPM_ENDRANGE   (1<<9)
 #define SCANPM_ISVAR_AT   ((-1)<<15)	/* Only sign bit is significant */
 
 /*



-- 
Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


To access the latest news from CSR copy this link into a web browser:  http://www.csr.com/email_sig.php

To get further information regarding CSR, please visit our Investor Relations page at http://ir.csr.com/csr/about/overview



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