zsh-workers
 help / color / mirror / code / Atom feed
* PATCH: Fix appending empty array to associations
@ 2015-08-31 13:19 Mikael Magnusson
  0 siblings, 0 replies; only message in thread
From: Mikael Magnusson @ 2015-08-31 13:19 UTC (permalink / raw)
  To: zsh-workers

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


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2015-08-31 13:19 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-08-31 13:19 PATCH: Fix appending empty array to associations Mikael Magnusson

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