zsh-workers
 help / color / mirror / code / Atom feed
From: Mikael Magnusson <mikachu@gmail.com>
To: Bart Schaefer <schaefer@brasslantern.com>
Cc: zsh workers <zsh-workers@zsh.org>
Subject: Re: support negative LEN in ${VAR:OFFSET:LEN}
Date: Thu, 10 Mar 2011 16:31:29 +0100	[thread overview]
Message-ID: <AANLkTimq_V7N_awnwv2R5H8Cy2tZRjwdQAafo4ChgdN=@mail.gmail.com> (raw)
In-Reply-To: <110310071902.ZM15166@torch.brasslantern.com>

On 10 March 2011 16:19, Bart Schaefer <schaefer@brasslantern.com> 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


  reply	other threads:[~2011-03-10 15:56 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-03-10 11:10 Mikael Magnusson
2011-03-10 15:19 ` Bart Schaefer
2011-03-10 15:31   ` Mikael Magnusson [this message]
2011-03-16 15:04     ` Mikael Magnusson
2011-03-16 16:21       ` Bart Schaefer
2011-05-11 15:39         ` PATCH: " Mikael Magnusson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='AANLkTimq_V7N_awnwv2R5H8Cy2tZRjwdQAafo4ChgdN=@mail.gmail.com' \
    --to=mikachu@gmail.com \
    --cc=schaefer@brasslantern.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).