blob: 71bcd6ccabda8165ffa0a998ba0a7d6cde506d9c [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * Handling of regular expressions: vim_regcomp(), vim_regexec(), vim_regsub()
4 *
5 * NOTICE:
6 *
7 * This is NOT the original regular expression code as written by Henry
8 * Spencer. This code has been modified specifically for use with the VIM
9 * editor, and should not be used separately from Vim. If you want a good
10 * regular expression library, get the original code. The copyright notice
11 * that follows is from the original.
12 *
13 * END NOTICE
14 *
15 * Copyright (c) 1986 by University of Toronto.
16 * Written by Henry Spencer. Not derived from licensed software.
17 *
18 * Permission is granted to anyone to use this software for any
19 * purpose on any computer system, and to redistribute it freely,
20 * subject to the following restrictions:
21 *
22 * 1. The author is not responsible for the consequences of use of
23 * this software, no matter how awful, even if they arise
24 * from defects in it.
25 *
26 * 2. The origin of this software must not be misrepresented, either
27 * by explicit claim or by omission.
28 *
29 * 3. Altered versions must be plainly marked as such, and must not
30 * be misrepresented as being the original software.
31 *
32 * Beware that some of this code is subtly aware of the way operator
33 * precedence is structured in regular expressions. Serious changes in
34 * regular-expression syntax might require a total rethink.
35 *
Bram Moolenaarc0197e22004-09-13 20:26:32 +000036 * Changes have been made by Tony Andrews, Olaf 'Rhialto' Seibert, Robert
37 * Webb, Ciaran McCreesh and Bram Moolenaar.
Bram Moolenaar071d4272004-06-13 20:20:40 +000038 * Named character class support added by Walter Briscoe (1998 Jul 01)
39 */
40
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020041/* Uncomment the first if you do not want to see debugging logs or files
42 * related to regular expressions, even when compiling with -DDEBUG.
43 * Uncomment the second to get the regexp debugging. */
44/* #undef DEBUG */
45/* #define DEBUG */
46
Bram Moolenaar071d4272004-06-13 20:20:40 +000047#include "vim.h"
48
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020049#ifdef DEBUG
50/* show/save debugging data when BT engine is used */
51# define BT_REGEXP_DUMP
52/* save the debugging data to a file instead of displaying it */
53# define BT_REGEXP_LOG
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +020054# define BT_REGEXP_DEBUG_LOG
55# define BT_REGEXP_DEBUG_LOG_NAME "bt_regexp_debug.log"
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020056#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000057
58/*
59 * The "internal use only" fields in regexp.h are present to pass info from
60 * compile to execute that permits the execute phase to run lots faster on
61 * simple cases. They are:
62 *
63 * regstart char that must begin a match; NUL if none obvious; Can be a
64 * multi-byte character.
65 * reganch is the match anchored (at beginning-of-line only)?
66 * regmust string (pointer into program) that match must include, or NULL
67 * regmlen length of regmust string
68 * regflags RF_ values or'ed together
69 *
70 * Regstart and reganch permit very fast decisions on suitable starting points
71 * for a match, cutting down the work a lot. Regmust permits fast rejection
72 * of lines that cannot possibly match. The regmust tests are costly enough
73 * that vim_regcomp() supplies a regmust only if the r.e. contains something
74 * potentially expensive (at present, the only such thing detected is * or +
75 * at the start of the r.e., which can involve a lot of backup). Regmlen is
76 * supplied because the test in vim_regexec() needs it and vim_regcomp() is
77 * computing it anyway.
78 */
79
80/*
81 * Structure for regexp "program". This is essentially a linear encoding
82 * of a nondeterministic finite-state machine (aka syntax charts or
83 * "railroad normal form" in parsing technology). Each node is an opcode
84 * plus a "next" pointer, possibly plus an operand. "Next" pointers of
85 * all nodes except BRANCH and BRACES_COMPLEX implement concatenation; a "next"
86 * pointer with a BRANCH on both ends of it is connecting two alternatives.
87 * (Here we have one of the subtle syntax dependencies: an individual BRANCH
88 * (as opposed to a collection of them) is never concatenated with anything
89 * because of operator precedence). The "next" pointer of a BRACES_COMPLEX
Bram Moolenaardf177f62005-02-22 08:39:57 +000090 * node points to the node after the stuff to be repeated.
91 * The operand of some types of node is a literal string; for others, it is a
92 * node leading into a sub-FSM. In particular, the operand of a BRANCH node
93 * is the first node of the branch.
94 * (NB this is *not* a tree structure: the tail of the branch connects to the
95 * thing following the set of BRANCHes.)
Bram Moolenaar071d4272004-06-13 20:20:40 +000096 *
97 * pattern is coded like:
98 *
99 * +-----------------+
100 * | V
101 * <aa>\|<bb> BRANCH <aa> BRANCH <bb> --> END
102 * | ^ | ^
103 * +------+ +----------+
104 *
105 *
106 * +------------------+
107 * V |
108 * <aa>* BRANCH BRANCH <aa> --> BACK BRANCH --> NOTHING --> END
109 * | | ^ ^
110 * | +---------------+ |
111 * +---------------------------------------------+
112 *
113 *
Bram Moolenaardf177f62005-02-22 08:39:57 +0000114 * +----------------------+
115 * V |
Bram Moolenaar582fd852005-03-28 20:58:01 +0000116 * <aa>\+ BRANCH <aa> --> BRANCH --> BACK BRANCH --> NOTHING --> END
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000117 * | | ^ ^
118 * | +-----------+ |
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000119 * +--------------------------------------------------+
Bram Moolenaardf177f62005-02-22 08:39:57 +0000120 *
121 *
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122 * +-------------------------+
123 * V |
124 * <aa>\{} BRANCH BRACE_LIMITS --> BRACE_COMPLEX <aa> --> BACK END
125 * | | ^
126 * | +----------------+
127 * +-----------------------------------------------+
128 *
129 *
130 * <aa>\@!<bb> BRANCH NOMATCH <aa> --> END <bb> --> END
131 * | | ^ ^
132 * | +----------------+ |
133 * +--------------------------------+
134 *
135 * +---------+
136 * | V
137 * \z[abc] BRANCH BRANCH a BRANCH b BRANCH c BRANCH NOTHING --> END
138 * | | | | ^ ^
139 * | | | +-----+ |
140 * | | +----------------+ |
141 * | +---------------------------+ |
142 * +------------------------------------------------------+
143 *
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +0000144 * They all start with a BRANCH for "\|" alternatives, even when there is only
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145 * one alternative.
146 */
147
148/*
149 * The opcodes are:
150 */
151
152/* definition number opnd? meaning */
153#define END 0 /* End of program or NOMATCH operand. */
154#define BOL 1 /* Match "" at beginning of line. */
155#define EOL 2 /* Match "" at end of line. */
156#define BRANCH 3 /* node Match this alternative, or the
157 * next... */
158#define BACK 4 /* Match "", "next" ptr points backward. */
159#define EXACTLY 5 /* str Match this string. */
160#define NOTHING 6 /* Match empty string. */
161#define STAR 7 /* node Match this (simple) thing 0 or more
162 * times. */
163#define PLUS 8 /* node Match this (simple) thing 1 or more
164 * times. */
165#define MATCH 9 /* node match the operand zero-width */
166#define NOMATCH 10 /* node check for no match with operand */
167#define BEHIND 11 /* node look behind for a match with operand */
168#define NOBEHIND 12 /* node look behind for no match with operand */
169#define SUBPAT 13 /* node match the operand here */
170#define BRACE_SIMPLE 14 /* node Match this (simple) thing between m and
171 * n times (\{m,n\}). */
172#define BOW 15 /* Match "" after [^a-zA-Z0-9_] */
173#define EOW 16 /* Match "" at [^a-zA-Z0-9_] */
174#define BRACE_LIMITS 17 /* nr nr define the min & max for BRACE_SIMPLE
175 * and BRACE_COMPLEX. */
176#define NEWL 18 /* Match line-break */
177#define BHPOS 19 /* End position for BEHIND or NOBEHIND */
178
179
180/* character classes: 20-48 normal, 50-78 include a line-break */
181#define ADD_NL 30
182#define FIRST_NL ANY + ADD_NL
183#define ANY 20 /* Match any one character. */
184#define ANYOF 21 /* str Match any character in this string. */
185#define ANYBUT 22 /* str Match any character not in this
186 * string. */
187#define IDENT 23 /* Match identifier char */
188#define SIDENT 24 /* Match identifier char but no digit */
189#define KWORD 25 /* Match keyword char */
190#define SKWORD 26 /* Match word char but no digit */
191#define FNAME 27 /* Match file name char */
192#define SFNAME 28 /* Match file name char but no digit */
193#define PRINT 29 /* Match printable char */
194#define SPRINT 30 /* Match printable char but no digit */
195#define WHITE 31 /* Match whitespace char */
196#define NWHITE 32 /* Match non-whitespace char */
197#define DIGIT 33 /* Match digit char */
198#define NDIGIT 34 /* Match non-digit char */
199#define HEX 35 /* Match hex char */
200#define NHEX 36 /* Match non-hex char */
201#define OCTAL 37 /* Match octal char */
202#define NOCTAL 38 /* Match non-octal char */
203#define WORD 39 /* Match word char */
204#define NWORD 40 /* Match non-word char */
205#define HEAD 41 /* Match head char */
206#define NHEAD 42 /* Match non-head char */
207#define ALPHA 43 /* Match alpha char */
208#define NALPHA 44 /* Match non-alpha char */
209#define LOWER 45 /* Match lowercase char */
210#define NLOWER 46 /* Match non-lowercase char */
211#define UPPER 47 /* Match uppercase char */
212#define NUPPER 48 /* Match non-uppercase char */
213#define LAST_NL NUPPER + ADD_NL
214#define WITH_NL(op) ((op) >= FIRST_NL && (op) <= LAST_NL)
215
216#define MOPEN 80 /* -89 Mark this point in input as start of
217 * \( subexpr. MOPEN + 0 marks start of
218 * match. */
219#define MCLOSE 90 /* -99 Analogous to MOPEN. MCLOSE + 0 marks
220 * end of match. */
221#define BACKREF 100 /* -109 node Match same string again \1-\9 */
222
223#ifdef FEAT_SYN_HL
224# define ZOPEN 110 /* -119 Mark this point in input as start of
225 * \z( subexpr. */
226# define ZCLOSE 120 /* -129 Analogous to ZOPEN. */
227# define ZREF 130 /* -139 node Match external submatch \z1-\z9 */
228#endif
229
230#define BRACE_COMPLEX 140 /* -149 node Match nodes between m & n times */
231
232#define NOPEN 150 /* Mark this point in input as start of
233 \%( subexpr. */
234#define NCLOSE 151 /* Analogous to NOPEN. */
235
236#define MULTIBYTECODE 200 /* mbc Match one multi-byte character */
237#define RE_BOF 201 /* Match "" at beginning of file. */
238#define RE_EOF 202 /* Match "" at end of file. */
239#define CURSOR 203 /* Match location of cursor. */
240
241#define RE_LNUM 204 /* nr cmp Match line number */
242#define RE_COL 205 /* nr cmp Match column number */
243#define RE_VCOL 206 /* nr cmp Match virtual column number */
244
Bram Moolenaar71fe80d2006-01-22 23:25:56 +0000245#define RE_MARK 207 /* mark cmp Match mark position */
246#define RE_VISUAL 208 /* Match Visual area */
Bram Moolenaar8df5acf2014-05-13 19:37:29 +0200247#define RE_COMPOSING 209 /* any composing characters */
Bram Moolenaar71fe80d2006-01-22 23:25:56 +0000248
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249/*
250 * Magic characters have a special meaning, they don't match literally.
251 * Magic characters are negative. This separates them from literal characters
252 * (possibly multi-byte). Only ASCII characters can be Magic.
253 */
254#define Magic(x) ((int)(x) - 256)
255#define un_Magic(x) ((x) + 256)
256#define is_Magic(x) ((x) < 0)
257
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100259no_Magic(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260{
261 if (is_Magic(x))
262 return un_Magic(x);
263 return x;
264}
265
266 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100267toggle_Magic(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268{
269 if (is_Magic(x))
270 return un_Magic(x);
271 return Magic(x);
272}
273
274/*
275 * The first byte of the regexp internal "program" is actually this magic
276 * number; the start node begins in the second byte. It's used to catch the
277 * most severe mutilation of the program by the caller.
278 */
279
280#define REGMAGIC 0234
281
282/*
283 * Opcode notes:
284 *
285 * BRANCH The set of branches constituting a single choice are hooked
286 * together with their "next" pointers, since precedence prevents
287 * anything being concatenated to any individual branch. The
288 * "next" pointer of the last BRANCH in a choice points to the
289 * thing following the whole choice. This is also where the
290 * final "next" pointer of each individual branch points; each
291 * branch starts with the operand node of a BRANCH node.
292 *
293 * BACK Normal "next" pointers all implicitly point forward; BACK
294 * exists to make loop structures possible.
295 *
296 * STAR,PLUS '=', and complex '*' and '+', are implemented as circular
297 * BRANCH structures using BACK. Simple cases (one character
298 * per match) are implemented with STAR and PLUS for speed
299 * and to minimize recursive plunges.
300 *
301 * BRACE_LIMITS This is always followed by a BRACE_SIMPLE or BRACE_COMPLEX
302 * node, and defines the min and max limits to be used for that
303 * node.
304 *
305 * MOPEN,MCLOSE ...are numbered at compile time.
306 * ZOPEN,ZCLOSE ...ditto
307 */
308
309/*
310 * A node is one char of opcode followed by two chars of "next" pointer.
311 * "Next" pointers are stored as two 8-bit bytes, high order first. The
312 * value is a positive offset from the opcode of the node containing it.
313 * An operand, if any, simply follows the node. (Note that much of the
314 * code generation knows about this implicit relationship.)
315 *
316 * Using two bytes for the "next" pointer is vast overkill for most things,
317 * but allows patterns to get big without disasters.
318 */
319#define OP(p) ((int)*(p))
320#define NEXT(p) (((*((p) + 1) & 0377) << 8) + (*((p) + 2) & 0377))
321#define OPERAND(p) ((p) + 3)
322/* Obtain an operand that was stored as four bytes, MSB first. */
323#define OPERAND_MIN(p) (((long)(p)[3] << 24) + ((long)(p)[4] << 16) \
324 + ((long)(p)[5] << 8) + (long)(p)[6])
325/* Obtain a second operand stored as four bytes. */
326#define OPERAND_MAX(p) OPERAND_MIN((p) + 4)
327/* Obtain a second single-byte operand stored after a four bytes operand. */
328#define OPERAND_CMP(p) (p)[7]
329
330/*
331 * Utility definitions.
332 */
333#define UCHARAT(p) ((int)*(char_u *)(p))
334
335/* Used for an error (down from) vim_regcomp(): give the error message, set
336 * rc_did_emsg and return NULL */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100337#define EMSG_RET_NULL(m) return (emsg((m)), rc_did_emsg = TRUE, (void *)NULL)
338#define IEMSG_RET_NULL(m) return (iemsg((m)), rc_did_emsg = TRUE, (void *)NULL)
339#define EMSG_RET_FAIL(m) return (emsg((m)), rc_did_emsg = TRUE, FAIL)
340#define EMSG2_RET_NULL(m, c) return (semsg((const char *)(m), (c) ? "" : "\\"), rc_did_emsg = TRUE, (void *)NULL)
Bram Moolenaar1be45b22019-01-14 22:46:15 +0100341#define EMSG3_RET_NULL(m, c, a) return (semsg((const char *)(m), (c) ? "" : "\\", (a)), rc_did_emsg = TRUE, (void *)NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100342#define EMSG2_RET_FAIL(m, c) return (semsg((const char *)(m), (c) ? "" : "\\"), rc_did_emsg = TRUE, FAIL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200343#define EMSG_ONE_RET_NULL EMSG2_RET_NULL(_("E369: invalid item in %s%%[]"), reg_magic == MAGIC_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344
Bram Moolenaar95f09602016-11-10 20:01:45 +0100345
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346#define MAX_LIMIT (32767L << 16L)
347
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100348static int cstrncmp(char_u *s1, char_u *s2, int *n);
349static char_u *cstrchr(char_u *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200351#ifdef BT_REGEXP_DUMP
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100352static void regdump(char_u *, bt_regprog_T *);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354#ifdef DEBUG
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100355static char_u *regprop(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356#endif
357
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100358static int re_mult_next(char *what);
Bram Moolenaarfb031402014-09-09 17:18:49 +0200359
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200360static char_u e_missingbracket[] = N_("E769: Missing ] after %s[");
Bram Moolenaar966e58e2017-06-05 16:54:08 +0200361static char_u e_reverse_range[] = N_("E944: Reverse range in character class");
Bram Moolenaar6c95fbc2017-06-05 17:53:37 +0200362#ifdef FEAT_MBYTE
Bram Moolenaar966e58e2017-06-05 16:54:08 +0200363static char_u e_large_class[] = N_("E945: Range too large in character class");
Bram Moolenaar6c95fbc2017-06-05 17:53:37 +0200364#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200365static char_u e_unmatchedpp[] = N_("E53: Unmatched %s%%(");
366static char_u e_unmatchedp[] = N_("E54: Unmatched %s(");
367static char_u e_unmatchedpar[] = N_("E55: Unmatched %s)");
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200368#ifdef FEAT_SYN_HL
Bram Moolenaar5de820b2013-06-02 15:01:57 +0200369static char_u e_z_not_allowed[] = N_("E66: \\z( not allowed here");
Bram Moolenaarbcf94422018-06-23 14:21:42 +0200370static char_u e_z1_not_allowed[] = N_("E67: \\z1 - \\z9 not allowed here");
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200371#endif
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +0200372static char_u e_missing_sb[] = N_("E69: Missing ] after %s%%[");
Bram Moolenaar2976c022013-06-05 21:30:37 +0200373static char_u e_empty_sb[] = N_("E70: Empty %s%%[]");
Bram Moolenaar0270f382018-07-17 05:43:58 +0200374static char_u e_recursive[] = N_("E956: Cannot use pattern recursively");
375
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376#define NOT_MULTI 0
377#define MULTI_ONE 1
378#define MULTI_MULT 2
379/*
380 * Return NOT_MULTI if c is not a "multi" operator.
381 * Return MULTI_ONE if c is a single "multi" operator.
382 * Return MULTI_MULT if c is a multi "multi" operator.
383 */
384 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100385re_multi_type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386{
387 if (c == Magic('@') || c == Magic('=') || c == Magic('?'))
388 return MULTI_ONE;
389 if (c == Magic('*') || c == Magic('+') || c == Magic('{'))
390 return MULTI_MULT;
391 return NOT_MULTI;
392}
393
394/*
395 * Flags to be passed up and down.
396 */
397#define HASWIDTH 0x1 /* Known never to match null string. */
398#define SIMPLE 0x2 /* Simple enough to be STAR/PLUS operand. */
399#define SPSTART 0x4 /* Starts with * or +. */
400#define HASNL 0x8 /* Contains some \n. */
401#define HASLOOKBH 0x10 /* Contains "\@<=" or "\@<!". */
402#define WORST 0 /* Worst case. */
403
404/*
405 * When regcode is set to this value, code is not emitted and size is computed
406 * instead.
407 */
408#define JUST_CALC_SIZE ((char_u *) -1)
409
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000410static char_u *reg_prev_sub = NULL;
411
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412/*
413 * REGEXP_INRANGE contains all characters which are always special in a []
414 * range after '\'.
415 * REGEXP_ABBR contains all characters which act as abbreviations after '\'.
416 * These are:
417 * \n - New line (NL).
418 * \r - Carriage Return (CR).
419 * \t - Tab (TAB).
420 * \e - Escape (ESC).
421 * \b - Backspace (Ctrl_H).
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000422 * \d - Character code in decimal, eg \d123
423 * \o - Character code in octal, eg \o80
424 * \x - Character code in hex, eg \x4a
425 * \u - Multibyte character code, eg \u20ac
426 * \U - Long multibyte character code, eg \U12345678
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427 */
428static char_u REGEXP_INRANGE[] = "]^-n\\";
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000429static char_u REGEXP_ABBR[] = "nrtebdoxuU";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431/*
432 * Translate '\x' to its control character, except "\n", which is Magic.
433 */
434 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100435backslash_trans(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436{
437 switch (c)
438 {
439 case 'r': return CAR;
440 case 't': return TAB;
441 case 'e': return ESC;
442 case 'b': return BS;
443 }
444 return c;
445}
446
447/*
Bram Moolenaardf177f62005-02-22 08:39:57 +0000448 * Check for a character class name "[:name:]". "pp" points to the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 * Returns one of the CLASS_ items. CLASS_NONE means that no item was
450 * recognized. Otherwise "pp" is advanced to after the item.
451 */
452 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100453get_char_class(char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454{
455 static const char *(class_names[]) =
456 {
457 "alnum:]",
458#define CLASS_ALNUM 0
459 "alpha:]",
460#define CLASS_ALPHA 1
461 "blank:]",
462#define CLASS_BLANK 2
463 "cntrl:]",
464#define CLASS_CNTRL 3
465 "digit:]",
466#define CLASS_DIGIT 4
467 "graph:]",
468#define CLASS_GRAPH 5
469 "lower:]",
470#define CLASS_LOWER 6
471 "print:]",
472#define CLASS_PRINT 7
473 "punct:]",
474#define CLASS_PUNCT 8
475 "space:]",
476#define CLASS_SPACE 9
477 "upper:]",
478#define CLASS_UPPER 10
479 "xdigit:]",
480#define CLASS_XDIGIT 11
481 "tab:]",
482#define CLASS_TAB 12
483 "return:]",
484#define CLASS_RETURN 13
485 "backspace:]",
486#define CLASS_BACKSPACE 14
487 "escape:]",
488#define CLASS_ESCAPE 15
489 };
490#define CLASS_NONE 99
491 int i;
492
493 if ((*pp)[1] == ':')
494 {
Bram Moolenaar78a15312009-05-15 19:33:18 +0000495 for (i = 0; i < (int)(sizeof(class_names) / sizeof(*class_names)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 if (STRNCMP(*pp + 2, class_names[i], STRLEN(class_names[i])) == 0)
497 {
498 *pp += STRLEN(class_names[i]) + 2;
499 return i;
500 }
501 }
502 return CLASS_NONE;
503}
504
505/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 * Specific version of character class functions.
507 * Using a table to keep this fast.
508 */
509static short class_tab[256];
510
511#define RI_DIGIT 0x01
512#define RI_HEX 0x02
513#define RI_OCTAL 0x04
514#define RI_WORD 0x08
515#define RI_HEAD 0x10
516#define RI_ALPHA 0x20
517#define RI_LOWER 0x40
518#define RI_UPPER 0x80
519#define RI_WHITE 0x100
520
521 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100522init_class_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523{
524 int i;
525 static int done = FALSE;
526
527 if (done)
528 return;
529
530 for (i = 0; i < 256; ++i)
531 {
532 if (i >= '0' && i <= '7')
533 class_tab[i] = RI_DIGIT + RI_HEX + RI_OCTAL + RI_WORD;
534 else if (i >= '8' && i <= '9')
535 class_tab[i] = RI_DIGIT + RI_HEX + RI_WORD;
536 else if (i >= 'a' && i <= 'f')
537 class_tab[i] = RI_HEX + RI_WORD + RI_HEAD + RI_ALPHA + RI_LOWER;
538#ifdef EBCDIC
539 else if ((i >= 'g' && i <= 'i') || (i >= 'j' && i <= 'r')
540 || (i >= 's' && i <= 'z'))
541#else
542 else if (i >= 'g' && i <= 'z')
543#endif
544 class_tab[i] = RI_WORD + RI_HEAD + RI_ALPHA + RI_LOWER;
545 else if (i >= 'A' && i <= 'F')
546 class_tab[i] = RI_HEX + RI_WORD + RI_HEAD + RI_ALPHA + RI_UPPER;
547#ifdef EBCDIC
548 else if ((i >= 'G' && i <= 'I') || ( i >= 'J' && i <= 'R')
549 || (i >= 'S' && i <= 'Z'))
550#else
551 else if (i >= 'G' && i <= 'Z')
552#endif
553 class_tab[i] = RI_WORD + RI_HEAD + RI_ALPHA + RI_UPPER;
554 else if (i == '_')
555 class_tab[i] = RI_WORD + RI_HEAD;
556 else
557 class_tab[i] = 0;
558 }
559 class_tab[' '] |= RI_WHITE;
560 class_tab['\t'] |= RI_WHITE;
561 done = TRUE;
562}
563
564#ifdef FEAT_MBYTE
565# define ri_digit(c) (c < 0x100 && (class_tab[c] & RI_DIGIT))
566# define ri_hex(c) (c < 0x100 && (class_tab[c] & RI_HEX))
567# define ri_octal(c) (c < 0x100 && (class_tab[c] & RI_OCTAL))
568# define ri_word(c) (c < 0x100 && (class_tab[c] & RI_WORD))
569# define ri_head(c) (c < 0x100 && (class_tab[c] & RI_HEAD))
570# define ri_alpha(c) (c < 0x100 && (class_tab[c] & RI_ALPHA))
571# define ri_lower(c) (c < 0x100 && (class_tab[c] & RI_LOWER))
572# define ri_upper(c) (c < 0x100 && (class_tab[c] & RI_UPPER))
573# define ri_white(c) (c < 0x100 && (class_tab[c] & RI_WHITE))
574#else
575# define ri_digit(c) (class_tab[c] & RI_DIGIT)
576# define ri_hex(c) (class_tab[c] & RI_HEX)
577# define ri_octal(c) (class_tab[c] & RI_OCTAL)
578# define ri_word(c) (class_tab[c] & RI_WORD)
579# define ri_head(c) (class_tab[c] & RI_HEAD)
580# define ri_alpha(c) (class_tab[c] & RI_ALPHA)
581# define ri_lower(c) (class_tab[c] & RI_LOWER)
582# define ri_upper(c) (class_tab[c] & RI_UPPER)
583# define ri_white(c) (class_tab[c] & RI_WHITE)
584#endif
585
586/* flags for regflags */
587#define RF_ICASE 1 /* ignore case */
588#define RF_NOICASE 2 /* don't ignore case */
589#define RF_HASNL 4 /* can match a NL */
590#define RF_ICOMBINE 8 /* ignore combining characters */
591#define RF_LOOKBH 16 /* uses "\@<=" or "\@<!" */
592
593/*
594 * Global work variables for vim_regcomp().
595 */
596
597static char_u *regparse; /* Input-scan pointer. */
598static int prevchr_len; /* byte length of previous char */
599static int num_complex_braces; /* Complex \{...} count */
600static int regnpar; /* () count. */
601#ifdef FEAT_SYN_HL
602static int regnzpar; /* \z() count. */
603static int re_has_z; /* \z item detected */
604#endif
605static char_u *regcode; /* Code-emit pointer, or JUST_CALC_SIZE */
606static long regsize; /* Code size. */
Bram Moolenaard3005802009-11-25 17:21:32 +0000607static int reg_toolong; /* TRUE when offset out of range */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608static char_u had_endbrace[NSUBEXP]; /* flags, TRUE if end of () found */
609static unsigned regflags; /* RF_ flags for prog */
610static long brace_min[10]; /* Minimums for complex brace repeats */
611static long brace_max[10]; /* Maximums for complex brace repeats */
612static int brace_count[10]; /* Current counts for complex brace repeats */
613#if defined(FEAT_SYN_HL) || defined(PROTO)
614static int had_eol; /* TRUE when EOL found by vim_regcomp() */
615#endif
616static int one_exactly = FALSE; /* only do one char for EXACTLY */
617
618static int reg_magic; /* magicness of the pattern: */
619#define MAGIC_NONE 1 /* "\V" very unmagic */
620#define MAGIC_OFF 2 /* "\M" or 'magic' off */
621#define MAGIC_ON 3 /* "\m" or 'magic' */
622#define MAGIC_ALL 4 /* "\v" very magic */
623
624static int reg_string; /* matching with a string instead of a buffer
625 line */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000626static int reg_strict; /* "[abc" is illegal */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627
628/*
629 * META contains all characters that may be magic, except '^' and '$'.
630 */
631
632#ifdef EBCDIC
633static char_u META[] = "%&()*+.123456789<=>?@ACDFHIKLMOPSUVWX[_acdfhiklmnopsuvwxz{|~";
634#else
635/* META[] is used often enough to justify turning it into a table. */
636static char_u META_flags[] = {
637 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
638 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
639/* % & ( ) * + . */
640 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0,
641/* 1 2 3 4 5 6 7 8 9 < = > ? */
642 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1,
643/* @ A C D F H I K L M O */
644 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1,
645/* P S U V W X Z [ _ */
646 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1,
647/* a c d f h i k l m n o */
648 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1,
649/* p s u v w x z { | ~ */
650 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1
651};
652#endif
653
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200654static int curchr; /* currently parsed character */
655/* Previous character. Note: prevchr is sometimes -1 when we are not at the
656 * start, eg in /[ ^I]^ the pattern was never found even if it existed,
657 * because ^ was taken to be magic -- webb */
658static int prevchr;
659static int prevprevchr; /* previous-previous character */
660static int nextchr; /* used for ungetchr() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661
662/* arguments for reg() */
663#define REG_NOPAREN 0 /* toplevel reg() */
664#define REG_PAREN 1 /* \(\) */
665#define REG_ZPAREN 2 /* \z(\) */
666#define REG_NPAREN 3 /* \%(\) */
667
Bram Moolenaar3737fc12013-06-01 14:42:56 +0200668typedef struct
669{
670 char_u *regparse;
671 int prevchr_len;
672 int curchr;
673 int prevchr;
674 int prevprevchr;
675 int nextchr;
676 int at_start;
677 int prev_at_start;
678 int regnpar;
679} parse_state_T;
680
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681/*
682 * Forward declarations for vim_regcomp()'s friends.
683 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100684static void initchr(char_u *);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100685static int getchr(void);
686static void skipchr_keepstart(void);
687static int peekchr(void);
688static void skipchr(void);
689static void ungetchr(void);
Bram Moolenaar4c22a912017-11-02 22:29:38 +0100690static long gethexchrs(int maxinputlen);
691static long getoctchrs(void);
692static long getdecchrs(void);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100693static int coll_get_char(void);
694static void regcomp_start(char_u *expr, int flags);
695static char_u *reg(int, int *);
696static char_u *regbranch(int *flagp);
697static char_u *regconcat(int *flagp);
698static char_u *regpiece(int *);
699static char_u *regatom(int *);
700static char_u *regnode(int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000701#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100702static int use_multibytecode(int c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000703#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100704static int prog_magic_wrong(void);
705static char_u *regnext(char_u *);
706static void regc(int b);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100708static void regmbc(int c);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200709# define REGMBC(x) regmbc(x);
710# define CASEMBC(x) case x:
Bram Moolenaardf177f62005-02-22 08:39:57 +0000711#else
712# define regmbc(c) regc(c)
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200713# define REGMBC(x)
714# define CASEMBC(x)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100716static void reginsert(int, char_u *);
717static void reginsert_nr(int op, long val, char_u *opnd);
718static void reginsert_limits(int, long, long, char_u *);
719static char_u *re_put_long(char_u *pr, long_u val);
720static int read_limits(long *, long *);
721static void regtail(char_u *, char_u *);
722static void regoptail(char_u *, char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200724static regengine_T bt_regengine;
725static regengine_T nfa_regengine;
726
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727/*
728 * Return TRUE if compiled regular expression "prog" can match a line break.
729 */
730 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100731re_multiline(regprog_T *prog)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732{
733 return (prog->regflags & RF_HASNL);
734}
735
736/*
Bram Moolenaardf177f62005-02-22 08:39:57 +0000737 * Check for an equivalence class name "[=a=]". "pp" points to the '['.
738 * Returns a character representing the class. Zero means that no item was
739 * recognized. Otherwise "pp" is advanced to after the item.
740 */
741 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100742get_equi_class(char_u **pp)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000743{
744 int c;
745 int l = 1;
746 char_u *p = *pp;
747
748 if (p[1] == '=')
749 {
750#ifdef FEAT_MBYTE
751 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000752 l = (*mb_ptr2len)(p + 2);
Bram Moolenaardf177f62005-02-22 08:39:57 +0000753#endif
754 if (p[l + 2] == '=' && p[l + 3] == ']')
755 {
756#ifdef FEAT_MBYTE
757 if (has_mbyte)
758 c = mb_ptr2char(p + 2);
759 else
760#endif
761 c = p[2];
762 *pp += l + 4;
763 return c;
764 }
765 }
766 return 0;
767}
768
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200769#ifdef EBCDIC
770/*
771 * Table for equivalence class "c". (IBM-1047)
772 */
773char *EQUIVAL_CLASS_C[16] = {
774 "A\x62\x63\x64\x65\x66\x67",
775 "C\x68",
776 "E\x71\x72\x73\x74",
777 "I\x75\x76\x77\x78",
778 "N\x69",
Bram Moolenaar22e42152016-04-03 14:02:02 +0200779 "O\xEB\xEC\xED\xEE\xEF\x80",
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200780 "U\xFB\xFC\xFD\xFE",
781 "Y\xBA",
782 "a\x42\x43\x44\x45\x46\x47",
783 "c\x48",
784 "e\x51\x52\x53\x54",
785 "i\x55\x56\x57\x58",
786 "n\x49",
Bram Moolenaar22e42152016-04-03 14:02:02 +0200787 "o\xCB\xCC\xCD\xCE\xCF\x70",
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200788 "u\xDB\xDC\xDD\xDE",
789 "y\x8D\xDF",
790};
791#endif
792
Bram Moolenaardf177f62005-02-22 08:39:57 +0000793/*
794 * Produce the bytes for equivalence class "c".
795 * Currently only handles latin1, latin9 and utf-8.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200796 * NOTE: When changing this function, also change nfa_emit_equi_class()
Bram Moolenaardf177f62005-02-22 08:39:57 +0000797 */
798 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100799reg_equi_class(int c)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000800{
801#ifdef FEAT_MBYTE
802 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
Bram Moolenaar78622822005-08-23 21:00:13 +0000803 || STRCMP(p_enc, "iso-8859-15") == 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000804#endif
805 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200806#ifdef EBCDIC
807 int i;
808
809 /* This might be slower than switch/case below. */
810 for (i = 0; i < 16; i++)
811 {
812 if (vim_strchr(EQUIVAL_CLASS_C[i], c) != NULL)
813 {
814 char *p = EQUIVAL_CLASS_C[i];
815
816 while (*p != 0)
817 regmbc(*p++);
818 return;
819 }
820 }
821#else
Bram Moolenaardf177f62005-02-22 08:39:57 +0000822 switch (c)
823 {
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200824 /* Do not use '\300' style, it results in a negative number. */
825 case 'A': case 0xc0: case 0xc1: case 0xc2:
826 case 0xc3: case 0xc4: case 0xc5:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200827 CASEMBC(0x100) CASEMBC(0x102) CASEMBC(0x104) CASEMBC(0x1cd)
828 CASEMBC(0x1de) CASEMBC(0x1e0) CASEMBC(0x1ea2)
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200829 regmbc('A'); regmbc(0xc0); regmbc(0xc1);
830 regmbc(0xc2); regmbc(0xc3); regmbc(0xc4);
831 regmbc(0xc5);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200832 REGMBC(0x100) REGMBC(0x102) REGMBC(0x104)
833 REGMBC(0x1cd) REGMBC(0x1de) REGMBC(0x1e0)
834 REGMBC(0x1ea2)
835 return;
836 case 'B': CASEMBC(0x1e02) CASEMBC(0x1e06)
837 regmbc('B'); REGMBC(0x1e02) REGMBC(0x1e06)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000838 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200839 case 'C': case 0xc7:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200840 CASEMBC(0x106) CASEMBC(0x108) CASEMBC(0x10a) CASEMBC(0x10c)
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200841 regmbc('C'); regmbc(0xc7);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200842 REGMBC(0x106) REGMBC(0x108) REGMBC(0x10a)
843 REGMBC(0x10c)
844 return;
845 case 'D': CASEMBC(0x10e) CASEMBC(0x110) CASEMBC(0x1e0a)
846 CASEMBC(0x1e0e) CASEMBC(0x1e10)
847 regmbc('D'); REGMBC(0x10e) REGMBC(0x110)
848 REGMBC(0x1e0a) REGMBC(0x1e0e) REGMBC(0x1e10)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000849 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200850 case 'E': case 0xc8: case 0xc9: case 0xca: case 0xcb:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200851 CASEMBC(0x112) CASEMBC(0x114) CASEMBC(0x116) CASEMBC(0x118)
852 CASEMBC(0x11a) CASEMBC(0x1eba) CASEMBC(0x1ebc)
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200853 regmbc('E'); regmbc(0xc8); regmbc(0xc9);
854 regmbc(0xca); regmbc(0xcb);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200855 REGMBC(0x112) REGMBC(0x114) REGMBC(0x116)
856 REGMBC(0x118) REGMBC(0x11a) REGMBC(0x1eba)
857 REGMBC(0x1ebc)
858 return;
859 case 'F': CASEMBC(0x1e1e)
860 regmbc('F'); REGMBC(0x1e1e)
861 return;
862 case 'G': CASEMBC(0x11c) CASEMBC(0x11e) CASEMBC(0x120)
863 CASEMBC(0x122) CASEMBC(0x1e4) CASEMBC(0x1e6) CASEMBC(0x1f4)
864 CASEMBC(0x1e20)
865 regmbc('G'); REGMBC(0x11c) REGMBC(0x11e)
866 REGMBC(0x120) REGMBC(0x122) REGMBC(0x1e4)
867 REGMBC(0x1e6) REGMBC(0x1f4) REGMBC(0x1e20)
868 return;
869 case 'H': CASEMBC(0x124) CASEMBC(0x126) CASEMBC(0x1e22)
870 CASEMBC(0x1e26) CASEMBC(0x1e28)
871 regmbc('H'); REGMBC(0x124) REGMBC(0x126)
872 REGMBC(0x1e22) REGMBC(0x1e26) REGMBC(0x1e28)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000873 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200874 case 'I': case 0xcc: case 0xcd: case 0xce: case 0xcf:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200875 CASEMBC(0x128) CASEMBC(0x12a) CASEMBC(0x12c) CASEMBC(0x12e)
876 CASEMBC(0x130) CASEMBC(0x1cf) CASEMBC(0x1ec8)
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200877 regmbc('I'); regmbc(0xcc); regmbc(0xcd);
878 regmbc(0xce); regmbc(0xcf);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200879 REGMBC(0x128) REGMBC(0x12a) REGMBC(0x12c)
880 REGMBC(0x12e) REGMBC(0x130) REGMBC(0x1cf)
881 REGMBC(0x1ec8)
882 return;
883 case 'J': CASEMBC(0x134)
884 regmbc('J'); REGMBC(0x134)
885 return;
886 case 'K': CASEMBC(0x136) CASEMBC(0x1e8) CASEMBC(0x1e30)
887 CASEMBC(0x1e34)
888 regmbc('K'); REGMBC(0x136) REGMBC(0x1e8)
889 REGMBC(0x1e30) REGMBC(0x1e34)
890 return;
891 case 'L': CASEMBC(0x139) CASEMBC(0x13b) CASEMBC(0x13d)
892 CASEMBC(0x13f) CASEMBC(0x141) CASEMBC(0x1e3a)
893 regmbc('L'); REGMBC(0x139) REGMBC(0x13b)
894 REGMBC(0x13d) REGMBC(0x13f) REGMBC(0x141)
895 REGMBC(0x1e3a)
896 return;
897 case 'M': CASEMBC(0x1e3e) CASEMBC(0x1e40)
898 regmbc('M'); REGMBC(0x1e3e) REGMBC(0x1e40)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000899 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200900 case 'N': case 0xd1:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200901 CASEMBC(0x143) CASEMBC(0x145) CASEMBC(0x147) CASEMBC(0x1e44)
902 CASEMBC(0x1e48)
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200903 regmbc('N'); regmbc(0xd1);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200904 REGMBC(0x143) REGMBC(0x145) REGMBC(0x147)
905 REGMBC(0x1e44) REGMBC(0x1e48)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000906 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200907 case 'O': case 0xd2: case 0xd3: case 0xd4: case 0xd5:
908 case 0xd6: case 0xd8:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200909 CASEMBC(0x14c) CASEMBC(0x14e) CASEMBC(0x150) CASEMBC(0x1a0)
910 CASEMBC(0x1d1) CASEMBC(0x1ea) CASEMBC(0x1ec) CASEMBC(0x1ece)
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200911 regmbc('O'); regmbc(0xd2); regmbc(0xd3);
912 regmbc(0xd4); regmbc(0xd5); regmbc(0xd6);
913 regmbc(0xd8);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200914 REGMBC(0x14c) REGMBC(0x14e) REGMBC(0x150)
915 REGMBC(0x1a0) REGMBC(0x1d1) REGMBC(0x1ea)
916 REGMBC(0x1ec) REGMBC(0x1ece)
917 return;
918 case 'P': case 0x1e54: case 0x1e56:
919 regmbc('P'); REGMBC(0x1e54) REGMBC(0x1e56)
920 return;
921 case 'R': CASEMBC(0x154) CASEMBC(0x156) CASEMBC(0x158)
922 CASEMBC(0x1e58) CASEMBC(0x1e5e)
923 regmbc('R'); REGMBC(0x154) REGMBC(0x156) REGMBC(0x158)
924 REGMBC(0x1e58) REGMBC(0x1e5e)
925 return;
926 case 'S': CASEMBC(0x15a) CASEMBC(0x15c) CASEMBC(0x15e)
927 CASEMBC(0x160) CASEMBC(0x1e60)
928 regmbc('S'); REGMBC(0x15a) REGMBC(0x15c)
929 REGMBC(0x15e) REGMBC(0x160) REGMBC(0x1e60)
930 return;
931 case 'T': CASEMBC(0x162) CASEMBC(0x164) CASEMBC(0x166)
932 CASEMBC(0x1e6a) CASEMBC(0x1e6e)
933 regmbc('T'); REGMBC(0x162) REGMBC(0x164)
934 REGMBC(0x166) REGMBC(0x1e6a) REGMBC(0x1e6e)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000935 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200936 case 'U': case 0xd9: case 0xda: case 0xdb: case 0xdc:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200937 CASEMBC(0x168) CASEMBC(0x16a) CASEMBC(0x16c) CASEMBC(0x16e)
938 CASEMBC(0x170) CASEMBC(0x172) CASEMBC(0x1af) CASEMBC(0x1d3)
939 CASEMBC(0x1ee6)
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200940 regmbc('U'); regmbc(0xd9); regmbc(0xda);
941 regmbc(0xdb); regmbc(0xdc);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200942 REGMBC(0x168) REGMBC(0x16a) REGMBC(0x16c)
943 REGMBC(0x16e) REGMBC(0x170) REGMBC(0x172)
944 REGMBC(0x1af) REGMBC(0x1d3) REGMBC(0x1ee6)
945 return;
946 case 'V': CASEMBC(0x1e7c)
947 regmbc('V'); REGMBC(0x1e7c)
948 return;
949 case 'W': CASEMBC(0x174) CASEMBC(0x1e80) CASEMBC(0x1e82)
950 CASEMBC(0x1e84) CASEMBC(0x1e86)
951 regmbc('W'); REGMBC(0x174) REGMBC(0x1e80)
952 REGMBC(0x1e82) REGMBC(0x1e84) REGMBC(0x1e86)
953 return;
954 case 'X': CASEMBC(0x1e8a) CASEMBC(0x1e8c)
955 regmbc('X'); REGMBC(0x1e8a) REGMBC(0x1e8c)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000956 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200957 case 'Y': case 0xdd:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200958 CASEMBC(0x176) CASEMBC(0x178) CASEMBC(0x1e8e) CASEMBC(0x1ef2)
959 CASEMBC(0x1ef6) CASEMBC(0x1ef8)
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200960 regmbc('Y'); regmbc(0xdd);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200961 REGMBC(0x176) REGMBC(0x178) REGMBC(0x1e8e)
962 REGMBC(0x1ef2) REGMBC(0x1ef6) REGMBC(0x1ef8)
963 return;
964 case 'Z': CASEMBC(0x179) CASEMBC(0x17b) CASEMBC(0x17d)
965 CASEMBC(0x1b5) CASEMBC(0x1e90) CASEMBC(0x1e94)
966 regmbc('Z'); REGMBC(0x179) REGMBC(0x17b)
967 REGMBC(0x17d) REGMBC(0x1b5) REGMBC(0x1e90)
968 REGMBC(0x1e94)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000969 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200970 case 'a': case 0xe0: case 0xe1: case 0xe2:
971 case 0xe3: case 0xe4: case 0xe5:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200972 CASEMBC(0x101) CASEMBC(0x103) CASEMBC(0x105) CASEMBC(0x1ce)
973 CASEMBC(0x1df) CASEMBC(0x1e1) CASEMBC(0x1ea3)
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200974 regmbc('a'); regmbc(0xe0); regmbc(0xe1);
975 regmbc(0xe2); regmbc(0xe3); regmbc(0xe4);
976 regmbc(0xe5);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200977 REGMBC(0x101) REGMBC(0x103) REGMBC(0x105)
978 REGMBC(0x1ce) REGMBC(0x1df) REGMBC(0x1e1)
979 REGMBC(0x1ea3)
980 return;
981 case 'b': CASEMBC(0x1e03) CASEMBC(0x1e07)
982 regmbc('b'); REGMBC(0x1e03) REGMBC(0x1e07)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000983 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200984 case 'c': case 0xe7:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200985 CASEMBC(0x107) CASEMBC(0x109) CASEMBC(0x10b) CASEMBC(0x10d)
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200986 regmbc('c'); regmbc(0xe7);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200987 REGMBC(0x107) REGMBC(0x109) REGMBC(0x10b)
988 REGMBC(0x10d)
989 return;
Bram Moolenaar2c61ec62015-07-10 19:16:34 +0200990 case 'd': CASEMBC(0x10f) CASEMBC(0x111) CASEMBC(0x1e0b)
991 CASEMBC(0x1e0f) CASEMBC(0x1e11)
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200992 regmbc('d'); REGMBC(0x10f) REGMBC(0x111)
Bram Moolenaar2c61ec62015-07-10 19:16:34 +0200993 REGMBC(0x1e0b) REGMBC(0x1e0f) REGMBC(0x1e11)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000994 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200995 case 'e': case 0xe8: case 0xe9: case 0xea: case 0xeb:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200996 CASEMBC(0x113) CASEMBC(0x115) CASEMBC(0x117) CASEMBC(0x119)
997 CASEMBC(0x11b) CASEMBC(0x1ebb) CASEMBC(0x1ebd)
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200998 regmbc('e'); regmbc(0xe8); regmbc(0xe9);
999 regmbc(0xea); regmbc(0xeb);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001000 REGMBC(0x113) REGMBC(0x115) REGMBC(0x117)
1001 REGMBC(0x119) REGMBC(0x11b) REGMBC(0x1ebb)
1002 REGMBC(0x1ebd)
1003 return;
1004 case 'f': CASEMBC(0x1e1f)
1005 regmbc('f'); REGMBC(0x1e1f)
1006 return;
1007 case 'g': CASEMBC(0x11d) CASEMBC(0x11f) CASEMBC(0x121)
1008 CASEMBC(0x123) CASEMBC(0x1e5) CASEMBC(0x1e7) CASEMBC(0x1f5)
1009 CASEMBC(0x1e21)
1010 regmbc('g'); REGMBC(0x11d) REGMBC(0x11f)
1011 REGMBC(0x121) REGMBC(0x123) REGMBC(0x1e5)
1012 REGMBC(0x1e7) REGMBC(0x1f5) REGMBC(0x1e21)
1013 return;
1014 case 'h': CASEMBC(0x125) CASEMBC(0x127) CASEMBC(0x1e23)
1015 CASEMBC(0x1e27) CASEMBC(0x1e29) CASEMBC(0x1e96)
1016 regmbc('h'); REGMBC(0x125) REGMBC(0x127)
1017 REGMBC(0x1e23) REGMBC(0x1e27) REGMBC(0x1e29)
1018 REGMBC(0x1e96)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001019 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001020 case 'i': case 0xec: case 0xed: case 0xee: case 0xef:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001021 CASEMBC(0x129) CASEMBC(0x12b) CASEMBC(0x12d) CASEMBC(0x12f)
1022 CASEMBC(0x1d0) CASEMBC(0x1ec9)
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001023 regmbc('i'); regmbc(0xec); regmbc(0xed);
1024 regmbc(0xee); regmbc(0xef);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001025 REGMBC(0x129) REGMBC(0x12b) REGMBC(0x12d)
1026 REGMBC(0x12f) REGMBC(0x1d0) REGMBC(0x1ec9)
1027 return;
1028 case 'j': CASEMBC(0x135) CASEMBC(0x1f0)
1029 regmbc('j'); REGMBC(0x135) REGMBC(0x1f0)
1030 return;
1031 case 'k': CASEMBC(0x137) CASEMBC(0x1e9) CASEMBC(0x1e31)
1032 CASEMBC(0x1e35)
1033 regmbc('k'); REGMBC(0x137) REGMBC(0x1e9)
1034 REGMBC(0x1e31) REGMBC(0x1e35)
1035 return;
1036 case 'l': CASEMBC(0x13a) CASEMBC(0x13c) CASEMBC(0x13e)
1037 CASEMBC(0x140) CASEMBC(0x142) CASEMBC(0x1e3b)
1038 regmbc('l'); REGMBC(0x13a) REGMBC(0x13c)
1039 REGMBC(0x13e) REGMBC(0x140) REGMBC(0x142)
1040 REGMBC(0x1e3b)
1041 return;
1042 case 'm': CASEMBC(0x1e3f) CASEMBC(0x1e41)
1043 regmbc('m'); REGMBC(0x1e3f) REGMBC(0x1e41)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001044 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001045 case 'n': case 0xf1:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001046 CASEMBC(0x144) CASEMBC(0x146) CASEMBC(0x148) CASEMBC(0x149)
1047 CASEMBC(0x1e45) CASEMBC(0x1e49)
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001048 regmbc('n'); regmbc(0xf1);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001049 REGMBC(0x144) REGMBC(0x146) REGMBC(0x148)
1050 REGMBC(0x149) REGMBC(0x1e45) REGMBC(0x1e49)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001051 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001052 case 'o': case 0xf2: case 0xf3: case 0xf4: case 0xf5:
1053 case 0xf6: case 0xf8:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001054 CASEMBC(0x14d) CASEMBC(0x14f) CASEMBC(0x151) CASEMBC(0x1a1)
1055 CASEMBC(0x1d2) CASEMBC(0x1eb) CASEMBC(0x1ed) CASEMBC(0x1ecf)
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001056 regmbc('o'); regmbc(0xf2); regmbc(0xf3);
1057 regmbc(0xf4); regmbc(0xf5); regmbc(0xf6);
1058 regmbc(0xf8);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001059 REGMBC(0x14d) REGMBC(0x14f) REGMBC(0x151)
1060 REGMBC(0x1a1) REGMBC(0x1d2) REGMBC(0x1eb)
1061 REGMBC(0x1ed) REGMBC(0x1ecf)
1062 return;
1063 case 'p': CASEMBC(0x1e55) CASEMBC(0x1e57)
1064 regmbc('p'); REGMBC(0x1e55) REGMBC(0x1e57)
1065 return;
1066 case 'r': CASEMBC(0x155) CASEMBC(0x157) CASEMBC(0x159)
1067 CASEMBC(0x1e59) CASEMBC(0x1e5f)
1068 regmbc('r'); REGMBC(0x155) REGMBC(0x157) REGMBC(0x159)
1069 REGMBC(0x1e59) REGMBC(0x1e5f)
1070 return;
1071 case 's': CASEMBC(0x15b) CASEMBC(0x15d) CASEMBC(0x15f)
1072 CASEMBC(0x161) CASEMBC(0x1e61)
1073 regmbc('s'); REGMBC(0x15b) REGMBC(0x15d)
1074 REGMBC(0x15f) REGMBC(0x161) REGMBC(0x1e61)
1075 return;
1076 case 't': CASEMBC(0x163) CASEMBC(0x165) CASEMBC(0x167)
1077 CASEMBC(0x1e6b) CASEMBC(0x1e6f) CASEMBC(0x1e97)
1078 regmbc('t'); REGMBC(0x163) REGMBC(0x165) REGMBC(0x167)
1079 REGMBC(0x1e6b) REGMBC(0x1e6f) REGMBC(0x1e97)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001080 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001081 case 'u': case 0xf9: case 0xfa: case 0xfb: case 0xfc:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001082 CASEMBC(0x169) CASEMBC(0x16b) CASEMBC(0x16d) CASEMBC(0x16f)
1083 CASEMBC(0x171) CASEMBC(0x173) CASEMBC(0x1b0) CASEMBC(0x1d4)
1084 CASEMBC(0x1ee7)
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001085 regmbc('u'); regmbc(0xf9); regmbc(0xfa);
1086 regmbc(0xfb); regmbc(0xfc);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001087 REGMBC(0x169) REGMBC(0x16b) REGMBC(0x16d)
1088 REGMBC(0x16f) REGMBC(0x171) REGMBC(0x173)
1089 REGMBC(0x1b0) REGMBC(0x1d4) REGMBC(0x1ee7)
1090 return;
1091 case 'v': CASEMBC(0x1e7d)
1092 regmbc('v'); REGMBC(0x1e7d)
1093 return;
1094 case 'w': CASEMBC(0x175) CASEMBC(0x1e81) CASEMBC(0x1e83)
1095 CASEMBC(0x1e85) CASEMBC(0x1e87) CASEMBC(0x1e98)
1096 regmbc('w'); REGMBC(0x175) REGMBC(0x1e81)
1097 REGMBC(0x1e83) REGMBC(0x1e85) REGMBC(0x1e87)
1098 REGMBC(0x1e98)
1099 return;
1100 case 'x': CASEMBC(0x1e8b) CASEMBC(0x1e8d)
1101 regmbc('x'); REGMBC(0x1e8b) REGMBC(0x1e8d)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001102 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001103 case 'y': case 0xfd: case 0xff:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001104 CASEMBC(0x177) CASEMBC(0x1e8f) CASEMBC(0x1e99)
1105 CASEMBC(0x1ef3) CASEMBC(0x1ef7) CASEMBC(0x1ef9)
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001106 regmbc('y'); regmbc(0xfd); regmbc(0xff);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001107 REGMBC(0x177) REGMBC(0x1e8f) REGMBC(0x1e99)
1108 REGMBC(0x1ef3) REGMBC(0x1ef7) REGMBC(0x1ef9)
1109 return;
1110 case 'z': CASEMBC(0x17a) CASEMBC(0x17c) CASEMBC(0x17e)
1111 CASEMBC(0x1b6) CASEMBC(0x1e91) CASEMBC(0x1e95)
1112 regmbc('z'); REGMBC(0x17a) REGMBC(0x17c)
1113 REGMBC(0x17e) REGMBC(0x1b6) REGMBC(0x1e91)
1114 REGMBC(0x1e95)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001115 return;
1116 }
Bram Moolenaar2c704a72010-06-03 21:17:25 +02001117#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00001118 }
1119 regmbc(c);
1120}
1121
1122/*
1123 * Check for a collating element "[.a.]". "pp" points to the '['.
1124 * Returns a character. Zero means that no item was recognized. Otherwise
1125 * "pp" is advanced to after the item.
1126 * Currently only single characters are recognized!
1127 */
1128 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001129get_coll_element(char_u **pp)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001130{
1131 int c;
1132 int l = 1;
1133 char_u *p = *pp;
1134
Bram Moolenaarb878bbb2015-06-09 20:39:24 +02001135 if (p[0] != NUL && p[1] == '.')
Bram Moolenaardf177f62005-02-22 08:39:57 +00001136 {
1137#ifdef FEAT_MBYTE
1138 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001139 l = (*mb_ptr2len)(p + 2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00001140#endif
1141 if (p[l + 2] == '.' && p[l + 3] == ']')
1142 {
1143#ifdef FEAT_MBYTE
1144 if (has_mbyte)
1145 c = mb_ptr2char(p + 2);
1146 else
1147#endif
1148 c = p[2];
1149 *pp += l + 4;
1150 return c;
1151 }
1152 }
1153 return 0;
1154}
1155
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001156static int reg_cpo_lit; /* 'cpoptions' contains 'l' flag */
1157static int reg_cpo_bsl; /* 'cpoptions' contains '\' flag */
1158
1159 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001160get_cpo_flags(void)
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001161{
1162 reg_cpo_lit = vim_strchr(p_cpo, CPO_LITERAL) != NULL;
1163 reg_cpo_bsl = vim_strchr(p_cpo, CPO_BACKSL) != NULL;
1164}
Bram Moolenaardf177f62005-02-22 08:39:57 +00001165
1166/*
1167 * Skip over a "[]" range.
1168 * "p" must point to the character after the '['.
1169 * The returned pointer is on the matching ']', or the terminating NUL.
1170 */
1171 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001172skip_anyof(char_u *p)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001173{
Bram Moolenaardf177f62005-02-22 08:39:57 +00001174#ifdef FEAT_MBYTE
1175 int l;
1176#endif
1177
Bram Moolenaardf177f62005-02-22 08:39:57 +00001178 if (*p == '^') /* Complement of range. */
1179 ++p;
1180 if (*p == ']' || *p == '-')
1181 ++p;
1182 while (*p != NUL && *p != ']')
1183 {
1184#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001185 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001186 p += l;
1187 else
1188#endif
1189 if (*p == '-')
1190 {
1191 ++p;
1192 if (*p != ']' && *p != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001193 MB_PTR_ADV(p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00001194 }
1195 else if (*p == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001196 && !reg_cpo_bsl
Bram Moolenaardf177f62005-02-22 08:39:57 +00001197 && (vim_strchr(REGEXP_INRANGE, p[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001198 || (!reg_cpo_lit && vim_strchr(REGEXP_ABBR, p[1]) != NULL)))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001199 p += 2;
1200 else if (*p == '[')
1201 {
1202 if (get_char_class(&p) == CLASS_NONE
1203 && get_equi_class(&p) == 0
Bram Moolenaarb878bbb2015-06-09 20:39:24 +02001204 && get_coll_element(&p) == 0
1205 && *p != NUL)
1206 ++p; /* it is not a class name and not NUL */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001207 }
1208 else
1209 ++p;
1210 }
1211
1212 return p;
1213}
1214
1215/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 * Skip past regular expression.
Bram Moolenaar748bf032005-02-02 23:04:36 +00001217 * Stop at end of "startp" or where "dirc" is found ('/', '?', etc).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 * Take care of characters with a backslash in front of it.
1219 * Skip strings inside [ and ].
1220 * When "newp" is not NULL and "dirc" is '?', make an allocated copy of the
1221 * expression and change "\?" to "?". If "*newp" is not NULL the expression
1222 * is changed in-place.
1223 */
1224 char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001225skip_regexp(
1226 char_u *startp,
1227 int dirc,
1228 int magic,
1229 char_u **newp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230{
1231 int mymagic;
1232 char_u *p = startp;
1233
1234 if (magic)
1235 mymagic = MAGIC_ON;
1236 else
1237 mymagic = MAGIC_OFF;
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001238 get_cpo_flags();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001240 for (; p[0] != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 {
1242 if (p[0] == dirc) /* found end of regexp */
1243 break;
1244 if ((p[0] == '[' && mymagic >= MAGIC_ON)
1245 || (p[0] == '\\' && p[1] == '[' && mymagic <= MAGIC_OFF))
1246 {
1247 p = skip_anyof(p + 1);
1248 if (p[0] == NUL)
1249 break;
1250 }
1251 else if (p[0] == '\\' && p[1] != NUL)
1252 {
1253 if (dirc == '?' && newp != NULL && p[1] == '?')
1254 {
1255 /* change "\?" to "?", make a copy first. */
1256 if (*newp == NULL)
1257 {
1258 *newp = vim_strsave(startp);
1259 if (*newp != NULL)
1260 p = *newp + (p - startp);
1261 }
1262 if (*newp != NULL)
Bram Moolenaar446cb832008-06-24 21:56:24 +00001263 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 else
1265 ++p;
1266 }
1267 else
1268 ++p; /* skip next character */
1269 if (*p == 'v')
1270 mymagic = MAGIC_ALL;
1271 else if (*p == 'V')
1272 mymagic = MAGIC_NONE;
1273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 }
1275 return p;
1276}
1277
Bram Moolenaar1ef9bbe2017-06-17 20:08:20 +02001278/*
1279 * Return TRUE if the back reference is legal. We must have seen the close
1280 * brace.
1281 * TODO: Should also check that we don't refer to something that is repeated
1282 * (+*=): what instance of the repetition should we match?
1283 */
1284 static int
1285seen_endbrace(int refnum)
1286{
1287 if (!had_endbrace[refnum])
1288 {
1289 char_u *p;
1290
1291 /* Trick: check if "@<=" or "@<!" follows, in which case
1292 * the \1 can appear before the referenced match. */
1293 for (p = regparse; *p != NUL; ++p)
1294 if (p[0] == '@' && p[1] == '<' && (p[2] == '!' || p[2] == '='))
1295 break;
1296 if (*p == NUL)
1297 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001298 emsg(_("E65: Illegal back reference"));
Bram Moolenaar1ef9bbe2017-06-17 20:08:20 +02001299 rc_did_emsg = TRUE;
1300 return FALSE;
1301 }
1302 }
1303 return TRUE;
1304}
1305
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001307 * bt_regcomp() - compile a regular expression into internal code for the
1308 * traditional back track matcher.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001309 * Returns the program in allocated space. Returns NULL for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 *
1311 * We can't allocate space until we know how big the compiled form will be,
1312 * but we can't compile it (and thus know how big it is) until we've got a
1313 * place to put the code. So we cheat: we compile it twice, once with code
1314 * generation turned off and size counting turned on, and once "for real".
1315 * This also means that we don't allocate space until we are sure that the
1316 * thing really will compile successfully, and we never have to move the
1317 * code and thus invalidate pointers into it. (Note that it has to be in
1318 * one piece because vim_free() must be able to free it all.)
1319 *
1320 * Whether upper/lower case is to be ignored is decided when executing the
1321 * program, it does not matter here.
1322 *
1323 * Beware that the optimization-preparation code in here knows about some
1324 * of the structure of the compiled regexp.
1325 * "re_flags": RE_MAGIC and/or RE_STRING.
1326 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001327 static regprog_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001328bt_regcomp(char_u *expr, int re_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001330 bt_regprog_T *r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 char_u *scan;
1332 char_u *longest;
1333 int len;
1334 int flags;
1335
1336 if (expr == NULL)
1337 EMSG_RET_NULL(_(e_null));
1338
1339 init_class_tab();
1340
1341 /*
1342 * First pass: determine size, legality.
1343 */
1344 regcomp_start(expr, re_flags);
1345 regcode = JUST_CALC_SIZE;
1346 regc(REGMAGIC);
1347 if (reg(REG_NOPAREN, &flags) == NULL)
1348 return NULL;
1349
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 /* Allocate space. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001351 r = (bt_regprog_T *)lalloc(sizeof(bt_regprog_T) + regsize, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 if (r == NULL)
1353 return NULL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02001354 r->re_in_use = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355
1356 /*
1357 * Second pass: emit code.
1358 */
1359 regcomp_start(expr, re_flags);
1360 regcode = r->program;
1361 regc(REGMAGIC);
Bram Moolenaard3005802009-11-25 17:21:32 +00001362 if (reg(REG_NOPAREN, &flags) == NULL || reg_toolong)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 {
1364 vim_free(r);
Bram Moolenaard3005802009-11-25 17:21:32 +00001365 if (reg_toolong)
1366 EMSG_RET_NULL(_("E339: Pattern too long"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 return NULL;
1368 }
1369
1370 /* Dig out information for optimizations. */
1371 r->regstart = NUL; /* Worst-case defaults. */
1372 r->reganch = 0;
1373 r->regmust = NULL;
1374 r->regmlen = 0;
1375 r->regflags = regflags;
1376 if (flags & HASNL)
1377 r->regflags |= RF_HASNL;
1378 if (flags & HASLOOKBH)
1379 r->regflags |= RF_LOOKBH;
1380#ifdef FEAT_SYN_HL
1381 /* Remember whether this pattern has any \z specials in it. */
1382 r->reghasz = re_has_z;
1383#endif
1384 scan = r->program + 1; /* First BRANCH. */
1385 if (OP(regnext(scan)) == END) /* Only one top-level choice. */
1386 {
1387 scan = OPERAND(scan);
1388
1389 /* Starting-point info. */
1390 if (OP(scan) == BOL || OP(scan) == RE_BOF)
1391 {
1392 r->reganch++;
1393 scan = regnext(scan);
1394 }
1395
1396 if (OP(scan) == EXACTLY)
1397 {
1398#ifdef FEAT_MBYTE
1399 if (has_mbyte)
1400 r->regstart = (*mb_ptr2char)(OPERAND(scan));
1401 else
1402#endif
1403 r->regstart = *OPERAND(scan);
1404 }
1405 else if ((OP(scan) == BOW
1406 || OP(scan) == EOW
1407 || OP(scan) == NOTHING
1408 || OP(scan) == MOPEN + 0 || OP(scan) == NOPEN
1409 || OP(scan) == MCLOSE + 0 || OP(scan) == NCLOSE)
1410 && OP(regnext(scan)) == EXACTLY)
1411 {
1412#ifdef FEAT_MBYTE
1413 if (has_mbyte)
1414 r->regstart = (*mb_ptr2char)(OPERAND(regnext(scan)));
1415 else
1416#endif
1417 r->regstart = *OPERAND(regnext(scan));
1418 }
1419
1420 /*
1421 * If there's something expensive in the r.e., find the longest
1422 * literal string that must appear and make it the regmust. Resolve
1423 * ties in favor of later strings, since the regstart check works
1424 * with the beginning of the r.e. and avoiding duplication
1425 * strengthens checking. Not a strong reason, but sufficient in the
1426 * absence of others.
1427 */
1428 /*
1429 * When the r.e. starts with BOW, it is faster to look for a regmust
1430 * first. Used a lot for "#" and "*" commands. (Added by mool).
1431 */
1432 if ((flags & SPSTART || OP(scan) == BOW || OP(scan) == EOW)
1433 && !(flags & HASNL))
1434 {
1435 longest = NULL;
1436 len = 0;
1437 for (; scan != NULL; scan = regnext(scan))
1438 if (OP(scan) == EXACTLY && STRLEN(OPERAND(scan)) >= (size_t)len)
1439 {
1440 longest = OPERAND(scan);
1441 len = (int)STRLEN(OPERAND(scan));
1442 }
1443 r->regmust = longest;
1444 r->regmlen = len;
1445 }
1446 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001447#ifdef BT_REGEXP_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 regdump(expr, r);
1449#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001450 r->engine = &bt_regengine;
1451 return (regprog_T *)r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452}
1453
1454/*
Bram Moolenaar473de612013-06-08 18:19:48 +02001455 * Free a compiled regexp program, returned by bt_regcomp().
1456 */
1457 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001458bt_regfree(regprog_T *prog)
Bram Moolenaar473de612013-06-08 18:19:48 +02001459{
1460 vim_free(prog);
1461}
1462
1463/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 * Setup to parse the regexp. Used once to get the length and once to do it.
1465 */
1466 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001467regcomp_start(
1468 char_u *expr,
1469 int re_flags) /* see vim_regcomp() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470{
1471 initchr(expr);
1472 if (re_flags & RE_MAGIC)
1473 reg_magic = MAGIC_ON;
1474 else
1475 reg_magic = MAGIC_OFF;
1476 reg_string = (re_flags & RE_STRING);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001477 reg_strict = (re_flags & RE_STRICT);
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001478 get_cpo_flags();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479
1480 num_complex_braces = 0;
1481 regnpar = 1;
1482 vim_memset(had_endbrace, 0, sizeof(had_endbrace));
1483#ifdef FEAT_SYN_HL
1484 regnzpar = 1;
1485 re_has_z = 0;
1486#endif
1487 regsize = 0L;
Bram Moolenaard3005802009-11-25 17:21:32 +00001488 reg_toolong = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 regflags = 0;
1490#if defined(FEAT_SYN_HL) || defined(PROTO)
1491 had_eol = FALSE;
1492#endif
1493}
1494
1495#if defined(FEAT_SYN_HL) || defined(PROTO)
1496/*
1497 * Check if during the previous call to vim_regcomp the EOL item "$" has been
1498 * found. This is messy, but it works fine.
1499 */
1500 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001501vim_regcomp_had_eol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502{
1503 return had_eol;
1504}
1505#endif
1506
Bram Moolenaar0270f382018-07-17 05:43:58 +02001507// variables used for parsing
1508static int at_start; // True when on the first character
1509static int prev_at_start; // True when on the second character
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001510
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001512 * Parse regular expression, i.e. main body or parenthesized thing.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 *
1514 * Caller must absorb opening parenthesis.
1515 *
1516 * Combining parenthesis handling with the base level of regular expression
1517 * is a trifle forced, but the need to tie the tails of the branches to what
1518 * follows makes it hard to avoid.
1519 */
1520 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001521reg(
1522 int paren, /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1523 int *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524{
1525 char_u *ret;
1526 char_u *br;
1527 char_u *ender;
1528 int parno = 0;
1529 int flags;
1530
1531 *flagp = HASWIDTH; /* Tentatively. */
1532
1533#ifdef FEAT_SYN_HL
1534 if (paren == REG_ZPAREN)
1535 {
1536 /* Make a ZOPEN node. */
1537 if (regnzpar >= NSUBEXP)
1538 EMSG_RET_NULL(_("E50: Too many \\z("));
1539 parno = regnzpar;
1540 regnzpar++;
1541 ret = regnode(ZOPEN + parno);
1542 }
1543 else
1544#endif
1545 if (paren == REG_PAREN)
1546 {
1547 /* Make a MOPEN node. */
1548 if (regnpar >= NSUBEXP)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001549 EMSG2_RET_NULL(_("E51: Too many %s("), reg_magic == MAGIC_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 parno = regnpar;
1551 ++regnpar;
1552 ret = regnode(MOPEN + parno);
1553 }
1554 else if (paren == REG_NPAREN)
1555 {
1556 /* Make a NOPEN node. */
1557 ret = regnode(NOPEN);
1558 }
1559 else
1560 ret = NULL;
1561
1562 /* Pick up the branches, linking them together. */
1563 br = regbranch(&flags);
1564 if (br == NULL)
1565 return NULL;
1566 if (ret != NULL)
1567 regtail(ret, br); /* [MZ]OPEN -> first. */
1568 else
1569 ret = br;
1570 /* If one of the branches can be zero-width, the whole thing can.
1571 * If one of the branches has * at start or matches a line-break, the
1572 * whole thing can. */
1573 if (!(flags & HASWIDTH))
1574 *flagp &= ~HASWIDTH;
1575 *flagp |= flags & (SPSTART | HASNL | HASLOOKBH);
1576 while (peekchr() == Magic('|'))
1577 {
1578 skipchr();
1579 br = regbranch(&flags);
Bram Moolenaard3005802009-11-25 17:21:32 +00001580 if (br == NULL || reg_toolong)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 return NULL;
1582 regtail(ret, br); /* BRANCH -> BRANCH. */
1583 if (!(flags & HASWIDTH))
1584 *flagp &= ~HASWIDTH;
1585 *flagp |= flags & (SPSTART | HASNL | HASLOOKBH);
1586 }
1587
1588 /* Make a closing node, and hook it on the end. */
1589 ender = regnode(
1590#ifdef FEAT_SYN_HL
1591 paren == REG_ZPAREN ? ZCLOSE + parno :
1592#endif
1593 paren == REG_PAREN ? MCLOSE + parno :
1594 paren == REG_NPAREN ? NCLOSE : END);
1595 regtail(ret, ender);
1596
1597 /* Hook the tails of the branches to the closing node. */
1598 for (br = ret; br != NULL; br = regnext(br))
1599 regoptail(br, ender);
1600
1601 /* Check for proper termination. */
1602 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1603 {
1604#ifdef FEAT_SYN_HL
1605 if (paren == REG_ZPAREN)
Bram Moolenaar45eeb132005-06-06 21:59:07 +00001606 EMSG_RET_NULL(_("E52: Unmatched \\z("));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 else
1608#endif
1609 if (paren == REG_NPAREN)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001610 EMSG2_RET_NULL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001612 EMSG2_RET_NULL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 }
1614 else if (paren == REG_NOPAREN && peekchr() != NUL)
1615 {
1616 if (curchr == Magic(')'))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001617 EMSG2_RET_NULL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 else
Bram Moolenaar45eeb132005-06-06 21:59:07 +00001619 EMSG_RET_NULL(_(e_trailing)); /* "Can't happen". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 /* NOTREACHED */
1621 }
1622 /*
1623 * Here we set the flag allowing back references to this set of
1624 * parentheses.
1625 */
1626 if (paren == REG_PAREN)
1627 had_endbrace[parno] = TRUE; /* have seen the close paren */
1628 return ret;
1629}
1630
1631/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001632 * Parse one alternative of an | operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 * Implements the & operator.
1634 */
1635 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001636regbranch(int *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637{
1638 char_u *ret;
1639 char_u *chain = NULL;
1640 char_u *latest;
1641 int flags;
1642
1643 *flagp = WORST | HASNL; /* Tentatively. */
1644
1645 ret = regnode(BRANCH);
1646 for (;;)
1647 {
1648 latest = regconcat(&flags);
1649 if (latest == NULL)
1650 return NULL;
1651 /* If one of the branches has width, the whole thing has. If one of
1652 * the branches anchors at start-of-line, the whole thing does.
1653 * If one of the branches uses look-behind, the whole thing does. */
1654 *flagp |= flags & (HASWIDTH | SPSTART | HASLOOKBH);
1655 /* If one of the branches doesn't match a line-break, the whole thing
1656 * doesn't. */
1657 *flagp &= ~HASNL | (flags & HASNL);
1658 if (chain != NULL)
1659 regtail(chain, latest);
1660 if (peekchr() != Magic('&'))
1661 break;
1662 skipchr();
1663 regtail(latest, regnode(END)); /* operand ends */
Bram Moolenaard3005802009-11-25 17:21:32 +00001664 if (reg_toolong)
1665 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 reginsert(MATCH, latest);
1667 chain = latest;
1668 }
1669
1670 return ret;
1671}
1672
1673/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001674 * Parse one alternative of an | or & operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 * Implements the concatenation operator.
1676 */
1677 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001678regconcat(int *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679{
1680 char_u *first = NULL;
1681 char_u *chain = NULL;
1682 char_u *latest;
1683 int flags;
1684 int cont = TRUE;
1685
1686 *flagp = WORST; /* Tentatively. */
1687
1688 while (cont)
1689 {
1690 switch (peekchr())
1691 {
1692 case NUL:
1693 case Magic('|'):
1694 case Magic('&'):
1695 case Magic(')'):
1696 cont = FALSE;
1697 break;
1698 case Magic('Z'):
1699#ifdef FEAT_MBYTE
1700 regflags |= RF_ICOMBINE;
1701#endif
1702 skipchr_keepstart();
1703 break;
1704 case Magic('c'):
1705 regflags |= RF_ICASE;
1706 skipchr_keepstart();
1707 break;
1708 case Magic('C'):
1709 regflags |= RF_NOICASE;
1710 skipchr_keepstart();
1711 break;
1712 case Magic('v'):
1713 reg_magic = MAGIC_ALL;
1714 skipchr_keepstart();
1715 curchr = -1;
1716 break;
1717 case Magic('m'):
1718 reg_magic = MAGIC_ON;
1719 skipchr_keepstart();
1720 curchr = -1;
1721 break;
1722 case Magic('M'):
1723 reg_magic = MAGIC_OFF;
1724 skipchr_keepstart();
1725 curchr = -1;
1726 break;
1727 case Magic('V'):
1728 reg_magic = MAGIC_NONE;
1729 skipchr_keepstart();
1730 curchr = -1;
1731 break;
1732 default:
1733 latest = regpiece(&flags);
Bram Moolenaard3005802009-11-25 17:21:32 +00001734 if (latest == NULL || reg_toolong)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 return NULL;
1736 *flagp |= flags & (HASWIDTH | HASNL | HASLOOKBH);
1737 if (chain == NULL) /* First piece. */
1738 *flagp |= flags & SPSTART;
1739 else
1740 regtail(chain, latest);
1741 chain = latest;
1742 if (first == NULL)
1743 first = latest;
1744 break;
1745 }
1746 }
1747 if (first == NULL) /* Loop ran zero times. */
1748 first = regnode(NOTHING);
1749 return first;
1750}
1751
1752/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001753 * Parse something followed by possible [*+=].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 *
1755 * Note that the branching code sequences used for = and the general cases
1756 * of * and + are somewhat optimized: they use the same NOTHING node as
1757 * both the endmarker for their branch list and the body of the last branch.
1758 * It might seem that this node could be dispensed with entirely, but the
1759 * endmarker role is not redundant.
1760 */
1761 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001762regpiece(int *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763{
1764 char_u *ret;
1765 int op;
1766 char_u *next;
1767 int flags;
1768 long minval;
1769 long maxval;
1770
1771 ret = regatom(&flags);
1772 if (ret == NULL)
1773 return NULL;
1774
1775 op = peekchr();
1776 if (re_multi_type(op) == NOT_MULTI)
1777 {
1778 *flagp = flags;
1779 return ret;
1780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 /* default flags */
1782 *flagp = (WORST | SPSTART | (flags & (HASNL | HASLOOKBH)));
1783
1784 skipchr();
1785 switch (op)
1786 {
1787 case Magic('*'):
1788 if (flags & SIMPLE)
1789 reginsert(STAR, ret);
1790 else
1791 {
1792 /* Emit x* as (x&|), where & means "self". */
1793 reginsert(BRANCH, ret); /* Either x */
1794 regoptail(ret, regnode(BACK)); /* and loop */
1795 regoptail(ret, ret); /* back */
1796 regtail(ret, regnode(BRANCH)); /* or */
1797 regtail(ret, regnode(NOTHING)); /* null. */
1798 }
1799 break;
1800
1801 case Magic('+'):
1802 if (flags & SIMPLE)
1803 reginsert(PLUS, ret);
1804 else
1805 {
1806 /* Emit x+ as x(&|), where & means "self". */
1807 next = regnode(BRANCH); /* Either */
1808 regtail(ret, next);
Bram Moolenaar582fd852005-03-28 20:58:01 +00001809 regtail(regnode(BACK), ret); /* loop back */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 regtail(next, regnode(BRANCH)); /* or */
1811 regtail(ret, regnode(NOTHING)); /* null. */
1812 }
1813 *flagp = (WORST | HASWIDTH | (flags & (HASNL | HASLOOKBH)));
1814 break;
1815
1816 case Magic('@'):
1817 {
1818 int lop = END;
Bram Moolenaar4c22a912017-11-02 22:29:38 +01001819 long nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820
Bram Moolenaar75eb1612013-05-29 18:45:11 +02001821 nr = getdecchrs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 switch (no_Magic(getchr()))
1823 {
1824 case '=': lop = MATCH; break; /* \@= */
1825 case '!': lop = NOMATCH; break; /* \@! */
1826 case '>': lop = SUBPAT; break; /* \@> */
1827 case '<': switch (no_Magic(getchr()))
1828 {
1829 case '=': lop = BEHIND; break; /* \@<= */
1830 case '!': lop = NOBEHIND; break; /* \@<! */
1831 }
1832 }
1833 if (lop == END)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001834 EMSG2_RET_NULL(_("E59: invalid character after %s@"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 reg_magic == MAGIC_ALL);
1836 /* Look behind must match with behind_pos. */
1837 if (lop == BEHIND || lop == NOBEHIND)
1838 {
1839 regtail(ret, regnode(BHPOS));
1840 *flagp |= HASLOOKBH;
1841 }
1842 regtail(ret, regnode(END)); /* operand ends */
Bram Moolenaar75eb1612013-05-29 18:45:11 +02001843 if (lop == BEHIND || lop == NOBEHIND)
1844 {
1845 if (nr < 0)
1846 nr = 0; /* no limit is same as zero limit */
1847 reginsert_nr(lop, nr, ret);
1848 }
1849 else
1850 reginsert(lop, ret);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 break;
1852 }
1853
1854 case Magic('?'):
1855 case Magic('='):
1856 /* Emit x= as (x|) */
1857 reginsert(BRANCH, ret); /* Either x */
1858 regtail(ret, regnode(BRANCH)); /* or */
1859 next = regnode(NOTHING); /* null. */
1860 regtail(ret, next);
1861 regoptail(ret, next);
1862 break;
1863
1864 case Magic('{'):
1865 if (!read_limits(&minval, &maxval))
1866 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 if (flags & SIMPLE)
1868 {
1869 reginsert(BRACE_SIMPLE, ret);
1870 reginsert_limits(BRACE_LIMITS, minval, maxval, ret);
1871 }
1872 else
1873 {
1874 if (num_complex_braces >= 10)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001875 EMSG2_RET_NULL(_("E60: Too many complex %s{...}s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 reg_magic == MAGIC_ALL);
1877 reginsert(BRACE_COMPLEX + num_complex_braces, ret);
1878 regoptail(ret, regnode(BACK));
1879 regoptail(ret, ret);
1880 reginsert_limits(BRACE_LIMITS, minval, maxval, ret);
1881 ++num_complex_braces;
1882 }
1883 if (minval > 0 && maxval > 0)
1884 *flagp = (HASWIDTH | (flags & (HASNL | HASLOOKBH)));
1885 break;
1886 }
1887 if (re_multi_type(peekchr()) != NOT_MULTI)
1888 {
Bram Moolenaar1be45b22019-01-14 22:46:15 +01001889 // Can't have a multi follow a multi.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 if (peekchr() == Magic('*'))
Bram Moolenaar1be45b22019-01-14 22:46:15 +01001891 EMSG2_RET_NULL(_("E61: Nested %s*"), reg_magic >= MAGIC_ON);
1892 EMSG3_RET_NULL(_("E62: Nested %s%c"), reg_magic == MAGIC_ALL,
1893 no_Magic(peekchr()));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 }
1895
1896 return ret;
1897}
1898
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001899/* When making changes to classchars also change nfa_classcodes. */
1900static char_u *classchars = (char_u *)".iIkKfFpPsSdDxXoOwWhHaAlLuU";
1901static int classcodes[] = {
1902 ANY, IDENT, SIDENT, KWORD, SKWORD,
1903 FNAME, SFNAME, PRINT, SPRINT,
1904 WHITE, NWHITE, DIGIT, NDIGIT,
1905 HEX, NHEX, OCTAL, NOCTAL,
1906 WORD, NWORD, HEAD, NHEAD,
1907 ALPHA, NALPHA, LOWER, NLOWER,
1908 UPPER, NUPPER
1909};
1910
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001912 * Parse the lowest level.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 *
1914 * Optimization: gobbles an entire sequence of ordinary characters so that
1915 * it can turn them into a single node, which is smaller to store and
1916 * faster to run. Don't do this when one_exactly is set.
1917 */
1918 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001919regatom(int *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920{
1921 char_u *ret;
1922 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 char_u *p;
1925 int extra = 0;
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001926 int save_prev_at_start = prev_at_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927
1928 *flagp = WORST; /* Tentatively. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929
1930 c = getchr();
1931 switch (c)
1932 {
1933 case Magic('^'):
1934 ret = regnode(BOL);
1935 break;
1936
1937 case Magic('$'):
1938 ret = regnode(EOL);
1939#if defined(FEAT_SYN_HL) || defined(PROTO)
1940 had_eol = TRUE;
1941#endif
1942 break;
1943
1944 case Magic('<'):
1945 ret = regnode(BOW);
1946 break;
1947
1948 case Magic('>'):
1949 ret = regnode(EOW);
1950 break;
1951
1952 case Magic('_'):
1953 c = no_Magic(getchr());
1954 if (c == '^') /* "\_^" is start-of-line */
1955 {
1956 ret = regnode(BOL);
1957 break;
1958 }
1959 if (c == '$') /* "\_$" is end-of-line */
1960 {
1961 ret = regnode(EOL);
1962#if defined(FEAT_SYN_HL) || defined(PROTO)
1963 had_eol = TRUE;
1964#endif
1965 break;
1966 }
1967
1968 extra = ADD_NL;
1969 *flagp |= HASNL;
1970
1971 /* "\_[" is character range plus newline */
1972 if (c == '[')
1973 goto collection;
1974
1975 /* "\_x" is character class plus newline */
Bram Moolenaar2f40d122017-10-24 21:49:36 +02001976 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977
1978 /*
1979 * Character classes.
1980 */
1981 case Magic('.'):
1982 case Magic('i'):
1983 case Magic('I'):
1984 case Magic('k'):
1985 case Magic('K'):
1986 case Magic('f'):
1987 case Magic('F'):
1988 case Magic('p'):
1989 case Magic('P'):
1990 case Magic('s'):
1991 case Magic('S'):
1992 case Magic('d'):
1993 case Magic('D'):
1994 case Magic('x'):
1995 case Magic('X'):
1996 case Magic('o'):
1997 case Magic('O'):
1998 case Magic('w'):
1999 case Magic('W'):
2000 case Magic('h'):
2001 case Magic('H'):
2002 case Magic('a'):
2003 case Magic('A'):
2004 case Magic('l'):
2005 case Magic('L'):
2006 case Magic('u'):
2007 case Magic('U'):
2008 p = vim_strchr(classchars, no_Magic(c));
2009 if (p == NULL)
2010 EMSG_RET_NULL(_("E63: invalid use of \\_"));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002011#ifdef FEAT_MBYTE
2012 /* When '.' is followed by a composing char ignore the dot, so that
2013 * the composing char is matched here. */
2014 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
2015 {
2016 c = getchr();
2017 goto do_multibyte;
2018 }
2019#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 ret = regnode(classcodes[p - classchars] + extra);
2021 *flagp |= HASWIDTH | SIMPLE;
2022 break;
2023
2024 case Magic('n'):
2025 if (reg_string)
2026 {
2027 /* In a string "\n" matches a newline character. */
2028 ret = regnode(EXACTLY);
2029 regc(NL);
2030 regc(NUL);
2031 *flagp |= HASWIDTH | SIMPLE;
2032 }
2033 else
2034 {
2035 /* In buffer text "\n" matches the end of a line. */
2036 ret = regnode(NEWL);
2037 *flagp |= HASWIDTH | HASNL;
2038 }
2039 break;
2040
2041 case Magic('('):
2042 if (one_exactly)
2043 EMSG_ONE_RET_NULL;
2044 ret = reg(REG_PAREN, &flags);
2045 if (ret == NULL)
2046 return NULL;
2047 *flagp |= flags & (HASWIDTH | SPSTART | HASNL | HASLOOKBH);
2048 break;
2049
2050 case NUL:
2051 case Magic('|'):
2052 case Magic('&'):
2053 case Magic(')'):
Bram Moolenaard4210772008-01-02 14:35:30 +00002054 if (one_exactly)
2055 EMSG_ONE_RET_NULL;
Bram Moolenaar95f09602016-11-10 20:01:45 +01002056 IEMSG_RET_NULL(_(e_internal)); /* Supposed to be caught earlier. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 /* NOTREACHED */
2058
2059 case Magic('='):
2060 case Magic('?'):
2061 case Magic('+'):
2062 case Magic('@'):
2063 case Magic('{'):
2064 case Magic('*'):
2065 c = no_Magic(c);
Bram Moolenaar1be45b22019-01-14 22:46:15 +01002066 EMSG3_RET_NULL(_("E64: %s%c follows nothing"),
2067 (c == '*' ? reg_magic >= MAGIC_ON : reg_magic == MAGIC_ALL), c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 /* NOTREACHED */
2069
2070 case Magic('~'): /* previous substitute pattern */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002071 if (reg_prev_sub != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 {
2073 char_u *lp;
2074
2075 ret = regnode(EXACTLY);
2076 lp = reg_prev_sub;
2077 while (*lp != NUL)
2078 regc(*lp++);
2079 regc(NUL);
2080 if (*reg_prev_sub != NUL)
2081 {
2082 *flagp |= HASWIDTH;
2083 if ((lp - reg_prev_sub) == 1)
2084 *flagp |= SIMPLE;
2085 }
2086 }
2087 else
2088 EMSG_RET_NULL(_(e_nopresub));
2089 break;
2090
2091 case Magic('1'):
2092 case Magic('2'):
2093 case Magic('3'):
2094 case Magic('4'):
2095 case Magic('5'):
2096 case Magic('6'):
2097 case Magic('7'):
2098 case Magic('8'):
2099 case Magic('9'):
2100 {
2101 int refnum;
2102
2103 refnum = c - Magic('0');
Bram Moolenaar1ef9bbe2017-06-17 20:08:20 +02002104 if (!seen_endbrace(refnum))
2105 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 ret = regnode(BACKREF + refnum);
2107 }
2108 break;
2109
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 case Magic('z'):
2111 {
2112 c = no_Magic(getchr());
2113 switch (c)
2114 {
Bram Moolenaarc4956c82006-03-12 21:58:43 +00002115#ifdef FEAT_SYN_HL
Bram Moolenaarbcf94422018-06-23 14:21:42 +02002116 case '(': if ((reg_do_extmatch & REX_SET) == 0)
Bram Moolenaar5de820b2013-06-02 15:01:57 +02002117 EMSG_RET_NULL(_(e_z_not_allowed));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 if (one_exactly)
2119 EMSG_ONE_RET_NULL;
2120 ret = reg(REG_ZPAREN, &flags);
2121 if (ret == NULL)
2122 return NULL;
2123 *flagp |= flags & (HASWIDTH|SPSTART|HASNL|HASLOOKBH);
2124 re_has_z = REX_SET;
2125 break;
2126
2127 case '1':
2128 case '2':
2129 case '3':
2130 case '4':
2131 case '5':
2132 case '6':
2133 case '7':
2134 case '8':
Bram Moolenaarbcf94422018-06-23 14:21:42 +02002135 case '9': if ((reg_do_extmatch & REX_USE) == 0)
Bram Moolenaar5de820b2013-06-02 15:01:57 +02002136 EMSG_RET_NULL(_(e_z1_not_allowed));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 ret = regnode(ZREF + c - '0');
2138 re_has_z = REX_USE;
2139 break;
Bram Moolenaarc4956c82006-03-12 21:58:43 +00002140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141
2142 case 's': ret = regnode(MOPEN + 0);
Bram Moolenaarfb031402014-09-09 17:18:49 +02002143 if (re_mult_next("\\zs") == FAIL)
2144 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 break;
2146
2147 case 'e': ret = regnode(MCLOSE + 0);
Bram Moolenaarfb031402014-09-09 17:18:49 +02002148 if (re_mult_next("\\ze") == FAIL)
2149 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 break;
2151
2152 default: EMSG_RET_NULL(_("E68: Invalid character after \\z"));
2153 }
2154 }
2155 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156
2157 case Magic('%'):
2158 {
2159 c = no_Magic(getchr());
2160 switch (c)
2161 {
2162 /* () without a back reference */
2163 case '(':
2164 if (one_exactly)
2165 EMSG_ONE_RET_NULL;
2166 ret = reg(REG_NPAREN, &flags);
2167 if (ret == NULL)
2168 return NULL;
2169 *flagp |= flags & (HASWIDTH | SPSTART | HASNL | HASLOOKBH);
2170 break;
2171
2172 /* Catch \%^ and \%$ regardless of where they appear in the
2173 * pattern -- regardless of whether or not it makes sense. */
2174 case '^':
2175 ret = regnode(RE_BOF);
2176 break;
2177
2178 case '$':
2179 ret = regnode(RE_EOF);
2180 break;
2181
2182 case '#':
2183 ret = regnode(CURSOR);
2184 break;
2185
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00002186 case 'V':
2187 ret = regnode(RE_VISUAL);
2188 break;
2189
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02002190 case 'C':
2191 ret = regnode(RE_COMPOSING);
2192 break;
2193
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 /* \%[abc]: Emit as a list of branches, all ending at the last
2195 * branch which matches nothing. */
2196 case '[':
2197 if (one_exactly) /* doesn't nest */
2198 EMSG_ONE_RET_NULL;
2199 {
2200 char_u *lastbranch;
2201 char_u *lastnode = NULL;
2202 char_u *br;
2203
2204 ret = NULL;
2205 while ((c = getchr()) != ']')
2206 {
2207 if (c == NUL)
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02002208 EMSG2_RET_NULL(_(e_missing_sb),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 reg_magic == MAGIC_ALL);
2210 br = regnode(BRANCH);
2211 if (ret == NULL)
2212 ret = br;
2213 else
2214 regtail(lastnode, br);
2215
2216 ungetchr();
2217 one_exactly = TRUE;
2218 lastnode = regatom(flagp);
2219 one_exactly = FALSE;
2220 if (lastnode == NULL)
2221 return NULL;
2222 }
2223 if (ret == NULL)
Bram Moolenaar2976c022013-06-05 21:30:37 +02002224 EMSG2_RET_NULL(_(e_empty_sb),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 reg_magic == MAGIC_ALL);
2226 lastbranch = regnode(BRANCH);
2227 br = regnode(NOTHING);
2228 if (ret != JUST_CALC_SIZE)
2229 {
2230 regtail(lastnode, br);
2231 regtail(lastbranch, br);
2232 /* connect all branches to the NOTHING
2233 * branch at the end */
2234 for (br = ret; br != lastnode; )
2235 {
2236 if (OP(br) == BRANCH)
2237 {
2238 regtail(br, lastbranch);
2239 br = OPERAND(br);
2240 }
2241 else
2242 br = regnext(br);
2243 }
2244 }
Bram Moolenaara6404a42008-08-08 11:45:39 +00002245 *flagp &= ~(HASWIDTH | SIMPLE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 break;
2247 }
2248
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002249 case 'd': /* %d123 decimal */
2250 case 'o': /* %o123 octal */
2251 case 'x': /* %xab hex 2 */
2252 case 'u': /* %uabcd hex 4 */
2253 case 'U': /* %U1234abcd hex 8 */
2254 {
Bram Moolenaar4c22a912017-11-02 22:29:38 +01002255 long i;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002256
2257 switch (c)
2258 {
2259 case 'd': i = getdecchrs(); break;
2260 case 'o': i = getoctchrs(); break;
2261 case 'x': i = gethexchrs(2); break;
2262 case 'u': i = gethexchrs(4); break;
2263 case 'U': i = gethexchrs(8); break;
2264 default: i = -1; break;
2265 }
2266
2267 if (i < 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002268 EMSG2_RET_NULL(
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002269 _("E678: Invalid character after %s%%[dxouU]"),
2270 reg_magic == MAGIC_ALL);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002271#ifdef FEAT_MBYTE
2272 if (use_multibytecode(i))
2273 ret = regnode(MULTIBYTECODE);
2274 else
2275#endif
2276 ret = regnode(EXACTLY);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002277 if (i == 0)
2278 regc(0x0a);
2279 else
2280#ifdef FEAT_MBYTE
2281 regmbc(i);
2282#else
2283 regc(i);
2284#endif
2285 regc(NUL);
2286 *flagp |= HASWIDTH;
2287 break;
2288 }
2289
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 default:
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00002291 if (VIM_ISDIGIT(c) || c == '<' || c == '>'
2292 || c == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 {
2294 long_u n = 0;
2295 int cmp;
2296
2297 cmp = c;
2298 if (cmp == '<' || cmp == '>')
2299 c = getchr();
2300 while (VIM_ISDIGIT(c))
2301 {
2302 n = n * 10 + (c - '0');
2303 c = getchr();
2304 }
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00002305 if (c == '\'' && n == 0)
2306 {
2307 /* "\%'m", "\%<'m" and "\%>'m": Mark */
2308 c = getchr();
2309 ret = regnode(RE_MARK);
2310 if (ret == JUST_CALC_SIZE)
2311 regsize += 2;
2312 else
2313 {
2314 *regcode++ = c;
2315 *regcode++ = cmp;
2316 }
2317 break;
2318 }
2319 else if (c == 'l' || c == 'c' || c == 'v')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 {
2321 if (c == 'l')
Bram Moolenaar7c29f382016-02-12 19:08:15 +01002322 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 ret = regnode(RE_LNUM);
Bram Moolenaar7c29f382016-02-12 19:08:15 +01002324 if (save_prev_at_start)
2325 at_start = TRUE;
2326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 else if (c == 'c')
2328 ret = regnode(RE_COL);
2329 else
2330 ret = regnode(RE_VCOL);
2331 if (ret == JUST_CALC_SIZE)
2332 regsize += 5;
2333 else
2334 {
2335 /* put the number and the optional
2336 * comparator after the opcode */
2337 regcode = re_put_long(regcode, n);
2338 *regcode++ = cmp;
2339 }
2340 break;
2341 }
2342 }
2343
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002344 EMSG2_RET_NULL(_("E71: Invalid character after %s%%"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 reg_magic == MAGIC_ALL);
2346 }
2347 }
2348 break;
2349
2350 case Magic('['):
2351collection:
2352 {
2353 char_u *lp;
2354
2355 /*
2356 * If there is no matching ']', we assume the '[' is a normal
2357 * character. This makes 'incsearch' and ":help [" work.
2358 */
2359 lp = skip_anyof(regparse);
2360 if (*lp == ']') /* there is a matching ']' */
2361 {
2362 int startc = -1; /* > 0 when next '-' is a range */
2363 int endc;
2364
2365 /*
2366 * In a character class, different parsing rules apply.
2367 * Not even \ is special anymore, nothing is.
2368 */
2369 if (*regparse == '^') /* Complement of range. */
2370 {
2371 ret = regnode(ANYBUT + extra);
2372 regparse++;
2373 }
2374 else
2375 ret = regnode(ANYOF + extra);
2376
2377 /* At the start ']' and '-' mean the literal character. */
2378 if (*regparse == ']' || *regparse == '-')
Bram Moolenaardf177f62005-02-22 08:39:57 +00002379 {
2380 startc = *regparse;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 regc(*regparse++);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383
2384 while (*regparse != NUL && *regparse != ']')
2385 {
2386 if (*regparse == '-')
2387 {
2388 ++regparse;
2389 /* The '-' is not used for a range at the end and
2390 * after or before a '\n'. */
2391 if (*regparse == ']' || *regparse == NUL
2392 || startc == -1
2393 || (regparse[0] == '\\' && regparse[1] == 'n'))
2394 {
2395 regc('-');
2396 startc = '-'; /* [--x] is a range */
2397 }
2398 else
2399 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00002400 /* Also accept "a-[.z.]" */
2401 endc = 0;
2402 if (*regparse == '[')
2403 endc = get_coll_element(&regparse);
2404 if (endc == 0)
2405 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406#ifdef FEAT_MBYTE
Bram Moolenaardf177f62005-02-22 08:39:57 +00002407 if (has_mbyte)
2408 endc = mb_ptr2char_adv(&regparse);
2409 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00002411 endc = *regparse++;
2412 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002413
2414 /* Handle \o40, \x20 and \u20AC style sequences */
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02002415 if (endc == '\\' && !reg_cpo_lit && !reg_cpo_bsl)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002416 endc = coll_get_char();
2417
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 if (startc > endc)
Bram Moolenaar966e58e2017-06-05 16:54:08 +02002419 EMSG_RET_NULL(_(e_reverse_range));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420#ifdef FEAT_MBYTE
2421 if (has_mbyte && ((*mb_char2len)(startc) > 1
2422 || (*mb_char2len)(endc) > 1))
2423 {
Bram Moolenaar966e58e2017-06-05 16:54:08 +02002424 /* Limit to a range of 256 chars. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 if (endc > startc + 256)
Bram Moolenaar966e58e2017-06-05 16:54:08 +02002426 EMSG_RET_NULL(_(e_large_class));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 while (++startc <= endc)
2428 regmbc(startc);
2429 }
2430 else
2431#endif
2432 {
2433#ifdef EBCDIC
2434 int alpha_only = FALSE;
2435
2436 /* for alphabetical range skip the gaps
2437 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
2438 if (isalpha(startc) && isalpha(endc))
2439 alpha_only = TRUE;
2440#endif
2441 while (++startc <= endc)
2442#ifdef EBCDIC
2443 if (!alpha_only || isalpha(startc))
2444#endif
2445 regc(startc);
2446 }
2447 startc = -1;
2448 }
2449 }
2450 /*
2451 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
2452 * accepts "\t", "\e", etc., but only when the 'l' flag in
2453 * 'cpoptions' is not included.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002454 * Posix doesn't recognize backslash at all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 */
2456 else if (*regparse == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02002457 && !reg_cpo_bsl
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02002459 || (!reg_cpo_lit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 && vim_strchr(REGEXP_ABBR,
2461 regparse[1]) != NULL)))
2462 {
2463 regparse++;
2464 if (*regparse == 'n')
2465 {
2466 /* '\n' in range: also match NL */
2467 if (ret != JUST_CALC_SIZE)
2468 {
Bram Moolenaare337e5f2013-01-30 18:21:51 +01002469 /* Using \n inside [^] does not change what
2470 * matches. "[^\n]" is the same as ".". */
2471 if (*ret == ANYOF)
2472 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 *ret = ANYOF + ADD_NL;
Bram Moolenaare337e5f2013-01-30 18:21:51 +01002474 *flagp |= HASNL;
2475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 /* else: must have had a \n already */
2477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 regparse++;
2479 startc = -1;
2480 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002481 else if (*regparse == 'd'
2482 || *regparse == 'o'
2483 || *regparse == 'x'
2484 || *regparse == 'u'
2485 || *regparse == 'U')
2486 {
2487 startc = coll_get_char();
2488 if (startc == 0)
2489 regc(0x0a);
2490 else
2491#ifdef FEAT_MBYTE
2492 regmbc(startc);
2493#else
2494 regc(startc);
2495#endif
2496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 else
2498 {
2499 startc = backslash_trans(*regparse++);
2500 regc(startc);
2501 }
2502 }
2503 else if (*regparse == '[')
2504 {
2505 int c_class;
2506 int cu;
2507
Bram Moolenaardf177f62005-02-22 08:39:57 +00002508 c_class = get_char_class(&regparse);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 startc = -1;
2510 /* Characters assumed to be 8 bits! */
2511 switch (c_class)
2512 {
2513 case CLASS_NONE:
Bram Moolenaardf177f62005-02-22 08:39:57 +00002514 c_class = get_equi_class(&regparse);
2515 if (c_class != 0)
2516 {
2517 /* produce equivalence class */
2518 reg_equi_class(c_class);
2519 }
2520 else if ((c_class =
2521 get_coll_element(&regparse)) != 0)
2522 {
2523 /* produce a collating element */
2524 regmbc(c_class);
2525 }
2526 else
2527 {
2528 /* literal '[', allow [[-x] as a range */
2529 startc = *regparse++;
2530 regc(startc);
2531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 break;
2533 case CLASS_ALNUM:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02002534 for (cu = 1; cu < 128; cu++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 if (isalnum(cu))
Bram Moolenaaraf98a492016-04-24 14:40:12 +02002536 regmbc(cu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 break;
2538 case CLASS_ALPHA:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02002539 for (cu = 1; cu < 128; cu++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 if (isalpha(cu))
Bram Moolenaaraf98a492016-04-24 14:40:12 +02002541 regmbc(cu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 break;
2543 case CLASS_BLANK:
2544 regc(' ');
2545 regc('\t');
2546 break;
2547 case CLASS_CNTRL:
Bram Moolenaar0c078fc2017-03-29 15:31:20 +02002548 for (cu = 1; cu <= 127; cu++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 if (iscntrl(cu))
Bram Moolenaaraf98a492016-04-24 14:40:12 +02002550 regmbc(cu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 break;
2552 case CLASS_DIGIT:
Bram Moolenaar0c078fc2017-03-29 15:31:20 +02002553 for (cu = 1; cu <= 127; cu++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 if (VIM_ISDIGIT(cu))
Bram Moolenaaraf98a492016-04-24 14:40:12 +02002555 regmbc(cu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 break;
2557 case CLASS_GRAPH:
Bram Moolenaar0c078fc2017-03-29 15:31:20 +02002558 for (cu = 1; cu <= 127; cu++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 if (isgraph(cu))
Bram Moolenaaraf98a492016-04-24 14:40:12 +02002560 regmbc(cu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 break;
2562 case CLASS_LOWER:
2563 for (cu = 1; cu <= 255; cu++)
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02002564 if (MB_ISLOWER(cu) && cu != 170
2565 && cu != 186)
Bram Moolenaaraf98a492016-04-24 14:40:12 +02002566 regmbc(cu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 break;
2568 case CLASS_PRINT:
2569 for (cu = 1; cu <= 255; cu++)
2570 if (vim_isprintc(cu))
Bram Moolenaaraf98a492016-04-24 14:40:12 +02002571 regmbc(cu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 break;
2573 case CLASS_PUNCT:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02002574 for (cu = 1; cu < 128; cu++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 if (ispunct(cu))
Bram Moolenaaraf98a492016-04-24 14:40:12 +02002576 regmbc(cu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 break;
2578 case CLASS_SPACE:
2579 for (cu = 9; cu <= 13; cu++)
2580 regc(cu);
2581 regc(' ');
2582 break;
2583 case CLASS_UPPER:
2584 for (cu = 1; cu <= 255; cu++)
Bram Moolenaara245a5b2007-08-11 11:58:23 +00002585 if (MB_ISUPPER(cu))
Bram Moolenaaraf98a492016-04-24 14:40:12 +02002586 regmbc(cu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 break;
2588 case CLASS_XDIGIT:
2589 for (cu = 1; cu <= 255; cu++)
2590 if (vim_isxdigit(cu))
Bram Moolenaaraf98a492016-04-24 14:40:12 +02002591 regmbc(cu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 break;
2593 case CLASS_TAB:
2594 regc('\t');
2595 break;
2596 case CLASS_RETURN:
2597 regc('\r');
2598 break;
2599 case CLASS_BACKSPACE:
2600 regc('\b');
2601 break;
2602 case CLASS_ESCAPE:
2603 regc('\033');
2604 break;
2605 }
2606 }
2607 else
2608 {
2609#ifdef FEAT_MBYTE
2610 if (has_mbyte)
2611 {
2612 int len;
2613
2614 /* produce a multibyte character, including any
2615 * following composing characters */
2616 startc = mb_ptr2char(regparse);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002617 len = (*mb_ptr2len)(regparse);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 if (enc_utf8 && utf_char2len(startc) != len)
2619 startc = -1; /* composing chars */
2620 while (--len >= 0)
2621 regc(*regparse++);
2622 }
2623 else
2624#endif
2625 {
2626 startc = *regparse++;
2627 regc(startc);
2628 }
2629 }
2630 }
2631 regc(NUL);
2632 prevchr_len = 1; /* last char was the ']' */
2633 if (*regparse != ']')
2634 EMSG_RET_NULL(_(e_toomsbra)); /* Cannot happen? */
2635 skipchr(); /* let's be friends with the lexer again */
2636 *flagp |= HASWIDTH | SIMPLE;
2637 break;
2638 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002639 else if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002640 EMSG2_RET_NULL(_(e_missingbracket), reg_magic > MAGIC_OFF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 }
2642 /* FALLTHROUGH */
2643
2644 default:
2645 {
2646 int len;
2647
2648#ifdef FEAT_MBYTE
2649 /* A multi-byte character is handled as a separate atom if it's
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002650 * before a multi and when it's a composing char. */
2651 if (use_multibytecode(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002653do_multibyte:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 ret = regnode(MULTIBYTECODE);
2655 regmbc(c);
2656 *flagp |= HASWIDTH | SIMPLE;
2657 break;
2658 }
2659#endif
2660
2661 ret = regnode(EXACTLY);
2662
2663 /*
2664 * Append characters as long as:
2665 * - there is no following multi, we then need the character in
2666 * front of it as a single character operand
2667 * - not running into a Magic character
2668 * - "one_exactly" is not set
2669 * But always emit at least one character. Might be a Multi,
2670 * e.g., a "[" without matching "]".
2671 */
2672 for (len = 0; c != NUL && (len == 0
2673 || (re_multi_type(peekchr()) == NOT_MULTI
2674 && !one_exactly
2675 && !is_Magic(c))); ++len)
2676 {
2677 c = no_Magic(c);
2678#ifdef FEAT_MBYTE
2679 if (has_mbyte)
2680 {
2681 regmbc(c);
2682 if (enc_utf8)
2683 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 int l;
2685
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002686 /* Need to get composing character too. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 for (;;)
2688 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002689 l = utf_ptr2len(regparse);
2690 if (!UTF_COMPOSINGLIKE(regparse, regparse + l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 break;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002692 regmbc(utf_ptr2char(regparse));
2693 skipchr();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 }
2696 }
2697 else
2698#endif
2699 regc(c);
2700 c = getchr();
2701 }
2702 ungetchr();
2703
2704 regc(NUL);
2705 *flagp |= HASWIDTH;
2706 if (len == 1)
2707 *flagp |= SIMPLE;
2708 }
2709 break;
2710 }
2711
2712 return ret;
2713}
2714
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002715#ifdef FEAT_MBYTE
2716/*
2717 * Return TRUE if MULTIBYTECODE should be used instead of EXACTLY for
2718 * character "c".
2719 */
2720 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002721use_multibytecode(int c)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002722{
2723 return has_mbyte && (*mb_char2len)(c) > 1
2724 && (re_multi_type(peekchr()) != NOT_MULTI
2725 || (enc_utf8 && utf_iscomposing(c)));
2726}
2727#endif
2728
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002730 * Emit a node.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 * Return pointer to generated code.
2732 */
2733 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002734regnode(int op)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735{
2736 char_u *ret;
2737
2738 ret = regcode;
2739 if (ret == JUST_CALC_SIZE)
2740 regsize += 3;
2741 else
2742 {
2743 *regcode++ = op;
2744 *regcode++ = NUL; /* Null "next" pointer. */
2745 *regcode++ = NUL;
2746 }
2747 return ret;
2748}
2749
2750/*
2751 * Emit (if appropriate) a byte of code
2752 */
2753 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002754regc(int b)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755{
2756 if (regcode == JUST_CALC_SIZE)
2757 regsize++;
2758 else
2759 *regcode++ = b;
2760}
2761
2762#ifdef FEAT_MBYTE
2763/*
2764 * Emit (if appropriate) a multi-byte character of code
2765 */
2766 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002767regmbc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768{
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02002769 if (!has_mbyte && c > 0xff)
2770 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 if (regcode == JUST_CALC_SIZE)
2772 regsize += (*mb_char2len)(c);
2773 else
2774 regcode += (*mb_char2bytes)(c, regcode);
2775}
2776#endif
2777
2778/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002779 * Insert an operator in front of already-emitted operand
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 *
2781 * Means relocating the operand.
2782 */
2783 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002784reginsert(int op, char_u *opnd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785{
2786 char_u *src;
2787 char_u *dst;
2788 char_u *place;
2789
2790 if (regcode == JUST_CALC_SIZE)
2791 {
2792 regsize += 3;
2793 return;
2794 }
2795 src = regcode;
2796 regcode += 3;
2797 dst = regcode;
2798 while (src > opnd)
2799 *--dst = *--src;
2800
2801 place = opnd; /* Op node, where operand used to be. */
2802 *place++ = op;
2803 *place++ = NUL;
2804 *place = NUL;
2805}
2806
2807/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002808 * Insert an operator in front of already-emitted operand.
Bram Moolenaar75eb1612013-05-29 18:45:11 +02002809 * Add a number to the operator.
2810 */
2811 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002812reginsert_nr(int op, long val, char_u *opnd)
Bram Moolenaar75eb1612013-05-29 18:45:11 +02002813{
2814 char_u *src;
2815 char_u *dst;
2816 char_u *place;
2817
2818 if (regcode == JUST_CALC_SIZE)
2819 {
2820 regsize += 7;
2821 return;
2822 }
2823 src = regcode;
2824 regcode += 7;
2825 dst = regcode;
2826 while (src > opnd)
2827 *--dst = *--src;
2828
2829 place = opnd; /* Op node, where operand used to be. */
2830 *place++ = op;
2831 *place++ = NUL;
2832 *place++ = NUL;
2833 place = re_put_long(place, (long_u)val);
2834}
2835
2836/*
2837 * Insert an operator in front of already-emitted operand.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 * The operator has the given limit values as operands. Also set next pointer.
2839 *
2840 * Means relocating the operand.
2841 */
2842 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002843reginsert_limits(
2844 int op,
2845 long minval,
2846 long maxval,
2847 char_u *opnd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848{
2849 char_u *src;
2850 char_u *dst;
2851 char_u *place;
2852
2853 if (regcode == JUST_CALC_SIZE)
2854 {
2855 regsize += 11;
2856 return;
2857 }
2858 src = regcode;
2859 regcode += 11;
2860 dst = regcode;
2861 while (src > opnd)
2862 *--dst = *--src;
2863
2864 place = opnd; /* Op node, where operand used to be. */
2865 *place++ = op;
2866 *place++ = NUL;
2867 *place++ = NUL;
2868 place = re_put_long(place, (long_u)minval);
2869 place = re_put_long(place, (long_u)maxval);
2870 regtail(opnd, place);
2871}
2872
2873/*
2874 * Write a long as four bytes at "p" and return pointer to the next char.
2875 */
2876 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002877re_put_long(char_u *p, long_u val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878{
2879 *p++ = (char_u) ((val >> 24) & 0377);
2880 *p++ = (char_u) ((val >> 16) & 0377);
2881 *p++ = (char_u) ((val >> 8) & 0377);
2882 *p++ = (char_u) (val & 0377);
2883 return p;
2884}
2885
2886/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002887 * Set the next-pointer at the end of a node chain.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 */
2889 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002890regtail(char_u *p, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891{
2892 char_u *scan;
2893 char_u *temp;
2894 int offset;
2895
2896 if (p == JUST_CALC_SIZE)
2897 return;
2898
2899 /* Find last node. */
2900 scan = p;
2901 for (;;)
2902 {
2903 temp = regnext(scan);
2904 if (temp == NULL)
2905 break;
2906 scan = temp;
2907 }
2908
Bram Moolenaar582fd852005-03-28 20:58:01 +00002909 if (OP(scan) == BACK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 offset = (int)(scan - val);
2911 else
2912 offset = (int)(val - scan);
Bram Moolenaard3005802009-11-25 17:21:32 +00002913 /* When the offset uses more than 16 bits it can no longer fit in the two
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02002914 * bytes available. Use a global flag to avoid having to check return
Bram Moolenaard3005802009-11-25 17:21:32 +00002915 * values in too many places. */
2916 if (offset > 0xffff)
2917 reg_toolong = TRUE;
2918 else
2919 {
2920 *(scan + 1) = (char_u) (((unsigned)offset >> 8) & 0377);
2921 *(scan + 2) = (char_u) (offset & 0377);
2922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923}
2924
2925/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002926 * Like regtail, on item after a BRANCH; nop if none.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 */
2928 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002929regoptail(char_u *p, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930{
2931 /* When op is neither BRANCH nor BRACE_COMPLEX0-9, it is "operandless" */
2932 if (p == NULL || p == JUST_CALC_SIZE
2933 || (OP(p) != BRANCH
2934 && (OP(p) < BRACE_COMPLEX || OP(p) > BRACE_COMPLEX + 9)))
2935 return;
2936 regtail(OPERAND(p), val);
2937}
2938
2939/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002940 * Functions for getting characters from the regexp input.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002942/*
2943 * Start parsing at "str".
2944 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002946initchr(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947{
2948 regparse = str;
2949 prevchr_len = 0;
2950 curchr = prevprevchr = prevchr = nextchr = -1;
2951 at_start = TRUE;
2952 prev_at_start = FALSE;
2953}
2954
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002955/*
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002956 * Save the current parse state, so that it can be restored and parsing
2957 * starts in the same state again.
2958 */
2959 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002960save_parse_state(parse_state_T *ps)
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002961{
2962 ps->regparse = regparse;
2963 ps->prevchr_len = prevchr_len;
2964 ps->curchr = curchr;
2965 ps->prevchr = prevchr;
2966 ps->prevprevchr = prevprevchr;
2967 ps->nextchr = nextchr;
2968 ps->at_start = at_start;
2969 ps->prev_at_start = prev_at_start;
2970 ps->regnpar = regnpar;
2971}
2972
2973/*
2974 * Restore a previously saved parse state.
2975 */
2976 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002977restore_parse_state(parse_state_T *ps)
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002978{
2979 regparse = ps->regparse;
2980 prevchr_len = ps->prevchr_len;
2981 curchr = ps->curchr;
2982 prevchr = ps->prevchr;
2983 prevprevchr = ps->prevprevchr;
2984 nextchr = ps->nextchr;
2985 at_start = ps->at_start;
2986 prev_at_start = ps->prev_at_start;
2987 regnpar = ps->regnpar;
2988}
2989
2990
2991/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002992 * Get the next character without advancing.
2993 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002995peekchr(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996{
Bram Moolenaardf177f62005-02-22 08:39:57 +00002997 static int after_slash = FALSE;
2998
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 if (curchr == -1)
3000 {
3001 switch (curchr = regparse[0])
3002 {
3003 case '.':
3004 case '[':
3005 case '~':
3006 /* magic when 'magic' is on */
3007 if (reg_magic >= MAGIC_ON)
3008 curchr = Magic(curchr);
3009 break;
3010 case '(':
3011 case ')':
3012 case '{':
3013 case '%':
3014 case '+':
3015 case '=':
3016 case '?':
3017 case '@':
3018 case '!':
3019 case '&':
3020 case '|':
3021 case '<':
3022 case '>':
3023 case '#': /* future ext. */
3024 case '"': /* future ext. */
3025 case '\'': /* future ext. */
3026 case ',': /* future ext. */
3027 case '-': /* future ext. */
3028 case ':': /* future ext. */
3029 case ';': /* future ext. */
3030 case '`': /* future ext. */
3031 case '/': /* Can't be used in / command */
3032 /* magic only after "\v" */
3033 if (reg_magic == MAGIC_ALL)
3034 curchr = Magic(curchr);
3035 break;
3036 case '*':
Bram Moolenaardf177f62005-02-22 08:39:57 +00003037 /* * is not magic as the very first character, eg "?*ptr", when
3038 * after '^', eg "/^*ptr" and when after "\(", "\|", "\&". But
3039 * "\(\*" is not magic, thus must be magic if "after_slash" */
3040 if (reg_magic >= MAGIC_ON
3041 && !at_start
3042 && !(prev_at_start && prevchr == Magic('^'))
3043 && (after_slash
3044 || (prevchr != Magic('(')
3045 && prevchr != Magic('&')
3046 && prevchr != Magic('|'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 curchr = Magic('*');
3048 break;
3049 case '^':
3050 /* '^' is only magic as the very first character and if it's after
3051 * "\(", "\|", "\&' or "\n" */
3052 if (reg_magic >= MAGIC_OFF
3053 && (at_start
3054 || reg_magic == MAGIC_ALL
3055 || prevchr == Magic('(')
3056 || prevchr == Magic('|')
3057 || prevchr == Magic('&')
3058 || prevchr == Magic('n')
3059 || (no_Magic(prevchr) == '('
3060 && prevprevchr == Magic('%'))))
3061 {
3062 curchr = Magic('^');
3063 at_start = TRUE;
3064 prev_at_start = FALSE;
3065 }
3066 break;
3067 case '$':
3068 /* '$' is only magic as the very last char and if it's in front of
3069 * either "\|", "\)", "\&", or "\n" */
3070 if (reg_magic >= MAGIC_OFF)
3071 {
3072 char_u *p = regparse + 1;
Bram Moolenaarff65ac82014-07-09 19:32:34 +02003073 int is_magic_all = (reg_magic == MAGIC_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074
Bram Moolenaarff65ac82014-07-09 19:32:34 +02003075 /* ignore \c \C \m \M \v \V and \Z after '$' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 while (p[0] == '\\' && (p[1] == 'c' || p[1] == 'C'
Bram Moolenaarff65ac82014-07-09 19:32:34 +02003077 || p[1] == 'm' || p[1] == 'M'
3078 || p[1] == 'v' || p[1] == 'V' || p[1] == 'Z'))
3079 {
3080 if (p[1] == 'v')
3081 is_magic_all = TRUE;
3082 else if (p[1] == 'm' || p[1] == 'M' || p[1] == 'V')
3083 is_magic_all = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 p += 2;
Bram Moolenaarff65ac82014-07-09 19:32:34 +02003085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 if (p[0] == NUL
3087 || (p[0] == '\\'
3088 && (p[1] == '|' || p[1] == '&' || p[1] == ')'
3089 || p[1] == 'n'))
Bram Moolenaarff65ac82014-07-09 19:32:34 +02003090 || (is_magic_all
3091 && (p[0] == '|' || p[0] == '&' || p[0] == ')'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 || reg_magic == MAGIC_ALL)
3093 curchr = Magic('$');
3094 }
3095 break;
3096 case '\\':
3097 {
3098 int c = regparse[1];
3099
3100 if (c == NUL)
3101 curchr = '\\'; /* trailing '\' */
3102 else if (
3103#ifdef EBCDIC
3104 vim_strchr(META, c)
3105#else
3106 c <= '~' && META_flags[c]
3107#endif
3108 )
3109 {
3110 /*
3111 * META contains everything that may be magic sometimes,
3112 * except ^ and $ ("\^" and "\$" are only magic after
Bram Moolenaarb878bbb2015-06-09 20:39:24 +02003113 * "\V"). We now fetch the next character and toggle its
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 * magicness. Therefore, \ is so meta-magic that it is
3115 * not in META.
3116 */
3117 curchr = -1;
3118 prev_at_start = at_start;
3119 at_start = FALSE; /* be able to say "/\*ptr" */
3120 ++regparse;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003121 ++after_slash;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 peekchr();
3123 --regparse;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003124 --after_slash;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 curchr = toggle_Magic(curchr);
3126 }
3127 else if (vim_strchr(REGEXP_ABBR, c))
3128 {
3129 /*
3130 * Handle abbreviations, like "\t" for TAB -- webb
3131 */
3132 curchr = backslash_trans(c);
3133 }
3134 else if (reg_magic == MAGIC_NONE && (c == '$' || c == '^'))
3135 curchr = toggle_Magic(c);
3136 else
3137 {
3138 /*
3139 * Next character can never be (made) magic?
3140 * Then backslashing it won't do anything.
3141 */
3142#ifdef FEAT_MBYTE
3143 if (has_mbyte)
3144 curchr = (*mb_ptr2char)(regparse + 1);
3145 else
3146#endif
3147 curchr = c;
3148 }
3149 break;
3150 }
3151
3152#ifdef FEAT_MBYTE
3153 default:
3154 if (has_mbyte)
3155 curchr = (*mb_ptr2char)(regparse);
3156#endif
3157 }
3158 }
3159
3160 return curchr;
3161}
3162
3163/*
3164 * Eat one lexed character. Do this in a way that we can undo it.
3165 */
3166 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003167skipchr(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168{
3169 /* peekchr() eats a backslash, do the same here */
3170 if (*regparse == '\\')
3171 prevchr_len = 1;
3172 else
3173 prevchr_len = 0;
3174 if (regparse[prevchr_len] != NUL)
3175 {
3176#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003177 if (enc_utf8)
Bram Moolenaar8f5c5782007-11-29 20:27:21 +00003178 /* exclude composing chars that mb_ptr2len does include */
3179 prevchr_len += utf_ptr2len(regparse + prevchr_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003180 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003181 prevchr_len += (*mb_ptr2len)(regparse + prevchr_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 else
3183#endif
3184 ++prevchr_len;
3185 }
3186 regparse += prevchr_len;
3187 prev_at_start = at_start;
3188 at_start = FALSE;
3189 prevprevchr = prevchr;
3190 prevchr = curchr;
3191 curchr = nextchr; /* use previously unget char, or -1 */
3192 nextchr = -1;
3193}
3194
3195/*
3196 * Skip a character while keeping the value of prev_at_start for at_start.
3197 * prevchr and prevprevchr are also kept.
3198 */
3199 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003200skipchr_keepstart(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201{
3202 int as = prev_at_start;
3203 int pr = prevchr;
3204 int prpr = prevprevchr;
3205
3206 skipchr();
3207 at_start = as;
3208 prevchr = pr;
3209 prevprevchr = prpr;
3210}
3211
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003212/*
3213 * Get the next character from the pattern. We know about magic and such, so
3214 * therefore we need a lexical analyzer.
3215 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003217getchr(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218{
3219 int chr = peekchr();
3220
3221 skipchr();
3222 return chr;
3223}
3224
3225/*
3226 * put character back. Works only once!
3227 */
3228 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003229ungetchr(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230{
3231 nextchr = curchr;
3232 curchr = prevchr;
3233 prevchr = prevprevchr;
3234 at_start = prev_at_start;
3235 prev_at_start = FALSE;
3236
3237 /* Backup regparse, so that it's at the same position as before the
3238 * getchr(). */
3239 regparse -= prevchr_len;
3240}
3241
3242/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00003243 * Get and return the value of the hex string at the current position.
3244 * Return -1 if there is no valid hex number.
3245 * The position is updated:
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003246 * blahblah\%x20asdf
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003247 * before-^ ^-after
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003248 * The parameter controls the maximum number of input characters. This will be
3249 * 2 when reading a \%x20 sequence and 4 when reading a \%u20AC sequence.
3250 */
Bram Moolenaar4c22a912017-11-02 22:29:38 +01003251 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01003252gethexchrs(int maxinputlen)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003253{
Bram Moolenaar4c22a912017-11-02 22:29:38 +01003254 long_u nr = 0;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003255 int c;
3256 int i;
3257
3258 for (i = 0; i < maxinputlen; ++i)
3259 {
3260 c = regparse[0];
3261 if (!vim_isxdigit(c))
3262 break;
3263 nr <<= 4;
3264 nr |= hex2nr(c);
3265 ++regparse;
3266 }
3267
3268 if (i == 0)
3269 return -1;
Bram Moolenaar4c22a912017-11-02 22:29:38 +01003270 return (long)nr;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003271}
3272
3273/*
Bram Moolenaar75eb1612013-05-29 18:45:11 +02003274 * Get and return the value of the decimal string immediately after the
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003275 * current position. Return -1 for invalid. Consumes all digits.
3276 */
Bram Moolenaar4c22a912017-11-02 22:29:38 +01003277 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01003278getdecchrs(void)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003279{
Bram Moolenaar4c22a912017-11-02 22:29:38 +01003280 long_u nr = 0;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003281 int c;
3282 int i;
3283
3284 for (i = 0; ; ++i)
3285 {
3286 c = regparse[0];
3287 if (c < '0' || c > '9')
3288 break;
3289 nr *= 10;
3290 nr += c - '0';
3291 ++regparse;
Bram Moolenaar75eb1612013-05-29 18:45:11 +02003292 curchr = -1; /* no longer valid */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003293 }
3294
3295 if (i == 0)
3296 return -1;
Bram Moolenaar4c22a912017-11-02 22:29:38 +01003297 return (long)nr;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003298}
3299
3300/*
3301 * get and return the value of the octal string immediately after the current
3302 * position. Return -1 for invalid, or 0-255 for valid. Smart enough to handle
3303 * numbers > 377 correctly (for example, 400 is treated as 40) and doesn't
3304 * treat 8 or 9 as recognised characters. Position is updated:
3305 * blahblah\%o210asdf
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003306 * before-^ ^-after
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003307 */
Bram Moolenaar4c22a912017-11-02 22:29:38 +01003308 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01003309getoctchrs(void)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003310{
Bram Moolenaar4c22a912017-11-02 22:29:38 +01003311 long_u nr = 0;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003312 int c;
3313 int i;
3314
3315 for (i = 0; i < 3 && nr < 040; ++i)
3316 {
3317 c = regparse[0];
3318 if (c < '0' || c > '7')
3319 break;
3320 nr <<= 3;
3321 nr |= hex2nr(c);
3322 ++regparse;
3323 }
3324
3325 if (i == 0)
3326 return -1;
Bram Moolenaar4c22a912017-11-02 22:29:38 +01003327 return (long)nr;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003328}
3329
3330/*
3331 * Get a number after a backslash that is inside [].
3332 * When nothing is recognized return a backslash.
3333 */
3334 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003335coll_get_char(void)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003336{
Bram Moolenaar4c22a912017-11-02 22:29:38 +01003337 long nr = -1;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003338
3339 switch (*regparse++)
3340 {
3341 case 'd': nr = getdecchrs(); break;
3342 case 'o': nr = getoctchrs(); break;
3343 case 'x': nr = gethexchrs(2); break;
3344 case 'u': nr = gethexchrs(4); break;
3345 case 'U': nr = gethexchrs(8); break;
3346 }
3347 if (nr < 0)
3348 {
3349 /* If getting the number fails be backwards compatible: the character
3350 * is a backslash. */
3351 --regparse;
3352 nr = '\\';
3353 }
3354 return nr;
3355}
3356
3357/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 * read_limits - Read two integers to be taken as a minimum and maximum.
3359 * If the first character is '-', then the range is reversed.
3360 * Should end with 'end'. If minval is missing, zero is default, if maxval is
3361 * missing, a very big number is the default.
3362 */
3363 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003364read_limits(long *minval, long *maxval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365{
3366 int reverse = FALSE;
3367 char_u *first_char;
3368 long tmp;
3369
3370 if (*regparse == '-')
3371 {
3372 /* Starts with '-', so reverse the range later */
3373 regparse++;
3374 reverse = TRUE;
3375 }
3376 first_char = regparse;
3377 *minval = getdigits(&regparse);
3378 if (*regparse == ',') /* There is a comma */
3379 {
3380 if (vim_isdigit(*++regparse))
3381 *maxval = getdigits(&regparse);
3382 else
3383 *maxval = MAX_LIMIT;
3384 }
3385 else if (VIM_ISDIGIT(*first_char))
3386 *maxval = *minval; /* It was \{n} or \{-n} */
3387 else
3388 *maxval = MAX_LIMIT; /* It was \{} or \{-} */
3389 if (*regparse == '\\')
3390 regparse++; /* Allow either \{...} or \{...\} */
Bram Moolenaardf177f62005-02-22 08:39:57 +00003391 if (*regparse != '}')
Bram Moolenaar1be45b22019-01-14 22:46:15 +01003392 EMSG2_RET_FAIL(_("E554: Syntax error in %s{...}"),
3393 reg_magic == MAGIC_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394
3395 /*
3396 * Reverse the range if there was a '-', or make sure it is in the right
3397 * order otherwise.
3398 */
3399 if ((!reverse && *minval > *maxval) || (reverse && *minval < *maxval))
3400 {
3401 tmp = *minval;
3402 *minval = *maxval;
3403 *maxval = tmp;
3404 }
3405 skipchr(); /* let's be friends with the lexer again */
3406 return OK;
3407}
3408
3409/*
3410 * vim_regexec and friends
3411 */
3412
3413/*
3414 * Global work variables for vim_regexec().
3415 */
3416
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417/*
3418 * Structure used to save the current input state, when it needs to be
3419 * restored after trying a match. Used by reg_save() and reg_restore().
Bram Moolenaar582fd852005-03-28 20:58:01 +00003420 * Also stores the length of "backpos".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 */
3422typedef struct
3423{
3424 union
3425 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02003426 char_u *ptr; /* rex.input pointer, for single-line regexp */
3427 lpos_T pos; /* rex.input pos, for multi-line regexp */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 } rs_u;
Bram Moolenaar582fd852005-03-28 20:58:01 +00003429 int rs_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430} regsave_T;
3431
3432/* struct to save start/end pointer/position in for \(\) */
3433typedef struct
3434{
3435 union
3436 {
3437 char_u *ptr;
3438 lpos_T pos;
3439 } se_u;
3440} save_se_T;
3441
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00003442/* used for BEHIND and NOBEHIND matching */
3443typedef struct regbehind_S
3444{
3445 regsave_T save_after;
3446 regsave_T save_behind;
Bram Moolenaarfde483c2008-06-15 12:21:50 +00003447 int save_need_clear_subexpr;
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00003448 save_se_T save_start[NSUBEXP];
3449 save_se_T save_end[NSUBEXP];
3450} regbehind_T;
3451
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02003452static long bt_regexec_both(char_u *line, colnr_T col, proftime_T *tm, int *timed_out);
Bram Moolenaar09463262017-06-17 20:55:06 +02003453static long regtry(bt_regprog_T *prog, colnr_T col, proftime_T *tm, int *timed_out);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003454static void cleanup_subexpr(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003456static void cleanup_zsubexpr(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003458static void save_subexpr(regbehind_T *bp);
3459static void restore_subexpr(regbehind_T *bp);
3460static void reg_nextline(void);
3461static void reg_save(regsave_T *save, garray_T *gap);
3462static void reg_restore(regsave_T *save, garray_T *gap);
3463static int reg_save_equal(regsave_T *save);
3464static void save_se_multi(save_se_T *savep, lpos_T *posp);
3465static void save_se_one(save_se_T *savep, char_u **pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466
3467/* Save the sub-expressions before attempting a match. */
3468#define save_se(savep, posp, pp) \
3469 REG_MULTI ? save_se_multi((savep), (posp)) : save_se_one((savep), (pp))
3470
3471/* After a failed match restore the sub-expressions. */
3472#define restore_se(savep, posp, pp) { \
3473 if (REG_MULTI) \
3474 *(posp) = (savep)->se_u.pos; \
3475 else \
3476 *(pp) = (savep)->se_u.ptr; }
3477
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003478static int re_num_cmp(long_u val, char_u *scan);
3479static int match_with_backref(linenr_T start_lnum, colnr_T start_col, linenr_T end_lnum, colnr_T end_col, int *bytelen);
Bram Moolenaar09463262017-06-17 20:55:06 +02003480static int regmatch(char_u *prog, proftime_T *tm, int *timed_out);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003481static int regrepeat(char_u *p, long maxcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482
3483#ifdef DEBUG
3484int regnarrate = 0;
3485#endif
3486
3487/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 * Sometimes need to save a copy of a line. Since alloc()/free() is very
3489 * slow, we keep one allocated piece of memory and only re-allocate it when
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003490 * it's too small. It's freed in bt_regexec_both() when finished.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 */
Bram Moolenaard4210772008-01-02 14:35:30 +00003492static char_u *reg_tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493static unsigned reg_tofreelen;
3494
3495/*
Bram Moolenaar6100d022016-10-02 16:51:57 +02003496 * Structure used to store the execution state of the regex engine.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00003497 * Which ones are set depends on whether a single-line or multi-line match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 * done:
3499 * single-line multi-line
3500 * reg_match &regmatch_T NULL
3501 * reg_mmatch NULL &regmmatch_T
3502 * reg_startp reg_match->startp <invalid>
3503 * reg_endp reg_match->endp <invalid>
3504 * reg_startpos <invalid> reg_mmatch->startpos
3505 * reg_endpos <invalid> reg_mmatch->endpos
3506 * reg_win NULL window in which to search
Bram Moolenaar2f315ab2013-01-25 20:11:01 +01003507 * reg_buf curbuf buffer in which to search
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 * reg_firstlnum <invalid> first line in which to search
3509 * reg_maxline 0 last line nr
3510 * reg_line_lbr FALSE or TRUE FALSE
3511 */
Bram Moolenaar6100d022016-10-02 16:51:57 +02003512typedef struct {
3513 regmatch_T *reg_match;
3514 regmmatch_T *reg_mmatch;
3515 char_u **reg_startp;
3516 char_u **reg_endp;
3517 lpos_T *reg_startpos;
3518 lpos_T *reg_endpos;
3519 win_T *reg_win;
3520 buf_T *reg_buf;
3521 linenr_T reg_firstlnum;
3522 linenr_T reg_maxline;
3523 int reg_line_lbr; /* "\n" in string is line break */
3524
Bram Moolenaar0270f382018-07-17 05:43:58 +02003525 // The current match-position is stord in these variables:
3526 linenr_T lnum; // line number, relative to first line
3527 char_u *line; // start of current line
3528 char_u *input; // current input, points into "regline"
3529
3530 int need_clear_subexpr; // subexpressions still need to be cleared
3531#ifdef FEAT_SYN_HL
3532 int need_clear_zsubexpr; // extmatch subexpressions still need to be
3533 // cleared
3534#endif
3535
Bram Moolenaar6100d022016-10-02 16:51:57 +02003536 /* Internal copy of 'ignorecase'. It is set at each call to vim_regexec().
3537 * Normally it gets the value of "rm_ic" or "rmm_ic", but when the pattern
3538 * contains '\c' or '\C' the value is overruled. */
3539 int reg_ic;
3540
3541#ifdef FEAT_MBYTE
Bram Moolenaar0270f382018-07-17 05:43:58 +02003542 /* Similar to "reg_ic", but only for 'combining' characters. Set with \Z
Bram Moolenaar6100d022016-10-02 16:51:57 +02003543 * flag in the regexp. Defaults to false, always. */
3544 int reg_icombine;
3545#endif
3546
3547 /* Copy of "rmm_maxcol": maximum column to search for a match. Zero when
3548 * there is no maximum. */
3549 colnr_T reg_maxcol;
Bram Moolenaar0270f382018-07-17 05:43:58 +02003550
3551 // State for the NFA engine regexec.
3552 int nfa_has_zend; // NFA regexp \ze operator encountered.
3553 int nfa_has_backref; // NFA regexp \1 .. \9 encountered.
3554 int nfa_nsubexpr; // Number of sub expressions actually being used
3555 // during execution. 1 if only the whole match
3556 // (subexpr 0) is used.
3557 // listid is global, so that it increases on recursive calls to
3558 // nfa_regmatch(), which means we don't have to clear the lastlist field of
3559 // all the states.
3560 int nfa_listid;
3561 int nfa_alt_listid;
3562
3563#ifdef FEAT_SYN_HL
3564 int nfa_has_zsubexpr; // NFA regexp has \z( ), set zsubexpr.
3565#endif
Bram Moolenaar6100d022016-10-02 16:51:57 +02003566} regexec_T;
3567
3568static regexec_T rex;
3569static int rex_in_use = FALSE;
3570
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003572/* Values for rs_state in regitem_T. */
3573typedef enum regstate_E
3574{
3575 RS_NOPEN = 0 /* NOPEN and NCLOSE */
3576 , RS_MOPEN /* MOPEN + [0-9] */
3577 , RS_MCLOSE /* MCLOSE + [0-9] */
3578#ifdef FEAT_SYN_HL
3579 , RS_ZOPEN /* ZOPEN + [0-9] */
3580 , RS_ZCLOSE /* ZCLOSE + [0-9] */
3581#endif
3582 , RS_BRANCH /* BRANCH */
3583 , RS_BRCPLX_MORE /* BRACE_COMPLEX and trying one more match */
3584 , RS_BRCPLX_LONG /* BRACE_COMPLEX and trying longest match */
3585 , RS_BRCPLX_SHORT /* BRACE_COMPLEX and trying shortest match */
3586 , RS_NOMATCH /* NOMATCH */
3587 , RS_BEHIND1 /* BEHIND / NOBEHIND matching rest */
3588 , RS_BEHIND2 /* BEHIND / NOBEHIND matching behind part */
3589 , RS_STAR_LONG /* STAR/PLUS/BRACE_SIMPLE longest match */
3590 , RS_STAR_SHORT /* STAR/PLUS/BRACE_SIMPLE shortest match */
3591} regstate_T;
3592
3593/*
3594 * When there are alternatives a regstate_T is put on the regstack to remember
3595 * what we are doing.
3596 * Before it may be another type of item, depending on rs_state, to remember
3597 * more things.
3598 */
3599typedef struct regitem_S
3600{
3601 regstate_T rs_state; /* what we are doing, one of RS_ above */
3602 char_u *rs_scan; /* current node in program */
3603 union
3604 {
3605 save_se_T sesave;
3606 regsave_T regsave;
Bram Moolenaar0270f382018-07-17 05:43:58 +02003607 } rs_un; /* room for saving rex.input */
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00003608 short rs_no; /* submatch nr or BEHIND/NOBEHIND */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003609} regitem_T;
3610
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003611static regitem_T *regstack_push(regstate_T state, char_u *scan);
3612static void regstack_pop(char_u **scan);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003613
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003614/* used for STAR, PLUS and BRACE_SIMPLE matching */
3615typedef struct regstar_S
3616{
3617 int nextb; /* next byte */
3618 int nextb_ic; /* next byte reverse case */
3619 long count;
3620 long minval;
3621 long maxval;
3622} regstar_T;
3623
3624/* used to store input position when a BACK was encountered, so that we now if
3625 * we made any progress since the last time. */
3626typedef struct backpos_S
3627{
3628 char_u *bp_scan; /* "scan" where BACK was encountered */
3629 regsave_T bp_pos; /* last input position */
3630} backpos_T;
3631
3632/*
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003633 * "regstack" and "backpos" are used by regmatch(). They are kept over calls
3634 * to avoid invoking malloc() and free() often.
3635 * "regstack" is a stack with regitem_T items, sometimes preceded by regstar_T
3636 * or regbehind_T.
3637 * "backpos_T" is a table with backpos_T for BACK
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003638 */
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003639static garray_T regstack = {0, 0, 0, 0, NULL};
3640static garray_T backpos = {0, 0, 0, 0, NULL};
3641
3642/*
3643 * Both for regstack and backpos tables we use the following strategy of
3644 * allocation (to reduce malloc/free calls):
3645 * - Initial size is fairly small.
3646 * - When needed, the tables are grown bigger (8 times at first, double after
3647 * that).
3648 * - After executing the match we free the memory only if the array has grown.
3649 * Thus the memory is kept allocated when it's at the initial size.
3650 * This makes it fast while not keeping a lot of memory allocated.
3651 * A three times speed increase was observed when using many simple patterns.
3652 */
3653#define REGSTACK_INITIAL 2048
3654#define BACKPOS_INITIAL 64
3655
3656#if defined(EXITFREE) || defined(PROTO)
3657 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003658free_regexp_stuff(void)
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003659{
3660 ga_clear(&regstack);
3661 ga_clear(&backpos);
3662 vim_free(reg_tofree);
3663 vim_free(reg_prev_sub);
3664}
3665#endif
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003666
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667/*
3668 * Get pointer to the line "lnum", which is relative to "reg_firstlnum".
3669 */
3670 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003671reg_getline(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672{
3673 /* when looking behind for a match/no-match lnum is negative. But we
3674 * can't go before line 1 */
Bram Moolenaar6100d022016-10-02 16:51:57 +02003675 if (rex.reg_firstlnum + lnum < 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 return NULL;
Bram Moolenaar6100d022016-10-02 16:51:57 +02003677 if (lnum > rex.reg_maxline)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003678 /* Must have matched the "\n" in the last line. */
3679 return (char_u *)"";
Bram Moolenaar6100d022016-10-02 16:51:57 +02003680 return ml_get_buf(rex.reg_buf, rex.reg_firstlnum + lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681}
3682
3683static regsave_T behind_pos;
3684
3685#ifdef FEAT_SYN_HL
3686static char_u *reg_startzp[NSUBEXP]; /* Workspace to mark beginning */
3687static char_u *reg_endzp[NSUBEXP]; /* and end of \z(...\) matches */
3688static lpos_T reg_startzpos[NSUBEXP]; /* idem, beginning pos */
3689static lpos_T reg_endzpos[NSUBEXP]; /* idem, end pos */
3690#endif
3691
3692/* TRUE if using multi-line regexp. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02003693#define REG_MULTI (rex.reg_match == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003694
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695/*
3696 * Match a regexp against a string.
3697 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
3698 * Uses curbuf for line count and 'iskeyword'.
Bram Moolenaar2af78a12014-04-23 19:06:37 +02003699 * if "line_lbr" is TRUE consider a "\n" in "line" to be a line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 *
Bram Moolenaar66a3e792014-11-20 23:07:05 +01003701 * Returns 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003703 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003704bt_regexec_nl(
3705 regmatch_T *rmp,
3706 char_u *line, /* string to match against */
3707 colnr_T col, /* column to start looking for match */
3708 int line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709{
Bram Moolenaar6100d022016-10-02 16:51:57 +02003710 rex.reg_match = rmp;
3711 rex.reg_mmatch = NULL;
3712 rex.reg_maxline = 0;
3713 rex.reg_line_lbr = line_lbr;
3714 rex.reg_buf = curbuf;
3715 rex.reg_win = NULL;
3716 rex.reg_ic = rmp->rm_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02003718 rex.reg_icombine = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719#endif
Bram Moolenaar6100d022016-10-02 16:51:57 +02003720 rex.reg_maxcol = 0;
Bram Moolenaar66a3e792014-11-20 23:07:05 +01003721
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02003722 return bt_regexec_both(line, col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723}
3724
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725/*
3726 * Match a regexp against multiple lines.
3727 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
3728 * Uses curbuf for line count and 'iskeyword'.
3729 *
3730 * Return zero if there is no match. Return number of lines contained in the
3731 * match otherwise.
3732 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003733 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01003734bt_regexec_multi(
3735 regmmatch_T *rmp,
3736 win_T *win, /* window in which to search or NULL */
3737 buf_T *buf, /* buffer in which to search */
3738 linenr_T lnum, /* nr of line to start looking for match */
3739 colnr_T col, /* column to start looking for match */
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02003740 proftime_T *tm, /* timeout limit or NULL */
3741 int *timed_out) /* flag set on timeout or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742{
Bram Moolenaar6100d022016-10-02 16:51:57 +02003743 rex.reg_match = NULL;
3744 rex.reg_mmatch = rmp;
3745 rex.reg_buf = buf;
3746 rex.reg_win = win;
3747 rex.reg_firstlnum = lnum;
3748 rex.reg_maxline = rex.reg_buf->b_ml.ml_line_count - lnum;
3749 rex.reg_line_lbr = FALSE;
3750 rex.reg_ic = rmp->rmm_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02003752 rex.reg_icombine = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753#endif
Bram Moolenaar6100d022016-10-02 16:51:57 +02003754 rex.reg_maxcol = rmp->rmm_maxcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02003756 return bt_regexec_both(NULL, col, tm, timed_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757}
3758
3759/*
3760 * Match a regexp against a string ("line" points to the string) or multiple
3761 * lines ("line" is NULL, use reg_getline()).
Bram Moolenaar66a3e792014-11-20 23:07:05 +01003762 * Returns 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01003765bt_regexec_both(
3766 char_u *line,
3767 colnr_T col, /* column to start looking for match */
Bram Moolenaar09463262017-06-17 20:55:06 +02003768 proftime_T *tm, /* timeout limit or NULL */
3769 int *timed_out) /* flag set on timeout or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770{
Bram Moolenaar66a3e792014-11-20 23:07:05 +01003771 bt_regprog_T *prog;
3772 char_u *s;
3773 long retval = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003775 /* Create "regstack" and "backpos" if they are not allocated yet.
3776 * We allocate *_INITIAL amount of bytes first and then set the grow size
3777 * to much bigger value to avoid many malloc calls in case of deep regular
3778 * expressions. */
3779 if (regstack.ga_data == NULL)
3780 {
3781 /* Use an item size of 1 byte, since we push different things
3782 * onto the regstack. */
3783 ga_init2(&regstack, 1, REGSTACK_INITIAL);
Bram Moolenaarcde88542015-08-11 19:14:00 +02003784 (void)ga_grow(&regstack, REGSTACK_INITIAL);
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003785 regstack.ga_growsize = REGSTACK_INITIAL * 8;
3786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003788 if (backpos.ga_data == NULL)
3789 {
3790 ga_init2(&backpos, sizeof(backpos_T), BACKPOS_INITIAL);
Bram Moolenaarcde88542015-08-11 19:14:00 +02003791 (void)ga_grow(&backpos, BACKPOS_INITIAL);
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003792 backpos.ga_growsize = BACKPOS_INITIAL * 8;
3793 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003794
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 if (REG_MULTI)
3796 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02003797 prog = (bt_regprog_T *)rex.reg_mmatch->regprog;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 line = reg_getline((linenr_T)0);
Bram Moolenaar6100d022016-10-02 16:51:57 +02003799 rex.reg_startpos = rex.reg_mmatch->startpos;
3800 rex.reg_endpos = rex.reg_mmatch->endpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 }
3802 else
3803 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02003804 prog = (bt_regprog_T *)rex.reg_match->regprog;
3805 rex.reg_startp = rex.reg_match->startp;
3806 rex.reg_endp = rex.reg_match->endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 }
3808
3809 /* Be paranoid... */
3810 if (prog == NULL || line == NULL)
3811 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003812 emsg(_(e_null));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 goto theend;
3814 }
3815
3816 /* Check validity of program. */
3817 if (prog_magic_wrong())
3818 goto theend;
3819
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003820 /* If the start column is past the maximum column: no need to try. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02003821 if (rex.reg_maxcol > 0 && col >= rex.reg_maxcol)
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003822 goto theend;
3823
Bram Moolenaar6100d022016-10-02 16:51:57 +02003824 /* If pattern contains "\c" or "\C": overrule value of rex.reg_ic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 if (prog->regflags & RF_ICASE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02003826 rex.reg_ic = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 else if (prog->regflags & RF_NOICASE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02003828 rex.reg_ic = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829
3830#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02003831 /* If pattern contains "\Z" overrule value of rex.reg_icombine */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 if (prog->regflags & RF_ICOMBINE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02003833 rex.reg_icombine = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834#endif
3835
3836 /* If there is a "must appear" string, look for it. */
3837 if (prog->regmust != NULL)
3838 {
3839 int c;
3840
3841#ifdef FEAT_MBYTE
3842 if (has_mbyte)
3843 c = (*mb_ptr2char)(prog->regmust);
3844 else
3845#endif
3846 c = *prog->regmust;
3847 s = line + col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003848
3849 /*
3850 * This is used very often, esp. for ":global". Use three versions of
3851 * the loop to avoid overhead of conditions.
3852 */
Bram Moolenaar6100d022016-10-02 16:51:57 +02003853 if (!rex.reg_ic
Bram Moolenaar05159a02005-02-26 23:04:13 +00003854#ifdef FEAT_MBYTE
3855 && !has_mbyte
3856#endif
3857 )
3858 while ((s = vim_strbyte(s, c)) != NULL)
3859 {
3860 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
3861 break; /* Found it. */
3862 ++s;
3863 }
3864#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02003865 else if (!rex.reg_ic || (!enc_utf8 && mb_char2len(c) > 1))
Bram Moolenaar05159a02005-02-26 23:04:13 +00003866 while ((s = vim_strchr(s, c)) != NULL)
3867 {
3868 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
3869 break; /* Found it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003870 MB_PTR_ADV(s);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003871 }
3872#endif
3873 else
3874 while ((s = cstrchr(s, c)) != NULL)
3875 {
3876 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
3877 break; /* Found it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003878 MB_PTR_ADV(s);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 if (s == NULL) /* Not present. */
3881 goto theend;
3882 }
3883
Bram Moolenaar0270f382018-07-17 05:43:58 +02003884 rex.line = line;
3885 rex.lnum = 0;
Bram Moolenaar73a92fe2010-09-14 10:55:47 +02003886 reg_toolong = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887
3888 /* Simplest case: Anchored match need be tried only once. */
3889 if (prog->reganch)
3890 {
3891 int c;
3892
3893#ifdef FEAT_MBYTE
3894 if (has_mbyte)
Bram Moolenaar0270f382018-07-17 05:43:58 +02003895 c = (*mb_ptr2char)(rex.line + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 else
3897#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02003898 c = rex.line[col];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 if (prog->regstart == NUL
3900 || prog->regstart == c
Bram Moolenaar6100d022016-10-02 16:51:57 +02003901 || (rex.reg_ic && ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902#ifdef FEAT_MBYTE
3903 (enc_utf8 && utf_fold(prog->regstart) == utf_fold(c)))
3904 || (c < 255 && prog->regstart < 255 &&
3905#endif
Bram Moolenaara245a5b2007-08-11 11:58:23 +00003906 MB_TOLOWER(prog->regstart) == MB_TOLOWER(c)))))
Bram Moolenaar09463262017-06-17 20:55:06 +02003907 retval = regtry(prog, col, tm, timed_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 else
3909 retval = 0;
3910 }
3911 else
3912 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003913#ifdef FEAT_RELTIME
3914 int tm_count = 0;
3915#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 /* Messy cases: unanchored match. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003917 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 {
3919 if (prog->regstart != NUL)
3920 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003921 /* Skip until the char we know it must start with.
3922 * Used often, do some work to avoid call overhead. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02003923 if (!rex.reg_ic
Bram Moolenaar05159a02005-02-26 23:04:13 +00003924#ifdef FEAT_MBYTE
3925 && !has_mbyte
3926#endif
3927 )
Bram Moolenaar0270f382018-07-17 05:43:58 +02003928 s = vim_strbyte(rex.line + col, prog->regstart);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003929 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02003930 s = cstrchr(rex.line + col, prog->regstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 if (s == NULL)
3932 {
3933 retval = 0;
3934 break;
3935 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02003936 col = (int)(s - rex.line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 }
3938
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003939 /* Check for maximum column to try. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02003940 if (rex.reg_maxcol > 0 && col >= rex.reg_maxcol)
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003941 {
3942 retval = 0;
3943 break;
3944 }
3945
Bram Moolenaar09463262017-06-17 20:55:06 +02003946 retval = regtry(prog, col, tm, timed_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 if (retval > 0)
3948 break;
3949
3950 /* if not currently on the first line, get it again */
Bram Moolenaar0270f382018-07-17 05:43:58 +02003951 if (rex.lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02003953 rex.lnum = 0;
3954 rex.line = reg_getline((linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02003956 if (rex.line[col] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 break;
3958#ifdef FEAT_MBYTE
3959 if (has_mbyte)
Bram Moolenaar0270f382018-07-17 05:43:58 +02003960 col += (*mb_ptr2len)(rex.line + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 else
3962#endif
3963 ++col;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003964#ifdef FEAT_RELTIME
3965 /* Check for timeout once in a twenty times to avoid overhead. */
3966 if (tm != NULL && ++tm_count == 20)
3967 {
3968 tm_count = 0;
3969 if (profile_passed_limit(tm))
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02003970 {
3971 if (timed_out != NULL)
3972 *timed_out = TRUE;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003973 break;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02003974 }
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003975 }
3976#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 }
3978 }
3979
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980theend:
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003981 /* Free "reg_tofree" when it's a bit big.
3982 * Free regstack and backpos if they are bigger than their initial size. */
3983 if (reg_tofreelen > 400)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003984 VIM_CLEAR(reg_tofree);
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003985 if (regstack.ga_maxlen > REGSTACK_INITIAL)
3986 ga_clear(&regstack);
3987 if (backpos.ga_maxlen > BACKPOS_INITIAL)
3988 ga_clear(&backpos);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003989
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 return retval;
3991}
3992
3993#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994/*
3995 * Create a new extmatch and mark it as referenced once.
3996 */
3997 static reg_extmatch_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003998make_extmatch(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999{
4000 reg_extmatch_T *em;
4001
4002 em = (reg_extmatch_T *)alloc_clear((unsigned)sizeof(reg_extmatch_T));
4003 if (em != NULL)
4004 em->refcnt = 1;
4005 return em;
4006}
4007
4008/*
4009 * Add a reference to an extmatch.
4010 */
4011 reg_extmatch_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004012ref_extmatch(reg_extmatch_T *em)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013{
4014 if (em != NULL)
4015 em->refcnt++;
4016 return em;
4017}
4018
4019/*
4020 * Remove a reference to an extmatch. If there are no references left, free
4021 * the info.
4022 */
4023 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004024unref_extmatch(reg_extmatch_T *em)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025{
4026 int i;
4027
4028 if (em != NULL && --em->refcnt <= 0)
4029 {
4030 for (i = 0; i < NSUBEXP; ++i)
4031 vim_free(em->matches[i]);
4032 vim_free(em);
4033 }
4034}
4035#endif
4036
4037/*
Bram Moolenaar0270f382018-07-17 05:43:58 +02004038 * regtry - try match of "prog" with at rex.line["col"].
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 * Returns 0 for failure, number of lines contained in the match otherwise.
4040 */
4041 static long
Bram Moolenaar09463262017-06-17 20:55:06 +02004042regtry(
4043 bt_regprog_T *prog,
4044 colnr_T col,
4045 proftime_T *tm, /* timeout limit or NULL */
4046 int *timed_out) /* flag set on timeout or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047{
Bram Moolenaar0270f382018-07-17 05:43:58 +02004048 rex.input = rex.line + col;
4049 rex.need_clear_subexpr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02004051 // Clear the external match subpointers if necessary.
4052 rex.need_clear_zsubexpr = (prog->reghasz == REX_SET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053#endif
4054
Bram Moolenaar09463262017-06-17 20:55:06 +02004055 if (regmatch(prog->program + 1, tm, timed_out) == 0)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004056 return 0;
4057
4058 cleanup_subexpr();
4059 if (REG_MULTI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02004061 if (rex.reg_startpos[0].lnum < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02004063 rex.reg_startpos[0].lnum = 0;
4064 rex.reg_startpos[0].col = col;
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004065 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02004066 if (rex.reg_endpos[0].lnum < 0)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004067 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004068 rex.reg_endpos[0].lnum = rex.lnum;
4069 rex.reg_endpos[0].col = (int)(rex.input - rex.line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 }
4071 else
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004072 /* Use line number of "\ze". */
Bram Moolenaar0270f382018-07-17 05:43:58 +02004073 rex.lnum = rex.reg_endpos[0].lnum;
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004074 }
4075 else
4076 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02004077 if (rex.reg_startp[0] == NULL)
Bram Moolenaar0270f382018-07-17 05:43:58 +02004078 rex.reg_startp[0] = rex.line + col;
Bram Moolenaar6100d022016-10-02 16:51:57 +02004079 if (rex.reg_endp[0] == NULL)
Bram Moolenaar0270f382018-07-17 05:43:58 +02004080 rex.reg_endp[0] = rex.input;
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082#ifdef FEAT_SYN_HL
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004083 /* Package any found \z(...\) matches for export. Default is none. */
4084 unref_extmatch(re_extmatch_out);
4085 re_extmatch_out = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004087 if (prog->reghasz == REX_SET)
4088 {
4089 int i;
4090
4091 cleanup_zsubexpr();
4092 re_extmatch_out = make_extmatch();
4093 for (i = 0; i < NSUBEXP; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004095 if (REG_MULTI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004097 /* Only accept single line matches. */
4098 if (reg_startzpos[i].lnum >= 0
Bram Moolenaar5a4e1602014-04-06 21:34:04 +02004099 && reg_endzpos[i].lnum == reg_startzpos[i].lnum
4100 && reg_endzpos[i].col >= reg_startzpos[i].col)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004101 re_extmatch_out->matches[i] =
4102 vim_strnsave(reg_getline(reg_startzpos[i].lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 + reg_startzpos[i].col,
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004104 reg_endzpos[i].col - reg_startzpos[i].col);
4105 }
4106 else
4107 {
4108 if (reg_startzp[i] != NULL && reg_endzp[i] != NULL)
4109 re_extmatch_out->matches[i] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 vim_strnsave(reg_startzp[i],
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004111 (int)(reg_endzp[i] - reg_startzp[i]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 }
4113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 }
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004115#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02004116 return 1 + rex.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117}
4118
4119#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120/*
4121 * Get class of previous character.
4122 */
4123 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004124reg_prev_class(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125{
Bram Moolenaar0270f382018-07-17 05:43:58 +02004126 if (rex.input > rex.line)
4127 return mb_get_class_buf(rex.input - 1
4128 - (*mb_head_off)(rex.line, rex.input - 1), rex.reg_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 return -1;
4130}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004132
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004133/*
Bram Moolenaar0270f382018-07-17 05:43:58 +02004134 * Return TRUE if the current rex.input position matches the Visual area.
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004135 */
4136 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004137reg_match_visual(void)
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004138{
4139 pos_T top, bot;
4140 linenr_T lnum;
4141 colnr_T col;
Bram Moolenaar6100d022016-10-02 16:51:57 +02004142 win_T *wp = rex.reg_win == NULL ? curwin : rex.reg_win;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004143 int mode;
4144 colnr_T start, end;
4145 colnr_T start2, end2;
4146 colnr_T cols;
4147
4148 /* Check if the buffer is the current buffer. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02004149 if (rex.reg_buf != curbuf || VIsual.lnum == 0)
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004150 return FALSE;
4151
4152 if (VIsual_active)
4153 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004154 if (LT_POS(VIsual, wp->w_cursor))
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004155 {
4156 top = VIsual;
4157 bot = wp->w_cursor;
4158 }
4159 else
4160 {
4161 top = wp->w_cursor;
4162 bot = VIsual;
4163 }
4164 mode = VIsual_mode;
4165 }
4166 else
4167 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004168 if (LT_POS(curbuf->b_visual.vi_start, curbuf->b_visual.vi_end))
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004169 {
4170 top = curbuf->b_visual.vi_start;
4171 bot = curbuf->b_visual.vi_end;
4172 }
4173 else
4174 {
4175 top = curbuf->b_visual.vi_end;
4176 bot = curbuf->b_visual.vi_start;
4177 }
4178 mode = curbuf->b_visual.vi_mode;
4179 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02004180 lnum = rex.lnum + rex.reg_firstlnum;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004181 if (lnum < top.lnum || lnum > bot.lnum)
4182 return FALSE;
4183
4184 if (mode == 'v')
4185 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004186 col = (colnr_T)(rex.input - rex.line);
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004187 if ((lnum == top.lnum && col < top.col)
4188 || (lnum == bot.lnum && col >= bot.col + (*p_sel != 'e')))
4189 return FALSE;
4190 }
4191 else if (mode == Ctrl_V)
4192 {
4193 getvvcol(wp, &top, &start, NULL, &end);
4194 getvvcol(wp, &bot, &start2, NULL, &end2);
4195 if (start2 < start)
4196 start = start2;
4197 if (end2 > end)
4198 end = end2;
4199 if (top.col == MAXCOL || bot.col == MAXCOL)
4200 end = MAXCOL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02004201 cols = win_linetabsize(wp, rex.line, (colnr_T)(rex.input - rex.line));
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004202 if (cols < start || cols > end - (*p_sel == 'e'))
4203 return FALSE;
4204 }
4205 return TRUE;
4206}
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004207
Bram Moolenaar0270f382018-07-17 05:43:58 +02004208#define ADVANCE_REGINPUT() MB_PTR_ADV(rex.input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209
4210/*
4211 * The arguments from BRACE_LIMITS are stored here. They are actually local
4212 * to regmatch(), but they are here to reduce the amount of stack space used
4213 * (it can be called recursively many times).
4214 */
4215static long bl_minval;
4216static long bl_maxval;
4217
4218/*
4219 * regmatch - main matching routine
4220 *
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004221 * Conceptually the strategy is simple: Check to see whether the current node
4222 * matches, push an item onto the regstack and loop to see whether the rest
4223 * matches, and then act accordingly. In practice we make some effort to
4224 * avoid using the regstack, in particular by going through "ordinary" nodes
4225 * (that don't need to know whether the rest of the match failed) by a nested
4226 * loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 *
Bram Moolenaar0270f382018-07-17 05:43:58 +02004228 * Returns TRUE when there is a match. Leaves rex.input and rex.lnum just after
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 * the last matched character.
Bram Moolenaar0270f382018-07-17 05:43:58 +02004230 * Returns FALSE when there is no match. Leaves rex.input and rex.lnum in an
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 * undefined state!
4232 */
4233 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004234regmatch(
Bram Moolenaar09463262017-06-17 20:55:06 +02004235 char_u *scan, /* Current node. */
4236 proftime_T *tm UNUSED, /* timeout limit or NULL */
4237 int *timed_out UNUSED) /* flag set on timeout or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004239 char_u *next; /* Next node. */
4240 int op;
4241 int c;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004242 regitem_T *rp;
4243 int no;
4244 int status; /* one of the RA_ values: */
4245#define RA_FAIL 1 /* something failed, abort */
4246#define RA_CONT 2 /* continue in inner loop */
4247#define RA_BREAK 3 /* break inner loop */
4248#define RA_MATCH 4 /* successful match */
4249#define RA_NOMATCH 5 /* didn't match */
Bram Moolenaar09463262017-06-17 20:55:06 +02004250#ifdef FEAT_RELTIME
4251 int tm_count = 0;
4252#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00004254 /* Make "regstack" and "backpos" empty. They are allocated and freed in
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004255 * bt_regexec_both() to reduce malloc()/free() calls. */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004256 regstack.ga_len = 0;
4257 backpos.ga_len = 0;
Bram Moolenaar582fd852005-03-28 20:58:01 +00004258
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004259 /*
Bram Moolenaar582fd852005-03-28 20:58:01 +00004260 * Repeat until "regstack" is empty.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004261 */
4262 for (;;)
4263 {
Bram Moolenaar41f12052013-08-25 17:01:42 +02004264 /* Some patterns may take a long time to match, e.g., "\([a-z]\+\)\+Q".
4265 * Allow interrupting them with CTRL-C. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 fast_breakcheck();
4267
4268#ifdef DEBUG
4269 if (scan != NULL && regnarrate)
4270 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004271 mch_errmsg((char *)regprop(scan));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 mch_errmsg("(\n");
4273 }
4274#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004275
4276 /*
Bram Moolenaar582fd852005-03-28 20:58:01 +00004277 * Repeat for items that can be matched sequentially, without using the
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004278 * regstack.
4279 */
4280 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004282 if (got_int || scan == NULL)
4283 {
4284 status = RA_FAIL;
4285 break;
4286 }
Bram Moolenaar09463262017-06-17 20:55:06 +02004287#ifdef FEAT_RELTIME
4288 /* Check for timeout once in a 100 times to avoid overhead. */
4289 if (tm != NULL && ++tm_count == 100)
4290 {
4291 tm_count = 0;
4292 if (profile_passed_limit(tm))
4293 {
4294 if (timed_out != NULL)
4295 *timed_out = TRUE;
4296 status = RA_FAIL;
4297 break;
4298 }
4299 }
4300#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004301 status = RA_CONT;
4302
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303#ifdef DEBUG
4304 if (regnarrate)
4305 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004306 mch_errmsg((char *)regprop(scan));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 mch_errmsg("...\n");
4308# ifdef FEAT_SYN_HL
4309 if (re_extmatch_in != NULL)
4310 {
4311 int i;
4312
4313 mch_errmsg(_("External submatches:\n"));
4314 for (i = 0; i < NSUBEXP; i++)
4315 {
4316 mch_errmsg(" \"");
4317 if (re_extmatch_in->matches[i] != NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004318 mch_errmsg((char *)re_extmatch_in->matches[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 mch_errmsg("\"\n");
4320 }
4321 }
4322# endif
4323 }
4324#endif
4325 next = regnext(scan);
4326
4327 op = OP(scan);
4328 /* Check for character class with NL added. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02004329 if (!rex.reg_line_lbr && WITH_NL(op) && REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02004330 && *rex.input == NUL && rex.lnum <= rex.reg_maxline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 {
4332 reg_nextline();
4333 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02004334 else if (rex.reg_line_lbr && WITH_NL(op) && *rex.input == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 {
4336 ADVANCE_REGINPUT();
4337 }
4338 else
4339 {
4340 if (WITH_NL(op))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004341 op -= ADD_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342#ifdef FEAT_MBYTE
4343 if (has_mbyte)
Bram Moolenaar0270f382018-07-17 05:43:58 +02004344 c = (*mb_ptr2char)(rex.input);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 else
4346#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02004347 c = *rex.input;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 switch (op)
4349 {
4350 case BOL:
Bram Moolenaar0270f382018-07-17 05:43:58 +02004351 if (rex.input != rex.line)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004352 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 break;
4354
4355 case EOL:
4356 if (c != NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004357 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 break;
4359
4360 case RE_BOF:
Bram Moolenaara7139332007-12-09 18:26:22 +00004361 /* We're not at the beginning of the file when below the first
4362 * line where we started, not at the start of the line or we
4363 * didn't start at the first line of the buffer. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02004364 if (rex.lnum != 0 || rex.input != rex.line
Bram Moolenaar6100d022016-10-02 16:51:57 +02004365 || (REG_MULTI && rex.reg_firstlnum > 1))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004366 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 break;
4368
4369 case RE_EOF:
Bram Moolenaar0270f382018-07-17 05:43:58 +02004370 if (rex.lnum != rex.reg_maxline || c != NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004371 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 break;
4373
4374 case CURSOR:
4375 /* Check if the buffer is in a window and compare the
Bram Moolenaar6100d022016-10-02 16:51:57 +02004376 * rex.reg_win->w_cursor position to the match position. */
4377 if (rex.reg_win == NULL
Bram Moolenaar0270f382018-07-17 05:43:58 +02004378 || (rex.lnum + rex.reg_firstlnum
Bram Moolenaar6100d022016-10-02 16:51:57 +02004379 != rex.reg_win->w_cursor.lnum)
Bram Moolenaar0270f382018-07-17 05:43:58 +02004380 || ((colnr_T)(rex.input - rex.line)
Bram Moolenaar6100d022016-10-02 16:51:57 +02004381 != rex.reg_win->w_cursor.col))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004382 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 break;
4384
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00004385 case RE_MARK:
Bram Moolenaar044aa292013-06-04 21:27:38 +02004386 /* Compare the mark position to the match position. */
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00004387 {
4388 int mark = OPERAND(scan)[0];
4389 int cmp = OPERAND(scan)[1];
4390 pos_T *pos;
4391
Bram Moolenaar6100d022016-10-02 16:51:57 +02004392 pos = getmark_buf(rex.reg_buf, mark, FALSE);
Bram Moolenaare9400a42007-05-06 13:04:32 +00004393 if (pos == NULL /* mark doesn't exist */
Bram Moolenaar044aa292013-06-04 21:27:38 +02004394 || pos->lnum <= 0 /* mark isn't set in reg_buf */
Bram Moolenaar0270f382018-07-17 05:43:58 +02004395 || (pos->lnum == rex.lnum + rex.reg_firstlnum
4396 ? (pos->col == (colnr_T)(rex.input - rex.line)
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00004397 ? (cmp == '<' || cmp == '>')
Bram Moolenaar0270f382018-07-17 05:43:58 +02004398 : (pos->col < (colnr_T)(rex.input - rex.line)
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00004399 ? cmp != '>'
4400 : cmp != '<'))
Bram Moolenaar0270f382018-07-17 05:43:58 +02004401 : (pos->lnum < rex.lnum + rex.reg_firstlnum
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00004402 ? cmp != '>'
4403 : cmp != '<')))
4404 status = RA_NOMATCH;
4405 }
4406 break;
4407
4408 case RE_VISUAL:
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004409 if (!reg_match_visual())
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004410 status = RA_NOMATCH;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00004411 break;
4412
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 case RE_LNUM:
Bram Moolenaar0270f382018-07-17 05:43:58 +02004414 if (!REG_MULTI || !re_num_cmp((long_u)(rex.lnum + rex.reg_firstlnum),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004416 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 break;
4418
4419 case RE_COL:
Bram Moolenaar0270f382018-07-17 05:43:58 +02004420 if (!re_num_cmp((long_u)(rex.input - rex.line) + 1, scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004421 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 break;
4423
4424 case RE_VCOL:
4425 if (!re_num_cmp((long_u)win_linetabsize(
Bram Moolenaar6100d022016-10-02 16:51:57 +02004426 rex.reg_win == NULL ? curwin : rex.reg_win,
Bram Moolenaar0270f382018-07-17 05:43:58 +02004427 rex.line, (colnr_T)(rex.input - rex.line)) + 1, scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004428 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 break;
4430
Bram Moolenaar0270f382018-07-17 05:43:58 +02004431 case BOW: /* \<word; rex.input points to w */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 if (c == NUL) /* Can't match at end of line */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004433 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434#ifdef FEAT_MBYTE
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004435 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 {
4437 int this_class;
4438
4439 /* Get class of current and previous char (if it exists). */
Bram Moolenaar0270f382018-07-17 05:43:58 +02004440 this_class = mb_get_class_buf(rex.input, rex.reg_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 if (this_class <= 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004442 status = RA_NOMATCH; /* not on a word at all */
4443 else if (reg_prev_class() == this_class)
4444 status = RA_NOMATCH; /* previous char is in same word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 }
4446#endif
4447 else
4448 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004449 if (!vim_iswordc_buf(c, rex.reg_buf) || (rex.input > rex.line
4450 && vim_iswordc_buf(rex.input[-1], rex.reg_buf)))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004451 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 }
4453 break;
4454
Bram Moolenaar0270f382018-07-17 05:43:58 +02004455 case EOW: /* word\>; rex.input points after d */
4456 if (rex.input == rex.line) /* Can't match at start of line */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004457 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458#ifdef FEAT_MBYTE
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004459 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 {
4461 int this_class, prev_class;
4462
4463 /* Get class of current and previous char (if it exists). */
Bram Moolenaar0270f382018-07-17 05:43:58 +02004464 this_class = mb_get_class_buf(rex.input, rex.reg_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 prev_class = reg_prev_class();
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004466 if (this_class == prev_class
4467 || prev_class == 0 || prev_class == 1)
4468 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004471 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004473 if (!vim_iswordc_buf(rex.input[-1], rex.reg_buf)
4474 || (rex.input[0] != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02004475 && vim_iswordc_buf(c, rex.reg_buf)))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004476 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 }
4478 break; /* Matched with EOW */
4479
4480 case ANY:
Bram Moolenaare337e5f2013-01-30 18:21:51 +01004481 /* ANY does not match new lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 if (c == NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004483 status = RA_NOMATCH;
4484 else
4485 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 break;
4487
4488 case IDENT:
4489 if (!vim_isIDc(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004490 status = RA_NOMATCH;
4491 else
4492 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 break;
4494
4495 case SIDENT:
Bram Moolenaar0270f382018-07-17 05:43:58 +02004496 if (VIM_ISDIGIT(*rex.input) || !vim_isIDc(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004497 status = RA_NOMATCH;
4498 else
4499 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 break;
4501
4502 case KWORD:
Bram Moolenaar0270f382018-07-17 05:43:58 +02004503 if (!vim_iswordp_buf(rex.input, rex.reg_buf))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004504 status = RA_NOMATCH;
4505 else
4506 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 break;
4508
4509 case SKWORD:
Bram Moolenaar0270f382018-07-17 05:43:58 +02004510 if (VIM_ISDIGIT(*rex.input)
4511 || !vim_iswordp_buf(rex.input, rex.reg_buf))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004512 status = RA_NOMATCH;
4513 else
4514 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 break;
4516
4517 case FNAME:
4518 if (!vim_isfilec(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004519 status = RA_NOMATCH;
4520 else
4521 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 break;
4523
4524 case SFNAME:
Bram Moolenaar0270f382018-07-17 05:43:58 +02004525 if (VIM_ISDIGIT(*rex.input) || !vim_isfilec(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004526 status = RA_NOMATCH;
4527 else
4528 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 break;
4530
4531 case PRINT:
Bram Moolenaar0270f382018-07-17 05:43:58 +02004532 if (!vim_isprintc(PTR2CHAR(rex.input)))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004533 status = RA_NOMATCH;
4534 else
4535 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 break;
4537
4538 case SPRINT:
Bram Moolenaar0270f382018-07-17 05:43:58 +02004539 if (VIM_ISDIGIT(*rex.input) || !vim_isprintc(PTR2CHAR(rex.input)))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004540 status = RA_NOMATCH;
4541 else
4542 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 break;
4544
4545 case WHITE:
Bram Moolenaar1c465442017-03-12 20:10:05 +01004546 if (!VIM_ISWHITE(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004547 status = RA_NOMATCH;
4548 else
4549 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 break;
4551
4552 case NWHITE:
Bram Moolenaar1c465442017-03-12 20:10:05 +01004553 if (c == NUL || VIM_ISWHITE(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004554 status = RA_NOMATCH;
4555 else
4556 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 break;
4558
4559 case DIGIT:
4560 if (!ri_digit(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004561 status = RA_NOMATCH;
4562 else
4563 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 break;
4565
4566 case NDIGIT:
4567 if (c == NUL || ri_digit(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004568 status = RA_NOMATCH;
4569 else
4570 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 break;
4572
4573 case HEX:
4574 if (!ri_hex(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004575 status = RA_NOMATCH;
4576 else
4577 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 break;
4579
4580 case NHEX:
4581 if (c == NUL || ri_hex(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004582 status = RA_NOMATCH;
4583 else
4584 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 break;
4586
4587 case OCTAL:
4588 if (!ri_octal(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004589 status = RA_NOMATCH;
4590 else
4591 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 break;
4593
4594 case NOCTAL:
4595 if (c == NUL || ri_octal(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004596 status = RA_NOMATCH;
4597 else
4598 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 break;
4600
4601 case WORD:
4602 if (!ri_word(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004603 status = RA_NOMATCH;
4604 else
4605 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 break;
4607
4608 case NWORD:
4609 if (c == NUL || ri_word(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004610 status = RA_NOMATCH;
4611 else
4612 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 break;
4614
4615 case HEAD:
4616 if (!ri_head(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004617 status = RA_NOMATCH;
4618 else
4619 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 break;
4621
4622 case NHEAD:
4623 if (c == NUL || ri_head(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004624 status = RA_NOMATCH;
4625 else
4626 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 break;
4628
4629 case ALPHA:
4630 if (!ri_alpha(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004631 status = RA_NOMATCH;
4632 else
4633 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 break;
4635
4636 case NALPHA:
4637 if (c == NUL || ri_alpha(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004638 status = RA_NOMATCH;
4639 else
4640 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 break;
4642
4643 case LOWER:
4644 if (!ri_lower(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004645 status = RA_NOMATCH;
4646 else
4647 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 break;
4649
4650 case NLOWER:
4651 if (c == NUL || ri_lower(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004652 status = RA_NOMATCH;
4653 else
4654 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 break;
4656
4657 case UPPER:
4658 if (!ri_upper(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004659 status = RA_NOMATCH;
4660 else
4661 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 break;
4663
4664 case NUPPER:
4665 if (c == NUL || ri_upper(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004666 status = RA_NOMATCH;
4667 else
4668 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 break;
4670
4671 case EXACTLY:
4672 {
4673 int len;
4674 char_u *opnd;
4675
4676 opnd = OPERAND(scan);
4677 /* Inline the first byte, for speed. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02004678 if (*opnd != *rex.input
Bram Moolenaar6100d022016-10-02 16:51:57 +02004679 && (!rex.reg_ic || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680#ifdef FEAT_MBYTE
4681 !enc_utf8 &&
4682#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02004683 MB_TOLOWER(*opnd) != MB_TOLOWER(*rex.input))))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004684 status = RA_NOMATCH;
4685 else if (*opnd == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 {
4687 /* match empty string always works; happens when "~" is
4688 * empty. */
4689 }
Bram Moolenaar6082bea2014-05-13 18:04:00 +02004690 else
4691 {
4692 if (opnd[1] == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02004694 && !(enc_utf8 && rex.reg_ic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695#endif
4696 )
Bram Moolenaar6082bea2014-05-13 18:04:00 +02004697 {
4698 len = 1; /* matched a single byte above */
4699 }
4700 else
4701 {
4702 /* Need to match first byte again for multi-byte. */
4703 len = (int)STRLEN(opnd);
Bram Moolenaar0270f382018-07-17 05:43:58 +02004704 if (cstrncmp(opnd, rex.input, &len) != 0)
Bram Moolenaar6082bea2014-05-13 18:04:00 +02004705 status = RA_NOMATCH;
4706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707#ifdef FEAT_MBYTE
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004708 /* Check for following composing character, unless %C
4709 * follows (skips over all composing chars). */
Bram Moolenaar6082bea2014-05-13 18:04:00 +02004710 if (status != RA_NOMATCH
4711 && enc_utf8
Bram Moolenaar0270f382018-07-17 05:43:58 +02004712 && UTF_COMPOSINGLIKE(rex.input, rex.input + len)
Bram Moolenaar6100d022016-10-02 16:51:57 +02004713 && !rex.reg_icombine
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004714 && OP(next) != RE_COMPOSING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 {
4716 /* raaron: This code makes a composing character get
4717 * ignored, which is the correct behavior (sometimes)
4718 * for voweled Hebrew texts. */
Bram Moolenaar6082bea2014-05-13 18:04:00 +02004719 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721#endif
Bram Moolenaar6082bea2014-05-13 18:04:00 +02004722 if (status != RA_NOMATCH)
Bram Moolenaar0270f382018-07-17 05:43:58 +02004723 rex.input += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 }
4725 }
4726 break;
4727
4728 case ANYOF:
4729 case ANYBUT:
4730 if (c == NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004731 status = RA_NOMATCH;
4732 else if ((cstrchr(OPERAND(scan), c) == NULL) == (op == ANYOF))
4733 status = RA_NOMATCH;
4734 else
4735 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 break;
4737
4738#ifdef FEAT_MBYTE
4739 case MULTIBYTECODE:
4740 if (has_mbyte)
4741 {
4742 int i, len;
4743 char_u *opnd;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004744 int opndc = 0, inpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745
4746 opnd = OPERAND(scan);
4747 /* Safety check (just in case 'encoding' was changed since
4748 * compiling the program). */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004749 if ((len = (*mb_ptr2len)(opnd)) < 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004750 {
4751 status = RA_NOMATCH;
4752 break;
4753 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004754 if (enc_utf8)
Bram Moolenaarace95982017-03-29 17:30:27 +02004755 opndc = utf_ptr2char(opnd);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004756 if (enc_utf8 && utf_iscomposing(opndc))
4757 {
4758 /* When only a composing char is given match at any
4759 * position where that composing char appears. */
4760 status = RA_NOMATCH;
Bram Moolenaar0270f382018-07-17 05:43:58 +02004761 for (i = 0; rex.input[i] != NUL;
4762 i += utf_ptr2len(rex.input + i))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004763 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004764 inpc = utf_ptr2char(rex.input + i);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004765 if (!utf_iscomposing(inpc))
4766 {
4767 if (i > 0)
4768 break;
4769 }
4770 else if (opndc == inpc)
4771 {
4772 /* Include all following composing chars. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02004773 len = i + utfc_ptr2len(rex.input + i);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004774 status = RA_MATCH;
4775 break;
4776 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004777 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004778 }
4779 else
4780 for (i = 0; i < len; ++i)
Bram Moolenaar0270f382018-07-17 05:43:58 +02004781 if (opnd[i] != rex.input[i])
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004782 {
4783 status = RA_NOMATCH;
4784 break;
4785 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02004786 rex.input += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 }
4788 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004789 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 break;
4791#endif
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004792 case RE_COMPOSING:
4793#ifdef FEAT_MBYTE
4794 if (enc_utf8)
4795 {
4796 /* Skip composing characters. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02004797 while (utf_iscomposing(utf_ptr2char(rex.input)))
4798 MB_CPTR_ADV(rex.input);
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004799 }
4800#endif
4801 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802
4803 case NOTHING:
4804 break;
4805
4806 case BACK:
Bram Moolenaar582fd852005-03-28 20:58:01 +00004807 {
4808 int i;
4809 backpos_T *bp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810
Bram Moolenaar582fd852005-03-28 20:58:01 +00004811 /*
4812 * When we run into BACK we need to check if we don't keep
4813 * looping without matching any input. The second and later
4814 * times a BACK is encountered it fails if the input is still
4815 * at the same position as the previous time.
4816 * The positions are stored in "backpos" and found by the
4817 * current value of "scan", the position in the RE program.
4818 */
4819 bp = (backpos_T *)backpos.ga_data;
4820 for (i = 0; i < backpos.ga_len; ++i)
4821 if (bp[i].bp_scan == scan)
4822 break;
4823 if (i == backpos.ga_len)
4824 {
4825 /* First time at this BACK, make room to store the pos. */
4826 if (ga_grow(&backpos, 1) == FAIL)
4827 status = RA_FAIL;
4828 else
4829 {
4830 /* get "ga_data" again, it may have changed */
4831 bp = (backpos_T *)backpos.ga_data;
4832 bp[i].bp_scan = scan;
4833 ++backpos.ga_len;
4834 }
4835 }
4836 else if (reg_save_equal(&bp[i].bp_pos))
4837 /* Still at same position as last time, fail. */
4838 status = RA_NOMATCH;
4839
4840 if (status != RA_FAIL && status != RA_NOMATCH)
4841 reg_save(&bp[i].bp_pos, &backpos);
4842 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004843 break;
4844
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 case MOPEN + 0: /* Match start: \zs */
4846 case MOPEN + 1: /* \( */
4847 case MOPEN + 2:
4848 case MOPEN + 3:
4849 case MOPEN + 4:
4850 case MOPEN + 5:
4851 case MOPEN + 6:
4852 case MOPEN + 7:
4853 case MOPEN + 8:
4854 case MOPEN + 9:
4855 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 no = op - MOPEN;
4857 cleanup_subexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004858 rp = regstack_push(RS_MOPEN, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004859 if (rp == NULL)
4860 status = RA_FAIL;
4861 else
4862 {
4863 rp->rs_no = no;
Bram Moolenaar6100d022016-10-02 16:51:57 +02004864 save_se(&rp->rs_un.sesave, &rex.reg_startpos[no],
4865 &rex.reg_startp[no]);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004866 /* We simply continue and handle the result when done. */
4867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004869 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870
4871 case NOPEN: /* \%( */
4872 case NCLOSE: /* \) after \%( */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004873 if (regstack_push(RS_NOPEN, scan) == NULL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004874 status = RA_FAIL;
4875 /* We simply continue and handle the result when done. */
4876 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877
4878#ifdef FEAT_SYN_HL
4879 case ZOPEN + 1:
4880 case ZOPEN + 2:
4881 case ZOPEN + 3:
4882 case ZOPEN + 4:
4883 case ZOPEN + 5:
4884 case ZOPEN + 6:
4885 case ZOPEN + 7:
4886 case ZOPEN + 8:
4887 case ZOPEN + 9:
4888 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 no = op - ZOPEN;
4890 cleanup_zsubexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004891 rp = regstack_push(RS_ZOPEN, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004892 if (rp == NULL)
4893 status = RA_FAIL;
4894 else
4895 {
4896 rp->rs_no = no;
4897 save_se(&rp->rs_un.sesave, &reg_startzpos[no],
4898 &reg_startzp[no]);
4899 /* We simply continue and handle the result when done. */
4900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004902 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903#endif
4904
4905 case MCLOSE + 0: /* Match end: \ze */
4906 case MCLOSE + 1: /* \) */
4907 case MCLOSE + 2:
4908 case MCLOSE + 3:
4909 case MCLOSE + 4:
4910 case MCLOSE + 5:
4911 case MCLOSE + 6:
4912 case MCLOSE + 7:
4913 case MCLOSE + 8:
4914 case MCLOSE + 9:
4915 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 no = op - MCLOSE;
4917 cleanup_subexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004918 rp = regstack_push(RS_MCLOSE, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004919 if (rp == NULL)
4920 status = RA_FAIL;
4921 else
4922 {
4923 rp->rs_no = no;
Bram Moolenaar6100d022016-10-02 16:51:57 +02004924 save_se(&rp->rs_un.sesave, &rex.reg_endpos[no],
4925 &rex.reg_endp[no]);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004926 /* We simply continue and handle the result when done. */
4927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004929 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930
4931#ifdef FEAT_SYN_HL
4932 case ZCLOSE + 1: /* \) after \z( */
4933 case ZCLOSE + 2:
4934 case ZCLOSE + 3:
4935 case ZCLOSE + 4:
4936 case ZCLOSE + 5:
4937 case ZCLOSE + 6:
4938 case ZCLOSE + 7:
4939 case ZCLOSE + 8:
4940 case ZCLOSE + 9:
4941 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 no = op - ZCLOSE;
4943 cleanup_zsubexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004944 rp = regstack_push(RS_ZCLOSE, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004945 if (rp == NULL)
4946 status = RA_FAIL;
4947 else
4948 {
4949 rp->rs_no = no;
4950 save_se(&rp->rs_un.sesave, &reg_endzpos[no],
4951 &reg_endzp[no]);
4952 /* We simply continue and handle the result when done. */
4953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004955 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956#endif
4957
4958 case BACKREF + 1:
4959 case BACKREF + 2:
4960 case BACKREF + 3:
4961 case BACKREF + 4:
4962 case BACKREF + 5:
4963 case BACKREF + 6:
4964 case BACKREF + 7:
4965 case BACKREF + 8:
4966 case BACKREF + 9:
4967 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969
4970 no = op - BACKREF;
4971 cleanup_subexpr();
4972 if (!REG_MULTI) /* Single-line regexp */
4973 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02004974 if (rex.reg_startp[no] == NULL || rex.reg_endp[no] == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 {
4976 /* Backref was not set: Match an empty string. */
4977 len = 0;
4978 }
4979 else
4980 {
4981 /* Compare current input with back-ref in the same
4982 * line. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02004983 len = (int)(rex.reg_endp[no] - rex.reg_startp[no]);
Bram Moolenaar0270f382018-07-17 05:43:58 +02004984 if (cstrncmp(rex.reg_startp[no], rex.input, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004985 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 }
4987 }
4988 else /* Multi-line regexp */
4989 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02004990 if (rex.reg_startpos[no].lnum < 0
4991 || rex.reg_endpos[no].lnum < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 {
4993 /* Backref was not set: Match an empty string. */
4994 len = 0;
4995 }
4996 else
4997 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004998 if (rex.reg_startpos[no].lnum == rex.lnum
4999 && rex.reg_endpos[no].lnum == rex.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 {
5001 /* Compare back-ref within the current line. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02005002 len = rex.reg_endpos[no].col
5003 - rex.reg_startpos[no].col;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005004 if (cstrncmp(rex.line + rex.reg_startpos[no].col,
5005 rex.input, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005006 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 }
5008 else
5009 {
5010 /* Messy situation: Need to compare between two
5011 * lines. */
Bram Moolenaar141f6bb2013-06-15 15:09:50 +02005012 int r = match_with_backref(
Bram Moolenaar6100d022016-10-02 16:51:57 +02005013 rex.reg_startpos[no].lnum,
5014 rex.reg_startpos[no].col,
5015 rex.reg_endpos[no].lnum,
5016 rex.reg_endpos[no].col,
Bram Moolenaar4cff8fa2013-06-14 22:48:54 +02005017 &len);
Bram Moolenaar141f6bb2013-06-15 15:09:50 +02005018
5019 if (r != RA_MATCH)
5020 status = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 }
5022 }
5023 }
5024
5025 /* Matched the backref, skip over it. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005026 rex.input += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 }
5028 break;
5029
5030#ifdef FEAT_SYN_HL
5031 case ZREF + 1:
5032 case ZREF + 2:
5033 case ZREF + 3:
5034 case ZREF + 4:
5035 case ZREF + 5:
5036 case ZREF + 6:
5037 case ZREF + 7:
5038 case ZREF + 8:
5039 case ZREF + 9:
5040 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 int len;
5042
5043 cleanup_zsubexpr();
5044 no = op - ZREF;
5045 if (re_extmatch_in != NULL
5046 && re_extmatch_in->matches[no] != NULL)
5047 {
5048 len = (int)STRLEN(re_extmatch_in->matches[no]);
5049 if (cstrncmp(re_extmatch_in->matches[no],
Bram Moolenaar0270f382018-07-17 05:43:58 +02005050 rex.input, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005051 status = RA_NOMATCH;
5052 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005053 rex.input += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 }
5055 else
5056 {
5057 /* Backref was not set: Match an empty string. */
5058 }
5059 }
5060 break;
5061#endif
5062
5063 case BRANCH:
5064 {
5065 if (OP(next) != BRANCH) /* No choice. */
5066 next = OPERAND(scan); /* Avoid recursion. */
5067 else
5068 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005069 rp = regstack_push(RS_BRANCH, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005070 if (rp == NULL)
5071 status = RA_FAIL;
5072 else
5073 status = RA_BREAK; /* rest is below */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 }
5075 }
5076 break;
5077
5078 case BRACE_LIMITS:
5079 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 if (OP(next) == BRACE_SIMPLE)
5081 {
5082 bl_minval = OPERAND_MIN(scan);
5083 bl_maxval = OPERAND_MAX(scan);
5084 }
5085 else if (OP(next) >= BRACE_COMPLEX
5086 && OP(next) < BRACE_COMPLEX + 10)
5087 {
5088 no = OP(next) - BRACE_COMPLEX;
5089 brace_min[no] = OPERAND_MIN(scan);
5090 brace_max[no] = OPERAND_MAX(scan);
5091 brace_count[no] = 0;
5092 }
5093 else
5094 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01005095 internal_error("BRACE_LIMITS");
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005096 status = RA_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 }
5098 }
5099 break;
5100
5101 case BRACE_COMPLEX + 0:
5102 case BRACE_COMPLEX + 1:
5103 case BRACE_COMPLEX + 2:
5104 case BRACE_COMPLEX + 3:
5105 case BRACE_COMPLEX + 4:
5106 case BRACE_COMPLEX + 5:
5107 case BRACE_COMPLEX + 6:
5108 case BRACE_COMPLEX + 7:
5109 case BRACE_COMPLEX + 8:
5110 case BRACE_COMPLEX + 9:
5111 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 no = op - BRACE_COMPLEX;
5113 ++brace_count[no];
5114
5115 /* If not matched enough times yet, try one more */
5116 if (brace_count[no] <= (brace_min[no] <= brace_max[no]
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005117 ? brace_min[no] : brace_max[no]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005119 rp = regstack_push(RS_BRCPLX_MORE, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005120 if (rp == NULL)
5121 status = RA_FAIL;
5122 else
5123 {
5124 rp->rs_no = no;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005125 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005126 next = OPERAND(scan);
5127 /* We continue and handle the result when done. */
5128 }
5129 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 }
5131
5132 /* If matched enough times, may try matching some more */
5133 if (brace_min[no] <= brace_max[no])
5134 {
5135 /* Range is the normal way around, use longest match */
5136 if (brace_count[no] <= brace_max[no])
5137 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005138 rp = regstack_push(RS_BRCPLX_LONG, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005139 if (rp == NULL)
5140 status = RA_FAIL;
5141 else
5142 {
5143 rp->rs_no = no;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005144 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005145 next = OPERAND(scan);
5146 /* We continue and handle the result when done. */
5147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 }
5149 }
5150 else
5151 {
5152 /* Range is backwards, use shortest match first */
5153 if (brace_count[no] <= brace_min[no])
5154 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005155 rp = regstack_push(RS_BRCPLX_SHORT, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005156 if (rp == NULL)
5157 status = RA_FAIL;
5158 else
5159 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005160 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005161 /* We continue and handle the result when done. */
5162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 }
5164 }
5165 }
5166 break;
5167
5168 case BRACE_SIMPLE:
5169 case STAR:
5170 case PLUS:
5171 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005172 regstar_T rst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173
5174 /*
5175 * Lookahead to avoid useless match attempts when we know
5176 * what character comes next.
5177 */
5178 if (OP(next) == EXACTLY)
5179 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005180 rst.nextb = *OPERAND(next);
Bram Moolenaar6100d022016-10-02 16:51:57 +02005181 if (rex.reg_ic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 {
Bram Moolenaara245a5b2007-08-11 11:58:23 +00005183 if (MB_ISUPPER(rst.nextb))
5184 rst.nextb_ic = MB_TOLOWER(rst.nextb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 else
Bram Moolenaara245a5b2007-08-11 11:58:23 +00005186 rst.nextb_ic = MB_TOUPPER(rst.nextb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 }
5188 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005189 rst.nextb_ic = rst.nextb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 }
5191 else
5192 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005193 rst.nextb = NUL;
5194 rst.nextb_ic = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 }
5196 if (op != BRACE_SIMPLE)
5197 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005198 rst.minval = (op == STAR) ? 0 : 1;
5199 rst.maxval = MAX_LIMIT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 }
5201 else
5202 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005203 rst.minval = bl_minval;
5204 rst.maxval = bl_maxval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 }
5206
5207 /*
5208 * When maxval > minval, try matching as much as possible, up
5209 * to maxval. When maxval < minval, try matching at least the
5210 * minimal number (since the range is backwards, that's also
5211 * maxval!).
5212 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005213 rst.count = regrepeat(OPERAND(scan), rst.maxval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005216 status = RA_FAIL;
5217 break;
5218 }
5219 if (rst.minval <= rst.maxval
5220 ? rst.count >= rst.minval : rst.count >= rst.maxval)
5221 {
5222 /* It could match. Prepare for trying to match what
5223 * follows. The code is below. Parameters are stored in
5224 * a regstar_T on the regstack. */
Bram Moolenaar916b7af2005-03-16 09:52:38 +00005225 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005226 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005227 emsg(_(e_maxmempat));
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005228 status = RA_FAIL;
5229 }
5230 else if (ga_grow(&regstack, sizeof(regstar_T)) == FAIL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005231 status = RA_FAIL;
5232 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005234 regstack.ga_len += sizeof(regstar_T);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005235 rp = regstack_push(rst.minval <= rst.maxval
Bram Moolenaar582fd852005-03-28 20:58:01 +00005236 ? RS_STAR_LONG : RS_STAR_SHORT, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005237 if (rp == NULL)
5238 status = RA_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005240 {
5241 *(((regstar_T *)rp) - 1) = rst;
5242 status = RA_BREAK; /* skip the restore bits */
5243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 }
5245 }
5246 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005247 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 }
5250 break;
5251
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005252 case NOMATCH:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 case MATCH:
5254 case SUBPAT:
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005255 rp = regstack_push(RS_NOMATCH, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005256 if (rp == NULL)
5257 status = RA_FAIL;
5258 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005260 rp->rs_no = op;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005261 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005262 next = OPERAND(scan);
5263 /* We continue and handle the result when done. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 }
5265 break;
5266
5267 case BEHIND:
5268 case NOBEHIND:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005269 /* Need a bit of room to store extra positions. */
Bram Moolenaar916b7af2005-03-16 09:52:38 +00005270 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005271 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005272 emsg(_(e_maxmempat));
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005273 status = RA_FAIL;
5274 }
5275 else if (ga_grow(&regstack, sizeof(regbehind_T)) == FAIL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005276 status = RA_FAIL;
5277 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005279 regstack.ga_len += sizeof(regbehind_T);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005280 rp = regstack_push(RS_BEHIND1, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005281 if (rp == NULL)
5282 status = RA_FAIL;
5283 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 {
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005285 /* Need to save the subexpr to be able to restore them
5286 * when there is a match but we don't use it. */
5287 save_subexpr(((regbehind_T *)rp) - 1);
5288
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005289 rp->rs_no = op;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005290 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005291 /* First try if what follows matches. If it does then we
5292 * check the behind match by looping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005295 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296
5297 case BHPOS:
5298 if (REG_MULTI)
5299 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005300 if (behind_pos.rs_u.pos.col != (colnr_T)(rex.input - rex.line)
5301 || behind_pos.rs_u.pos.lnum != rex.lnum)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005302 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02005304 else if (behind_pos.rs_u.ptr != rex.input)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005305 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 break;
5307
5308 case NEWL:
Bram Moolenaar0270f382018-07-17 05:43:58 +02005309 if ((c != NUL || !REG_MULTI || rex.lnum > rex.reg_maxline
Bram Moolenaar6100d022016-10-02 16:51:57 +02005310 || rex.reg_line_lbr)
5311 && (c != '\n' || !rex.reg_line_lbr))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005312 status = RA_NOMATCH;
Bram Moolenaar6100d022016-10-02 16:51:57 +02005313 else if (rex.reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 ADVANCE_REGINPUT();
5315 else
5316 reg_nextline();
5317 break;
5318
5319 case END:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005320 status = RA_MATCH; /* Success! */
5321 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322
5323 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005324 emsg(_(e_re_corr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325#ifdef DEBUG
5326 printf("Illegal op code %d\n", op);
5327#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005328 status = RA_FAIL;
5329 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 }
5331 }
5332
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005333 /* If we can't continue sequentially, break the inner loop. */
5334 if (status != RA_CONT)
5335 break;
5336
5337 /* Continue in inner loop, advance to next item. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 scan = next;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005339
5340 } /* end of inner loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341
5342 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005343 * If there is something on the regstack execute the code for the state.
Bram Moolenaar582fd852005-03-28 20:58:01 +00005344 * If the state is popped then loop and use the older state.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005346 while (regstack.ga_len > 0 && status != RA_FAIL)
5347 {
5348 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len) - 1;
5349 switch (rp->rs_state)
5350 {
5351 case RS_NOPEN:
5352 /* Result is passed on as-is, simply pop the state. */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005353 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005354 break;
5355
5356 case RS_MOPEN:
5357 /* Pop the state. Restore pointers when there is no match. */
5358 if (status == RA_NOMATCH)
Bram Moolenaar6100d022016-10-02 16:51:57 +02005359 restore_se(&rp->rs_un.sesave, &rex.reg_startpos[rp->rs_no],
5360 &rex.reg_startp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005361 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005362 break;
5363
5364#ifdef FEAT_SYN_HL
5365 case RS_ZOPEN:
5366 /* Pop the state. Restore pointers when there is no match. */
5367 if (status == RA_NOMATCH)
5368 restore_se(&rp->rs_un.sesave, &reg_startzpos[rp->rs_no],
5369 &reg_startzp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005370 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005371 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005373
5374 case RS_MCLOSE:
5375 /* Pop the state. Restore pointers when there is no match. */
5376 if (status == RA_NOMATCH)
Bram Moolenaar6100d022016-10-02 16:51:57 +02005377 restore_se(&rp->rs_un.sesave, &rex.reg_endpos[rp->rs_no],
5378 &rex.reg_endp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005379 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005380 break;
5381
5382#ifdef FEAT_SYN_HL
5383 case RS_ZCLOSE:
5384 /* Pop the state. Restore pointers when there is no match. */
5385 if (status == RA_NOMATCH)
5386 restore_se(&rp->rs_un.sesave, &reg_endzpos[rp->rs_no],
5387 &reg_endzp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005388 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005389 break;
5390#endif
5391
5392 case RS_BRANCH:
5393 if (status == RA_MATCH)
5394 /* this branch matched, use it */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005395 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005396 else
5397 {
5398 if (status != RA_BREAK)
5399 {
5400 /* After a non-matching branch: try next one. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00005401 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005402 scan = rp->rs_scan;
5403 }
5404 if (scan == NULL || OP(scan) != BRANCH)
5405 {
5406 /* no more branches, didn't find a match */
5407 status = RA_NOMATCH;
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005408 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005409 }
5410 else
5411 {
5412 /* Prepare to try a branch. */
5413 rp->rs_scan = regnext(scan);
Bram Moolenaar582fd852005-03-28 20:58:01 +00005414 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005415 scan = OPERAND(scan);
5416 }
5417 }
5418 break;
5419
5420 case RS_BRCPLX_MORE:
5421 /* Pop the state. Restore pointers when there is no match. */
5422 if (status == RA_NOMATCH)
5423 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005424 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005425 --brace_count[rp->rs_no]; /* decrement match count */
5426 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005427 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005428 break;
5429
5430 case RS_BRCPLX_LONG:
5431 /* Pop the state. Restore pointers when there is no match. */
5432 if (status == RA_NOMATCH)
5433 {
5434 /* There was no match, but we did find enough matches. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00005435 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005436 --brace_count[rp->rs_no];
5437 /* continue with the items after "\{}" */
5438 status = RA_CONT;
5439 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005440 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005441 if (status == RA_CONT)
5442 scan = regnext(scan);
5443 break;
5444
5445 case RS_BRCPLX_SHORT:
5446 /* Pop the state. Restore pointers when there is no match. */
5447 if (status == RA_NOMATCH)
5448 /* There was no match, try to match one more item. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00005449 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005450 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005451 if (status == RA_NOMATCH)
5452 {
5453 scan = OPERAND(scan);
5454 status = RA_CONT;
5455 }
5456 break;
5457
5458 case RS_NOMATCH:
5459 /* Pop the state. If the operand matches for NOMATCH or
5460 * doesn't match for MATCH/SUBPAT, we fail. Otherwise backup,
5461 * except for SUBPAT, and continue with the next item. */
5462 if (status == (rp->rs_no == NOMATCH ? RA_MATCH : RA_NOMATCH))
5463 status = RA_NOMATCH;
5464 else
5465 {
5466 status = RA_CONT;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005467 if (rp->rs_no != SUBPAT) /* zero-width */
5468 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005469 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005470 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005471 if (status == RA_CONT)
5472 scan = regnext(scan);
5473 break;
5474
5475 case RS_BEHIND1:
5476 if (status == RA_NOMATCH)
5477 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005478 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005479 regstack.ga_len -= sizeof(regbehind_T);
5480 }
5481 else
5482 {
5483 /* The stuff after BEHIND/NOBEHIND matches. Now try if
5484 * the behind part does (not) match before the current
5485 * position in the input. This must be done at every
5486 * position in the input and checking if the match ends at
5487 * the current position. */
5488
5489 /* save the position after the found match for next */
Bram Moolenaar582fd852005-03-28 20:58:01 +00005490 reg_save(&(((regbehind_T *)rp) - 1)->save_after, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005491
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005492 /* Start looking for a match with operand at the current
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00005493 * position. Go back one character until we find the
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005494 * result, hitting the start of the line or the previous
5495 * line (for multi-line matching).
5496 * Set behind_pos to where the match should end, BHPOS
5497 * will match it. Save the current value. */
5498 (((regbehind_T *)rp) - 1)->save_behind = behind_pos;
5499 behind_pos = rp->rs_un.regsave;
5500
5501 rp->rs_state = RS_BEHIND2;
5502
Bram Moolenaar582fd852005-03-28 20:58:01 +00005503 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005504 scan = OPERAND(rp->rs_scan) + 4;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005505 }
5506 break;
5507
5508 case RS_BEHIND2:
5509 /*
5510 * Looping for BEHIND / NOBEHIND match.
5511 */
5512 if (status == RA_MATCH && reg_save_equal(&behind_pos))
5513 {
5514 /* found a match that ends where "next" started */
5515 behind_pos = (((regbehind_T *)rp) - 1)->save_behind;
5516 if (rp->rs_no == BEHIND)
Bram Moolenaar582fd852005-03-28 20:58:01 +00005517 reg_restore(&(((regbehind_T *)rp) - 1)->save_after,
5518 &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005519 else
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005520 {
5521 /* But we didn't want a match. Need to restore the
5522 * subexpr, because what follows matched, so they have
5523 * been set. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005524 status = RA_NOMATCH;
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005525 restore_subexpr(((regbehind_T *)rp) - 1);
5526 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005527 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005528 regstack.ga_len -= sizeof(regbehind_T);
5529 }
5530 else
5531 {
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005532 long limit;
5533
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005534 /* No match or a match that doesn't end where we want it: Go
5535 * back one character. May go to previous line once. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005536 no = OK;
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005537 limit = OPERAND_MIN(rp->rs_scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005538 if (REG_MULTI)
5539 {
Bram Moolenaar61602c52013-06-01 19:54:43 +02005540 if (limit > 0
5541 && ((rp->rs_un.regsave.rs_u.pos.lnum
5542 < behind_pos.rs_u.pos.lnum
Bram Moolenaar0270f382018-07-17 05:43:58 +02005543 ? (colnr_T)STRLEN(rex.line)
Bram Moolenaar61602c52013-06-01 19:54:43 +02005544 : behind_pos.rs_u.pos.col)
5545 - rp->rs_un.regsave.rs_u.pos.col >= limit))
5546 no = FAIL;
5547 else if (rp->rs_un.regsave.rs_u.pos.col == 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005548 {
5549 if (rp->rs_un.regsave.rs_u.pos.lnum
5550 < behind_pos.rs_u.pos.lnum
5551 || reg_getline(
5552 --rp->rs_un.regsave.rs_u.pos.lnum)
5553 == NULL)
5554 no = FAIL;
5555 else
5556 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005557 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005558 rp->rs_un.regsave.rs_u.pos.col =
Bram Moolenaar0270f382018-07-17 05:43:58 +02005559 (colnr_T)STRLEN(rex.line);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005560 }
5561 }
5562 else
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005563 {
Bram Moolenaarf5e44a72013-02-26 18:46:01 +01005564#ifdef FEAT_MBYTE
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005565 if (has_mbyte)
Bram Moolenaarbc197192018-02-13 16:35:06 +01005566 {
5567 char_u *line =
Bram Moolenaar866f3552019-01-01 22:19:08 +01005568 reg_getline(rp->rs_un.regsave.rs_u.pos.lnum);
Bram Moolenaarbc197192018-02-13 16:35:06 +01005569
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005570 rp->rs_un.regsave.rs_u.pos.col -=
Bram Moolenaarbc197192018-02-13 16:35:06 +01005571 (*mb_head_off)(line, line
Bram Moolenaarf5e44a72013-02-26 18:46:01 +01005572 + rp->rs_un.regsave.rs_u.pos.col - 1) + 1;
Bram Moolenaarbc197192018-02-13 16:35:06 +01005573 }
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005574 else
Bram Moolenaarf5e44a72013-02-26 18:46:01 +01005575#endif
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005576 --rp->rs_un.regsave.rs_u.pos.col;
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005577 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005578 }
5579 else
5580 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005581 if (rp->rs_un.regsave.rs_u.ptr == rex.line)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005582 no = FAIL;
5583 else
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005584 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005585 MB_PTR_BACK(rex.line, rp->rs_un.regsave.rs_u.ptr);
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005586 if (limit > 0 && (long)(behind_pos.rs_u.ptr
5587 - rp->rs_un.regsave.rs_u.ptr) > limit)
5588 no = FAIL;
5589 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005590 }
5591 if (no == OK)
5592 {
5593 /* Advanced, prepare for finding match again. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00005594 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005595 scan = OPERAND(rp->rs_scan) + 4;
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005596 if (status == RA_MATCH)
5597 {
5598 /* We did match, so subexpr may have been changed,
5599 * need to restore them for the next try. */
5600 status = RA_NOMATCH;
5601 restore_subexpr(((regbehind_T *)rp) - 1);
5602 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005603 }
5604 else
5605 {
5606 /* Can't advance. For NOBEHIND that's a match. */
5607 behind_pos = (((regbehind_T *)rp) - 1)->save_behind;
5608 if (rp->rs_no == NOBEHIND)
5609 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005610 reg_restore(&(((regbehind_T *)rp) - 1)->save_after,
5611 &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005612 status = RA_MATCH;
5613 }
5614 else
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005615 {
5616 /* We do want a proper match. Need to restore the
5617 * subexpr if we had a match, because they may have
5618 * been set. */
5619 if (status == RA_MATCH)
5620 {
5621 status = RA_NOMATCH;
5622 restore_subexpr(((regbehind_T *)rp) - 1);
5623 }
5624 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005625 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005626 regstack.ga_len -= sizeof(regbehind_T);
5627 }
5628 }
5629 break;
5630
5631 case RS_STAR_LONG:
5632 case RS_STAR_SHORT:
5633 {
5634 regstar_T *rst = ((regstar_T *)rp) - 1;
5635
5636 if (status == RA_MATCH)
5637 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005638 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005639 regstack.ga_len -= sizeof(regstar_T);
5640 break;
5641 }
5642
5643 /* Tried once already, restore input pointers. */
5644 if (status != RA_BREAK)
Bram Moolenaar582fd852005-03-28 20:58:01 +00005645 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005646
5647 /* Repeat until we found a position where it could match. */
5648 for (;;)
5649 {
5650 if (status != RA_BREAK)
5651 {
5652 /* Tried first position already, advance. */
5653 if (rp->rs_state == RS_STAR_LONG)
5654 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005655 /* Trying for longest match, but couldn't or
5656 * didn't match -- back up one char. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005657 if (--rst->count < rst->minval)
5658 break;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005659 if (rex.input == rex.line)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005660 {
5661 /* backup to last char of previous line */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005662 --rex.lnum;
5663 rex.line = reg_getline(rex.lnum);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005664 /* Just in case regrepeat() didn't count
5665 * right. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005666 if (rex.line == NULL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005667 break;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005668 rex.input = rex.line + STRLEN(rex.line);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005669 fast_breakcheck();
5670 }
5671 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005672 MB_PTR_BACK(rex.line, rex.input);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005673 }
5674 else
5675 {
5676 /* Range is backwards, use shortest match first.
5677 * Careful: maxval and minval are exchanged!
5678 * Couldn't or didn't match: try advancing one
5679 * char. */
5680 if (rst->count == rst->minval
5681 || regrepeat(OPERAND(rp->rs_scan), 1L) == 0)
5682 break;
5683 ++rst->count;
5684 }
5685 if (got_int)
5686 break;
5687 }
5688 else
5689 status = RA_NOMATCH;
5690
5691 /* If it could match, try it. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005692 if (rst->nextb == NUL || *rex.input == rst->nextb
5693 || *rex.input == rst->nextb_ic)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005694 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005695 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005696 scan = regnext(rp->rs_scan);
5697 status = RA_CONT;
5698 break;
5699 }
5700 }
5701 if (status != RA_CONT)
5702 {
5703 /* Failed. */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005704 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005705 regstack.ga_len -= sizeof(regstar_T);
5706 status = RA_NOMATCH;
5707 }
5708 }
5709 break;
5710 }
5711
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005712 /* If we want to continue the inner loop or didn't pop a state
5713 * continue matching loop */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005714 if (status == RA_CONT || rp == (regitem_T *)
5715 ((char *)regstack.ga_data + regstack.ga_len) - 1)
5716 break;
5717 }
5718
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005719 /* May need to continue with the inner loop, starting at "scan". */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005720 if (status == RA_CONT)
5721 continue;
5722
5723 /*
5724 * If the regstack is empty or something failed we are done.
5725 */
5726 if (regstack.ga_len == 0 || status == RA_FAIL)
5727 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005728 if (scan == NULL)
5729 {
5730 /*
5731 * We get here only if there's trouble -- normally "case END" is
5732 * the terminating point.
5733 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005734 emsg(_(e_re_corr));
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005735#ifdef DEBUG
5736 printf("Premature EOL\n");
5737#endif
5738 }
5739 return (status == RA_MATCH);
5740 }
5741
5742 } /* End of loop until the regstack is empty. */
5743
5744 /* NOTREACHED */
5745}
5746
5747/*
5748 * Push an item onto the regstack.
5749 * Returns pointer to new item. Returns NULL when out of memory.
5750 */
5751 static regitem_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01005752regstack_push(regstate_T state, char_u *scan)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005753{
5754 regitem_T *rp;
5755
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005756 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005757 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005758 emsg(_(e_maxmempat));
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005759 return NULL;
5760 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005761 if (ga_grow(&regstack, sizeof(regitem_T)) == FAIL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005762 return NULL;
5763
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005764 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005765 rp->rs_state = state;
5766 rp->rs_scan = scan;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005767
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005768 regstack.ga_len += sizeof(regitem_T);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005769 return rp;
5770}
5771
5772/*
5773 * Pop an item from the regstack.
5774 */
5775 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005776regstack_pop(char_u **scan)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005777{
5778 regitem_T *rp;
5779
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005780 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len) - 1;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005781 *scan = rp->rs_scan;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005782
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005783 regstack.ga_len -= sizeof(regitem_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784}
5785
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786/*
5787 * regrepeat - repeatedly match something simple, return how many.
Bram Moolenaar0270f382018-07-17 05:43:58 +02005788 * Advances rex.input (and rex.lnum) to just after the matched chars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 */
5790 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005791regrepeat(
5792 char_u *p,
5793 long maxcount) /* maximum number of matches allowed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794{
5795 long count = 0;
5796 char_u *scan;
5797 char_u *opnd;
5798 int mask;
5799 int testval = 0;
5800
Bram Moolenaar0270f382018-07-17 05:43:58 +02005801 scan = rex.input; /* Make local copy of rex.input for speed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 opnd = OPERAND(p);
5803 switch (OP(p))
5804 {
5805 case ANY:
5806 case ANY + ADD_NL:
5807 while (count < maxcount)
5808 {
5809 /* Matching anything means we continue until end-of-line (or
5810 * end-of-file for ANY + ADD_NL), only limited by maxcount. */
5811 while (*scan != NUL && count < maxcount)
5812 {
5813 ++count;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005814 MB_PTR_ADV(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02005816 if (!REG_MULTI || !WITH_NL(OP(p)) || rex.lnum > rex.reg_maxline
Bram Moolenaar6100d022016-10-02 16:51:57 +02005817 || rex.reg_line_lbr || count == maxcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 break;
5819 ++count; /* count the line-break */
5820 reg_nextline();
Bram Moolenaar0270f382018-07-17 05:43:58 +02005821 scan = rex.input;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 if (got_int)
5823 break;
5824 }
5825 break;
5826
5827 case IDENT:
5828 case IDENT + ADD_NL:
5829 testval = TRUE;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005830 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 case SIDENT:
5832 case SIDENT + ADD_NL:
5833 while (count < maxcount)
5834 {
Bram Moolenaar09ea9fc2013-05-21 00:03:02 +02005835 if (vim_isIDc(PTR2CHAR(scan)) && (testval || !VIM_ISDIGIT(*scan)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005837 MB_PTR_ADV(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 }
5839 else if (*scan == NUL)
5840 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005841 if (!REG_MULTI || !WITH_NL(OP(p)) || rex.lnum > rex.reg_maxline
Bram Moolenaar6100d022016-10-02 16:51:57 +02005842 || rex.reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 break;
5844 reg_nextline();
Bram Moolenaar0270f382018-07-17 05:43:58 +02005845 scan = rex.input;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 if (got_int)
5847 break;
5848 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02005849 else if (rex.reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 ++scan;
5851 else
5852 break;
5853 ++count;
5854 }
5855 break;
5856
5857 case KWORD:
5858 case KWORD + ADD_NL:
5859 testval = TRUE;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005860 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 case SKWORD:
5862 case SKWORD + ADD_NL:
5863 while (count < maxcount)
5864 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02005865 if (vim_iswordp_buf(scan, rex.reg_buf)
Bram Moolenaarf813a182013-01-30 13:59:37 +01005866 && (testval || !VIM_ISDIGIT(*scan)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005868 MB_PTR_ADV(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 }
5870 else if (*scan == NUL)
5871 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005872 if (!REG_MULTI || !WITH_NL(OP(p)) || rex.lnum > rex.reg_maxline
Bram Moolenaar6100d022016-10-02 16:51:57 +02005873 || rex.reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 break;
5875 reg_nextline();
Bram Moolenaar0270f382018-07-17 05:43:58 +02005876 scan = rex.input;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 if (got_int)
5878 break;
5879 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02005880 else if (rex.reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 ++scan;
5882 else
5883 break;
5884 ++count;
5885 }
5886 break;
5887
5888 case FNAME:
5889 case FNAME + ADD_NL:
5890 testval = TRUE;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005891 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 case SFNAME:
5893 case SFNAME + ADD_NL:
5894 while (count < maxcount)
5895 {
Bram Moolenaar09ea9fc2013-05-21 00:03:02 +02005896 if (vim_isfilec(PTR2CHAR(scan)) && (testval || !VIM_ISDIGIT(*scan)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005898 MB_PTR_ADV(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 }
5900 else if (*scan == NUL)
5901 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005902 if (!REG_MULTI || !WITH_NL(OP(p)) || rex.lnum > rex.reg_maxline
Bram Moolenaar6100d022016-10-02 16:51:57 +02005903 || rex.reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 break;
5905 reg_nextline();
Bram Moolenaar0270f382018-07-17 05:43:58 +02005906 scan = rex.input;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 if (got_int)
5908 break;
5909 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02005910 else if (rex.reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 ++scan;
5912 else
5913 break;
5914 ++count;
5915 }
5916 break;
5917
5918 case PRINT:
5919 case PRINT + ADD_NL:
5920 testval = TRUE;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005921 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 case SPRINT:
5923 case SPRINT + ADD_NL:
5924 while (count < maxcount)
5925 {
5926 if (*scan == NUL)
5927 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005928 if (!REG_MULTI || !WITH_NL(OP(p)) || rex.lnum > rex.reg_maxline
Bram Moolenaar6100d022016-10-02 16:51:57 +02005929 || rex.reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 break;
5931 reg_nextline();
Bram Moolenaar0270f382018-07-17 05:43:58 +02005932 scan = rex.input;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 if (got_int)
5934 break;
5935 }
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02005936 else if (vim_isprintc(PTR2CHAR(scan)) == 1
5937 && (testval || !VIM_ISDIGIT(*scan)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005939 MB_PTR_ADV(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02005941 else if (rex.reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 ++scan;
5943 else
5944 break;
5945 ++count;
5946 }
5947 break;
5948
5949 case WHITE:
5950 case WHITE + ADD_NL:
5951 testval = mask = RI_WHITE;
5952do_class:
5953 while (count < maxcount)
5954 {
5955#ifdef FEAT_MBYTE
5956 int l;
5957#endif
5958 if (*scan == NUL)
5959 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005960 if (!REG_MULTI || !WITH_NL(OP(p)) || rex.lnum > rex.reg_maxline
Bram Moolenaar6100d022016-10-02 16:51:57 +02005961 || rex.reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 break;
5963 reg_nextline();
Bram Moolenaar0270f382018-07-17 05:43:58 +02005964 scan = rex.input;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 if (got_int)
5966 break;
5967 }
5968#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005969 else if (has_mbyte && (l = (*mb_ptr2len)(scan)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 {
5971 if (testval != 0)
5972 break;
5973 scan += l;
5974 }
5975#endif
5976 else if ((class_tab[*scan] & mask) == testval)
5977 ++scan;
Bram Moolenaar6100d022016-10-02 16:51:57 +02005978 else if (rex.reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 ++scan;
5980 else
5981 break;
5982 ++count;
5983 }
5984 break;
5985
5986 case NWHITE:
5987 case NWHITE + ADD_NL:
5988 mask = RI_WHITE;
5989 goto do_class;
5990 case DIGIT:
5991 case DIGIT + ADD_NL:
5992 testval = mask = RI_DIGIT;
5993 goto do_class;
5994 case NDIGIT:
5995 case NDIGIT + ADD_NL:
5996 mask = RI_DIGIT;
5997 goto do_class;
5998 case HEX:
5999 case HEX + ADD_NL:
6000 testval = mask = RI_HEX;
6001 goto do_class;
6002 case NHEX:
6003 case NHEX + ADD_NL:
6004 mask = RI_HEX;
6005 goto do_class;
6006 case OCTAL:
6007 case OCTAL + ADD_NL:
6008 testval = mask = RI_OCTAL;
6009 goto do_class;
6010 case NOCTAL:
6011 case NOCTAL + ADD_NL:
6012 mask = RI_OCTAL;
6013 goto do_class;
6014 case WORD:
6015 case WORD + ADD_NL:
6016 testval = mask = RI_WORD;
6017 goto do_class;
6018 case NWORD:
6019 case NWORD + ADD_NL:
6020 mask = RI_WORD;
6021 goto do_class;
6022 case HEAD:
6023 case HEAD + ADD_NL:
6024 testval = mask = RI_HEAD;
6025 goto do_class;
6026 case NHEAD:
6027 case NHEAD + ADD_NL:
6028 mask = RI_HEAD;
6029 goto do_class;
6030 case ALPHA:
6031 case ALPHA + ADD_NL:
6032 testval = mask = RI_ALPHA;
6033 goto do_class;
6034 case NALPHA:
6035 case NALPHA + ADD_NL:
6036 mask = RI_ALPHA;
6037 goto do_class;
6038 case LOWER:
6039 case LOWER + ADD_NL:
6040 testval = mask = RI_LOWER;
6041 goto do_class;
6042 case NLOWER:
6043 case NLOWER + ADD_NL:
6044 mask = RI_LOWER;
6045 goto do_class;
6046 case UPPER:
6047 case UPPER + ADD_NL:
6048 testval = mask = RI_UPPER;
6049 goto do_class;
6050 case NUPPER:
6051 case NUPPER + ADD_NL:
6052 mask = RI_UPPER;
6053 goto do_class;
6054
6055 case EXACTLY:
6056 {
6057 int cu, cl;
6058
6059 /* This doesn't do a multi-byte character, because a MULTIBYTECODE
Bram Moolenaara245a5b2007-08-11 11:58:23 +00006060 * would have been used for it. It does handle single-byte
6061 * characters, such as latin1. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006062 if (rex.reg_ic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 {
Bram Moolenaara245a5b2007-08-11 11:58:23 +00006064 cu = MB_TOUPPER(*opnd);
6065 cl = MB_TOLOWER(*opnd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 while (count < maxcount && (*scan == cu || *scan == cl))
6067 {
6068 count++;
6069 scan++;
6070 }
6071 }
6072 else
6073 {
6074 cu = *opnd;
6075 while (count < maxcount && *scan == cu)
6076 {
6077 count++;
6078 scan++;
6079 }
6080 }
6081 break;
6082 }
6083
6084#ifdef FEAT_MBYTE
6085 case MULTIBYTECODE:
6086 {
6087 int i, len, cf = 0;
6088
6089 /* Safety check (just in case 'encoding' was changed since
6090 * compiling the program). */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006091 if ((len = (*mb_ptr2len)(opnd)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006093 if (rex.reg_ic && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 cf = utf_fold(utf_ptr2char(opnd));
Bram Moolenaar069dd082015-05-04 09:56:49 +02006095 while (count < maxcount && (*mb_ptr2len)(scan) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 {
6097 for (i = 0; i < len; ++i)
6098 if (opnd[i] != scan[i])
6099 break;
Bram Moolenaar6100d022016-10-02 16:51:57 +02006100 if (i < len && (!rex.reg_ic || !enc_utf8
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 || utf_fold(utf_ptr2char(scan)) != cf))
6102 break;
6103 scan += len;
6104 ++count;
6105 }
6106 }
6107 }
6108 break;
6109#endif
6110
6111 case ANYOF:
6112 case ANYOF + ADD_NL:
6113 testval = TRUE;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02006114 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115
6116 case ANYBUT:
6117 case ANYBUT + ADD_NL:
6118 while (count < maxcount)
6119 {
6120#ifdef FEAT_MBYTE
6121 int len;
6122#endif
6123 if (*scan == NUL)
6124 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02006125 if (!REG_MULTI || !WITH_NL(OP(p)) || rex.lnum > rex.reg_maxline
Bram Moolenaar6100d022016-10-02 16:51:57 +02006126 || rex.reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 break;
6128 reg_nextline();
Bram Moolenaar0270f382018-07-17 05:43:58 +02006129 scan = rex.input;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 if (got_int)
6131 break;
6132 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006133 else if (rex.reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 ++scan;
6135#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006136 else if (has_mbyte && (len = (*mb_ptr2len)(scan)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 {
6138 if ((cstrchr(opnd, (*mb_ptr2char)(scan)) == NULL) == testval)
6139 break;
6140 scan += len;
6141 }
6142#endif
6143 else
6144 {
6145 if ((cstrchr(opnd, *scan) == NULL) == testval)
6146 break;
6147 ++scan;
6148 }
6149 ++count;
6150 }
6151 break;
6152
6153 case NEWL:
6154 while (count < maxcount
Bram Moolenaar0270f382018-07-17 05:43:58 +02006155 && ((*scan == NUL && rex.lnum <= rex.reg_maxline
Bram Moolenaar6100d022016-10-02 16:51:57 +02006156 && !rex.reg_line_lbr && REG_MULTI)
6157 || (*scan == '\n' && rex.reg_line_lbr)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 {
6159 count++;
Bram Moolenaar6100d022016-10-02 16:51:57 +02006160 if (rex.reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 ADVANCE_REGINPUT();
6162 else
6163 reg_nextline();
Bram Moolenaar0270f382018-07-17 05:43:58 +02006164 scan = rex.input;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 if (got_int)
6166 break;
6167 }
6168 break;
6169
6170 default: /* Oh dear. Called inappropriately. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006171 emsg(_(e_re_corr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172#ifdef DEBUG
6173 printf("Called regrepeat with op code %d\n", OP(p));
6174#endif
6175 break;
6176 }
6177
Bram Moolenaar0270f382018-07-17 05:43:58 +02006178 rex.input = scan;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179
6180 return (int)count;
6181}
6182
6183/*
6184 * regnext - dig the "next" pointer out of a node
Bram Moolenaard3005802009-11-25 17:21:32 +00006185 * Returns NULL when calculating size, when there is no next item and when
6186 * there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 */
6188 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01006189regnext(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190{
6191 int offset;
6192
Bram Moolenaard3005802009-11-25 17:21:32 +00006193 if (p == JUST_CALC_SIZE || reg_toolong)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 return NULL;
6195
6196 offset = NEXT(p);
6197 if (offset == 0)
6198 return NULL;
6199
Bram Moolenaar582fd852005-03-28 20:58:01 +00006200 if (OP(p) == BACK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 return p - offset;
6202 else
6203 return p + offset;
6204}
6205
6206/*
6207 * Check the regexp program for its magic number.
6208 * Return TRUE if it's wrong.
6209 */
6210 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006211prog_magic_wrong(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006213 regprog_T *prog;
6214
Bram Moolenaar6100d022016-10-02 16:51:57 +02006215 prog = REG_MULTI ? rex.reg_mmatch->regprog : rex.reg_match->regprog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006216 if (prog->engine == &nfa_regengine)
6217 /* For NFA matcher we don't check the magic */
6218 return FALSE;
6219
6220 if (UCHARAT(((bt_regprog_T *)prog)->program) != REGMAGIC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006222 emsg(_(e_re_corr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 return TRUE;
6224 }
6225 return FALSE;
6226}
6227
6228/*
6229 * Cleanup the subexpressions, if this wasn't done yet.
6230 * This construction is used to clear the subexpressions only when they are
6231 * used (to increase speed).
6232 */
6233 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006234cleanup_subexpr(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235{
Bram Moolenaar0270f382018-07-17 05:43:58 +02006236 if (rex.need_clear_subexpr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 {
6238 if (REG_MULTI)
6239 {
6240 /* Use 0xff to set lnum to -1 */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006241 vim_memset(rex.reg_startpos, 0xff, sizeof(lpos_T) * NSUBEXP);
6242 vim_memset(rex.reg_endpos, 0xff, sizeof(lpos_T) * NSUBEXP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 }
6244 else
6245 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006246 vim_memset(rex.reg_startp, 0, sizeof(char_u *) * NSUBEXP);
6247 vim_memset(rex.reg_endp, 0, sizeof(char_u *) * NSUBEXP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02006249 rex.need_clear_subexpr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 }
6251}
6252
6253#ifdef FEAT_SYN_HL
6254 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006255cleanup_zsubexpr(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256{
Bram Moolenaar0270f382018-07-17 05:43:58 +02006257 if (rex.need_clear_zsubexpr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 {
6259 if (REG_MULTI)
6260 {
6261 /* Use 0xff to set lnum to -1 */
6262 vim_memset(reg_startzpos, 0xff, sizeof(lpos_T) * NSUBEXP);
6263 vim_memset(reg_endzpos, 0xff, sizeof(lpos_T) * NSUBEXP);
6264 }
6265 else
6266 {
6267 vim_memset(reg_startzp, 0, sizeof(char_u *) * NSUBEXP);
6268 vim_memset(reg_endzp, 0, sizeof(char_u *) * NSUBEXP);
6269 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02006270 rex.need_clear_zsubexpr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 }
6272}
6273#endif
6274
6275/*
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006276 * Save the current subexpr to "bp", so that they can be restored
6277 * later by restore_subexpr().
6278 */
6279 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006280save_subexpr(regbehind_T *bp)
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006281{
6282 int i;
6283
Bram Moolenaar0270f382018-07-17 05:43:58 +02006284 /* When "rex.need_clear_subexpr" is set we don't need to save the values, only
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006285 * remember that this flag needs to be set again when restoring. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006286 bp->save_need_clear_subexpr = rex.need_clear_subexpr;
6287 if (!rex.need_clear_subexpr)
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006288 {
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006289 for (i = 0; i < NSUBEXP; ++i)
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006290 {
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006291 if (REG_MULTI)
6292 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006293 bp->save_start[i].se_u.pos = rex.reg_startpos[i];
6294 bp->save_end[i].se_u.pos = rex.reg_endpos[i];
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006295 }
6296 else
6297 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006298 bp->save_start[i].se_u.ptr = rex.reg_startp[i];
6299 bp->save_end[i].se_u.ptr = rex.reg_endp[i];
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006300 }
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006301 }
6302 }
6303}
6304
6305/*
6306 * Restore the subexpr from "bp".
6307 */
6308 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006309restore_subexpr(regbehind_T *bp)
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006310{
6311 int i;
6312
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006313 /* Only need to restore saved values when they are not to be cleared. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006314 rex.need_clear_subexpr = bp->save_need_clear_subexpr;
6315 if (!rex.need_clear_subexpr)
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006316 {
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006317 for (i = 0; i < NSUBEXP; ++i)
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006318 {
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006319 if (REG_MULTI)
6320 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006321 rex.reg_startpos[i] = bp->save_start[i].se_u.pos;
6322 rex.reg_endpos[i] = bp->save_end[i].se_u.pos;
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006323 }
6324 else
6325 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006326 rex.reg_startp[i] = bp->save_start[i].se_u.ptr;
6327 rex.reg_endp[i] = bp->save_end[i].se_u.ptr;
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006328 }
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006329 }
6330 }
6331}
6332
6333/*
Bram Moolenaar0270f382018-07-17 05:43:58 +02006334 * Advance rex.lnum, rex.line and rex.input to the next line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 */
6336 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006337reg_nextline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338{
Bram Moolenaar0270f382018-07-17 05:43:58 +02006339 rex.line = reg_getline(++rex.lnum);
6340 rex.input = rex.line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 fast_breakcheck();
6342}
6343
6344/*
6345 * Save the input line and position in a regsave_T.
6346 */
6347 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006348reg_save(regsave_T *save, garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349{
6350 if (REG_MULTI)
6351 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02006352 save->rs_u.pos.col = (colnr_T)(rex.input - rex.line);
6353 save->rs_u.pos.lnum = rex.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 }
6355 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02006356 save->rs_u.ptr = rex.input;
Bram Moolenaar582fd852005-03-28 20:58:01 +00006357 save->rs_len = gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358}
6359
6360/*
6361 * Restore the input line and position from a regsave_T.
6362 */
6363 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006364reg_restore(regsave_T *save, garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365{
6366 if (REG_MULTI)
6367 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02006368 if (rex.lnum != save->rs_u.pos.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 {
6370 /* only call reg_getline() when the line number changed to save
6371 * a bit of time */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006372 rex.lnum = save->rs_u.pos.lnum;
6373 rex.line = reg_getline(rex.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02006375 rex.input = rex.line + save->rs_u.pos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 }
6377 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02006378 rex.input = save->rs_u.ptr;
Bram Moolenaar582fd852005-03-28 20:58:01 +00006379 gap->ga_len = save->rs_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380}
6381
6382/*
6383 * Return TRUE if current position is equal to saved position.
6384 */
6385 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006386reg_save_equal(regsave_T *save)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387{
6388 if (REG_MULTI)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006389 return rex.lnum == save->rs_u.pos.lnum
6390 && rex.input == rex.line + save->rs_u.pos.col;
6391 return rex.input == save->rs_u.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392}
6393
6394/*
6395 * Tentatively set the sub-expression start to the current position (after
6396 * calling regmatch() they will have changed). Need to save the existing
6397 * values for when there is no match.
6398 * Use se_save() to use pointer (save_se_multi()) or position (save_se_one()),
6399 * depending on REG_MULTI.
6400 */
6401 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006402save_se_multi(save_se_T *savep, lpos_T *posp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403{
6404 savep->se_u.pos = *posp;
Bram Moolenaar0270f382018-07-17 05:43:58 +02006405 posp->lnum = rex.lnum;
6406 posp->col = (colnr_T)(rex.input - rex.line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407}
6408
6409 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006410save_se_one(save_se_T *savep, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411{
6412 savep->se_u.ptr = *pp;
Bram Moolenaar0270f382018-07-17 05:43:58 +02006413 *pp = rex.input;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414}
6415
6416/*
6417 * Compare a number with the operand of RE_LNUM, RE_COL or RE_VCOL.
6418 */
6419 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006420re_num_cmp(long_u val, char_u *scan)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421{
6422 long_u n = OPERAND_MIN(scan);
6423
6424 if (OPERAND_CMP(scan) == '>')
6425 return val > n;
6426 if (OPERAND_CMP(scan) == '<')
6427 return val < n;
6428 return val == n;
6429}
6430
Bram Moolenaar580abea2013-06-14 20:31:28 +02006431/*
6432 * Check whether a backreference matches.
6433 * Returns RA_FAIL, RA_NOMATCH or RA_MATCH.
Bram Moolenaar438ee5b2013-11-21 17:13:00 +01006434 * If "bytelen" is not NULL, it is set to the byte length of the match in the
6435 * last line.
Bram Moolenaar580abea2013-06-14 20:31:28 +02006436 */
6437 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006438match_with_backref(
6439 linenr_T start_lnum,
6440 colnr_T start_col,
6441 linenr_T end_lnum,
6442 colnr_T end_col,
6443 int *bytelen)
Bram Moolenaar580abea2013-06-14 20:31:28 +02006444{
6445 linenr_T clnum = start_lnum;
6446 colnr_T ccol = start_col;
6447 int len;
6448 char_u *p;
6449
6450 if (bytelen != NULL)
6451 *bytelen = 0;
6452 for (;;)
6453 {
6454 /* Since getting one line may invalidate the other, need to make copy.
6455 * Slow! */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006456 if (rex.line != reg_tofree)
Bram Moolenaar580abea2013-06-14 20:31:28 +02006457 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02006458 len = (int)STRLEN(rex.line);
Bram Moolenaar580abea2013-06-14 20:31:28 +02006459 if (reg_tofree == NULL || len >= (int)reg_tofreelen)
6460 {
6461 len += 50; /* get some extra */
6462 vim_free(reg_tofree);
6463 reg_tofree = alloc(len);
6464 if (reg_tofree == NULL)
6465 return RA_FAIL; /* out of memory!*/
6466 reg_tofreelen = len;
6467 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02006468 STRCPY(reg_tofree, rex.line);
6469 rex.input = reg_tofree + (rex.input - rex.line);
6470 rex.line = reg_tofree;
Bram Moolenaar580abea2013-06-14 20:31:28 +02006471 }
6472
6473 /* Get the line to compare with. */
6474 p = reg_getline(clnum);
6475 if (clnum == end_lnum)
6476 len = end_col - ccol;
6477 else
6478 len = (int)STRLEN(p + ccol);
6479
Bram Moolenaar0270f382018-07-17 05:43:58 +02006480 if (cstrncmp(p + ccol, rex.input, &len) != 0)
Bram Moolenaar580abea2013-06-14 20:31:28 +02006481 return RA_NOMATCH; /* doesn't match */
6482 if (bytelen != NULL)
6483 *bytelen += len;
6484 if (clnum == end_lnum)
6485 break; /* match and at end! */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006486 if (rex.lnum >= rex.reg_maxline)
Bram Moolenaar580abea2013-06-14 20:31:28 +02006487 return RA_NOMATCH; /* text too short */
6488
6489 /* Advance to next line. */
6490 reg_nextline();
Bram Moolenaar438ee5b2013-11-21 17:13:00 +01006491 if (bytelen != NULL)
6492 *bytelen = 0;
Bram Moolenaar580abea2013-06-14 20:31:28 +02006493 ++clnum;
6494 ccol = 0;
6495 if (got_int)
6496 return RA_FAIL;
6497 }
6498
Bram Moolenaar0270f382018-07-17 05:43:58 +02006499 /* found a match! Note that rex.line may now point to a copy of the line,
Bram Moolenaar580abea2013-06-14 20:31:28 +02006500 * that should not matter. */
6501 return RA_MATCH;
6502}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006504#ifdef BT_REGEXP_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505
6506/*
6507 * regdump - dump a regexp onto stdout in vaguely comprehensible form
6508 */
6509 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006510regdump(char_u *pattern, bt_regprog_T *r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511{
6512 char_u *s;
6513 int op = EXACTLY; /* Arbitrary non-END op. */
6514 char_u *next;
6515 char_u *end = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006516 FILE *f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006518#ifdef BT_REGEXP_LOG
6519 f = fopen("bt_regexp_log.log", "a");
6520#else
6521 f = stdout;
6522#endif
6523 if (f == NULL)
6524 return;
6525 fprintf(f, "-------------------------------------\n\r\nregcomp(%s):\r\n", pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526
6527 s = r->program + 1;
6528 /*
6529 * Loop until we find the END that isn't before a referred next (an END
6530 * can also appear in a NOMATCH operand).
6531 */
6532 while (op != END || s <= end)
6533 {
6534 op = OP(s);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006535 fprintf(f, "%2d%s", (int)(s - r->program), regprop(s)); /* Where, what. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 next = regnext(s);
6537 if (next == NULL) /* Next ptr. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006538 fprintf(f, "(0)");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006540 fprintf(f, "(%d)", (int)((s - r->program) + (next - s)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 if (end < next)
6542 end = next;
6543 if (op == BRACE_LIMITS)
6544 {
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02006545 /* Two ints */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006546 fprintf(f, " minval %ld, maxval %ld", OPERAND_MIN(s), OPERAND_MAX(s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 s += 8;
6548 }
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02006549 else if (op == BEHIND || op == NOBEHIND)
6550 {
6551 /* one int */
6552 fprintf(f, " count %ld", OPERAND_MIN(s));
6553 s += 4;
6554 }
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +02006555 else if (op == RE_LNUM || op == RE_COL || op == RE_VCOL)
6556 {
6557 /* one int plus comperator */
6558 fprintf(f, " count %ld", OPERAND_MIN(s));
6559 s += 5;
6560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 s += 3;
6562 if (op == ANYOF || op == ANYOF + ADD_NL
6563 || op == ANYBUT || op == ANYBUT + ADD_NL
6564 || op == EXACTLY)
6565 {
6566 /* Literal string, where present. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006567 fprintf(f, "\nxxxxxxxxx\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 while (*s != NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006569 fprintf(f, "%c", *s++);
6570 fprintf(f, "\nxxxxxxxxx\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 s++;
6572 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006573 fprintf(f, "\r\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 }
6575
6576 /* Header fields of interest. */
6577 if (r->regstart != NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006578 fprintf(f, "start `%s' 0x%x; ", r->regstart < 256
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 ? (char *)transchar(r->regstart)
6580 : "multibyte", r->regstart);
6581 if (r->reganch)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006582 fprintf(f, "anchored; ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 if (r->regmust != NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006584 fprintf(f, "must have \"%s\"", r->regmust);
6585 fprintf(f, "\r\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006587#ifdef BT_REGEXP_LOG
6588 fclose(f);
6589#endif
6590}
6591#endif /* BT_REGEXP_DUMP */
6592
6593#ifdef DEBUG
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594/*
6595 * regprop - printable representation of opcode
6596 */
6597 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01006598regprop(char_u *op)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006600 char *p;
6601 static char buf[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006603 STRCPY(buf, ":");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006605 switch ((int) OP(op))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 {
6607 case BOL:
6608 p = "BOL";
6609 break;
6610 case EOL:
6611 p = "EOL";
6612 break;
6613 case RE_BOF:
6614 p = "BOF";
6615 break;
6616 case RE_EOF:
6617 p = "EOF";
6618 break;
6619 case CURSOR:
6620 p = "CURSOR";
6621 break;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00006622 case RE_VISUAL:
6623 p = "RE_VISUAL";
6624 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 case RE_LNUM:
6626 p = "RE_LNUM";
6627 break;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00006628 case RE_MARK:
6629 p = "RE_MARK";
6630 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 case RE_COL:
6632 p = "RE_COL";
6633 break;
6634 case RE_VCOL:
6635 p = "RE_VCOL";
6636 break;
6637 case BOW:
6638 p = "BOW";
6639 break;
6640 case EOW:
6641 p = "EOW";
6642 break;
6643 case ANY:
6644 p = "ANY";
6645 break;
6646 case ANY + ADD_NL:
6647 p = "ANY+NL";
6648 break;
6649 case ANYOF:
6650 p = "ANYOF";
6651 break;
6652 case ANYOF + ADD_NL:
6653 p = "ANYOF+NL";
6654 break;
6655 case ANYBUT:
6656 p = "ANYBUT";
6657 break;
6658 case ANYBUT + ADD_NL:
6659 p = "ANYBUT+NL";
6660 break;
6661 case IDENT:
6662 p = "IDENT";
6663 break;
6664 case IDENT + ADD_NL:
6665 p = "IDENT+NL";
6666 break;
6667 case SIDENT:
6668 p = "SIDENT";
6669 break;
6670 case SIDENT + ADD_NL:
6671 p = "SIDENT+NL";
6672 break;
6673 case KWORD:
6674 p = "KWORD";
6675 break;
6676 case KWORD + ADD_NL:
6677 p = "KWORD+NL";
6678 break;
6679 case SKWORD:
6680 p = "SKWORD";
6681 break;
6682 case SKWORD + ADD_NL:
6683 p = "SKWORD+NL";
6684 break;
6685 case FNAME:
6686 p = "FNAME";
6687 break;
6688 case FNAME + ADD_NL:
6689 p = "FNAME+NL";
6690 break;
6691 case SFNAME:
6692 p = "SFNAME";
6693 break;
6694 case SFNAME + ADD_NL:
6695 p = "SFNAME+NL";
6696 break;
6697 case PRINT:
6698 p = "PRINT";
6699 break;
6700 case PRINT + ADD_NL:
6701 p = "PRINT+NL";
6702 break;
6703 case SPRINT:
6704 p = "SPRINT";
6705 break;
6706 case SPRINT + ADD_NL:
6707 p = "SPRINT+NL";
6708 break;
6709 case WHITE:
6710 p = "WHITE";
6711 break;
6712 case WHITE + ADD_NL:
6713 p = "WHITE+NL";
6714 break;
6715 case NWHITE:
6716 p = "NWHITE";
6717 break;
6718 case NWHITE + ADD_NL:
6719 p = "NWHITE+NL";
6720 break;
6721 case DIGIT:
6722 p = "DIGIT";
6723 break;
6724 case DIGIT + ADD_NL:
6725 p = "DIGIT+NL";
6726 break;
6727 case NDIGIT:
6728 p = "NDIGIT";
6729 break;
6730 case NDIGIT + ADD_NL:
6731 p = "NDIGIT+NL";
6732 break;
6733 case HEX:
6734 p = "HEX";
6735 break;
6736 case HEX + ADD_NL:
6737 p = "HEX+NL";
6738 break;
6739 case NHEX:
6740 p = "NHEX";
6741 break;
6742 case NHEX + ADD_NL:
6743 p = "NHEX+NL";
6744 break;
6745 case OCTAL:
6746 p = "OCTAL";
6747 break;
6748 case OCTAL + ADD_NL:
6749 p = "OCTAL+NL";
6750 break;
6751 case NOCTAL:
6752 p = "NOCTAL";
6753 break;
6754 case NOCTAL + ADD_NL:
6755 p = "NOCTAL+NL";
6756 break;
6757 case WORD:
6758 p = "WORD";
6759 break;
6760 case WORD + ADD_NL:
6761 p = "WORD+NL";
6762 break;
6763 case NWORD:
6764 p = "NWORD";
6765 break;
6766 case NWORD + ADD_NL:
6767 p = "NWORD+NL";
6768 break;
6769 case HEAD:
6770 p = "HEAD";
6771 break;
6772 case HEAD + ADD_NL:
6773 p = "HEAD+NL";
6774 break;
6775 case NHEAD:
6776 p = "NHEAD";
6777 break;
6778 case NHEAD + ADD_NL:
6779 p = "NHEAD+NL";
6780 break;
6781 case ALPHA:
6782 p = "ALPHA";
6783 break;
6784 case ALPHA + ADD_NL:
6785 p = "ALPHA+NL";
6786 break;
6787 case NALPHA:
6788 p = "NALPHA";
6789 break;
6790 case NALPHA + ADD_NL:
6791 p = "NALPHA+NL";
6792 break;
6793 case LOWER:
6794 p = "LOWER";
6795 break;
6796 case LOWER + ADD_NL:
6797 p = "LOWER+NL";
6798 break;
6799 case NLOWER:
6800 p = "NLOWER";
6801 break;
6802 case NLOWER + ADD_NL:
6803 p = "NLOWER+NL";
6804 break;
6805 case UPPER:
6806 p = "UPPER";
6807 break;
6808 case UPPER + ADD_NL:
6809 p = "UPPER+NL";
6810 break;
6811 case NUPPER:
6812 p = "NUPPER";
6813 break;
6814 case NUPPER + ADD_NL:
6815 p = "NUPPER+NL";
6816 break;
6817 case BRANCH:
6818 p = "BRANCH";
6819 break;
6820 case EXACTLY:
6821 p = "EXACTLY";
6822 break;
6823 case NOTHING:
6824 p = "NOTHING";
6825 break;
6826 case BACK:
6827 p = "BACK";
6828 break;
6829 case END:
6830 p = "END";
6831 break;
6832 case MOPEN + 0:
6833 p = "MATCH START";
6834 break;
6835 case MOPEN + 1:
6836 case MOPEN + 2:
6837 case MOPEN + 3:
6838 case MOPEN + 4:
6839 case MOPEN + 5:
6840 case MOPEN + 6:
6841 case MOPEN + 7:
6842 case MOPEN + 8:
6843 case MOPEN + 9:
6844 sprintf(buf + STRLEN(buf), "MOPEN%d", OP(op) - MOPEN);
6845 p = NULL;
6846 break;
6847 case MCLOSE + 0:
6848 p = "MATCH END";
6849 break;
6850 case MCLOSE + 1:
6851 case MCLOSE + 2:
6852 case MCLOSE + 3:
6853 case MCLOSE + 4:
6854 case MCLOSE + 5:
6855 case MCLOSE + 6:
6856 case MCLOSE + 7:
6857 case MCLOSE + 8:
6858 case MCLOSE + 9:
6859 sprintf(buf + STRLEN(buf), "MCLOSE%d", OP(op) - MCLOSE);
6860 p = NULL;
6861 break;
6862 case BACKREF + 1:
6863 case BACKREF + 2:
6864 case BACKREF + 3:
6865 case BACKREF + 4:
6866 case BACKREF + 5:
6867 case BACKREF + 6:
6868 case BACKREF + 7:
6869 case BACKREF + 8:
6870 case BACKREF + 9:
6871 sprintf(buf + STRLEN(buf), "BACKREF%d", OP(op) - BACKREF);
6872 p = NULL;
6873 break;
6874 case NOPEN:
6875 p = "NOPEN";
6876 break;
6877 case NCLOSE:
6878 p = "NCLOSE";
6879 break;
6880#ifdef FEAT_SYN_HL
6881 case ZOPEN + 1:
6882 case ZOPEN + 2:
6883 case ZOPEN + 3:
6884 case ZOPEN + 4:
6885 case ZOPEN + 5:
6886 case ZOPEN + 6:
6887 case ZOPEN + 7:
6888 case ZOPEN + 8:
6889 case ZOPEN + 9:
6890 sprintf(buf + STRLEN(buf), "ZOPEN%d", OP(op) - ZOPEN);
6891 p = NULL;
6892 break;
6893 case ZCLOSE + 1:
6894 case ZCLOSE + 2:
6895 case ZCLOSE + 3:
6896 case ZCLOSE + 4:
6897 case ZCLOSE + 5:
6898 case ZCLOSE + 6:
6899 case ZCLOSE + 7:
6900 case ZCLOSE + 8:
6901 case ZCLOSE + 9:
6902 sprintf(buf + STRLEN(buf), "ZCLOSE%d", OP(op) - ZCLOSE);
6903 p = NULL;
6904 break;
6905 case ZREF + 1:
6906 case ZREF + 2:
6907 case ZREF + 3:
6908 case ZREF + 4:
6909 case ZREF + 5:
6910 case ZREF + 6:
6911 case ZREF + 7:
6912 case ZREF + 8:
6913 case ZREF + 9:
6914 sprintf(buf + STRLEN(buf), "ZREF%d", OP(op) - ZREF);
6915 p = NULL;
6916 break;
6917#endif
6918 case STAR:
6919 p = "STAR";
6920 break;
6921 case PLUS:
6922 p = "PLUS";
6923 break;
6924 case NOMATCH:
6925 p = "NOMATCH";
6926 break;
6927 case MATCH:
6928 p = "MATCH";
6929 break;
6930 case BEHIND:
6931 p = "BEHIND";
6932 break;
6933 case NOBEHIND:
6934 p = "NOBEHIND";
6935 break;
6936 case SUBPAT:
6937 p = "SUBPAT";
6938 break;
6939 case BRACE_LIMITS:
6940 p = "BRACE_LIMITS";
6941 break;
6942 case BRACE_SIMPLE:
6943 p = "BRACE_SIMPLE";
6944 break;
6945 case BRACE_COMPLEX + 0:
6946 case BRACE_COMPLEX + 1:
6947 case BRACE_COMPLEX + 2:
6948 case BRACE_COMPLEX + 3:
6949 case BRACE_COMPLEX + 4:
6950 case BRACE_COMPLEX + 5:
6951 case BRACE_COMPLEX + 6:
6952 case BRACE_COMPLEX + 7:
6953 case BRACE_COMPLEX + 8:
6954 case BRACE_COMPLEX + 9:
6955 sprintf(buf + STRLEN(buf), "BRACE_COMPLEX%d", OP(op) - BRACE_COMPLEX);
6956 p = NULL;
6957 break;
6958#ifdef FEAT_MBYTE
6959 case MULTIBYTECODE:
6960 p = "MULTIBYTECODE";
6961 break;
6962#endif
6963 case NEWL:
6964 p = "NEWL";
6965 break;
6966 default:
6967 sprintf(buf + STRLEN(buf), "corrupt %d", OP(op));
6968 p = NULL;
6969 break;
6970 }
6971 if (p != NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006972 STRCAT(buf, p);
6973 return (char_u *)buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006975#endif /* DEBUG */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976
Bram Moolenaarfb031402014-09-09 17:18:49 +02006977/*
6978 * Used in a place where no * or \+ can follow.
6979 */
6980 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006981re_mult_next(char *what)
Bram Moolenaarfb031402014-09-09 17:18:49 +02006982{
6983 if (re_multi_type(peekchr()) == MULTI_MULT)
Bram Moolenaar1be45b22019-01-14 22:46:15 +01006984 {
6985 semsg(_("E888: (NFA regexp) cannot repeat %s"), what);
6986 rc_did_emsg = TRUE;
6987 return FAIL;
6988 }
Bram Moolenaarfb031402014-09-09 17:18:49 +02006989 return OK;
6990}
6991
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993typedef struct
6994{
6995 int a, b, c;
6996} decomp_T;
6997
6998
6999/* 0xfb20 - 0xfb4f */
Bram Moolenaard6f676d2005-06-01 21:51:55 +00007000static decomp_T decomp_table[0xfb4f-0xfb20+1] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001{
7002 {0x5e2,0,0}, /* 0xfb20 alt ayin */
7003 {0x5d0,0,0}, /* 0xfb21 alt alef */
7004 {0x5d3,0,0}, /* 0xfb22 alt dalet */
7005 {0x5d4,0,0}, /* 0xfb23 alt he */
7006 {0x5db,0,0}, /* 0xfb24 alt kaf */
7007 {0x5dc,0,0}, /* 0xfb25 alt lamed */
7008 {0x5dd,0,0}, /* 0xfb26 alt mem-sofit */
7009 {0x5e8,0,0}, /* 0xfb27 alt resh */
7010 {0x5ea,0,0}, /* 0xfb28 alt tav */
7011 {'+', 0, 0}, /* 0xfb29 alt plus */
7012 {0x5e9, 0x5c1, 0}, /* 0xfb2a shin+shin-dot */
7013 {0x5e9, 0x5c2, 0}, /* 0xfb2b shin+sin-dot */
7014 {0x5e9, 0x5c1, 0x5bc}, /* 0xfb2c shin+shin-dot+dagesh */
7015 {0x5e9, 0x5c2, 0x5bc}, /* 0xfb2d shin+sin-dot+dagesh */
7016 {0x5d0, 0x5b7, 0}, /* 0xfb2e alef+patah */
7017 {0x5d0, 0x5b8, 0}, /* 0xfb2f alef+qamats */
7018 {0x5d0, 0x5b4, 0}, /* 0xfb30 alef+hiriq */
7019 {0x5d1, 0x5bc, 0}, /* 0xfb31 bet+dagesh */
7020 {0x5d2, 0x5bc, 0}, /* 0xfb32 gimel+dagesh */
7021 {0x5d3, 0x5bc, 0}, /* 0xfb33 dalet+dagesh */
7022 {0x5d4, 0x5bc, 0}, /* 0xfb34 he+dagesh */
7023 {0x5d5, 0x5bc, 0}, /* 0xfb35 vav+dagesh */
7024 {0x5d6, 0x5bc, 0}, /* 0xfb36 zayin+dagesh */
7025 {0xfb37, 0, 0}, /* 0xfb37 -- UNUSED */
7026 {0x5d8, 0x5bc, 0}, /* 0xfb38 tet+dagesh */
7027 {0x5d9, 0x5bc, 0}, /* 0xfb39 yud+dagesh */
7028 {0x5da, 0x5bc, 0}, /* 0xfb3a kaf sofit+dagesh */
7029 {0x5db, 0x5bc, 0}, /* 0xfb3b kaf+dagesh */
7030 {0x5dc, 0x5bc, 0}, /* 0xfb3c lamed+dagesh */
7031 {0xfb3d, 0, 0}, /* 0xfb3d -- UNUSED */
7032 {0x5de, 0x5bc, 0}, /* 0xfb3e mem+dagesh */
7033 {0xfb3f, 0, 0}, /* 0xfb3f -- UNUSED */
7034 {0x5e0, 0x5bc, 0}, /* 0xfb40 nun+dagesh */
7035 {0x5e1, 0x5bc, 0}, /* 0xfb41 samech+dagesh */
7036 {0xfb42, 0, 0}, /* 0xfb42 -- UNUSED */
7037 {0x5e3, 0x5bc, 0}, /* 0xfb43 pe sofit+dagesh */
7038 {0x5e4, 0x5bc,0}, /* 0xfb44 pe+dagesh */
7039 {0xfb45, 0, 0}, /* 0xfb45 -- UNUSED */
7040 {0x5e6, 0x5bc, 0}, /* 0xfb46 tsadi+dagesh */
7041 {0x5e7, 0x5bc, 0}, /* 0xfb47 qof+dagesh */
7042 {0x5e8, 0x5bc, 0}, /* 0xfb48 resh+dagesh */
7043 {0x5e9, 0x5bc, 0}, /* 0xfb49 shin+dagesh */
7044 {0x5ea, 0x5bc, 0}, /* 0xfb4a tav+dagesh */
7045 {0x5d5, 0x5b9, 0}, /* 0xfb4b vav+holam */
7046 {0x5d1, 0x5bf, 0}, /* 0xfb4c bet+rafe */
7047 {0x5db, 0x5bf, 0}, /* 0xfb4d kaf+rafe */
7048 {0x5e4, 0x5bf, 0}, /* 0xfb4e pe+rafe */
7049 {0x5d0, 0x5dc, 0} /* 0xfb4f alef-lamed */
7050};
7051
7052 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007053mb_decompose(int c, int *c1, int *c2, int *c3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054{
7055 decomp_T d;
7056
Bram Moolenaar2eec59e2013-05-21 21:37:20 +02007057 if (c >= 0xfb20 && c <= 0xfb4f)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 {
7059 d = decomp_table[c - 0xfb20];
7060 *c1 = d.a;
7061 *c2 = d.b;
7062 *c3 = d.c;
7063 }
7064 else
7065 {
7066 *c1 = c;
7067 *c2 = *c3 = 0;
7068 }
7069}
7070#endif
7071
7072/*
Bram Moolenaar6100d022016-10-02 16:51:57 +02007073 * Compare two strings, ignore case if rex.reg_ic set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 * Return 0 if strings match, non-zero otherwise.
7075 * Correct the length "*n" when composing characters are ignored.
7076 */
7077 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007078cstrncmp(char_u *s1, char_u *s2, int *n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079{
7080 int result;
7081
Bram Moolenaar6100d022016-10-02 16:51:57 +02007082 if (!rex.reg_ic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083 result = STRNCMP(s1, s2, *n);
7084 else
7085 result = MB_STRNICMP(s1, s2, *n);
7086
7087#ifdef FEAT_MBYTE
7088 /* if it failed and it's utf8 and we want to combineignore: */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007089 if (result != 0 && enc_utf8 && rex.reg_icombine)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 {
7091 char_u *str1, *str2;
7092 int c1, c2, c11, c12;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 int junk;
7094
7095 /* we have to handle the strcmp ourselves, since it is necessary to
7096 * deal with the composing characters by ignoring them: */
7097 str1 = s1;
7098 str2 = s2;
7099 c1 = c2 = 0;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007100 while ((int)(str1 - s1) < *n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 {
7102 c1 = mb_ptr2char_adv(&str1);
7103 c2 = mb_ptr2char_adv(&str2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104
7105 /* decompose the character if necessary, into 'base' characters
7106 * because I don't care about Arabic, I will hard-code the Hebrew
7107 * which I *do* care about! So sue me... */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007108 if (c1 != c2 && (!rex.reg_ic || utf_fold(c1) != utf_fold(c2)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 {
7110 /* decomposition necessary? */
7111 mb_decompose(c1, &c11, &junk, &junk);
7112 mb_decompose(c2, &c12, &junk, &junk);
7113 c1 = c11;
7114 c2 = c12;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007115 if (c11 != c12
7116 && (!rex.reg_ic || utf_fold(c11) != utf_fold(c12)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 break;
7118 }
7119 }
7120 result = c2 - c1;
7121 if (result == 0)
7122 *n = (int)(str2 - s2);
7123 }
7124#endif
7125
7126 return result;
7127}
7128
7129/*
7130 * cstrchr: This function is used a lot for simple searches, keep it fast!
7131 */
7132 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01007133cstrchr(char_u *s, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134{
7135 char_u *p;
7136 int cc;
7137
Bram Moolenaar6100d022016-10-02 16:51:57 +02007138 if (!rex.reg_ic
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139#ifdef FEAT_MBYTE
7140 || (!enc_utf8 && mb_char2len(c) > 1)
7141#endif
7142 )
7143 return vim_strchr(s, c);
7144
7145 /* tolower() and toupper() can be slow, comparing twice should be a lot
7146 * faster (esp. when using MS Visual C++!).
7147 * For UTF-8 need to use folded case. */
7148#ifdef FEAT_MBYTE
7149 if (enc_utf8 && c > 0x80)
7150 cc = utf_fold(c);
7151 else
7152#endif
Bram Moolenaara245a5b2007-08-11 11:58:23 +00007153 if (MB_ISUPPER(c))
7154 cc = MB_TOLOWER(c);
7155 else if (MB_ISLOWER(c))
7156 cc = MB_TOUPPER(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 else
7158 return vim_strchr(s, c);
7159
7160#ifdef FEAT_MBYTE
7161 if (has_mbyte)
7162 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007163 for (p = s; *p != NUL; p += (*mb_ptr2len)(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 {
7165 if (enc_utf8 && c > 0x80)
7166 {
7167 if (utf_fold(utf_ptr2char(p)) == cc)
7168 return p;
7169 }
7170 else if (*p == c || *p == cc)
7171 return p;
7172 }
7173 }
7174 else
7175#endif
7176 /* Faster version for when there are no multi-byte characters. */
7177 for (p = s; *p != NUL; ++p)
7178 if (*p == c || *p == cc)
7179 return p;
7180
7181 return NULL;
7182}
7183
7184/***************************************************************
7185 * regsub stuff *
7186 ***************************************************************/
7187
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188/*
7189 * We should define ftpr as a pointer to a function returning a pointer to
7190 * a function returning a pointer to a function ...
7191 * This is impossible, so we declare a pointer to a function returning a
7192 * pointer to a function returning void. This should work for all compilers.
7193 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007194typedef void (*(*fptr_T)(int *, int))();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007196static int vim_regsub_both(char_u *source, typval_T *expr, char_u *dest, int copy, int magic, int backslash);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007198 static fptr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01007199do_upper(int *d, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200{
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007201 *d = MB_TOUPPER(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007203 return (fptr_T)NULL;
7204}
7205
7206 static fptr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01007207do_Upper(int *d, int c)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007208{
7209 *d = MB_TOUPPER(c);
7210
7211 return (fptr_T)do_Upper;
7212}
7213
7214 static fptr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01007215do_lower(int *d, int c)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007216{
7217 *d = MB_TOLOWER(c);
7218
7219 return (fptr_T)NULL;
7220}
7221
7222 static fptr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01007223do_Lower(int *d, int c)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007224{
7225 *d = MB_TOLOWER(c);
7226
7227 return (fptr_T)do_Lower;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228}
7229
7230/*
7231 * regtilde(): Replace tildes in the pattern by the old pattern.
7232 *
7233 * Short explanation of the tilde: It stands for the previous replacement
7234 * pattern. If that previous pattern also contains a ~ we should go back a
7235 * step further... But we insert the previous pattern into the current one
7236 * and remember that.
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007237 * This still does not handle the case where "magic" changes. So require the
7238 * user to keep his hands off of "magic".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 *
7240 * The tildes are parsed once before the first call to vim_regsub().
7241 */
7242 char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01007243regtilde(char_u *source, int magic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244{
7245 char_u *newsub = source;
7246 char_u *tmpsub;
7247 char_u *p;
7248 int len;
7249 int prevlen;
7250
7251 for (p = newsub; *p; ++p)
7252 {
7253 if ((*p == '~' && magic) || (*p == '\\' && *(p + 1) == '~' && !magic))
7254 {
7255 if (reg_prev_sub != NULL)
7256 {
7257 /* length = len(newsub) - 1 + len(prev_sub) + 1 */
7258 prevlen = (int)STRLEN(reg_prev_sub);
7259 tmpsub = alloc((unsigned)(STRLEN(newsub) + prevlen));
7260 if (tmpsub != NULL)
7261 {
7262 /* copy prefix */
7263 len = (int)(p - newsub); /* not including ~ */
7264 mch_memmove(tmpsub, newsub, (size_t)len);
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00007265 /* interpret tilde */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 mch_memmove(tmpsub + len, reg_prev_sub, (size_t)prevlen);
7267 /* copy postfix */
7268 if (!magic)
7269 ++p; /* back off \ */
7270 STRCPY(tmpsub + len + prevlen, p + 1);
7271
7272 if (newsub != source) /* already allocated newsub */
7273 vim_free(newsub);
7274 newsub = tmpsub;
7275 p = newsub + len + prevlen;
7276 }
7277 }
7278 else if (magic)
Bram Moolenaar446cb832008-06-24 21:56:24 +00007279 STRMOVE(p, p + 1); /* remove '~' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 else
Bram Moolenaar446cb832008-06-24 21:56:24 +00007281 STRMOVE(p, p + 2); /* remove '\~' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 --p;
7283 }
7284 else
7285 {
7286 if (*p == '\\' && p[1]) /* skip escaped characters */
7287 ++p;
7288#ifdef FEAT_MBYTE
7289 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007290 p += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291#endif
7292 }
7293 }
7294
7295 vim_free(reg_prev_sub);
7296 if (newsub != source) /* newsub was allocated, just keep it */
7297 reg_prev_sub = newsub;
7298 else /* no ~ found, need to save newsub */
7299 reg_prev_sub = vim_strsave(newsub);
7300 return newsub;
7301}
7302
7303#ifdef FEAT_EVAL
7304static int can_f_submatch = FALSE; /* TRUE when submatch() can be used */
7305
Bram Moolenaar6100d022016-10-02 16:51:57 +02007306/* These pointers are used for reg_submatch(). Needed for when the
7307 * substitution string is an expression that contains a call to substitute()
7308 * and submatch(). */
7309typedef struct {
7310 regmatch_T *sm_match;
7311 regmmatch_T *sm_mmatch;
7312 linenr_T sm_firstlnum;
7313 linenr_T sm_maxline;
7314 int sm_line_lbr;
7315} regsubmatch_T;
7316
7317static regsubmatch_T rsm; /* can only be used when can_f_submatch is TRUE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318#endif
7319
7320#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardf48fb42016-07-22 21:50:18 +02007321
7322/*
7323 * Put the submatches in "argv[0]" which is a list passed into call_func() by
7324 * vim_regsub_both().
7325 */
7326 static int
7327fill_submatch_list(int argc UNUSED, typval_T *argv, int argcount)
7328{
7329 listitem_T *li;
7330 int i;
7331 char_u *s;
7332
7333 if (argcount == 0)
7334 /* called function doesn't take an argument */
7335 return 0;
7336
7337 /* Relies on sl_list to be the first item in staticList10_T. */
7338 init_static_list((staticList10_T *)(argv->vval.v_list));
7339
7340 /* There are always 10 list items in staticList10_T. */
7341 li = argv->vval.v_list->lv_first;
7342 for (i = 0; i < 10; ++i)
7343 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007344 s = rsm.sm_match->startp[i];
7345 if (s == NULL || rsm.sm_match->endp[i] == NULL)
Bram Moolenaardf48fb42016-07-22 21:50:18 +02007346 s = NULL;
7347 else
Bram Moolenaar6100d022016-10-02 16:51:57 +02007348 s = vim_strnsave(s, (int)(rsm.sm_match->endp[i] - s));
Bram Moolenaardf48fb42016-07-22 21:50:18 +02007349 li->li_tv.v_type = VAR_STRING;
7350 li->li_tv.vval.v_string = s;
7351 li = li->li_next;
7352 }
7353 return 1;
7354}
7355
7356 static void
7357clear_submatch_list(staticList10_T *sl)
7358{
7359 int i;
7360
7361 for (i = 0; i < 10; ++i)
7362 vim_free(sl->sl_items[i].li_tv.vval.v_string);
7363}
7364
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365/*
7366 * vim_regsub() - perform substitutions after a vim_regexec() or
7367 * vim_regexec_multi() match.
7368 *
7369 * If "copy" is TRUE really copy into "dest".
7370 * If "copy" is FALSE nothing is copied, this is just to find out the length
7371 * of the result.
7372 *
7373 * If "backslash" is TRUE, a backslash will be removed later, need to double
7374 * them to keep them, and insert a backslash before a CR to avoid it being
7375 * replaced with a line break later.
7376 *
7377 * Note: The matched text must not change between the call of
7378 * vim_regexec()/vim_regexec_multi() and vim_regsub()! It would make the back
7379 * references invalid!
7380 *
7381 * Returns the size of the replacement, including terminating NUL.
7382 */
7383 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007384vim_regsub(
7385 regmatch_T *rmp,
7386 char_u *source,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007387 typval_T *expr,
Bram Moolenaar05540972016-01-30 20:31:25 +01007388 char_u *dest,
7389 int copy,
7390 int magic,
7391 int backslash)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392{
Bram Moolenaar6100d022016-10-02 16:51:57 +02007393 int result;
7394 regexec_T rex_save;
7395 int rex_in_use_save = rex_in_use;
7396
7397 if (rex_in_use)
7398 /* Being called recursively, save the state. */
7399 rex_save = rex;
7400 rex_in_use = TRUE;
7401
7402 rex.reg_match = rmp;
7403 rex.reg_mmatch = NULL;
7404 rex.reg_maxline = 0;
7405 rex.reg_buf = curbuf;
7406 rex.reg_line_lbr = TRUE;
7407 result = vim_regsub_both(source, expr, dest, copy, magic, backslash);
7408
7409 rex_in_use = rex_in_use_save;
7410 if (rex_in_use)
7411 rex = rex_save;
7412
7413 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414}
7415#endif
7416
7417 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007418vim_regsub_multi(
7419 regmmatch_T *rmp,
7420 linenr_T lnum,
7421 char_u *source,
7422 char_u *dest,
7423 int copy,
7424 int magic,
7425 int backslash)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426{
Bram Moolenaar6100d022016-10-02 16:51:57 +02007427 int result;
7428 regexec_T rex_save;
7429 int rex_in_use_save = rex_in_use;
7430
7431 if (rex_in_use)
7432 /* Being called recursively, save the state. */
7433 rex_save = rex;
7434 rex_in_use = TRUE;
7435
7436 rex.reg_match = NULL;
7437 rex.reg_mmatch = rmp;
7438 rex.reg_buf = curbuf; /* always works on the current buffer! */
7439 rex.reg_firstlnum = lnum;
7440 rex.reg_maxline = curbuf->b_ml.ml_line_count - lnum;
7441 rex.reg_line_lbr = FALSE;
7442 result = vim_regsub_both(source, NULL, dest, copy, magic, backslash);
7443
7444 rex_in_use = rex_in_use_save;
7445 if (rex_in_use)
7446 rex = rex_save;
7447
7448 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449}
7450
7451 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007452vim_regsub_both(
7453 char_u *source,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007454 typval_T *expr,
Bram Moolenaar05540972016-01-30 20:31:25 +01007455 char_u *dest,
7456 int copy,
7457 int magic,
7458 int backslash)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459{
7460 char_u *src;
7461 char_u *dst;
7462 char_u *s;
7463 int c;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007464 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 int no = -1;
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007466 fptr_T func_all = (fptr_T)NULL;
7467 fptr_T func_one = (fptr_T)NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 linenr_T clnum = 0; /* init for GCC */
7469 int len = 0; /* init for GCC */
7470#ifdef FEAT_EVAL
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007471 static char_u *eval_result = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473
7474 /* Be paranoid... */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007475 if ((source == NULL && expr == NULL) || dest == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007477 emsg(_(e_null));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 return 0;
7479 }
7480 if (prog_magic_wrong())
7481 return 0;
7482 src = source;
7483 dst = dest;
7484
7485 /*
7486 * When the substitute part starts with "\=" evaluate it as an expression.
7487 */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007488 if (expr != NULL || (source[0] == '\\' && source[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 {
7490#ifdef FEAT_EVAL
7491 /* To make sure that the length doesn't change between checking the
7492 * length and copying the string, and to speed up things, the
7493 * resulting string is saved from the call with "copy" == FALSE to the
7494 * call with "copy" == TRUE. */
7495 if (copy)
7496 {
7497 if (eval_result != NULL)
7498 {
7499 STRCPY(dest, eval_result);
7500 dst += STRLEN(eval_result);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007501 VIM_CLEAR(eval_result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 }
7503 }
7504 else
7505 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007506 int prev_can_f_submatch = can_f_submatch;
7507 regsubmatch_T rsm_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508
7509 vim_free(eval_result);
7510
7511 /* The expression may contain substitute(), which calls us
7512 * recursively. Make sure submatch() gets the text from the first
Bram Moolenaar6100d022016-10-02 16:51:57 +02007513 * level. */
7514 if (can_f_submatch)
7515 rsm_save = rsm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 can_f_submatch = TRUE;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007517 rsm.sm_match = rex.reg_match;
7518 rsm.sm_mmatch = rex.reg_mmatch;
7519 rsm.sm_firstlnum = rex.reg_firstlnum;
7520 rsm.sm_maxline = rex.reg_maxline;
7521 rsm.sm_line_lbr = rex.reg_line_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007523 if (expr != NULL)
7524 {
Bram Moolenaardf48fb42016-07-22 21:50:18 +02007525 typval_T argv[2];
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007526 int dummy;
7527 char_u buf[NUMBUFLEN];
7528 typval_T rettv;
Bram Moolenaardf48fb42016-07-22 21:50:18 +02007529 staticList10_T matchList;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007530
7531 rettv.v_type = VAR_STRING;
7532 rettv.vval.v_string = NULL;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007533 argv[0].v_type = VAR_LIST;
7534 argv[0].vval.v_list = &matchList.sl_list;
7535 matchList.sl_list.lv_len = 0;
7536 if (expr->v_type == VAR_FUNC)
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007537 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007538 s = expr->vval.v_string;
7539 call_func(s, (int)STRLEN(s), &rettv,
7540 1, argv, fill_submatch_list,
7541 0L, 0L, &dummy, TRUE, NULL, NULL);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007542 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02007543 else if (expr->v_type == VAR_PARTIAL)
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007544 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007545 partial_T *partial = expr->vval.v_partial;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007546
Bram Moolenaar6100d022016-10-02 16:51:57 +02007547 s = partial_name(partial);
7548 call_func(s, (int)STRLEN(s), &rettv,
7549 1, argv, fill_submatch_list,
7550 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007551 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02007552 if (matchList.sl_list.lv_len > 0)
7553 /* fill_submatch_list() was called */
7554 clear_submatch_list(&matchList);
7555
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007556 eval_result = tv_get_string_buf_chk(&rettv, buf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007557 if (eval_result != NULL)
7558 eval_result = vim_strsave(eval_result);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02007559 clear_tv(&rettv);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007560 }
7561 else
7562 eval_result = eval_to_string(source + 2, NULL, TRUE);
7563
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564 if (eval_result != NULL)
7565 {
Bram Moolenaar06975a42010-03-23 16:27:22 +01007566 int had_backslash = FALSE;
7567
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007568 for (s = eval_result; *s != NUL; MB_PTR_ADV(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569 {
Bram Moolenaar978287b2011-06-19 04:32:15 +02007570 /* Change NL to CR, so that it becomes a line break,
7571 * unless called from vim_regexec_nl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 * Skip over a backslashed character. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007573 if (*s == NL && !rsm.sm_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 *s = CAR;
7575 else if (*s == '\\' && s[1] != NUL)
Bram Moolenaar06975a42010-03-23 16:27:22 +01007576 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577 ++s;
Bram Moolenaar60190782010-05-21 13:08:58 +02007578 /* Change NL to CR here too, so that this works:
7579 * :s/abc\\\ndef/\="aaa\\\nbbb"/ on text:
7580 * abc\
7581 * def
Bram Moolenaar978287b2011-06-19 04:32:15 +02007582 * Not when called from vim_regexec_nl().
Bram Moolenaar60190782010-05-21 13:08:58 +02007583 */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007584 if (*s == NL && !rsm.sm_line_lbr)
Bram Moolenaar60190782010-05-21 13:08:58 +02007585 *s = CAR;
Bram Moolenaar06975a42010-03-23 16:27:22 +01007586 had_backslash = TRUE;
7587 }
7588 }
7589 if (had_backslash && backslash)
7590 {
7591 /* Backslashes will be consumed, need to double them. */
7592 s = vim_strsave_escaped(eval_result, (char_u *)"\\");
7593 if (s != NULL)
7594 {
7595 vim_free(eval_result);
7596 eval_result = s;
7597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 }
7599
7600 dst += STRLEN(eval_result);
7601 }
7602
Bram Moolenaar6100d022016-10-02 16:51:57 +02007603 can_f_submatch = prev_can_f_submatch;
7604 if (can_f_submatch)
7605 rsm = rsm_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 }
7607#endif
7608 }
7609 else
7610 while ((c = *src++) != NUL)
7611 {
7612 if (c == '&' && magic)
7613 no = 0;
7614 else if (c == '\\' && *src != NUL)
7615 {
7616 if (*src == '&' && !magic)
7617 {
7618 ++src;
7619 no = 0;
7620 }
7621 else if ('0' <= *src && *src <= '9')
7622 {
7623 no = *src++ - '0';
7624 }
7625 else if (vim_strchr((char_u *)"uUlLeE", *src))
7626 {
7627 switch (*src++)
7628 {
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007629 case 'u': func_one = (fptr_T)do_upper;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 continue;
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007631 case 'U': func_all = (fptr_T)do_Upper;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 continue;
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007633 case 'l': func_one = (fptr_T)do_lower;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 continue;
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007635 case 'L': func_all = (fptr_T)do_Lower;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 continue;
7637 case 'e':
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007638 case 'E': func_one = func_all = (fptr_T)NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 continue;
7640 }
7641 }
7642 }
7643 if (no < 0) /* Ordinary character. */
7644 {
Bram Moolenaardb552d602006-03-23 22:59:57 +00007645 if (c == K_SPECIAL && src[0] != NUL && src[1] != NUL)
7646 {
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00007647 /* Copy a special key as-is. */
Bram Moolenaardb552d602006-03-23 22:59:57 +00007648 if (copy)
7649 {
7650 *dst++ = c;
7651 *dst++ = *src++;
7652 *dst++ = *src++;
7653 }
7654 else
7655 {
7656 dst += 3;
7657 src += 2;
7658 }
7659 continue;
7660 }
7661
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 if (c == '\\' && *src != NUL)
7663 {
7664 /* Check for abbreviations -- webb */
7665 switch (*src)
7666 {
7667 case 'r': c = CAR; ++src; break;
7668 case 'n': c = NL; ++src; break;
7669 case 't': c = TAB; ++src; break;
7670 /* Oh no! \e already has meaning in subst pat :-( */
7671 /* case 'e': c = ESC; ++src; break; */
7672 case 'b': c = Ctrl_H; ++src; break;
7673
7674 /* If "backslash" is TRUE the backslash will be removed
7675 * later. Used to insert a literal CR. */
7676 default: if (backslash)
7677 {
7678 if (copy)
7679 *dst = '\\';
7680 ++dst;
7681 }
7682 c = *src++;
7683 }
7684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685#ifdef FEAT_MBYTE
Bram Moolenaardb552d602006-03-23 22:59:57 +00007686 else if (has_mbyte)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007687 c = mb_ptr2char(src - 1);
7688#endif
7689
Bram Moolenaardb552d602006-03-23 22:59:57 +00007690 /* Write to buffer, if copy is set. */
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007691 if (func_one != (fptr_T)NULL)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007692 /* Turbo C complains without the typecast */
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007693 func_one = (fptr_T)(func_one(&cc, c));
7694 else if (func_all != (fptr_T)NULL)
7695 /* Turbo C complains without the typecast */
7696 func_all = (fptr_T)(func_all(&cc, c));
7697 else /* just copy */
7698 cc = c;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007699
7700#ifdef FEAT_MBYTE
7701 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 {
Bram Moolenaar0c56c602010-07-12 22:42:33 +02007703 int totlen = mb_ptr2len(src - 1);
7704
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 if (copy)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007706 mb_char2bytes(cc, dst);
7707 dst += mb_char2len(cc) - 1;
Bram Moolenaar0c56c602010-07-12 22:42:33 +02007708 if (enc_utf8)
7709 {
7710 int clen = utf_ptr2len(src - 1);
7711
7712 /* If the character length is shorter than "totlen", there
7713 * are composing characters; copy them as-is. */
7714 if (clen < totlen)
7715 {
7716 if (copy)
7717 mch_memmove(dst + 1, src - 1 + clen,
7718 (size_t)(totlen - clen));
7719 dst += totlen - clen;
7720 }
7721 }
7722 src += totlen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 }
7724 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725#endif
7726 if (copy)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007727 *dst = cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 dst++;
7729 }
7730 else
7731 {
7732 if (REG_MULTI)
7733 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007734 clnum = rex.reg_mmatch->startpos[no].lnum;
7735 if (clnum < 0 || rex.reg_mmatch->endpos[no].lnum < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 s = NULL;
7737 else
7738 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007739 s = reg_getline(clnum) + rex.reg_mmatch->startpos[no].col;
7740 if (rex.reg_mmatch->endpos[no].lnum == clnum)
7741 len = rex.reg_mmatch->endpos[no].col
7742 - rex.reg_mmatch->startpos[no].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 else
7744 len = (int)STRLEN(s);
7745 }
7746 }
7747 else
7748 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007749 s = rex.reg_match->startp[no];
7750 if (rex.reg_match->endp[no] == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 s = NULL;
7752 else
Bram Moolenaar6100d022016-10-02 16:51:57 +02007753 len = (int)(rex.reg_match->endp[no] - s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 }
7755 if (s != NULL)
7756 {
7757 for (;;)
7758 {
7759 if (len == 0)
7760 {
7761 if (REG_MULTI)
7762 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007763 if (rex.reg_mmatch->endpos[no].lnum == clnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764 break;
7765 if (copy)
7766 *dst = CAR;
7767 ++dst;
7768 s = reg_getline(++clnum);
Bram Moolenaar6100d022016-10-02 16:51:57 +02007769 if (rex.reg_mmatch->endpos[no].lnum == clnum)
7770 len = rex.reg_mmatch->endpos[no].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 else
7772 len = (int)STRLEN(s);
7773 }
7774 else
7775 break;
7776 }
7777 else if (*s == NUL) /* we hit NUL. */
7778 {
7779 if (copy)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007780 emsg(_(e_re_damg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 goto exit;
7782 }
7783 else
7784 {
7785 if (backslash && (*s == CAR || *s == '\\'))
7786 {
7787 /*
7788 * Insert a backslash in front of a CR, otherwise
7789 * it will be replaced by a line break.
7790 * Number of backslashes will be halved later,
7791 * double them here.
7792 */
7793 if (copy)
7794 {
7795 dst[0] = '\\';
7796 dst[1] = *s;
7797 }
7798 dst += 2;
7799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 else
7801 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007802#ifdef FEAT_MBYTE
7803 if (has_mbyte)
7804 c = mb_ptr2char(s);
7805 else
7806#endif
7807 c = *s;
7808
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007809 if (func_one != (fptr_T)NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 /* Turbo C complains without the typecast */
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007811 func_one = (fptr_T)(func_one(&cc, c));
7812 else if (func_all != (fptr_T)NULL)
7813 /* Turbo C complains without the typecast */
7814 func_all = (fptr_T)(func_all(&cc, c));
7815 else /* just copy */
7816 cc = c;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007817
7818#ifdef FEAT_MBYTE
7819 if (has_mbyte)
7820 {
Bram Moolenaar9225efb2007-07-30 20:32:53 +00007821 int l;
7822
7823 /* Copy composing characters separately, one
7824 * at a time. */
7825 if (enc_utf8)
7826 l = utf_ptr2len(s) - 1;
7827 else
7828 l = mb_ptr2len(s) - 1;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007829
7830 s += l;
7831 len -= l;
7832 if (copy)
7833 mb_char2bytes(cc, dst);
7834 dst += mb_char2len(cc) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007836 else
7837#endif
7838 if (copy)
7839 *dst = cc;
7840 dst++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007842
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 ++s;
7844 --len;
7845 }
7846 }
7847 }
7848 no = -1;
7849 }
7850 }
7851 if (copy)
7852 *dst = NUL;
7853
7854exit:
7855 return (int)((dst - dest) + 1);
7856}
7857
7858#ifdef FEAT_EVAL
7859/*
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007860 * Call reg_getline() with the line numbers from the submatch. If a
7861 * substitute() was used the reg_maxline and other values have been
7862 * overwritten.
7863 */
7864 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01007865reg_getline_submatch(linenr_T lnum)
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007866{
7867 char_u *s;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007868 linenr_T save_first = rex.reg_firstlnum;
7869 linenr_T save_max = rex.reg_maxline;
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007870
Bram Moolenaar6100d022016-10-02 16:51:57 +02007871 rex.reg_firstlnum = rsm.sm_firstlnum;
7872 rex.reg_maxline = rsm.sm_maxline;
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007873
7874 s = reg_getline(lnum);
7875
Bram Moolenaar6100d022016-10-02 16:51:57 +02007876 rex.reg_firstlnum = save_first;
7877 rex.reg_maxline = save_max;
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007878 return s;
7879}
7880
7881/*
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00007882 * Used for the submatch() function: get the string from the n'th submatch in
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 * allocated memory.
7884 * Returns NULL when not in a ":s" command and for a non-existing submatch.
7885 */
7886 char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01007887reg_submatch(int no)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888{
7889 char_u *retval = NULL;
7890 char_u *s;
7891 int len;
7892 int round;
7893 linenr_T lnum;
7894
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007895 if (!can_f_submatch || no < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 return NULL;
7897
Bram Moolenaar6100d022016-10-02 16:51:57 +02007898 if (rsm.sm_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 {
7900 /*
7901 * First round: compute the length and allocate memory.
7902 * Second round: copy the text.
7903 */
7904 for (round = 1; round <= 2; ++round)
7905 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007906 lnum = rsm.sm_mmatch->startpos[no].lnum;
7907 if (lnum < 0 || rsm.sm_mmatch->endpos[no].lnum < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 return NULL;
7909
Bram Moolenaar6100d022016-10-02 16:51:57 +02007910 s = reg_getline_submatch(lnum) + rsm.sm_mmatch->startpos[no].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 if (s == NULL) /* anti-crash check, cannot happen? */
7912 break;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007913 if (rsm.sm_mmatch->endpos[no].lnum == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 {
7915 /* Within one line: take form start to end col. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007916 len = rsm.sm_mmatch->endpos[no].col
7917 - rsm.sm_mmatch->startpos[no].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 if (round == 2)
Bram Moolenaarbbebc852005-07-18 21:47:53 +00007919 vim_strncpy(retval, s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 ++len;
7921 }
7922 else
7923 {
7924 /* Multiple lines: take start line from start col, middle
7925 * lines completely and end line up to end col. */
7926 len = (int)STRLEN(s);
7927 if (round == 2)
7928 {
7929 STRCPY(retval, s);
7930 retval[len] = '\n';
7931 }
7932 ++len;
7933 ++lnum;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007934 while (lnum < rsm.sm_mmatch->endpos[no].lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 {
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007936 s = reg_getline_submatch(lnum++);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 if (round == 2)
7938 STRCPY(retval + len, s);
7939 len += (int)STRLEN(s);
7940 if (round == 2)
7941 retval[len] = '\n';
7942 ++len;
7943 }
7944 if (round == 2)
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007945 STRNCPY(retval + len, reg_getline_submatch(lnum),
Bram Moolenaar6100d022016-10-02 16:51:57 +02007946 rsm.sm_mmatch->endpos[no].col);
7947 len += rsm.sm_mmatch->endpos[no].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 if (round == 2)
7949 retval[len] = NUL;
7950 ++len;
7951 }
7952
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007953 if (retval == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 {
7955 retval = lalloc((long_u)len, TRUE);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007956 if (retval == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 return NULL;
7958 }
7959 }
7960 }
7961 else
7962 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007963 s = rsm.sm_match->startp[no];
7964 if (s == NULL || rsm.sm_match->endp[no] == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 retval = NULL;
7966 else
Bram Moolenaar6100d022016-10-02 16:51:57 +02007967 retval = vim_strnsave(s, (int)(rsm.sm_match->endp[no] - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 }
7969
7970 return retval;
7971}
Bram Moolenaar41571762014-04-02 19:00:58 +02007972
7973/*
7974 * Used for the submatch() function with the optional non-zero argument: get
7975 * the list of strings from the n'th submatch in allocated memory with NULs
7976 * represented in NLs.
7977 * Returns a list of allocated strings. Returns NULL when not in a ":s"
7978 * command, for a non-existing submatch and for any error.
7979 */
7980 list_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01007981reg_submatch_list(int no)
Bram Moolenaar41571762014-04-02 19:00:58 +02007982{
7983 char_u *s;
7984 linenr_T slnum;
7985 linenr_T elnum;
7986 colnr_T scol;
7987 colnr_T ecol;
7988 int i;
7989 list_T *list;
7990 int error = FALSE;
7991
7992 if (!can_f_submatch || no < 0)
7993 return NULL;
7994
Bram Moolenaar6100d022016-10-02 16:51:57 +02007995 if (rsm.sm_match == NULL)
Bram Moolenaar41571762014-04-02 19:00:58 +02007996 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007997 slnum = rsm.sm_mmatch->startpos[no].lnum;
7998 elnum = rsm.sm_mmatch->endpos[no].lnum;
Bram Moolenaar41571762014-04-02 19:00:58 +02007999 if (slnum < 0 || elnum < 0)
8000 return NULL;
8001
Bram Moolenaar6100d022016-10-02 16:51:57 +02008002 scol = rsm.sm_mmatch->startpos[no].col;
8003 ecol = rsm.sm_mmatch->endpos[no].col;
Bram Moolenaar41571762014-04-02 19:00:58 +02008004
8005 list = list_alloc();
8006 if (list == NULL)
8007 return NULL;
8008
8009 s = reg_getline_submatch(slnum) + scol;
8010 if (slnum == elnum)
8011 {
8012 if (list_append_string(list, s, ecol - scol) == FAIL)
8013 error = TRUE;
8014 }
8015 else
8016 {
8017 if (list_append_string(list, s, -1) == FAIL)
8018 error = TRUE;
8019 for (i = 1; i < elnum - slnum; i++)
8020 {
8021 s = reg_getline_submatch(slnum + i);
8022 if (list_append_string(list, s, -1) == FAIL)
8023 error = TRUE;
8024 }
8025 s = reg_getline_submatch(elnum);
8026 if (list_append_string(list, s, ecol) == FAIL)
8027 error = TRUE;
8028 }
8029 }
8030 else
8031 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02008032 s = rsm.sm_match->startp[no];
8033 if (s == NULL || rsm.sm_match->endp[no] == NULL)
Bram Moolenaar41571762014-04-02 19:00:58 +02008034 return NULL;
8035 list = list_alloc();
8036 if (list == NULL)
8037 return NULL;
8038 if (list_append_string(list, s,
Bram Moolenaar6100d022016-10-02 16:51:57 +02008039 (int)(rsm.sm_match->endp[no] - s)) == FAIL)
Bram Moolenaar41571762014-04-02 19:00:58 +02008040 error = TRUE;
8041 }
8042
8043 if (error)
8044 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02008045 list_free(list);
Bram Moolenaar41571762014-04-02 19:00:58 +02008046 return NULL;
8047 }
8048 return list;
8049}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008051
8052static regengine_T bt_regengine =
8053{
8054 bt_regcomp,
Bram Moolenaar473de612013-06-08 18:19:48 +02008055 bt_regfree,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008056 bt_regexec_nl,
Bram Moolenaarfda37292014-11-05 14:27:36 +01008057 bt_regexec_multi,
8058 (char_u *)""
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008059};
8060
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008061#include "regexp_nfa.c"
8062
8063static regengine_T nfa_regengine =
8064{
8065 nfa_regcomp,
Bram Moolenaar473de612013-06-08 18:19:48 +02008066 nfa_regfree,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008067 nfa_regexec_nl,
Bram Moolenaarfda37292014-11-05 14:27:36 +01008068 nfa_regexec_multi,
8069 (char_u *)""
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008070};
8071
8072/* Which regexp engine to use? Needed for vim_regcomp().
8073 * Must match with 'regexpengine'. */
8074static int regexp_engine = 0;
Bram Moolenaarfda37292014-11-05 14:27:36 +01008075
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008076#ifdef DEBUG
8077static char_u regname[][30] = {
8078 "AUTOMATIC Regexp Engine",
Bram Moolenaar75eb1612013-05-29 18:45:11 +02008079 "BACKTRACKING Regexp Engine",
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008080 "NFA Regexp Engine"
8081 };
8082#endif
8083
8084/*
8085 * Compile a regular expression into internal code.
Bram Moolenaar473de612013-06-08 18:19:48 +02008086 * Returns the program in allocated memory.
8087 * Use vim_regfree() to free the memory.
8088 * Returns NULL for an error.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008089 */
8090 regprog_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01008091vim_regcomp(char_u *expr_arg, int re_flags)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008092{
8093 regprog_T *prog = NULL;
8094 char_u *expr = expr_arg;
8095
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008096 regexp_engine = p_re;
8097
8098 /* Check for prefix "\%#=", that sets the regexp engine */
8099 if (STRNCMP(expr, "\\%#=", 4) == 0)
8100 {
8101 int newengine = expr[4] - '0';
8102
8103 if (newengine == AUTOMATIC_ENGINE
8104 || newengine == BACKTRACKING_ENGINE
8105 || newengine == NFA_ENGINE)
8106 {
8107 regexp_engine = expr[4] - '0';
8108 expr += 5;
8109#ifdef DEBUG
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008110 smsg("New regexp mode selected (%d): %s",
Bram Moolenaar6e132072014-05-13 16:46:32 +02008111 regexp_engine, regname[newengine]);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008112#endif
8113 }
8114 else
8115 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008116 emsg(_("E864: \\%#= can only be followed by 0, 1, or 2. The automatic engine will be used "));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008117 regexp_engine = AUTOMATIC_ENGINE;
8118 }
8119 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02008120#ifdef DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008121 bt_regengine.expr = expr;
8122 nfa_regengine.expr = expr;
Bram Moolenaar0270f382018-07-17 05:43:58 +02008123#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008124
8125 /*
8126 * First try the NFA engine, unless backtracking was requested.
8127 */
8128 if (regexp_engine != BACKTRACKING_ENGINE)
Bram Moolenaard23a8232018-02-10 18:45:26 +01008129 prog = nfa_regengine.regcomp(expr,
Bram Moolenaare0ad3652015-01-27 12:59:55 +01008130 re_flags + (regexp_engine == AUTOMATIC_ENGINE ? RE_AUTO : 0));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008131 else
8132 prog = bt_regengine.regcomp(expr, re_flags);
8133
Bram Moolenaarfda37292014-11-05 14:27:36 +01008134 /* Check for error compiling regexp with initial engine. */
8135 if (prog == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008136 {
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02008137#ifdef BT_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008138 if (regexp_engine != BACKTRACKING_ENGINE) /* debugging log for NFA */
8139 {
8140 FILE *f;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02008141 f = fopen(BT_REGEXP_DEBUG_LOG_NAME, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008142 if (f)
8143 {
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02008144 fprintf(f, "Syntax error in \"%s\"\n", expr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008145 fclose(f);
8146 }
8147 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008148 semsg("(NFA) Could not open \"%s\" to write !!!",
Bram Moolenaard23a8232018-02-10 18:45:26 +01008149 BT_REGEXP_DEBUG_LOG_NAME);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008150 }
8151#endif
8152 /*
Bram Moolenaarfda37292014-11-05 14:27:36 +01008153 * If the NFA engine failed, try the backtracking engine.
Bram Moolenaare0ad3652015-01-27 12:59:55 +01008154 * The NFA engine also fails for patterns that it can't handle well
8155 * but are still valid patterns, thus a retry should work.
8156 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008157 if (regexp_engine == AUTOMATIC_ENGINE)
Bram Moolenaarfda37292014-11-05 14:27:36 +01008158 {
Bram Moolenaare0ad3652015-01-27 12:59:55 +01008159 regexp_engine = BACKTRACKING_ENGINE;
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02008160 prog = bt_regengine.regcomp(expr, re_flags);
Bram Moolenaarfda37292014-11-05 14:27:36 +01008161 }
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02008162 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008163
Bram Moolenaarfda37292014-11-05 14:27:36 +01008164 if (prog != NULL)
8165 {
8166 /* Store the info needed to call regcomp() again when the engine turns
8167 * out to be very slow when executing it. */
8168 prog->re_engine = regexp_engine;
8169 prog->re_flags = re_flags;
8170 }
8171
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008172 return prog;
8173}
8174
8175/*
Bram Moolenaar473de612013-06-08 18:19:48 +02008176 * Free a compiled regexp program, returned by vim_regcomp().
8177 */
8178 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008179vim_regfree(regprog_T *prog)
Bram Moolenaar473de612013-06-08 18:19:48 +02008180{
8181 if (prog != NULL)
8182 prog->engine->regfree(prog);
8183}
8184
Bram Moolenaarfda37292014-11-05 14:27:36 +01008185#ifdef FEAT_EVAL
Bram Moolenaarfda37292014-11-05 14:27:36 +01008186 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008187report_re_switch(char_u *pat)
Bram Moolenaarfda37292014-11-05 14:27:36 +01008188{
8189 if (p_verbose > 0)
8190 {
8191 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01008192 msg_puts(_("Switching to backtracking RE engine for pattern: "));
8193 msg_puts((char *)pat);
Bram Moolenaarfda37292014-11-05 14:27:36 +01008194 verbose_leave();
8195 }
8196}
8197#endif
8198
Bram Moolenaar113e1072019-01-20 15:30:40 +01008199#if (defined(FEAT_X11) && (defined(FEAT_TITLE) || defined(FEAT_XCLIPBOARD))) \
8200 || defined(PROTO)
Bram Moolenaar473de612013-06-08 18:19:48 +02008201/*
Bram Moolenaara8bfa172018-12-29 22:28:46 +01008202 * Return whether "prog" is currently being executed.
8203 */
8204 int
8205regprog_in_use(regprog_T *prog)
8206{
8207 return prog->re_in_use;
8208}
Bram Moolenaar113e1072019-01-20 15:30:40 +01008209#endif
Bram Moolenaara8bfa172018-12-29 22:28:46 +01008210
8211/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008212 * Match a regexp against a string.
8213 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
Bram Moolenaardffa5b82014-11-19 16:38:07 +01008214 * Note: "rmp->regprog" may be freed and changed.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008215 * Uses curbuf for line count and 'iskeyword'.
Bram Moolenaarfda37292014-11-05 14:27:36 +01008216 * When "nl" is TRUE consider a "\n" in "line" to be a line break.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008217 *
8218 * Return TRUE if there is a match, FALSE if not.
8219 */
Bram Moolenaarfda37292014-11-05 14:27:36 +01008220 static int
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02008221vim_regexec_string(
Bram Moolenaar05540972016-01-30 20:31:25 +01008222 regmatch_T *rmp,
8223 char_u *line, /* string to match against */
8224 colnr_T col, /* column to start looking for match */
8225 int nl)
Bram Moolenaarfda37292014-11-05 14:27:36 +01008226{
Bram Moolenaar6100d022016-10-02 16:51:57 +02008227 int result;
8228 regexec_T rex_save;
8229 int rex_in_use_save = rex_in_use;
8230
Bram Moolenaar0270f382018-07-17 05:43:58 +02008231 // Cannot use the same prog recursively, it contains state.
8232 if (rmp->regprog->re_in_use)
8233 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008234 emsg(_(e_recursive));
Bram Moolenaar0270f382018-07-17 05:43:58 +02008235 return FALSE;
8236 }
8237 rmp->regprog->re_in_use = TRUE;
8238
Bram Moolenaar6100d022016-10-02 16:51:57 +02008239 if (rex_in_use)
Bram Moolenaar0270f382018-07-17 05:43:58 +02008240 // Being called recursively, save the state.
Bram Moolenaar6100d022016-10-02 16:51:57 +02008241 rex_save = rex;
8242 rex_in_use = TRUE;
Bram Moolenaar0270f382018-07-17 05:43:58 +02008243
Bram Moolenaar6100d022016-10-02 16:51:57 +02008244 rex.reg_startp = NULL;
8245 rex.reg_endp = NULL;
8246 rex.reg_startpos = NULL;
8247 rex.reg_endpos = NULL;
8248
8249 result = rmp->regprog->engine->regexec_nl(rmp, line, col, nl);
Bram Moolenaar41499802018-07-18 06:02:09 +02008250 rmp->regprog->re_in_use = FALSE;
Bram Moolenaarfda37292014-11-05 14:27:36 +01008251
8252 /* NFA engine aborted because it's very slow. */
8253 if (rmp->regprog->re_engine == AUTOMATIC_ENGINE
8254 && result == NFA_TOO_EXPENSIVE)
8255 {
8256 int save_p_re = p_re;
8257 int re_flags = rmp->regprog->re_flags;
8258 char_u *pat = vim_strsave(((nfa_regprog_T *)rmp->regprog)->pattern);
8259
8260 p_re = BACKTRACKING_ENGINE;
8261 vim_regfree(rmp->regprog);
8262 if (pat != NULL)
8263 {
8264#ifdef FEAT_EVAL
8265 report_re_switch(pat);
8266#endif
8267 rmp->regprog = vim_regcomp(pat, re_flags);
8268 if (rmp->regprog != NULL)
Bram Moolenaar41499802018-07-18 06:02:09 +02008269 {
8270 rmp->regprog->re_in_use = TRUE;
Bram Moolenaarfda37292014-11-05 14:27:36 +01008271 result = rmp->regprog->engine->regexec_nl(rmp, line, col, nl);
Bram Moolenaar41499802018-07-18 06:02:09 +02008272 rmp->regprog->re_in_use = FALSE;
8273 }
Bram Moolenaarfda37292014-11-05 14:27:36 +01008274 vim_free(pat);
8275 }
8276
8277 p_re = save_p_re;
8278 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02008279
8280 rex_in_use = rex_in_use_save;
8281 if (rex_in_use)
8282 rex = rex_save;
8283
Bram Moolenaar66a3e792014-11-20 23:07:05 +01008284 return result > 0;
Bram Moolenaarfda37292014-11-05 14:27:36 +01008285}
8286
Bram Moolenaardffa5b82014-11-19 16:38:07 +01008287/*
8288 * Note: "*prog" may be freed and changed.
Bram Moolenaar66a3e792014-11-20 23:07:05 +01008289 * Return TRUE if there is a match, FALSE if not.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01008290 */
8291 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008292vim_regexec_prog(
8293 regprog_T **prog,
8294 int ignore_case,
8295 char_u *line,
8296 colnr_T col)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01008297{
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02008298 int r;
8299 regmatch_T regmatch;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01008300
8301 regmatch.regprog = *prog;
8302 regmatch.rm_ic = ignore_case;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02008303 r = vim_regexec_string(&regmatch, line, col, FALSE);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01008304 *prog = regmatch.regprog;
8305 return r;
8306}
8307
8308/*
8309 * Note: "rmp->regprog" may be freed and changed.
Bram Moolenaar66a3e792014-11-20 23:07:05 +01008310 * Return TRUE if there is a match, FALSE if not.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01008311 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008312 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008313vim_regexec(regmatch_T *rmp, char_u *line, colnr_T col)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008314{
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02008315 return vim_regexec_string(rmp, line, col, FALSE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008316}
8317
8318#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
8319 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
8320/*
8321 * Like vim_regexec(), but consider a "\n" in "line" to be a line break.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01008322 * Note: "rmp->regprog" may be freed and changed.
Bram Moolenaar66a3e792014-11-20 23:07:05 +01008323 * Return TRUE if there is a match, FALSE if not.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008324 */
8325 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008326vim_regexec_nl(regmatch_T *rmp, char_u *line, colnr_T col)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008327{
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02008328 return vim_regexec_string(rmp, line, col, TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008329}
8330#endif
8331
8332/*
8333 * Match a regexp against multiple lines.
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008334 * "rmp->regprog" must be a compiled regexp as returned by vim_regcomp().
8335 * Note: "rmp->regprog" may be freed and changed, even set to NULL.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008336 * Uses curbuf for line count and 'iskeyword'.
8337 *
8338 * Return zero if there is no match. Return number of lines contained in the
8339 * match otherwise.
8340 */
8341 long
Bram Moolenaar05540972016-01-30 20:31:25 +01008342vim_regexec_multi(
8343 regmmatch_T *rmp,
Bram Moolenaard23a8232018-02-10 18:45:26 +01008344 win_T *win, /* window in which to search or NULL */
8345 buf_T *buf, /* buffer in which to search */
8346 linenr_T lnum, /* nr of line to start looking for match */
8347 colnr_T col, /* column to start looking for match */
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008348 proftime_T *tm, /* timeout limit or NULL */
8349 int *timed_out) /* flag is set when timeout limit reached */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008350{
Bram Moolenaar6100d022016-10-02 16:51:57 +02008351 int result;
8352 regexec_T rex_save;
8353 int rex_in_use_save = rex_in_use;
8354
Bram Moolenaar0270f382018-07-17 05:43:58 +02008355 // Cannot use the same prog recursively, it contains state.
8356 if (rmp->regprog->re_in_use)
8357 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008358 emsg(_(e_recursive));
Bram Moolenaar0270f382018-07-17 05:43:58 +02008359 return FALSE;
8360 }
8361 rmp->regprog->re_in_use = TRUE;
8362
Bram Moolenaar6100d022016-10-02 16:51:57 +02008363 if (rex_in_use)
8364 /* Being called recursively, save the state. */
8365 rex_save = rex;
8366 rex_in_use = TRUE;
8367
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008368 result = rmp->regprog->engine->regexec_multi(
8369 rmp, win, buf, lnum, col, tm, timed_out);
Bram Moolenaar41499802018-07-18 06:02:09 +02008370 rmp->regprog->re_in_use = FALSE;
Bram Moolenaarfda37292014-11-05 14:27:36 +01008371
8372 /* NFA engine aborted because it's very slow. */
8373 if (rmp->regprog->re_engine == AUTOMATIC_ENGINE
8374 && result == NFA_TOO_EXPENSIVE)
8375 {
8376 int save_p_re = p_re;
8377 int re_flags = rmp->regprog->re_flags;
8378 char_u *pat = vim_strsave(((nfa_regprog_T *)rmp->regprog)->pattern);
8379
8380 p_re = BACKTRACKING_ENGINE;
8381 vim_regfree(rmp->regprog);
8382 if (pat != NULL)
8383 {
8384#ifdef FEAT_EVAL
8385 report_re_switch(pat);
8386#endif
Bram Moolenaar1f8c4692018-06-23 15:09:10 +02008387#ifdef FEAT_SYN_HL
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008388 // checking for \z misuse was already done when compiling for NFA,
8389 // allow all here
8390 reg_do_extmatch = REX_ALL;
Bram Moolenaar1f8c4692018-06-23 15:09:10 +02008391#endif
Bram Moolenaarfda37292014-11-05 14:27:36 +01008392 rmp->regprog = vim_regcomp(pat, re_flags);
Bram Moolenaar1f8c4692018-06-23 15:09:10 +02008393#ifdef FEAT_SYN_HL
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008394 reg_do_extmatch = 0;
Bram Moolenaar1f8c4692018-06-23 15:09:10 +02008395#endif
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008396
Bram Moolenaarfda37292014-11-05 14:27:36 +01008397 if (rmp->regprog != NULL)
Bram Moolenaar41499802018-07-18 06:02:09 +02008398 {
8399 rmp->regprog->re_in_use = TRUE;
Bram Moolenaarfda37292014-11-05 14:27:36 +01008400 result = rmp->regprog->engine->regexec_multi(
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008401 rmp, win, buf, lnum, col, tm, timed_out);
Bram Moolenaar41499802018-07-18 06:02:09 +02008402 rmp->regprog->re_in_use = FALSE;
8403 }
Bram Moolenaarfda37292014-11-05 14:27:36 +01008404 vim_free(pat);
8405 }
8406 p_re = save_p_re;
8407 }
8408
Bram Moolenaar6100d022016-10-02 16:51:57 +02008409 rex_in_use = rex_in_use_save;
8410 if (rex_in_use)
8411 rex = rex_save;
8412
Bram Moolenaar66a3e792014-11-20 23:07:05 +01008413 return result <= 0 ? 0 : result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008414}