zsh-workers
 help / color / mirror / code / Atom feed
From: Mikael Magnusson <mikachu@gmail.com>
To: zsh-workers@zsh.org
Subject: PATCH: Fix appending empty array to associations
Date: Mon, 31 Aug 2015 15:19:17 +0200	[thread overview]
Message-ID: <1441027157-30623-1-git-send-email-mikachu@gmail.com> (raw)

I discovered that
% typeset -A foo=(a 1)
% foo+=()
results in foo being emptied, which surprised me.

Daniel Shahaf tracked down the code in question and figured out removing
the original "if (alen)" line appeared to fix it. This patch attempts to
be more similar to the previous code in the case that we are not appending
(which is called augmenting in the code but nowhere in the documentation,
okay). I also took the opportunity to reword the very confusing golfed
statement into something you can follow somewhat easily.

I haven't run into any problems so far, but it might be worth giving
this a second look.

---
 Src/params.c        | 9 ++++++---
 Test/A06assign.ztst | 8 ++++++++
 2 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/Src/params.c b/Src/params.c
index c8b4356..e7c2ce7 100644
--- a/Src/params.c
+++ b/Src/params.c
@@ -3315,9 +3315,12 @@ arrhashsetfn(Param pm, char **val, int augment)
 	zerr("bad set of key/value pairs for associative array");
 	return;
     }
-    if (alen)
-    	if (!(augment && (ht = paramtab = pm->gsu.h->getfn(pm))))
-	    ht = paramtab = newparamtable(17, pm->node.nam);
+    if (augment) {
+	ht = paramtab = pm->gsu.h->getfn(pm);
+    }
+    if (alen && (!augment || !paramtab)) {
+	ht = paramtab = newparamtable(17, pm->node.nam);
+    }
     while (*aptr) {
 	/* The parameter name is ztrdup'd... */
 	v->pm = createparam(*aptr, PM_SCALAR|PM_UNSET);
diff --git a/Test/A06assign.ztst b/Test/A06assign.ztst
index 302659c..1e3d2ed 100644
--- a/Test/A06assign.ztst
+++ b/Test/A06assign.ztst
@@ -249,6 +249,14 @@
 >2
 >3
 
+ typeset -A h
+ h=(a 1 b 2)
+ h+=()
+ print -l $h
+0:add empty array to association
+>1
+>2
+
 # tests of var[range]+=scalar
 
  s=sting
-- 
2.5.0


                 reply	other threads:[~2015-08-31 13:19 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=1441027157-30623-1-git-send-email-mikachu@gmail.com \
    --to=mikachu@gmail.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).