changeset 4805:66803af09906 v7.3.1149

updated for version 7.3.1149 Problem: New regexp engine: Matching plain text could be faster. Solution: Detect a plain text match and handle it specifically. Add vim_regfree().
author Bram Moolenaar <bram@vim.org>
date Sat, 08 Jun 2013 18:19:48 +0200
parents 62663cfeb216
children 33a813d17d75
files src/buffer.c src/edit.c src/eval.c src/ex_cmds.c src/ex_cmds2.c src/ex_docmd.c src/ex_eval.c src/ex_getln.c src/fileio.c src/gui.c src/macros.h src/misc1.c src/misc2.c src/option.c src/proto/regexp.pro src/quickfix.c src/regexp.c src/regexp.h src/regexp_nfa.c src/screen.c src/search.c src/spell.c src/syntax.c src/tag.c src/testdir/test64.in src/testdir/test64.ok src/version.c src/window.c
diffstat 28 files changed, 283 insertions(+), 81 deletions(-) [+]
line wrap: on
line diff
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -1898,7 +1898,7 @@ free_buf_options(buf, free_p_ff)
 #ifdef FEAT_SPELL
     clear_string_option(&buf->b_s.b_p_spc);
     clear_string_option(&buf->b_s.b_p_spf);
-    vim_free(buf->b_s.b_cap_prog);
+    vim_regfree(buf->b_s.b_cap_prog);
     buf->b_s.b_cap_prog = NULL;
     clear_string_option(&buf->b_s.b_p_spl);
 #endif
@@ -2246,7 +2246,7 @@ buflist_findpat(pattern, pattern_end, un
 			match = buf->b_fnum;	/* remember first match */
 		    }
 
-		vim_free(prog);
+		vim_regfree(prog);
 		if (match >= 0)			/* found one match */
 		    break;
 	    }
@@ -2355,14 +2355,14 @@ ExpandBufnames(pat, num_file, file, opti
 		*file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
 		if (*file == NULL)
 		{
-		    vim_free(prog);
+		    vim_regfree(prog);
 		    if (patc != pat)
 			vim_free(patc);
 		    return FAIL;
 		}
 	    }
 	}
-	vim_free(prog);
+	vim_regfree(prog);
 	if (count)		/* match(es) found, break here */
 	    break;
     }
--- a/src/edit.c
+++ b/src/edit.c
@@ -3134,7 +3134,7 @@ ins_compl_dictionaries(dict_start, pat, 
 
 theend:
     p_scs = save_p_scs;
-    vim_free(regmatch.regprog);
+    vim_regfree(regmatch.regprog);
     vim_free(buf);
 }
 
--- a/src/eval.c
+++ b/src/eval.c
@@ -4560,7 +4560,7 @@ eval4(arg, rettv, evaluate)
 			    if (regmatch.regprog != NULL)
 			    {
 				n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
-				vim_free(regmatch.regprog);
+				vim_regfree(regmatch.regprog);
 				if (type == TYPE_NOMATCH)
 				    n1 = !n1;
 			    }
@@ -13981,7 +13981,7 @@ find_some_match(argvars, rettv, type)
 		rettv->vval.v_number += (varnumber_T)(str - expr);
 	    }
 	}
-	vim_free(regmatch.regprog);
+	vim_regfree(regmatch.regprog);
     }
 
 theend:
@@ -17214,7 +17214,7 @@ f_split(argvars, rettv)
 	    str = regmatch.endp[0];
 	}
 
-	vim_free(regmatch.regprog);
+	vim_regfree(regmatch.regprog);
     }
 
     p_cpo = save_cpo;
@@ -21066,7 +21066,7 @@ ex_function(eap)
 			    list_func_head(fp, FALSE);
 		    }
 		}
-		vim_free(regmatch.regprog);
+		vim_regfree(regmatch.regprog);
 	    }
 	}
 	if (*p == '/')
@@ -24220,7 +24220,7 @@ do_string_sub(str, pat, sub, flags)
 	if (ga.ga_data != NULL)
 	    STRCPY((char *)ga.ga_data + ga.ga_len, tail);
 
-	vim_free(regmatch.regprog);
+	vim_regfree(regmatch.regprog);
     }
 
     ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
--- a/src/ex_cmds.c
+++ b/src/ex_cmds.c
@@ -571,7 +571,7 @@ sortend:
     vim_free(nrs);
     vim_free(sortbuf1);
     vim_free(sortbuf2);
-    vim_free(regmatch.regprog);
+    vim_regfree(regmatch.regprog);
     if (got_int)
 	EMSG(_(e_interr));
 }
@@ -5261,7 +5261,7 @@ outofmem:
 	changed_window_setting();
 #endif
 
-    vim_free(regmatch.regprog);
+    vim_regfree(regmatch.regprog);
 }
 
 /*
@@ -5436,7 +5436,7 @@ ex_global(eap)
 	global_exe(cmd);
 
     ml_clearmarked();	   /* clear rest of the marks */
