From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 14887 invoked by alias); 10 Mar 2011 15:56:02 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: X-Seq: 28882 Received: (qmail 8568 invoked from network); 10 Mar 2011 15:56:00 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received-SPF: pass (ns1.primenet.com.au: SPF record at _spf.google.com designates 209.85.220.171 as permitted sender) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type; bh=XtD3n5B+P1qTZKulGdTIxOd904DYl7WEwVPZWZ7cC60=; b=QLwZIsrqZdR4vC5UgKEny/wuUv2UEwVitCSUY8A83G+8vYirBhDBVP3u+BihB/tgd5 271Vw6F88WJX2pHY9a/hBm8bG6PQhNccQ2KnZMVPQ8+uI1Xnrm79xsg0b/q38sw97Ufw BEAA8ugb01mQpwjJ+YJT69+S5LoOykUFf8uFg= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; b=PZh/dHFRyOr13aeiLBVCIxlKvTPyIrGWmlLLJC5Wv1OVY2ozuSJ4aBHMDMtkKKka/d 0/tcIxn5s56RW37nD73MZbToJco4ijMPd/2Hg/wdiaa0W03VmUI8DjR5bDL5sMU6KUHq y1sGqlwPlD4e/9tkSDFrd/q6WZ6pBHwKajnL0= MIME-Version: 1.0 In-Reply-To: <110310071902.ZM15166@torch.brasslantern.com> References: <110310071902.ZM15166@torch.brasslantern.com> Date: Thu, 10 Mar 2011 16:31:29 +0100 Message-ID: Subject: Re: support negative LEN in ${VAR:OFFSET:LEN} From: Mikael Magnusson To: Bart Schaefer Cc: zsh workers Content-Type: text/plain; charset=UTF-8 On 10 March 2011 16:19, Bart Schaefer wrote: > On Mar 10, 12:10pm, Mikael Magnusson wrote: > } Subject: support negative LEN in ${VAR:OFFSET:LEN} > } > } +var(length) is treated directly as a length when it is positive. > } +When it is negative, it works as an offset just like var(offset). If > } +this results in a negative length, a diagnostic will be printed and > } +nothing will be substituted. > > I don't object to adding the feature, but that documentation is a bit > confusing. Also, when writing documentation, it's almost always better > to avoid passive phrasing like "x will be y". Perhaps: > > When positive, var(length) counts from the var(offset) position > toward the end of the scalar or array. When negative, var(length) > counts back from the end. If this results in a position smaller > than var(offset), a diagnostic is printed and nothing is substituted. That's fine by me, I didn't spend a lot of time thinking about that phrasing :). > The equivalent $var[start,end] expression would not print a diagnostic. > Does bash really work that way? That is, you have to know the length > of the string in order to safely count backwards from the end of it? > > } + foo="123456789" > } + print ${foo:5:-6} > } +1:Regression test for total length < 0 in string > } +?(eval):2: substring expression < 0: -2 > > Is that what bash's diagnostic looks like? If so we should borrow > consistently, but it'd be a lot clearer if it said > > substring expression: 3 < 5 It's almost the same, $ echo ${PATH: -20:-30} bash: -30: substring expression < 0 % echo ${PATH: -20:-30} zsh: substring expression < 0: -10 Well, it seems bash simply prints the given length, while I print the resulting length. I can change this if you want? Interestingly (well, not really), the feature doesn't work on arrays in bash: $ set a b c $ echo ${*:0:2} bash a $ echo ${*:0:-2} bash: -2: substring expression < 0 % echo ${*:0:-2} zsh a -- Mikael Magnusson