From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 18050 invoked by alias); 1 Aug 2014 17:44:36 -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: 32937 Received: (qmail 7114 invoked from network); 1 Aug 2014 17:44:24 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:subject:date:message-id; bh=3vlBfNNfaIvIC35BHoudHiAojp3wsGgEdJVVWaWXes4=; b=W3q2xc7Ed/BOW9ZTt1WiOC7EoR8of3A+UOuUCVBjMdKVjmKv50Cqu77y5e54bYbJDz 4WtIw49q7oc89DGwtybwW6AVYYNBZKSQqLT7kTwqV9YXYNKpBe9HsPdFGrb2DnxrwVA8 06VQFETrUBhEp9YZT1WqY1Y24luQ2p/wOWD+70RCqmQwaiQXmYzGhshOk+d8c7ie3hQn DFOEMgGQkt0A9dCFUYaVYVxmiS2lJQsqyg7zSW/cxIdrYBWQjscY4yb2ftdB+iVCu3dj MRqyGpQq3rVfDWYWK4RMdoAvmZoBa3cn/TRpqSDJ9WOff3QkFjUN5+GY1nGtK4oG+LWk mS1g== X-Received: by 10.180.99.65 with SMTP id eo1mr8421559wib.12.1406908199213; Fri, 01 Aug 2014 08:49:59 -0700 (PDT) From: Mikael Magnusson To: zsh-workers@zsh.org Subject: PATCH: Add :^ syntax for zipping two arrays (v2) Date: Fri, 1 Aug 2014 17:47:38 +0200 Message-Id: <1406908058-7730-1-git-send-email-mikachu@gmail.com> X-Mailer: git-send-email 1.9.0 Here's my current version with :^ and :^^ and using heap alloc including hmkarray. --- Doc/Zsh/expn.yo | 17 +++++++++++++++++ Src/subst.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/Doc/Zsh/expn.yo b/Doc/Zsh/expn.yo index ff9f2b1..4d28d86 100644 --- a/Doc/Zsh/expn.yo +++ b/Doc/Zsh/expn.yo @@ -636,6 +636,23 @@ Similar to the preceding subsitution, but in the opposite sense, so that entries present in both the original substitution and as elements of var(arrayname) are retained and others removed. ) +xitem(tt(${)var(name)tt(:^)var(arrayname)tt(})) +item(tt(${)var(name)tt(:^^)var(arrayname)tt(}))( +Zips two arrays, such that the output array is twice as long as the +shortest (longest for `tt(:^^)') of tt(name) and tt(arrayname), with +the elements alternatingly being picked from them. For `tt(:^)', if one +of the input arrays is longer, the output will stop when the end of the +shorter array is reached. Thus, + +example(a=(1 2 3 4); b=(a b); print ${a:^b}) + +will output `tt(1 a 2 b)'. For `tt(:^^)', then the input is repeated +until all of the longer array has been used up and the above will output +`tt(1 a 2 b 3 a 4 b)'. + +Either or both inputs may be a scalar, they will be treated as an array +of length 1 with the scalar as the only element. +) xitem(tt(${)var(name)tt(:)var(offset)tt(})) item(tt(${)var(name)tt(:)var(offset)tt(:)var(length)tt(}))( This syntax gives effects similar to parameter subscripting diff --git a/Src/subst.c b/Src/subst.c index 944492f..21e1a1a 100644 --- a/Src/subst.c +++ b/Src/subst.c @@ -2878,6 +2878,62 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int pf_flags) } break; } + } else if (inbrace && (*s == '^' || *s == Hat)) { + char **zip, **ap, **apsrc; + int shortest = 1; + ++s; + if (*s == '^' || *s == Hat) { + shortest = 0; + ++s; + } + if (*itype_end(s, IIDENT, 0)) { + untokenize(s); + zerr("not an identifier: %s", s); + return NULL; + } + if (vunset) { + if (unset(UNSET)) { + *idend = '\0'; + zerr("%s: parameter not set", idbeg); + return NULL; + } + val = dupstring(""); + } else { + char *sval; + zip = getaparam(s); + if (!zip) { + sval = getsparam(s); + if (sval) + zip = hmkarray(sval); + } + if (!isarr) aval = mkarray(val); + if (zip) { + char **out; + int alen, ziplen, outlen, i = 0; + alen = arrlen(aval); + ziplen = arrlen(zip); + outlen = shortest ^ (alen > ziplen) ? alen : ziplen; + out = zhalloc(sizeof(char *) * (2 * outlen + 1)); + while (i < outlen) { + if (copied) + out[i*2] = aval[i % alen]; + else + out[i*2] = dupstring(aval[i % alen]); + out[i*2+1] = dupstring(zip[i % ziplen]); + i++; + } + out[i*2] = NULL; + aval = out; + copied = 1; + isarr = 1; + } else { + if (unset(UNSET)) { + zerr("%s: parameter not set", s); + return NULL; + } + val = dupstring(""); + } + } } else if (inbrace && (*s == '|' || *s == Bar || *s == '*' || *s == Star)) { int intersect = (*s == '*' || *s == Star); -- 1.9.0