-    vim_free(regmatch.regprog);
+    vim_regfree(regmatch.regprog);
 }
 
 /*
--- a/src/ex_cmds2.c
+++ b/src/ex_cmds2.c
@@ -652,7 +652,7 @@ ex_breakdel(eap)
 	while (gap->ga_len > 0)
 	{
 	    vim_free(DEBUGGY(gap, todel).dbg_name);
-	    vim_free(DEBUGGY(gap, todel).dbg_prog);
+	    vim_regfree(DEBUGGY(gap, todel).dbg_prog);
 	    --gap->ga_len;
 	    if (todel < gap->ga_len)
 		mch_memmove(&DEBUGGY(gap, todel), &DEBUGGY(gap, todel + 1),
@@ -1985,7 +1985,7 @@ do_arglist(str, what, after)
 		    --match;
 		}
 
-	    vim_free(regmatch.regprog);
+	    vim_regfree(regmatch.regprog);
 	    vim_free(p);
 	    if (!didone)
 		EMSG2(_(e_nomatch2), ((char_u **)new_ga.ga_data)[i]);
--- a/src/ex_docmd.c
+++ b/src/ex_docmd.c
@@ -7779,7 +7779,7 @@ ex_open(eap)
 		curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
 	    else
 		EMSG(_(e_nomatch));
-	    vim_free(regmatch.regprog);
+	    vim_regfree(regmatch.regprog);
 	}
 	/* Move to the NUL, ignore any other arguments. */
 	eap->arg += STRLEN(eap->arg);
--- a/src/ex_eval.c
+++ b/src/ex_eval.c
@@ -1576,7 +1576,7 @@ ex_catch(eap)
 		    caught = vim_regexec_nl(&regmatch, current_exception->value,
 			    (colnr_T)0);
 		    got_int |= prev_got_int;
-		    vim_free(regmatch.regprog);
+		    vim_regfree(regmatch.regprog);
 		}
 	    }
 	}
--- a/src/ex_getln.c
+++ b/src/ex_getln.c
@@ -4717,7 +4717,7 @@ ExpandFromContext(xp, pat, num_file, fil
 	    }
     }
 
-    vim_free(regmatch.regprog);
+    vim_regfree(regmatch.regprog);
 
     return ret;
 #endif /* FEAT_CMDL_COMPL */
@@ -5785,7 +5785,7 @@ del_history_entry(histype, str)
 	if (history[histype][idx].hisstr == NULL)
 	    hisidx[histype] = -1;
     }
-    vim_free(regmatch.regprog);
+    vim_regfree(regmatch.regprog);
     return found;
 }
 
--- a/src/fileio.c
+++ b/src/fileio.c
@@ -7921,7 +7921,7 @@ au_cleanup()
 	    if (ap->pat == NULL)
 	    {
 		*prev_ap = ap->next;
-		vim_free(ap->reg_prog);
+		vim_regfree(ap->reg_prog);
 		vim_free(ap);
 	    }
 	    else
@@ -10070,7 +10070,7 @@ match_file_pat(pattern, prog, fname, sfn
 	result = TRUE;
 
     if (prog == NULL)
-	vim_free(regmatch.regprog);
+	vim_regfree(regmatch.regprog);
     return result;
 }
 #endif
--- a/src/gui.c
+++ b/src/gui.c
@@ -5319,7 +5319,7 @@ gui_do_findrepl(flags, find_text, repl_t
 	    }
 	    else
 		MSG(_("No match at cursor, finding next"));
-	    vim_free(regmatch.regprog);
+	    vim_regfree(regmatch.regprog);
 	}
     }
 
--- a/src/macros.h
+++ b/src/macros.h
@@ -272,6 +272,7 @@
 
 # define MB_COPY_CHAR(f, t) if (has_mbyte) mb_copy_char(&f, &t); else *t++ = *f++
 # define MB_CHARLEN(p)	    (has_mbyte ? mb_charlen(p) : (int)STRLEN(p))
+# define MB_CHAR2LEN(c)	    (has_mbyte ? mb_char2len(c) : 1)
 # define PTR2CHAR(p)	    (has_mbyte ? mb_ptr2char(p) : (int)*(p))
 #else
 # define MB_PTR2LEN(p)		1
@@ -280,6 +281,7 @@
 # define mb_ptr_back(s, p)	--p
 # define MB_COPY_CHAR(f, t)	*t++ = *f++
 # define MB_CHARLEN(p)		STRLEN(p)
+# define MB_CHAR2LEN(c)		1
 # define PTR2CHAR(p)		((int)*(p))
 #endif
 
--- a/src/misc1.c
+++ b/src/misc1.c
@@ -456,8 +456,8 @@ get_number_indent(lnum)
 	    pos.coladd = 0;
 #endif
 	}
