zsh-workers
 help / color / mirror / code / Atom feed
* bar='#pat'; ${foo/$bar/...} problem
@ 1999-04-01 14:04 Andrej Borsenkow
  1999-04-02 10:39 ` Bart Schaefer
  0 siblings, 1 reply; 4+ messages in thread
From: Andrej Borsenkow @ 1999-04-01 14:04 UTC (permalink / raw)
  To: ZSH workers mailing list

It seems to be impossible to specify `#' (and probably `%') as part of the
replaced pattern:

bor@itsrm2:/tools/src/zsh-3.1.5-pws-14%> zsh -f
itsrm2% foo=(xxya xxyb)
itsrm2% bar=('xx' 'zz')
itsrm2% print ${foo/$bar[1]/$bar[2]}
zzya zzyb
itsrm2% bar=('#xx' 'zz')
itsrm2% print ${foo/$bar[1]/$bar[2]}
xxya xxyb
itsrm2% print ${foo/$~bar[1]/$bar[2]}
xxya xxyb
itsrm2% setopt extendedglob
itsrm2% print ${foo/$bar[1]/$bar[2]}
xxya xxyb
itsrm2% print ${foo/$~bar[1]/$bar[2]}
zsh: bad pattern: #xx
itsrm2% bar=('\#xx' 'zz')
itsrm2% print ${foo/$~bar[1]/$bar[2]}
xxya xxyb
itsrm2% print ${foo/$bar[1]/$bar[2]}
xxya xxyb
itsrm2%

Only the frirst one yields the correct result. But I need exactly the
run-time pattern, that should match only at the beginning ...

cheers

/andrej


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: bar='#pat'; ${foo/$bar/...} problem
  1999-04-01 14:04 bar='#pat'; ${foo/$bar/...} problem Andrej Borsenkow
@ 1999-04-02 10:39 ` Bart Schaefer
  1999-04-02 11:13   ` PATCH: 3.1.5-pws-14: " Bart Schaefer
  0 siblings, 1 reply; 4+ messages in thread
From: Bart Schaefer @ 1999-04-02 10:39 UTC (permalink / raw)
  To: ZSH workers mailing list

On Apr 1,  6:04pm, Andrej Borsenkow wrote:
} Subject: bar='#pat'; ${foo/$bar/...} problem
}
} It seems to be impossible to specify `#' (and probably `%') as part of the
} replaced pattern:

What you mean here is, specify left-anchored or right-anchored patterns,
right?  In that case, the # or % is not "part of the pattern", it's part
of the ${...} syntax.  The rest is just a glob pattern and doesn't have
any built-in notion of "anchored."  So really there are three:

	${foo/pat/rep}		pat --> rep, anywhere in foo
	${foo/#pat/rep}		pat --> rep, at left of foo
	${foo/%pat/rep}		pat --> rep, at right of foo

Once you get that, it's easy to see that what you're attempting is the
same as if you did

	% bar=('/#xx' '/zz'}
	% print ${foo$bar[1]$bar[2]}

which you obviously wouldn't expect to work (I hope).

Now, you might argue that the implementation of /# and /% should be as
if # and % are extensions of the glob syntax, and in fact that is how
it appears to be implemented in bash:

	$ foo=xxya
	$ bar='#xx'
	$ echo ${foo/$bar/zz}
	zzya

In the meantime, though, you need an eval step of some kind:

	% foo=(xxya xxyb)
	% bar=('#xx' 'zz')
	% eval print '${foo/'"$bar[1]"'/$bar[2]}'

(Did I get to be article 6000? :-)

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com


^ permalink raw reply	[flat|nested] 4+ messages in thread

* PATCH: 3.1.5-pws-14: Re: bar='#pat'; ${foo/$bar/...} problem
  1999-04-02 10:39 ` Bart Schaefer
@ 1999-04-02 11:13   ` Bart Schaefer
  1999-04-02 11:33     ` PATCH: " Bart Schaefer
  0 siblings, 1 reply; 4+ messages in thread
From: Bart Schaefer @ 1999-04-02 11:13 UTC (permalink / raw)
  To: ZSH workers mailing list

On Apr 2,  2:39am, Bart Schaefer wrote:
} Subject: Re: bar='#pat'; ${foo/$bar/...} problem
}
} Now, you might argue that the implementation of /# and /% should be as
} if # and % are extensions of the glob syntax

Index: Src/subst.c
===================================================================
--- subst.c	1999/03/29 21:45:50	1.22
+++ subst.c	1999/04/02 11:06:21
@@ -1361,7 +1361,19 @@
 			 NULL, s[-1]);
 		    return NULL;
 		}
-	    singsub(&s);
+	    {
+		char t = s[-1];
+
+		singsub(&s);
+		if (t == '/' && (flags & SUB_SUBSTR)) {
+		    if (*s == '#' || *s == '%') {
+			flags &= ~SUB_SUBSTR;
+			if (*s == '%')
+			    flags |= SUB_END;
+			s++;
+		    }
+		}
+	    }
 
 	    if (!vunset && isarr) {
 		char **ap = aval;

} (Did I get to be article 6000? :-)

(Hopefully, I did a better job of patching than I did of noticing which
message's headers I was reading the X-Seq: from.  Hard to be 6000 when
that's the one you're replying to.)

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com


^ permalink raw reply	[flat|nested] 4+ messages in thread

* PATCH: Re: PATCH: 3.1.5-pws-14: Re: bar='#pat'; ${foo/$bar/...} problem
  1999-04-02 11:13   ` PATCH: 3.1.5-pws-14: " Bart Schaefer
@ 1999-04-02 11:33     ` Bart Schaefer
  0 siblings, 0 replies; 4+ messages in thread
From: Bart Schaefer @ 1999-04-02 11:33 UTC (permalink / raw)
  To: ZSH workers mailing list

On Apr 2,  3:13am, Bart Schaefer wrote:
} Subject: PATCH: 3.1.5-pws-14: Re: bar='#pat'; ${foo/$bar/...} problem
}
} Hopefully, I did a better job of patching than I did of noticing which
} message's headers I was reading the X-Seq: from.

Not too bad, but I noticed that bash lets you quote the leading # or %
like this: ${foo/\\$pat/$repl}

I think this handles it:

Index: Src/subst.c
===================================================================
--- subst.c	1999/04/02 11:26:29	1.23
+++ subst.c	1999/04/02 11:27:33
@@ -1371,6 +1371,8 @@
 			if (*s == '%')
 			    flags |= SUB_END;
 			s++;
+		    } else if (*s == '\\') {
+			s++;
 		    }
 		}
 	    }

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~1999-04-02 11:34 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-04-01 14:04 bar='#pat'; ${foo/$bar/...} problem Andrej Borsenkow
1999-04-02 10:39 ` Bart Schaefer
1999-04-02 11:13   ` PATCH: 3.1.5-pws-14: " Bart Schaefer
1999-04-02 11:33     ` PATCH: " Bart Schaefer

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).