zsh-workers
 help / color / mirror / code / Atom feed
* Possible bug with ${(A)foo=} and ${(AA)foo=}
@ 1999-06-22 16:13 Andrej Borsenkow
  1999-06-22 17:36 ` Bart Schaefer
  0 siblings, 1 reply; 4+ messages in thread
From: Andrej Borsenkow @ 1999-06-22 16:13 UTC (permalink / raw)
  To: ZSH workers mailing list

While playing with something else I discovered, that it is impossible to create
empty array with this form and it is impossible to create hash in this way
without error message:

bor@itsrm2:~%> unset foo
bor@itsrm2:~%> : ${(A)foo=}
bor@itsrm2:~%> print ${#foo}
1

bor@itsrm2:~%> unset foo
bor@itsrm2:~%> : ${(A)foo=""}
bor@itsrm2:~%> print ${#foo}
1

bor@itsrm2:~%> unset foo
bor@itsrm2:~%> : ${(AA)foo=""}
zsh: bad set of key/value pairs for associative array

bor@itsrm2:~%> print ${#foo}
0

bor@itsrm2:~%> : ${(AA)foo=}
zsh: bad set of key/value pairs for associative array

bor@itsrm2:~%> print ${#foo}
0

I think, that if ${foo=} is valid at all, then we must distinguish between no
value and empty value. It is the same as in command line (and, after all, we
compute it using the same rules?) - empty value is lost but explict NULL like ""
is retained. In other words,

${(A)foo=} - creates empty array
${(A)foo=""} - creates array with one element of length 0
${(AA)foo=} - creates empty hash
${(AA)foo=""} - is invalid assignment as expected

[Background - ${(AA)foo=} seems currently to be the only way to create global
hash from within Zsh function]

/andrej


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Possible bug with ${(A)foo=} and ${(AA)foo=}
  1999-06-22 16:13 Possible bug with ${(A)foo=} and ${(AA)foo=} Andrej Borsenkow
@ 1999-06-22 17:36 ` Bart Schaefer
  1999-06-23 10:02   ` Peter Stephenson
  0 siblings, 1 reply; 4+ messages in thread
From: Bart Schaefer @ 1999-06-22 17:36 UTC (permalink / raw)
  To: Andrej Borsenkow, ZSH workers mailing list

On Jun 22,  8:13pm, Andrej Borsenkow wrote:
} Subject: Possible bug with ${(A)foo=} and ${(AA)foo=}
}
} While playing with something else I discovered, that it is impossible to
} create empty array with this form and it is impossible to create hash in
} this way without error message:

The first problem is what causes the second, though creating an empty AA
this way would create an empty hash table (normally the hash table is not
created until the AA has at least one value).

} I think, that if ${foo=} is valid at all, then we must distinguish between
} no value and empty value.

I've been looking at this ... we can distinguish between ${x=} and ${x=''},
but without jumping through a lot of hoops we can't distinguish whether $y
is set or unset in ${x=$y}.  Perhaps that doesn't matter ...

Anyway, I've just run out of time to work on this, so a patch will have to
wait unless someone else gets to it.

} It is the same as in command line (and, after all, we
} compute it using the same rules?)

I'm not sure what you mean by that, but we don't parse it with the same
code.

} [Background - ${(AA)foo=} seems currently to be the only way to create
} global hash from within Zsh function]

Hmm, yes, that does seem to be an issue.

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Possible bug with ${(A)foo=} and ${(AA)foo=}
  1999-06-22 17:36 ` Bart Schaefer
@ 1999-06-23 10:02   ` Peter Stephenson
  1999-06-24 15:36     ` PATCH: " Bart Schaefer
  0 siblings, 1 reply; 4+ messages in thread
From: Peter Stephenson @ 1999-06-23 10:02 UTC (permalink / raw)
  To: ZSH workers mailing list

"Bart Schaefer" wrote:
> On Jun 22,  8:13pm, Andrej Borsenkow wrote:
> } Subject: Possible bug with ${(A)foo=} and ${(AA)foo=}
> }
> } While playing with something else I discovered, that it is impossible to
> } create empty array with this form and it is impossible to create hash in
> } this way without error message:
> 
> The first problem is what causes the second, though creating an empty AA
> this way would create an empty hash table (normally the hash table is not
> created until the AA has at least one value).