-    }
-    vim_free(regmatch.regprog);
+	vim_regfree(regmatch.regprog);
+    }
 
     if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL)
 	return -1;
@@ -9751,7 +9751,7 @@ dos_expandpath(
 # endif
 #endif
     vim_free(buf);
-    vim_free(regmatch.regprog);
+    vim_regfree(regmatch.regprog);
     vim_free(matchname);
 
     matches = gap->ga_len - start_len;
@@ -9993,7 +9993,7 @@ unix_expandpath(gap, path, wildoff, flag
     }
 
     vim_free(buf);
-    vim_free(regmatch.regprog);
+    vim_regfree(regmatch.regprog);
 
     matches = gap->ga_len - start_len;
     if (matches > 0)
@@ -10358,7 +10358,7 @@ theend:
 	vim_free(in_curdir);
     }
     ga_clear_strings(&path_ga);
-    vim_free(regmatch.regprog);
+    vim_regfree(regmatch.regprog);
 
     if (sort_again)
 	remove_duplicates(gap);
--- a/src/misc2.c
+++ b/src/misc2.c
@@ -1134,7 +1134,7 @@ free_all_mem()
     /* Free some global vars. */
     vim_free(username);
 # ifdef FEAT_CLIPBOARD
-    vim_free(clip_exclude_prog);
+    vim_regfree(clip_exclude_prog);
 # endif
     vim_free(last_cmdline);
 # ifdef FEAT_CMDHIST
@@ -5008,8 +5008,8 @@ vim_findfile(search_ctx_arg)
 #endif
 		{
 		    /*
-		     * we don't have further wildcards to expand, so we have to
-		     * check for the final file now
+		     * We don't have further wildcards to expand, so we have to
+		     * check for the final file now.
 		     */
 		    for (i = stackp->ffs_filearray_cur;
 					  i < stackp->ffs_filearray_size; ++i)
--- a/src/option.c
+++ b/src/option.c
@@ -7491,7 +7491,7 @@ check_clipboard_option()
 	clip_autoselect_plus = new_autoselect_plus;
 	clip_autoselectml = new_autoselectml;
 	clip_html = new_html;
-	vim_free(clip_exclude_prog);
+	vim_regfree(clip_exclude_prog);
 	clip_exclude_prog = new_exclude_prog;
 #ifdef FEAT_GUI_GTK
 	if (gui.in_use)
@@ -7502,7 +7502,7 @@ check_clipboard_option()
 #endif
     }
     else
-	vim_free(new_exclude_prog);
+	vim_regfree(new_exclude_prog);
 
     return errmsg;
 }
@@ -7529,16 +7529,16 @@ compile_cap_prog(synblock)
 	if (re != NULL)
 	{
 	    synblock->b_cap_prog = vim_regcomp(re, RE_MAGIC);
+	    vim_free(re);
 	    if (synblock->b_cap_prog == NULL)
 	    {
 		synblock->b_cap_prog = rp; /* restore the previous program */
 		return e_invarg;
 	    }
-	    vim_free(re);
-	}
-    }
-
-    vim_free(rp);
+	}
+    }
+
+    vim_regfree(rp);
     return NULL;
 }
 #endif
--- a/src/proto/regexp.pro
+++ b/src/proto/regexp.pro
@@ -2,16 +2,17 @@
 int re_multiline __ARGS((regprog_T *prog));
 int re_lookbehind __ARGS((regprog_T *prog));
 char_u *skip_regexp __ARGS((char_u *startp, int dirc, int magic, char_u **newp));
-regprog_T *vim_regcomp __ARGS((char_u *expr, int re_flags));
 int vim_regcomp_had_eol __ARGS((void));
 void free_regexp_stuff __ARGS((void));
-int vim_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
-int vim_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
-long vim_regexec_multi __ARGS((regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm));
 reg_extmatch_T *ref_extmatch __ARGS((reg_extmatch_T *em));
 void unref_extmatch __ARGS((reg_extmatch_T *em));
 char_u *regtilde __ARGS((char_u *source, int magic));
 int vim_regsub __ARGS((regmatch_T *rmp, char_u *source, char_u *dest, int copy, int magic, int backslash));
 int vim_regsub_multi __ARGS((regmmatch_T *rmp, linenr_T lnum, char_u *source, char_u *dest, int copy, int magic, int backslash));
 char_u *reg_submatch __ARGS((int no));
+regprog_T *vim_regcomp __ARGS((char_u *expr_arg, int re_flags));
+void vim_regfree __ARGS((regprog_T *prog));
+int vim_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
+int vim_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
+long vim_regexec_multi __ARGS((regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm));
 /* vim: set ft=c : */
--- a/src/quickfix.c
+++ b/src/quickfix.c
@@ -863,7 +863,7 @@ qf_init_ok:
     for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_first)
     {
 	fmt_first = fmt_ptr->next;
-	vim_free(fmt_ptr->prog);
+	vim_regfree(fmt_ptr->prog);
 	vim_free(fmt_ptr);
     }
     qf_clean_dir_stack(&dir_stack);
