Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: [PATCH] (take two?) typeset array[position=index]=value / unset hash[$stuff]
- X-seq: zsh-workers 48984
- From: Stephane Chazelas <stephane@xxxxxxxxxxxx>
- To: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- Cc: Zsh hackers list <zsh-workers@xxxxxxx>
- Subject: Re: [PATCH] (take two?) typeset array[position=index]=value / unset hash[$stuff]
- Date: Wed, 2 Jun 2021 11:06:51 +0100
- Archived-at: <https://zsh.org/workers/48984>
- In-reply-to: <CAH+w=7Z4RTLsEujWFyvDHZ+GVwiRcSk+p15qgDEPD0dbQwaxOw@mail.gmail.com>
- List-id: <zsh-workers.zsh.org>
- Mail-followup-to: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>, Zsh hackers list <zsh-workers@xxxxxxx>
- References: <2ea6feb3-a686-4d83-ab27-6a582424487c@www.fastmail.com> <20200101140343.qwfx2xaojumuds3d@chaz.gmail.com> <20210430061117.buyhdhky5crqjrf2@chazelas.org> <CAH+w=7bHxSbFr60ZU0+oZ6+qEejhfBYTzvL7=aXadY5XzWtSzw@mail.gmail.com> <20210505114521.bemoiekpophssbug@chazelas.org> <CAH+w=7ZqE2DnxpHhvCjZnXB4A1vJ=EKB2fpWyUMaZX0VYqU9kg@mail.gmail.com> <CAH+w=7bVn2LiTsq194GjshVSOCSib7t4T=uZQ2ZvUGu6Z2XoPw@mail.gmail.com> <20210601053235.b4junj6muuwegl7b@chazelas.org> <CAH+w=7a+M7nTTqzmve+SZHwtEeRPTicQB5ZRy2XO6er4UhMb1w@mail.gmail.com> <CAH+w=7Z4RTLsEujWFyvDHZ+GVwiRcSk+p15qgDEPD0dbQwaxOw@mail.gmail.com>
2021-06-01 19:51:32 -0700, Bart Schaefer:
[...]
> > The issue with the empty key seems merely to be that the subscript
> > validity test for associative arrays never changed from the one for
> > plain arrays.
>
> To maintain error-equivalent backward compatibility I didn't "fix"
> this, instead, hash[(e)] (or hash[(e)''] if you think that more
> readable) is required in order to unset the element with the empty
> key.
I have to admit I don't see the problem here. I would have
thought allowing a[]=foo and unset 'a[]' would be no-brainers
as there's no concern about backward compatibility as those
currently return an error.
Even for plain arrays, IMO, it would make sense to allow empty
subscripts. In most contexts, an empty arithmetic expression is
interpreted as 0:
$ echo $(())
0
$ printf '%d\n' ''
0
$ set -o ksharrays; a[empty]=1; typeset -p a
typeset -a a=( 1 )
In ksh93:
$ ksh -c '(( a[] = 1 )); typeset -p a'
typeset -a a=(1)
$ ksh -c 'a[]=1'
ksh: syntax error at line 1: `[]' empty subscript
(oddly enough)
$ ksh -c 'a[""]=1; typeset -p a; unset "a[]"; typeset -p a'
typeset -a a=(1)
typeset -a a=()
$ ksh -c 'typeset -A a; a[""]=1; typeset -p a; unset "a[]"; typeset -p a'
typeset -A a=(['']=1)
typeset -A a=()
mksh:
$ mksh -xc 'a[]=1; typeset -p a; unset "a[]"; typeset -p a'
+ a[]=1
+ typeset -p a
set -A a
typeset a[0]=1
+ unset 'a[]'
+ typeset -p a
> The one compatibility issue with the foregoing is this:
[...]
> With the patch, the "(e)" appearing in the value of $bad becomes a
> subscript flag, because $bad is expanded before "unset" parses:
> % zz[$bad]=x
> % typeset -p zz
> typeset -A zz=( ['(e)bang']=x )
> % unset zz\["$bad"\]
> % typeset -p zz
> typeset -A zz=( ['(e)bang']=x )
>
> You have to double the flag:
> % unset zz\["(e)$bad"\]
Or more legibly:
unset "zz[(e)$bad]"
> % typeset -p zz
> typeset -A zz=( )
>
> Is that a small enough incompatibility for this to be acceptable?
[...]
Well, currently, you already need to escape the (s and )s in
general (except when they're matched):
$ key='(' zsh -c 'typeset -A a; a[$key]=x; unset "a[$key]"'
zsh:unset:1: a[(]: invalid parameter name
So I'm not sure there's much of a compatibility problem.
But while it allows unsetting the element with empty key with
unset 'a[(e)]', it seems to make it even more difficult to unset
elements with arbitrary keys.
One still can't use:
unset "a[$key]"
nor
unset "a[(e)$key]"
That still chokes on ()[]`\ and that still can't be worked around with
unset "a[${(b)key}]"
as it inserts backslashes in too many places and not in front of
backticks:
$ key='?' ./Src/zsh -c 'typeset -A a; a[x]=y; a[$key]=x; typeset -p a; unset "a[${(b)key}]"; typeset -p a'
typeset -A a=( ['?']=x [x]=y )
typeset -A a=( ['?']=x [x]=y )
And with (e), we can't use backslash to escape problematic
characters:
$ typeset -A a=('[' x)
$ unset 'a[(e)[]'
unset: a[(e)[]: invalid parameter name
$ unset 'a[(e)\[]'
$ typeset -p a
typeset -A a=( ['[']=x )
So, you'd need something like:
if [[ -n $key ]]; then
() {
set -o localoptions +o multibyte -o extendedglob
unset "a[${key//[][()\`\\]/\\$MATCH}]"
}
else
unset "a[(e)]"
fi
(untested)
To unset an element with arbitrary key (granted, that's an
improvement as you can now unset the element with empty key, but
IMO not an acceptable solution).
"e" for "exact" is also a bit misleading in that case as without it,
wildcards and */@ are not treated specially.
It's also a bit confusing that subscript flags would be
seemingly parsed but later ignored (included in the value of the
key) except for (e). The fact that (e) is recognised and (ee) is
not also makes for a not very consistent API.
--
Stephane
Messages sorted by:
Reverse Date,
Date,
Thread,
Author