zsh-users
 help / color / mirror / code / Atom feed
* priority problem of ":|" and ":*"
@ 2012-12-24 10:52 Han Pingtian
  2012-12-25 23:11 ` Han Pingtian
  0 siblings, 1 reply; 7+ messages in thread
From: Han Pingtian @ 2012-12-24 10:52 UTC (permalink / raw)
  To: zsh-user

hi,

I believe the new ":|" and ":*" should be in Rule 7 Modifiers of
paramter expansion, so this looks like a problem in priority:

    % a1=(a b c);a2=('a b c');print "${a1:|a2}"
    
    % a1=(a b c);a2=('a b c');print "${#a1:|a2}"
    3

in this, the priority should be "5 Double-quoted joining" first, then "7
modifiers", then "9 length". So 'print "${#a1:|a2}"' should output 0 I
think. And the ":*" has the same problem. 



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

* Re: priority problem of ":|" and ":*"
  2012-12-24 10:52 priority problem of ":|" and ":*" Han Pingtian
@ 2012-12-25 23:11 ` Han Pingtian
  2012-12-26  9:10   ` Bart Schaefer
  0 siblings, 1 reply; 7+ messages in thread
From: Han Pingtian @ 2012-12-25 23:11 UTC (permalink / raw)
  To: zsh-users

On Mon, Dec 24, 2012 at 06:52:41PM +0800, Han Pingtian wrote:
> hi,
> 
> I believe the new ":|" and ":*" should be in Rule 7 Modifiers of
> paramter expansion, so this looks like a problem in priority:
> 
>     % a1=(a b c);a2=('a b c');print "${a1:|a2}"
>     
>     % a1=(a b c);a2=('a b c');print "${#a1:|a2}"
>     3
> 
> in this, the priority should be "5 Double-quoted joining" first, then "7
> modifiers", then "9 length". So 'print "${#a1:|a2}"' should output 0 I
> think. And the ":*" has the same problem. 
> 
For the ":*", we have this result:

    % a1=(a b c);a2=(a b c);print ${a1:*a2}
    a b c
    % a1=(a b c);a2=(a b c);print "${a1:*a2}"

    % a1=(a b c);a2=(a b c);print "${#a1:*a2}"
    3
    %


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

* Re: priority problem of ":|" and ":*"
  2012-12-25 23:11 ` Han Pingtian
@ 2012-12-26  9:10   ` Bart Schaefer
  2012-12-26 10:09     ` Han Pingtian
  2012-12-26 16:44     ` Ray Andrews
  0 siblings, 2 replies; 7+ messages in thread
From: Bart Schaefer @ 2012-12-26  9:10 UTC (permalink / raw)
  To: zsh-users

On Dec 26,  7:11am, Han Pingtian wrote:
}
} >     % a1=(a b c);a2=('a b c');print "${#a1:|a2}"
} >     3
} > 
} > in this, the priority should be "5 Double-quoted joining" first, then "7
} > modifiers", then "9 length". So 'print "${#a1:|a2}"' should output 0 I
} > think. And the ":*" has the same problem. 

Hmm.

% a1=(a b c); print "${#a1}" :"${a1}":
3 :a b c:

Plainly if length were applied after double-quoted joining, the above
should print 5 rather than 3.  As I'm pretty sure this hasn't changed
*ever*, it must be that the documentation is wrong, not that :| has the
wrong priority.

And in fact poring through the code it appears that rule 5 double-quoted
joining is explicitly SKIPPED when the length is requested:

    if (isarr) {
        if (nojoin)
            isarr = -1;
        if (qt && !getlen && isarr > 0) {
            val = sepjoin(aval, sep, 1);
            isarr = 0;
        }
    }

"qt" there means double-quoted, but "getlen" means the "#" was seen.  So
when evaluating length, we do not remove arrayness.

} For the ":*", we have this result:
} 
}     % a1=(a b c);a2=(a b c);print "${#a1:*a2}"
}     3

This is consistent with the above.

I admit that this is not intuitive when applying operations to the members
of the array, but it's the way #, ##, %, %%, and :# have always worked, so
it's a bit late to change it and arguably worse to make it different for
:| and :*.  (My sympathies if anyone's mail reader is too clever by half
and renders a lot of that as emoticons.)


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

* Re: priority problem of ":|" and ":*"
  2012-12-26  9:10   ` Bart Schaefer