@@ -3487,7 +3487,7 @@ theend:
     vim_free(dirname_now);
     vim_free(dirname_start);
     vim_free(target_dir);
-    vim_free(regmatch.regprog);
+    vim_regfree(regmatch.regprog);
 }
 
 /*
@@ -4178,7 +4178,7 @@ ex_helpgrep(eap)
 	    }
 	}
 
-	vim_free(regmatch.regprog);
+	vim_regfree(regmatch.regprog);
 #ifdef FEAT_MBYTE
 	if (vc.vc_type != CONV_NONE)
 	    convert_setup(&vc, NULL, NULL);
--- a/src/regexp.c
+++ b/src/regexp.c
@@ -1297,7 +1297,8 @@ skip_regexp(startp, dirc, magic, newp)
     return p;
 }
 
-static regprog_T    *bt_regcomp __ARGS((char_u *expr, int re_flags));
+static regprog_T  *bt_regcomp __ARGS((char_u *expr, int re_flags));
+static void bt_regfree __ARGS((regprog_T *prog));
 
 /*
  * bt_regcomp() - compile a regular expression into internal code for the
@@ -1455,6 +1456,16 @@ bt_regcomp(expr, re_flags)
 }
 
 /*
+ * Free a compiled regexp program, returned by bt_regcomp().
+ */
+    static void
+bt_regfree(prog)
+    regprog_T   *prog;
+{
+    vim_free(prog);
+}
+
+/*
  * Setup to parse the regexp.  Used once to get the length and once to do it.
  */
     static void
