Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: support negative LEN in ${VAR:OFFSET:LEN}
- X-seq: zsh-workers 28882
- From: Mikael Magnusson <mikachu@xxxxxxxxx>
- To: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- Subject: Re: support negative LEN in ${VAR:OFFSET:LEN}
- Date: Thu, 10 Mar 2011 16:31:29 +0100
- Cc: zsh workers <zsh-workers@xxxxxxx>
- 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=
- In-reply-to: <110310071902.ZM15166@torch.brasslantern.com>
- List-help: <mailto:zsh-workers-help@zsh.org>
- List-id: Zsh Workers List <zsh-workers.zsh.org>
- List-post: <mailto:zsh-workers@zsh.org>
- Mailing-list: contact zsh-workers-help@xxxxxxx; run by ezmlm
- References: <AANLkTinMNB+5_+ZG1-FgwRcG-2KP88Td8bhOYYE1rRsE@mail.gmail.com> <110310071902.ZM15166@torch.brasslantern.com>
On 10 March 2011 16:19, Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> 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
Messages sorted by:
Reverse Date,
Date,
Thread,
Author