Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
PATCH: Add :^ syntax for zipping two arrays
- X-seq: zsh-workers 32928
- From: Mikael Magnusson <mikachu@xxxxxxxxx>
- To: zsh-workers@xxxxxxx
- Subject: PATCH: Add :^ syntax for zipping two arrays
- Date: Thu, 31 Jul 2014 19:45:39 +0200
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:subject:date:message-id; bh=wrkfrs1u+z/zg2ZTotskeYnHAMoessYZnKt09Pcl9PU=; b=DOCR/D1hRj4Hl1UNCcW3+LoULeOFhfirk4bfFXg+hxKhZzY2QkH2n6kzUPt8S6z6GW EgTzEkvPFWmB3MwXZFZllB8I6FJOT1nlaGAFNz2PZIY/5NwX8EP/10eigUYA0JS6s+Vq DTEBbMgzKY4AIQrT3JOL46/+KRTi2jzxVJ8qZyTReXR2YVhLHwszSQDZL6qmm+Jc+mqo Aq2JWeLt5D3YyWxIs0AHm3r6BXtSdQl30Xf+8NDusLY4LvHyuEvEHNVTPiZkbHtl6jvC SgxUlFoO+86mBepOmtDZF1T+tedidLDEax3qM3/ENxTAJdpjHkUHDWRE88V7Fb+buNk7 /VGQ==
- List-help: <mailto:zsh-workers-help@zsh.org>
- List-id: Zsh Workers List <zsh-workers.zsh.org>
- List-post: <mailto:zsh-workers@zsh.org>
- Mailing-list: contact zsh-workers-help@xxxxxxx; run by ezmlm
Any objections to adding this? Comments on the code is also welcome
since I'm not at all sure I thought of all the gotchas in paramsubst(),
in particular some allocations might be leaking? Not adding tests until
any and all comments are incorporated.
---
Doc/Zsh/expn.yo | 12 ++++++++++++
Src/subst.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 63 insertions(+)
diff --git a/Doc/Zsh/expn.yo b/Doc/Zsh/expn.yo
index ff9f2b1..2fb803a 100644
--- a/Doc/Zsh/expn.yo
+++ b/Doc/Zsh/expn.yo
@@ -636,6 +636,18 @@ 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.
)
+item(tt(${)var(name)tt(:^)var(arrayname)tt(}))(
+Zips two arrays, such that the output array is twice as long as the
+longest of tt(name) and tt(arrayname), with the elements alternatingly
+being picked from them. If one of the input arrays is shorter, then the
+input is repeated until all of the other array has been used up. Thus,
+
+example(a=(1 2 3 4); b=(a b); print ${a:^b})
+
+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 d4b68e2..a66ce01 100644
--- a/Src/subst.c
+++ b/Src/subst.c
@@ -2878,6 +2878,57 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int pf_flags)
}
break;
}
+ } else if (inbrace && (*s == '^' || *s == Hat)) {
+ char **zip, **ap, **apsrc;
+ ++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 = mkarray(sval);
+ }
+ if (!isarr) aval = mkarray(val);
+ if (zip) {
+ char **out;
+ int alen, ziplen, outlen, i = 0;
+ alen = arrlen(aval);
+ ziplen = arrlen(zip);
+ outlen = alen > ziplen ? alen : ziplen;
+ out = zalloc(sizeof(char *) * (2 * outlen + 1));
+ while (i < outlen) {
+ if (copied)
+ out[i*2] = aval[i % alen];
+ else
+ out[i*2] = ztrdup(aval[i % alen]);
+ out[i*2+1] = ztrdup(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
Messages sorted by:
Reverse Date,
Date,
Thread,
Author