@@ -7876,6 +7887,7 @@ reg_submatch(no)
 static regengine_T bt_regengine =
 {
     bt_regcomp,
+    bt_regfree,
     bt_regexec,
 #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
 	|| defined(FIND_REPLACE_DIALOG) || defined(PROTO)
@@ -7893,6 +7905,7 @@ static regengine_T bt_regengine =
 static regengine_T nfa_regengine =
 {
     nfa_regcomp,
+    nfa_regfree,
     nfa_regexec,
 #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
 	|| defined(FIND_REPLACE_DIALOG) || defined(PROTO)
@@ -7920,7 +7933,9 @@ static char_u regname[][30] = {
 
 /*
  * Compile a regular expression into internal code.
- * Returns the program in allocated memory.  Returns NULL for an error.
+ * Returns the program in allocated memory.
+ * Use vim_regfree() to free the memory.
+ * Returns NULL for an error.
  */
     regprog_T *
 vim_regcomp(expr_arg, re_flags)
@@ -7997,6 +8012,17 @@ vim_regcomp(expr_arg, re_flags)
 }
 
 /*
+ * Free a compiled regexp program, returned by vim_regcomp().
+ */
+    void
+vim_regfree(prog)
+    regprog_T   *prog;
+{
+    if (prog != NULL)
+	prog->engine->regfree(prog);
+}
+
+/*
  * Match a regexp against a string.
  * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
  * Uses curbuf for line count and 'iskeyword'.
--- a/src/regexp.h
+++ b/src/regexp.h
@@ -89,6 +89,7 @@ typedef struct
 
     int			reganch;	/* pattern starts with ^ */
     int			regstart;	/* char at start of pattern */
+    char_u		*match_text;	/* plain text to match with */
 
     int			has_zend;	/* pattern contains \ze */
     int			has_backref;	/* pattern contains \1 .. \9 */
@@ -147,6 +148,7 @@ typedef struct
 struct regengine
 {
     regprog_T	*(*regcomp)(char_u*, int);
+    void	(*regfree)(regprog_T *);
     int		(*regexec)(regmatch_T*, char_u*, colnr_T);
 #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
 	|| defined(FIND_REPLACE_DIALOG) || defined(PROTO)
--- a/src/regexp_nfa.c
+++ b/src/regexp_nfa.c
@@ -270,6 +270,7 @@ static int nfa_ll_index = 0;
 static int nfa_regcomp_start __ARGS((char_u *expr, int re_flags));
 static int nfa_get_reganch __ARGS((nfa_state_T *start, int depth));
 static int nfa_get_regstart __ARGS((nfa_state_T *start, int depth));
+static char_u *nfa_get_match_text __ARGS((nfa_state_T *start));
 static int nfa_recognize_char_class __ARGS((char_u *start, char_u *end, int extra_newl));
 static int nfa_emit_equi_class __ARGS((int c));
 static int nfa_regatom __ARGS((void));
@@ -295,6 +296,7 @@ static int nfa_re_num_cmp __ARGS((long_u
 static long nfa_regtry __ARGS((nfa_regprog_T *prog, colnr_T col));
 static long nfa_regexec_both __ARGS((char_u *line, colnr_T col));
 static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags));
+static void nfa_regfree __ARGS((regprog_T *prog));
 static int nfa_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
 static long nfa_regexec_multi __ARGS((regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm));
 
@@ -493,6 +495,52 @@ nfa_get_regstart(start, depth)
 }
 
 /*
+ * Figure out if the NFA state list contains just literal text and nothing
+ * else.  If so return a string with what must match after regstart.
+ * Otherwise return NULL.
+ */
+    static char_u *
+nfa_get_match_text(start)
+    nfa_state_T *start;
+{
+    nfa_state_T *p = start;
+    int		len = 0;
+    char_u	*ret;
+    char_u	*s;
+
+    if (p->c != NFA_MOPEN)
+	return NULL; /* just in case */
+    p = p->out;
+    while (p->c > 0)
+    {
+	len += MB_CHAR2LEN(p->c);
+	p = p->out;
+    }
+    if (p->c != NFA_MCLOSE || p->out->c != NFA_MATCH)
+	return NULL;
+
+    ret = alloc(len);
+    if (ret != NULL)
+    {
+	len = 0;
+	p = start->out->out; /* skip first char, it goes into regstart */
+	s = ret;
+	while (p->c > 0)
+	{
+#ifdef FEAT_MBYTE
+	    if (has_mbyte)
+		s += (*mb_char2bytes)(p->c, s);
+	    else
+#endif
+		*s++ = p->c;
+	    p = p->out;
+	}
+	*s = NUL;
+    }
+    return ret;
+}
+
+/*
  * Allocate more space for post_start.  Called when
  * running above the estimated number of states.
  */
@@ -2280,8 +2328,13 @@ nfa_dump(prog)
     {
 	nfa_print_state(debugf, prog->start);
 
-	fprintf(debugf, "reganch: %d\n", prog->reganch);
-	fprintf(debugf, "regstart: %d\n", prog->regstart);
+	if (prog->reganch)
+	    fprintf(debugf, "reganch: %d\n", prog->reganch);
+	if (prog->regstart != NUL)
+	    fprintf(debugf, "regstart: %c (decimal: %d)\n",
+					      prog->regstart, prog->regstart);
+	if (prog->match_text != NULL)
+	    fprintf(debugf, "match_text: \"%s\"\n", prog->match_text);
 
 	fclose(debugf);
     }
@@ -4154,6 +4207,7 @@ recursive_regmatch(state, prog, submatch
 
 static int failure_chance __ARGS((nfa_state_T *state, int depth));
 static int skip_to_start __ARGS((int c, colnr_T *colp));
+static long find_match_text __ARGS((colnr_T startcol, int regstart, char_u *match_text));
 
 /*
  * Estimate the chance of a match with "state" failing.
@@ -4331,6 +4385,69 @@ skip_to_start(c, colp)
 }
 
 /*
+ * Check for a match with match_text.
+ * Called after skip_to_start() has find regstart.
+ * Returns zero for no match, 1 for a match.
+ */
+    static long
+find_match_text(startcol, regstart, match_text)
+    colnr_T startcol;
+    int	    regstart;
+    char_u  *match_text;
+{
+    colnr_T col = startcol;
+    int	    c1, c2;
+    int	    len1, len2;
+    int	    match;
+
+    for (;;)
+    {
+	match = TRUE;
+	len2 = MB_CHAR2LEN(regstart); /* skip regstart */
+	for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1))
+	{
+	    c1 = PTR2CHAR(match_text + len1);
+	    c2 = PTR2CHAR(regline + col + len2);
+	    if (c1 != c2 && (!ireg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2)))
+	    {
+		match = FALSE;
+		break;
+	    }
+	    len2 += MB_CHAR2LEN(c2);
+	}
+	if (match
+#ifdef FEAT_MBYTE
+		/* check that no composing char follows */
+		&& !(enc_utf8
+			   && utf_iscomposing(PTR2CHAR(regline + col + len2)))
+#endif
+		)
+	{
+	    cleanup_subexpr();
+	    if (REG_MULTI)
+	    {
+		reg_startpos[0].lnum = reglnum;
+		reg_startpos[0].col = col;
+		reg_endpos[0].lnum = reglnum;
+		reg_endpos[0].col = col + len2;
+	    }
+	    else
+	    {
+		reg_startp[0] = regline + col;
+		reg_endp[0] = regline + col + len2;
+	    }
+	    return 1L;
+	}
+
+	/* Try finding regstart after the current match. */
+	col += MB_CHAR2LEN(regstart); /* skip regstart */
+	if (skip_to_start(regstart, &col) == FAIL)
+	    break;
+    }
+    return 0L;
+}
+
+/*
  * Main matching routine.
  *
  * Run NFA to determine whether it matches reginput.
@@ -5584,17 +5701,6 @@ nfa_regtry(prog, col)
 #endif
 
     reginput = regline + col;
-    need_clear_subexpr = TRUE;
-#ifdef FEAT_SYN_HL
-    /* Clear the external match subpointers if necessary. */
-    if (prog->reghasz == REX_SET)
-    {
-	nfa_has_zsubexpr = TRUE;
-	need_clear_zsubexpr = TRUE;
-    }
-    else
-	nfa_has_zsubexpr = FALSE;
-#endif
 
 #ifdef ENABLE_LOG
     f = fopen(NFA_REGEXP_RUN_LOG, "a");
