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

Re: PATCH Re: let unset array element remove compatible with bash



On Jul 2, 10:11am, Peter Stephenson wrote:
}
} Hmm... I'd have said putting in an empty element in was the least
} surprising thing to do, at least in the short term.  I don't think
} shifting an array so that the numbering is different mimics the ability
} to have only certain elements set to any great degree, there are too
} many other cases.

OK, here's one more patch for this, which replaces both 30552 and 30557.

Note that unsetting a slice of a scalar still deletes the slice, but it
might in fact be possible to poke a Nularg in there instead and really
have a 4-character string with a length of 5.  On the other hand, it
does not do sensible things to put a string-consisting-of-Nularg into
an unset array element, because this causes the empty element to be
retained when expanding ${array[*]} whereas the desired effect is to
have an unset element disappear.

I waffled a long time on the "invalid element" error, which in this
incarnation only happens if you try to unset a slice of a numeric type.
If it'd be better that the error become a silent status of 1 (as I had
it in 30557), just put a "-" on the beginning of that line in place of
the space there, and the patch will still apply.


Index: Src/builtin.c
--- ../zsh-forge/current/Src/builtin.c	2012-06-30 10:33:44.000000000 -0700
+++ Src/builtin.c	2012-07-07 09:35:19.000000000 -0700
@@ -3055,8 +3055,35 @@
 		    *sse = ']';
 		}
 		paramtab = tht;
+	    } else if (PM_TYPE(pm->node.flags) == PM_SCALAR ||
+		       PM_TYPE(pm->node.flags) == PM_ARRAY) {
+		struct value vbuf;
+		vbuf.isarr = (PM_TYPE(pm->node.flags) == PM_ARRAY ?
+			      SCANPM_ARRONLY : 0);
+		vbuf.pm = pm;
+		vbuf.flags = 0;
+		vbuf.start = 0;
+		vbuf.end = -1;
+		vbuf.arr = 0;
+		*ss = '[';
+		if (getindex(&ss, &vbuf, SCANPM_ASSIGNING) == 0 &&
+		    vbuf.pm && !(vbuf.pm->node.flags & PM_UNSET)) {
+		    if (PM_TYPE(pm->node.flags) == PM_SCALAR) {
+			setstrvalue(&vbuf, ztrdup(""));
+		    } else {
+			/* start is after the element for reverse index */
+			int start = vbuf.start - !!(vbuf.flags & VALFLAG_INV);
+			if (start < arrlen(vbuf.pm->u.arr)) {
+			    char *arr[2];
+			    arr[0] = "";
+			    arr[1] = 0;
+			    setarrvalue(&vbuf, zarrdup(arr));
+			}
+		    }
+		}
+		returnval = errflag;
+		errflag = 0;
 	    } else {
 		zerrnam(name, "%s: invalid element for unset", s);
 		returnval = 1;
 	    }
 	} else {



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