Karsten Hopp a2374c
To: vim_dev@googlegroups.com
Karsten Hopp a2374c
Subject: Patch 7.3.1157
Karsten Hopp a2374c
Fcc: outbox
Karsten Hopp a2374c
From: Bram Moolenaar <Bram@moolenaar.net>
Karsten Hopp a2374c
Mime-Version: 1.0
Karsten Hopp a2374c
Content-Type: text/plain; charset=UTF-8
Karsten Hopp a2374c
Content-Transfer-Encoding: 8bit
Karsten Hopp a2374c
------------
Karsten Hopp a2374c
Karsten Hopp a2374c
Patch 7.3.1157
Karsten Hopp a2374c
Problem:    New regexp engine fails on "\(\
Karsten Hopp a2374c
Solution:   Fix rule for postponing match.  Further tune estimating whether
Karsten Hopp a2374c
	    postponing works better.  Add test.
Karsten Hopp a2374c
Files:	    src/regexp_nfa.c, src/testdir/test64.in, src/testdir/test64.ok
Karsten Hopp a2374c
Karsten Hopp a2374c
Karsten Hopp a2374c
*** ../vim-7.3.1156/src/regexp_nfa.c	2013-06-09 20:50:56.000000000 +0200
Karsten Hopp a2374c
--- src/regexp_nfa.c	2013-06-10 16:28:58.000000000 +0200
Karsten Hopp a2374c
***************
Karsten Hopp a2374c
*** 4587,4592 ****
Karsten Hopp a2374c
--- 4587,4593 ----
Karsten Hopp a2374c
  
Karsten Hopp a2374c
  /*
Karsten Hopp a2374c
   * Estimate the chance of a match with "state" failing.
Karsten Hopp a2374c
+  * empty match: 0
Karsten Hopp a2374c
   * NFA_ANY: 1
Karsten Hopp a2374c
   * specific character: 99
Karsten Hopp a2374c
   */
Karsten Hopp a2374c
***************
Karsten Hopp a2374c
*** 4616,4622 ****
Karsten Hopp a2374c
--- 4617,4625 ----
Karsten Hopp a2374c
  	case NFA_ANY:
Karsten Hopp a2374c
  	    /* matches anything, unlikely to fail */
Karsten Hopp a2374c
  	    return 1;
Karsten Hopp a2374c
+ 
Karsten Hopp a2374c
  	case NFA_MATCH:
Karsten Hopp a2374c
+ 	case NFA_MCLOSE:
Karsten Hopp a2374c
  	    /* empty match works always */
Karsten Hopp a2374c
  	    return 0;
Karsten Hopp a2374c
  
Karsten Hopp a2374c
***************
Karsten Hopp a2374c
*** 4664,4670 ****
Karsten Hopp a2374c
  	case NFA_ZCLOSE9:
Karsten Hopp a2374c
  #endif
Karsten Hopp a2374c
  	case NFA_NOPEN:
Karsten Hopp a2374c
- 	case NFA_MCLOSE:
Karsten Hopp a2374c
  	case NFA_MCLOSE1:
Karsten Hopp a2374c
  	case NFA_MCLOSE2:
Karsten Hopp a2374c
  	case NFA_MCLOSE3:
Karsten Hopp a2374c
--- 4667,4672 ----
Karsten Hopp a2374c
***************
Karsten Hopp a2374c
*** 5095,5117 ****
Karsten Hopp a2374c
  	    case NFA_START_INVISIBLE_BEFORE:
Karsten Hopp a2374c
  	    case NFA_START_INVISIBLE_BEFORE_NEG:
