zsh-workers
 help / color / mirror / code / Atom feed
From: Stephane Chazelas <stephane@chazelas.org>
To: Bart Schaefer <schaefer@brasslantern.com>
Cc: Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: [PATCH] (take two?) typeset array[position=index]=value / unset hash[$stuff]
Date: Wed, 2 Jun 2021 11:06:51 +0100	[thread overview]
Message-ID: <20210602100651.qn3rrop5dup46ikc@chazelas.org> (raw)
In-Reply-To: <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


  reply	other threads:[~2021-06-02 10:07 UTC|newest]

Thread overview: 58+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-16 21:10 regexp-replace and ^, word boundary or look-behind operators Stephane Chazelas
2019-12-16 21:27 ` Stephane Chazelas
2019-12-17  7:38   ` Stephane Chazelas
2019-12-17 11:11     ` [PATCH] " Stephane Chazelas
2019-12-18  0:22       ` Daniel Shahaf
2019-12-18  8:31         ` Stephane Chazelas
2020-01-01 14:03         ` [PATCH v2] " Stephane Chazelas
2021-04-30  6:11           ` Stephane Chazelas
2021-04-30 23:13             ` Bart Schaefer
2021-05-05 11:45               ` [PATCH v3] regexp-replace and ^, word boundary or look-behind operators (and more) Stephane Chazelas
2021-05-31  0:58                 ` Lawrence Velázquez
2021-05-31 18:18                 ` Bart Schaefer
2021-05-31 21:37                   ` [PATCH] (?) typeset array[position=index]=value Bart Schaefer
2021-06-01  5:32                     ` Stephane Chazelas
2021-06-01 16:05                       ` Bart Schaefer
2021-06-02  2:51                         ` [PATCH] (take two?) typeset array[position=index]=value / unset hash[$stuff] Bart Schaefer
2021-06-02 10:06                           ` Stephane Chazelas [this message]
2021-06-02 14:52                             ` Bart Schaefer
2021-06-02 16:02                               ` Stephane Chazelas
2021-06-02  9:11                         ` [PATCH] (?) typeset array[position=index]=value Stephane Chazelas
2021-06-02 13:34                           ` Daniel Shahaf
2021-06-02 14:20                             ` Stephane Chazelas
2021-06-02 15:59                               ` Bart Schaefer
2021-06-03  2:04                                 ` [PATCH (not final)] (take three?) unset "array[$anything]" Bart Schaefer
2021-06-03  2:42                                   ` Bart Schaefer
2021-06-03  6:12                                     ` Bart Schaefer
2021-06-03  8:54                                       ` Peter Stephenson
2021-06-03 13:13                                         ` Stephane Chazelas
2021-06-03 14:41                                           ` Peter Stephenson
2021-06-04 19:25                                             ` Bart Schaefer
2021-06-05 18:18                                               ` Peter Stephenson
2021-06-09 23:31                                                 ` Bart Schaefer
2021-06-13 16:51                                                   ` Peter Stephenson
2021-06-13 18:04                                                     ` Bart Schaefer
2021-06-13 19:48                                                       ` Peter Stephenson
2021-06-13 21:44                                                         ` Bart Schaefer
2021-06-14  7:19                                                           ` Stephane Chazelas
2021-06-03 18:12                                           ` Bart Schaefer
2021-06-04  8:02                                             ` Stephane Chazelas
2021-06-04 18:36                                               ` Bart Schaefer
2021-06-04 20:21                                                 ` Stephane Chazelas
2021-06-05  0:20                                                   ` Bart Schaefer
2021-06-05 17:05                                                     ` Stephane Chazelas
2021-06-10  0:14                                                       ` Square brackets in command position Bart Schaefer
2021-06-03  6:05                                   ` [PATCH (not final)] (take three?) unset "array[$anything]" Stephane Chazelas
2021-06-03  6:43                                     ` Bart Schaefer
2021-06-03  7:31                                       ` Stephane Chazelas
2021-06-10  0:21                         ` [PATCH] (?) typeset array[position=index]=value Bart Schaefer
2021-06-05  4:29                     ` Mikael Magnusson
2021-06-05  5:49                       ` Bart Schaefer
2021-06-05 11:06                         ` Mikael Magnusson
2021-06-05 16:22                           ` Bart Schaefer
2021-06-18 10:53                         ` Mikael Magnusson
2024-03-08 15:30                 ` [PATCH v3] regexp-replace and ^, word boundary or look-behind operators (and more) Stephane Chazelas
2024-03-09  8:41                   ` [PATCH v5] " Stephane Chazelas
2024-03-09  9:21                     ` MBEGIN when =~ finds bytes inside characters (Was: [PATCH v5] regexp-replace and ^, word boundary or look-behind operators (and more).) Stephane Chazelas
2024-03-09 13:03                   ` [PATCH v3] regexp-replace and ^, word boundary or look-behind operators (and more) Stephane Chazelas
2024-03-10 19:52                     ` [PATCH v6] " Stephane Chazelas

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=20210602100651.qn3rrop5dup46ikc@chazelas.org \
    --to=stephane@chazelas.org \
    --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).