@@ -5764,12 +5870,31 @@ nfa_regexec_both(line, startcol)
     if (prog->reganch && col > 0)
 	return 0L;
 
+    need_clear_subexpr = TRUE;
+#ifdef FEAT_SYN_HL
+    /* Clear the external match subpointers if necessary. */
+    if (prog->reghasz == REX_SET)
+    {
+	nfa_has_zsubexpr = TRUE;
+	need_clear_zsubexpr = TRUE;
+    }
+    else
+	nfa_has_zsubexpr = FALSE;
+#endif
+
     if (prog->regstart != NUL)
+    {
 	/* Skip ahead until a character we know the match must start with.
 	 * When there is none there is no match. */
 	if (skip_to_start(prog->regstart, &col) == FAIL)
 	    return 0L;
 
+	/* If match_text is set it contains the full text that must match.
+	 * Nothing else to try. Doesn't handle combining chars well. */
+	if (prog->match_text != NULL && !ireg_icombine)
+	    return find_match_text(col, prog->regstart, prog->match_text);
+    }
+
     /* If the start column is past the maximum column: no need to try. */
     if (ireg_maxcol > 0 && col >= ireg_maxcol)
 	goto theend;
@@ -5876,6 +6001,8 @@ nfa_regcomp(expr, re_flags)
     prog->reganch = nfa_get_reganch(prog->start, 0);
     prog->regstart = nfa_get_regstart(prog->start, 0);
 
+    prog->match_text = nfa_get_match_text(prog->start);
+
 #ifdef ENABLE_LOG
     nfa_postfix_dump(expr, OK);
     nfa_dump(prog);
@@ -5885,7 +6012,7 @@ nfa_regcomp(expr, re_flags)
     prog->reghasz = re_has_z;
 #endif
 #ifdef DEBUG
-    prog->pattern = vim_strsave(expr); /* memory will leak */
+    prog->pattern = vim_strsave(expr);
     nfa_regengine.expr = NULL;
 #endif
 
@@ -5907,6 +6034,22 @@ fail:
     goto out;
 }
 
+/*
+ * Free a compiled regexp program, returned by nfa_regcomp().
+ */
+    static void
+nfa_regfree(prog)
+    regprog_T   *prog;
+{
+    if (prog != NULL)
+    {
+	vim_free(((nfa_regprog_T *)prog)->match_text);
+#ifdef DEBUG
+	vim_free(((nfa_regprog_T *)prog)->pattern);
+#endif
+	vim_free(prog);
+    }
+}
 
 /*
  * Match a regexp against a string.
--- a/src/screen.c
+++ b/src/screen.c
@@ -7082,7 +7082,7 @@ end_search_hl()
 {
     if (search_hl.rm.regprog != NULL)
     {
-	vim_free(search_hl.rm.regprog);
+	vim_regfree(search_hl.rm.regprog);
 	search_hl.rm.regprog = NULL;
     }
 }
@@ -7284,7 +7284,7 @@ next_search_hl(win, shl, lnum, mincol)
 	    if (shl == &search_hl)
 	    {
 		/* don't free regprog in the match list, it's a copy */
-		vim_free(shl->rm.regprog);
+		vim_regfree(shl->rm.regprog);
 		no_hlsearch = TRUE;
 	    }
 	    shl->rm.regprog = NULL;
--- a/src/search.c
+++ b/src/search.c
@@ -972,7 +972,7 @@ searchit(win, buf, pos, dir, pat, count,
     }
     while (--count > 0 && found);   /* stop after count matches or no match */
 
-    vim_free(regmatch.regprog);
+    vim_regfree(regmatch.regprog);
 
     called_emsg |= save_called_emsg;
 
@@ -4680,7 +4680,7 @@ is_zerowidth(pattern)
     }
 
     called_emsg |= save_called_emsg;
-    vim_free(regmatch.regprog);
+    vim_regfree(regmatch.regprog);
     return result;
 }
 #endif /* FEAT_VISUAL */
@@ -5402,9 +5402,9 @@ exit_matched:
 
 fpip_end:
     vim_free(file_line);
-    vim_free(regmatch.regprog);
-    vim_free(incl_regmatch.regprog);
-    vim_free(def_regmatch.regprog);
+    vim_regfree(regmatch.regprog);
+    vim_regfree(incl_regmatch.regprog);
+    vim_regfree(def_regmatch.regprog);
 }
 
     static void