@ 2012-12-26 10:09     ` Han Pingtian
  2012-12-27  6:24       ` Bart Schaefer
  2012-12-26 16:44     ` Ray Andrews
  1 sibling, 1 reply; 7+ messages in thread
From: Han Pingtian @ 2012-12-26 10:09 UTC (permalink / raw)
  To: zsh-users

On Wed, Dec 26, 2012 at 01:10:19AM -0800, Bart Schaefer wrote:
> Hmm.
> 
> % a1=(a b c); print "${#a1}" :"${a1}":
> 3 :a b c:
> 
> Plainly if length were applied after double-quoted joining, the above
> should print 5 rather than 3.  As I'm pretty sure this hasn't changed
> *ever*, it must be that the documentation is wrong, not that :| has the
> wrong priority.
> 
> And in fact poring through the code it appears that rule 5 double-quoted
> joining is explicitly SKIPPED when the length is requested:
> 
>     if (isarr) {
>         if (nojoin)
>             isarr = -1;
>         if (qt && !getlen && isarr > 0) {
>             val = sepjoin(aval, sep, 1);
>             isarr = 0;
>         }
>     }
> 
> "qt" there means double-quoted, but "getlen" means the "#" was seen.  So
> when evaluating length, we do not remove arrayness.
> 
But I think ":|" doesn't skip the double-quoted, so the "#" would get the
length of the result of ":|", because "#"'s priority is lower than
":|"'s. Or when running in double-quoted, "#" will has a higher priority
?

    % a1=(a b c);a2=('a b c');print ${#a1:|a2}
    3
    % a1=(a b c);a2=('a b c');print "${a1:|a2}"

    % a1=(a b c);a2=('a b c');print "${#a1:|a2}"
    3

Looks like #'s priority will be higher than :| in the double-quoted. 



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

* Re: priority problem of ":|" and ":*"
  2012-12-26  9:10   ` Bart Schaefer
  2012-12-26 10:09     ` Han Pingtian
@ 2012-12-26 16:44     ` Ray Andrews
  1 sibling, 0 replies; 7+ messages in thread
From: Ray Andrews @ 2012-12-26 16:44 UTC (permalink / raw)
  To: zsh-users

On 26/12/12 01:10 AM, Bart Schaefer wrote:
> } For the ":*", we have this result:
> }
> }     % a1=(a b c);a2=(a b c);print "${#a1:*a2}"
> }     3
>
> This is consistent with the above.
>
> I admit that this is not intuitive when applying operations to the members
> of the array, but it's the way #, ##, %, %%, and :# have always worked, so
> it's a bit late to change it and arguably worse to make it different for
> :| and :*.  (My sympathies if anyone's mail reader is too clever by half
> and renders a lot of that as emoticons.)
>
I think it's astonishing that all this syntax hasn't imploded under it's 
own weight ;-)


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

* Re: priority problem of ":|" and ":*"
  2012-12-26 10:09     ` Han Pingtian
@ 2012-12-27  6:24       ` Bart Schaefer
  2012-12-27  9:06         ` Han Pingtian
  0 siblings, 1 reply; 7+ messages in thread
From: Bart Schaefer @ 2012-12-27  6:24 UTC (permalink / raw)
  To: zsh-users

On Dec 26,  6:09pm, Han Pingtian wrote:
} Subject: Re: priority problem of ":|" and ":*"
}
} On Wed, Dec 26, 2012 at 01:10:19AM -0800, Bart Schaefer wrote:
} > And in fact poring through the code it appears that rule 5 double-quoted
} > joining is explicitly SKIPPED when the length is requested:
}
} But I think ":|" doesn't skip the double-quoted, so the "#" would get the
} length of the result of ":|", because "#"'s priority is lower than
} ":|"'s. Or when running in double-quoted, "#" will has a higher priority
} ?

Unfortunately you can't think of the parameter flags and modifiers like
arithmetic operators.  They don't have well-defined precedences.  So it
is almost meaningless to speak of # or :| having relative "priority."
The numbering of the "rules" for parameter expansion in the manual
describes the order in which operations are applied, and sometimes the
rules do not cover all possible combinations.

Those "rules" were derived after the fact by code examination, not made
up ahead of time as a specification for how the code is suppose to work.
In situations where the code and the manual disagree, code is definitive
unless we are able to determine that (a) the description in the manual
is more sensible and (b) nothing is going to break if we revise the code
to match the documentation.  I don't believe we've established (b) here.

In this case rule 5 should have mentioned an exception for the # length
expression as it does for the (@) array flag, and the description of SPEC
in the ${#SPEC} documentation might have been qualified to explain that
surrounding double-quotes are intentionally prevented from altering the
semantics of the length calculation.

To get what you expected, you must use a nested substitution.


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

* Re: priority problem of ":|" and ":*"
  2012-12-27  6:24       ` Bart Schaefer
@ 2012-12-27  9:06         ` Han Pingtian
  0 siblings, 0 replies; 7+ messages in thread
From: Han Pingtian @ 2012-12-27  9:06 UTC (permalink / raw)
  To: zsh-users

On Wed, Dec 26, 2012 at 10:24:58PM -0800, Bart Schaefer wrote:
> In this case rule 5 should have mentioned an exception for the # length
> expression as it does for the (@) array flag, and the description of SPEC
> in the ${#SPEC} documentation might have been qualified to explain that
> surrounding double-quotes are intentionally prevented from altering the
> semantics of the length calculation.
> 
> To get what you expected, you must use a nested substitution.
Thanks.


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

end of thread, other threads:[~2012-12-27  9:07 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-12-24 10:52 priority problem of ":|" and ":*" Han Pingtian
2012-12-25 23:11 ` Han Pingtian
2012-12-26  9:10   ` Bart Schaefer
2012-12-26 10:09     ` Han Pingtian
2012-12-27  6:24       ` Bart Schaefer
2012-12-27  9:06         ` Han Pingtian
2012-12-26 16:44     ` Ray Andrews

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