zsh-users
 help / color / mirror / code / Atom feed
* What's the reasoning behind z & s returning nular for empty input?
@ 2019-11-09  7:26 Sebastian Gniazdowski
  2019-11-09  7:50 ` Roman Perepelitsa
  0 siblings, 1 reply; 9+ messages in thread
From: Sebastian Gniazdowski @ 2019-11-09  7:26 UTC (permalink / raw)
  To: Zsh Users

Hi
arr=( "${(s:,:):-}" )
print -r ${#arr} ${(q)arr}
Output: 1 ''

arr=( "${(z):-}" )
print -r ${#arr} ${(q)arr}
Output: 1 ''

I guess the code is returning the nular (or something similar, as
nular is not defined in subst.c). I wonder why? Is the reason
historical? I always have to add exceptions to the code, especially
for (z). Maybe I'm not seeing some reason for this behavior?

-- 
Sebastian Gniazdowski
News: https://twitter.com/ZdharmaI
IRC: https://kiwiirc.com/client/chat.freenode.net:+6697/#zplugin
Blog: http://zdharma.org

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: What's the reasoning behind z & s returning nular for empty input?
  2019-11-09  7:26 What's the reasoning behind z & s returning nular for empty input? Sebastian Gniazdowski
@ 2019-11-09  7:50 ` Roman Perepelitsa
  2019-11-09  7:52   ` Roman Perepelitsa
  2019-11-09  8:11   ` Sebastian Gniazdowski
  0 siblings, 2 replies; 9+ messages in thread
From: Roman Perepelitsa @ 2019-11-09  7:50 UTC (permalink / raw)
  To: Sebastian Gniazdowski; +Cc: Zsh Users

