zsh-workers
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.w.stephenson@ntlworld.com>
To: zsh-workers@zsh.org
Subject: Re: (s) splitting – is there any way to provide «dynamic» separator
Date: Thu, 9 Oct 2014 22:14:59 +0100	[thread overview]
Message-ID: <20141009221459.7512f179@pws-pc.ntlworld.com> (raw)
In-Reply-To: <5436A310.7040101@gmail.com>

On Thu, 09 Oct 2014 21:00:32 +0600
Vasiliy Ivanov <beelzebubbie.logs@gmail.com> wrote:
> Sorry if I (maybe) missed something obvious, but I failed to find a way to use separator from
> parameter (e.g. a='1:2:3'; sep=':'; print -l ${(s.$sep.)a}).
> I think this is because expansion/substitution are not available in
> flags? If so, is there any  another way for such a «dynamic» splitting?

I don't think you've missed anything obvious or elegant.

One way is

(){
  local IFS=$sep
  print -l ${=a}
}

Or if you can rely on not having whitespace

print -l ${=${a//:/ }}

Or, of course,

eval print -l '${(s.'$sep'.)a}'

as long as you can sanity check $sep.


Of course there's nothing to stop us adding a simple parameter
substitution as a special case at this point (or any similarly delimited
parameter argument) ...  Can't see how this can do much harm since it
sure as heck *looks* like a parameter expansion.

diff --git a/Src/subst.c b/Src/subst.c
index 1aa9b98..5727c12 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 ((*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-09 21:15 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 [this message]
2014-10-09 21:48   ` Mikael Magnusson
2014-10-10 18:26     ` Peter Stephenson
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=20141009221459.7512f179@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).