Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: BUG: small posix glitch in ${x%*}



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
+>



Messages sorted by: Reverse Date, Date, Thread, Author