diff options
Diffstat (limited to 'src/nvim/regexp.c')
-rw-r--r-- | src/nvim/regexp.c | 73 |
1 files changed, 33 insertions, 40 deletions
diff --git a/src/nvim/regexp.c b/src/nvim/regexp.c index 55490446e5..ab1a7c7b3e 100644 --- a/src/nvim/regexp.c +++ b/src/nvim/regexp.c @@ -1210,13 +1210,10 @@ char_u *skip_regexp(char_u *startp, int dirc, int magic, char_u **newp) return p; } -/// /// Return TRUE if the back reference is legal. We must have seen the close /// brace. -/// TODO(billy4195): Should also check that we don't refer to something that is -/// repeated +/// TODO(vim): Should also check that we don't refer to something repeated /// (+*=): what instance of the repetition should we match? -/// static int seen_endbrace(int refnum) { if (!had_endbrace[refnum]) { @@ -3388,17 +3385,13 @@ static long bt_regexec_multi(regmmatch_T *rmp, win_T *win, buf_T *buf, return bt_regexec_both(NULL, col, tm, timed_out); } -/* - * Match a regexp against a string ("line" points to the string) or multiple - * lines ("line" is NULL, use reg_getline()). - * Returns 0 for failure, number of lines contained in the match otherwise. - */ +/// Match a regexp against a string ("line" points to the string) or multiple +/// lines ("line" is NULL, use reg_getline()). +/// @return 0 for failure, or number of lines contained in the match. static long bt_regexec_both(char_u *line, - // column to start looking for match - colnr_T col, + colnr_T col, // column to start search proftime_T *tm, // timeout limit or NULL - int *timed_out // flag set on timeout or NULL - ) + int *timed_out) // flag set on timeout or NULL { bt_regprog_T *prog; char_u *s; @@ -3604,12 +3597,12 @@ void unref_extmatch(reg_extmatch_T *em) } } -/* - * regtry - try match of "prog" with at regline["col"]. - * Returns 0 for failure, number of lines contained in the match otherwise. - */ -static long regtry(bt_regprog_T *prog, colnr_T col, - proftime_T *tm, int *timed_out) +/// Try match of "prog" with at regline["col"]. +/// @returns 0 for failure, or number of lines contained in the match. +static long regtry(bt_regprog_T *prog, + colnr_T col, + proftime_T *tm, // timeout limit or NULL + int *timed_out) // flag set on timeout or NULL { reginput = regline + col; need_clear_subexpr = TRUE; @@ -3760,26 +3753,23 @@ static int reg_match_visual(void) static long bl_minval; static long bl_maxval; -/* - * regmatch - main matching routine - * - * Conceptually the strategy is simple: Check to see whether the current node - * matches, push an item onto the regstack and loop to see whether the rest - * matches, and then act accordingly. In practice we make some effort to - * avoid using the regstack, in particular by going through "ordinary" nodes - * (that don't need to know whether the rest of the match failed) by a nested - * loop. - * - * Returns TRUE when there is a match. Leaves reginput and reglnum just after - * the last matched character. - * Returns FALSE when there is no match. Leaves reginput and reglnum in an - * undefined state! - */ -static int -regmatch( +/// Main matching routine +/// +/// Conceptually the strategy is simple: Check to see whether the current node +/// matches, push an item onto the regstack and loop to see whether the rest +/// matches, and then act accordingly. In practice we make some effort to +/// avoid using the regstack, in particular by going through "ordinary" nodes +/// (that don't need to know whether the rest of the match failed) by a nested +/// loop. +/// +/// Returns TRUE when there is a match. Leaves reginput and reglnum just after +/// the last matched character. +/// Returns FALSE when there is no match. Leaves reginput and reglnum in an +/// undefined state! +static int regmatch( char_u *scan, // Current node. - proftime_T *tm, - int *timed_out + proftime_T *tm, // timeout limit or NULL + int *timed_out // flag set on timeout or NULL ) { char_u *next; /* Next node. */ @@ -3788,14 +3778,13 @@ regmatch( regitem_T *rp; int no; int status; // one of the RA_ values: - int tm_count; // counter for checking timeout + int tm_count = 0; #define RA_FAIL 1 // something failed, abort #define RA_CONT 2 // continue in inner loop #define RA_BREAK 3 // break inner loop #define RA_MATCH 4 // successful match #define RA_NOMATCH 5 // didn't match - tm_count = 0; // Make "regstack" and "backpos" empty. They are allocated and freed in // bt_regexec_both() to reduce malloc()/free() calls. regstack.ga_len = 0; @@ -3825,9 +3814,13 @@ regmatch( status = RA_FAIL; break; } + // Check for timeout once in a 100 times to avoid overhead. if (tm != NULL && ++tm_count == 100) { tm_count = 0; if (profile_passed_limit(*tm)) { + if (timed_out != NULL) { + *timed_out = true; + } status = RA_FAIL; break; } |