From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 14625 invoked from network); 15 May 2007 10:01:53 -0000 X-Spam-Checker-Version: SpamAssassin 3.2.0 (2007-05-01) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.5 required=5.0 tests=AWL,BAYES_00 autolearn=no version=3.2.0 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by ns1.primenet.com.au with SMTP; 15 May 2007 10:01:53 -0000 Received-SPF: none (ns1.primenet.com.au: domain at sunsite.dk does not designate permitted sender hosts) Received: (qmail 38414 invoked from network); 15 May 2007 10:01:47 -0000 Received: from sunsite.dk (130.225.247.90) by a.mx.sunsite.dk with SMTP; 15 May 2007 10:01:47 -0000 Received: (qmail 10663 invoked by alias); 15 May 2007 10:01:44 -0000 Mailing-List: contact zsh-workers-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 23440 Received: (qmail 10654 invoked from network); 15 May 2007 10:01:43 -0000 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by sunsite.dk with SMTP; 15 May 2007 10:01:43 -0000 Received: (qmail 38136 invoked from network); 15 May 2007 10:01:43 -0000 Received: from cluster-c.mailcontrol.com (168.143.177.190) by a.mx.sunsite.dk with SMTP; 15 May 2007 10:01:40 -0000 Received: from cameurexb01.EUROPE.ROOT.PRI ([62.189.241.200]) by rly18c.srv.mailcontrol.com (MailControl) with ESMTP id l4F9x3vr013541 for ; Tue, 15 May 2007 11:01:23 +0100 Received: from news01.csr.com ([10.103.143.38]) by cameurexb01.EUROPE.ROOT.PRI with Microsoft SMTPSVC(6.0.3790.1830); Tue, 15 May 2007 10:59:56 +0100 Received: from news01.csr.com (localhost.localdomain [127.0.0.1]) by news01.csr.com (8.13.8/8.13.4) with ESMTP id l4F9xucN010441 for ; Tue, 15 May 2007 10:59:56 +0100 Received: from csr.com (pws@localhost) by news01.csr.com (8.13.8/8.13.8/Submit) with ESMTP id l4F9xqv9010438 for ; Tue, 15 May 2007 10:59:56 +0100 Message-Id: <200705150959.l4F9xqv9010438@news01.csr.com> X-Authentication-Warning: news01.csr.com: pws owned process doing -bs To: zsh-workers@sunsite.dk (Zsh hackers list) Subject: PATCH: reverse matching (tentative) Date: Tue, 15 May 2007 10:59:52 +0100 From: Peter Stephenson X-OriginalArrivalTime: 15 May 2007 09:59:56.0362 (UTC) FILETIME=[CA8B72A0:01C796D7] Content-Type: text/plain MIME-Version: 1.0 X-Scanned-By: MailControl A-07-07-00 (www.mailcontrol.com) on 10.67.0.128 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 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