--- a/src/spell.c
+++ b/src/spell.c
@@ -2658,7 +2658,7 @@ slang_clear(lp)
     ga_clear(gap);
 
     for (i = 0; i < lp->sl_prefixcnt; ++i)
-	vim_free(lp->sl_prefprog[i]);
+	vim_regfree(lp->sl_prefprog[i]);
     lp->sl_prefixcnt = 0;
     vim_free(lp->sl_prefprog);
     lp->sl_prefprog = NULL;
@@ -2669,7 +2669,7 @@ slang_clear(lp)
     vim_free(lp->sl_midword);
     lp->sl_midword = NULL;
 
-    vim_free(lp->sl_compprog);
+    vim_regfree(lp->sl_compprog);
     vim_free(lp->sl_comprules);
     vim_free(lp->sl_compstartflags);
     vim_free(lp->sl_compallflags);
@@ -5802,7 +5802,7 @@ spell_read_aff(spin, fname)
 					{
 					    sprintf((char *)buf, "^%s",
 							  aff_entry->ae_cond);
-					    vim_free(aff_entry->ae_prog);
+					    vim_regfree(aff_entry->ae_prog);
 					    aff_entry->ae_prog = vim_regcomp(
 						    buf, RE_MAGIC + RE_STRING);
 					}
@@ -6507,7 +6507,7 @@ spell_free_aff(aff)
 		--todo;
 		ah = HI2AH(hi);
 		for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
-		    vim_free(ae->ae_prog);
+		    vim_regfree(ae->ae_prog);
 	    }
 	}
 	if (ht == &aff->af_suff)
--- a/src/syntax.c
+++ b/src/syntax.c
@@ -3495,7 +3495,7 @@ syntax_clear(block)
     block->b_syn_sync_maxlines = 0;
     block->b_syn_sync_linebreaks = 0;
 
-    vim_free(block->b_syn_linecont_prog);
+    vim_regfree(block->b_syn_linecont_prog);
     block->b_syn_linecont_prog = NULL;
     vim_free(block->b_syn_linecont_pat);
     block->b_syn_linecont_pat = NULL;
@@ -3544,7 +3544,7 @@ syntax_sync_clear()
     curwin->w_s->b_syn_sync_maxlines = 0;
     curwin->w_s->b_syn_sync_linebreaks = 0;
 
-    vim_free(curwin->w_s->b_syn_linecont_prog);
+    vim_regfree(curwin->w_s->b_syn_linecont_prog);
     curwin->w_s->b_syn_linecont_prog = NULL;
     vim_free(curwin->w_s->b_syn_linecont_pat);
     curwin->w_s->b_syn_linecont_pat = NULL;
@@ -3583,7 +3583,7 @@ syn_clear_pattern(block, i)
     int		i;
 {
     vim_free(SYN_ITEMS(block)[i].sp_pattern);
-    vim_free(SYN_ITEMS(block)[i].sp_prog);
+    vim_regfree(SYN_ITEMS(block)[i].sp_prog);
     /* Only free sp_cont_list and sp_next_list of first start pattern */
     if (i == 0 || SYN_ITEMS(block)[i - 1].sp_type != SPTYPE_START)
     {
@@ -4991,7 +4991,7 @@ syn_cmd_match(eap, syncing)
     /*
      * Something failed, free the allocated memory.
      */
-    vim_free(item.sp_prog);
+    vim_regfree(item.sp_prog);
     vim_free(item.sp_pattern);
     vim_free(syn_opt_arg.cont_list);
     vim_free(syn_opt_arg.cont_in_list);
@@ -5248,7 +5248,7 @@ syn_cmd_region(eap, syncing)
 	{
 	    if (!success)
 	    {
-		vim_free(ppp->pp_synp->sp_prog);
+		vim_regfree(ppp->pp_synp->sp_prog);
 		vim_free(ppp->pp_synp->sp_pattern);
 	    }
 	    vim_free(ppp->pp_synp);
@@ -6022,7 +6022,7 @@ get_id_list(arg, keylen, list)
 			    id = -1;	    /* remember that we found one */
 			}
 		    }
-		    vim_free(regmatch.regprog);
+		    vim_regfree(regmatch.regprog);
 		}
 	    }
 	    vim_free(name);
@@ -6295,7 +6295,7 @@ ex_ownsyntax(eap)
 	curwin->w_p_spell = FALSE;	/* No spell checking */
 	clear_string_option(&curwin->w_s->b_p_spc);
 	clear_string_option(&curwin->w_s->b_p_spf);
-	vim_free(curwin->w_s->b_cap_prog);
+	vim_regfree(curwin->w_s->b_cap_prog);
 	curwin->w_s->b_cap_prog = NULL;
 	clear_string_option(&curwin->w_s->b_p_spl);
 #endif
--- a/src/tag.c
+++ b/src/tag.c
@@ -2491,7 +2491,7 @@ line_read_in:
 
 findtag_end:
     vim_free(lbuf);
