zsh-workers
 help / color / mirror / code / Atom feed
From: dana <dana@dana.is>
To: zsh-workers@zsh.org
Subject: [PATCH] Fix (e) subscript parameter used in scalar context
Date: Thu, 18 Jan 2018 15:57:56 -0600	[thread overview]
Message-ID: <087F6EC8-F0D8-4BAF-A457-8510BB334D97@dana.is> (raw)

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


                 reply	other threads:[~2018-01-18 21:58 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=087F6EC8-F0D8-4BAF-A457-8510BB334D97@dana.is \
    --to=dana@dana.is \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).