Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: (s) splitting – is there any way to provide «dynamic» separator
On Thu, 9 Oct 2014 23:48:37 +0200
Mikael Magnusson <mikachu@xxxxxxxxx> 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@xxxxxxxxxxxx>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/
Messages sorted by:
Reverse Date,
Date,
Thread,
Author