Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

[PATCH] Fix (e) subscript parameter used in scalar context



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