Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
[PATCH] Fix (e) subscript parameter used in scalar context
- X-seq: zsh-workers 42297
- From: dana <dana@xxxxxxx>
- To: zsh-workers@xxxxxxx
- Subject: [PATCH] Fix (e) subscript parameter used in scalar context
- Date: Thu, 18 Jan 2018 15:57:56 -0600
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dana-is.20150623.gappssmtp.com; s=20150623; h=from:content-transfer-encoding:mime-version:subject:message-id:date :to; bh=osHLMDjNQf2OgY4LtGKcU5vychvkdEUcMhfCmXx/FGA=; b=BtakSCzY42ZkIhbYA/l3+MDsm/whn3zoEe99u3hhRnn81gGa9MI7IM+5sZ9sajSchW pVFij+OvmYU9JXHa0ICh2yx6+kCp7yA1z0hh+iTP+jASj/RPpP1J6im3Okm5vhb4rYYj pDrFWQC9lxo0cGLACcdV9pMsRaFAojKXAB01CZBzfeRpOFgW8lzyxqaKXvmwE3LE+WQH ksfoPrNyvCJeImnhrZb1+EP38vHs93eLdm2daI+kmq1Rg/C0SuoXFSO8kxy8l4Qfn16B /vhe0ONewMjJNPljlqpBkyCcrWqJJkYTk8VuZzPNhShfmfN6zx/Bomc+w8+7RibNJjzT 9BWQ==
- List-help: <mailto:zsh-workers-help@zsh.org>
- List-id: Zsh Workers List <zsh-workers.zsh.org>
- List-post: <mailto:zsh-workers@zsh.org>
- List-unsubscribe: <mailto:zsh-workers-unsubscribe@zsh.org>
- Mailing-list: contact zsh-workers-help@xxxxxxx; run by ezmlm
Someone on IRC (i forget who) found that the (e) subscript parameter doesn't
behave as expected when used with scalars:
% a='foo?bar'
% print $a[(ei)?]
8 # should be 4
This is apparently because an implicit * is added to the beginning/end of the
pattern in scalar context, and it's left untokenised when (e) is used.
The way i did this feels a bit redundant, but it fixes it anyway.
dana
diff --git a/Src/params.c b/Src/params.c
index de7730ae7..062df733d 100644
--- a/Src/params.c
+++ b/Src/params.c
@@ -1513,7 +1513,7 @@ getarg(char **str, int *inv, Value v, int a2, zlong *w,
}
}
} else {
- if (!v->isarr && !word) {
+ if (!v->isarr && !word && !quote_arg) {
l = strlen(s);
if (a2) {
if (!l || *s != '*') {
@@ -1532,9 +1532,23 @@ getarg(char **str, int *inv, Value v, int a2, zlong *w,
}
}
if (!keymatch) {
- if (quote_arg)
+ if (quote_arg) {
untokenize(s);
- else
+ // Scalar (e) needs implicit asterisk tokens
+ if (!v->isarr && !word) {
+ l = strlen(s);
+ d = (char *) hcalloc(l + 2);
+ if (a2) {
+ *d = Star;
+ strcpy(d + 1, s);
+ } else {
+ strcpy(d, s);
+ d[l] = Star;
+ d[l + 1] = '\0';
+ }
+ s = d;
+ }
+ } else
tokenize(s);
remnulargs(s);
pprog = patcompile(s, 0, NULL);
diff --git a/Test/D06subscript.ztst b/Test/D06subscript.ztst
index f0a858b1c..3ea7fb7e4 100644
--- a/Test/D06subscript.ztst
+++ b/Test/D06subscript.ztst
@@ -273,3 +273,19 @@
print ${string[1,twoarg(1,4)]}
0:Commas inside parentheses do not confuse subscripts
>abc
+
+ string='foobarbaz foob?rbaz foob?rbaz'
+ print $string[(i)b?r] $string[(I)b?r]
+ print $string[(r)b?r] $string[(R)b?r]
+ print $string[(r)b?r,(R)b?r]
+ print $string[(ei)b?r] $string[(eI)b?r]
+ print $string[(er)b?r] $string[(eR)b?r]
+ print $string[(er)b?r,(eR)b?r]
+0:Pattern handling with scalars
+F:Regression test for workers/42297
+>4 24
+>b b
+>barbaz foob?rbaz foob?r
+>14 24
+>b b
+>b?rbaz foob?r
Messages sorted by:
Reverse Date,
Date,
Thread,
Author