On Sat, Nov 9, 2019 at 8:27 AM Sebastian Gniazdowski
<sgniazdowski@gmail.com> wrote:
>
> Hi
> arr=( "${(s:,:):-}" )
> print -r ${#arr} ${(q)arr}
> Output: 1 ''

This makes sense. Consider how many elements we should get when
splitting a string on commas:

    split ",,," => 4 elements
    split ",," => 3 elements
    split "," => 2 elements
    split "" => ???

The last split could give either 1 element or 0. The former is
consistent with the rest, the latter is not.

> arr=( "${(z):-}" )
> print -r ${#arr} ${(q)arr}
> Output: 1 ''

This is a bug IMO. Let's again consider how many elements we should
get when tokenizing different strings:

    tokenize "a a a a" => 4 elements
    tokenize "a a a" => 3 elements
    tokenize "a a" => 2 elements
    tokenize "a" => 1 element
    tokenize "" => ???

Consistency requires that the last line gives zero elements. If it
gave one empty element, it would be the only case where (z) can
produce empty elements.

Here's another way to put it. For any array x,
"${(@z)${(j::)${(@q)x}}}" expands to "${(@q)x}", except when x is
empty. The special case is an inconsistency.


Roman.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: What's the reasoning behind z & s returning nular for empty input?
  2019-11-09  7:50 ` Roman Perepelitsa
@ 2019-11-09  7:52   ` Roman Perepelitsa
  2019-11-09  8:11   ` Sebastian Gniazdowski
  1 sibling, 0 replies; 9+ messages in thread
From: Roman Perepelitsa @ 2019-11-09  7:52 UTC (permalink / raw)
  To: Sebastian Gniazdowski; +Cc: Zsh Users

On Sat, Nov 9, 2019 at 8:50 AM Roman Perepelitsa
<roman.perepelitsa@gmail.com> wrote:
> "${(@z)${(j::)${(@q)x}}}" expands to "${(@q)x}"

Correction: (j::) should read (j: :).

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: What's the reasoning behind z & s returning nular for empty input?
  2019-11-09  7:50 ` Roman Perepelitsa
  2019-11-09  7:52   ` Roman Perepelitsa
@ 2019-11-09  8:11   ` Sebastian Gniazdowski
  2019-11-09  8:22     ` Roman Perepelitsa
  1 sibling, 1 reply; 9+ messages in thread
From: Sebastian Gniazdowski @ 2019-11-09  8:11 UTC (permalink / raw)
  To: Roman Perepelitsa; +Cc: Zsh Users

On Sat, 9 Nov 2019 at 08:50, Roman Perepelitsa
<roman.perepelitsa@gmail.com> wrote:
>
> On Sat, Nov 9, 2019 at 8:27 AM Sebastian Gniazdowski
> <sgniazdowski@gmail.com> wrote:
> >
> > Hi
> > arr=( "${(s:,:):-}" )
> > print -r ${#arr} ${(q)arr}
> > Output: 1 ''
>
> This makes sense. Consider how many elements we should get when
> splitting a string on commas:
>
>     split ",,," => 4 elements
>     split ",," => 3 elements
>     split "," => 2 elements
>     split "" => ???
>
> The last split could give either 1 element or 0. The former is
> consistent with the rest, the latter is not.

Thanks for the analysis. The additional problem is that quoted but
@-lacking s-flag should still elide the empty elements, as the manual
states. So this is an intentional exception and I wonder why it has
been added?

-- 
Sebastian Gniazdowski
News: https://twitter.com/ZdharmaI
IRC: https://kiwiirc.com/client/chat.freenode.net:+6697/#zplugin
Blog: http://zdharma.org

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: What's the reasoning behind z & s returning nular for empty input?
  2019-11-09  8:11   ` Sebastian Gniazdowski
@ 2019-11-09  8:22     ` Roman Perepelitsa
  2019-11-09  8:40       ` Sebastian Gniazdowski
  2019-11-09 14:33       ` Peter Stephenson
  0 siblings, 2 replies; 9+ messages in thread
From: Roman Perepelitsa @ 2019-11-09  8:22 UTC (permalink / raw)
  To: Sebastian Gniazdowski; +Cc: Zsh Users

On Sat, Nov 9, 2019 at 9:11 AM Sebastian Gniazdowski
<sgniazdowski@gmail.com> wrote:
> Thanks for the analysis. The additional problem is that quoted but
> @-lacking s-flag should still elide the empty elements, as the manual
> states. So this is an intentional exception and I wonder why it has
> been added?

Here's the passage from zshexpn:

    For historical reasons, the usual behaviour that empty array
    elements are retained inside double quotes is disabled for arrays
    generated by splitting; hence the following:

        line="one::three"
        print -l "${(s.:.)line}"

    produces two lines of output for one and three and elides the
    empty field. To override this behaviour, supply the ‘(@)’ flag as
    well, i.e. "${(@s.:.)line}".

The phrase "for history reasons" is used to describe past design
decisions and/or implementations that would be different if the
benefit of hindsight were available at the time, but changing which is
infeasible due to the existing code relying on the current behavior.
In other words, the current behavior is unfortunate but cannot be
changed.


Roman.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: What's the reasoning behind z & s returning nular for empty input?
  2019-11-09  8:22     ` Roman Perepelitsa
@ 2019-11-09  8:40       ` Sebastian Gniazdowski
  2019-11-09  8:52         ` Roman Perepelitsa
  2019-11-09 14:33       ` Peter Stephenson
  1 sibling, 1 reply; 9+ messages in thread
From: Sebastian Gniazdowski @ 2019-11-09  8:40 UTC (permalink / raw)
  To: Roman Perepelitsa; +Cc: Zsh Users

On Sat, 9 Nov 2019 at 09:22, Roman Perepelitsa
<roman.perepelitsa@gmail.com> wrote:
>
> On Sat, Nov 9, 2019 at 9:11 AM Sebastian Gniazdowski
> <sgniazdowski@gmail.com> wrote:
> > Thanks for the analysis. The additional problem is that quoted but
> > @-lacking s-flag should still elide the empty elements, as the manual
> > states. So this is an intentional exception and I wonder why it has
> > been added?
>
> Here's the passage from zshexpn:

Yes, I was also referring to this fragment, however it states that the
empty elements should be removed, and even – that they should be
removed more eagerly, so I still wonder why produce a nular for an
empty input?

-- 
Sebastian Gniazdowski
News: https://twitter.com/ZdharmaI
IRC: https://kiwiirc.com/client/chat.freenode.net:+6697/#zplugin
Blog: http://zdharma.org

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: What's the reasoning behind z & s returning nular for empty input?
  2019-11-09  8:40       ` Sebastian Gniazdowski
@ 2019-11-09  8:52         ` Roman Perepelitsa
  2019-11-09  9:09           ` Sebastian Gniazdowski
  0 siblings, 1 reply; 9+ messages in thread
From: Roman Perepelitsa @ 2019-11-09  8:52 UTC (permalink / raw)
  To: Sebastian Gniazdowski; +Cc: Zsh Users

On Sat, Nov 9, 2019 at 9:40 AM Sebastian Gniazdowski
<sgniazdowski@gmail.com> wrote:
>
> On Sat, 9 Nov 2019 at 09:22, Roman Perepelitsa
> <roman.perepelitsa@gmail.com> wrote:
> >
> > On Sat, Nov 9, 2019 at 9:11 AM Sebastian Gniazdowski
> > <sgniazdowski@gmail.com> wrote:
> > > Thanks for the analysis. The additional problem is that quoted but
> > > @-lacking s-flag should still elide the empty elements, as the manual
> > > states. So this is an intentional exception and I wonder why it has
> > > been added?
> >
> > Here's the passage from zshexpn:
>
> Yes, I was also referring to this fragment, however it states that the
> empty elements should be removed, and even – that they should be
> removed more eagerly, so I still wonder why produce a nular for an
> empty input?

Oh, I see what you mean. Apparently, only inner empty elements are
dropped by "${(s...)...}" while the first and the last are retained.

    "${(s:,:):-a,,b}" => (a b)
    "${(s:,:):-,,}" => ('' '')

Looks like it's either a bug in the code or in the documentation.

Roman.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: What's the reasoning behind z & s returning nular for empty input?
  2019-11-09  8:52         ` Roman Perepelitsa
@ 2019-11-09  9:09           ` Sebastian Gniazdowski
  0 siblings, 0 replies; 9+ messages in thread
From: Sebastian Gniazdowski @ 2019-11-09  9:09 UTC (permalink / raw)
  To: Roman Perepelitsa; +Cc: Zsh Users

On Sat, 9 Nov 2019 at 09:52, Roman Perepelitsa
<roman.perepelitsa@gmail.com> wrote:
>
> On Sat, Nov 9, 2019 at 9:40 AM Sebastian Gniazdowski
> <sgniazdowski@gmail.com> wrote:
> > Yes, I was also referring to this fragment, however it states that the
> > empty elements should be removed, and even – that they should be
> > removed more eagerly, so I still wonder why produce a nular for an
> > empty input?
>
> Oh, I see what you mean. Apparently, only inner empty elements are
> dropped by "${(s...)...}" while the first and the last are retained.
>
>     "${(s:,:):-a,,b}" => (a b)
>     "${(s:,:):-,,}" => ('' '')
>
> Looks like it's either a bug in the code or in the documentation.

True, I wasn't fully aware of that:

array=( "${(s:,:):-,,}" )
print -r ${(q)array}
Output:'' ''

array=( "${(s:,:):-,}" )
print -r ${(q)array}
Output:'' ''

I think that the documentation or the behavior should be updated.
-- 
Sebastian Gniazdowski
News: https://twitter.com/ZdharmaI
IRC: https://kiwiirc.com/client/chat.freenode.net:+6697/#zplugin
Blog: http://zdharma.org

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: What's the reasoning behind z & s returning nular for empty input?
  2019-11-09  8:22     ` Roman Perepelitsa
  2019-11-09  8:40       ` Sebastian Gniazdowski
@ 2019-11-09 14:33       ` Peter Stephenson
  1 sibling, 0 replies; 9+ messages in thread
From: Peter Stephenson @ 2019-11-09 14:33 UTC (permalink / raw)
  To: zsh-users

On Sat, 2019-11-09 at 09:22 +0100, Roman Perepelitsa wrote:
> On Sat, Nov 9, 2019 at 9:11 AM Sebastian Gniazdowski
> <sgniazdowski@gmail.com> wrote:
> > 
> > Thanks for the analysis. The additional problem is that quoted but
> > @-lacking s-flag should still elide the empty elements, as the manual
> > states. So this is an intentional exception and I wonder why it has
> > been added?
> Here's the passage from zshexpn:
> 
>     For historical reasons, the usual behaviour that empty array
>     elements are retained inside double quotes is disabled for arrays
>     generated by splitting; hence the following:
> 
>         line="one::three"
>         print -l "${(s.:.)line}"
> 
>     produces two lines of output for one and three and elides the
>     empty field. To override this behaviour, supply the ‘(@)’ flag as
>     well, i.e. "${(@s.:.)line}".
> 
> The phrase "for history reasons" is used to describe past design
> decisions and/or implementations that would be different if the
> benefit of hindsight were available at the time, but changing which is
> infeasible due to the existing code relying on the current behavior.
> In other words, the current behavior is unfortunate but cannot be
> changed.

This doesn't really help, but this would certainly be one of the first
"features" out of the window if we were starting from scratch...

pws


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2019-11-10 23:41 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-11-09  7:26 What's the reasoning behind z & s returning nular for empty input? Sebastian Gniazdowski
2019-11-09  7:50 ` Roman Perepelitsa
2019-11-09  7:52   ` Roman Perepelitsa
2019-11-09  8:11   ` Sebastian Gniazdowski
2019-11-09  8:22     ` Roman Perepelitsa
2019-11-09  8:40       ` Sebastian Gniazdowski
2019-11-09  8:52         ` Roman Perepelitsa
2019-11-09  9:09           ` Sebastian Gniazdowski
2019-11-09 14:33       ` Peter Stephenson

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).