From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 18712 invoked by alias); 29 Nov 2016 06:11:56 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: X-Seq: 40032 Received: (qmail 7828 invoked from network); 29 Nov 2016 06:11:55 -0000 X-Qmail-Scanner-Diagnostics: from mail-ua0-f171.google.com by f.primenet.com.au (envelope-from , uid 7791) with qmail-scanner-2.11 (clamdscan: 0.99.2/21882. spamassassin: 3.4.1. Clear:RC:0(209.85.217.171):SA:0(0.0/5.0):. Processed in 1.614806 secs); 29 Nov 2016 06:11:55 -0000 X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_PASS,T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.1 X-Envelope-From: schaefer@brasslantern.com X-Qmail-Scanner-Mime-Attachments: | X-Qmail-Scanner-Zip-Files: | Received-SPF: pass (ns1.primenet.com.au: SPF record at _netblocks.google.com designates 209.85.217.171 as permitted sender) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brasslantern-com.20150623.gappssmtp.com; s=20150623; h=from:message-id:date:in-reply-to:comments:references:to:subject :mime-version; bh=G2id9/1O4wXmsEzZz7nQBTlIhbEav7IZxI/6Wh21gew=; b=QQUK8w2qsct94lqXznruikKbaOF32rsC1gbOxg/ukkxP5bapTtqNRk3cMD3qK5bW+d tcYYB4JrBg7K+7btSRoHfB8U1C+hTS+HHmvP7I7QI2uckctySDRx1jSWimKr7rzAyUWE d+8Cu9LTOK8DzhnlIORfYb4syNKMKYvLzOwz7Ww9vgT7dRYpW2nRPNsV/QSdrdy41JTW OpbxwdttzXtmaLag3bShE970+KHQaYvsUiIADYNnHdk0n9FS/emtTvvI5I7GVnEfHakF Q0E61tc5PbTl9PWRkClHa2081lLtw9dIXqZUXL81IVwpQH1vWdlh/9xOWfEqf1c5nwIZ nS+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:message-id:date:in-reply-to:comments :references:to:subject:mime-version; bh=G2id9/1O4wXmsEzZz7nQBTlIhbEav7IZxI/6Wh21gew=; b=I0bM4MsXLGWMQjh9/rigBcIzkRX+zEwW8b8h7cKE3HN85U5cBOWg18rbc5JCtZFyQp Kt7wff39PdDEeWHUNWX5+np7aeQWjuULuErCvGf4V682EtjtEsm0HSj2AZqWCbhcTi/D xLLudYyKcg2hqZ9ys9AmW44jdFb7RKHzDmReqHahzlBzkfxKz0fMbecQa8d+tqCzYcDY FwOmSjXcXyxSuTnxO/jEOwU6rhg+btEPnn6QTBUNUwu8Wrgn1psU5bnOWsuzhVssq4K9 2nzN3iMaZTjBUtM46iLRwKxacvnM0bWrR9EklKaAnT8AzRRzVGKidVQJTn65l5/UHWUW aDgQ== X-Gm-Message-State: AKaTC03nX5eRI9fB2Zds2jwLZiIK1URN0w9Vhhw/pCW2ILJW69VY/oJQWA3e74BJ+l4D/g== X-Received: by 10.176.83.211 with SMTP id l19mr15521376uaa.69.1480399906103; Mon, 28 Nov 2016 22:11:46 -0800 (PST) From: Bart Schaefer Message-Id: <161128221144.ZM10240@torch.brasslantern.com> Date: Mon, 28 Nov 2016 22:11:44 -0800 In-Reply-To: Comments: In reply to "Jun T." "Re: [PATCH] Optimization of getarrvalue()" (Nov 24, 8:49pm) References: <1478635899.1897979.781551353.05792438@webmail.messagingengine.com> <20161109114207.6b929440@pwslap01u.europe.root.pri> <161109080328.ZM6075@torch.brasslantern.com> <20161115195721.43648236@ntlworld.com> <77EB3614-F9E2-4BEB-B93C-99DFD34A504F@kba.biglobe.ne.jp> <161116105013.ZM32526@torch.brasslantern.com> X-Mailer: OpenZMail Classic (0.9.2 24April2005) To: "zsh-workers@zsh.org" Subject: Array slices that don't exist [was Optimization of getarrvalue()] MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii On Nov 24, 8:49pm, Jun T. wrote: } } % a=(one two) } and see what is returned by } % nargs "${(@)a[i,j]}" } } For i >= 4 or i <= -3 it returns 1 if j >= i, } while for i=3 (=$#a+1) it returns 0 always. Hmm. Internally the array $a is stored as a[0]="one" a[1]="two" a[2]=NULL so when we look at $a[i,j] we decrement i but not j to look at the internal elements starting with a[i-1] and ending before (not including) a[j]. This means that if $i = $#a we're always going to take the branch that now has the comment /* Copy to a point before the end of the source array: * arrdup_max will copy at most v->end - v->start elements, * starting from v->start element. Original code said: * s[v->end - v->start] = NULL with v->start pointing at the NULL terminator of the internal array, so we return an empty array. If $i > $#a then we take the arrlen_lt(s, v->start) branch and always return a single element. So the question is ... is the following more consistent? for i in 1 2 3 4; do for j in 1 2 3 4 5; do print -n "$i $j = " nargs "${(@)a[i,j]}" done done 5.2 | 5.3 ** ----------+---------- 1 1 => 1 | 1 1 => 1 1 2 => 2 | 1 2 => 2 1 3 => 2 | 1 3 => 2 1 4 => 2 | 1 4 => 2 1 5 => 2 | 1 5 => 2 2 1 => 0 | 2 1 => 0 2 2 => 1 | 2 2 => 1 2 3 => 1 | 2 3 => 1 2 4 => 1 | 2 4 => 1 2 5 => 1 | 2 5 => 1 3 1 => 0 | 3 1 => 0 3 2 => 0 | 3 2 => 0 3 3 => 0 | 3 3 => 0 3 4 => 0 | 3 4 => 1 ** 3 5 => 0 | 3 5 => 1 ** 4 1 => 0 | 4 1 => 0 4 2 => 0 | 4 2 => 0 4 3 => 0 | 4 3 => 0 4 4 => 1 | 4 4 => 0 ** 4 5 => 1 | 4 5 => 1 This means you have to slice more than one non-existent element to get an empty string; a slice of one such element is an empty array. All "make check" tests still pass with that behavior though I'm not certain that all completions would work identically. Code looks like this: diff --git a/Src/params.c b/Src/params.c index 45f398a..aa8b196 100644 --- a/Src/params.c +++ b/Src/params.c @@ -2299,9 +2299,16 @@ getarrvalue(Value v) if (v->end <= v->start) { s = arrdup_max(nular, 0); } - else if (arrlen_lt(s, v->start) || v->start < 0) { + else if (v->start < 0) { s = arrdup_max(nular, 1); - } else { + } + else if (arrlen_le(s, v->start)) { + /* Handle $ary[i,i] consistently for any $i > $#ary + * and $ary[i,j] consistently for any $j > $i > $#ary + */ + s = arrdup_max(nular, v->end - (v->start + 1)); + } + else { /* Copy to a point before the end of the source array: * arrdup_max will copy at most v->end - v->start elements, * starting from v->start element. Original code said: