zsh-workers
 help / color / mirror / code / Atom feed
* BUG: small posix glitch in ${x%*}
@ 2019-12-09 22:07 ` Jan Grant
  2019-12-10 10:48   ` Peter Stephenson
  0 siblings, 1 reply; 2+ messages in thread
From: Jan Grant @ 2019-12-09 22:07 UTC (permalink / raw)
  To: zsh-workers

As I read the spec, the smallest suffix that can match * is the null string; so

    x=123; echo ${x%*}

should output "123". Zsh (as of 5.7.1) drops the last character.

(The behaviour for prefixes is correct.)

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

* Re: BUG: small posix glitch in ${x%*}
  2019-12-09 22:07 ` BUG: small posix glitch in ${x%*} Jan Grant
@ 2019-12-10 10:48   ` Peter Stephenson
  0 siblings, 0 replies; 2+ messages in thread
From: Peter Stephenson @ 2019-12-10 10:48 UTC (permalink / raw)
  To: zsh-workers

On Mon, 2019-12-09 at 22:07 +0000, Jan Grant wrote:
> As I read the spec, the smallest suffix that can match * is the null
> string; so
> 
>     x=123; echo ${x%*}
> 
> should output "123". Zsh (as of 5.7.1) drops the last character.
> 
> (The behaviour for prefixes is correct.)

Yes, that's right --- there are already some age-old icky special cases
for this in some places but not this one.  So a bit of copy and paste...

pws

diff --git a/Src/glob.c b/Src/glob.c
index 92fd64e7c..674563c8f 100644
--- a/Src/glob.c
+++ b/Src/glob.c
@@ -2909,6 +2909,12 @@ igetmatch(char **sp, Patprog p, int fl, int n, char *replstr,
 	     */
 	    mb_charinit();
 	    tmatch = NULL;
+	    set_pat_start(p, l);
+	    if (pattrylen(p, send, 0, 0, &patstralloc, umltot) &&
+		!--n) {
+		*sp = get_match_ret(&imd, umltot, umltot);
+		return 1;
+	    }
 	    for (ioff = 0, t = s, umlen = umltot; t < send; ioff++) {
 		set_pat_start(p, t-s);
 		if (pattrylen(p, t, umlen, 0, &patstralloc, ioff))
diff --git a/Test/D04parameter.ztst b/Test/D04parameter.ztst
index b6e85a9fe..c91af1a9c 100644
--- a/Test/D04parameter.ztst
+++ b/Test/D04parameter.ztst
@@ -2534,3 +2534,26 @@ F:behavior, see http://austingroupbugs.net/view.php?id=888
 0:Global variables are not trashed by "foo=bar builtin" (regression test)
 >function-value
 >global-value
+
+ foo=pws
+ print ${foo%*}
+0:Smallest match at end can match zero-length string
+>pws
+
+ foo=pws
+ print ${foo%?}
+0:Smallest match at end with a character always matches one
+>pw
+
+ setopt extendedglob
+ foo=pws
+ print ${foo%s#}
+ print ${foo%%s#}
+0:Smallest / largest match with non-trivial closure
+>pws
+>pw
+
+ foo=pws
+ print ${foo%%*}
+0:Largest match at end matches entire string
+>


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

end of thread, other threads:[~2019-12-10 10:49 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <CGME20191210014119eucas1p155aa663c9f726ba5ca07a37c54829bec@eucas1p1.samsung.com>
2019-12-09 22:07 ` BUG: small posix glitch in ${x%*} Jan Grant
2019-12-10 10:48   ` Peter Stephenson

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