aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/regexp_nfa.c
diff options
context:
space:
mode:
authorBilly Su <g4691821@gmail.com>2019-03-07 13:08:15 +0800
committerBilly Su <g4691821@gmail.com>2019-03-07 23:27:19 +0800
commit241b905b1378c5c45d8d284bb191b16807ff2a44 (patch)
treece1726447334ae05c894b1b7bd59718058a12a32 /src/nvim/regexp_nfa.c
parent5f84b1dc4154ea8def87b6113fa9b9ed058d83d2 (diff)
downloadrneovim-241b905b1378c5c45d8d284bb191b16807ff2a44.tar.gz
rneovim-241b905b1378c5c45d8d284bb191b16807ff2a44.tar.bz2
rneovim-241b905b1378c5c45d8d284bb191b16807ff2a44.zip
vim-patch:8.0.0643: when a pattern search is slow Vim becomes unusable
Problem: When 'hlsearch' is set and matching with the last search pattern is very slow, Vim becomes unusable. Cannot quit search by pressing CTRL-C. Solution: When the search times out set a flag and don't try again. Check for timeout and CTRL-C in NFA loop that adds states. https://github.com/vim/vim/commit/fbd0b0af6800f6ff89857863d6a07ea03f09ff6c
Diffstat (limited to 'src/nvim/regexp_nfa.c')
-rw-r--r--src/nvim/regexp_nfa.c48
1 files changed, 38 insertions, 10 deletions
diff --git a/src/nvim/regexp_nfa.c b/src/nvim/regexp_nfa.c
index 95030974d8..0df67336d3 100644
--- a/src/nvim/regexp_nfa.c
+++ b/src/nvim/regexp_nfa.c
@@ -3568,6 +3568,7 @@ static char *pim_info(nfa_pim_T *pim)
// Used during execution: whether a match has been found.
static int nfa_match;
static proftime_T *nfa_time_limit;
+static int *nfa_timed_out;
static int nfa_time_count;
// Copy postponed invisible match info from "from" to "to".
@@ -4939,6 +4940,17 @@ static long find_match_text(colnr_T startcol, int regstart, char_u *match_text)
#undef PTR2LEN
}
+static int nfa_did_time_out(void)
+{
+ if (nfa_time_limit != NULL && profile_passed_limit(*nfa_time_limit)) {
+ if (nfa_timed_out != NULL) {
+ *nfa_timed_out = true;
+ }
+ return true;
+ }
+ return false;
+}
+
/// Main matching routine.
///
/// Run NFA to determine whether it matches reginput.
@@ -4982,7 +4994,7 @@ static int nfa_regmatch(nfa_regprog_T *prog, nfa_state_T *start,
#endif
return false;
}
- if (nfa_time_limit != NULL && profile_passed_limit(*nfa_time_limit)) {
+ if (nfa_did_time_out()) {
#ifdef NFA_REGEXP_DEBUG_LOG
fclose(debug);
#endif
@@ -5095,8 +5107,20 @@ static int nfa_regmatch(nfa_regprog_T *prog, nfa_state_T *start,
if (thislist->n == 0)
break;
- /* compute nextlist */
- for (listidx = 0; listidx < thislist->n; ++listidx) {
+ // compute nextlist
+ for (listidx = 0; listidx < thislist->n; listidx++) {
+ // If the list gets very long there probably is something wrong.
+ // At least allow interrupting with CTRL-C.
+ fast_breakcheck();
+ if (got_int) {
+ break;
+ }
+ if (nfa_time_limit != NULL && ++nfa_time_count == 20) {
+ nfa_time_count = 0;
+ if (nfa_did_time_out()) {
+ break;
+ }
+ }
t = &thislist->t[listidx];
#ifdef NFA_REGEXP_DEBUG_LOG
@@ -6218,7 +6242,7 @@ nextchar:
// Check for timeout once every twenty times to avoid overhead.
if (nfa_time_limit != NULL && ++nfa_time_count == 20) {
nfa_time_count = 0;
- if (profile_passed_limit(*nfa_time_limit)) {
+ if (nfa_did_time_out()) {
break;
}
}
@@ -6245,7 +6269,8 @@ theend:
// Try match of "prog" with at regline["col"].
// Returns <= 0 for failure, number of lines contained in the match otherwise.
-static long nfa_regtry(nfa_regprog_T *prog, colnr_T col, proftime_T *tm)
+static long nfa_regtry(nfa_regprog_T *prog, colnr_T col,
+ proftime_T *tm, int *timed_out)
{
int i;
regsubs_T subs, m;
@@ -6256,6 +6281,7 @@ static long nfa_regtry(nfa_regprog_T *prog, colnr_T col, proftime_T *tm)
reginput = regline + col;
nfa_time_limit = tm;
+ nfa_timed_out = timed_out;
nfa_time_count = 0;
#ifdef REGEXP_DEBUG
@@ -6367,7 +6393,8 @@ static long nfa_regtry(nfa_regprog_T *prog, colnr_T col, proftime_T *tm)
///
/// @return <= 0 if there is no match and number of lines contained in the
/// match otherwise.
-static long nfa_regexec_both(char_u *line, colnr_T startcol, proftime_T *tm)
+static long nfa_regexec_both(char_u *line, colnr_T startcol,
+ proftime_T *tm, int *timed_out)
{
nfa_regprog_T *prog;
long retval = 0L;
@@ -6449,7 +6476,7 @@ static long nfa_regexec_both(char_u *line, colnr_T startcol, proftime_T *tm)
prog->state[i].lastlist[1] = 0;
}
- retval = nfa_regtry(prog, col, tm);
+ retval = nfa_regtry(prog, col, tm, timed_out);
nfa_regengine.expr = NULL;
@@ -6596,7 +6623,7 @@ nfa_regexec_nl (
rex.reg_ic = rmp->rm_ic;
rex.reg_icombine = false;
rex.reg_maxcol = 0;
- return nfa_regexec_both(line, col, NULL);
+ return nfa_regexec_both(line, col, NULL, NULL);
}
/// Matches a regexp against multiple lines.
@@ -6634,7 +6661,8 @@ nfa_regexec_nl (
/// @par
/// FIXME if this behavior is not compatible.
static long nfa_regexec_multi(regmmatch_T *rmp, win_T *win, buf_T *buf,
- linenr_T lnum, colnr_T col, proftime_T *tm)
+ linenr_T lnum, colnr_T col,
+ proftime_T *tm, int *timed_out)
{
rex.reg_match = NULL;
rex.reg_mmatch = rmp;
@@ -6647,5 +6675,5 @@ static long nfa_regexec_multi(regmmatch_T *rmp, win_T *win, buf_T *buf,
rex.reg_icombine = false;
rex.reg_maxcol = rmp->rmm_maxcol;
- return nfa_regexec_both(NULL, col, tm);
+ return nfa_regexec_both(NULL, col, tm, timed_out);
}