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

Re: support negative LEN in ${VAR:OFFSET:LEN}



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