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

PATCH: reverse matching (tentative)



We have this paragraph about the (R) subscript flag in the manual:

   Note  that  this  flag  can give odd results on failure.  For an
   ordinary array the item substituted  is  that  corresponding  to
   subscript 0.  If the option KSH_ARRAYS is not in effect, this is
   the same as the element corresponding to subscript  1,  although
   the  form  ${array[(I)pattern]}  will evaluate to 0 for a failed
   match.  If the option KSH_ARRAYS is in effect, the subscript  is
   still  0 for a failed match; this cannot be distinguished from a
   successful match without testing ${array[0]}  against  the  pat-
   tern.

I can't imagine anyone has ever found this other than a complete
nuisance (as I did, yet again).  Does anyone see any problems if
I fix this so that the empty string is returned?  We can do that
by returning an index off the end of an array if the value required
is an entry rather than the index itself.  This keeps the (useful)
values returned by (i) and (I) as they were before.

We still have the problem with 0 being ambiguous for (I) in the case
of KSH_ARRAYS.  I think we've dug that hole so deep we can't climb out.
The only alternative would be to return a value off the end of the
array, but it's now so ingrained that (I) returns 0 for failure that
that's too painful.  We could do it only if KSH_ARRAYS is set,
but the combination of zsh subscripting and ksh/bash arrays isn't
very common anyway.


Index: README
===================================================================
RCS file: /cvsroot/zsh/zsh/README,v
retrieving revision 1.44
diff -u -r1.44 README
--- README	8 May 2007 10:02:59 -0000	1.44
+++ README	15 May 2007 09:56:44 -0000
@@ -72,6 +72,10 @@
 $search starts with "%" considers the "%" to be part of the search
 string as before.
 
+Parameter subscripts of the form ${array[(R)test]} now return the
+empty string if they fail to match.  The previous longstanding behaviour
+was confusing and useless.
+
 The MULTIBYTE option is on by default where it is available; this
 causes many operations to recognise characters as in the current locale.
 Older versions of the shell always assumed a character was one byte.
Index: Doc/Zsh/params.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/params.yo,v
retrieving revision 1.36
diff -u -r1.36 params.yo
--- Doc/Zsh/params.yo	8 Feb 2007 10:43:30 -0000	1.36
+++ Doc/Zsh/params.yo	15 May 2007 09:56:46 -0000
@@ -234,15 +234,7 @@
 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.
-
-Note that this flag can give odd results on failure.  For an ordinary array
-the item substituted is that corresponding to subscript 0.  If the option
-tt(KSH_ARRAYS) is not in effect, this is the same as the element
-corresponding to subscript 1, although the form tt(${array[(I)pattern]})
-will evaluate to 0 for a failed match.  If the option tt(KSH_ARRAYS) is in
-effect, the subscript is still 0 for a failed match; this cannot be
-distinguished from a successful match without testing tt(${array[0]})
-against the pattern.
+On failure the empty string is returned.
 )
 item(tt(i))(
 Like `tt(r)', but gives the index of the match instead; this may not be
@@ -251,13 +243,16 @@
 is compared to the pattern, and the first matching key found is the
 result.
 
-See `tt(r)' for discussion of subscripts of failed matches.
+On failure, a value one past the end of the array or string is returned.
 )
 item(tt(I))(
 Like `tt(i)', but gives the index of the last match, or all possible
 matching keys in an associative array.
 
-See `tt(R)' for discussion of subscripts of failed matches.
+On failure the value 0 is returned.  If the option tt(KSH_ARRAYS) is in
+effect, the subscript is still 0 for a failed match; this cannot be
+distinguished from a successful match without testing tt(${array[0]})
+against the pattern.
 )
 item(tt(k))(
 If used in a subscript on an associative array, this flag causes the keys
Index: Src/params.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/params.c,v
retrieving revision 1.126
diff -u -r1.126 params.c
--- Src/params.c	14 May 2007 09:25:31 -0000	1.126
+++ Src/params.c	15 May 2007 09:56:47 -0000
@@ -1276,6 +1276,19 @@
 			if (pprog && pattry(pprog, *p) && !--num)
 			    return r;
 		    }
+		    /*
+		     * Failed to match.
+		     * If we're returning an index, return 0 to show
+		     * we've gone off the start.  Unfortunately this
+		     * 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
+		     * element (since 0 is treated the same as 1).
+		     */
+		    if (!ind)
+			r = len + 1;
 		} else
 		    for (r = 1 + beg, p = ta + beg; *p; r++, p++)
 			if (pprog && pattry(pprog, *p) && !--num)
@@ -1495,7 +1508,13 @@
 		    }
 		}
 	    }
-	    return down ? 0 : slen + 1;
+	    /*
+	     * Failed to match.
+	     * If the argument selects an element rather than
+	     * its index, ensure the element is empty.
+	     * See comments on the array case above.
+	     */
+	    return (down && ind) ? 0 : slen + 1;
 	}
     }
     return r;
Index: Test/D06subscript.ztst
===================================================================
RCS file: /cvsroot/zsh/zsh/Test/D06subscript.ztst,v
retrieving revision 1.9
diff -u -r1.9 D06subscript.ztst
--- Test/D06subscript.ztst	6 Sep 2005 15:28:18 -0000	1.9
+++ Test/D06subscript.ztst	15 May 2007 09:56:48 -0000
@@ -29,12 +29,11 @@
 >*, [how] I [wonder] what?  You are!
 >] I [
 
-  # $s[(R)x] actually is $s[0], but zsh treats 0 as 1 for subscripting.
   print $s[(i)x] : $s[(I)x]
   print $s[(r)x] : $s[(R)x]
 0:Scalar pattern subscripts that do not match
 >61 : 0
->: T
+>:
 
   print -R $s[$s[(i)\[]] $s[(i)$s[(r)\*]] $s[(i)${(q)s[(r)\]]}]
 0:Scalar subscripting using a pattern subscript to get the index

-- 
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