Karsten Hopp a2374c
  		{
Karsten Hopp a2374c
! 		    int cout = t->state->out1->out->c;
Karsten Hopp a2374c
  
Karsten Hopp a2374c
! 		    /* Do it directly when what follows is possibly end of
Karsten Hopp a2374c
! 		     * match (closing paren).
Karsten Hopp a2374c
! 		     * Do it directly if there already is a PIM.
Karsten Hopp a2374c
! 		     * Postpone when it is \@<= or \@
Karsten Hopp a2374c
! 		     * Otherwise first do the one that has the highest chance
Karsten Hopp a2374c
! 		     * of failing. */
Karsten Hopp a2374c
! 		    if ((cout >= NFA_MCLOSE && cout <= NFA_MCLOSE9)
Karsten Hopp a2374c
! #ifdef FEAT_SYN_HL
Karsten Hopp a2374c
! 			    || (cout >= NFA_ZCLOSE && cout <= NFA_ZCLOSE9)
Karsten Hopp a2374c
  #endif
Karsten Hopp a2374c
! 			    || t->pim.result != NFA_PIM_UNUSED
Karsten Hopp a2374c
! 			    || (t->state->c != NFA_START_INVISIBLE_BEFORE
Karsten Hopp a2374c
! 			        && t->state->c != NFA_START_INVISIBLE_BEFORE_NEG
Karsten Hopp a2374c
! 				&& failure_chance(t->state->out1->out, 0)
Karsten Hopp a2374c
! 					  < failure_chance(t->state->out, 0)))
Karsten Hopp a2374c
  		    {
Karsten Hopp a2374c
  			/*
Karsten Hopp a2374c
  			 * First try matching the invisible match, then what
Karsten Hopp a2374c
--- 5097,5142 ----
Karsten Hopp a2374c
  	    case NFA_START_INVISIBLE_BEFORE:
Karsten Hopp a2374c
  	    case NFA_START_INVISIBLE_BEFORE_NEG:
Karsten Hopp a2374c
  		{
Karsten Hopp a2374c
! 		    int directly = FALSE;
Karsten Hopp a2374c
  
Karsten Hopp a2374c
! #ifdef ENABLE_LOG
Karsten Hopp a2374c
! 		    fprintf(log_fd, "Failure chance invisible: %d, what follows: %d\n",
Karsten Hopp a2374c
! 			    failure_chance(t->state->out, 0),
Karsten Hopp a2374c
! 			    failure_chance(t->state->out1->out, 0));
Karsten Hopp a2374c
  #endif
Karsten Hopp a2374c
! 		    /* Do it directly when what follows is possibly the end of
Karsten Hopp a2374c
! 		     * the match.
Karsten Hopp a2374c
! 		     * Do it directly if there already is a PIM.
Karsten Hopp a2374c
! 		     * Postpone when the invisible match is expensive or has a
Karsten Hopp a2374c
! 		     * lower chance of failing. */
Karsten Hopp a2374c
! 		    if (match_follows(t->state->out1->out, 0)
Karsten Hopp a2374c
! 					   || t->pim.result != NFA_PIM_UNUSED)
Karsten Hopp a2374c
! 			directly = TRUE;
Karsten Hopp a2374c
! 		    else
Karsten Hopp a2374c
! 		    {
Karsten Hopp a2374c
! 			int ch_invisible = failure_chance(t->state->out, 0);
Karsten Hopp a2374c
! 			int ch_follows = failure_chance(t->state->out1->out, 0);
Karsten Hopp a2374c
! 
Karsten Hopp a2374c
! 			if (t->state->c == NFA_START_INVISIBLE_BEFORE
Karsten Hopp a2374c
! 			     || t->state->c == NFA_START_INVISIBLE_BEFORE_NEG)
Karsten Hopp a2374c
! 			{
Karsten Hopp a2374c
! 			    /* "before" matches are very expensive when
Karsten Hopp a2374c
! 			     * unbounded, always prefer what follows then,
Karsten Hopp a2374c
! 			     * unless what follows will always match.
Karsten Hopp a2374c
! 			     * Otherwise strongly prefer what follows. */
Karsten Hopp a2374c
! 			    if (t->state->val <= 0 && ch_follows > 0)
Karsten Hopp a2374c
! 				directly = FALSE;
Karsten Hopp a2374c
! 			    else
Karsten Hopp a2374c
! 				directly = ch_follows * 10 < ch_invisible;
Karsten Hopp a2374c
! 			}
Karsten Hopp a2374c
! 			else
Karsten Hopp a2374c
! 			{
Karsten Hopp a2374c
! 			    /* normal invisible, first do the one with the
Karsten Hopp a2374c
! 			     * highest failure chance */
Karsten Hopp a2374c
! 			    directly = ch_follows < ch_invisible;
Karsten Hopp a2374c
! 			}
Karsten Hopp a2374c
! 		    }
Karsten Hopp a2374c
! 		    if (directly)
Karsten Hopp a2374c
  		    {
Karsten Hopp a2374c
  			/*
Karsten Hopp a2374c
  			 * First try matching the invisible match, then what
Karsten Hopp a2374c
*** ../vim-7.3.1156/src/testdir/test64.in	2013-06-08 18:19:40.000000000 +0200
Karsten Hopp a2374c
--- src/testdir/test64.in	2013-06-10 16:19:49.000000000 +0200
Karsten Hopp a2374c
***************
Karsten Hopp a2374c
*** 392,397 ****
Karsten Hopp a2374c
--- 392,398 ----
Karsten Hopp a2374c
  :call add(tl, [2, '\v\C%(
Karsten Hopp a2374c
  :call add(tl, [2, '\(foo\)\@<=\>', 'foobar'])
Karsten Hopp a2374c
  :call add(tl, [2, '\(foo\)\@<=\>', 'barfoo', '', 'foo'])
Karsten Hopp a2374c
+ :call add(tl, [2, '\(foo\)\@<=.*', 'foobar', 'bar', 'foo'])
Karsten Hopp a2374c
  :"
Karsten Hopp a2374c
  :""""" \@>
Karsten Hopp a2374c
  :call add(tl, [2, '\(a*\)\@>a', 'aaaa'])
Karsten Hopp a2374c
*** ../vim-7.3.1156/src/testdir/test64.ok	2013-06-08 18:19:40.000000000 +0200
Karsten Hopp a2374c
--- src/testdir/test64.ok	2013-06-10 16:21:07.000000000 +0200
Karsten Hopp a2374c
***************
Karsten Hopp a2374c
*** 890,895 ****
Karsten Hopp a2374c
--- 890,898 ----
Karsten Hopp a2374c
  OK 0 - \(foo\)\@<=\>
Karsten Hopp a2374c
  OK 1 - \(foo\)\@<=\>
Karsten Hopp a2374c
  OK 2 - \(foo\)\@<=\>
Karsten Hopp a2374c
+ OK 0 - \(foo\)\@<=.*
Karsten Hopp a2374c
+ OK 1 - \(foo\)\@<=.*
Karsten Hopp a2374c
+ OK 2 - \(foo\)\@<=.*
Karsten Hopp a2374c
  OK 0 - \(a*\)\@>a
Karsten Hopp a2374c
  OK 1 - \(a*\)\@>a
Karsten Hopp a2374c
  OK 2 - \(a*\)\@>a
Karsten Hopp a2374c
*** ../vim-7.3.1156/src/version.c	2013-06-09 20:50:56.000000000 +0200
Karsten Hopp a2374c
--- src/version.c	2013-06-10 16:23:57.000000000 +0200
Karsten Hopp a2374c
***************
Karsten Hopp a2374c
*** 730,731 ****
Karsten Hopp a2374c
--- 730,733 ----
Karsten Hopp a2374c
  {   /* Add new patch number below this line */
Karsten Hopp a2374c
+ /**/
Karsten Hopp a2374c
+     1157,
Karsten Hopp a2374c
  /**/
Karsten Hopp a2374c
Karsten Hopp a2374c
-- 
Karsten Hopp a2374c
In a world without fences, who needs Gates and Windows?
Karsten Hopp a2374c
Karsten Hopp a2374c
 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
Karsten Hopp a2374c
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
Karsten Hopp a2374c
\\\  an exciting new programming language -- http://www.Zimbu.org        ///
Karsten Hopp a2374c
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///