zsh-workers
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.w.stephenson@ntlworld.com>
To: zsh workers <zsh-workers@zsh.org>
Subject: Re: (s) splitting – is there any way to provide «dynamic» separator
Date: Fri, 10 Oct 2014 19:26:44 +0100	[thread overview]
Message-ID: <20141010192644.0dfa3ecf@pws-pc.ntlworld.com> (raw)
In-Reply-To: <CAHYJk3Tk2OaHBZWAni9CTVxF6qBqBZOyA9VivifQgY-ktjwhUA@mail.gmail.com>


On Thu, 9 Oct 2014 23:48:37 +0200
Mikael Magnusson <mikachu@gmail.com> wrote:
> I don't know if it's worth it but we could possibly elect to only do
> this expansion if the p flag is active? Right now it means to expand
> \t into a literal tab, but it makes sense to have it more generally
> mean "expand stuff" in the string I think?

Yes, that works very well because anything with an escape is not a
parameter expansion.  So you're only going to come a cropper if you use
the (p) flag for no reason.  It's also easy and gives me a single place
to document the effect.

We could in principle do a full singsub() on the string argument when
the flag is present, but I think that's asking for trouble in terms of
complexity.

diff --git a/Doc/Zsh/expn.yo b/Doc/Zsh/expn.yo
index 5aab259..a0478e7 100644
--- a/Doc/Zsh/expn.yo
+++ b/Doc/Zsh/expn.yo
@@ -1124,6 +1124,19 @@ item(tt(p))(
 Recognize the same escape sequences as the tt(print) builtin
 in string arguments to any of the flags described below that
 follow this argument.
+
+Alternatively, with this option string arguments may be in the form
+tt($)var(var) in which case the value of the variable is substituted.
+Note this form is strict; the string argument does not undergo general
+parameter expansion.
+
+For example,
+
+example(sep=:
+val=a:b:c
+print ${+LPAR()ps.$sep.+RPAR()val})
+
+splits the variable on a tt(:).
 )
 item(tt(~))(
 Strings inserted into the expansion by any of the flags below are to
diff --git a/Src/subst.c b/Src/subst.c
index 1aa9b98..61aa1c1 100644
--- a/Src/subst.c
+++ b/Src/subst.c
@@ -1385,12 +1385,23 @@ static char *
 untok_and_escape(char *s, int escapes, int tok_arg)
 {
     int klen;
-    char *dst;
+    char *dst = NULL;
 
-    untokenize(dst = dupstring(s));
-    if (escapes) {
-	dst = getkeystring(dst, &klen, GETKEYS_SEP, NULL);
-	dst = metafy(dst, klen, META_HREALLOC);
+    if (escapes && (*s == String || *s == Qstring) && s[1]) {
+	char *pstart = s+1, *pend;
+	for (pend = pstart; *pend; pend++)
+	    if (!iident(*pend))
+		break;
+	if (!*pend) {
+	    dst = dupstring(getsparam(pstart));
+	}
+    }
+    if (dst == NULL) {
+	untokenize(dst = dupstring(s));
+	if (escapes) {
+	    dst = getkeystring(dst, &klen, GETKEYS_SEP, NULL);
+	    dst = metafy(dst, klen, META_HREALLOC);
+	}
     }
     if (tok_arg)
 	shtokenize(dst);

-- 
Peter Stephenson <p.w.stephenson@ntlworld.com>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/


  reply	other threads:[~2014-10-10 18:26 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-10-09 15:00 Vasiliy Ivanov
2014-10-09 21:14 ` Peter Stephenson
2014-10-09 21:48   ` Mikael Magnusson
2014-10-10 18:26     ` Peter Stephenson [this message]
2014-10-13 10:30       ` Peter Stephenson
2014-10-10  1:45 ` (s) splitting - is there any way to provid e "dynamic" separator Bart Schaefer
2014-10-10  7:06   ` (s) splitting - is there any way to provide " Vasiliy Ivanov
2014-10-11  0:38     ` Han Pingtian
2014-10-11  3:31       ` Bart Schaefer
2014-10-11 11:52         ` Han Pingtian

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=20141010192644.0dfa3ecf@pws-pc.ntlworld.com \
    --to=p.w.stephenson@ntlworld.com \
    --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).