Without fixing the general array-splitting problem, which is difficult, I
don't see there's any problem deciding what to do in the case of an
associative array, since the intention in such a case can only be to create
an empty one because there is no key/value pair, even with ${(AA)foo=''}
since wordsplitting can't change that fact.  So until somebody gets around
to doing array splitting in this case properly, I suggest we just assume it
should create an empty associative array.  The other problems are far less
pressing because if the associative array already exists there are always
other ways of setting it, which in practice aret the ones we always use.

I was thinking about `typeset -g foo' to stop foo becoming local if it
doesn't exist, but there is always the problem with what to do when foo
already exists but has been unset.  The easiest thing is always to use that
value at the level at which was set.  This means, for example,

fn2() { typset -g foo; }
fn1() { typeset foo; unset foo; fn2(); foo=bar; }

that foo remains throughout local to fn1.  I think this version could be
implemented quite easily.  Any other version is going to be messy.

--- Src/subst.c.aa	Tue Jun 15 17:06:48 1999
+++ Src/subst.c	Wed Jun 23 11:51:00 1999
@@ -1328,10 +1328,15 @@
 			else
 			    t = aval;
 		    } else if (!isarr) {
-			arr[0] = val;
-			arr[1] = NULL;
+			if (!*val && arrasg > 1) {
+			    arr[0] = NULL;
+			    l = 0;
+			} else {
+			    arr[0] = val;
+			    arr[1] = NULL;
+			    l = 1;
+			}
 			t = aval = arr;
-			l = 1;
 		    } else
 			l = arrlen(aval), t = aval;
 		    p = a = zalloc(sizeof(char *) * (l + 1));

-- 
Peter Stephenson <pws@ibmth.df.unipi.it>       Tel: +39 050 844536
WWW:  http://www.ifh.de/~pws/
Dipartimento di Fisica, Via Buonarroti 2, 56127 Pisa, Italy


^ permalink raw reply	[flat|nested] 4+ messages in thread

* PATCH: Re: Possible bug with ${(A)foo=} and ${(AA)foo=}
  1999-06-23 10:02   ` Peter Stephenson
@ 1999-06-24 15:36     ` Bart Schaefer
  0 siblings, 0 replies; 4+ messages in thread
From: Bart Schaefer @ 1999-06-24 15:36 UTC (permalink / raw)
  To: ZSH workers mailing list

On Jun 23, 12:02pm, Peter Stephenson wrote:
} Subject: Re: Possible bug with ${(A)foo=} and ${(AA)foo=}
}
} Without fixing the general array-splitting problem, which is difficult, I
} don't see there's any problem deciding what to do in the case of an
} associative array, since the intention in such a case can only be to create
} an empty one because there is no key/value pair, even with ${(AA)foo=''}

OK, then ... here's a companion patch which defers allocation of the hash
table when there's nothing to put into it.

Index: Src/params.c
===================================================================
@@ -1898,7 +1898,7 @@
      * since that could cause trouble for special hashes.  This way, *
      * it's up to pm->sets.hfn() what to do.                         */
     int alen = arrlen(val);
-    HashTable opmtab = paramtab, ht;
+    HashTable opmtab = paramtab, ht = 0;
     char **aptr = val;
     Value v = (Value) hcalloc(sizeof *v);
     v->b = -1;
@@ -1909,7 +1909,8 @@
 	     NULL, 0);
 	return;
     }
-    ht = paramtab = newparamtable(17, pm->nam);
+    if (alen)
+	ht = paramtab = newparamtable(17, pm->nam);
     while (*aptr) {
 	/* The parameter name is ztrdup'd... */
 	v->pm = createparam(*aptr, PM_SCALAR|PM_UNSET);

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~1999-06-24 15:37 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-06-22 16:13 Possible bug with ${(A)foo=} and ${(AA)foo=} Andrej Borsenkow
1999-06-22 17:36 ` Bart Schaefer
1999-06-23 10:02   ` Peter Stephenson
1999-06-24 15:36     ` PATCH: " Bart Schaefer

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