From: "Jun T." <takimoto-j@kba.biglobe.ne.jp>
To: "zsh-workers@zsh.org" <zsh-workers@zsh.org>
Subject: Re: [PATCH] Optimization of getarrvalue()
Date: Wed, 16 Nov 2016 23:06:13 +0900 [thread overview]
Message-ID: <77EB3614-F9E2-4BEB-B93C-99DFD34A504F@kba.biglobe.ne.jp> (raw)
In-Reply-To: <20161115195721.43648236@ntlworld.com>
On 2016/11/16, at 4:57, Peter Stephenson <p.w.stephenson@ntlworld.com> wrote:
> On Mon, 14 Nov 2016 21:32:19 +0900
> "Jun T." <takimoto-j@kba.biglobe.ne.jp> wrote:
>>
>> zsh% nargs "${(@)a[i]}"
>>
>> will output 0 only for i=0. On the other hand
>>
>> zsh% nargs "${(@)a[i,i]}"
>>
>> will output 0 for i=0 and 2.
>
> 0 is an invalid subscript, which is probably the difference in the first
> case between it and other values.
The document says (15.2.1)
If the KSH_ARRAYS option is not set, then by default accesses to an
array element with a subscript that evaluates to zero return an empty
string,
so someone may expect that "${(@)a[0]}" is also replaced by an empty
string "" rather than removed from the command line. But I'm not sure.
And I fear changing this behavior might break some existing scripts.
> setopt rcexpandparam
> local -A hash=(X x)
> print LOST key=$hash[(I)y] val=$hash[(R)Y]
>
> outputs LOST with the arguments removed. With your change you get an
> array wth an empty element and that doesn't happen.
I haven't yet understood how associative arrays are handled in C code,
but how about the following patch? With this patch all the test passes,
and "${(@)a[i,i]}" becomes an empty array (and removed from the command
line) only if i=0. I think no (reasonable) scripts are relying on the
current behavior that "${(@)a[i,i]}" is removed if i==($#a+1).
diff --git a/Src/params.c b/Src/params.c
index 3c8658c..c501e5d 100644
--- a/Src/params.c
+++ b/Src/params.c
@@ -2291,11 +2291,12 @@ getarrvalue(Value v)
if (v->end < 0)
v->end += arrlen(s) + 1;
- /* Null if 1) array too short, 2) index still negative */
- if (v->end <= v->start) {
+ if (!*s || v->end <= v->start) {
+ /* Empty array if 1) s[] is empty or 2) inconsistent indexes */
s = arrdup_max(nular, 0);
}
- else if (arrlen_lt(s, v->start) || v->start < 0) {
+ else if (arrlen_le(s, v->start) || v->start < 0) {
+ /* Null if 1) array too short, 2) index still negative */
s = arrdup_max(nular, 1);
} else {
/* Copy to a point before the end of the source array:
next prev parent reply other threads:[~2016-11-16 14:43 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <CGME20161108201233epcas1p1e2900e2d67af8b8558ebdb70eb7ad480@epcas1p1.samsung.com>
2016-11-08 20:11 ` Sebastian Gniazdowski
2016-11-08 21:58 ` Bart Schaefer
2016-11-09 7:11 ` Bart Schaefer
2016-11-09 11:42 ` Peter Stephenson
2016-11-09 16:03 ` Bart Schaefer
2016-11-14 12:32 ` Jun T.
2016-11-14 13:15 ` Jun T.
2016-11-14 13:57 ` Peter Stephenson
2016-11-14 15:35 ` Jun T.
2016-11-14 17:10 ` Bart Schaefer
2016-11-16 7:55 ` Sebastian Gniazdowski
2016-11-15 12:28 ` Peter Stephenson
2016-11-15 19:57 ` Peter Stephenson
2016-11-15 21:11 ` Bart Schaefer
2016-11-16 14:06 ` Jun T. [this message]
2016-11-16 16:14 ` Jun T.
2016-11-16 18:50 ` Bart Schaefer
2016-11-21 12:30 ` Jun T.
2016-11-24 0:55 ` Bart Schaefer
2016-11-24 11:49 ` Jun T.
2016-11-29 6:11 ` Array slices that don't exist [was Optimization of getarrvalue()] Bart Schaefer
2016-11-29 9:34 ` Peter Stephenson
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=77EB3614-F9E2-4BEB-B93C-99DFD34A504F@kba.biglobe.ne.jp \
--to=takimoto-j@kba.biglobe.ne.jp \
--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).