-    vim_free(orgpat.regmatch.regprog);
+    vim_regfree(orgpat.regmatch.regprog);
     vim_free(tag_fname);
 #ifdef FEAT_EMACS_TAGS
     vim_free(ebuf);
--- a/src/testdir/test64.in
+++ b/src/testdir/test64.in
@@ -260,6 +260,8 @@ STARTTEST
 :call add(tl, [2, '[^[:alpha:]]\+','abcccadfoij7787ysf287yrnccdu','7787'])
 :call add(tl, [2, '[-a]', '-', '-'])
 :call add(tl, [2, '[a-]', '-', '-'])
+:call add(tl, [2, '[a-f]*\c','ABCDEFGH','ABCDEF'])
+:call add(tl, [2, '[abc][xyz]\c','-af-AF-BY--','BY'])
 :" filename regexp
 :call add(tl, [2, '[-./[:alnum:]_~]\+', 'log13.file', 'log13.file'])
 :" special chars
@@ -385,6 +387,12 @@ STARTTEST
 :call add(tl, [2, '\(<<\)\@2<=span.', 'xxspanxxxx<spanxx<<spanyyy', 'spany', '<<'])
 :call add(tl, [2, '\(foo\)\@<!bar.', 'xx foobar1 xbar2 xx', 'bar2'])
 :"
+:" look-behind match in front of a zero-width item
+:call add(tl, [2, '\v\C%(<Last Changed:\s+)@<=.*$', '" test header'])
+:call add(tl, [2, '\v\C%(<Last Changed:\s+)@<=.*$', '" Last Changed: 1970', '1970'])
+:call add(tl, [2, '\(foo\)\@<=\>', 'foobar'])
+:call add(tl, [2, '\(foo\)\@<=\>', 'barfoo', '', 'foo'])
+:"
 :""""" \@>
 :call add(tl, [2, '\(a*\)\@>a', 'aaaa'])
 :call add(tl, [2, '\(a*\)\@>b', 'aaab', 'aaab', 'aaa'])
--- a/src/testdir/test64.ok
+++ b/src/testdir/test64.ok
@@ -584,6 +584,12 @@ OK 2 - [-a]
 OK 0 - [a-]
 OK 1 - [a-]
 OK 2 - [a-]
+OK 0 - [a-f]*\c
+OK 1 - [a-f]*\c
+OK 2 - [a-f]*\c
+OK 0 - [abc][xyz]\c
+OK 1 - [abc][xyz]\c
+OK 2 - [abc][xyz]\c
 OK 0 - [-./[:alnum:]_~]\+
 OK 1 - [-./[:alnum:]_~]\+
 OK 2 - [-./[:alnum:]_~]\+
@@ -872,6 +878,18 @@ OK 2 - \(<<\)\@2<=span.
 OK 0 - \(foo\)\@<!bar.
 OK 1 - \(foo\)\@<!bar.
 OK 2 - \(foo\)\@<!bar.
+OK 0 - \v\C%(<Last Changed:\s+)@<=.*$
+OK 1 - \v\C%(<Last Changed:\s+)@<=.*$
+OK 2 - \v\C%(<Last Changed:\s+)@<=.*$
+OK 0 - \v\C%(<Last Changed:\s+)@<=.*$
+OK 1 - \v\C%(<Last Changed:\s+)@<=.*$
+OK 2 - \v\C%(<Last Changed:\s+)@<=.*$
+OK 0 - \(foo\)\@<=\>
+OK 1 - \(foo\)\@<=\>
+OK 2 - \(foo\)\@<=\>
+OK 0 - \(foo\)\@<=\>
+OK 1 - \(foo\)\@<=\>
+OK 2 - \(foo\)\@<=\>
 OK 0 - \(a*\)\@>a
 OK 1 - \(a*\)\@>a
 OK 2 - \(a*\)\@>a
--- a/src/version.c
+++ b/src/version.c
@@ -729,6 +729,8 @@ static char *(features[]) =
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    1149,
+/**/
     1148,
 /**/
     1147,
--- a/src/window.c
+++ b/src/window.c
@@ -6818,7 +6818,7 @@ match_delete(wp, id, perr)
 	wp->w_match_head = cur->next;
     else
 	prev->next = cur->next;
-    vim_free(cur->match.regprog);
+    vim_regfree(cur->match.regprog);
     vim_free(cur->pattern);
     vim_free(cur);
     redraw_later(SOME_VALID);
@@ -6837,7 +6837,7 @@ clear_matches(wp)
     while (wp->w_match_head != NULL)
     {
 	m = wp->w_match_head->next;
-	vim_free(wp->w_match_head->match.regprog);
+	vim_regfree(wp->w_match_head->match.regprog);
 	vim_free(wp->w_match_head->pattern);
 	vim_free(wp->w_match_head);
 	wp->w_match_head = m;