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

Re: Issue with ${var#(*_)(#cN,M)}



Sigh.  Can't see the wood for the trees.  It is a backtracking problem,
but it's a simple bug with restoring the state when backtracking, not a
logical error in the matching machine.

I'll take out the weasel words again, shall I?

pws

diff --git a/Doc/Zsh/expn.yo b/Doc/Zsh/expn.yo
index 49a0f0d..5ea8610 100644
--- a/Doc/Zsh/expn.yo
+++ b/Doc/Zsh/expn.yo
@@ -2192,16 +2192,6 @@ inclusive.  The form tt(LPAR()#c)var(N)tt(RPAR()) requires exactly tt(N)
 matches; tt(LPAR()#c,)var(M)tt(RPAR()) is equivalent to specifying var(N)
 as 0; tt(LPAR()#c)var(N)tt(,RPAR()) specifies that there is no maximum
 limit on the number of matches.
-
-Note that if the previous group of characters contains wildcards,
-results can be unpredictable to the point of being logically incorrect.
-It is recommended that the pattern be trimmed to match the minimum
-possible.  For example, to match a string of the form `tt(1_2_3_)', use
-a pattern of the form `tt(LPAR()[[:digit:]]##_+RPAR()LPAR()#c3+RPAR())', not
-`tt(LPAR()*_+RPAR()LPAR()#c3+RPAR())'.  This arises from the
-complicated interaction between attempts to match a number of
-repetitions of the whole pattern and attempts to match the wildcard
-`tt(*)'.
 )
 vindex(MATCH)
 vindex(MBEGIN)
diff --git a/Src/pattern.c b/Src/pattern.c
index 8b07cca..9e8a80a 100644
--- a/Src/pattern.c
+++ b/Src/pattern.c
@@ -3376,6 +3376,7 @@ patmatch(Upat prog)
 		    scan[P_CT_CURRENT].l = cur + 1;
 		    if (patmatch(scan + P_CT_OPERAND))
 			return 1;
+		    scan[P_CT_CURRENT].l = cur;
 		    patinput = patinput_thistime;
 		}
 		if (cur < min)
diff --git a/Test/D02glob.ztst b/Test/D02glob.ztst
index 3e2095a..f944a4f 100644
--- a/Test/D02glob.ztst
+++ b/Test/D02glob.ztst
@@ -574,3 +574,11 @@
 0:Optimisation to squeeze multiple *'s used as ordinary glob wildcards.
 >glob.tmp/ra=1.0_et=3.5
 >glob.tmp/ra=1.0_et=3.5
+
+  [[ 1_2_ = (*_)(#c1) ]] && print 1 OK  # because * matches 1_2
+  [[ 1_2_ = (*_)(#c2) ]] && print 2 OK
+  [[ 1_2_ = (*_)(#c3) ]] || print 3 OK
+0:Some more complicated backtracking with match counts.
+>1 OK
+>2 OK
+>3 OK
diff --git a/Test/D04parameter.ztst b/Test/D04parameter.ztst
index f1cc23e..cb7079c 100644
--- a/Test/D04parameter.ztst
+++ b/Test/D04parameter.ztst
@@ -1735,3 +1735,12 @@
 0:History modifier works the same for scalar and array substitution
 >ddd bdb cdc
 >ddd bdb cdc
+
+ a=1_2_3_4_5_6
+ print ${a#(*_)(#c2)}
+ print ${a#(*_)(#c5)}
+ print ${a#(*_)(#c7)}
+0:Complicated backtracking with match counts
+>3_4_5_6
+>6
+>1_2_3_4_5_6



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