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

Re: What's the reasoning behind z & s returning nular for empty input?



On Sat, Nov 9, 2019 at 8:27 AM Sebastian Gniazdowski
<sgniazdowski@xxxxxxxxx> 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.



Messages sorted by: Reverse Date, Date, Thread, Author