Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

PATCH: Fix appending empty array to associations



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



Messages sorted by: Reverse Date, Date, Thread, Author