Mercurial > vim
annotate src/regexp.c @ 10354:1406a8ee653a
Added tag v8.0.0071 for changeset 72d92c31b9f7ad28738a94d213c46963ad59c805
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Mon, 07 Nov 2016 22:45:04 +0100 |
parents | d76ccdacb41e |
children | 66f1b5bf3fa6 |
rev | line source |
---|---|
10042
4aead6a9b7a9
commit https://github.com/vim/vim/commit/edf3f97ae2af024708ebb4ac614227327033ca47
Christian Brabandt <cb@256bit.org>
parents:
9723
diff
changeset
|
1 /* vi:set ts=8 sts=4 sw=4 noet: |
7 | 2 * |
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 * | |
24 | 36 * Changes have been made by Tony Andrews, Olaf 'Rhialto' Seibert, Robert |
37 * Webb, Ciaran McCreesh and Bram Moolenaar. | |
7 | 38 * Named character class support added by Walter Briscoe (1998 Jul 01) |
39 */ | |
40 | |
4444 | 41 /* 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 | |
7 | 47 #include "vim.h" |
48 | |
4444 | 49 #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 | |
4460 | 54 # define BT_REGEXP_DEBUG_LOG |
55 # define BT_REGEXP_DEBUG_LOG_NAME "bt_regexp_debug.log" | |
4444 | 56 #endif |
7 | 57 |
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 | |
167 | 90 * 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.) | |
7 | 96 * |
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 * | |
167 | 114 * +----------------------+ |
115 * V | | |
233 | 116 * <aa>\+ BRANCH <aa> --> BRANCH --> BACK BRANCH --> NOTHING --> END |
856 | 117 * | | ^ ^ |
118 * | +-----------+ | | |
179 | 119 * +--------------------------------------------------+ |
167 | 120 * |
121 * | |
7 | 122 * +-------------------------+ |
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 * | |
1209 | 144 * They all start with a BRANCH for "\|" alternatives, even when there is only |
7 | 145 * 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 | |
639 | 245 #define RE_MARK 207 /* mark cmp Match mark position */ |
246 #define RE_VISUAL 208 /* Match Visual area */ | |
5901 | 247 #define RE_COMPOSING 209 /* any composing characters */ |
639 | 248 |
7 | 249 /* |
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 | |
258 static int | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
259 no_Magic(int x) |
7 | 260 { |
261 if (is_Magic(x)) | |
262 return un_Magic(x); | |
263 return x; | |
264 } | |
265 | |
266 static int | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
267 toggle_Magic(int x) |
7 | 268 { |
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 */ | |
653 | 337 #define EMSG_RET_NULL(m) return (EMSG(m), rc_did_emsg = TRUE, (void *)NULL) |
308 | 338 #define EMSG_RET_FAIL(m) return (EMSG(m), rc_did_emsg = TRUE, FAIL) |
4444 | 339 #define EMSG2_RET_NULL(m, c) return (EMSG2((m), (c) ? "" : "\\"), rc_did_emsg = TRUE, (void *)NULL) |
340 #define EMSG2_RET_FAIL(m, c) return (EMSG2((m), (c) ? "" : "\\"), rc_did_emsg = TRUE, FAIL) | |
341 #define EMSG_ONE_RET_NULL EMSG2_RET_NULL(_("E369: invalid item in %s%%[]"), reg_magic == MAGIC_ALL) | |
7 | 342 |
343 #define MAX_LIMIT (32767L << 16L) | |
344 | |
7805
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
345 static int re_multi_type(int); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
346 static int cstrncmp(char_u *s1, char_u *s2, int *n); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
347 static char_u *cstrchr(char_u *, int); |
7 | 348 |
4444 | 349 #ifdef BT_REGEXP_DUMP |
7805
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
350 static void regdump(char_u *, bt_regprog_T *); |
4444 | 351 #endif |
7 | 352 #ifdef DEBUG |
7805
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
353 static char_u *regprop(char_u *); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
354 #endif |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
355 |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
356 static int re_mult_next(char *what); |
6203 | 357 |
4444 | 358 static char_u e_missingbracket[] = N_("E769: Missing ] after %s["); |
359 static char_u e_unmatchedpp[] = N_("E53: Unmatched %s%%("); | |
360 static char_u e_unmatchedp[] = N_("E54: Unmatched %s("); | |
361 static char_u e_unmatchedpar[] = N_("E55: Unmatched %s)"); | |
4720
bd6bef0bd0fb
updated for version 7.3.1107
Bram Moolenaar <bram@vim.org>
parents:
4688
diff
changeset
|
362 #ifdef FEAT_SYN_HL |
4688
371cc0c44097
updated for version 7.3.1091
Bram Moolenaar <bram@vim.org>
parents:
4682
diff
changeset
|
363 static char_u e_z_not_allowed[] = N_("E66: \\z( not allowed here"); |
371cc0c44097
updated for version 7.3.1091
Bram Moolenaar <bram@vim.org>
parents:
4682
diff
changeset
|
364 static char_u e_z1_not_allowed[] = N_("E67: \\z1 et al. not allowed here"); |
4720
bd6bef0bd0fb
updated for version 7.3.1107
Bram Moolenaar <bram@vim.org>
parents:
4688
diff
changeset
|
365 #endif |
4744
a62695305e03
updated for version 7.3.1119
Bram Moolenaar <bram@vim.org>
parents:
4732
diff
changeset
|
366 static char_u e_missing_sb[] = N_("E69: Missing ] after %s%%["); |
4760
532a9855bd30
updated for version 7.3.1127
Bram Moolenaar <bram@vim.org>
parents:
4746
diff
changeset
|
367 static char_u e_empty_sb[] = N_("E70: Empty %s%%[]"); |
7 | 368 #define NOT_MULTI 0 |
369 #define MULTI_ONE 1 | |
370 #define MULTI_MULT 2 | |
371 /* | |
372 * Return NOT_MULTI if c is not a "multi" operator. | |
373 * Return MULTI_ONE if c is a single "multi" operator. | |
374 * Return MULTI_MULT if c is a multi "multi" operator. | |
375 */ | |
376 static int | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
377 re_multi_type(int c) |
7 | 378 { |
379 if (c == Magic('@') || c == Magic('=') || c == Magic('?')) | |
380 return MULTI_ONE; | |
381 if (c == Magic('*') || c == Magic('+') || c == Magic('{')) | |
382 return MULTI_MULT; | |
383 return NOT_MULTI; | |
384 } | |
385 | |
386 /* | |
387 * Flags to be passed up and down. | |
388 */ | |
389 #define HASWIDTH 0x1 /* Known never to match null string. */ | |
390 #define SIMPLE 0x2 /* Simple enough to be STAR/PLUS operand. */ | |
391 #define SPSTART 0x4 /* Starts with * or +. */ | |
392 #define HASNL 0x8 /* Contains some \n. */ | |
393 #define HASLOOKBH 0x10 /* Contains "\@<=" or "\@<!". */ | |
394 #define WORST 0 /* Worst case. */ | |
395 | |
396 /* | |
397 * When regcode is set to this value, code is not emitted and size is computed | |
398 * instead. | |
399 */ | |
400 #define JUST_CALC_SIZE ((char_u *) -1) | |
401 | |
359 | 402 static char_u *reg_prev_sub = NULL; |
403 | |
7 | 404 /* |
405 * REGEXP_INRANGE contains all characters which are always special in a [] | |
406 * range after '\'. | |
407 * REGEXP_ABBR contains all characters which act as abbreviations after '\'. | |
408 * These are: | |
409 * \n - New line (NL). | |
410 * \r - Carriage Return (CR). | |
411 * \t - Tab (TAB). | |
412 * \e - Escape (ESC). | |
413 * \b - Backspace (Ctrl_H). | |
24 | 414 * \d - Character code in decimal, eg \d123 |
415 * \o - Character code in octal, eg \o80 | |
416 * \x - Character code in hex, eg \x4a | |
417 * \u - Multibyte character code, eg \u20ac | |
418 * \U - Long multibyte character code, eg \U12345678 | |
7 | 419 */ |
420 static char_u REGEXP_INRANGE[] = "]^-n\\"; | |
24 | 421 static char_u REGEXP_ABBR[] = "nrtebdoxuU"; |
7 | 422 |
7805
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
423 static int backslash_trans(int c); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
424 static int get_char_class(char_u **pp); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
425 static int get_equi_class(char_u **pp); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
426 static void reg_equi_class(int c); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
427 static int get_coll_element(char_u **pp); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
428 static char_u *skip_anyof(char_u *p); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
429 static void init_class_tab(void); |
7 | 430 |
431 /* | |
432 * Translate '\x' to its control character, except "\n", which is Magic. | |
433 */ | |
434 static int | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
435 backslash_trans(int c) |
7 | 436 { |
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 /* | |
167 | 448 * Check for a character class name "[:name:]". "pp" points to the '['. |
7 | 449 * 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 | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
453 get_char_class(char_u **pp) |
7 | 454 { |
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 { | |
1877 | 495 for (i = 0; i < (int)(sizeof(class_names) / sizeof(*class_names)); ++i) |
7 | 496 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 /* | |
506 * Specific version of character class functions. | |
507 * Using a table to keep this fast. | |
508 */ | |
509 static 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 | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
522 init_class_tab(void) |
7 | 523 { |
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 | |
597 static char_u *regparse; /* Input-scan pointer. */ | |
598 static int prevchr_len; /* byte length of previous char */ | |
599 static int num_complex_braces; /* Complex \{...} count */ | |
600 static int regnpar; /* () count. */ | |
601 #ifdef FEAT_SYN_HL | |
602 static int regnzpar; /* \z() count. */ | |
603 static int re_has_z; /* \z item detected */ | |
604 #endif | |
605 static char_u *regcode; /* Code-emit pointer, or JUST_CALC_SIZE */ | |
606 static long regsize; /* Code size. */ | |
2010 | 607 static int reg_toolong; /* TRUE when offset out of range */ |
7 | 608 static char_u had_endbrace[NSUBEXP]; /* flags, TRUE if end of () found */ |
609 static unsigned regflags; /* RF_ flags for prog */ | |
610 static long brace_min[10]; /* Minimums for complex brace repeats */ | |
611 static long brace_max[10]; /* Maximums for complex brace repeats */ | |
612 static int brace_count[10]; /* Current counts for complex brace repeats */ | |
613 #if defined(FEAT_SYN_HL) || defined(PROTO) | |
614 static int had_eol; /* TRUE when EOL found by vim_regcomp() */ | |
615 #endif | |
616 static int one_exactly = FALSE; /* only do one char for EXACTLY */ | |
617 | |
618 static 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 | |
624 static int reg_string; /* matching with a string instead of a buffer | |
625 line */ | |
481 | 626 static int reg_strict; /* "[abc" is illegal */ |
7 | 627 |
628 /* | |
629 * META contains all characters that may be magic, except '^' and '$'. | |
630 */ | |
631 | |
632 #ifdef EBCDIC | |
633 static char_u META[] = "%&()*+.123456789<=>?@ACDFHIKLMOPSUVWX[_acdfhiklmnopsuvwxz{|~"; | |
634 #else | |
635 /* META[] is used often enough to justify turning it into a table. */ | |
636 static 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 | |
4444 | 654 static 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 */ | |
658 static int prevchr; | |
659 static int prevprevchr; /* previous-previous character */ | |
660 static int nextchr; /* used for ungetchr() */ | |
7 | 661 |
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 | |
4679
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
668 typedef struct |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
669 { |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
670 char_u *regparse; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
671 int prevchr_len; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
672 int curchr; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
673 int prevchr; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
674 int prevprevchr; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
675 int nextchr; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
676 int at_start; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
677 int prev_at_start; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
678 int regnpar; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
679 } parse_state_T; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
680 |
7 | 681 /* |
682 * Forward declarations for vim_regcomp()'s friends. | |
683 */ | |
7805
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
684 static void initchr(char_u *); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
685 static void save_parse_state(parse_state_T *ps); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
686 static void restore_parse_state(parse_state_T *ps); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
687 static int getchr(void); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
688 static void skipchr_keepstart(void); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
689 static int peekchr(void); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
690 static void skipchr(void); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
691 static void ungetchr(void); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
692 static int gethexchrs(int maxinputlen); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
693 static int getoctchrs(void); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
694 static int getdecchrs(void); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
695 static int coll_get_char(void); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
696 static void regcomp_start(char_u *expr, int flags); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
697 static char_u *reg(int, int *); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
698 static char_u *regbranch(int *flagp); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
699 static char_u *regconcat(int *flagp); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
700 static char_u *regpiece(int *); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
701 static char_u *regatom(int *); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
702 static char_u *regnode(int); |
714 | 703 #ifdef FEAT_MBYTE |
7805
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
704 static int use_multibytecode(int c); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
705 #endif |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
706 static int prog_magic_wrong(void); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
707 static char_u *regnext(char_u *); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
708 static void regc(int b); |
7 | 709 #ifdef FEAT_MBYTE |
7805
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
710 static void regmbc(int c); |
2974 | 711 # define REGMBC(x) regmbc(x); |
712 # define CASEMBC(x) case x: | |
167 | 713 #else |
714 # define regmbc(c) regc(c) | |
2974 | 715 # define REGMBC(x) |
716 # define CASEMBC(x) | |
7 | 717 #endif |
7805
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
718 static void reginsert(int, char_u *); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
719 static void reginsert_nr(int op, long val, char_u *opnd); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
720 static void reginsert_limits(int, long, long, char_u *); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
721 static char_u *re_put_long(char_u *pr, long_u val); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
722 static int read_limits(long *, long *); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
723 static void regtail(char_u *, char_u *); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
724 static void regoptail(char_u *, char_u *); |
7 | 725 |
4444 | 726 static regengine_T bt_regengine; |
727 static regengine_T nfa_regengine; | |
728 | |
7 | 729 /* |
730 * Return TRUE if compiled regular expression "prog" can match a line break. | |
731 */ | |
732 int | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
733 re_multiline(regprog_T *prog) |
7 | 734 { |
735 return (prog->regflags & RF_HASNL); | |
736 } | |
737 | |
738 /* | |
739 * Return TRUE if compiled regular expression "prog" looks before the start | |
740 * position (pattern contains "\@<=" or "\@<!"). | |
741 */ | |
742 int | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
743 re_lookbehind(regprog_T *prog) |
7 | 744 { |
745 return (prog->regflags & RF_LOOKBH); | |
746 } | |
747 | |
748 /* | |
167 | 749 * Check for an equivalence class name "[=a=]". "pp" points to the '['. |
750 * Returns a character representing the class. Zero means that no item was | |
751 * recognized. Otherwise "pp" is advanced to after the item. | |
752 */ | |
753 static int | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
754 get_equi_class(char_u **pp) |
167 | 755 { |
756 int c; | |
757 int l = 1; | |
758 char_u *p = *pp; | |
759 | |
760 if (p[1] == '=') | |
761 { | |
762 #ifdef FEAT_MBYTE | |
763 if (has_mbyte) | |
474 | 764 l = (*mb_ptr2len)(p + 2); |
167 | 765 #endif |
766 if (p[l + 2] == '=' && p[l + 3] == ']') | |
767 { | |
768 #ifdef FEAT_MBYTE | |
769 if (has_mbyte) | |
770 c = mb_ptr2char(p + 2); | |
771 else | |
772 #endif | |
773 c = p[2]; | |
774 *pp += l + 4; | |
775 return c; | |
776 } | |
777 } | |
778 return 0; | |
779 } | |
780 | |
2247
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
781 #ifdef EBCDIC |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
782 /* |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
783 * Table for equivalence class "c". (IBM-1047) |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
784 */ |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
785 char *EQUIVAL_CLASS_C[16] = { |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
786 "A\x62\x63\x64\x65\x66\x67", |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
787 "C\x68", |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
788 "E\x71\x72\x73\x74", |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
789 "I\x75\x76\x77\x78", |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
790 "N\x69", |
8825
318eaa6fa973
commit https://github.com/vim/vim/commit/22e421549d54147d003f6444de007cb1d73f1d27
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
791 "O\xEB\xEC\xED\xEE\xEF\x80", |
2247
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
792 "U\xFB\xFC\xFD\xFE", |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
793 "Y\xBA", |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
794 "a\x42\x43\x44\x45\x46\x47", |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
795 "c\x48", |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
796 "e\x51\x52\x53\x54", |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
797 "i\x55\x56\x57\x58", |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
798 "n\x49", |
8825
318eaa6fa973
commit https://github.com/vim/vim/commit/22e421549d54147d003f6444de007cb1d73f1d27
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
799 "o\xCB\xCC\xCD\xCE\xCF\x70", |
2247
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
800 "u\xDB\xDC\xDD\xDE", |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
801 "y\x8D\xDF", |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
802 }; |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
803 #endif |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
804 |
167 | 805 /* |
806 * Produce the bytes for equivalence class "c". | |
807 * Currently only handles latin1, latin9 and utf-8. | |
4444 | 808 * NOTE: When changing this function, also change nfa_emit_equi_class() |
167 | 809 */ |
810 static void | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
811 reg_equi_class(int c) |
167 | 812 { |
813 #ifdef FEAT_MBYTE | |
814 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0 | |
492 | 815 || STRCMP(p_enc, "iso-8859-15") == 0) |
167 | 816 #endif |
817 { | |
2247
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
818 #ifdef EBCDIC |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
819 int i; |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
820 |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
821 /* This might be slower than switch/case below. */ |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
822 for (i = 0; i < 16; i++) |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
823 { |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
824 if (vim_strchr(EQUIVAL_CLASS_C[i], c) != NULL) |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
825 { |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
826 char *p = EQUIVAL_CLASS_C[i]; |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
827 |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
828 while (*p != 0) |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
829 regmbc(*p++); |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
830 return; |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
831 } |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
832 } |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
833 #else |
167 | 834 switch (c) |
835 { | |
6765 | 836 /* Do not use '\300' style, it results in a negative number. */ |
837 case 'A': case 0xc0: case 0xc1: case 0xc2: | |
838 case 0xc3: case 0xc4: case 0xc5: | |
2974 | 839 CASEMBC(0x100) CASEMBC(0x102) CASEMBC(0x104) CASEMBC(0x1cd) |
840 CASEMBC(0x1de) CASEMBC(0x1e0) CASEMBC(0x1ea2) | |
6765 | 841 regmbc('A'); regmbc(0xc0); regmbc(0xc1); |
842 regmbc(0xc2); regmbc(0xc3); regmbc(0xc4); | |
843 regmbc(0xc5); | |
2974 | 844 REGMBC(0x100) REGMBC(0x102) REGMBC(0x104) |
845 REGMBC(0x1cd) REGMBC(0x1de) REGMBC(0x1e0) | |
846 REGMBC(0x1ea2) | |
847 return; | |
848 case 'B': CASEMBC(0x1e02) CASEMBC(0x1e06) | |
849 regmbc('B'); REGMBC(0x1e02) REGMBC(0x1e06) | |
167 | 850 return; |
6765 | 851 case 'C': case 0xc7: |
2974 | 852 CASEMBC(0x106) CASEMBC(0x108) CASEMBC(0x10a) CASEMBC(0x10c) |
6765 | 853 regmbc('C'); regmbc(0xc7); |
2974 | 854 REGMBC(0x106) REGMBC(0x108) REGMBC(0x10a) |
855 REGMBC(0x10c) | |
856 return; | |
857 case 'D': CASEMBC(0x10e) CASEMBC(0x110) CASEMBC(0x1e0a) | |
858 CASEMBC(0x1e0e) CASEMBC(0x1e10) | |
859 regmbc('D'); REGMBC(0x10e) REGMBC(0x110) | |
860 REGMBC(0x1e0a) REGMBC(0x1e0e) REGMBC(0x1e10) | |
167 | 861 return; |
6765 | 862 case 'E': case 0xc8: case 0xc9: case 0xca: case 0xcb: |
2974 | 863 CASEMBC(0x112) CASEMBC(0x114) CASEMBC(0x116) CASEMBC(0x118) |
864 CASEMBC(0x11a) CASEMBC(0x1eba) CASEMBC(0x1ebc) | |
6765 | 865 regmbc('E'); regmbc(0xc8); regmbc(0xc9); |
866 regmbc(0xca); regmbc(0xcb); | |
2974 | 867 REGMBC(0x112) REGMBC(0x114) REGMBC(0x116) |
868 REGMBC(0x118) REGMBC(0x11a) REGMBC(0x1eba) | |
869 REGMBC(0x1ebc) | |
870 return; | |
871 case 'F': CASEMBC(0x1e1e) | |
872 regmbc('F'); REGMBC(0x1e1e) | |
873 return; | |
874 case 'G': CASEMBC(0x11c) CASEMBC(0x11e) CASEMBC(0x120) | |
875 CASEMBC(0x122) CASEMBC(0x1e4) CASEMBC(0x1e6) CASEMBC(0x1f4) | |
876 CASEMBC(0x1e20) | |
877 regmbc('G'); REGMBC(0x11c) REGMBC(0x11e) | |
878 REGMBC(0x120) REGMBC(0x122) REGMBC(0x1e4) | |
879 REGMBC(0x1e6) REGMBC(0x1f4) REGMBC(0x1e20) | |
880 return; | |
881 case 'H': CASEMBC(0x124) CASEMBC(0x126) CASEMBC(0x1e22) | |
882 CASEMBC(0x1e26) CASEMBC(0x1e28) | |
883 regmbc('H'); REGMBC(0x124) REGMBC(0x126) | |
884 REGMBC(0x1e22) REGMBC(0x1e26) REGMBC(0x1e28) | |
167 | 885 return; |
6765 | 886 case 'I': case 0xcc: case 0xcd: case 0xce: case 0xcf: |
2974 | 887 CASEMBC(0x128) CASEMBC(0x12a) CASEMBC(0x12c) CASEMBC(0x12e) |
888 CASEMBC(0x130) CASEMBC(0x1cf) CASEMBC(0x1ec8) | |
6765 | 889 regmbc('I'); regmbc(0xcc); regmbc(0xcd); |
890 regmbc(0xce); regmbc(0xcf); | |
2974 | 891 REGMBC(0x128) REGMBC(0x12a) REGMBC(0x12c) |
892 REGMBC(0x12e) REGMBC(0x130) REGMBC(0x1cf) | |
893 REGMBC(0x1ec8) | |
894 return; | |
895 case 'J': CASEMBC(0x134) | |
896 regmbc('J'); REGMBC(0x134) | |
897 return; | |
898 case 'K': CASEMBC(0x136) CASEMBC(0x1e8) CASEMBC(0x1e30) | |
899 CASEMBC(0x1e34) | |
900 regmbc('K'); REGMBC(0x136) REGMBC(0x1e8) | |
901 REGMBC(0x1e30) REGMBC(0x1e34) | |
902 return; | |
903 case 'L': CASEMBC(0x139) CASEMBC(0x13b) CASEMBC(0x13d) | |
904 CASEMBC(0x13f) CASEMBC(0x141) CASEMBC(0x1e3a) | |
905 regmbc('L'); REGMBC(0x139) REGMBC(0x13b) | |
906 REGMBC(0x13d) REGMBC(0x13f) REGMBC(0x141) | |
907 REGMBC(0x1e3a) | |
908 return; | |
909 case 'M': CASEMBC(0x1e3e) CASEMBC(0x1e40) | |
910 regmbc('M'); REGMBC(0x1e3e) REGMBC(0x1e40) | |
167 | 911 return; |
6765 | 912 case 'N': case 0xd1: |
2974 | 913 CASEMBC(0x143) CASEMBC(0x145) CASEMBC(0x147) CASEMBC(0x1e44) |
914 CASEMBC(0x1e48) | |
6765 | 915 regmbc('N'); regmbc(0xd1); |
2974 | 916 REGMBC(0x143) REGMBC(0x145) REGMBC(0x147) |
917 REGMBC(0x1e44) REGMBC(0x1e48) | |
167 | 918 return; |
6765 | 919 case 'O': case 0xd2: case 0xd3: case 0xd4: case 0xd5: |
920 case 0xd6: case 0xd8: | |
2974 | 921 CASEMBC(0x14c) CASEMBC(0x14e) CASEMBC(0x150) CASEMBC(0x1a0) |
922 CASEMBC(0x1d1) CASEMBC(0x1ea) CASEMBC(0x1ec) CASEMBC(0x1ece) | |
6765 | 923 regmbc('O'); regmbc(0xd2); regmbc(0xd3); |
924 regmbc(0xd4); regmbc(0xd5); regmbc(0xd6); | |
925 regmbc(0xd8); | |
2974 | 926 REGMBC(0x14c) REGMBC(0x14e) REGMBC(0x150) |
927 REGMBC(0x1a0) REGMBC(0x1d1) REGMBC(0x1ea) | |
928 REGMBC(0x1ec) REGMBC(0x1ece) | |
929 return; | |
930 case 'P': case 0x1e54: case 0x1e56: | |
931 regmbc('P'); REGMBC(0x1e54) REGMBC(0x1e56) | |
932 return; | |
933 case 'R': CASEMBC(0x154) CASEMBC(0x156) CASEMBC(0x158) | |
934 CASEMBC(0x1e58) CASEMBC(0x1e5e) | |
935 regmbc('R'); REGMBC(0x154) REGMBC(0x156) REGMBC(0x158) | |
936 REGMBC(0x1e58) REGMBC(0x1e5e) | |
937 return; | |
938 case 'S': CASEMBC(0x15a) CASEMBC(0x15c) CASEMBC(0x15e) | |
939 CASEMBC(0x160) CASEMBC(0x1e60) | |
940 regmbc('S'); REGMBC(0x15a) REGMBC(0x15c) | |
941 REGMBC(0x15e) REGMBC(0x160) REGMBC(0x1e60) | |
942 return; | |
943 case 'T': CASEMBC(0x162) CASEMBC(0x164) CASEMBC(0x166) | |
944 CASEMBC(0x1e6a) CASEMBC(0x1e6e) | |
945 regmbc('T'); REGMBC(0x162) REGMBC(0x164) | |
946 REGMBC(0x166) REGMBC(0x1e6a) REGMBC(0x1e6e) | |
167 | 947 return; |
6765 | 948 case 'U': case 0xd9: case 0xda: case 0xdb: case 0xdc: |
2974 | 949 CASEMBC(0x168) CASEMBC(0x16a) CASEMBC(0x16c) CASEMBC(0x16e) |
950 CASEMBC(0x170) CASEMBC(0x172) CASEMBC(0x1af) CASEMBC(0x1d3) | |
951 CASEMBC(0x1ee6) | |
6765 | 952 regmbc('U'); regmbc(0xd9); regmbc(0xda); |
953 regmbc(0xdb); regmbc(0xdc); | |
2974 | 954 REGMBC(0x168) REGMBC(0x16a) REGMBC(0x16c) |
955 REGMBC(0x16e) REGMBC(0x170) REGMBC(0x172) | |
956 REGMBC(0x1af) REGMBC(0x1d3) REGMBC(0x1ee6) | |
957 return; | |
958 case 'V': CASEMBC(0x1e7c) | |
959 regmbc('V'); REGMBC(0x1e7c) | |
960 return; | |
961 case 'W': CASEMBC(0x174) CASEMBC(0x1e80) CASEMBC(0x1e82) | |
962 CASEMBC(0x1e84) CASEMBC(0x1e86) | |
963 regmbc('W'); REGMBC(0x174) REGMBC(0x1e80) | |
964 REGMBC(0x1e82) REGMBC(0x1e84) REGMBC(0x1e86) | |
965 return; | |
966 case 'X': CASEMBC(0x1e8a) CASEMBC(0x1e8c) | |
967 regmbc('X'); REGMBC(0x1e8a) REGMBC(0x1e8c) | |
167 | 968 return; |
6765 | 969 case 'Y': case 0xdd: |
2974 | 970 CASEMBC(0x176) CASEMBC(0x178) CASEMBC(0x1e8e) CASEMBC(0x1ef2) |
971 CASEMBC(0x1ef6) CASEMBC(0x1ef8) | |
6765 | 972 regmbc('Y'); regmbc(0xdd); |
2974 | 973 REGMBC(0x176) REGMBC(0x178) REGMBC(0x1e8e) |
974 REGMBC(0x1ef2) REGMBC(0x1ef6) REGMBC(0x1ef8) | |
975 return; | |
976 case 'Z': CASEMBC(0x179) CASEMBC(0x17b) CASEMBC(0x17d) | |
977 CASEMBC(0x1b5) CASEMBC(0x1e90) CASEMBC(0x1e94) | |
978 regmbc('Z'); REGMBC(0x179) REGMBC(0x17b) | |
979 REGMBC(0x17d) REGMBC(0x1b5) REGMBC(0x1e90) | |
980 REGMBC(0x1e94) | |
167 | 981 return; |
6765 | 982 case 'a': case 0xe0: case 0xe1: case 0xe2: |
983 case 0xe3: case 0xe4: case 0xe5: | |
2974 | 984 CASEMBC(0x101) CASEMBC(0x103) CASEMBC(0x105) CASEMBC(0x1ce) |
985 CASEMBC(0x1df) CASEMBC(0x1e1) CASEMBC(0x1ea3) | |
6765 | 986 regmbc('a'); regmbc(0xe0); regmbc(0xe1); |
987 regmbc(0xe2); regmbc(0xe3); regmbc(0xe4); | |
988 regmbc(0xe5); | |
2974 | 989 REGMBC(0x101) REGMBC(0x103) REGMBC(0x105) |
990 REGMBC(0x1ce) REGMBC(0x1df) REGMBC(0x1e1) | |
991 REGMBC(0x1ea3) | |
992 return; | |
993 case 'b': CASEMBC(0x1e03) CASEMBC(0x1e07) | |
994 regmbc('b'); REGMBC(0x1e03) REGMBC(0x1e07) | |
167 | 995 return; |
6765 | 996 case 'c': case 0xe7: |
2974 | 997 CASEMBC(0x107) CASEMBC(0x109) CASEMBC(0x10b) CASEMBC(0x10d) |
6765 | 998 regmbc('c'); regmbc(0xe7); |
2974 | 999 REGMBC(0x107) REGMBC(0x109) REGMBC(0x10b) |
1000 REGMBC(0x10d) | |
1001 return; | |
6914 | 1002 case 'd': CASEMBC(0x10f) CASEMBC(0x111) CASEMBC(0x1e0b) |
1003 CASEMBC(0x1e0f) CASEMBC(0x1e11) | |
2974 | 1004 regmbc('d'); REGMBC(0x10f) REGMBC(0x111) |
6914 | 1005 REGMBC(0x1e0b) REGMBC(0x1e0f) REGMBC(0x1e11) |
167 | 1006 return; |
6765 | 1007 case 'e': case 0xe8: case 0xe9: case 0xea: case 0xeb: |
2974 | 1008 CASEMBC(0x113) CASEMBC(0x115) CASEMBC(0x117) CASEMBC(0x119) |
1009 CASEMBC(0x11b) CASEMBC(0x1ebb) CASEMBC(0x1ebd) | |
6765 | 1010 regmbc('e'); regmbc(0xe8); regmbc(0xe9); |
1011 regmbc(0xea); regmbc(0xeb); | |
2974 | 1012 REGMBC(0x113) REGMBC(0x115) REGMBC(0x117) |
1013 REGMBC(0x119) REGMBC(0x11b) REGMBC(0x1ebb) | |
1014 REGMBC(0x1ebd) | |
1015 return; | |
1016 case 'f': CASEMBC(0x1e1f) | |
1017 regmbc('f'); REGMBC(0x1e1f) | |
1018 return; | |
1019 case 'g': CASEMBC(0x11d) CASEMBC(0x11f) CASEMBC(0x121) | |
1020 CASEMBC(0x123) CASEMBC(0x1e5) CASEMBC(0x1e7) CASEMBC(0x1f5) | |
1021 CASEMBC(0x1e21) | |
1022 regmbc('g'); REGMBC(0x11d) REGMBC(0x11f) | |
1023 REGMBC(0x121) REGMBC(0x123) REGMBC(0x1e5) | |
1024 REGMBC(0x1e7) REGMBC(0x1f5) REGMBC(0x1e21) | |
1025 return; | |
1026 case 'h': CASEMBC(0x125) CASEMBC(0x127) CASEMBC(0x1e23) | |
1027 CASEMBC(0x1e27) CASEMBC(0x1e29) CASEMBC(0x1e96) | |
1028 regmbc('h'); REGMBC(0x125) REGMBC(0x127) | |
1029 REGMBC(0x1e23) REGMBC(0x1e27) REGMBC(0x1e29) | |
1030 REGMBC(0x1e96) | |
167 | 1031 return; |
6765 | 1032 case 'i': case 0xec: case 0xed: case 0xee: case 0xef: |
2974 | 1033 CASEMBC(0x129) CASEMBC(0x12b) CASEMBC(0x12d) CASEMBC(0x12f) |
1034 CASEMBC(0x1d0) CASEMBC(0x1ec9) | |
6765 | 1035 regmbc('i'); regmbc(0xec); regmbc(0xed); |
1036 regmbc(0xee); regmbc(0xef); | |
2974 | 1037 REGMBC(0x129) REGMBC(0x12b) REGMBC(0x12d) |
1038 REGMBC(0x12f) REGMBC(0x1d0) REGMBC(0x1ec9) | |
1039 return; | |
1040 case 'j': CASEMBC(0x135) CASEMBC(0x1f0) | |
1041 regmbc('j'); REGMBC(0x135) REGMBC(0x1f0) | |
1042 return; | |
1043 case 'k': CASEMBC(0x137) CASEMBC(0x1e9) CASEMBC(0x1e31) | |
1044 CASEMBC(0x1e35) | |
1045 regmbc('k'); REGMBC(0x137) REGMBC(0x1e9) | |
1046 REGMBC(0x1e31) REGMBC(0x1e35) | |
1047 return; | |
1048 case 'l': CASEMBC(0x13a) CASEMBC(0x13c) CASEMBC(0x13e) | |
1049 CASEMBC(0x140) CASEMBC(0x142) CASEMBC(0x1e3b) | |
1050 regmbc('l'); REGMBC(0x13a) REGMBC(0x13c) | |
1051 REGMBC(0x13e) REGMBC(0x140) REGMBC(0x142) | |
1052 REGMBC(0x1e3b) | |
1053 return; | |
1054 case 'm': CASEMBC(0x1e3f) CASEMBC(0x1e41) | |
1055 regmbc('m'); REGMBC(0x1e3f) REGMBC(0x1e41) | |
167 | 1056 return; |
6765 | 1057 case 'n': case 0xf1: |
2974 | 1058 CASEMBC(0x144) CASEMBC(0x146) CASEMBC(0x148) CASEMBC(0x149) |
1059 CASEMBC(0x1e45) CASEMBC(0x1e49) | |
6765 | 1060 regmbc('n'); regmbc(0xf1); |
2974 | 1061 REGMBC(0x144) REGMBC(0x146) REGMBC(0x148) |
1062 REGMBC(0x149) REGMBC(0x1e45) REGMBC(0x1e49) | |
167 | 1063 return; |
6765 | 1064 case 'o': case 0xf2: case 0xf3: case 0xf4: case 0xf5: |
1065 case 0xf6: case 0xf8: | |
2974 | 1066 CASEMBC(0x14d) CASEMBC(0x14f) CASEMBC(0x151) CASEMBC(0x1a1) |
1067 CASEMBC(0x1d2) CASEMBC(0x1eb) CASEMBC(0x1ed) CASEMBC(0x1ecf) | |
6765 | 1068 regmbc('o'); regmbc(0xf2); regmbc(0xf3); |
1069 regmbc(0xf4); regmbc(0xf5); regmbc(0xf6); | |
1070 regmbc(0xf8); | |
2974 | 1071 REGMBC(0x14d) REGMBC(0x14f) REGMBC(0x151) |
1072 REGMBC(0x1a1) REGMBC(0x1d2) REGMBC(0x1eb) | |
1073 REGMBC(0x1ed) REGMBC(0x1ecf) | |
1074 return; | |
1075 case 'p': CASEMBC(0x1e55) CASEMBC(0x1e57) | |
1076 regmbc('p'); REGMBC(0x1e55) REGMBC(0x1e57) | |
1077 return; | |
1078 case 'r': CASEMBC(0x155) CASEMBC(0x157) CASEMBC(0x159) | |
1079 CASEMBC(0x1e59) CASEMBC(0x1e5f) | |
1080 regmbc('r'); REGMBC(0x155) REGMBC(0x157) REGMBC(0x159) | |
1081 REGMBC(0x1e59) REGMBC(0x1e5f) | |
1082 return; | |
1083 case 's': CASEMBC(0x15b) CASEMBC(0x15d) CASEMBC(0x15f) | |
1084 CASEMBC(0x161) CASEMBC(0x1e61) | |
1085 regmbc('s'); REGMBC(0x15b) REGMBC(0x15d) | |
1086 REGMBC(0x15f) REGMBC(0x161) REGMBC(0x1e61) | |
1087 return; | |
1088 case 't': CASEMBC(0x163) CASEMBC(0x165) CASEMBC(0x167) | |
1089 CASEMBC(0x1e6b) CASEMBC(0x1e6f) CASEMBC(0x1e97) | |
1090 regmbc('t'); REGMBC(0x163) REGMBC(0x165) REGMBC(0x167) | |
1091 REGMBC(0x1e6b) REGMBC(0x1e6f) REGMBC(0x1e97) | |
167 | 1092 return; |
6765 | 1093 case 'u': case 0xf9: case 0xfa: case 0xfb: case 0xfc: |
2974 | 1094 CASEMBC(0x169) CASEMBC(0x16b) CASEMBC(0x16d) CASEMBC(0x16f) |
1095 CASEMBC(0x171) CASEMBC(0x173) CASEMBC(0x1b0) CASEMBC(0x1d4) | |
1096 CASEMBC(0x1ee7) | |
6765 | 1097 regmbc('u'); regmbc(0xf9); regmbc(0xfa); |
1098 regmbc(0xfb); regmbc(0xfc); | |
2974 | 1099 REGMBC(0x169) REGMBC(0x16b) REGMBC(0x16d) |
1100 REGMBC(0x16f) REGMBC(0x171) REGMBC(0x173) | |
1101 REGMBC(0x1b0) REGMBC(0x1d4) REGMBC(0x1ee7) | |
1102 return; | |
1103 case 'v': CASEMBC(0x1e7d) | |
1104 regmbc('v'); REGMBC(0x1e7d) | |
1105 return; | |
1106 case 'w': CASEMBC(0x175) CASEMBC(0x1e81) CASEMBC(0x1e83) | |
1107 CASEMBC(0x1e85) CASEMBC(0x1e87) CASEMBC(0x1e98) | |
1108 regmbc('w'); REGMBC(0x175) REGMBC(0x1e81) | |
1109 REGMBC(0x1e83) REGMBC(0x1e85) REGMBC(0x1e87) | |
1110 REGMBC(0x1e98) | |
1111 return; | |
1112 case 'x': CASEMBC(0x1e8b) CASEMBC(0x1e8d) | |
1113 regmbc('x'); REGMBC(0x1e8b) REGMBC(0x1e8d) | |
167 | 1114 return; |
6765 | 1115 case 'y': case 0xfd: case 0xff: |
2974 | 1116 CASEMBC(0x177) CASEMBC(0x1e8f) CASEMBC(0x1e99) |
1117 CASEMBC(0x1ef3) CASEMBC(0x1ef7) CASEMBC(0x1ef9) | |
6765 | 1118 regmbc('y'); regmbc(0xfd); regmbc(0xff); |
2974 | 1119 REGMBC(0x177) REGMBC(0x1e8f) REGMBC(0x1e99) |
1120 REGMBC(0x1ef3) REGMBC(0x1ef7) REGMBC(0x1ef9) | |
1121 return; | |
1122 case 'z': CASEMBC(0x17a) CASEMBC(0x17c) CASEMBC(0x17e) | |
1123 CASEMBC(0x1b6) CASEMBC(0x1e91) CASEMBC(0x1e95) | |
1124 regmbc('z'); REGMBC(0x17a) REGMBC(0x17c) | |
1125 REGMBC(0x17e) REGMBC(0x1b6) REGMBC(0x1e91) | |
1126 REGMBC(0x1e95) | |
167 | 1127 return; |
1128 } | |
2247
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
1129 #endif |
167 | 1130 } |
1131 regmbc(c); | |
1132 } | |
1133 | |
1134 /* | |
1135 * Check for a collating element "[.a.]". "pp" points to the '['. | |
1136 * Returns a character. Zero means that no item was recognized. Otherwise | |
1137 * "pp" is advanced to after the item. | |
1138 * Currently only single characters are recognized! | |
1139 */ | |
1140 static int | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1141 get_coll_element(char_u **pp) |
167 | 1142 { |
1143 int c; | |
1144 int l = 1; | |
1145 char_u *p = *pp; | |
1146 | |
6830 | 1147 if (p[0] != NUL && p[1] == '.') |
167 | 1148 { |
1149 #ifdef FEAT_MBYTE | |
1150 if (has_mbyte) | |
474 | 1151 l = (*mb_ptr2len)(p + 2); |
167 | 1152 #endif |
1153 if (p[l + 2] == '.' && p[l + 3] == ']') | |
1154 { | |
1155 #ifdef FEAT_MBYTE | |
1156 if (has_mbyte) | |
1157 c = mb_ptr2char(p + 2); | |
1158 else | |
1159 #endif | |
1160 c = p[2]; | |
1161 *pp += l + 4; | |
1162 return c; | |
1163 } | |
1164 } | |
1165 return 0; | |
1166 } | |
1167 | |
7805
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1168 static void get_cpo_flags(void); |
4744
a62695305e03
updated for version 7.3.1119
Bram Moolenaar <bram@vim.org>
parents:
4732
diff
changeset
|
1169 static int reg_cpo_lit; /* 'cpoptions' contains 'l' flag */ |
a62695305e03
updated for version 7.3.1119
Bram Moolenaar <bram@vim.org>
parents:
4732
diff
changeset
|
1170 static int reg_cpo_bsl; /* 'cpoptions' contains '\' flag */ |
a62695305e03
updated for version 7.3.1119
Bram Moolenaar <bram@vim.org>
parents:
4732
diff
changeset
|
1171 |
a62695305e03
updated for version 7.3.1119
Bram Moolenaar <bram@vim.org>
parents:
4732
diff
changeset
|
1172 static void |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1173 get_cpo_flags(void) |
4744
a62695305e03
updated for version 7.3.1119
Bram Moolenaar <bram@vim.org>
parents:
4732
diff
changeset
|
1174 { |
a62695305e03
updated for version 7.3.1119
Bram Moolenaar <bram@vim.org>
parents:
4732
diff
changeset
|
1175 reg_cpo_lit = vim_strchr(p_cpo, CPO_LITERAL) != NULL; |
a62695305e03
updated for version 7.3.1119
Bram Moolenaar <bram@vim.org>
parents:
4732
diff
changeset
|
1176 reg_cpo_bsl = vim_strchr(p_cpo, CPO_BACKSL) != NULL; |
a62695305e03
updated for version 7.3.1119
Bram Moolenaar <bram@vim.org>
parents:
4732
diff
changeset
|
1177 } |
167 | 1178 |
1179 /* | |
1180 * Skip over a "[]" range. | |
1181 * "p" must point to the character after the '['. | |
1182 * The returned pointer is on the matching ']', or the terminating NUL. | |
1183 */ | |
1184 static char_u * | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1185 skip_anyof(char_u *p) |
167 | 1186 { |
1187 #ifdef FEAT_MBYTE | |
1188 int l; | |
1189 #endif | |
1190 | |
1191 if (*p == '^') /* Complement of range. */ | |
1192 ++p; | |
1193 if (*p == ']' || *p == '-') | |
1194 ++p; | |
1195 while (*p != NUL && *p != ']') | |
1196 { | |
1197 #ifdef FEAT_MBYTE | |
474 | 1198 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1) |
167 | 1199 p += l; |
1200 else | |
1201 #endif | |
1202 if (*p == '-') | |
1203 { | |
1204 ++p; | |
1205 if (*p != ']' && *p != NUL) | |
1206 mb_ptr_adv(p); | |
1207 } | |
1208 else if (*p == '\\' | |
4744
a62695305e03
updated for version 7.3.1119
Bram Moolenaar <bram@vim.org>
parents:
4732
diff
changeset
|
1209 && !reg_cpo_bsl |
167 | 1210 && (vim_strchr(REGEXP_INRANGE, p[1]) != NULL |
4744
a62695305e03
updated for version 7.3.1119
Bram Moolenaar <bram@vim.org>
parents:
4732
diff
changeset
|
1211 || (!reg_cpo_lit && vim_strchr(REGEXP_ABBR, p[1]) != NULL))) |
167 | 1212 p += 2; |
1213 else if (*p == '[') | |
1214 { | |
1215 if (get_char_class(&p) == CLASS_NONE | |
1216 && get_equi_class(&p) == 0 | |
6830 | 1217 && get_coll_element(&p) == 0 |
1218 && *p != NUL) | |
1219 ++p; /* it is not a class name and not NUL */ | |
167 | 1220 } |
1221 else | |
1222 ++p; | |
1223 } | |
1224 | |
1225 return p; | |
1226 } | |
1227 | |
1228 /* | |
7 | 1229 * Skip past regular expression. |
153 | 1230 * Stop at end of "startp" or where "dirc" is found ('/', '?', etc). |
7 | 1231 * Take care of characters with a backslash in front of it. |
1232 * Skip strings inside [ and ]. | |
1233 * When "newp" is not NULL and "dirc" is '?', make an allocated copy of the | |
1234 * expression and change "\?" to "?". If "*newp" is not NULL the expression | |
1235 * is changed in-place. | |
1236 */ | |
1237 char_u * | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1238 skip_regexp( |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1239 char_u *startp, |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1240 int dirc, |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1241 int magic, |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1242 char_u **newp) |
7 | 1243 { |
1244 int mymagic; | |
1245 char_u *p = startp; | |
1246 | |
1247 if (magic) | |
1248 mymagic = MAGIC_ON; | |
1249 else | |
1250 mymagic = MAGIC_OFF; | |
4744
a62695305e03
updated for version 7.3.1119
Bram Moolenaar <bram@vim.org>
parents:
4732
diff
changeset
|
1251 get_cpo_flags(); |
7 | 1252 |
39 | 1253 for (; p[0] != NUL; mb_ptr_adv(p)) |
7 | 1254 { |
1255 if (p[0] == dirc) /* found end of regexp */ | |
1256 break; | |
1257 if ((p[0] == '[' && mymagic >= MAGIC_ON) | |
1258 || (p[0] == '\\' && p[1] == '[' && mymagic <= MAGIC_OFF)) | |
1259 { | |
1260 p = skip_anyof(p + 1); | |
1261 if (p[0] == NUL) | |
1262 break; | |
1263 } | |
1264 else if (p[0] == '\\' && p[1] != NUL) | |
1265 { | |
1266 if (dirc == '?' && newp != NULL && p[1] == '?') | |
1267 { | |
1268 /* change "\?" to "?", make a copy first. */ | |
1269 if (*newp == NULL) | |
1270 { | |
1271 *newp = vim_strsave(startp); | |
1272 if (*newp != NULL) | |
1273 p = *newp + (p - startp); | |
1274 } | |
1275 if (*newp != NULL) | |
1621 | 1276 STRMOVE(p, p + 1); |
7 | 1277 else |
1278 ++p; | |
1279 } | |
1280 else | |
1281 ++p; /* skip next character */ | |
1282 if (*p == 'v') | |
1283 mymagic = MAGIC_ALL; | |
1284 else if (*p == 'V') | |
1285 mymagic = MAGIC_NONE; | |
1286 } | |
1287 } | |
1288 return p; | |
1289 } | |
1290 | |
7805
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1291 static regprog_T *bt_regcomp(char_u *expr, int re_flags); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1292 static void bt_regfree(regprog_T *prog); |
4444 | 1293 |
7 | 1294 /* |
4444 | 1295 * bt_regcomp() - compile a regular expression into internal code for the |
1296 * traditional back track matcher. | |
41 | 1297 * Returns the program in allocated space. Returns NULL for an error. |
7 | 1298 * |
1299 * We can't allocate space until we know how big the compiled form will be, | |
1300 * but we can't compile it (and thus know how big it is) until we've got a | |
1301 * place to put the code. So we cheat: we compile it twice, once with code | |
1302 * generation turned off and size counting turned on, and once "for real". | |
1303 * This also means that we don't allocate space until we are sure that the | |
1304 * thing really will compile successfully, and we never have to move the | |
1305 * code and thus invalidate pointers into it. (Note that it has to be in | |
1306 * one piece because vim_free() must be able to free it all.) | |
1307 * | |
1308 * Whether upper/lower case is to be ignored is decided when executing the | |
1309 * program, it does not matter here. | |
1310 * | |
1311 * Beware that the optimization-preparation code in here knows about some | |
1312 * of the structure of the compiled regexp. | |
1313 * "re_flags": RE_MAGIC and/or RE_STRING. | |
1314 */ | |
4444 | 1315 static regprog_T * |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1316 bt_regcomp(char_u *expr, int re_flags) |
7 | 1317 { |
4444 | 1318 bt_regprog_T *r; |
7 | 1319 char_u *scan; |
1320 char_u *longest; | |
1321 int len; | |
1322 int flags; | |
1323 | |
1324 if (expr == NULL) | |
1325 EMSG_RET_NULL(_(e_null)); | |
1326 | |
1327 init_class_tab(); | |
1328 | |
1329 /* | |
1330 * First pass: determine size, legality. | |
1331 */ | |
1332 regcomp_start(expr, re_flags); | |
1333 regcode = JUST_CALC_SIZE; | |
1334 regc(REGMAGIC); | |
1335 if (reg(REG_NOPAREN, &flags) == NULL) | |
1336 return NULL; | |
1337 | |
1338 /* Allocate space. */ | |
4444 | 1339 r = (bt_regprog_T *)lalloc(sizeof(bt_regprog_T) + regsize, TRUE); |
7 | 1340 if (r == NULL) |
1341 return NULL; | |
1342 | |
1343 /* | |
1344 * Second pass: emit code. | |
1345 */ | |
1346 regcomp_start(expr, re_flags); | |
1347 regcode = r->program; | |
1348 regc(REGMAGIC); | |
2010 | 1349 if (reg(REG_NOPAREN, &flags) == NULL || reg_toolong) |
7 | 1350 { |
1351 vim_free(r); | |
2010 | 1352 if (reg_toolong) |
1353 EMSG_RET_NULL(_("E339: Pattern too long")); | |
7 | 1354 return NULL; |
1355 } | |
1356 | |
1357 /* Dig out information for optimizations. */ | |
1358 r->regstart = NUL; /* Worst-case defaults. */ | |
1359 r->reganch = 0; | |
1360 r->regmust = NULL; | |
1361 r->regmlen = 0; | |
1362 r->regflags = regflags; | |
1363 if (flags & HASNL) | |
1364 r->regflags |= RF_HASNL; | |
1365 if (flags & HASLOOKBH) | |
1366 r->regflags |= RF_LOOKBH; | |
1367 #ifdef FEAT_SYN_HL | |
1368 /* Remember whether this pattern has any \z specials in it. */ | |
1369 r->reghasz = re_has_z; | |
1370 #endif | |
1371 scan = r->program + 1; /* First BRANCH. */ | |
1372 if (OP(regnext(scan)) == END) /* Only one top-level choice. */ | |
1373 { | |
1374 scan = OPERAND(scan); | |
1375 | |
1376 /* Starting-point info. */ | |
1377 if (OP(scan) == BOL || OP(scan) == RE_BOF) | |
1378 { | |
1379 r->reganch++; | |
1380 scan = regnext(scan); | |
1381 } | |
1382 | |
1383 if (OP(scan) == EXACTLY) | |
1384 { | |
1385 #ifdef FEAT_MBYTE | |
1386 if (has_mbyte) | |
1387 r->regstart = (*mb_ptr2char)(OPERAND(scan)); | |
1388 else | |
1389 #endif | |
1390 r->regstart = *OPERAND(scan); | |
1391 } | |
1392 else if ((OP(scan) == BOW | |
1393 || OP(scan) == EOW | |
1394 || OP(scan) == NOTHING | |
1395 || OP(scan) == MOPEN + 0 || OP(scan) == NOPEN | |
1396 || OP(scan) == MCLOSE + 0 || OP(scan) == NCLOSE) | |
1397 && OP(regnext(scan)) == EXACTLY) | |
1398 { | |
1399 #ifdef FEAT_MBYTE | |
1400 if (has_mbyte) | |
1401 r->regstart = (*mb_ptr2char)(OPERAND(regnext(scan))); | |
1402 else | |
1403 #endif | |
1404 r->regstart = *OPERAND(regnext(scan)); | |
1405 } | |
1406 | |
1407 /* | |
1408 * If there's something expensive in the r.e., find the longest | |
1409 * literal string that must appear and make it the regmust. Resolve | |
1410 * ties in favor of later strings, since the regstart check works | |
1411 * with the beginning of the r.e. and avoiding duplication | |
1412 * strengthens checking. Not a strong reason, but sufficient in the | |
1413 * absence of others. | |
1414 */ | |
1415 /* | |
1416 * When the r.e. starts with BOW, it is faster to look for a regmust | |
1417 * first. Used a lot for "#" and "*" commands. (Added by mool). | |
1418 */ | |
1419 if ((flags & SPSTART || OP(scan) == BOW || OP(scan) == EOW) | |
1420 && !(flags & HASNL)) | |
1421 { | |
1422 longest = NULL; | |
1423 len = 0; | |
1424 for (; scan != NULL; scan = regnext(scan)) | |
1425 if (OP(scan) == EXACTLY && STRLEN(OPERAND(scan)) >= (size_t)len) | |
1426 { | |
1427 longest = OPERAND(scan); | |
1428 len = (int)STRLEN(OPERAND(scan)); | |
1429 } | |
1430 r->regmust = longest; | |
1431 r->regmlen = len; | |
1432 } | |
1433 } | |
4444 | 1434 #ifdef BT_REGEXP_DUMP |
7 | 1435 regdump(expr, r); |
1436 #endif | |
4444 | 1437 r->engine = &bt_regengine; |
1438 return (regprog_T *)r; | |
7 | 1439 } |
1440 | |
1441 /* | |
4805
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4770
diff
changeset
|
1442 * Free a compiled regexp program, returned by bt_regcomp(). |
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4770
diff
changeset
|
1443 */ |
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4770
diff
changeset
|
1444 static void |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1445 bt_regfree(regprog_T *prog) |
4805
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4770
diff
changeset
|
1446 { |
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4770
diff
changeset
|
1447 vim_free(prog); |
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4770
diff
changeset
|
1448 } |
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4770
diff
changeset
|
1449 |
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4770
diff
changeset
|
1450 /* |
7 | 1451 * Setup to parse the regexp. Used once to get the length and once to do it. |
1452 */ | |
1453 static void | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1454 regcomp_start( |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1455 char_u *expr, |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1456 int re_flags) /* see vim_regcomp() */ |
7 | 1457 { |
1458 initchr(expr); | |
1459 if (re_flags & RE_MAGIC) | |
1460 reg_magic = MAGIC_ON; | |
1461 else | |
1462 reg_magic = MAGIC_OFF; | |
1463 reg_string = (re_flags & RE_STRING); | |
481 | 1464 reg_strict = (re_flags & RE_STRICT); |
4744
a62695305e03
updated for version 7.3.1119
Bram Moolenaar <bram@vim.org>
parents:
4732
diff
changeset
|
1465 get_cpo_flags(); |
7 | 1466 |
1467 num_complex_braces = 0; | |
1468 regnpar = 1; | |
1469 vim_memset(had_endbrace, 0, sizeof(had_endbrace)); | |
1470 #ifdef FEAT_SYN_HL | |
1471 regnzpar = 1; | |
1472 re_has_z = 0; | |
1473 #endif | |
1474 regsize = 0L; | |
2010 | 1475 reg_toolong = FALSE; |
7 | 1476 regflags = 0; |
1477 #if defined(FEAT_SYN_HL) || defined(PROTO) | |
1478 had_eol = FALSE; | |
1479 #endif | |
1480 } | |
1481 | |
1482 #if defined(FEAT_SYN_HL) || defined(PROTO) | |
1483 /* | |
1484 * Check if during the previous call to vim_regcomp the EOL item "$" has been | |
1485 * found. This is messy, but it works fine. | |
1486 */ | |
1487 int | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1488 vim_regcomp_had_eol(void) |
7 | 1489 { |
1490 return had_eol; | |
1491 } | |
1492 #endif | |
1493 | |
8021
b6b4f354df23
commit https://github.com/vim/vim/commit/7c29f387819b5817b003d2ba73e2b5cf3cb3d0dd
Christian Brabandt <cb@256bit.org>
parents:
7833
diff
changeset
|
1494 /* variables for parsing reginput */ |
b6b4f354df23
commit https://github.com/vim/vim/commit/7c29f387819b5817b003d2ba73e2b5cf3cb3d0dd
Christian Brabandt <cb@256bit.org>
parents:
7833
diff
changeset
|
1495 static int at_start; /* True when on the first character */ |
b6b4f354df23
commit https://github.com/vim/vim/commit/7c29f387819b5817b003d2ba73e2b5cf3cb3d0dd
Christian Brabandt <cb@256bit.org>
parents:
7833
diff
changeset
|
1496 static int prev_at_start; /* True when on the second character */ |
b6b4f354df23
commit https://github.com/vim/vim/commit/7c29f387819b5817b003d2ba73e2b5cf3cb3d0dd
Christian Brabandt <cb@256bit.org>
parents:
7833
diff
changeset
|
1497 |
7 | 1498 /* |
4444 | 1499 * Parse regular expression, i.e. main body or parenthesized thing. |
7 | 1500 * |
1501 * Caller must absorb opening parenthesis. | |
1502 * | |
1503 * Combining parenthesis handling with the base level of regular expression | |
1504 * is a trifle forced, but the need to tie the tails of the branches to what | |
1505 * follows makes it hard to avoid. | |
1506 */ | |
1507 static char_u * | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1508 reg( |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1509 int paren, /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */ |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1510 int *flagp) |
7 | 1511 { |
1512 char_u *ret; | |
1513 char_u *br; | |
1514 char_u *ender; | |
1515 int parno = 0; | |
1516 int flags; | |
1517 | |
1518 *flagp = HASWIDTH; /* Tentatively. */ | |
1519 | |
1520 #ifdef FEAT_SYN_HL | |
1521 if (paren == REG_ZPAREN) | |
1522 { | |
1523 /* Make a ZOPEN node. */ | |
1524 if (regnzpar >= NSUBEXP) | |
1525 EMSG_RET_NULL(_("E50: Too many \\z(")); | |
1526 parno = regnzpar; | |
1527 regnzpar++; | |
1528 ret = regnode(ZOPEN + parno); | |
1529 } | |
1530 else | |
1531 #endif | |
1532 if (paren == REG_PAREN) | |
1533 { | |
1534 /* Make a MOPEN node. */ | |
1535 if (regnpar >= NSUBEXP) | |
4444 | 1536 EMSG2_RET_NULL(_("E51: Too many %s("), reg_magic == MAGIC_ALL); |
7 | 1537 parno = regnpar; |
1538 ++regnpar; | |
1539 ret = regnode(MOPEN + parno); | |
1540 } | |
1541 else if (paren == REG_NPAREN) | |
1542 { | |
1543 /* Make a NOPEN node. */ | |
1544 ret = regnode(NOPEN); | |
1545 } | |
1546 else | |
1547 ret = NULL; | |
1548 | |
1549 /* Pick up the branches, linking them together. */ | |
1550 br = regbranch(&flags); | |
1551 if (br == NULL) | |
1552 return NULL; | |
1553 if (ret != NULL) | |
1554 regtail(ret, br); /* [MZ]OPEN -> first. */ | |
1555 else | |
1556 ret = br; | |
1557 /* If one of the branches can be zero-width, the whole thing can. | |
1558 * If one of the branches has * at start or matches a line-break, the | |
1559 * whole thing can. */ | |
1560 if (!(flags & HASWIDTH)) | |
1561 *flagp &= ~HASWIDTH; | |
1562 *flagp |= flags & (SPSTART | HASNL | HASLOOKBH); | |
1563 while (peekchr() == Magic('|')) | |
1564 { | |
1565 skipchr(); | |
1566 br = regbranch(&flags); | |
2010 | 1567 if (br == NULL || reg_toolong) |
7 | 1568 return NULL; |
1569 regtail(ret, br); /* BRANCH -> BRANCH. */ | |
1570 if (!(flags & HASWIDTH)) | |
1571 *flagp &= ~HASWIDTH; | |
1572 *flagp |= flags & (SPSTART | HASNL | HASLOOKBH); | |
1573 } | |
1574 | |
1575 /* Make a closing node, and hook it on the end. */ | |
1576 ender = regnode( | |
1577 #ifdef FEAT_SYN_HL | |
1578 paren == REG_ZPAREN ? ZCLOSE + parno : | |
1579 #endif | |
1580 paren == REG_PAREN ? MCLOSE + parno : | |
1581 paren == REG_NPAREN ? NCLOSE : END); | |
1582 regtail(ret, ender); | |
1583 | |
1584 /* Hook the tails of the branches to the closing node. */ | |
1585 for (br = ret; br != NULL; br = regnext(br)) | |
1586 regoptail(br, ender); | |
1587 | |
1588 /* Check for proper termination. */ | |
1589 if (paren != REG_NOPAREN && getchr() != Magic(')')) | |
1590 { | |
1591 #ifdef FEAT_SYN_HL | |
1592 if (paren == REG_ZPAREN) | |
308 | 1593 EMSG_RET_NULL(_("E52: Unmatched \\z(")); |
7 | 1594 else |
1595 #endif | |
1596 if (paren == REG_NPAREN) | |
4444 | 1597 EMSG2_RET_NULL(_(e_unmatchedpp), reg_magic == MAGIC_ALL); |
7 | 1598 else |
4444 | 1599 EMSG2_RET_NULL(_(e_unmatchedp), reg_magic == MAGIC_ALL); |
7 | 1600 } |
1601 else if (paren == REG_NOPAREN && peekchr() != NUL) | |
1602 { | |
1603 if (curchr == Magic(')')) | |
4444 | 1604 EMSG2_RET_NULL(_(e_unmatchedpar), reg_magic == MAGIC_ALL); |
7 | 1605 else |
308 | 1606 EMSG_RET_NULL(_(e_trailing)); /* "Can't happen". */ |
7 | 1607 /* NOTREACHED */ |
1608 } | |
1609 /* | |
1610 * Here we set the flag allowing back references to this set of | |
1611 * parentheses. | |
1612 */ | |
1613 if (paren == REG_PAREN) | |
1614 had_endbrace[parno] = TRUE; /* have seen the close paren */ | |
1615 return ret; | |
1616 } | |
1617 | |
1618 /* | |
4444 | 1619 * Parse one alternative of an | operator. |
7 | 1620 * Implements the & operator. |
1621 */ | |
1622 static char_u * | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1623 regbranch(int *flagp) |
7 | 1624 { |
1625 char_u *ret; | |
1626 char_u *chain = NULL; | |
1627 char_u *latest; | |
1628 int flags; | |
1629 | |
1630 *flagp = WORST | HASNL; /* Tentatively. */ | |
1631 | |
1632 ret = regnode(BRANCH); | |
1633 for (;;) | |
1634 { | |
1635 latest = regconcat(&flags); | |
1636 if (latest == NULL) | |
1637 return NULL; | |
1638 /* If one of the branches has width, the whole thing has. If one of | |
1639 * the branches anchors at start-of-line, the whole thing does. | |
1640 * If one of the branches uses look-behind, the whole thing does. */ | |
1641 *flagp |= flags & (HASWIDTH | SPSTART | HASLOOKBH); | |
1642 /* If one of the branches doesn't match a line-break, the whole thing | |
1643 * doesn't. */ | |
1644 *flagp &= ~HASNL | (flags & HASNL); | |
1645 if (chain != NULL) | |
1646 regtail(chain, latest); | |
1647 if (peekchr() != Magic('&')) | |
1648 break; | |
1649 skipchr(); | |
1650 regtail(latest, regnode(END)); /* operand ends */ | |
2010 | 1651 if (reg_toolong) |
1652 break; | |
7 | 1653 reginsert(MATCH, latest); |
1654 chain = latest; | |
1655 } | |
1656 | |
1657 return ret; | |
1658 } | |
1659 | |
1660 /* | |
4444 | 1661 * Parse one alternative of an | or & operator. |
7 | 1662 * Implements the concatenation operator. |
1663 */ | |
1664 static char_u * | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1665 regconcat(int *flagp) |
7 | 1666 { |
1667 char_u *first = NULL; | |
1668 char_u *chain = NULL; | |
1669 char_u *latest; | |
1670 int flags; | |
1671 int cont = TRUE; | |
1672 | |
1673 *flagp = WORST; /* Tentatively. */ | |
1674 | |
1675 while (cont) | |
1676 { | |
1677 switch (peekchr()) | |
1678 { | |
1679 case NUL: | |
1680 case Magic('|'): | |
1681 case Magic('&'): | |
1682 case Magic(')'): | |
1683 cont = FALSE; | |
1684 break; | |
1685 case Magic('Z'): | |
1686 #ifdef FEAT_MBYTE | |
1687 regflags |= RF_ICOMBINE; | |
1688 #endif | |
1689 skipchr_keepstart(); | |
1690 break; | |
1691 case Magic('c'): | |
1692 regflags |= RF_ICASE; | |
1693 skipchr_keepstart(); | |
1694 break; | |
1695 case Magic('C'): | |
1696 regflags |= RF_NOICASE; | |
1697 skipchr_keepstart(); | |
1698 break; | |
1699 case Magic('v'): | |
1700 reg_magic = MAGIC_ALL; | |
1701 skipchr_keepstart(); | |
1702 curchr = -1; | |
1703 break; | |
1704 case Magic('m'): | |
1705 reg_magic = MAGIC_ON; | |
1706 skipchr_keepstart(); | |
1707 curchr = -1; | |
1708 break; | |
1709 case Magic('M'): | |
1710 reg_magic = MAGIC_OFF; | |
1711 skipchr_keepstart(); | |
1712 curchr = -1; | |
1713 break; | |
1714 case Magic('V'): | |
1715 reg_magic = MAGIC_NONE; | |
1716 skipchr_keepstart(); | |
1717 curchr = -1; | |
1718 break; | |
1719 default: | |
1720 latest = regpiece(&flags); | |
2010 | 1721 if (latest == NULL || reg_toolong) |
7 | 1722 return NULL; |
1723 *flagp |= flags & (HASWIDTH | HASNL | HASLOOKBH); | |
1724 if (chain == NULL) /* First piece. */ | |
1725 *flagp |= flags & SPSTART; | |
1726 else | |
1727 regtail(chain, latest); | |
1728 chain = latest; | |
1729 if (first == NULL) | |
1730 first = latest; | |
1731 break; | |
1732 } | |
1733 } | |
1734 if (first == NULL) /* Loop ran zero times. */ | |
1735 first = regnode(NOTHING); | |
1736 return first; | |
1737 } | |
1738 | |
1739 /* | |
4444 | 1740 * Parse something followed by possible [*+=]. |
7 | 1741 * |
1742 * Note that the branching code sequences used for = and the general cases | |
1743 * of * and + are somewhat optimized: they use the same NOTHING node as | |
1744 * both the endmarker for their branch list and the body of the last branch. | |
1745 * It might seem that this node could be dispensed with entirely, but the | |
1746 * endmarker role is not redundant. | |
1747 */ | |
1748 static char_u * | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1749 regpiece(int *flagp) |
7 | 1750 { |
1751 char_u *ret; | |
1752 int op; | |
1753 char_u *next; | |
1754 int flags; | |
1755 long minval; | |
1756 long maxval; | |
1757 | |
1758 ret = regatom(&flags); | |
1759 if (ret == NULL) | |
1760 return NULL; | |
1761 | |
1762 op = peekchr(); | |
1763 if (re_multi_type(op) == NOT_MULTI) | |
1764 { | |
1765 *flagp = flags; | |
1766 return ret; | |
1767 } | |
1768 /* default flags */ | |
1769 *flagp = (WORST | SPSTART | (flags & (HASNL | HASLOOKBH))); | |
1770 | |
1771 skipchr(); | |
1772 switch (op) | |
1773 { | |
1774 case Magic('*'): | |
1775 if (flags & SIMPLE) | |
1776 reginsert(STAR, ret); | |
1777 else | |
1778 { | |
1779 /* Emit x* as (x&|), where & means "self". */ | |
1780 reginsert(BRANCH, ret); /* Either x */ | |
1781 regoptail(ret, regnode(BACK)); /* and loop */ | |
1782 regoptail(ret, ret); /* back */ | |
1783 regtail(ret, regnode(BRANCH)); /* or */ | |
1784 regtail(ret, regnode(NOTHING)); /* null. */ | |
1785 } | |
1786 break; | |
1787 | |
1788 case Magic('+'): | |
1789 if (flags & SIMPLE) | |
1790 reginsert(PLUS, ret); | |
1791 else | |
1792 { | |
1793 /* Emit x+ as x(&|), where & means "self". */ | |
1794 next = regnode(BRANCH); /* Either */ | |
1795 regtail(ret, next); | |
233 | 1796 regtail(regnode(BACK), ret); /* loop back */ |
7 | 1797 regtail(next, regnode(BRANCH)); /* or */ |
1798 regtail(ret, regnode(NOTHING)); /* null. */ | |
1799 } | |
1800 *flagp = (WORST | HASWIDTH | (flags & (HASNL | HASLOOKBH))); | |
1801 break; | |
1802 | |
1803 case Magic('@'): | |
1804 { | |
1805 int lop = END; | |
4579
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
1806 int nr; |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
1807 |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
1808 nr = getdecchrs(); |
7 | 1809 switch (no_Magic(getchr())) |
1810 { | |
1811 case '=': lop = MATCH; break; /* \@= */ | |
1812 case '!': lop = NOMATCH; break; /* \@! */ | |
1813 case '>': lop = SUBPAT; break; /* \@> */ | |
1814 case '<': switch (no_Magic(getchr())) | |
1815 { | |
1816 case '=': lop = BEHIND; break; /* \@<= */ | |
1817 case '!': lop = NOBEHIND; break; /* \@<! */ | |
1818 } | |
1819 } | |
1820 if (lop == END) | |
4444 | 1821 EMSG2_RET_NULL(_("E59: invalid character after %s@"), |
7 | 1822 reg_magic == MAGIC_ALL); |
1823 /* Look behind must match with behind_pos. */ | |
1824 if (lop == BEHIND || lop == NOBEHIND) | |
1825 { | |
1826 regtail(ret, regnode(BHPOS)); | |
1827 *flagp |= HASLOOKBH; | |
1828 } | |
1829 regtail(ret, regnode(END)); /* operand ends */ | |
4579
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
1830 if (lop == BEHIND || lop == NOBEHIND) |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
1831 { |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
1832 if (nr < 0) |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
1833 nr = 0; /* no limit is same as zero limit */ |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
1834 reginsert_nr(lop, nr, ret); |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
1835 } |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
1836 else |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
1837 reginsert(lop, ret); |
7 | 1838 break; |
1839 } | |
1840 | |
1841 case Magic('?'): | |
1842 case Magic('='): | |
1843 /* Emit x= as (x|) */ | |
1844 reginsert(BRANCH, ret); /* Either x */ | |
1845 regtail(ret, regnode(BRANCH)); /* or */ | |
1846 next = regnode(NOTHING); /* null. */ | |
1847 regtail(ret, next); | |
1848 regoptail(ret, next); | |
1849 break; | |
1850 | |
1851 case Magic('{'): | |
1852 if (!read_limits(&minval, &maxval)) | |
1853 return NULL; | |
1854 if (flags & SIMPLE) | |
1855 { | |
1856 reginsert(BRACE_SIMPLE, ret); | |
1857 reginsert_limits(BRACE_LIMITS, minval, maxval, ret); | |
1858 } | |
1859 else | |
1860 { | |
1861 if (num_complex_braces >= 10) | |
4444 | 1862 EMSG2_RET_NULL(_("E60: Too many complex %s{...}s"), |
7 | 1863 reg_magic == MAGIC_ALL); |
1864 reginsert(BRACE_COMPLEX + num_complex_braces, ret); | |
1865 regoptail(ret, regnode(BACK)); | |
1866 regoptail(ret, ret); | |
1867 reginsert_limits(BRACE_LIMITS, minval, maxval, ret); | |
1868 ++num_complex_braces; | |
1869 } | |
1870 if (minval > 0 && maxval > 0) | |
1871 *flagp = (HASWIDTH | (flags & (HASNL | HASLOOKBH))); | |
1872 break; | |
1873 } | |
1874 if (re_multi_type(peekchr()) != NOT_MULTI) | |
1875 { | |
1876 /* Can't have a multi follow a multi. */ | |
1877 if (peekchr() == Magic('*')) | |
1878 sprintf((char *)IObuff, _("E61: Nested %s*"), | |
1879 reg_magic >= MAGIC_ON ? "" : "\\"); | |
1880 else | |
1881 sprintf((char *)IObuff, _("E62: Nested %s%c"), | |
1882 reg_magic == MAGIC_ALL ? "" : "\\", no_Magic(peekchr())); | |
1883 EMSG_RET_NULL(IObuff); | |
1884 } | |
1885 | |
1886 return ret; | |
1887 } | |
1888 | |
4444 | 1889 /* When making changes to classchars also change nfa_classcodes. */ |
1890 static char_u *classchars = (char_u *)".iIkKfFpPsSdDxXoOwWhHaAlLuU"; | |
1891 static int classcodes[] = { | |
1892 ANY, IDENT, SIDENT, KWORD, SKWORD, | |
1893 FNAME, SFNAME, PRINT, SPRINT, | |
1894 WHITE, NWHITE, DIGIT, NDIGIT, | |
1895 HEX, NHEX, OCTAL, NOCTAL, | |
1896 WORD, NWORD, HEAD, NHEAD, | |
1897 ALPHA, NALPHA, LOWER, NLOWER, | |
1898 UPPER, NUPPER | |
1899 }; | |
1900 | |
7 | 1901 /* |
4444 | 1902 * Parse the lowest level. |
7 | 1903 * |
1904 * Optimization: gobbles an entire sequence of ordinary characters so that | |
1905 * it can turn them into a single node, which is smaller to store and | |
1906 * faster to run. Don't do this when one_exactly is set. | |
1907 */ | |
1908 static char_u * | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1909 regatom(int *flagp) |
7 | 1910 { |
1911 char_u *ret; | |
1912 int flags; | |
1913 int c; | |
1914 char_u *p; | |
1915 int extra = 0; | |
8021
b6b4f354df23
commit https://github.com/vim/vim/commit/7c29f387819b5817b003d2ba73e2b5cf3cb3d0dd
Christian Brabandt <cb@256bit.org>
parents:
7833
diff
changeset
|
1916 int save_prev_at_start = prev_at_start; |
7 | 1917 |
1918 *flagp = WORST; /* Tentatively. */ | |
1919 | |
1920 c = getchr(); | |
1921 switch (c) | |
1922 { | |
1923 case Magic('^'): | |
1924 ret = regnode(BOL); | |
1925 break; | |
1926 | |
1927 case Magic('$'): | |
1928 ret = regnode(EOL); | |
1929 #if defined(FEAT_SYN_HL) || defined(PROTO) | |
1930 had_eol = TRUE; | |
1931 #endif | |
1932 break; | |
1933 | |
1934 case Magic('<'): | |
1935 ret = regnode(BOW); | |
1936 break; | |
1937 | |
1938 case Magic('>'): | |
1939 ret = regnode(EOW); | |
1940 break; | |
1941 | |
1942 case Magic('_'): | |
1943 c = no_Magic(getchr()); | |
1944 if (c == '^') /* "\_^" is start-of-line */ | |
1945 { | |
1946 ret = regnode(BOL); | |
1947 break; | |
1948 } | |
1949 if (c == '$') /* "\_$" is end-of-line */ | |
1950 { | |
1951 ret = regnode(EOL); | |
1952 #if defined(FEAT_SYN_HL) || defined(PROTO) | |
1953 had_eol = TRUE; | |
1954 #endif | |
1955 break; | |
1956 } | |
1957 | |
1958 extra = ADD_NL; | |
1959 *flagp |= HASNL; | |
1960 | |
1961 /* "\_[" is character range plus newline */ | |
1962 if (c == '[') | |
1963 goto collection; | |
1964 | |
1965 /* "\_x" is character class plus newline */ | |
1966 /*FALLTHROUGH*/ | |
1967 | |
1968 /* | |
1969 * Character classes. | |
1970 */ | |
1971 case Magic('.'): | |
1972 case Magic('i'): | |
1973 case Magic('I'): | |
1974 case Magic('k'): | |
1975 case Magic('K'): | |
1976 case Magic('f'): | |
1977 case Magic('F'): | |
1978 case Magic('p'): | |
1979 case Magic('P'): | |
1980 case Magic('s'): | |
1981 case Magic('S'): | |
1982 case Magic('d'): | |
1983 case Magic('D'): | |
1984 case Magic('x'): | |
1985 case Magic('X'): | |
1986 case Magic('o'): | |
1987 case Magic('O'): | |
1988 case Magic('w'): | |
1989 case Magic('W'): | |
1990 case Magic('h'): | |
1991 case Magic('H'): | |
1992 case Magic('a'): | |
1993 case Magic('A'): | |
1994 case Magic('l'): | |
1995 case Magic('L'): | |
1996 case Magic('u'): | |
1997 case Magic('U'): | |
1998 p = vim_strchr(classchars, no_Magic(c)); | |
1999 if (p == NULL) | |
2000 EMSG_RET_NULL(_("E63: invalid use of \\_")); | |
714 | 2001 #ifdef FEAT_MBYTE |
2002 /* When '.' is followed by a composing char ignore the dot, so that | |
2003 * the composing char is matched here. */ | |
2004 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr())) | |
2005 { | |
2006 c = getchr(); | |
2007 goto do_multibyte; | |
2008 } | |
2009 #endif | |
7 | 2010 ret = regnode(classcodes[p - classchars] + extra); |
2011 *flagp |= HASWIDTH | SIMPLE; | |
2012 break; | |
2013 | |
2014 case Magic('n'): | |
2015 if (reg_string) | |
2016 { | |
2017 /* In a string "\n" matches a newline character. */ | |
2018 ret = regnode(EXACTLY); | |
2019 regc(NL); | |
2020 regc(NUL); | |
2021 *flagp |= HASWIDTH | SIMPLE; | |
2022 } | |
2023 else | |
2024 { | |
2025 /* In buffer text "\n" matches the end of a line. */ | |
2026 ret = regnode(NEWL); | |
2027 *flagp |= HASWIDTH | HASNL; | |
2028 } | |
2029 break; | |
2030 | |
2031 case Magic('('): | |
2032 if (one_exactly) | |
2033 EMSG_ONE_RET_NULL; | |
2034 ret = reg(REG_PAREN, &flags); | |
2035 if (ret == NULL) | |
2036 return NULL; | |
2037 *flagp |= flags & (HASWIDTH | SPSTART | HASNL | HASLOOKBH); | |
2038 break; | |
2039 | |
2040 case NUL: | |
2041 case Magic('|'): | |
2042 case Magic('&'): | |
2043 case Magic(')'): | |
1468 | 2044 if (one_exactly) |
2045 EMSG_ONE_RET_NULL; | |
7 | 2046 EMSG_RET_NULL(_(e_internal)); /* Supposed to be caught earlier. */ |
2047 /* NOTREACHED */ | |
2048 | |
2049 case Magic('='): | |
2050 case Magic('?'): | |
2051 case Magic('+'): | |
2052 case Magic('@'): | |
2053 case Magic('{'): | |
2054 case Magic('*'): | |
2055 c = no_Magic(c); | |
2056 sprintf((char *)IObuff, _("E64: %s%c follows nothing"), | |
2057 (c == '*' ? reg_magic >= MAGIC_ON : reg_magic == MAGIC_ALL) | |
2058 ? "" : "\\", c); | |
2059 EMSG_RET_NULL(IObuff); | |
2060 /* NOTREACHED */ | |
2061 | |
2062 case Magic('~'): /* previous substitute pattern */ | |
359 | 2063 if (reg_prev_sub != NULL) |
7 | 2064 { |
2065 char_u *lp; | |
2066 | |
2067 ret = regnode(EXACTLY); | |
2068 lp = reg_prev_sub; | |
2069 while (*lp != NUL) | |
2070 regc(*lp++); | |
2071 regc(NUL); | |
2072 if (*reg_prev_sub != NUL) | |
2073 { | |
2074 *flagp |= HASWIDTH; | |
2075 if ((lp - reg_prev_sub) == 1) | |
2076 *flagp |= SIMPLE; | |
2077 } | |
2078 } | |
2079 else | |
2080 EMSG_RET_NULL(_(e_nopresub)); | |
2081 break; | |
2082 | |
2083 case Magic('1'): | |
2084 case Magic('2'): | |
2085 case Magic('3'): | |
2086 case Magic('4'): | |
2087 case Magic('5'): | |
2088 case Magic('6'): | |
2089 case Magic('7'): | |
2090 case Magic('8'): | |
2091 case Magic('9'): | |
2092 { | |
2093 int refnum; | |
2094 | |
2095 refnum = c - Magic('0'); | |
2096 /* | |
2097 * Check if the back reference is legal. We must have seen the | |
2098 * close brace. | |
2099 * TODO: Should also check that we don't refer to something | |
2100 * that is repeated (+*=): what instance of the repetition | |
2101 * should we match? | |
2102 */ | |
2103 if (!had_endbrace[refnum]) | |
2104 { | |
2105 /* Trick: check if "@<=" or "@<!" follows, in which case | |
2106 * the \1 can appear before the referenced match. */ | |
2107 for (p = regparse; *p != NUL; ++p) | |
2108 if (p[0] == '@' && p[1] == '<' | |
2109 && (p[2] == '!' || p[2] == '=')) | |
2110 break; | |
2111 if (*p == NUL) | |
2112 EMSG_RET_NULL(_("E65: Illegal back reference")); | |
2113 } | |
2114 ret = regnode(BACKREF + refnum); | |
2115 } | |
2116 break; | |
2117 | |
2118 case Magic('z'): | |
2119 { | |
2120 c = no_Magic(getchr()); | |
2121 switch (c) | |
2122 { | |
741 | 2123 #ifdef FEAT_SYN_HL |
7 | 2124 case '(': if (reg_do_extmatch != REX_SET) |
4688
371cc0c44097
updated for version 7.3.1091
Bram Moolenaar <bram@vim.org>
parents:
4682
diff
changeset
|
2125 EMSG_RET_NULL(_(e_z_not_allowed)); |
7 | 2126 if (one_exactly) |
2127 EMSG_ONE_RET_NULL; | |
2128 ret = reg(REG_ZPAREN, &flags); | |
2129 if (ret == NULL) | |
2130 return NULL; | |
2131 *flagp |= flags & (HASWIDTH|SPSTART|HASNL|HASLOOKBH); | |
2132 re_has_z = REX_SET; | |
2133 break; | |
2134 | |
2135 case '1': | |
2136 case '2': | |
2137 case '3': | |
2138 case '4': | |
2139 case '5': | |
2140 case '6': | |
2141 case '7': | |
2142 case '8': | |
2143 case '9': if (reg_do_extmatch != REX_USE) | |
4688
371cc0c44097
updated for version 7.3.1091
Bram Moolenaar <bram@vim.org>
parents:
4682
diff
changeset
|
2144 EMSG_RET_NULL(_(e_z1_not_allowed)); |
7 | 2145 ret = regnode(ZREF + c - '0'); |
2146 re_has_z = REX_USE; | |
2147 break; | |
741 | 2148 #endif |
7 | 2149 |
2150 case 's': ret = regnode(MOPEN + 0); | |
6203 | 2151 if (re_mult_next("\\zs") == FAIL) |
2152 return NULL; | |
7 | 2153 break; |
2154 | |
2155 case 'e': ret = regnode(MCLOSE + 0); | |
6203 | 2156 if (re_mult_next("\\ze") == FAIL) |
2157 return NULL; | |
7 | 2158 break; |
2159 | |
2160 default: EMSG_RET_NULL(_("E68: Invalid character after \\z")); | |
2161 } | |
2162 } | |
2163 break; | |
2164 | |
2165 case Magic('%'): | |
2166 { | |
2167 c = no_Magic(getchr()); | |
2168 switch (c) | |
2169 { | |
2170 /* () without a back reference */ | |
2171 case '(': | |
2172 if (one_exactly) | |
2173 EMSG_ONE_RET_NULL; | |
2174 ret = reg(REG_NPAREN, &flags); | |
2175 if (ret == NULL) | |
2176 return NULL; | |
2177 *flagp |= flags & (HASWIDTH | SPSTART | HASNL | HASLOOKBH); | |
2178 break; | |
2179 | |
2180 /* Catch \%^ and \%$ regardless of where they appear in the | |
2181 * pattern -- regardless of whether or not it makes sense. */ | |
2182 case '^': | |
2183 ret = regnode(RE_BOF); | |
2184 break; | |
2185 | |
2186 case '$': | |
2187 ret = regnode(RE_EOF); | |
2188 break; | |
2189 | |
2190 case '#': | |
2191 ret = regnode(CURSOR); | |
2192 break; | |
2193 | |
639 | 2194 case 'V': |
2195 ret = regnode(RE_VISUAL); | |
2196 break; | |
2197 | |
5901 | 2198 case 'C': |
2199 ret = regnode(RE_COMPOSING); | |
2200 break; | |
2201 | |
7 | 2202 /* \%[abc]: Emit as a list of branches, all ending at the last |
2203 * branch which matches nothing. */ | |
2204 case '[': | |
2205 if (one_exactly) /* doesn't nest */ | |
2206 EMSG_ONE_RET_NULL; | |
2207 { | |
2208 char_u *lastbranch; | |
2209 char_u *lastnode = NULL; | |
2210 char_u *br; | |
2211 | |
2212 ret = NULL; | |
2213 while ((c = getchr()) != ']') | |
2214 { | |
2215 if (c == NUL) | |
4744
a62695305e03
updated for version 7.3.1119
Bram Moolenaar <bram@vim.org>
parents:
4732
diff
changeset
|
2216 EMSG2_RET_NULL(_(e_missing_sb), |
7 | 2217 reg_magic == MAGIC_ALL); |
2218 br = regnode(BRANCH); | |
2219 if (ret == NULL) | |
2220 ret = br; | |
2221 else | |
2222 regtail(lastnode, br); | |
2223 | |
2224 ungetchr(); | |
2225 one_exactly = TRUE; | |
2226 lastnode = regatom(flagp); | |
2227 one_exactly = FALSE; | |
2228 if (lastnode == NULL) | |
2229 return NULL; | |
2230 } | |
2231 if (ret == NULL) | |
4760
532a9855bd30
updated for version 7.3.1127
Bram Moolenaar <bram@vim.org>
parents:
4746
diff
changeset
|
2232 EMSG2_RET_NULL(_(e_empty_sb), |
7 | 2233 reg_magic == MAGIC_ALL); |
2234 lastbranch = regnode(BRANCH); | |
2235 br = regnode(NOTHING); | |
2236 if (ret != JUST_CALC_SIZE) | |
2237 { | |
2238 regtail(lastnode, br); | |
2239 regtail(lastbranch, br); | |
2240 /* connect all branches to the NOTHING | |
2241 * branch at the end */ | |
2242 for (br = ret; br != lastnode; ) | |
2243 { | |
2244 if (OP(br) == BRANCH) | |
2245 { | |
2246 regtail(br, lastbranch); | |
2247 br = OPERAND(br); | |
2248 } | |
2249 else | |
2250 br = regnext(br); | |
2251 } | |
2252 } | |
1701 | 2253 *flagp &= ~(HASWIDTH | SIMPLE); |
7 | 2254 break; |
2255 } | |
2256 | |
24 | 2257 case 'd': /* %d123 decimal */ |
2258 case 'o': /* %o123 octal */ | |
2259 case 'x': /* %xab hex 2 */ | |
2260 case 'u': /* %uabcd hex 4 */ | |
2261 case 'U': /* %U1234abcd hex 8 */ | |
2262 { | |
2263 int i; | |
2264 | |
2265 switch (c) | |
2266 { | |
2267 case 'd': i = getdecchrs(); break; | |
2268 case 'o': i = getoctchrs(); break; | |
2269 case 'x': i = gethexchrs(2); break; | |
2270 case 'u': i = gethexchrs(4); break; | |
2271 case 'U': i = gethexchrs(8); break; | |
2272 default: i = -1; break; | |
2273 } | |
2274 | |
2275 if (i < 0) | |
4444 | 2276 EMSG2_RET_NULL( |
24 | 2277 _("E678: Invalid character after %s%%[dxouU]"), |
2278 reg_magic == MAGIC_ALL); | |
714 | 2279 #ifdef FEAT_MBYTE |
2280 if (use_multibytecode(i)) | |
2281 ret = regnode(MULTIBYTECODE); | |
2282 else | |
2283 #endif | |
2284 ret = regnode(EXACTLY); | |
24 | 2285 if (i == 0) |
2286 regc(0x0a); | |
2287 else | |
2288 #ifdef FEAT_MBYTE | |
2289 regmbc(i); | |
2290 #else | |
2291 regc(i); | |
2292 #endif | |
2293 regc(NUL); | |
2294 *flagp |= HASWIDTH; | |
2295 break; | |
2296 } | |
2297 | |
7 | 2298 default: |
639 | 2299 if (VIM_ISDIGIT(c) || c == '<' || c == '>' |
2300 || c == '\'') | |
7 | 2301 { |
2302 long_u n = 0; | |
2303 int cmp; | |
2304 | |
2305 cmp = c; | |
2306 if (cmp == '<' || cmp == '>') | |
2307 c = getchr(); | |
2308 while (VIM_ISDIGIT(c)) | |
2309 { | |
2310 n = n * 10 + (c - '0'); | |
2311 c = getchr(); | |
2312 } | |
639 | 2313 if (c == '\'' && n == 0) |
2314 { | |
2315 /* "\%'m", "\%<'m" and "\%>'m": Mark */ | |
2316 c = getchr(); | |
2317 ret = regnode(RE_MARK); | |
2318 if (ret == JUST_CALC_SIZE) | |
2319 regsize += 2; | |
2320 else | |
2321 { | |
2322 *regcode++ = c; | |
2323 *regcode++ = cmp; | |
2324 } | |
2325 break; | |
2326 } | |
2327 else if (c == 'l' || c == 'c' || c == 'v') | |
7 | 2328 { |
2329 if (c == 'l') | |
8021
b6b4f354df23
commit https://github.com/vim/vim/commit/7c29f387819b5817b003d2ba73e2b5cf3cb3d0dd
Christian Brabandt <cb@256bit.org>
parents:
7833
diff
changeset
|
2330 { |
7 | 2331 ret = regnode(RE_LNUM); |
8021
b6b4f354df23
commit https://github.com/vim/vim/commit/7c29f387819b5817b003d2ba73e2b5cf3cb3d0dd
Christian Brabandt <cb@256bit.org>
parents:
7833
diff
changeset
|
2332 if (save_prev_at_start) |
b6b4f354df23
commit https://github.com/vim/vim/commit/7c29f387819b5817b003d2ba73e2b5cf3cb3d0dd
Christian Brabandt <cb@256bit.org>
parents:
7833
diff
changeset
|
2333 at_start = TRUE; |
b6b4f354df23
commit https://github.com/vim/vim/commit/7c29f387819b5817b003d2ba73e2b5cf3cb3d0dd
Christian Brabandt <cb@256bit.org>
parents:
7833
diff
changeset
|
2334 } |
7 | 2335 else if (c == 'c') |
2336 ret = regnode(RE_COL); | |
2337 else | |
2338 ret = regnode(RE_VCOL); | |
2339 if (ret == JUST_CALC_SIZE) | |
2340 regsize += 5; | |
2341 else | |
2342 { | |
2343 /* put the number and the optional | |
2344 * comparator after the opcode */ | |
2345 regcode = re_put_long(regcode, n); | |
2346 *regcode++ = cmp; | |
2347 } | |
2348 break; | |
2349 } | |
2350 } | |
2351 | |
4444 | 2352 EMSG2_RET_NULL(_("E71: Invalid character after %s%%"), |
7 | 2353 reg_magic == MAGIC_ALL); |
2354 } | |
2355 } | |
2356 break; | |
2357 | |
2358 case Magic('['): | |
2359 collection: | |
2360 { | |
2361 char_u *lp; | |
2362 | |
2363 /* | |
2364 * If there is no matching ']', we assume the '[' is a normal | |
2365 * character. This makes 'incsearch' and ":help [" work. | |
2366 */ | |
2367 lp = skip_anyof(regparse); | |
2368 if (*lp == ']') /* there is a matching ']' */ | |
2369 { | |
2370 int startc = -1; /* > 0 when next '-' is a range */ | |
2371 int endc; | |
2372 | |
2373 /* | |
2374 * In a character class, different parsing rules apply. | |
2375 * Not even \ is special anymore, nothing is. | |
2376 */ | |
2377 if (*regparse == '^') /* Complement of range. */ | |
2378 { | |
2379 ret = regnode(ANYBUT + extra); | |
2380 regparse++; | |
2381 } | |
2382 else | |
2383 ret = regnode(ANYOF + extra); | |
2384 | |
2385 /* At the start ']' and '-' mean the literal character. */ | |
2386 if (*regparse == ']' || *regparse == '-') | |
167 | 2387 { |
2388 startc = *regparse; | |
7 | 2389 regc(*regparse++); |
167 | 2390 } |
7 | 2391 |
2392 while (*regparse != NUL && *regparse != ']') | |
2393 { | |
2394 if (*regparse == '-') | |
2395 { | |
2396 ++regparse; | |
2397 /* The '-' is not used for a range at the end and | |
2398 * after or before a '\n'. */ | |
2399 if (*regparse == ']' || *regparse == NUL | |
2400 || startc == -1 | |
2401 || (regparse[0] == '\\' && regparse[1] == 'n')) | |
2402 { | |
2403 regc('-'); | |
2404 startc = '-'; /* [--x] is a range */ | |
2405 } | |
2406 else | |
2407 { | |
167 | 2408 /* Also accept "a-[.z.]" */ |
2409 endc = 0; | |
2410 if (*regparse == '[') | |
2411 endc = get_coll_element(®parse); | |
2412 if (endc == 0) | |
2413 { | |
7 | 2414 #ifdef FEAT_MBYTE |
167 | 2415 if (has_mbyte) |
2416 endc = mb_ptr2char_adv(®parse); | |
2417 else | |
7 | 2418 #endif |
167 | 2419 endc = *regparse++; |
2420 } | |
24 | 2421 |
2422 /* Handle \o40, \x20 and \u20AC style sequences */ | |
4744
a62695305e03
updated for version 7.3.1119
Bram Moolenaar <bram@vim.org>
parents:
4732
diff
changeset
|
2423 if (endc == '\\' && !reg_cpo_lit && !reg_cpo_bsl) |
24 | 2424 endc = coll_get_char(); |
2425 | |
7 | 2426 if (startc > endc) |
2427 EMSG_RET_NULL(_(e_invrange)); | |
2428 #ifdef FEAT_MBYTE | |
2429 if (has_mbyte && ((*mb_char2len)(startc) > 1 | |
2430 || (*mb_char2len)(endc) > 1)) | |
2431 { | |
2432 /* Limit to a range of 256 chars */ | |
2433 if (endc > startc + 256) | |
2434 EMSG_RET_NULL(_(e_invrange)); | |
2435 while (++startc <= endc) | |
2436 regmbc(startc); | |
2437 } | |
2438 else | |
2439 #endif | |
2440 { | |
2441 #ifdef EBCDIC | |
2442 int alpha_only = FALSE; | |
2443 | |
2444 /* for alphabetical range skip the gaps | |
2445 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */ | |
2446 if (isalpha(startc) && isalpha(endc)) | |
2447 alpha_only = TRUE; | |
2448 #endif | |
2449 while (++startc <= endc) | |
2450 #ifdef EBCDIC | |
2451 if (!alpha_only || isalpha(startc)) | |
2452 #endif | |
2453 regc(startc); | |
2454 } | |
2455 startc = -1; | |
2456 } | |
2457 } | |
2458 /* | |
2459 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim | |
2460 * accepts "\t", "\e", etc., but only when the 'l' flag in | |
2461 * 'cpoptions' is not included. | |
167 | 2462 * Posix doesn't recognize backslash at all. |
7 | 2463 */ |
2464 else if (*regparse == '\\' | |
4744
a62695305e03
updated for version 7.3.1119
Bram Moolenaar <bram@vim.org>
parents:
4732
diff
changeset
|
2465 && !reg_cpo_bsl |
7 | 2466 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL |
4744
a62695305e03
updated for version 7.3.1119
Bram Moolenaar <bram@vim.org>
parents:
4732
diff
changeset
|
2467 || (!reg_cpo_lit |
7 | 2468 && vim_strchr(REGEXP_ABBR, |
2469 regparse[1]) != NULL))) | |
2470 { | |
2471 regparse++; | |
2472 if (*regparse == 'n') | |
2473 { | |
2474 /* '\n' in range: also match NL */ | |
2475 if (ret != JUST_CALC_SIZE) | |
2476 { | |
4084 | 2477 /* Using \n inside [^] does not change what |
2478 * matches. "[^\n]" is the same as ".". */ | |
2479 if (*ret == ANYOF) | |
2480 { | |
7 | 2481 *ret = ANYOF + ADD_NL; |
4084 | 2482 *flagp |= HASNL; |
2483 } | |
7 | 2484 /* else: must have had a \n already */ |
2485 } | |
2486 regparse++; | |
2487 startc = -1; | |
2488 } | |
24 | 2489 else if (*regparse == 'd' |
2490 || *regparse == 'o' | |
2491 || *regparse == 'x' | |
2492 || *regparse == 'u' | |
2493 || *regparse == 'U') | |
2494 { | |
2495 startc = coll_get_char(); | |
2496 if (startc == 0) | |
2497 regc(0x0a); | |
2498 else | |
2499 #ifdef FEAT_MBYTE | |
2500 regmbc(startc); | |
2501 #else | |
2502 regc(startc); | |
2503 #endif | |
2504 } | |
7 | 2505 else |
2506 { | |
2507 startc = backslash_trans(*regparse++); | |
2508 regc(startc); | |
2509 } | |
2510 } | |
2511 else if (*regparse == '[') | |
2512 { | |
2513 int c_class; | |
2514 int cu; | |
2515 | |
167 | 2516 c_class = get_char_class(®parse); |
7 | 2517 startc = -1; |
2518 /* Characters assumed to be 8 bits! */ | |
2519 switch (c_class) | |
2520 { | |
2521 case CLASS_NONE: | |
167 | 2522 c_class = get_equi_class(®parse); |
2523 if (c_class != 0) | |
2524 { | |
2525 /* produce equivalence class */ | |
2526 reg_equi_class(c_class); | |
2527 } | |
2528 else if ((c_class = | |
2529 get_coll_element(®parse)) != 0) | |
2530 { | |
2531 /* produce a collating element */ | |
2532 regmbc(c_class); | |
2533 } | |
2534 else | |
2535 { | |
2536 /* literal '[', allow [[-x] as a range */ | |
2537 startc = *regparse++; | |
2538 regc(startc); | |
2539 } | |
7 | 2540 break; |
2541 case CLASS_ALNUM: | |
9015
42b228c8701b
commit https://github.com/vim/vim/commit/e8aee7dcf9b12becff86e8ce1783a86801c5f9f6
Christian Brabandt <cb@256bit.org>
parents:
8995
diff
changeset
|
2542 for (cu = 1; cu < 128; cu++) |
7 | 2543 if (isalnum(cu)) |
8995
3cf6704d6efc
commit https://github.com/vim/vim/commit/af98a49dd0ef1661b4998f118151fddbf6e4df75
Christian Brabandt <cb@256bit.org>
parents:
8863
diff
changeset
|
2544 regmbc(cu); |
7 | 2545 break; |
2546 case CLASS_ALPHA: | |
9015
42b228c8701b
commit https://github.com/vim/vim/commit/e8aee7dcf9b12becff86e8ce1783a86801c5f9f6
Christian Brabandt <cb@256bit.org>
parents:
8995
diff
changeset
|
2547 for (cu = 1; cu < 128; cu++) |
7 | 2548 if (isalpha(cu)) |
8995
3cf6704d6efc
commit https://github.com/vim/vim/commit/af98a49dd0ef1661b4998f118151fddbf6e4df75
Christian Brabandt <cb@256bit.org>
parents:
8863
diff
changeset
|
2549 regmbc(cu); |
7 | 2550 break; |
2551 case CLASS_BLANK: | |
2552 regc(' '); | |
2553 regc('\t'); | |
2554 break; | |
2555 case CLASS_CNTRL: | |
2556 for (cu = 1; cu <= 255; cu++) | |
2557 if (iscntrl(cu)) | |
8995
3cf6704d6efc
commit https://github.com/vim/vim/commit/af98a49dd0ef1661b4998f118151fddbf6e4df75
Christian Brabandt <cb@256bit.org>
parents:
8863
diff
changeset
|
2558 regmbc(cu); |
7 | 2559 break; |
2560 case CLASS_DIGIT: | |
2561 for (cu = 1; cu <= 255; cu++) | |
2562 if (VIM_ISDIGIT(cu)) | |
8995
3cf6704d6efc
commit https://github.com/vim/vim/commit/af98a49dd0ef1661b4998f118151fddbf6e4df75
Christian Brabandt <cb@256bit.org>
parents:
8863
diff
changeset
|
2563 regmbc(cu); |
7 | 2564 break; |
2565 case CLASS_GRAPH: | |
2566 for (cu = 1; cu <= 255; cu++) | |
2567 if (isgraph(cu)) | |
8995
3cf6704d6efc
commit https://github.com/vim/vim/commit/af98a49dd0ef1661b4998f118151fddbf6e4df75
Christian Brabandt <cb@256bit.org>
parents:
8863
diff
changeset
|
2568 regmbc(cu); |
7 | 2569 break; |
2570 case CLASS_LOWER: | |
2571 for (cu = 1; cu <= 255; cu++) | |
9015
42b228c8701b
commit https://github.com/vim/vim/commit/e8aee7dcf9b12becff86e8ce1783a86801c5f9f6
Christian Brabandt <cb@256bit.org>
parents:
8995
diff
changeset
|
2572 if (MB_ISLOWER(cu) && cu != 170 |
42b228c8701b
commit https://github.com/vim/vim/commit/e8aee7dcf9b12becff86e8ce1783a86801c5f9f6
Christian Brabandt <cb@256bit.org>
parents:
8995
diff
changeset
|
2573 && cu != 186) |
8995
3cf6704d6efc
commit https://github.com/vim/vim/commit/af98a49dd0ef1661b4998f118151fddbf6e4df75
Christian Brabandt <cb@256bit.org>
parents:
8863
diff
changeset
|
2574 regmbc(cu); |
7 | 2575 break; |
2576 case CLASS_PRINT: | |
2577 for (cu = 1; cu <= 255; cu++) | |
2578 if (vim_isprintc(cu)) | |
8995
3cf6704d6efc
commit https://github.com/vim/vim/commit/af98a49dd0ef1661b4998f118151fddbf6e4df75
Christian Brabandt <cb@256bit.org>
parents:
8863
diff
changeset
|
2579 regmbc(cu); |
7 | 2580 break; |
2581 case CLASS_PUNCT: | |
9015
42b228c8701b
commit https://github.com/vim/vim/commit/e8aee7dcf9b12becff86e8ce1783a86801c5f9f6
Christian Brabandt <cb@256bit.org>
parents:
8995
diff
changeset
|
2582 for (cu = 1; cu < 128; cu++) |
7 | 2583 if (ispunct(cu)) |
8995
3cf6704d6efc
commit https://github.com/vim/vim/commit/af98a49dd0ef1661b4998f118151fddbf6e4df75
Christian Brabandt <cb@256bit.org>
parents:
8863
diff
changeset
|
2584 regmbc(cu); |
7 | 2585 break; |
2586 case CLASS_SPACE: | |
2587 for (cu = 9; cu <= 13; cu++) | |
2588 regc(cu); | |
2589 regc(' '); | |
2590 break; | |
2591 case CLASS_UPPER: | |
2592 for (cu = 1; cu <= 255; cu++) | |
1347 | 2593 if (MB_ISUPPER(cu)) |
8995
3cf6704d6efc
commit https://github.com/vim/vim/commit/af98a49dd0ef1661b4998f118151fddbf6e4df75
Christian Brabandt <cb@256bit.org>
parents:
8863
diff
changeset
|
2594 regmbc(cu); |
7 | 2595 break; |
2596 case CLASS_XDIGIT: | |
2597 for (cu = 1; cu <= 255; cu++) | |
2598 if (vim_isxdigit(cu)) | |
8995
3cf6704d6efc
commit https://github.com/vim/vim/commit/af98a49dd0ef1661b4998f118151fddbf6e4df75
Christian Brabandt <cb@256bit.org>
parents:
8863
diff
changeset
|
2599 regmbc(cu); |
7 | 2600 break; |
2601 case CLASS_TAB: | |
2602 regc('\t'); | |
2603 break; | |
2604 case CLASS_RETURN: | |
2605 regc('\r'); | |
2606 break; | |
2607 case CLASS_BACKSPACE: | |
2608 regc('\b'); | |
2609 break; | |
2610 case CLASS_ESCAPE: | |
2611 regc('\033'); | |
2612 break; | |
2613 } | |
2614 } | |
2615 else | |
2616 { | |
2617 #ifdef FEAT_MBYTE | |
2618 if (has_mbyte) | |
2619 { | |
2620 int len; | |
2621 | |
2622 /* produce a multibyte character, including any | |
2623 * following composing characters */ | |
2624 startc = mb_ptr2char(regparse); | |
474 | 2625 len = (*mb_ptr2len)(regparse); |
7 | 2626 if (enc_utf8 && utf_char2len(startc) != len) |
2627 startc = -1; /* composing chars */ | |
2628 while (--len >= 0) | |
2629 regc(*regparse++); | |
2630 } | |
2631 else | |
2632 #endif | |
2633 { | |
2634 startc = *regparse++; | |
2635 regc(startc); | |
2636 } | |
2637 } | |
2638 } | |
2639 regc(NUL); | |
2640 prevchr_len = 1; /* last char was the ']' */ | |
2641 if (*regparse != ']') | |
2642 EMSG_RET_NULL(_(e_toomsbra)); /* Cannot happen? */ | |
2643 skipchr(); /* let's be friends with the lexer again */ | |
2644 *flagp |= HASWIDTH | SIMPLE; | |
2645 break; | |
2646 } | |
481 | 2647 else if (reg_strict) |
4444 | 2648 EMSG2_RET_NULL(_(e_missingbracket), reg_magic > MAGIC_OFF); |
7 | 2649 } |
2650 /* FALLTHROUGH */ | |
2651 | |
2652 default: | |
2653 { | |
2654 int len; | |
2655 | |
2656 #ifdef FEAT_MBYTE | |
2657 /* A multi-byte character is handled as a separate atom if it's | |
714 | 2658 * before a multi and when it's a composing char. */ |
2659 if (use_multibytecode(c)) | |
7 | 2660 { |
714 | 2661 do_multibyte: |
7 | 2662 ret = regnode(MULTIBYTECODE); |
2663 regmbc(c); | |
2664 *flagp |= HASWIDTH | SIMPLE; | |
2665 break; | |
2666 } | |
2667 #endif | |
2668 | |
2669 ret = regnode(EXACTLY); | |
2670 | |
2671 /* | |
2672 * Append characters as long as: | |
2673 * - there is no following multi, we then need the character in | |
2674 * front of it as a single character operand | |
2675 * - not running into a Magic character | |
2676 * - "one_exactly" is not set | |
2677 * But always emit at least one character. Might be a Multi, | |
2678 * e.g., a "[" without matching "]". | |
2679 */ | |
2680 for (len = 0; c != NUL && (len == 0 | |
2681 || (re_multi_type(peekchr()) == NOT_MULTI | |
2682 && !one_exactly | |
2683 && !is_Magic(c))); ++len) | |
2684 { | |
2685 c = no_Magic(c); | |
2686 #ifdef FEAT_MBYTE | |
2687 if (has_mbyte) | |
2688 { | |
2689 regmbc(c); | |
2690 if (enc_utf8) | |
2691 { | |
2692 int l; | |
2693 | |
714 | 2694 /* Need to get composing character too. */ |
7 | 2695 for (;;) |
2696 { | |
714 | 2697 l = utf_ptr2len(regparse); |
2698 if (!UTF_COMPOSINGLIKE(regparse, regparse + l)) | |
7 | 2699 break; |
714 | 2700 regmbc(utf_ptr2char(regparse)); |
2701 skipchr(); | |
7 | 2702 } |
2703 } | |
2704 } | |
2705 else | |
2706 #endif | |
2707 regc(c); | |
2708 c = getchr(); | |
2709 } | |
2710 ungetchr(); | |
2711 | |
2712 regc(NUL); | |
2713 *flagp |= HASWIDTH; | |
2714 if (len == 1) | |
2715 *flagp |= SIMPLE; | |
2716 } | |
2717 break; | |
2718 } | |
2719 | |
2720 return ret; | |
2721 } | |
2722 | |
714 | 2723 #ifdef FEAT_MBYTE |
2724 /* | |
2725 * Return TRUE if MULTIBYTECODE should be used instead of EXACTLY for | |
2726 * character "c". | |
2727 */ | |
2728 static int | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
2729 use_multibytecode(int c) |
714 | 2730 { |
2731 return has_mbyte && (*mb_char2len)(c) > 1 | |
2732 && (re_multi_type(peekchr()) != NOT_MULTI | |
2733 || (enc_utf8 && utf_iscomposing(c))); | |
2734 } | |
2735 #endif | |
2736 | |
7 | 2737 /* |
4444 | 2738 * Emit a node. |
7 | 2739 * Return pointer to generated code. |
2740 */ | |
2741 static char_u * | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
2742 regnode(int op) |
7 | 2743 { |
2744 char_u *ret; | |
2745 | |
2746 ret = regcode; | |
2747 if (ret == JUST_CALC_SIZE) | |
2748 regsize += 3; | |
2749 else | |
2750 { | |
2751 *regcode++ = op; | |
2752 *regcode++ = NUL; /* Null "next" pointer. */ | |
2753 *regcode++ = NUL; | |
2754 } | |
2755 return ret; | |
2756 } | |
2757 | |
2758 /* | |
2759 * Emit (if appropriate) a byte of code | |
2760 */ | |
2761 static void | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
2762 regc(int b) |
7 | 2763 { |
2764 if (regcode == JUST_CALC_SIZE) | |
2765 regsize++; | |
2766 else | |
2767 *regcode++ = b; | |
2768 } | |
2769 | |
2770 #ifdef FEAT_MBYTE | |
2771 /* | |
2772 * Emit (if appropriate) a multi-byte character of code | |
2773 */ | |
2774 static void | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
2775 regmbc(int c) |
7 | 2776 { |
2974 | 2777 if (!has_mbyte && c > 0xff) |
2778 return; | |
7 | 2779 if (regcode == JUST_CALC_SIZE) |
2780 regsize += (*mb_char2len)(c); | |
2781 else | |
2782 regcode += (*mb_char2bytes)(c, regcode); | |
2783 } | |
2784 #endif | |
2785 | |
2786 /* | |
4444 | 2787 * Insert an operator in front of already-emitted operand |
7 | 2788 * |
2789 * Means relocating the operand. | |
2790 */ | |
2791 static void | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
2792 reginsert(int op, char_u *opnd) |
7 | 2793 { |
2794 char_u *src; | |
2795 char_u *dst; | |
2796 char_u *place; | |
2797 | |
2798 if (regcode == JUST_CALC_SIZE) | |
2799 { | |
2800 regsize += 3; | |
2801 return; | |
2802 } | |
2803 src = regcode; | |
2804 regcode += 3; | |
2805 dst = regcode; | |
2806 while (src > opnd) | |
2807 *--dst = *--src; | |
2808 | |
2809 place = opnd; /* Op node, where operand used to be. */ | |
2810 *place++ = op; | |
2811 *place++ = NUL; | |
2812 *place = NUL; | |
2813 } | |
2814 | |
2815 /* | |
4444 | 2816 * Insert an operator in front of already-emitted operand. |
4579
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2817 * Add a number to the operator. |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2818 */ |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2819 static void |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
2820 reginsert_nr(int op, long val, char_u *opnd) |
4579
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2821 { |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2822 char_u *src; |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2823 char_u *dst; |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2824 char_u *place; |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2825 |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2826 if (regcode == JUST_CALC_SIZE) |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2827 { |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2828 regsize += 7; |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2829 return; |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2830 } |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2831 src = regcode; |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2832 regcode += 7; |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2833 dst = regcode; |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2834 while (src > opnd) |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2835 *--dst = *--src; |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2836 |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2837 place = opnd; /* Op node, where operand used to be. */ |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2838 *place++ = op; |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2839 *place++ = NUL; |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2840 *place++ = NUL; |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2841 place = re_put_long(place, (long_u)val); |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2842 } |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2843 |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2844 /* |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2845 * Insert an operator in front of already-emitted operand. |
7 | 2846 * The operator has the given limit values as operands. Also set next pointer. |
2847 * | |
2848 * Means relocating the operand. | |
2849 */ | |
2850 static void | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
2851 reginsert_limits( |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
2852 int op, |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
2853 long minval, |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
2854 long maxval, |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
2855 char_u *opnd) |
7 | 2856 { |
2857 char_u *src; | |
2858 char_u *dst; | |
2859 char_u *place; | |
2860 | |
2861 if (regcode == JUST_CALC_SIZE) | |
2862 { | |
2863 regsize += 11; | |
2864 return; | |
2865 } | |
2866 src = regcode; | |
2867 regcode += 11; | |
2868 dst = regcode; | |
2869 while (src > opnd) | |
2870 *--dst = *--src; | |
2871 | |
2872 place = opnd; /* Op node, where operand used to be. */ | |
2873 *place++ = op; | |
2874 *place++ = NUL; | |
2875 *place++ = NUL; | |
2876 place = re_put_long(place, (long_u)minval); | |
2877 place = re_put_long(place, (long_u)maxval); | |
2878 regtail(opnd, place); | |
2879 } | |
2880 | |
2881 /* | |
2882 * Write a long as four bytes at "p" and return pointer to the next char. | |
2883 */ | |
2884 static char_u * | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
2885 re_put_long(char_u *p, long_u val) |
7 | 2886 { |
2887 *p++ = (char_u) ((val >> 24) & 0377); | |
2888 *p++ = (char_u) ((val >> 16) & 0377); | |
2889 *p++ = (char_u) ((val >> 8) & 0377); | |
2890 *p++ = (char_u) (val & 0377); | |
2891 return p; | |
2892 } | |
2893 | |
2894 /* | |
4444 | 2895 * Set the next-pointer at the end of a node chain. |
7 | 2896 */ |
2897 static void | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
2898 regtail(char_u *p, char_u *val) |
7 | 2899 { |
2900 char_u *scan; | |
2901 char_u *temp; | |
2902 int offset; | |
2903 | |
2904 if (p == JUST_CALC_SIZE) | |
2905 return; | |
2906 | |
2907 /* Find last node. */ | |
2908 scan = p; | |
2909 for (;;) | |
2910 { | |
2911 temp = regnext(scan); | |
2912 if (temp == NULL) | |
2913 break; | |
2914 scan = temp; | |
2915 } | |
2916 | |
233 | 2917 if (OP(scan) == BACK) |
7 | 2918 offset = (int)(scan - val); |
2919 else | |
2920 offset = (int)(val - scan); | |
2010 | 2921 /* When the offset uses more than 16 bits it can no longer fit in the two |
2974 | 2922 * bytes available. Use a global flag to avoid having to check return |
2010 | 2923 * values in too many places. */ |
2924 if (offset > 0xffff) | |
2925 reg_toolong = TRUE; | |
2926 else | |
2927 { | |
2928 *(scan + 1) = (char_u) (((unsigned)offset >> 8) & 0377); | |
2929 *(scan + 2) = (char_u) (offset & 0377); | |
2930 } | |
7 | 2931 } |
2932 | |
2933 /* | |
4444 | 2934 * Like regtail, on item after a BRANCH; nop if none. |
7 | 2935 */ |
2936 static void | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
2937 regoptail(char_u *p, char_u *val) |
7 | 2938 { |
2939 /* When op is neither BRANCH nor BRACE_COMPLEX0-9, it is "operandless" */ | |
2940 if (p == NULL || p == JUST_CALC_SIZE | |
2941 || (OP(p) != BRANCH | |
2942 && (OP(p) < BRACE_COMPLEX || OP(p) > BRACE_COMPLEX + 9))) | |
2943 return; | |
2944 regtail(OPERAND(p), val); | |
2945 } | |
2946 | |
2947 /* | |
4444 | 2948 * Functions for getting characters from the regexp input. |
7 | 2949 */ |
4444 | 2950 /* |
2951 * Start parsing at "str". | |
2952 */ | |
7 | 2953 static void |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
2954 initchr(char_u *str) |
7 | 2955 { |
2956 regparse = str; | |
2957 prevchr_len = 0; | |
2958 curchr = prevprevchr = prevchr = nextchr = -1; | |
2959 at_start = TRUE; | |
2960 prev_at_start = FALSE; | |
2961 } | |
2962 | |
4444 | 2963 /* |
4679
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2964 * Save the current parse state, so that it can be restored and parsing |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2965 * starts in the same state again. |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2966 */ |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2967 static void |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
2968 save_parse_state(parse_state_T *ps) |
4679
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2969 { |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2970 ps->regparse = regparse; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2971 ps->prevchr_len = prevchr_len; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2972 ps->curchr = curchr; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2973 ps->prevchr = prevchr; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2974 ps->prevprevchr = prevprevchr; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2975 ps->nextchr = nextchr; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2976 ps->at_start = at_start; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2977 ps->prev_at_start = prev_at_start; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2978 ps->regnpar = regnpar; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2979 } |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2980 |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2981 /* |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2982 * Restore a previously saved parse state. |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2983 */ |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2984 static void |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
2985 restore_parse_state(parse_state_T *ps) |
4679
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2986 { |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2987 regparse = ps->regparse; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2988 prevchr_len = ps->prevchr_len; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2989 curchr = ps->curchr; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2990 prevchr = ps->prevchr; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2991 prevprevchr = ps->prevprevchr; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2992 nextchr = ps->nextchr; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2993 at_start = ps->at_start; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2994 prev_at_start = ps->prev_at_start; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2995 regnpar = ps->regnpar; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2996 } |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2997 |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2998 |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2999 /* |
4444 | 3000 * Get the next character without advancing. |
3001 */ | |
7 | 3002 static int |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
3003 peekchr(void) |
7 | 3004 { |
167 | 3005 static int after_slash = FALSE; |
3006 | |
7 | 3007 if (curchr == -1) |
3008 { | |
3009 switch (curchr = regparse[0]) | |
3010 { | |
3011 case '.': | |
3012 case '[': | |
3013 case '~': | |
3014 /* magic when 'magic' is on */ | |
3015 if (reg_magic >= MAGIC_ON) | |
3016 curchr = Magic(curchr); | |
3017 break; | |
3018 case '(': | |
3019 case ')': | |
3020 case '{': | |
3021 case '%': | |
3022 case '+': | |
3023 case '=': | |
3024 case '?': | |
3025 case '@': | |
3026 case '!': | |
3027 case '&': | |
3028 case '|': | |
3029 case '<': | |
3030 case '>': | |
3031 case '#': /* future ext. */ | |
3032 case '"': /* future ext. */ | |
3033 case '\'': /* future ext. */ | |
3034 case ',': /* future ext. */ | |
3035 case '-': /* future ext. */ | |
3036 case ':': /* future ext. */ | |
3037 case ';': /* future ext. */ | |
3038 case '`': /* future ext. */ | |
3039 case '/': /* Can't be used in / command */ | |
3040 /* magic only after "\v" */ | |
3041 if (reg_magic == MAGIC_ALL) | |
3042 curchr = Magic(curchr); | |
3043 break; | |
3044 case '*': | |
167 | 3045 /* * is not magic as the very first character, eg "?*ptr", when |
3046 * after '^', eg "/^*ptr" and when after "\(", "\|", "\&". But | |
3047 * "\(\*" is not magic, thus must be magic if "after_slash" */ | |
3048 if (reg_magic >= MAGIC_ON | |
3049 && !at_start | |
3050 && !(prev_at_start && prevchr == Magic('^')) | |
3051 && (after_slash | |
3052 || (prevchr != Magic('(') | |
3053 && prevchr != Magic('&') | |
3054 && prevchr != Magic('|')))) | |
7 | 3055 curchr = Magic('*'); |
3056 break; | |
3057 case '^': | |
3058 /* '^' is only magic as the very first character and if it's after | |
3059 * "\(", "\|", "\&' or "\n" */ | |
3060 if (reg_magic >= MAGIC_OFF | |
3061 && (at_start | |
3062 || reg_magic == MAGIC_ALL | |
3063 || prevchr == Magic('(') | |
3064 || prevchr == Magic('|') | |
3065 || prevchr == Magic('&') | |
3066 || prevchr == Magic('n') | |
3067 || (no_Magic(prevchr) == '(' | |
3068 && prevprevchr == Magic('%')))) | |
3069 { | |
3070 curchr = Magic('^'); | |
3071 at_start = TRUE; | |
3072 prev_at_start = FALSE; | |
3073 } | |
3074 break; | |
3075 case '$': | |
3076 /* '$' is only magic as the very last char and if it's in front of | |
3077 * either "\|", "\)", "\&", or "\n" */ | |
3078 if (reg_magic >= MAGIC_OFF) | |
3079 { | |
3080 char_u *p = regparse + 1; | |
6041 | 3081 int is_magic_all = (reg_magic == MAGIC_ALL); |
3082 | |
3083 /* ignore \c \C \m \M \v \V and \Z after '$' */ | |
7 | 3084 while (p[0] == '\\' && (p[1] == 'c' || p[1] == 'C' |
6041 | 3085 || p[1] == 'm' || p[1] == 'M' |
3086 || p[1] == 'v' || p[1] == 'V' || p[1] == 'Z')) | |
3087 { | |
3088 if (p[1] == 'v') | |
3089 is_magic_all = TRUE; | |
3090 else if (p[1] == 'm' || p[1] == 'M' || p[1] == 'V') | |
3091 is_magic_all = FALSE; | |
7 | 3092 p += 2; |
6041 | 3093 } |
7 | 3094 if (p[0] == NUL |
3095 || (p[0] == '\\' | |
3096 && (p[1] == '|' || p[1] == '&' || p[1] == ')' | |
3097 || p[1] == 'n')) | |
6041 | 3098 || (is_magic_all |
3099 && (p[0] == '|' || p[0] == '&' || p[0] == ')')) | |
7 | 3100 || reg_magic == MAGIC_ALL) |
3101 curchr = Magic('$'); | |
3102 } | |
3103 break; | |
3104 case '\\': | |
3105 { | |
3106 int c = regparse[1]; | |
3107 | |
3108 if (c == NUL) | |
3109 curchr = '\\'; /* trailing '\' */ | |
3110 else if ( | |
3111 #ifdef EBCDIC | |
3112 vim_strchr(META, c) | |
3113 #else | |
3114 c <= '~' && META_flags[c] | |
3115 #endif | |
3116 ) | |
3117 { | |
3118 /* | |
3119 * META contains everything that may be magic sometimes, | |
3120 * except ^ and $ ("\^" and "\$" are only magic after | |
6830 | 3121 * "\V"). We now fetch the next character and toggle its |
7 | 3122 * magicness. Therefore, \ is so meta-magic that it is |
3123 * not in META. | |
3124 */ | |
3125 curchr = -1; | |
3126 prev_at_start = at_start; | |
3127 at_start = FALSE; /* be able to say "/\*ptr" */ | |
3128 ++regparse; | |
167 | 3129 ++after_slash; |
7 | 3130 peekchr(); |
3131 --regparse; | |
167 | 3132 --after_slash; |
7 | 3133 curchr = toggle_Magic(curchr); |
3134 } | |
3135 else if (vim_strchr(REGEXP_ABBR, c)) | |
3136 { | |
3137 /* | |
3138 * Handle abbreviations, like "\t" for TAB -- webb | |
3139 */ | |
3140 curchr = backslash_trans(c); | |
3141 } | |
3142 else if (reg_magic == MAGIC_NONE && (c == '$' || c == '^')) | |
3143 curchr = toggle_Magic(c); | |
3144 else | |
3145 { | |
3146 /* | |
3147 * Next character can never be (made) magic? | |
3148 * Then backslashing it won't do anything. | |
3149 */ | |
3150 #ifdef FEAT_MBYTE | |
3151 if (has_mbyte) | |
3152 curchr = (*mb_ptr2char)(regparse + 1); | |
3153 else | |
3154 #endif | |
3155 curchr = c; | |
3156 } | |
3157 break; | |
3158 } | |
3159 | |
3160 #ifdef FEAT_MBYTE | |
3161 default: | |
3162 if (has_mbyte) | |
3163 curchr = (*mb_ptr2char)(regparse); | |
3164 #endif | |
3165 } | |
3166 } | |
3167 | |
3168 return curchr; | |
3169 } | |
3170 | |
3171 /* | |
3172 * Eat one lexed character. Do this in a way that we can undo it. | |
3173 */ | |
3174 static void | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
3175 skipchr(void) |
7 | 3176 { |
3177 /* peekchr() eats a backslash, do the same here */ | |
3178 if (*regparse == '\\') | |
3179 prevchr_len = 1; | |
3180 else | |
3181 prevchr_len = 0; | |
3182 if (regparse[prevchr_len] != NUL) | |
3183 { | |
3184 #ifdef FEAT_MBYTE | |
714 | 3185 if (enc_utf8) |
1449 | 3186 /* exclude composing chars that mb_ptr2len does include */ |
3187 prevchr_len += utf_ptr2len(regparse + prevchr_len); | |
714 | 3188 else if (has_mbyte) |
474 | 3189 prevchr_len += (*mb_ptr2len)(regparse + prevchr_len); |
7 | 3190 else |
3191 #endif | |
3192 ++prevchr_len; | |
3193 } | |
3194 regparse += prevchr_len; | |
3195 prev_at_start = at_start; | |
3196 at_start = FALSE; | |
3197 prevprevchr = prevchr; | |
3198 prevchr = curchr; | |
3199 curchr = nextchr; /* use previously unget char, or -1 */ | |
3200 nextchr = -1; | |
3201 } | |
3202 | |
3203 /* | |
3204 * Skip a character while keeping the value of prev_at_start for at_start. | |
3205 * prevchr and prevprevchr are also kept. | |
3206 */ | |
3207 static void | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
3208 skipchr_keepstart(void) |
7 | 3209 { |
3210 int as = prev_at_start; | |
3211 int pr = prevchr; | |
3212 int prpr = prevprevchr; | |
3213 | |
3214 skipchr(); | |
3215 at_start = as; | |
3216 prevchr = pr; | |
3217 prevprevchr = prpr; | |
3218 } | |
3219 | |
4444 | 3220 /* |
3221 * Get the next character from the pattern. We know about magic and such, so | |
3222 * therefore we need a lexical analyzer. | |
3223 */ | |
7 | 3224 static int |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
3225 getchr(void) |
7 | 3226 { |
3227 int chr = peekchr(); | |
3228 | |
3229 skipchr(); | |
3230 return chr; | |
3231 } | |
3232 | |
3233 /* | |
3234 * put character back. Works only once! | |
3235 */ | |
3236 static void | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
3237 ungetchr(void) |
7 | 3238 { |
3239 nextchr = curchr; | |
3240 curchr = prevchr; | |
3241 prevchr = prevprevchr; | |
3242 at_start = prev_at_start; | |
3243 prev_at_start = FALSE; | |
3244 | |
3245 /* Backup regparse, so that it's at the same position as before the | |
3246 * getchr(). */ | |
3247 regparse -= prevchr_len; | |
3248 } | |
3249 | |
3250 /* | |
29 | 3251 * Get and return the value of the hex string at the current position. |
3252 * Return -1 if there is no valid hex number. | |
3253 * The position is updated: | |
24 | 3254 * blahblah\%x20asdf |
856 | 3255 * before-^ ^-after |
24 | 3256 * The parameter controls the maximum number of input characters. This will be |
3257 * 2 when reading a \%x20 sequence and 4 when reading a \%u20AC sequence. | |
3258 */ | |
3259 static int | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
3260 gethexchrs(int maxinputlen) |
24 | 3261 { |
3262 int nr = 0; | |
3263 int c; | |
3264 int i; | |
3265 | |
3266 for (i = 0; i < maxinputlen; ++i) | |
3267 { | |
3268 c = regparse[0]; | |
3269 if (!vim_isxdigit(c)) | |
3270 break; | |
3271 nr <<= 4; | |
3272 nr |= hex2nr(c); | |
3273 ++regparse; | |
3274 } | |
3275 | |
3276 if (i == 0) | |
3277 return -1; | |
3278 return nr; | |
3279 } | |
3280 | |
3281 /* | |
4579
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
3282 * Get and return the value of the decimal string immediately after the |
24 | 3283 * current position. Return -1 for invalid. Consumes all digits. |
3284 */ | |
3285 static int | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
3286 getdecchrs(void) |
24 | 3287 { |
3288 int nr = 0; | |
3289 int c; | |
3290 int i; | |
3291 | |
3292 for (i = 0; ; ++i) | |
3293 { | |
3294 c = regparse[0]; | |
3295 if (c < '0' || c > '9') | |
3296 break; | |
3297 nr *= 10; | |
3298 nr += c - '0'; | |
3299 ++regparse; | |
4579
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
3300 curchr = -1; /* no longer valid */ |
24 | 3301 } |
3302 | |
3303 if (i == 0) | |
3304 return -1; | |
3305 return nr; | |
3306 } | |
3307 | |
3308 /* | |
3309 * get and return the value of the octal string immediately after the current | |
3310 * position. Return -1 for invalid, or 0-255 for valid. Smart enough to handle | |
3311 * numbers > 377 correctly (for example, 400 is treated as 40) and doesn't | |
3312 * treat 8 or 9 as recognised characters. Position is updated: | |
3313 * blahblah\%o210asdf | |
856 | 3314 * before-^ ^-after |
24 | 3315 */ |
3316 static int | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
3317 getoctchrs(void) |
24 | 3318 { |
3319 int nr = 0; | |
3320 int c; | |
3321 int i; | |
3322 | |
3323 for (i = 0; i < 3 && nr < 040; ++i) | |
3324 { | |
3325 c = regparse[0]; | |
3326 if (c < '0' || c > '7') | |
3327 break; | |
3328 nr <<= 3; | |
3329 nr |= hex2nr(c); | |
3330 ++regparse; | |
3331 } | |
3332 | |
3333 if (i == 0) | |
3334 return -1; | |
3335 return nr; | |
3336 } | |
3337 | |
3338 /* | |
3339 * Get a number after a backslash that is inside []. | |
3340 * When nothing is recognized return a backslash. | |
3341 */ | |
3342 static int | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
3343 coll_get_char(void) |
24 | 3344 { |
3345 int nr = -1; | |
3346 | |
3347 switch (*regparse++) | |
3348 { | |
3349 case 'd': nr = getdecchrs(); break; | |
3350 case 'o': nr = getoctchrs(); break; | |
3351 case 'x': nr = gethexchrs(2); break; | |
3352 case 'u': nr = gethexchrs(4); break; | |
3353 case 'U': nr = gethexchrs(8); break; | |
3354 } | |
3355 if (nr < 0) | |
3356 { | |
3357 /* If getting the number fails be backwards compatible: the character | |
3358 * is a backslash. */ | |
3359 --regparse; | |
3360 nr = '\\'; | |
3361 } | |
3362 return nr; | |
3363 } | |
3364 | |
3365 /* | |
7 | 3366 * read_limits - Read two integers to be taken as a minimum and maximum. |
3367 * If the first character is '-', then the range is reversed. | |
3368 * Should end with 'end'. If minval is missing, zero is default, if maxval is | |
3369 * missing, a very big number is the default. | |
3370 */ | |
3371 static int | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
3372 read_limits(long *minval, long *maxval) |
7 | 3373 { |
3374 int reverse = FALSE; | |
3375 char_u *first_char; | |
3376 long tmp; | |
3377 | |
3378 if (*regparse == '-') | |
3379 { | |
3380 /* Starts with '-', so reverse the range later */ | |
3381 regparse++; | |
3382 reverse = TRUE; | |
3383 } | |
3384 first_char = regparse; | |
3385 *minval = getdigits(®parse); | |
3386 if (*regparse == ',') /* There is a comma */ | |
3387 { | |
3388 if (vim_isdigit(*++regparse)) | |
3389 *maxval = getdigits(®parse); | |
3390 else | |
3391 *maxval = MAX_LIMIT; | |
3392 } | |
3393 else if (VIM_ISDIGIT(*first_char)) | |
3394 *maxval = *minval; /* It was \{n} or \{-n} */ | |
3395 else | |
3396 *maxval = MAX_LIMIT; /* It was \{} or \{-} */ | |
3397 if (*regparse == '\\') | |
3398 regparse++; /* Allow either \{...} or \{...\} */ | |
167 | 3399 if (*regparse != '}') |
7 | 3400 { |
3401 sprintf((char *)IObuff, _("E554: Syntax error in %s{...}"), | |
3402 reg_magic == MAGIC_ALL ? "" : "\\"); | |
3403 EMSG_RET_FAIL(IObuff); | |
3404 } | |
3405 | |
3406 /* | |
3407 * Reverse the range if there was a '-', or make sure it is in the right | |
3408 * order otherwise. | |
3409 */ | |
3410 if ((!reverse && *minval > *maxval) || (reverse && *minval < *maxval)) | |
3411 { | |
3412 tmp = *minval; | |
3413 *minval = *maxval; | |
3414 *maxval = tmp; | |
3415 } | |
3416 skipchr(); /* let's be friends with the lexer again */ | |
3417 return OK; | |
3418 } | |
3419 | |
3420 /* | |
3421 * vim_regexec and friends | |
3422 */ | |
3423 | |
3424 /* | |
3425 * Global work variables for vim_regexec(). | |
3426 */ | |
3427 | |
3428 /* The current match-position is remembered with these variables: */ | |
3429 static linenr_T reglnum; /* line number, relative to first line */ | |
3430 static char_u *regline; /* start of current line */ | |
3431 static char_u *reginput; /* current input, points into "regline" */ | |
3432 | |
3433 static int need_clear_subexpr; /* subexpressions still need to be | |
3434 * cleared */ | |
3435 #ifdef FEAT_SYN_HL | |
3436 static int need_clear_zsubexpr = FALSE; /* extmatch subexpressions | |
3437 * still need to be cleared */ | |
3438 #endif | |
3439 | |
3440 /* | |
3441 * Structure used to save the current input state, when it needs to be | |
3442 * restored after trying a match. Used by reg_save() and reg_restore(). | |
233 | 3443 * Also stores the length of "backpos". |
7 | 3444 */ |
3445 typedef struct | |
3446 { | |
3447 union | |
3448 { | |
3449 char_u *ptr; /* reginput pointer, for single-line regexp */ | |
3450 lpos_T pos; /* reginput pos, for multi-line regexp */ | |
3451 } rs_u; | |
233 | 3452 int rs_len; |
7 | 3453 } regsave_T; |
3454 | |
3455 /* struct to save start/end pointer/position in for \(\) */ | |
3456 typedef struct | |
3457 { | |
3458 union | |
3459 { | |
3460 char_u *ptr; | |
3461 lpos_T pos; | |
3462 } se_u; | |
3463 } save_se_T; | |
3464 | |
1579 | 3465 /* used for BEHIND and NOBEHIND matching */ |
3466 typedef struct regbehind_S | |
3467 { | |
3468 regsave_T save_after; | |
3469 regsave_T save_behind; | |
1602 | 3470 int save_need_clear_subexpr; |
1579 | 3471 save_se_T save_start[NSUBEXP]; |
3472 save_se_T save_end[NSUBEXP]; | |
3473 } regbehind_T; | |
3474 | |
7805
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
3475 static char_u *reg_getline(linenr_T lnum); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
3476 static long bt_regexec_both(char_u *line, colnr_T col, proftime_T *tm); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
3477 static long regtry(bt_regprog_T *prog, colnr_T col); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
3478 static void cleanup_subexpr(void); |
7 | 3479 #ifdef FEAT_SYN_HL |
7805
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
3480 static void cleanup_zsubexpr(void); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
3481 #endif |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
3482 static void save_subexpr(regbehind_T *bp); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
3483 static void restore_subexpr(regbehind_T *bp); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
3484 static void reg_nextline(void); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
3485 static void reg_save(regsave_T *save, garray_T *gap); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
3486 static void reg_restore(regsave_T *save, garray_T *gap); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
3487 static int reg_save_equal(regsave_T *save); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
3488 static void save_se_multi(save_se_T *savep, lpos_T *posp); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
3489 static void save_se_one(save_se_T *savep, char_u **pp); |
7 | 3490 |
3491 /* Save the sub-expressions before attempting a match. */ | |
3492 #define save_se(savep, posp, pp) \ | |
3493 REG_MULTI ? save_se_multi((savep), (posp)) : save_se_one((savep), (pp)) | |
3494 | |
3495 /* After a failed match restore the sub-expressions. */ | |
3496 #define restore_se(savep, posp, pp) { \ | |
3497 if (REG_MULTI) \ | |
3498 *(posp) = (savep)->se_u.pos; \ | |
3499 else \ | |
3500 *(pp) = (savep)->se_u.ptr; } | |
3501 | |
7805
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
3502 static int re_num_cmp(long_u val, char_u *scan); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
3503 static int match_with_backref(linenr_T start_lnum, colnr_T start_col, linenr_T end_lnum, colnr_T end_col, int *bytelen); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
3504 static int regmatch(char_u *prog); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
3505 static int regrepeat(char_u *p, long maxcount); |
7 | 3506 |
3507 #ifdef DEBUG | |
3508 int regnarrate = 0; | |
3509 #endif | |
3510 | |
3511 /* | |
3512 * Sometimes need to save a copy of a line. Since alloc()/free() is very | |
3513 * slow, we keep one allocated piece of memory and only re-allocate it when | |
4444 | 3514 * it's too small. It's freed in bt_regexec_both() when finished. |
7 | 3515 */ |
1468 | 3516 static char_u *reg_tofree = NULL; |
7 | 3517 static unsigned reg_tofreelen; |
3518 | |
3519 /* | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3520 * Structure used to store the execution state of the regex engine. |
1209 | 3521 * Which ones are set depends on whether a single-line or multi-line match is |
7 | 3522 * done: |
3523 * single-line multi-line | |
3524 * reg_match ®match_T NULL | |
3525 * reg_mmatch NULL ®mmatch_T | |
3526 * reg_startp reg_match->startp <invalid> | |
3527 * reg_endp reg_match->endp <invalid> | |
3528 * reg_startpos <invalid> reg_mmatch->startpos | |
3529 * reg_endpos <invalid> reg_mmatch->endpos | |
3530 * reg_win NULL window in which to search | |
4061 | 3531 * reg_buf curbuf buffer in which to search |
7 | 3532 * reg_firstlnum <invalid> first line in which to search |
3533 * reg_maxline 0 last line nr | |
3534 * reg_line_lbr FALSE or TRUE FALSE | |
3535 */ | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3536 typedef struct { |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3537 regmatch_T *reg_match; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3538 regmmatch_T *reg_mmatch; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3539 char_u **reg_startp; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3540 char_u **reg_endp; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3541 lpos_T *reg_startpos; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3542 lpos_T *reg_endpos; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3543 win_T *reg_win; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3544 buf_T *reg_buf; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3545 linenr_T reg_firstlnum; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3546 linenr_T reg_maxline; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3547 int reg_line_lbr; /* "\n" in string is line break */ |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3548 |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3549 /* Internal copy of 'ignorecase'. It is set at each call to vim_regexec(). |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3550 * Normally it gets the value of "rm_ic" or "rmm_ic", but when the pattern |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3551 * contains '\c' or '\C' the value is overruled. */ |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3552 int reg_ic; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3553 |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3554 #ifdef FEAT_MBYTE |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3555 /* Similar to rex.reg_ic, but only for 'combining' characters. Set with \Z |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3556 * flag in the regexp. Defaults to false, always. */ |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3557 int reg_icombine; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3558 #endif |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3559 |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3560 /* Copy of "rmm_maxcol": maximum column to search for a match. Zero when |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3561 * there is no maximum. */ |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3562 colnr_T reg_maxcol; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3563 } regexec_T; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3564 |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3565 static regexec_T rex; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3566 static int rex_in_use = FALSE; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3567 |
7 | 3568 |
270 | 3569 /* Values for rs_state in regitem_T. */ |
3570 typedef enum regstate_E | |
3571 { | |
3572 RS_NOPEN = 0 /* NOPEN and NCLOSE */ | |
3573 , RS_MOPEN /* MOPEN + [0-9] */ | |
3574 , RS_MCLOSE /* MCLOSE + [0-9] */ | |
3575 #ifdef FEAT_SYN_HL | |
3576 , RS_ZOPEN /* ZOPEN + [0-9] */ | |
3577 , RS_ZCLOSE /* ZCLOSE + [0-9] */ | |
3578 #endif | |
3579 , RS_BRANCH /* BRANCH */ | |
3580 , RS_BRCPLX_MORE /* BRACE_COMPLEX and trying one more match */ | |
3581 , RS_BRCPLX_LONG /* BRACE_COMPLEX and trying longest match */ | |
3582 , RS_BRCPLX_SHORT /* BRACE_COMPLEX and trying shortest match */ | |
3583 , RS_NOMATCH /* NOMATCH */ | |
3584 , RS_BEHIND1 /* BEHIND / NOBEHIND matching rest */ | |
3585 , RS_BEHIND2 /* BEHIND / NOBEHIND matching behind part */ | |
3586 , RS_STAR_LONG /* STAR/PLUS/BRACE_SIMPLE longest match */ | |
3587 , RS_STAR_SHORT /* STAR/PLUS/BRACE_SIMPLE shortest match */ | |
3588 } regstate_T; | |
3589 | |
3590 /* | |
3591 * When there are alternatives a regstate_T is put on the regstack to remember | |
3592 * what we are doing. | |
3593 * Before it may be another type of item, depending on rs_state, to remember | |
3594 * more things. | |
3595 */ | |
3596 typedef struct regitem_S | |
3597 { | |
3598 regstate_T rs_state; /* what we are doing, one of RS_ above */ | |
3599 char_u *rs_scan; /* current node in program */ | |
3600 union | |
3601 { | |
3602 save_se_T sesave; | |
3603 regsave_T regsave; | |
3604 } rs_un; /* room for saving reginput */ | |
1579 | 3605 short rs_no; /* submatch nr or BEHIND/NOBEHIND */ |
270 | 3606 } regitem_T; |
3607 | |
7805
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
3608 static regitem_T *regstack_push(regstate_T state, char_u *scan); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
3609 static void regstack_pop(char_u **scan); |
270 | 3610 |
3611 /* used for STAR, PLUS and BRACE_SIMPLE matching */ | |
3612 typedef struct regstar_S | |
3613 { | |
3614 int nextb; /* next byte */ | |
3615 int nextb_ic; /* next byte reverse case */ | |
3616 long count; | |
3617 long minval; | |
3618 long maxval; | |
3619 } regstar_T; | |
3620 | |
3621 /* used to store input position when a BACK was encountered, so that we now if | |
3622 * we made any progress since the last time. */ | |
3623 typedef struct backpos_S | |
3624 { | |
3625 char_u *bp_scan; /* "scan" where BACK was encountered */ | |
3626 regsave_T bp_pos; /* last input position */ | |
3627 } backpos_T; | |
3628 | |
3629 /* | |
1520 | 3630 * "regstack" and "backpos" are used by regmatch(). They are kept over calls |
3631 * to avoid invoking malloc() and free() often. | |
3632 * "regstack" is a stack with regitem_T items, sometimes preceded by regstar_T | |
3633 * or regbehind_T. | |
3634 * "backpos_T" is a table with backpos_T for BACK | |
270 | 3635 */ |
1520 | 3636 static garray_T regstack = {0, 0, 0, 0, NULL}; |
3637 static garray_T backpos = {0, 0, 0, 0, NULL}; | |
3638 | |
3639 /* | |
3640 * Both for regstack and backpos tables we use the following strategy of | |
3641 * allocation (to reduce malloc/free calls): | |
3642 * - Initial size is fairly small. | |
3643 * - When needed, the tables are grown bigger (8 times at first, double after | |
3644 * that). | |
3645 * - After executing the match we free the memory only if the array has grown. | |
3646 * Thus the memory is kept allocated when it's at the initial size. | |
3647 * This makes it fast while not keeping a lot of memory allocated. | |
3648 * A three times speed increase was observed when using many simple patterns. | |
3649 */ | |
3650 #define REGSTACK_INITIAL 2048 | |
3651 #define BACKPOS_INITIAL 64 | |
3652 | |
3653 #if defined(EXITFREE) || defined(PROTO) | |
3654 void | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
3655 free_regexp_stuff(void) |
1520 | 3656 { |
3657 ga_clear(®stack); | |
3658 ga_clear(&backpos); | |
3659 vim_free(reg_tofree); | |
3660 vim_free(reg_prev_sub); | |
3661 } | |
3662 #endif | |
270 | 3663 |
7 | 3664 /* |
3665 * Get pointer to the line "lnum", which is relative to "reg_firstlnum". | |
3666 */ | |
3667 static char_u * | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
3668 reg_getline(linenr_T lnum) |
7 | 3669 { |
3670 /* when looking behind for a match/no-match lnum is negative. But we | |
3671 * can't go before line 1 */ | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3672 if (rex.reg_firstlnum + lnum < 1) |
7 | 3673 return NULL; |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3674 if (lnum > rex.reg_maxline) |
481 | 3675 /* Must have matched the "\n" in the last line. */ |
3676 return (char_u *)""; | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3677 return ml_get_buf(rex.reg_buf, rex.reg_firstlnum + lnum, FALSE); |
7 | 3678 } |
3679 | |
3680 static regsave_T behind_pos; | |
3681 | |
3682 #ifdef FEAT_SYN_HL | |
3683 static char_u *reg_startzp[NSUBEXP]; /* Workspace to mark beginning */ | |
3684 static char_u *reg_endzp[NSUBEXP]; /* and end of \z(...\) matches */ | |
3685 static lpos_T reg_startzpos[NSUBEXP]; /* idem, beginning pos */ | |
3686 static lpos_T reg_endzpos[NSUBEXP]; /* idem, end pos */ | |
3687 #endif | |
3688 | |
3689 /* TRUE if using multi-line regexp. */ | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3690 #define REG_MULTI (rex.reg_match == NULL) |
4444 | 3691 |
7 | 3692 /* |
3693 * Match a regexp against a string. | |
3694 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp(). | |
3695 * Uses curbuf for line count and 'iskeyword'. | |
5838 | 3696 * if "line_lbr" is TRUE consider a "\n" in "line" to be a line break. |
7 | 3697 * |
6390 | 3698 * Returns 0 for failure, number of lines contained in the match otherwise. |
7 | 3699 */ |
4444 | 3700 static int |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
3701 bt_regexec_nl( |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
3702 regmatch_T *rmp, |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
3703 char_u *line, /* string to match against */ |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
3704 colnr_T col, /* column to start looking for match */ |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
3705 int line_lbr) |
7 | 3706 { |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3707 rex.reg_match = rmp; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3708 rex.reg_mmatch = NULL; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3709 rex.reg_maxline = 0; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3710 rex.reg_line_lbr = line_lbr; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3711 rex.reg_buf = curbuf; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3712 rex.reg_win = NULL; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3713 rex.reg_ic = rmp->rm_ic; |
7 | 3714 #ifdef FEAT_MBYTE |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3715 rex.reg_icombine = FALSE; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3716 #endif |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3717 rex.reg_maxcol = 0; |
6390 | 3718 |
3719 return bt_regexec_both(line, col, NULL); | |
7 | 3720 } |
3721 | |
3722 /* | |
3723 * Match a regexp against multiple lines. | |
3724 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp(). | |
3725 * Uses curbuf for line count and 'iskeyword'. | |
3726 * | |
3727 * Return zero if there is no match. Return number of lines contained in the | |
3728 * match otherwise. | |
3729 */ | |
4444 | 3730 static long |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
3731 bt_regexec_multi( |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
3732 regmmatch_T *rmp, |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
3733 win_T *win, /* window in which to search or NULL */ |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
3734 buf_T *buf, /* buffer in which to search */ |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
3735 linenr_T lnum, /* nr of line to start looking for match */ |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
3736 colnr_T col, /* column to start looking for match */ |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
3737 proftime_T *tm) /* timeout limit or NULL */ |
7 | 3738 { |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3739 rex.reg_match = NULL; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3740 rex.reg_mmatch = rmp; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3741 rex.reg_buf = buf; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3742 rex.reg_win = win; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3743 rex.reg_firstlnum = lnum; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3744 rex.reg_maxline = rex.reg_buf->b_ml.ml_line_count - lnum; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3745 rex.reg_line_lbr = FALSE; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3746 rex.reg_ic = rmp->rmm_ic; |
7 | 3747 #ifdef FEAT_MBYTE |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3748 rex.reg_icombine = FALSE; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3749 #endif |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3750 rex.reg_maxcol = rmp->rmm_maxcol; |
7 | 3751 |
6390 | 3752 return bt_regexec_both(NULL, col, tm); |
7 | 3753 } |
3754 | |
3755 /* | |
3756 * Match a regexp against a string ("line" points to the string) or multiple | |
3757 * lines ("line" is NULL, use reg_getline()). | |
6390 | 3758 * Returns 0 for failure, number of lines contained in the match otherwise. |
7 | 3759 */ |
3760 static long | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
3761 bt_regexec_both( |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
3762 char_u *line, |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
3763 colnr_T col, /* column to start looking for match */ |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
3764 proftime_T *tm UNUSED) /* timeout limit or NULL */ |
7 | 3765 { |
6390 | 3766 bt_regprog_T *prog; |
3767 char_u *s; | |
3768 long retval = 0L; | |
7 | 3769 |
1520 | 3770 /* Create "regstack" and "backpos" if they are not allocated yet. |
3771 * We allocate *_INITIAL amount of bytes first and then set the grow size | |
3772 * to much bigger value to avoid many malloc calls in case of deep regular | |
3773 * expressions. */ | |
3774 if (regstack.ga_data == NULL) | |
3775 { | |
3776 /* Use an item size of 1 byte, since we push different things | |
3777 * onto the regstack. */ | |
3778 ga_init2(®stack, 1, REGSTACK_INITIAL); | |
7009 | 3779 (void)ga_grow(®stack, REGSTACK_INITIAL); |
1520 | 3780 regstack.ga_growsize = REGSTACK_INITIAL * 8; |
3781 } | |
3782 | |
3783 if (backpos.ga_data == NULL) | |
3784 { | |
3785 ga_init2(&backpos, sizeof(backpos_T), BACKPOS_INITIAL); | |
7009 | 3786 (void)ga_grow(&backpos, BACKPOS_INITIAL); |
1520 | 3787 backpos.ga_growsize = BACKPOS_INITIAL * 8; |
3788 } | |
270 | 3789 |
7 | 3790 if (REG_MULTI) |
3791 { | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3792 prog = (bt_regprog_T *)rex.reg_mmatch->regprog; |
7 | 3793 line = reg_getline((linenr_T)0); |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3794 rex.reg_startpos = rex.reg_mmatch->startpos; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3795 rex.reg_endpos = rex.reg_mmatch->endpos; |
7 | 3796 } |
3797 else | |
3798 { | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3799 prog = (bt_regprog_T *)rex.reg_match->regprog; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3800 rex.reg_startp = rex.reg_match->startp; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3801 rex.reg_endp = rex.reg_match->endp; |
7 | 3802 } |
3803 | |
3804 /* Be paranoid... */ | |
3805 if (prog == NULL || line == NULL) | |
3806 { | |
3807 EMSG(_(e_null)); | |
3808 goto theend; | |
3809 } | |
3810 | |
3811 /* Check validity of program. */ | |
3812 if (prog_magic_wrong()) | |
3813 goto theend; | |
3814 | |
410 | 3815 /* If the start column is past the maximum column: no need to try. */ |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3816 if (rex.reg_maxcol > 0 && col >= rex.reg_maxcol) |
410 | 3817 goto theend; |
3818 | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3819 /* If pattern contains "\c" or "\C": overrule value of rex.reg_ic */ |
7 | 3820 if (prog->regflags & RF_ICASE) |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3821 rex.reg_ic = TRUE; |
7 | 3822 else if (prog->regflags & RF_NOICASE) |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3823 rex.reg_ic = FALSE; |
7 | 3824 |
3825 #ifdef FEAT_MBYTE | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3826 /* If pattern contains "\Z" overrule value of rex.reg_icombine */ |
7 | 3827 if (prog->regflags & RF_ICOMBINE) |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3828 rex.reg_icombine = TRUE; |
7 | 3829 #endif |
3830 | |
3831 /* If there is a "must appear" string, look for it. */ | |
3832 if (prog->regmust != NULL) | |
3833 { | |
3834 int c; | |
3835 | |
3836 #ifdef FEAT_MBYTE | |
3837 if (has_mbyte) | |
3838 c = (*mb_ptr2char)(prog->regmust); | |
3839 else | |
3840 #endif | |
3841 c = *prog->regmust; | |
3842 s = line + col; | |
170 | 3843 |
3844 /* | |
3845 * This is used very often, esp. for ":global". Use three versions of | |
3846 * the loop to avoid overhead of conditions. | |
3847 */ | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3848 if (!rex.reg_ic |
170 | 3849 #ifdef FEAT_MBYTE |
3850 && !has_mbyte | |
3851 #endif | |
3852 ) | |
3853 while ((s = vim_strbyte(s, c)) != NULL) | |
3854 { | |
3855 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0) | |
3856 break; /* Found it. */ | |
3857 ++s; | |
3858 } | |
3859 #ifdef FEAT_MBYTE | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3860 else if (!rex.reg_ic || (!enc_utf8 && mb_char2len(c) > 1)) |
170 | 3861 while ((s = vim_strchr(s, c)) != NULL) |
3862 { | |
3863 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0) | |
3864 break; /* Found it. */ | |
3865 mb_ptr_adv(s); | |
3866 } | |
3867 #endif | |
3868 else | |
3869 while ((s = cstrchr(s, c)) != NULL) | |
3870 { | |
3871 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0) | |
3872 break; /* Found it. */ | |
3873 mb_ptr_adv(s); | |
3874 } | |
7 | 3875 if (s == NULL) /* Not present. */ |
3876 goto theend; | |
3877 } | |
3878 | |
3879 regline = line; | |
3880 reglnum = 0; | |
2578 | 3881 reg_toolong = FALSE; |
7 | 3882 |
3883 /* Simplest case: Anchored match need be tried only once. */ | |
3884 if (prog->reganch) | |
3885 { | |
3886 int c; | |
3887 | |
3888 #ifdef FEAT_MBYTE | |
3889 if (has_mbyte) | |
3890 c = (*mb_ptr2char)(regline + col); | |
3891 else | |
3892 #endif | |
3893 c = regline[col]; | |
3894 if (prog->regstart == NUL | |
3895 || prog->regstart == c | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3896 || (rex.reg_ic && (( |
7 | 3897 #ifdef FEAT_MBYTE |
3898 (enc_utf8 && utf_fold(prog->regstart) == utf_fold(c))) | |
3899 || (c < 255 && prog->regstart < 255 && | |
3900 #endif | |
1347 | 3901 MB_TOLOWER(prog->regstart) == MB_TOLOWER(c))))) |
7 | 3902 retval = regtry(prog, col); |
3903 else | |
3904 retval = 0; | |
3905 } | |
3906 else | |
3907 { | |
1521 | 3908 #ifdef FEAT_RELTIME |
3909 int tm_count = 0; | |
3910 #endif | |
7 | 3911 /* Messy cases: unanchored match. */ |
180 | 3912 while (!got_int) |
7 | 3913 { |
3914 if (prog->regstart != NUL) | |
3915 { | |
170 | 3916 /* Skip until the char we know it must start with. |
3917 * Used often, do some work to avoid call overhead. */ | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3918 if (!rex.reg_ic |
170 | 3919 #ifdef FEAT_MBYTE |
3920 && !has_mbyte | |
3921 #endif | |
3922 ) | |
3923 s = vim_strbyte(regline + col, prog->regstart); | |
3924 else | |
3925 s = cstrchr(regline + col, prog->regstart); | |
7 | 3926 if (s == NULL) |
3927 { | |
3928 retval = 0; | |
3929 break; | |
3930 } | |
3931 col = (int)(s - regline); | |
3932 } | |
3933 | |
410 | 3934 /* Check for maximum column to try. */ |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
3935 if (rex.reg_maxcol > 0 && col >= rex.reg_maxcol) |
410 | 3936 { |
3937 retval = 0; | |
3938 break; | |
3939 } | |
3940 | |
7 | 3941 retval = regtry(prog, col); |
3942 if (retval > 0) | |
3943 break; | |
3944 | |
3945 /* if not currently on the first line, get it again */ | |
3946 if (reglnum != 0) | |
3947 { | |
481 | 3948 reglnum = 0; |
7 | 3949 regline = reg_getline((linenr_T)0); |
3950 } | |
3951 if (regline[col] == NUL) | |
3952 break; | |
3953 #ifdef FEAT_MBYTE | |
3954 if (has_mbyte) | |
474 | 3955 col += (*mb_ptr2len)(regline + col); |
7 | 3956 else |
3957 #endif | |
3958 ++col; | |
1521 | 3959 #ifdef FEAT_RELTIME |
3960 /* Check for timeout once in a twenty times to avoid overhead. */ | |
3961 if (tm != NULL && ++tm_count == 20) | |
3962 { | |
3963 tm_count = 0; | |
3964 if (profile_passed_limit(tm)) | |
3965 break; | |
3966 } | |
3967 #endif | |
7 | 3968 } |
3969 } | |
3970 | |
3971 theend: | |
1520 | 3972 /* Free "reg_tofree" when it's a bit big. |
3973 * Free regstack and backpos if they are bigger than their initial size. */ | |
3974 if (reg_tofreelen > 400) | |
3975 { | |
3976 vim_free(reg_tofree); | |
3977 reg_tofree = NULL; | |
3978 } | |
3979 if (regstack.ga_maxlen > REGSTACK_INITIAL) | |
3980 ga_clear(®stack); | |
3981 if (backpos.ga_maxlen > BACKPOS_INITIAL) | |
3982 ga_clear(&backpos); | |
270 | 3983 |
7 | 3984 return retval; |
3985 } | |
3986 | |
3987 #ifdef FEAT_SYN_HL | |
7805
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
3988 static reg_extmatch_T *make_extmatch(void); |
7 | 3989 |
3990 /* | |
3991 * Create a new extmatch and mark it as referenced once. | |
3992 */ | |
3993 static reg_extmatch_T * | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
3994 make_extmatch(void) |
7 | 3995 { |
3996 reg_extmatch_T *em; | |
3997 | |
3998 em = (reg_extmatch_T *)alloc_clear((unsigned)sizeof(reg_extmatch_T)); | |
3999 if (em != NULL) | |
4000 em->refcnt = 1; | |
4001 return em; | |
4002 } | |
4003 | |
4004 /* | |
4005 * Add a reference to an extmatch. | |
4006 */ | |
4007 reg_extmatch_T * | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
4008 ref_extmatch(reg_extmatch_T *em) |
7 | 4009 { |
4010 if (em != NULL) | |
4011 em->refcnt++; | |
4012 return em; | |
4013 } | |
4014 | |
4015 /* | |
4016 * Remove a reference to an extmatch. If there are no references left, free | |
4017 * the info. | |
4018 */ | |
4019 void | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
4020 unref_extmatch(reg_extmatch_T *em) |
7 | 4021 { |
4022 int i; | |
4023 | |
4024 if (em != NULL && --em->refcnt <= 0) | |
4025 { | |
4026 for (i = 0; i < NSUBEXP; ++i) | |
4027 vim_free(em->matches[i]); | |
4028 vim_free(em); | |
4029 } | |
4030 } | |
4031 #endif | |
4032 | |
4033 /* | |
4034 * regtry - try match of "prog" with at regline["col"]. | |
4035 * Returns 0 for failure, number of lines contained in the match otherwise. | |
4036 */ | |
4037 static long | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
4038 regtry(bt_regprog_T *prog, colnr_T col) |
7 | 4039 { |
4040 reginput = regline + col; | |
4041 need_clear_subexpr = TRUE; | |
4042 #ifdef FEAT_SYN_HL | |
4043 /* Clear the external match subpointers if necessary. */ | |
4044 if (prog->reghasz == REX_SET) | |
4045 need_clear_zsubexpr = TRUE; | |
4046 #endif | |
4047 | |
189 | 4048 if (regmatch(prog->program + 1) == 0) |
4049 return 0; | |
4050 | |
4051 cleanup_subexpr(); | |
4052 if (REG_MULTI) | |
7 | 4053 { |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4054 if (rex.reg_startpos[0].lnum < 0) |
7 | 4055 { |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4056 rex.reg_startpos[0].lnum = 0; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4057 rex.reg_startpos[0].col = col; |
189 | 4058 } |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4059 if (rex.reg_endpos[0].lnum < 0) |
189 | 4060 { |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4061 rex.reg_endpos[0].lnum = reglnum; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4062 rex.reg_endpos[0].col = (int)(reginput - regline); |
7 | 4063 } |
4064 else | |
189 | 4065 /* Use line number of "\ze". */ |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4066 reglnum = rex.reg_endpos[0].lnum; |
189 | 4067 } |
4068 else | |
4069 { | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4070 if (rex.reg_startp[0] == NULL) |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4071 rex.reg_startp[0] = regline + col; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4072 if (rex.reg_endp[0] == NULL) |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4073 rex.reg_endp[0] = reginput; |
189 | 4074 } |
7 | 4075 #ifdef FEAT_SYN_HL |
189 | 4076 /* Package any found \z(...\) matches for export. Default is none. */ |
4077 unref_extmatch(re_extmatch_out); | |
4078 re_extmatch_out = NULL; | |
4079 | |
4080 if (prog->reghasz == REX_SET) | |
4081 { | |
4082 int i; | |
4083 | |
4084 cleanup_zsubexpr(); | |
4085 re_extmatch_out = make_extmatch(); | |
4086 for (i = 0; i < NSUBEXP; i++) | |
7 | 4087 { |
189 | 4088 if (REG_MULTI) |
7 | 4089 { |
189 | 4090 /* Only accept single line matches. */ |
4091 if (reg_startzpos[i].lnum >= 0 | |
5820 | 4092 && reg_endzpos[i].lnum == reg_startzpos[i].lnum |
4093 && reg_endzpos[i].col >= reg_startzpos[i].col) | |
189 | 4094 re_extmatch_out->matches[i] = |
4095 vim_strnsave(reg_getline(reg_startzpos[i].lnum) | |
7 | 4096 + reg_startzpos[i].col, |
189 | 4097 reg_endzpos[i].col - reg_startzpos[i].col); |
4098 } | |
4099 else | |
4100 { | |
4101 if (reg_startzp[i] != NULL && reg_endzp[i] != NULL) | |
4102 re_extmatch_out->matches[i] = | |
7 | 4103 vim_strnsave(reg_startzp[i], |
189 | 4104 (int)(reg_endzp[i] - reg_startzp[i])); |
7 | 4105 } |
4106 } | |
189 | 4107 } |
7 | 4108 #endif |
189 | 4109 return 1 + reglnum; |
7 | 4110 } |
4111 | |
4112 #ifdef FEAT_MBYTE | |
7805
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
4113 static int reg_prev_class(void); |
7 | 4114 |
4115 /* | |
4116 * Get class of previous character. | |
4117 */ | |
4118 static int | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
4119 reg_prev_class(void) |
7 | 4120 { |
4121 if (reginput > regline) | |
4069 | 4122 return mb_get_class_buf(reginput - 1 |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4123 - (*mb_head_off)(regline, reginput - 1), rex.reg_buf); |
7 | 4124 return -1; |
4125 } | |
5735 | 4126 #endif |
4127 | |
7805
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
4128 static int reg_match_visual(void); |
4730
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4129 |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4130 /* |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4131 * Return TRUE if the current reginput position matches the Visual area. |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4132 */ |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4133 static int |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
4134 reg_match_visual(void) |
4730
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4135 { |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4136 pos_T top, bot; |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4137 linenr_T lnum; |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4138 colnr_T col; |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4139 win_T *wp = rex.reg_win == NULL ? curwin : rex.reg_win; |
4730
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4140 int mode; |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4141 colnr_T start, end; |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4142 colnr_T start2, end2; |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4143 colnr_T cols; |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4144 |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4145 /* Check if the buffer is the current buffer. */ |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4146 if (rex.reg_buf != curbuf || VIsual.lnum == 0) |
4730
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4147 return FALSE; |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4148 |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4149 if (VIsual_active) |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4150 { |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4151 if (lt(VIsual, wp->w_cursor)) |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4152 { |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4153 top = VIsual; |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4154 bot = wp->w_cursor; |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4155 } |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4156 else |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4157 { |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4158 top = wp->w_cursor; |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4159 bot = VIsual; |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4160 } |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4161 mode = VIsual_mode; |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4162 } |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4163 else |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4164 { |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4165 if (lt(curbuf->b_visual.vi_start, curbuf->b_visual.vi_end)) |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4166 { |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4167 top = curbuf->b_visual.vi_start; |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4168 bot = curbuf->b_visual.vi_end; |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4169 } |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4170 else |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4171 { |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4172 top = curbuf->b_visual.vi_end; |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4173 bot = curbuf->b_visual.vi_start; |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4174 } |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4175 mode = curbuf->b_visual.vi_mode; |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4176 } |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4177 lnum = reglnum + rex.reg_firstlnum; |
4730
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4178 if (lnum < top.lnum || lnum > bot.lnum) |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4179 return FALSE; |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4180 |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4181 if (mode == 'v') |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4182 { |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4183 col = (colnr_T)(reginput - regline); |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4184 if ((lnum == top.lnum && col < top.col) |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4185 || (lnum == bot.lnum && col >= bot.col + (*p_sel != 'e'))) |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4186 return FALSE; |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4187 } |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4188 else if (mode == Ctrl_V) |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4189 { |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4190 getvvcol(wp, &top, &start, NULL, &end); |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4191 getvvcol(wp, &bot, &start2, NULL, &end2); |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4192 if (start2 < start) |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4193 start = start2; |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4194 if (end2 > end) |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4195 end = end2; |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4196 if (top.col == MAXCOL || bot.col == MAXCOL) |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4197 end = MAXCOL; |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4198 cols = win_linetabsize(wp, regline, (colnr_T)(reginput - regline)); |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4199 if (cols < start || cols > end - (*p_sel == 'e')) |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4200 return FALSE; |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4201 } |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4202 return TRUE; |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4203 } |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4204 |
39 | 4205 #define ADVANCE_REGINPUT() mb_ptr_adv(reginput) |
7 | 4206 |
4207 /* | |
4208 * The arguments from BRACE_LIMITS are stored here. They are actually local | |
4209 * to regmatch(), but they are here to reduce the amount of stack space used | |
4210 * (it can be called recursively many times). | |
4211 */ | |
4212 static long bl_minval; | |
4213 static long bl_maxval; | |
4214 | |
4215 /* | |
4216 * regmatch - main matching routine | |
4217 * | |
180 | 4218 * Conceptually the strategy is simple: Check to see whether the current node |
4219 * matches, push an item onto the regstack and loop to see whether the rest | |
4220 * matches, and then act accordingly. In practice we make some effort to | |
4221 * avoid using the regstack, in particular by going through "ordinary" nodes | |
4222 * (that don't need to know whether the rest of the match failed) by a nested | |
4223 * loop. | |
7 | 4224 * |
4225 * Returns TRUE when there is a match. Leaves reginput and reglnum just after | |
4226 * the last matched character. | |
4227 * Returns FALSE when there is no match. Leaves reginput and reglnum in an | |
4228 * undefined state! | |
4229 */ | |
4230 static int | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
4231 regmatch( |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
4232 char_u *scan) /* Current node. */ |
7 | 4233 { |
180 | 4234 char_u *next; /* Next node. */ |
4235 int op; | |
4236 int c; | |
4237 regitem_T *rp; | |
4238 int no; | |
4239 int status; /* one of the RA_ values: */ | |
4240 #define RA_FAIL 1 /* something failed, abort */ | |
4241 #define RA_CONT 2 /* continue in inner loop */ | |
4242 #define RA_BREAK 3 /* break inner loop */ | |
4243 #define RA_MATCH 4 /* successful match */ | |
4244 #define RA_NOMATCH 5 /* didn't match */ | |
270 | 4245 |
1520 | 4246 /* Make "regstack" and "backpos" empty. They are allocated and freed in |
4444 | 4247 * bt_regexec_both() to reduce malloc()/free() calls. */ |
270 | 4248 regstack.ga_len = 0; |
4249 backpos.ga_len = 0; | |
233 | 4250 |
180 | 4251 /* |
233 | 4252 * Repeat until "regstack" is empty. |
180 | 4253 */ |
4254 for (;;) | |
4255 { | |
5310 | 4256 /* Some patterns may take a long time to match, e.g., "\([a-z]\+\)\+Q". |
4257 * Allow interrupting them with CTRL-C. */ | |
7 | 4258 fast_breakcheck(); |
4259 | |
4260 #ifdef DEBUG | |
4261 if (scan != NULL && regnarrate) | |
4262 { | |
4444 | 4263 mch_errmsg((char *)regprop(scan)); |
7 | 4264 mch_errmsg("(\n"); |
4265 } | |
4266 #endif | |
180 | 4267 |
4268 /* | |
233 | 4269 * Repeat for items that can be matched sequentially, without using the |
180 | 4270 * regstack. |
4271 */ | |
4272 for (;;) | |
7 | 4273 { |
180 | 4274 if (got_int || scan == NULL) |
4275 { | |
4276 status = RA_FAIL; | |
4277 break; | |
4278 } | |
4279 status = RA_CONT; | |
4280 | |
7 | 4281 #ifdef DEBUG |
4282 if (regnarrate) | |
4283 { | |
4444 | 4284 mch_errmsg((char *)regprop(scan)); |
7 | 4285 mch_errmsg("...\n"); |
4286 # ifdef FEAT_SYN_HL | |
4287 if (re_extmatch_in != NULL) | |
4288 { | |
4289 int i; | |
4290 | |
4291 mch_errmsg(_("External submatches:\n")); | |
4292 for (i = 0; i < NSUBEXP; i++) | |
4293 { | |
4294 mch_errmsg(" \""); | |
4295 if (re_extmatch_in->matches[i] != NULL) | |
4444 | 4296 mch_errmsg((char *)re_extmatch_in->matches[i]); |
7 | 4297 mch_errmsg("\"\n"); |
4298 } | |
4299 } | |
4300 # endif | |
4301 } | |
4302 #endif | |
4303 next = regnext(scan); | |
4304 | |
4305 op = OP(scan); | |
4306 /* Check for character class with NL added. */ | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4307 if (!rex.reg_line_lbr && WITH_NL(op) && REG_MULTI |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4308 && *reginput == NUL && reglnum <= rex.reg_maxline) |
7 | 4309 { |
4310 reg_nextline(); | |
4311 } | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4312 else if (rex.reg_line_lbr && WITH_NL(op) && *reginput == '\n') |
7 | 4313 { |
4314 ADVANCE_REGINPUT(); | |
4315 } | |
4316 else | |
4317 { | |
4318 if (WITH_NL(op)) | |
180 | 4319 op -= ADD_NL; |
7 | 4320 #ifdef FEAT_MBYTE |
4321 if (has_mbyte) | |
4322 c = (*mb_ptr2char)(reginput); | |
4323 else | |
4324 #endif | |
4325 c = *reginput; | |
4326 switch (op) | |
4327 { | |
4328 case BOL: | |
4329 if (reginput != regline) | |
180 | 4330 status = RA_NOMATCH; |
7 | 4331 break; |
4332 | |
4333 case EOL: | |
4334 if (c != NUL) | |
180 | 4335 status = RA_NOMATCH; |
7 | 4336 break; |
4337 | |
4338 case RE_BOF: | |
1458 | 4339 /* We're not at the beginning of the file when below the first |
4340 * line where we started, not at the start of the line or we | |
4341 * didn't start at the first line of the buffer. */ | |
7 | 4342 if (reglnum != 0 || reginput != regline |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4343 || (REG_MULTI && rex.reg_firstlnum > 1)) |
180 | 4344 status = RA_NOMATCH; |
7 | 4345 break; |
4346 | |
4347 case RE_EOF: | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4348 if (reglnum != rex.reg_maxline || c != NUL) |
180 | 4349 status = RA_NOMATCH; |
7 | 4350 break; |
4351 | |
4352 case CURSOR: | |
4353 /* Check if the buffer is in a window and compare the | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4354 * rex.reg_win->w_cursor position to the match position. */ |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4355 if (rex.reg_win == NULL |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4356 || (reglnum + rex.reg_firstlnum |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4357 != rex.reg_win->w_cursor.lnum) |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4358 || ((colnr_T)(reginput - regline) |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4359 != rex.reg_win->w_cursor.col)) |
180 | 4360 status = RA_NOMATCH; |
7 | 4361 break; |
4362 | |
639 | 4363 case RE_MARK: |
4732
0798b096bab3
updated for version 7.3.1113
Bram Moolenaar <bram@vim.org>
parents:
4730
diff
changeset
|
4364 /* Compare the mark position to the match position. */ |
639 | 4365 { |
4366 int mark = OPERAND(scan)[0]; | |
4367 int cmp = OPERAND(scan)[1]; | |
4368 pos_T *pos; | |
4369 | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4370 pos = getmark_buf(rex.reg_buf, mark, FALSE); |
1148 | 4371 if (pos == NULL /* mark doesn't exist */ |
4732
0798b096bab3
updated for version 7.3.1113
Bram Moolenaar <bram@vim.org>
parents:
4730
diff
changeset
|
4372 || pos->lnum <= 0 /* mark isn't set in reg_buf */ |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4373 || (pos->lnum == reglnum + rex.reg_firstlnum |
639 | 4374 ? (pos->col == (colnr_T)(reginput - regline) |
4375 ? (cmp == '<' || cmp == '>') | |
4376 : (pos->col < (colnr_T)(reginput - regline) | |
4377 ? cmp != '>' | |
4378 : cmp != '<')) | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4379 : (pos->lnum < reglnum + rex.reg_firstlnum |
639 | 4380 ? cmp != '>' |
4381 : cmp != '<'))) | |
4382 status = RA_NOMATCH; | |
4383 } | |
4384 break; | |
4385 | |
4386 case RE_VISUAL: | |
4730
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4387 if (!reg_match_visual()) |
639 | 4388 status = RA_NOMATCH; |
4389 break; | |
4390 | |
7 | 4391 case RE_LNUM: |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4392 if (!REG_MULTI || !re_num_cmp((long_u)(reglnum + rex.reg_firstlnum), |
7 | 4393 scan)) |
180 | 4394 status = RA_NOMATCH; |
7 | 4395 break; |
4396 | |
4397 case RE_COL: | |
4398 if (!re_num_cmp((long_u)(reginput - regline) + 1, scan)) | |
180 | 4399 status = RA_NOMATCH; |
7 | 4400 break; |
4401 | |
4402 case RE_VCOL: | |
4403 if (!re_num_cmp((long_u)win_linetabsize( | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4404 rex.reg_win == NULL ? curwin : rex.reg_win, |
7 | 4405 regline, (colnr_T)(reginput - regline)) + 1, scan)) |
180 | 4406 status = RA_NOMATCH; |
7 | 4407 break; |
4408 | |
4409 case BOW: /* \<word; reginput points to w */ | |
4410 if (c == NUL) /* Can't match at end of line */ | |
180 | 4411 status = RA_NOMATCH; |
7 | 4412 #ifdef FEAT_MBYTE |
180 | 4413 else if (has_mbyte) |
7 | 4414 { |
4415 int this_class; | |
4416 | |
4417 /* Get class of current and previous char (if it exists). */ | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4418 this_class = mb_get_class_buf(reginput, rex.reg_buf); |
7 | 4419 if (this_class <= 1) |
180 | 4420 status = RA_NOMATCH; /* not on a word at all */ |
4421 else if (reg_prev_class() == this_class) | |
4422 status = RA_NOMATCH; /* previous char is in same word */ | |
7 | 4423 } |
4424 #endif | |
4425 else | |
4426 { | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4427 if (!vim_iswordc_buf(c, rex.reg_buf) || (reginput > regline |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4428 && vim_iswordc_buf(reginput[-1], rex.reg_buf))) |
180 | 4429 status = RA_NOMATCH; |
7 | 4430 } |
4431 break; | |
4432 | |
4433 case EOW: /* word\>; reginput points after d */ | |
4434 if (reginput == regline) /* Can't match at start of line */ | |
180 | 4435 status = RA_NOMATCH; |
7 | 4436 #ifdef FEAT_MBYTE |
180 | 4437 else if (has_mbyte) |
7 | 4438 { |
4439 int this_class, prev_class; | |
4440 | |
4441 /* Get class of current and previous char (if it exists). */ | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4442 this_class = mb_get_class_buf(reginput, rex.reg_buf); |
7 | 4443 prev_class = reg_prev_class(); |
180 | 4444 if (this_class == prev_class |
4445 || prev_class == 0 || prev_class == 1) | |
4446 status = RA_NOMATCH; | |
7 | 4447 } |
180 | 4448 #endif |
7 | 4449 else |
4450 { | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4451 if (!vim_iswordc_buf(reginput[-1], rex.reg_buf) |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4452 || (reginput[0] != NUL |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4453 && vim_iswordc_buf(c, rex.reg_buf))) |
180 | 4454 status = RA_NOMATCH; |
7 | 4455 } |
4456 break; /* Matched with EOW */ | |
4457 | |
4458 case ANY: | |
4084 | 4459 /* ANY does not match new lines. */ |
7 | 4460 if (c == NUL) |
180 | 4461 status = RA_NOMATCH; |
4462 else | |
4463 ADVANCE_REGINPUT(); | |
7 | 4464 break; |
4465 | |
4466 case IDENT: | |
4467 if (!vim_isIDc(c)) | |
180 | 4468 status = RA_NOMATCH; |
4469 else | |
4470 ADVANCE_REGINPUT(); | |
7 | 4471 break; |
4472 | |
4473 case SIDENT: | |
4474 if (VIM_ISDIGIT(*reginput) || !vim_isIDc(c)) | |
180 | 4475 status = RA_NOMATCH; |
4476 else | |
4477 ADVANCE_REGINPUT(); | |
7 | 4478 break; |
4479 | |
4480 case KWORD: | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4481 if (!vim_iswordp_buf(reginput, rex.reg_buf)) |
180 | 4482 status = RA_NOMATCH; |
4483 else | |
4484 ADVANCE_REGINPUT(); | |
7 | 4485 break; |
4486 | |
4487 case SKWORD: | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4488 if (VIM_ISDIGIT(*reginput) |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4489 || !vim_iswordp_buf(reginput, rex.reg_buf)) |
180 | 4490 status = RA_NOMATCH; |
4491 else | |
4492 ADVANCE_REGINPUT(); | |
7 | 4493 break; |
4494 | |
4495 case FNAME: | |
4496 if (!vim_isfilec(c)) | |
180 | 4497 status = RA_NOMATCH; |
4498 else | |
4499 ADVANCE_REGINPUT(); | |
7 | 4500 break; |
4501 | |
4502 case SFNAME: | |
4503 if (VIM_ISDIGIT(*reginput) || !vim_isfilec(c)) | |
180 | 4504 status = RA_NOMATCH; |
4505 else | |
4506 ADVANCE_REGINPUT(); | |
7 | 4507 break; |
4508 | |
4509 case PRINT: | |
5221
9982ec574beb
updated for version 7.4a.036
Bram Moolenaar <bram@vim.org>
parents:
4901
diff
changeset
|
4510 if (!vim_isprintc(PTR2CHAR(reginput))) |
180 | 4511 status = RA_NOMATCH; |
4512 else | |
4513 ADVANCE_REGINPUT(); | |
7 | 4514 break; |
4515 | |
4516 case SPRINT: | |
5221
9982ec574beb
updated for version 7.4a.036
Bram Moolenaar <bram@vim.org>
parents:
4901
diff
changeset
|
4517 if (VIM_ISDIGIT(*reginput) || !vim_isprintc(PTR2CHAR(reginput))) |
180 | 4518 status = RA_NOMATCH; |
4519 else | |
4520 ADVANCE_REGINPUT(); | |
7 | 4521 break; |
4522 | |
4523 case WHITE: | |
4524 if (!vim_iswhite(c)) | |
180 | 4525 status = RA_NOMATCH; |
4526 else | |
4527 ADVANCE_REGINPUT(); | |
7 | 4528 break; |
4529 | |
4530 case NWHITE: | |
4531 if (c == NUL || vim_iswhite(c)) | |
180 | 4532 status = RA_NOMATCH; |
4533 else | |
4534 ADVANCE_REGINPUT(); | |
7 | 4535 break; |
4536 | |
4537 case DIGIT: | |
4538 if (!ri_digit(c)) | |
180 | 4539 status = RA_NOMATCH; |
4540 else | |
4541 ADVANCE_REGINPUT(); | |
7 | 4542 break; |
4543 | |
4544 case NDIGIT: | |
4545 if (c == NUL || ri_digit(c)) | |
180 | 4546 status = RA_NOMATCH; |
4547 else | |
4548 ADVANCE_REGINPUT(); | |
7 | 4549 break; |
4550 | |
4551 case HEX: | |
4552 if (!ri_hex(c)) | |
180 | 4553 status = RA_NOMATCH; |
4554 else | |
4555 ADVANCE_REGINPUT(); | |
7 | 4556 break; |
4557 | |
4558 case NHEX: | |
4559 if (c == NUL || ri_hex(c)) | |
180 | 4560 status = RA_NOMATCH; |
4561 else | |
4562 ADVANCE_REGINPUT(); | |
7 | 4563 break; |
4564 | |
4565 case OCTAL: | |
4566 if (!ri_octal(c)) | |
180 | 4567 status = RA_NOMATCH; |
4568 else | |
4569 ADVANCE_REGINPUT(); | |
7 | 4570 break; |
4571 | |
4572 case NOCTAL: | |
4573 if (c == NUL || ri_octal(c)) | |
180 | 4574 status = RA_NOMATCH; |
4575 else | |
4576 ADVANCE_REGINPUT(); | |
7 | 4577 break; |
4578 | |
4579 case WORD: | |
4580 if (!ri_word(c)) | |
180 | 4581 status = RA_NOMATCH; |
4582 else | |
4583 ADVANCE_REGINPUT(); | |
7 | 4584 break; |
4585 | |
4586 case NWORD: | |
4587 if (c == NUL || ri_word(c)) | |
180 | 4588 status = RA_NOMATCH; |
4589 else | |
4590 ADVANCE_REGINPUT(); | |
7 | 4591 break; |
4592 | |
4593 case HEAD: | |
4594 if (!ri_head(c)) | |
180 | 4595 status = RA_NOMATCH; |
4596 else | |
4597 ADVANCE_REGINPUT(); | |
7 | 4598 break; |
4599 | |
4600 case NHEAD: | |
4601 if (c == NUL || ri_head(c)) | |
180 | 4602 status = RA_NOMATCH; |
4603 else | |
4604 ADVANCE_REGINPUT(); | |
7 | 4605 break; |
4606 | |
4607 case ALPHA: | |
4608 if (!ri_alpha(c)) | |
180 | 4609 status = RA_NOMATCH; |
4610 else | |
4611 ADVANCE_REGINPUT(); | |
7 | 4612 break; |
4613 | |
4614 case NALPHA: | |
4615 if (c == NUL || ri_alpha(c)) | |
180 | 4616 status = RA_NOMATCH; |
4617 else | |
4618 ADVANCE_REGINPUT(); | |
7 | 4619 break; |
4620 | |
4621 case LOWER: | |
4622 if (!ri_lower(c)) | |
180 | 4623 status = RA_NOMATCH; |
4624 else | |
4625 ADVANCE_REGINPUT(); | |
7 | 4626 break; |
4627 | |
4628 case NLOWER: | |
4629 if (c == NUL || ri_lower(c)) | |
180 | 4630 status = RA_NOMATCH; |
4631 else | |
4632 ADVANCE_REGINPUT(); | |
7 | 4633 break; |
4634 | |
4635 case UPPER: | |
4636 if (!ri_upper(c)) | |
180 | 4637 status = RA_NOMATCH; |
4638 else | |
4639 ADVANCE_REGINPUT(); | |
7 | 4640 break; |
4641 | |
4642 case NUPPER: | |
4643 if (c == NUL || ri_upper(c)) | |
180 | 4644 status = RA_NOMATCH; |
4645 else | |
4646 ADVANCE_REGINPUT(); | |
7 | 4647 break; |
4648 | |
4649 case EXACTLY: | |
4650 { | |
4651 int len; | |
4652 char_u *opnd; | |
4653 | |
4654 opnd = OPERAND(scan); | |
4655 /* Inline the first byte, for speed. */ | |
4656 if (*opnd != *reginput | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4657 && (!rex.reg_ic || ( |
7 | 4658 #ifdef FEAT_MBYTE |
4659 !enc_utf8 && | |
4660 #endif | |
1347 | 4661 MB_TOLOWER(*opnd) != MB_TOLOWER(*reginput)))) |
180 | 4662 status = RA_NOMATCH; |
4663 else if (*opnd == NUL) | |
7 | 4664 { |
4665 /* match empty string always works; happens when "~" is | |
4666 * empty. */ | |
4667 } | |
5899 | 4668 else |
4669 { | |
4670 if (opnd[1] == NUL | |
7 | 4671 #ifdef FEAT_MBYTE |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4672 && !(enc_utf8 && rex.reg_ic) |
7 | 4673 #endif |
4674 ) | |
5899 | 4675 { |
4676 len = 1; /* matched a single byte above */ | |
4677 } | |
4678 else | |
4679 { | |
4680 /* Need to match first byte again for multi-byte. */ | |
4681 len = (int)STRLEN(opnd); | |
4682 if (cstrncmp(opnd, reginput, &len) != 0) | |
4683 status = RA_NOMATCH; | |
4684 } | |
7 | 4685 #ifdef FEAT_MBYTE |
5901 | 4686 /* Check for following composing character, unless %C |
4687 * follows (skips over all composing chars). */ | |
5899 | 4688 if (status != RA_NOMATCH |
4689 && enc_utf8 | |
4690 && UTF_COMPOSINGLIKE(reginput, reginput + len) | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4691 && !rex.reg_icombine |
5901 | 4692 && OP(next) != RE_COMPOSING) |
7 | 4693 { |
4694 /* raaron: This code makes a composing character get | |
4695 * ignored, which is the correct behavior (sometimes) | |
4696 * for voweled Hebrew texts. */ | |
5899 | 4697 status = RA_NOMATCH; |
7 | 4698 } |
180 | 4699 #endif |
5899 | 4700 if (status != RA_NOMATCH) |
180 | 4701 reginput += len; |
7 | 4702 } |
4703 } | |
4704 break; | |
4705 | |
4706 case ANYOF: | |
4707 case ANYBUT: | |
4708 if (c == NUL) | |
180 | 4709 status = RA_NOMATCH; |
4710 else if ((cstrchr(OPERAND(scan), c) == NULL) == (op == ANYOF)) | |
4711 status = RA_NOMATCH; | |
4712 else | |
4713 ADVANCE_REGINPUT(); | |
7 | 4714 break; |
4715 | |
4716 #ifdef FEAT_MBYTE | |
4717 case MULTIBYTECODE: | |
4718 if (has_mbyte) | |
4719 { | |
4720 int i, len; | |
4721 char_u *opnd; | |
944 | 4722 int opndc = 0, inpc; |
7 | 4723 |
4724 opnd = OPERAND(scan); | |
4725 /* Safety check (just in case 'encoding' was changed since | |
4726 * compiling the program). */ | |
474 | 4727 if ((len = (*mb_ptr2len)(opnd)) < 2) |
180 | 4728 { |
4729 status = RA_NOMATCH; | |
4730 break; | |
4731 } | |
714 | 4732 if (enc_utf8) |
4733 opndc = mb_ptr2char(opnd); | |
4734 if (enc_utf8 && utf_iscomposing(opndc)) | |
4735 { | |
4736 /* When only a composing char is given match at any | |
4737 * position where that composing char appears. */ | |
4738 status = RA_NOMATCH; | |
6723 | 4739 for (i = 0; reginput[i] != NUL; |
4740 i += utf_ptr2len(reginput + i)) | |
180 | 4741 { |
714 | 4742 inpc = mb_ptr2char(reginput + i); |
4743 if (!utf_iscomposing(inpc)) | |
4744 { | |
4745 if (i > 0) | |
4746 break; | |
4747 } | |
4748 else if (opndc == inpc) | |
4749 { | |
4750 /* Include all following composing chars. */ | |
4751 len = i + mb_ptr2len(reginput + i); | |
4752 status = RA_MATCH; | |
4753 break; | |
4754 } | |
180 | 4755 } |
714 | 4756 } |
4757 else | |
4758 for (i = 0; i < len; ++i) | |
4759 if (opnd[i] != reginput[i]) | |
4760 { | |
4761 status = RA_NOMATCH; | |
4762 break; | |
4763 } | |
7 | 4764 reginput += len; |
4765 } | |
4766 else | |
180 | 4767 status = RA_NOMATCH; |
7 | 4768 break; |
4769 #endif | |
5901 | 4770 case RE_COMPOSING: |
4771 #ifdef FEAT_MBYTE | |
4772 if (enc_utf8) | |
4773 { | |
4774 /* Skip composing characters. */ | |
4775 while (utf_iscomposing(utf_ptr2char(reginput))) | |
4776 mb_cptr_adv(reginput); | |
4777 } | |
4778 #endif | |
4779 break; | |
7 | 4780 |
4781 case NOTHING: | |
4782 break; | |
4783 | |
4784 case BACK: | |
233 | 4785 { |
4786 int i; | |
4787 backpos_T *bp; | |
4788 | |
4789 /* | |
4790 * When we run into BACK we need to check if we don't keep | |
4791 * looping without matching any input. The second and later | |
4792 * times a BACK is encountered it fails if the input is still | |
4793 * at the same position as the previous time. | |
4794 * The positions are stored in "backpos" and found by the | |
4795 * current value of "scan", the position in the RE program. | |
4796 */ | |
4797 bp = (backpos_T *)backpos.ga_data; | |
4798 for (i = 0; i < backpos.ga_len; ++i) | |
4799 if (bp[i].bp_scan == scan) | |
4800 break; | |
4801 if (i == backpos.ga_len) | |
4802 { | |
4803 /* First time at this BACK, make room to store the pos. */ | |
4804 if (ga_grow(&backpos, 1) == FAIL) | |
4805 status = RA_FAIL; | |
4806 else | |
4807 { | |
4808 /* get "ga_data" again, it may have changed */ | |
4809 bp = (backpos_T *)backpos.ga_data; | |
4810 bp[i].bp_scan = scan; | |
4811 ++backpos.ga_len; | |
4812 } | |
4813 } | |
4814 else if (reg_save_equal(&bp[i].bp_pos)) | |
4815 /* Still at same position as last time, fail. */ | |
4816 status = RA_NOMATCH; | |
4817 | |
4818 if (status != RA_FAIL && status != RA_NOMATCH) | |
4819 reg_save(&bp[i].bp_pos, &backpos); | |
4820 } | |
179 | 4821 break; |
4822 | |
7 | 4823 case MOPEN + 0: /* Match start: \zs */ |
4824 case MOPEN + 1: /* \( */ | |
4825 case MOPEN + 2: | |
4826 case MOPEN + 3: | |
4827 case MOPEN + 4: | |
4828 case MOPEN + 5: | |
4829 case MOPEN + 6: | |
4830 case MOPEN + 7: | |
4831 case MOPEN + 8: | |
4832 case MOPEN + 9: | |
4833 { | |
4834 no = op - MOPEN; | |
4835 cleanup_subexpr(); | |
270 | 4836 rp = regstack_push(RS_MOPEN, scan); |
180 | 4837 if (rp == NULL) |
4838 status = RA_FAIL; | |
4839 else | |
4840 { | |
4841 rp->rs_no = no; | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4842 save_se(&rp->rs_un.sesave, &rex.reg_startpos[no], |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4843 &rex.reg_startp[no]); |
180 | 4844 /* We simply continue and handle the result when done. */ |
4845 } | |
7 | 4846 } |
180 | 4847 break; |
7 | 4848 |
4849 case NOPEN: /* \%( */ | |
4850 case NCLOSE: /* \) after \%( */ | |
270 | 4851 if (regstack_push(RS_NOPEN, scan) == NULL) |
180 | 4852 status = RA_FAIL; |
4853 /* We simply continue and handle the result when done. */ | |
4854 break; | |
7 | 4855 |
4856 #ifdef FEAT_SYN_HL | |
4857 case ZOPEN + 1: | |
4858 case ZOPEN + 2: | |
4859 case ZOPEN + 3: | |
4860 case ZOPEN + 4: | |
4861 case ZOPEN + 5: | |
4862 case ZOPEN + 6: | |
4863 case ZOPEN + 7: | |
4864 case ZOPEN + 8: | |
4865 case ZOPEN + 9: | |
4866 { | |
4867 no = op - ZOPEN; | |
4868 cleanup_zsubexpr(); | |
270 | 4869 rp = regstack_push(RS_ZOPEN, scan); |
180 | 4870 if (rp == NULL) |
4871 status = RA_FAIL; | |
4872 else | |
4873 { | |
4874 rp->rs_no = no; | |
4875 save_se(&rp->rs_un.sesave, ®_startzpos[no], | |
4876 ®_startzp[no]); | |
4877 /* We simply continue and handle the result when done. */ | |
4878 } | |
7 | 4879 } |
180 | 4880 break; |
7 | 4881 #endif |
4882 | |
4883 case MCLOSE + 0: /* Match end: \ze */ | |
4884 case MCLOSE + 1: /* \) */ | |
4885 case MCLOSE + 2: | |
4886 case MCLOSE + 3: | |
4887 case MCLOSE + 4: | |
4888 case MCLOSE + 5: | |
4889 case MCLOSE + 6: | |
4890 case MCLOSE + 7: | |
4891 case MCLOSE + 8: | |
4892 case MCLOSE + 9: | |
4893 { | |
4894 no = op - MCLOSE; | |
4895 cleanup_subexpr(); | |
270 | 4896 rp = regstack_push(RS_MCLOSE, scan); |
180 | 4897 if (rp == NULL) |
4898 status = RA_FAIL; | |
4899 else | |
4900 { | |
4901 rp->rs_no = no; | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4902 save_se(&rp->rs_un.sesave, &rex.reg_endpos[no], |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4903 &rex.reg_endp[no]); |
180 | 4904 /* We simply continue and handle the result when done. */ |
4905 } | |
7 | 4906 } |
180 | 4907 break; |
7 | 4908 |
4909 #ifdef FEAT_SYN_HL | |
4910 case ZCLOSE + 1: /* \) after \z( */ | |
4911 case ZCLOSE + 2: | |
4912 case ZCLOSE + 3: | |
4913 case ZCLOSE + 4: | |
4914 case ZCLOSE + 5: | |
4915 case ZCLOSE + 6: | |
4916 case ZCLOSE + 7: | |
4917 case ZCLOSE + 8: | |
4918 case ZCLOSE + 9: | |
4919 { | |
4920 no = op - ZCLOSE; | |
4921 cleanup_zsubexpr(); | |
270 | 4922 rp = regstack_push(RS_ZCLOSE, scan); |
180 | 4923 if (rp == NULL) |
4924 status = RA_FAIL; | |
4925 else | |
4926 { | |
4927 rp->rs_no = no; | |
4928 save_se(&rp->rs_un.sesave, ®_endzpos[no], | |
4929 ®_endzp[no]); | |
4930 /* We simply continue and handle the result when done. */ | |
4931 } | |
7 | 4932 } |
180 | 4933 break; |
7 | 4934 #endif |
4935 | |
4936 case BACKREF + 1: | |
4937 case BACKREF + 2: | |
4938 case BACKREF + 3: | |
4939 case BACKREF + 4: | |
4940 case BACKREF + 5: | |
4941 case BACKREF + 6: | |
4942 case BACKREF + 7: | |
4943 case BACKREF + 8: | |
4944 case BACKREF + 9: | |
4945 { | |
4946 int len; | |
4947 | |
4948 no = op - BACKREF; | |
4949 cleanup_subexpr(); | |
4950 if (!REG_MULTI) /* Single-line regexp */ | |
4951 { | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4952 if (rex.reg_startp[no] == NULL || rex.reg_endp[no] == NULL) |
7 | 4953 { |
4954 /* Backref was not set: Match an empty string. */ | |
4955 len = 0; | |
4956 } | |
4957 else | |
4958 { | |
4959 /* Compare current input with back-ref in the same | |
4960 * line. */ | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4961 len = (int)(rex.reg_endp[no] - rex.reg_startp[no]); |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4962 if (cstrncmp(rex.reg_startp[no], reginput, &len) != 0) |
180 | 4963 status = RA_NOMATCH; |
7 | 4964 } |
4965 } | |
4966 else /* Multi-line regexp */ | |
4967 { | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4968 if (rex.reg_startpos[no].lnum < 0 |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4969 || rex.reg_endpos[no].lnum < 0) |
7 | 4970 { |
4971 /* Backref was not set: Match an empty string. */ | |
4972 len = 0; | |
4973 } | |
4974 else | |
4975 { | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4976 if (rex.reg_startpos[no].lnum == reglnum |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4977 && rex.reg_endpos[no].lnum == reglnum) |
7 | 4978 { |
4979 /* Compare back-ref within the current line. */ | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4980 len = rex.reg_endpos[no].col |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4981 - rex.reg_startpos[no].col; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4982 if (cstrncmp(regline + rex.reg_startpos[no].col, |
7 | 4983 reginput, &len) != 0) |
180 | 4984 status = RA_NOMATCH; |
7 | 4985 } |
4986 else | |
4987 { | |
4988 /* Messy situation: Need to compare between two | |
4989 * lines. */ | |
4901
56fbf60e9476
updated for version 7.3.1196
Bram Moolenaar <bram@vim.org>
parents:
4899
diff
changeset
|
4990 int r = match_with_backref( |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4991 rex.reg_startpos[no].lnum, |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4992 rex.reg_startpos[no].col, |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4993 rex.reg_endpos[no].lnum, |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
4994 rex.reg_endpos[no].col, |
4899
4837fd61be52
updated for version 7.3.1195
Bram Moolenaar <bram@vim.org>
parents:
4891
diff
changeset
|
4995 &len); |
4901
56fbf60e9476
updated for version 7.3.1196
Bram Moolenaar <bram@vim.org>
parents:
4899
diff
changeset
|
4996 |
56fbf60e9476
updated for version 7.3.1196
Bram Moolenaar <bram@vim.org>
parents:
4899
diff
changeset
|
4997 if (r != RA_MATCH) |
56fbf60e9476
updated for version 7.3.1196
Bram Moolenaar <bram@vim.org>
parents:
4899
diff
changeset
|
4998 status = r; |
7 | 4999 } |
5000 } | |
5001 } | |
5002 | |
5003 /* Matched the backref, skip over it. */ | |
5004 reginput += len; | |
5005 } | |
5006 break; | |
5007 | |
5008 #ifdef FEAT_SYN_HL | |
5009 case ZREF + 1: | |
5010 case ZREF + 2: | |
5011 case ZREF + 3: | |
5012 case ZREF + 4: | |
5013 case ZREF + 5: | |
5014 case ZREF + 6: | |
5015 case ZREF + 7: | |
5016 case ZREF + 8: | |
5017 case ZREF + 9: | |
5018 { | |
5019 int len; | |
5020 | |
5021 cleanup_zsubexpr(); | |
5022 no = op - ZREF; | |
5023 if (re_extmatch_in != NULL | |
5024 && re_extmatch_in->matches[no] != NULL) | |
5025 { | |
5026 len = (int)STRLEN(re_extmatch_in->matches[no]); | |
5027 if (cstrncmp(re_extmatch_in->matches[no], | |
5028 reginput, &len) != 0) | |
180 | 5029 status = RA_NOMATCH; |
5030 else | |
5031 reginput += len; | |
7 | 5032 } |
5033 else | |
5034 { | |
5035 /* Backref was not set: Match an empty string. */ | |
5036 } | |
5037 } | |
5038 break; | |
5039 #endif | |
5040 | |
5041 case BRANCH: | |
5042 { | |
5043 if (OP(next) != BRANCH) /* No choice. */ | |
5044 next = OPERAND(scan); /* Avoid recursion. */ | |
5045 else | |
5046 { | |
270 | 5047 rp = regstack_push(RS_BRANCH, scan); |
180 | 5048 if (rp == NULL) |
5049 status = RA_FAIL; | |
5050 else | |
5051 status = RA_BREAK; /* rest is below */ | |
7 | 5052 } |
5053 } | |
5054 break; | |
5055 | |
5056 case BRACE_LIMITS: | |
5057 { | |
5058 if (OP(next) == BRACE_SIMPLE) | |
5059 { | |
5060 bl_minval = OPERAND_MIN(scan); | |
5061 bl_maxval = OPERAND_MAX(scan); | |
5062 } | |
5063 else if (OP(next) >= BRACE_COMPLEX | |
5064 && OP(next) < BRACE_COMPLEX + 10) | |
5065 { | |
5066 no = OP(next) - BRACE_COMPLEX; | |
5067 brace_min[no] = OPERAND_MIN(scan); | |
5068 brace_max[no] = OPERAND_MAX(scan); | |
5069 brace_count[no] = 0; | |
5070 } | |
5071 else | |
5072 { | |
5073 EMSG(_(e_internal)); /* Shouldn't happen */ | |
180 | 5074 status = RA_FAIL; |
7 | 5075 } |
5076 } | |
5077 break; | |
5078 | |
5079 case BRACE_COMPLEX + 0: | |
5080 case BRACE_COMPLEX + 1: | |
5081 case BRACE_COMPLEX + 2: | |
5082 case BRACE_COMPLEX + 3: | |
5083 case BRACE_COMPLEX + 4: | |
5084 case BRACE_COMPLEX + 5: | |
5085 case BRACE_COMPLEX + 6: | |
5086 case BRACE_COMPLEX + 7: | |
5087 case BRACE_COMPLEX + 8: | |
5088 case BRACE_COMPLEX + 9: | |
5089 { | |
5090 no = op - BRACE_COMPLEX; | |
5091 ++brace_count[no]; | |
5092 | |
5093 /* If not matched enough times yet, try one more */ | |
5094 if (brace_count[no] <= (brace_min[no] <= brace_max[no] | |
180 | 5095 ? brace_min[no] : brace_max[no])) |
7 | 5096 { |
270 | 5097 rp = regstack_push(RS_BRCPLX_MORE, scan); |
180 | 5098 if (rp == NULL) |
5099 status = RA_FAIL; | |
5100 else | |
5101 { | |
5102 rp->rs_no = no; | |
233 | 5103 reg_save(&rp->rs_un.regsave, &backpos); |
180 | 5104 next = OPERAND(scan); |
5105 /* We continue and handle the result when done. */ | |
5106 } | |
5107 break; | |
7 | 5108 } |
5109 | |
5110 /* If matched enough times, may try matching some more */ | |
5111 if (brace_min[no] <= brace_max[no]) | |
5112 { | |
5113 /* Range is the normal way around, use longest match */ | |
5114 if (brace_count[no] <= brace_max[no]) | |
5115 { | |
270 | 5116 rp = regstack_push(RS_BRCPLX_LONG, scan); |
180 | 5117 if (rp == NULL) |
5118 status = RA_FAIL; | |
5119 else | |
5120 { | |
5121 rp->rs_no = no; | |
233 | 5122 reg_save(&rp->rs_un.regsave, &backpos); |
180 | 5123 next = OPERAND(scan); |
5124 /* We continue and handle the result when done. */ | |
5125 } | |
7 | 5126 } |
5127 } | |
5128 else | |
5129 { | |
5130 /* Range is backwards, use shortest match first */ | |
5131 if (brace_count[no] <= brace_min[no]) | |
5132 { | |
270 | 5133 rp = regstack_push(RS_BRCPLX_SHORT, scan); |
180 | 5134 if (rp == NULL) |
5135 status = RA_FAIL; | |
5136 else | |
5137 { | |
233 | 5138 reg_save(&rp->rs_un.regsave, &backpos); |
180 | 5139 /* We continue and handle the result when done. */ |
5140 } | |
7 | 5141 } |
5142 } | |
5143 } | |
5144 break; | |
5145 | |
5146 case BRACE_SIMPLE: | |
5147 case STAR: | |
5148 case PLUS: | |
5149 { | |
180 | 5150 regstar_T rst; |
7 | 5151 |
5152 /* | |
5153 * Lookahead to avoid useless match attempts when we know | |
5154 * what character comes next. | |
5155 */ | |
5156 if (OP(next) == EXACTLY) | |
5157 { | |
180 | 5158 rst.nextb = *OPERAND(next); |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
5159 if (rex.reg_ic) |
7 | 5160 { |
1347 | 5161 if (MB_ISUPPER(rst.nextb)) |
5162 rst.nextb_ic = MB_TOLOWER(rst.nextb); | |
7 | 5163 else |
1347 | 5164 rst.nextb_ic = MB_TOUPPER(rst.nextb); |
7 | 5165 } |
5166 else | |
180 | 5167 rst.nextb_ic = rst.nextb; |
7 | 5168 } |
5169 else | |
5170 { | |
180 | 5171 rst.nextb = NUL; |
5172 rst.nextb_ic = NUL; | |
7 | 5173 } |
5174 if (op != BRACE_SIMPLE) | |
5175 { | |
180 | 5176 rst.minval = (op == STAR) ? 0 : 1; |
5177 rst.maxval = MAX_LIMIT; | |
7 | 5178 } |
5179 else | |
5180 { | |
180 | 5181 rst.minval = bl_minval; |
5182 rst.maxval = bl_maxval; | |
7 | 5183 } |
5184 | |
5185 /* | |
5186 * When maxval > minval, try matching as much as possible, up | |
5187 * to maxval. When maxval < minval, try matching at least the | |
5188 * minimal number (since the range is backwards, that's also | |
5189 * maxval!). | |
5190 */ | |
180 | 5191 rst.count = regrepeat(OPERAND(scan), rst.maxval); |
7 | 5192 if (got_int) |
180 | 5193 { |
5194 status = RA_FAIL; | |
5195 break; | |
5196 } | |
5197 if (rst.minval <= rst.maxval | |
5198 ? rst.count >= rst.minval : rst.count >= rst.maxval) | |
7 | 5199 { |
180 | 5200 /* It could match. Prepare for trying to match what |
5201 * follows. The code is below. Parameters are stored in | |
5202 * a regstar_T on the regstack. */ | |
212 | 5203 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp) |
189 | 5204 { |
5205 EMSG(_(e_maxmempat)); | |
5206 status = RA_FAIL; | |
5207 } | |
5208 else if (ga_grow(®stack, sizeof(regstar_T)) == FAIL) | |
180 | 5209 status = RA_FAIL; |
5210 else | |
7 | 5211 { |
180 | 5212 regstack.ga_len += sizeof(regstar_T); |
270 | 5213 rp = regstack_push(rst.minval <= rst.maxval |
233 | 5214 ? RS_STAR_LONG : RS_STAR_SHORT, scan); |
180 | 5215 if (rp == NULL) |
5216 status = RA_FAIL; | |
5217 else | |
7 | 5218 { |
180 | 5219 *(((regstar_T *)rp) - 1) = rst; |
5220 status = RA_BREAK; /* skip the restore bits */ | |
7 | 5221 } |
5222 } | |
5223 } | |
5224 else | |
180 | 5225 status = RA_NOMATCH; |
5226 | |
7 | 5227 } |
180 | 5228 break; |
7 | 5229 |
5230 case NOMATCH: | |
5231 case MATCH: | |
5232 case SUBPAT: | |
270 | 5233 rp = regstack_push(RS_NOMATCH, scan); |
180 | 5234 if (rp == NULL) |
5235 status = RA_FAIL; | |
5236 else | |
7 | 5237 { |
180 | 5238 rp->rs_no = op; |
233 | 5239 reg_save(&rp->rs_un.regsave, &backpos); |
180 | 5240 next = OPERAND(scan); |
5241 /* We continue and handle the result when done. */ | |
7 | 5242 } |
5243 break; | |
5244 | |
5245 case BEHIND: | |
5246 case NOBEHIND: | |
180 | 5247 /* Need a bit of room to store extra positions. */ |
212 | 5248 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp) |
189 | 5249 { |
5250 EMSG(_(e_maxmempat)); | |
5251 status = RA_FAIL; | |
5252 } | |
5253 else if (ga_grow(®stack, sizeof(regbehind_T)) == FAIL) | |
180 | 5254 status = RA_FAIL; |
5255 else | |
7 | 5256 { |
180 | 5257 regstack.ga_len += sizeof(regbehind_T); |
270 | 5258 rp = regstack_push(RS_BEHIND1, scan); |
180 | 5259 if (rp == NULL) |
5260 status = RA_FAIL; | |
5261 else | |
7 | 5262 { |
1579 | 5263 /* Need to save the subexpr to be able to restore them |
5264 * when there is a match but we don't use it. */ | |
5265 save_subexpr(((regbehind_T *)rp) - 1); | |
5266 | |
180 | 5267 rp->rs_no = op; |
233 | 5268 reg_save(&rp->rs_un.regsave, &backpos); |
180 | 5269 /* First try if what follows matches. If it does then we |
5270 * check the behind match by looping. */ | |
7 | 5271 } |
5272 } | |
180 | 5273 break; |
7 | 5274 |
5275 case BHPOS: | |
5276 if (REG_MULTI) | |
5277 { | |
5278 if (behind_pos.rs_u.pos.col != (colnr_T)(reginput - regline) | |
5279 || behind_pos.rs_u.pos.lnum != reglnum) | |
180 | 5280 status = RA_NOMATCH; |
7 | 5281 } |
5282 else if (behind_pos.rs_u.ptr != reginput) | |
180 | 5283 status = RA_NOMATCH; |
7 | 5284 break; |
5285 | |
5286 case NEWL: | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
5287 if ((c != NUL || !REG_MULTI || reglnum > rex.reg_maxline |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
5288 || rex.reg_line_lbr) |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
5289 && (c != '\n' || !rex.reg_line_lbr)) |
180 | 5290 status = RA_NOMATCH; |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
5291 else if (rex.reg_line_lbr) |
7 | 5292 ADVANCE_REGINPUT(); |
5293 else | |
5294 reg_nextline(); | |
5295 break; | |
5296 | |
5297 case END: | |
180 | 5298 status = RA_MATCH; /* Success! */ |
5299 break; | |
7 | 5300 |
5301 default: | |
5302 EMSG(_(e_re_corr)); | |
5303 #ifdef DEBUG | |
5304 printf("Illegal op code %d\n", op); | |
5305 #endif | |
180 | 5306 status = RA_FAIL; |
5307 break; | |
7 | 5308 } |
5309 } | |
5310 | |
180 | 5311 /* If we can't continue sequentially, break the inner loop. */ |
5312 if (status != RA_CONT) | |
5313 break; | |
5314 | |
5315 /* Continue in inner loop, advance to next item. */ | |
7 | 5316 scan = next; |
180 | 5317 |
5318 } /* end of inner loop */ | |
7 | 5319 |
5320 /* | |
180 | 5321 * If there is something on the regstack execute the code for the state. |
233 | 5322 * If the state is popped then loop and use the older state. |
7 | 5323 */ |
180 | 5324 while (regstack.ga_len > 0 && status != RA_FAIL) |
5325 { | |
5326 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len) - 1; | |
5327 switch (rp->rs_state) | |
5328 { | |
5329 case RS_NOPEN: | |
5330 /* Result is passed on as-is, simply pop the state. */ | |
270 | 5331 regstack_pop(&scan); |
180 | 5332 break; |
5333 | |
5334 case RS_MOPEN: | |
5335 /* Pop the state. Restore pointers when there is no match. */ | |
5336 if (status == RA_NOMATCH) | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
5337 restore_se(&rp->rs_un.sesave, &rex.reg_startpos[rp->rs_no], |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
5338 &rex.reg_startp[rp->rs_no]); |
270 | 5339 regstack_pop(&scan); |
180 | 5340 break; |
5341 | |
5342 #ifdef FEAT_SYN_HL | |
5343 case RS_ZOPEN: | |
5344 /* Pop the state. Restore pointers when there is no match. */ | |
5345 if (status == RA_NOMATCH) | |
5346 restore_se(&rp->rs_un.sesave, ®_startzpos[rp->rs_no], | |
5347 ®_startzp[rp->rs_no]); | |
270 | 5348 regstack_pop(&scan); |
180 | 5349 break; |
5350 #endif | |
5351 | |
5352 case RS_MCLOSE: | |
5353 /* Pop the state. Restore pointers when there is no match. */ | |
5354 if (status == RA_NOMATCH) | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
5355 restore_se(&rp->rs_un.sesave, &rex.reg_endpos[rp->rs_no], |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
5356 &rex.reg_endp[rp->rs_no]); |
270 | 5357 regstack_pop(&scan); |
180 | 5358 break; |
5359 | |
5360 #ifdef FEAT_SYN_HL | |
5361 case RS_ZCLOSE: | |
5362 /* Pop the state. Restore pointers when there is no match. */ | |
5363 if (status == RA_NOMATCH) | |
5364 restore_se(&rp->rs_un.sesave, ®_endzpos[rp->rs_no], | |
5365 ®_endzp[rp->rs_no]); | |
270 | 5366 regstack_pop(&scan); |
180 | 5367 break; |
5368 #endif | |
5369 | |
5370 case RS_BRANCH: | |
5371 if (status == RA_MATCH) | |
5372 /* this branch matched, use it */ | |
270 | 5373 regstack_pop(&scan); |
180 | 5374 else |
5375 { | |
5376 if (status != RA_BREAK) | |
5377 { | |
5378 /* After a non-matching branch: try next one. */ | |
233 | 5379 reg_restore(&rp->rs_un.regsave, &backpos); |
180 | 5380 scan = rp->rs_scan; |
5381 } | |
5382 if (scan == NULL || OP(scan) != BRANCH) | |
5383 { | |
5384 /* no more branches, didn't find a match */ | |
5385 status = RA_NOMATCH; | |
270 | 5386 regstack_pop(&scan); |
180 | 5387 } |
5388 else | |
5389 { | |
5390 /* Prepare to try a branch. */ | |
5391 rp->rs_scan = regnext(scan); | |
233 | 5392 reg_save(&rp->rs_un.regsave, &backpos); |
180 | 5393 scan = OPERAND(scan); |
5394 } | |
5395 } | |
5396 break; | |
5397 | |
5398 case RS_BRCPLX_MORE: | |
5399 /* Pop the state. Restore pointers when there is no match. */ | |
5400 if (status == RA_NOMATCH) | |
5401 { | |
233 | 5402 reg_restore(&rp->rs_un.regsave, &backpos); |
180 | 5403 --brace_count[rp->rs_no]; /* decrement match count */ |
5404 } | |
270 | 5405 regstack_pop(&scan); |
180 | 5406 break; |
5407 | |
5408 case RS_BRCPLX_LONG: | |
5409 /* Pop the state. Restore pointers when there is no match. */ | |
5410 if (status == RA_NOMATCH) | |
5411 { | |
5412 /* There was no match, but we did find enough matches. */ | |
233 | 5413 reg_restore(&rp->rs_un.regsave, &backpos); |
180 | 5414 --brace_count[rp->rs_no]; |
5415 /* continue with the items after "\{}" */ | |
5416 status = RA_CONT; | |
5417 } | |
270 | 5418 regstack_pop(&scan); |
180 | 5419 if (status == RA_CONT) |
5420 scan = regnext(scan); | |
5421 break; | |
5422 | |
5423 case RS_BRCPLX_SHORT: | |
5424 /* Pop the state. Restore pointers when there is no match. */ | |
5425 if (status == RA_NOMATCH) | |
5426 /* There was no match, try to match one more item. */ | |
233 | 5427 reg_restore(&rp->rs_un.regsave, &backpos); |
270 | 5428 regstack_pop(&scan); |
180 | 5429 if (status == RA_NOMATCH) |
5430 { | |
5431 scan = OPERAND(scan); | |
5432 status = RA_CONT; | |
5433 } | |
5434 break; | |
5435 | |
5436 case RS_NOMATCH: | |
5437 /* Pop the state. If the operand matches for NOMATCH or | |
5438 * doesn't match for MATCH/SUBPAT, we fail. Otherwise backup, | |
5439 * except for SUBPAT, and continue with the next item. */ | |
5440 if (status == (rp->rs_no == NOMATCH ? RA_MATCH : RA_NOMATCH)) | |
5441 status = RA_NOMATCH; | |
5442 else | |
5443 { | |
5444 status = RA_CONT; | |
233 | 5445 if (rp->rs_no != SUBPAT) /* zero-width */ |
5446 reg_restore(&rp->rs_un.regsave, &backpos); | |
180 | 5447 } |
270 | 5448 regstack_pop(&scan); |
180 | 5449 if (status == RA_CONT) |
5450 scan = regnext(scan); | |
5451 break; | |
5452 | |
5453 case RS_BEHIND1: | |
5454 if (status == RA_NOMATCH) | |
5455 { | |
270 | 5456 regstack_pop(&scan); |
180 | 5457 regstack.ga_len -= sizeof(regbehind_T); |
5458 } | |
5459 else | |
5460 { | |
5461 /* The stuff after BEHIND/NOBEHIND matches. Now try if | |
5462 * the behind part does (not) match before the current | |
5463 * position in the input. This must be done at every | |
5464 * position in the input and checking if the match ends at | |
5465 * the current position. */ | |
5466 | |
5467 /* save the position after the found match for next */ | |
233 | 5468 reg_save(&(((regbehind_T *)rp) - 1)->save_after, &backpos); |
180 | 5469 |
4579
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5470 /* Start looking for a match with operand at the current |
1209 | 5471 * position. Go back one character until we find the |
180 | 5472 * result, hitting the start of the line or the previous |
5473 * line (for multi-line matching). | |
5474 * Set behind_pos to where the match should end, BHPOS | |
5475 * will match it. Save the current value. */ | |
5476 (((regbehind_T *)rp) - 1)->save_behind = behind_pos; | |
5477 behind_pos = rp->rs_un.regsave; | |
5478 | |
5479 rp->rs_state = RS_BEHIND2; | |
5480 | |
233 | 5481 reg_restore(&rp->rs_un.regsave, &backpos); |
4579
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5482 scan = OPERAND(rp->rs_scan) + 4; |
180 | 5483 } |
5484 break; | |
5485 | |
5486 case RS_BEHIND2: | |
5487 /* | |
5488 * Looping for BEHIND / NOBEHIND match. | |
5489 */ | |
5490 if (status == RA_MATCH && reg_save_equal(&behind_pos)) | |
5491 { | |
5492 /* found a match that ends where "next" started */ | |
5493 behind_pos = (((regbehind_T *)rp) - 1)->save_behind; | |
5494 if (rp->rs_no == BEHIND) | |
233 | 5495 reg_restore(&(((regbehind_T *)rp) - 1)->save_after, |
5496 &backpos); | |
180 | 5497 else |
1579 | 5498 { |
5499 /* But we didn't want a match. Need to restore the | |
5500 * subexpr, because what follows matched, so they have | |
5501 * been set. */ | |
180 | 5502 status = RA_NOMATCH; |
1579 | 5503 restore_subexpr(((regbehind_T *)rp) - 1); |
5504 } | |
270 | 5505 regstack_pop(&scan); |
180 | 5506 regstack.ga_len -= sizeof(regbehind_T); |
5507 } | |
5508 else | |
5509 { | |
4579
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5510 long limit; |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5511 |
1579 | 5512 /* No match or a match that doesn't end where we want it: Go |
5513 * back one character. May go to previous line once. */ | |
180 | 5514 no = OK; |
4579
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5515 limit = OPERAND_MIN(rp->rs_scan); |
180 | 5516 if (REG_MULTI) |
5517 { | |
4682
2f51ee8825db
updated for version 7.3.1088
Bram Moolenaar <bram@vim.org>
parents:
4679
diff
changeset
|
5518 if (limit > 0 |
2f51ee8825db
updated for version 7.3.1088
Bram Moolenaar <bram@vim.org>
parents:
4679
diff
changeset
|
5519 && ((rp->rs_un.regsave.rs_u.pos.lnum |
2f51ee8825db
updated for version 7.3.1088
Bram Moolenaar <bram@vim.org>
parents:
4679
diff
changeset
|
5520 < behind_pos.rs_u.pos.lnum |
2f51ee8825db
updated for version 7.3.1088
Bram Moolenaar <bram@vim.org>
parents:
4679
diff
changeset
|
5521 ? (colnr_T)STRLEN(regline) |
2f51ee8825db
updated for version 7.3.1088
Bram Moolenaar <bram@vim.org>
parents:
4679
diff
changeset
|
5522 : behind_pos.rs_u.pos.col) |
2f51ee8825db
updated for version 7.3.1088
Bram Moolenaar <bram@vim.org>
parents:
4679
diff
changeset
|
5523 - rp->rs_un.regsave.rs_u.pos.col >= limit)) |
2f51ee8825db
updated for version 7.3.1088
Bram Moolenaar <bram@vim.org>
parents:
4679
diff
changeset
|
5524 no = FAIL; |
2f51ee8825db
updated for version 7.3.1088
Bram Moolenaar <bram@vim.org>
parents:
4679
diff
changeset
|
5525 else if (rp->rs_un.regsave.rs_u.pos.col == 0) |
180 | 5526 { |
5527 if (rp->rs_un.regsave.rs_u.pos.lnum | |
5528 < behind_pos.rs_u.pos.lnum | |
5529 || reg_getline( | |
5530 --rp->rs_un.regsave.rs_u.pos.lnum) | |
5531 == NULL) | |
5532 no = FAIL; | |
5533 else | |
5534 { | |
233 | 5535 reg_restore(&rp->rs_un.regsave, &backpos); |
180 | 5536 rp->rs_un.regsave.rs_u.pos.col = |
5537 (colnr_T)STRLEN(regline); | |
5538 } | |
5539 } | |
5540 else | |
4579
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5541 { |
4176 | 5542 #ifdef FEAT_MBYTE |
4579
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5543 if (has_mbyte) |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5544 rp->rs_un.regsave.rs_u.pos.col -= |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5545 (*mb_head_off)(regline, regline |
4176 | 5546 + rp->rs_un.regsave.rs_u.pos.col - 1) + 1; |
4579
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5547 else |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5548 #endif |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5549 --rp->rs_un.regsave.rs_u.pos.col; |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5550 } |
180 | 5551 } |
5552 else | |
5553 { | |
5554 if (rp->rs_un.regsave.rs_u.ptr == regline) | |
5555 no = FAIL; | |
5556 else | |
4579
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5557 { |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5558 mb_ptr_back(regline, rp->rs_un.regsave.rs_u.ptr); |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5559 if (limit > 0 && (long)(behind_pos.rs_u.ptr |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5560 - rp->rs_un.regsave.rs_u.ptr) > limit) |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5561 no = FAIL; |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5562 } |
180 | 5563 } |
5564 if (no == OK) | |
5565 { | |
5566 /* Advanced, prepare for finding match again. */ | |
233 | 5567 reg_restore(&rp->rs_un.regsave, &backpos); |
4579
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5568 scan = OPERAND(rp->rs_scan) + 4; |
1579 | 5569 if (status == RA_MATCH) |
5570 { | |
5571 /* We did match, so subexpr may have been changed, | |
5572 * need to restore them for the next try. */ | |
5573 status = RA_NOMATCH; | |
5574 restore_subexpr(((regbehind_T *)rp) - 1); | |
5575 } | |
180 | 5576 } |
5577 else | |
5578 { | |
5579 /* Can't advance. For NOBEHIND that's a match. */ | |
5580 behind_pos = (((regbehind_T *)rp) - 1)->save_behind; | |
5581 if (rp->rs_no == NOBEHIND) | |
5582 { | |
233 | 5583 reg_restore(&(((regbehind_T *)rp) - 1)->save_after, |
5584 &backpos); | |
180 | 5585 status = RA_MATCH; |
5586 } | |
5587 else | |
1579 | 5588 { |
5589 /* We do want a proper match. Need to restore the | |
5590 * subexpr if we had a match, because they may have | |
5591 * been set. */ | |
5592 if (status == RA_MATCH) | |
5593 { | |
5594 status = RA_NOMATCH; | |
5595 restore_subexpr(((regbehind_T *)rp) - 1); | |
5596 } | |
5597 } | |
270 | 5598 regstack_pop(&scan); |
180 | 5599 regstack.ga_len -= sizeof(regbehind_T); |
5600 } | |
5601 } | |
5602 break; | |
5603 | |
5604 case RS_STAR_LONG: | |
5605 case RS_STAR_SHORT: | |
5606 { | |
5607 regstar_T *rst = ((regstar_T *)rp) - 1; | |
5608 | |
5609 if (status == RA_MATCH) | |
5610 { | |
270 | 5611 regstack_pop(&scan); |
180 | 5612 regstack.ga_len -= sizeof(regstar_T); |
5613 break; | |
5614 } | |
5615 | |
5616 /* Tried once already, restore input pointers. */ | |
5617 if (status != RA_BREAK) | |
233 | 5618 reg_restore(&rp->rs_un.regsave, &backpos); |
180 | 5619 |
5620 /* Repeat until we found a position where it could match. */ | |
5621 for (;;) | |
5622 { | |
5623 if (status != RA_BREAK) | |
5624 { | |
5625 /* Tried first position already, advance. */ | |
5626 if (rp->rs_state == RS_STAR_LONG) | |
5627 { | |
685 | 5628 /* Trying for longest match, but couldn't or |
5629 * didn't match -- back up one char. */ | |
180 | 5630 if (--rst->count < rst->minval) |
5631 break; | |
5632 if (reginput == regline) | |
5633 { | |
5634 /* backup to last char of previous line */ | |
5635 --reglnum; | |
5636 regline = reg_getline(reglnum); | |
5637 /* Just in case regrepeat() didn't count | |
5638 * right. */ | |
5639 if (regline == NULL) | |
5640 break; | |
5641 reginput = regline + STRLEN(regline); | |
5642 fast_breakcheck(); | |
5643 } | |
5644 else | |
5645 mb_ptr_back(regline, reginput); | |
5646 } | |
5647 else | |
5648 { | |
5649 /* Range is backwards, use shortest match first. | |
5650 * Careful: maxval and minval are exchanged! | |
5651 * Couldn't or didn't match: try advancing one | |
5652 * char. */ | |
5653 if (rst->count == rst->minval | |
5654 || regrepeat(OPERAND(rp->rs_scan), 1L) == 0) | |
5655 break; | |
5656 ++rst->count; | |
5657 } | |
5658 if (got_int) | |
5659 break; | |
5660 } | |
5661 else | |
5662 status = RA_NOMATCH; | |
5663 | |
5664 /* If it could match, try it. */ | |
5665 if (rst->nextb == NUL || *reginput == rst->nextb | |
5666 || *reginput == rst->nextb_ic) | |
5667 { | |
233 | 5668 reg_save(&rp->rs_un.regsave, &backpos); |
180 | 5669 scan = regnext(rp->rs_scan); |
5670 status = RA_CONT; | |
5671 break; | |
5672 } | |
5673 } | |
5674 if (status != RA_CONT) | |
5675 { | |
5676 /* Failed. */ | |
270 | 5677 regstack_pop(&scan); |
180 | 5678 regstack.ga_len -= sizeof(regstar_T); |
5679 status = RA_NOMATCH; | |
5680 } | |
5681 } | |
5682 break; | |
5683 } | |
5684 | |
685 | 5685 /* If we want to continue the inner loop or didn't pop a state |
5686 * continue matching loop */ | |
180 | 5687 if (status == RA_CONT || rp == (regitem_T *) |
5688 ((char *)regstack.ga_data + regstack.ga_len) - 1) | |
5689 break; | |
5690 } | |
5691 | |
189 | 5692 /* May need to continue with the inner loop, starting at "scan". */ |
180 | 5693 if (status == RA_CONT) |
5694 continue; | |
5695 | |
5696 /* | |
5697 * If the regstack is empty or something failed we are done. | |
5698 */ | |
5699 if (regstack.ga_len == 0 || status == RA_FAIL) | |
5700 { | |
5701 if (scan == NULL) | |
5702 { | |
5703 /* | |
5704 * We get here only if there's trouble -- normally "case END" is | |
5705 * the terminating point. | |
5706 */ | |
5707 EMSG(_(e_re_corr)); | |
7 | 5708 #ifdef DEBUG |
180 | 5709 printf("Premature EOL\n"); |
7 | 5710 #endif |
180 | 5711 } |
189 | 5712 if (status == RA_FAIL) |
5713 got_int = TRUE; | |
180 | 5714 return (status == RA_MATCH); |
5715 } | |
5716 | |
5717 } /* End of loop until the regstack is empty. */ | |
5718 | |
5719 /* NOTREACHED */ | |
5720 } | |
5721 | |
5722 /* | |
5723 * Push an item onto the regstack. | |
5724 * Returns pointer to new item. Returns NULL when out of memory. | |
5725 */ | |
5726 static regitem_T * | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
5727 regstack_push(regstate_T state, char_u *scan) |
180 | 5728 { |
5729 regitem_T *rp; | |
5730 | |
270 | 5731 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp) |
189 | 5732 { |
5733 EMSG(_(e_maxmempat)); | |
5734 return NULL; | |
5735 } | |
270 | 5736 if (ga_grow(®stack, sizeof(regitem_T)) == FAIL) |
180 | 5737 return NULL; |
5738 | |
270 | 5739 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len); |
180 | 5740 rp->rs_state = state; |
5741 rp->rs_scan = scan; | |
5742 | |
270 | 5743 regstack.ga_len += sizeof(regitem_T); |
180 | 5744 return rp; |
5745 } | |
5746 | |
5747 /* | |
5748 * Pop an item from the regstack. | |
5749 */ | |
5750 static void | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
5751 regstack_pop(char_u **scan) |
180 | 5752 { |
5753 regitem_T *rp; | |
5754 | |
270 | 5755 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len) - 1; |
180 | 5756 *scan = rp->rs_scan; |
5757 | |
270 | 5758 regstack.ga_len -= sizeof(regitem_T); |
7 | 5759 } |
5760 | |
5761 /* | |
5762 * regrepeat - repeatedly match something simple, return how many. | |
5763 * Advances reginput (and reglnum) to just after the matched chars. | |
5764 */ | |
5765 static int | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
5766 regrepeat( |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
5767 char_u *p, |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
5768 long maxcount) /* maximum number of matches allowed */ |
7 | 5769 { |
5770 long count = 0; | |
5771 char_u *scan; | |
5772 char_u *opnd; | |
5773 int mask; | |
5774 int testval = 0; | |
5775 | |
5776 scan = reginput; /* Make local copy of reginput for speed. */ | |
5777 opnd = OPERAND(p); | |
5778 switch (OP(p)) | |
5779 { | |
5780 case ANY: | |
5781 case ANY + ADD_NL: | |
5782 while (count < maxcount) | |
5783 { | |
5784 /* Matching anything means we continue until end-of-line (or | |
5785 * end-of-file for ANY + ADD_NL), only limited by maxcount. */ | |
5786 while (*scan != NUL && count < maxcount) | |
5787 { | |
5788 ++count; | |
39 | 5789 mb_ptr_adv(scan); |
7 | 5790 } |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
5791 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > rex.reg_maxline |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
5792 || rex.reg_line_lbr || count == maxcount) |
7 | 5793 break; |
5794 ++count; /* count the line-break */ | |
5795 reg_nextline(); | |
5796 scan = reginput; | |
5797 if (got_int) | |
5798 break; | |
5799 } | |
5800 break; | |
5801 | |
5802 case IDENT: | |
5803 case IDENT + ADD_NL: | |
5804 testval = TRUE; | |
5805 /*FALLTHROUGH*/ | |
5806 case SIDENT: | |
5807 case SIDENT + ADD_NL: | |
5808 while (count < maxcount) | |
5809 { | |
4466 | 5810 if (vim_isIDc(PTR2CHAR(scan)) && (testval || !VIM_ISDIGIT(*scan))) |
7 | 5811 { |
39 | 5812 mb_ptr_adv(scan); |
7 | 5813 } |
5814 else if (*scan == NUL) | |
5815 { | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
5816 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > rex.reg_maxline |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
5817 || rex.reg_line_lbr) |
7 | 5818 break; |
5819 reg_nextline(); | |
5820 scan = reginput; | |
5821 if (got_int) | |
5822 break; | |
5823 } | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
5824 else if (rex.reg_line_lbr && *scan == '\n' && WITH_NL(OP(p))) |
7 | 5825 ++scan; |
5826 else | |
5827 break; | |
5828 ++count; | |
5829 } | |
5830 break; | |
5831 | |
5832 case KWORD: | |
5833 case KWORD + ADD_NL: | |
5834 testval = TRUE; | |
5835 /*FALLTHROUGH*/ | |
5836 case SKWORD: | |
5837 case SKWORD + ADD_NL: | |
5838 while (count < maxcount) | |
5839 { | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
5840 if (vim_iswordp_buf(scan, rex.reg_buf) |
4069 | 5841 && (testval || !VIM_ISDIGIT(*scan))) |
7 | 5842 { |
39 | 5843 mb_ptr_adv(scan); |
7 | 5844 } |
5845 else if (*scan == NUL) | |
5846 { | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
5847 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > rex.reg_maxline |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
5848 || rex.reg_line_lbr) |
7 | 5849 break; |
5850 reg_nextline(); | |
5851 scan = reginput; | |
5852 if (got_int) | |
5853 break; | |
5854 } | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
5855 else if (rex.reg_line_lbr && *scan == '\n' && WITH_NL(OP(p))) |
7 | 5856 ++scan; |
5857 else | |
5858 break; | |
5859 ++count; | |
5860 } | |
5861 break; | |
5862 | |
5863 case FNAME: | |
5864 case FNAME + ADD_NL: | |
5865 testval = TRUE; | |
5866 /*FALLTHROUGH*/ | |
5867 case SFNAME: | |
5868 case SFNAME + ADD_NL: | |
5869 while (count < maxcount) | |
5870 { | |
4466 | 5871 if (vim_isfilec(PTR2CHAR(scan)) && (testval || !VIM_ISDIGIT(*scan))) |
7 | 5872 { |
39 | 5873 mb_ptr_adv(scan); |
7 | 5874 } |
5875 else if (*scan == NUL) | |
5876 { | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
5877 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > rex.reg_maxline |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
5878 || rex.reg_line_lbr) |
7 | 5879 break; |
5880 reg_nextline(); | |
5881 scan = reginput; | |
5882 if (got_int) | |
5883 break; | |
5884 } | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
5885 else if (rex.reg_line_lbr && *scan == '\n' && WITH_NL(OP(p))) |
7 | 5886 ++scan; |
5887 else | |
5888 break; | |
5889 ++count; | |
5890 } | |
5891 break; | |
5892 | |
5893 case PRINT: | |
5894 case PRINT + ADD_NL: | |
5895 testval = TRUE; | |
5896 /*FALLTHROUGH*/ | |
5897 case SPRINT: | |
5898 case SPRINT + ADD_NL: | |
5899 while (count < maxcount) | |
5900 { | |
5901 if (*scan == NUL) | |
5902 { | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
5903 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > rex.reg_maxline |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
5904 || rex.reg_line_lbr) |
7 | 5905 break; |
5906 reg_nextline(); | |
5907 scan = reginput; | |
5908 if (got_int) | |
5909 break; | |
5910 } | |
5221
9982ec574beb
updated for version 7.4a.036
Bram Moolenaar <bram@vim.org>
parents:
4901
diff
changeset
|
5911 else if (vim_isprintc(PTR2CHAR(scan)) == 1 |
9982ec574beb
updated for version 7.4a.036
Bram Moolenaar <bram@vim.org>
parents:
4901
diff
changeset
|
5912 && (testval || !VIM_ISDIGIT(*scan))) |
7 | 5913 { |
39 | 5914 mb_ptr_adv(scan); |
7 | 5915 } |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
5916 else if (rex.reg_line_lbr && *scan == '\n' && WITH_NL(OP(p))) |
7 | 5917 ++scan; |
5918 else | |
5919 break; | |
5920 ++count; | |
5921 } | |
5922 break; | |
5923 | |
5924 case WHITE: | |
5925 case WHITE + ADD_NL: | |
5926 testval = mask = RI_WHITE; | |
5927 do_class: | |
5928 while (count < maxcount) | |
5929 { | |
5930 #ifdef FEAT_MBYTE | |
5931 int l; | |
5932 #endif | |
5933 if (*scan == NUL) | |
5934 { | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
5935 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > rex.reg_maxline |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
5936 || rex.reg_line_lbr) |
7 | 5937 break; |
5938 reg_nextline(); | |
5939 scan = reginput; | |
5940 if (got_int) | |
5941 break; | |
5942 } | |
5943 #ifdef FEAT_MBYTE | |
474 | 5944 else if (has_mbyte && (l = (*mb_ptr2len)(scan)) > 1) |
7 | 5945 { |
5946 if (testval != 0) | |
5947 break; | |
5948 scan += l; | |
5949 } | |
5950 #endif | |
5951 else if ((class_tab[*scan] & mask) == testval) | |
5952 ++scan; | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
5953 else if (rex.reg_line_lbr && *scan == '\n' && WITH_NL(OP(p))) |
7 | 5954 ++scan; |
5955 else | |
5956 break; | |
5957 ++count; | |
5958 } | |
5959 break; | |
5960 | |
5961 case NWHITE: | |
5962 case NWHITE + ADD_NL: | |
5963 mask = RI_WHITE; | |
5964 goto do_class; | |
5965 case DIGIT: | |
5966 case DIGIT + ADD_NL: | |
5967 testval = mask = RI_DIGIT; | |
5968 goto do_class; | |
5969 case NDIGIT: | |
5970 case NDIGIT + ADD_NL: | |
5971 mask = RI_DIGIT; | |
5972 goto do_class; | |
5973 case HEX: | |
5974 case HEX + ADD_NL: | |
5975 testval = mask = RI_HEX; | |
5976 goto do_class; | |
5977 case NHEX: | |
5978 case NHEX + ADD_NL: | |
5979 mask = RI_HEX; | |
5980 goto do_class; | |
5981 case OCTAL: | |
5982 case OCTAL + ADD_NL: | |
5983 testval = mask = RI_OCTAL; | |
5984 goto do_class; | |
5985 case NOCTAL: | |
5986 case NOCTAL + ADD_NL: | |
5987 mask = RI_OCTAL; | |
5988 goto do_class; | |
5989 case WORD: | |
5990 case WORD + ADD_NL: | |
5991 testval = mask = RI_WORD; | |
5992 goto do_class; | |
5993 case NWORD: | |
5994 case NWORD + ADD_NL: | |
5995 mask = RI_WORD; | |
5996 goto do_class; | |
5997 case HEAD: | |
5998 case HEAD + ADD_NL: | |
5999 testval = mask = RI_HEAD; | |
6000 goto do_class; | |
6001 case NHEAD: | |
6002 case NHEAD + ADD_NL: | |
6003 mask = RI_HEAD; | |
6004 goto do_class; | |
6005 case ALPHA: | |
6006 case ALPHA + ADD_NL: | |
6007 testval = mask = RI_ALPHA; | |
6008 goto do_class; | |
6009 case NALPHA: | |
6010 case NALPHA + ADD_NL: | |
6011 mask = RI_ALPHA; | |
6012 goto do_class; | |
6013 case LOWER: | |
6014 case LOWER + ADD_NL: | |
6015 testval = mask = RI_LOWER; | |
6016 goto do_class; | |
6017 case NLOWER: | |
6018 case NLOWER + ADD_NL: | |
6019 mask = RI_LOWER; | |
6020 goto do_class; | |
6021 case UPPER: | |
6022 case UPPER + ADD_NL: | |
6023 testval = mask = RI_UPPER; | |
6024 goto do_class; | |
6025 case NUPPER: | |
6026 case NUPPER + ADD_NL: | |
6027 mask = RI_UPPER; | |
6028 goto do_class; | |
6029 | |
6030 case EXACTLY: | |
6031 { | |
6032 int cu, cl; | |
6033 | |
6034 /* This doesn't do a multi-byte character, because a MULTIBYTECODE | |
1347 | 6035 * would have been used for it. It does handle single-byte |
6036 * characters, such as latin1. */ | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
6037 if (rex.reg_ic) |
7 | 6038 { |
1347 | 6039 cu = MB_TOUPPER(*opnd); |
6040 cl = MB_TOLOWER(*opnd); | |
7 | 6041 while (count < maxcount && (*scan == cu || *scan == cl)) |
6042 { | |
6043 count++; | |
6044 scan++; | |
6045 } | |
6046 } | |
6047 else | |
6048 { | |
6049 cu = *opnd; | |
6050 while (count < maxcount && *scan == cu) | |
6051 { | |
6052 count++; | |
6053 scan++; | |
6054 } | |
6055 } | |
6056 break; | |
6057 } | |
6058 | |
6059 #ifdef FEAT_MBYTE | |
6060 case MULTIBYTECODE: | |
6061 { | |
6062 int i, len, cf = 0; | |
6063 | |
6064 /* Safety check (just in case 'encoding' was changed since | |
6065 * compiling the program). */ | |
474 | 6066 if ((len = (*mb_ptr2len)(opnd)) > 1) |
7 | 6067 { |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
6068 if (rex.reg_ic && enc_utf8) |
7 | 6069 cf = utf_fold(utf_ptr2char(opnd)); |
6785 | 6070 while (count < maxcount && (*mb_ptr2len)(scan) >= len) |
7 | 6071 { |
6072 for (i = 0; i < len; ++i) | |
6073 if (opnd[i] != scan[i]) | |
6074 break; | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
6075 if (i < len && (!rex.reg_ic || !enc_utf8 |
7 | 6076 || utf_fold(utf_ptr2char(scan)) != cf)) |
6077 break; | |
6078 scan += len; | |
6079 ++count; | |
6080 } | |
6081 } | |
6082 } | |
6083 break; | |
6084 #endif | |
6085 | |
6086 case ANYOF: | |
6087 case ANYOF + ADD_NL: | |
6088 testval = TRUE; | |
6089 /*FALLTHROUGH*/ | |
6090 | |
6091 case ANYBUT: | |
6092 case ANYBUT + ADD_NL: | |
6093 while (count < maxcount) | |
6094 { | |
6095 #ifdef FEAT_MBYTE | |
6096 int len; | |
6097 #endif | |
6098 if (*scan == NUL) | |
6099 { | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
6100 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > rex.reg_maxline |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
6101 || rex.reg_line_lbr) |
7 | 6102 break; |
6103 reg_nextline(); | |
6104 scan = reginput; | |
6105 if (got_int) | |
6106 break; | |
6107 } | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
6108 else if (rex.reg_line_lbr && *scan == '\n' && WITH_NL(OP(p))) |
7 | 6109 ++scan; |
6110 #ifdef FEAT_MBYTE | |
474 | 6111 else if (has_mbyte && (len = (*mb_ptr2len)(scan)) > 1) |
7 | 6112 { |
6113 if ((cstrchr(opnd, (*mb_ptr2char)(scan)) == NULL) == testval) | |
6114 break; | |
6115 scan += len; | |
6116 } | |
6117 #endif | |
6118 else | |
6119 { | |
6120 if ((cstrchr(opnd, *scan) == NULL) == testval) | |
6121 break; | |
6122 ++scan; | |
6123 } | |
6124 ++count; | |
6125 } | |
6126 break; | |
6127 | |
6128 case NEWL: | |
6129 while (count < maxcount | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
6130 && ((*scan == NUL && reglnum <= rex.reg_maxline |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
6131 && !rex.reg_line_lbr && REG_MULTI) |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
6132 || (*scan == '\n' && rex.reg_line_lbr))) |
7 | 6133 { |
6134 count++; | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
6135 if (rex.reg_line_lbr) |
7 | 6136 ADVANCE_REGINPUT(); |
6137 else | |
6138 reg_nextline(); | |
6139 scan = reginput; | |
6140 if (got_int) | |
6141 break; | |
6142 } | |
6143 break; | |
6144 | |
6145 default: /* Oh dear. Called inappropriately. */ | |
6146 EMSG(_(e_re_corr)); | |
6147 #ifdef DEBUG | |
6148 printf("Called regrepeat with op code %d\n", OP(p)); | |
6149 #endif | |
6150 break; | |
6151 } | |
6152 | |
6153 reginput = scan; | |
6154 | |
6155 return (int)count; | |
6156 } | |
6157 | |
6158 /* | |
6159 * regnext - dig the "next" pointer out of a node | |
2010 | 6160 * Returns NULL when calculating size, when there is no next item and when |
6161 * there is an error. | |
7 | 6162 */ |
6163 static char_u * | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
6164 regnext(char_u *p) |
7 | 6165 { |
6166 int offset; | |
6167 | |
2010 | 6168 if (p == JUST_CALC_SIZE || reg_toolong) |
7 | 6169 return NULL; |
6170 | |
6171 offset = NEXT(p); | |
6172 if (offset == 0) | |
6173 return NULL; | |
6174 | |
233 | 6175 if (OP(p) == BACK) |
7 | 6176 return p - offset; |
6177 else | |
6178 return p + offset; | |
6179 } | |
6180 | |
6181 /* | |
6182 * Check the regexp program for its magic number. | |
6183 * Return TRUE if it's wrong. | |
6184 */ | |
6185 static int | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
6186 prog_magic_wrong(void) |
7 | 6187 { |
4444 | 6188 regprog_T *prog; |
6189 | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
6190 prog = REG_MULTI ? rex.reg_mmatch->regprog : rex.reg_match->regprog; |
4444 | 6191 if (prog->engine == &nfa_regengine) |
6192 /* For NFA matcher we don't check the magic */ | |
6193 return FALSE; | |
6194 | |
6195 if (UCHARAT(((bt_regprog_T *)prog)->program) != REGMAGIC) | |
7 | 6196 { |
6197 EMSG(_(e_re_corr)); | |
6198 return TRUE; | |
6199 } | |
6200 return FALSE; | |
6201 } | |
6202 | |
6203 /* | |
6204 * Cleanup the subexpressions, if this wasn't done yet. | |
6205 * This construction is used to clear the subexpressions only when they are | |
6206 * used (to increase speed). | |
6207 */ | |
6208 static void | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
6209 cleanup_subexpr(void) |
7 | 6210 { |
6211 if (need_clear_subexpr) | |
6212 { | |
6213 if (REG_MULTI) | |
6214 { | |
6215 /* Use 0xff to set lnum to -1 */ | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
6216 vim_memset(rex.reg_startpos, 0xff, sizeof(lpos_T) * NSUBEXP); |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
6217 vim_memset(rex.reg_endpos, 0xff, sizeof(lpos_T) * NSUBEXP); |
7 | 6218 } |
6219 else | |
6220 { | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
6221 vim_memset(rex.reg_startp, 0, sizeof(char_u *) * NSUBEXP); |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
6222 vim_memset(rex.reg_endp, 0, sizeof(char_u *) * NSUBEXP); |
7 | 6223 } |
6224 need_clear_subexpr = FALSE; | |
6225 } | |
6226 } | |
6227 | |
6228 #ifdef FEAT_SYN_HL | |
6229 static void | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
6230 cleanup_zsubexpr(void) |
7 | 6231 { |
6232 if (need_clear_zsubexpr) | |
6233 { | |
6234 if (REG_MULTI) | |
6235 { | |
6236 /* Use 0xff to set lnum to -1 */ | |
6237 vim_memset(reg_startzpos, 0xff, sizeof(lpos_T) * NSUBEXP); | |
6238 vim_memset(reg_endzpos, 0xff, sizeof(lpos_T) * NSUBEXP); | |
6239 } | |
6240 else | |
6241 { | |
6242 vim_memset(reg_startzp, 0, sizeof(char_u *) * NSUBEXP); | |
6243 vim_memset(reg_endzp, 0, sizeof(char_u *) * NSUBEXP); | |
6244 } | |
6245 need_clear_zsubexpr = FALSE; | |
6246 } | |
6247 } | |
6248 #endif | |
6249 | |
6250 /* | |
1579 | 6251 * Save the current subexpr to "bp", so that they can be restored |
6252 * later by restore_subexpr(). | |
6253 */ | |
6254 static void | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
6255 save_subexpr(regbehind_T *bp) |
1579 | 6256 { |
6257 int i; | |
6258 | |
1602 | 6259 /* When "need_clear_subexpr" is set we don't need to save the values, only |
6260 * remember that this flag needs to be set again when restoring. */ | |
6261 bp->save_need_clear_subexpr = need_clear_subexpr; | |
6262 if (!need_clear_subexpr) | |
1579 | 6263 { |
1602 | 6264 for (i = 0; i < NSUBEXP; ++i) |
1579 | 6265 { |
1602 | 6266 if (REG_MULTI) |
6267 { | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
6268 bp->save_start[i].se_u.pos = rex.reg_startpos[i]; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
6269 bp->save_end[i].se_u.pos = rex.reg_endpos[i]; |
1602 | 6270 } |
6271 else | |
6272 { | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
6273 bp->save_start[i].se_u.ptr = rex.reg_startp[i]; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
6274 bp->save_end[i].se_u.ptr = rex.reg_endp[i]; |
1602 | 6275 } |
1579 | 6276 } |
6277 } | |
6278 } | |
6279 | |
6280 /* | |
6281 * Restore the subexpr from "bp". | |
6282 */ | |
6283 static void | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
6284 restore_subexpr(regbehind_T *bp) |
1579 | 6285 { |
6286 int i; | |
6287 | |
1602 | 6288 /* Only need to restore saved values when they are not to be cleared. */ |
6289 need_clear_subexpr = bp->save_need_clear_subexpr; | |
6290 if (!need_clear_subexpr) | |
1579 | 6291 { |
1602 | 6292 for (i = 0; i < NSUBEXP; ++i) |
1579 | 6293 { |
1602 | 6294 if (REG_MULTI) |
6295 { | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
6296 rex.reg_startpos[i] = bp->save_start[i].se_u.pos; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
6297 rex.reg_endpos[i] = bp->save_end[i].se_u.pos; |
1602 | 6298 } |
6299 else | |
6300 { | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
6301 rex.reg_startp[i] = bp->save_start[i].se_u.ptr; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
6302 rex.reg_endp[i] = bp->save_end[i].se_u.ptr; |
1602 | 6303 } |
1579 | 6304 } |
6305 } | |
6306 } | |
6307 | |
6308 /* | |
7 | 6309 * Advance reglnum, regline and reginput to the next line. |
6310 */ | |
6311 static void | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
6312 reg_nextline(void) |
7 | 6313 { |
6314 regline = reg_getline(++reglnum); | |
6315 reginput = regline; | |
6316 fast_breakcheck(); | |
6317 } | |
6318 | |
6319 /* | |
6320 * Save the input line and position in a regsave_T. | |
6321 */ | |
6322 static void | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
6323 reg_save(regsave_T *save, garray_T *gap) |
7 | 6324 { |
6325 if (REG_MULTI) | |
6326 { | |
6327 save->rs_u.pos.col = (colnr_T)(reginput - regline); | |
6328 save->rs_u.pos.lnum = reglnum; | |
6329 } | |
6330 else | |
6331 save->rs_u.ptr = reginput; | |
233 | 6332 save->rs_len = gap->ga_len; |
7 | 6333 } |
6334 | |
6335 /* | |
6336 * Restore the input line and position from a regsave_T. | |
6337 */ | |
6338 static void | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
6339 reg_restore(regsave_T *save, garray_T *gap) |
7 | 6340 { |
6341 if (REG_MULTI) | |
6342 { | |
6343 if (reglnum != save->rs_u.pos.lnum) | |
6344 { | |
6345 /* only call reg_getline() when the line number changed to save | |
6346 * a bit of time */ | |
6347 reglnum = save->rs_u.pos.lnum; | |
6348 regline = reg_getline(reglnum); | |
6349 } | |
6350 reginput = regline + save->rs_u.pos.col; | |
6351 } | |
6352 else | |
6353 reginput = save->rs_u.ptr; | |
233 | 6354 gap->ga_len = save->rs_len; |
7 | 6355 } |
6356 | |
6357 /* | |
6358 * Return TRUE if current position is equal to saved position. | |
6359 */ | |
6360 static int | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
6361 reg_save_equal(regsave_T *save) |
7 | 6362 { |
6363 if (REG_MULTI) | |
6364 return reglnum == save->rs_u.pos.lnum | |
6365 && reginput == regline + save->rs_u.pos.col; | |
6366 return reginput == save->rs_u.ptr; | |
6367 } | |
6368 | |
6369 /* | |
6370 * Tentatively set the sub-expression start to the current position (after | |
6371 * calling regmatch() they will have changed). Need to save the existing | |
6372 * values for when there is no match. | |
6373 * Use se_save() to use pointer (save_se_multi()) or position (save_se_one()), | |
6374 * depending on REG_MULTI. | |
6375 */ | |
6376 static void | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
6377 save_se_multi(save_se_T *savep, lpos_T *posp) |
7 | 6378 { |
6379 savep->se_u.pos = *posp; | |
6380 posp->lnum = reglnum; | |
6381 posp->col = (colnr_T)(reginput - regline); | |
6382 } | |
6383 | |
6384 static void | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
6385 save_se_one(save_se_T *savep, char_u **pp) |
7 | 6386 { |
6387 savep->se_u.ptr = *pp; | |
6388 *pp = reginput; | |
6389 } | |
6390 | |
6391 /* | |
6392 * Compare a number with the operand of RE_LNUM, RE_COL or RE_VCOL. | |
6393 */ | |
6394 static int | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
6395 re_num_cmp(long_u val, char_u *scan) |
7 | 6396 { |
6397 long_u n = OPERAND_MIN(scan); | |
6398 | |
6399 if (OPERAND_CMP(scan) == '>') | |
6400 return val > n; | |
6401 if (OPERAND_CMP(scan) == '<') | |
6402 return val < n; | |
6403 return val == n; | |
6404 } | |
6405 | |
4891
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6406 /* |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6407 * Check whether a backreference matches. |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6408 * Returns RA_FAIL, RA_NOMATCH or RA_MATCH. |
5504 | 6409 * If "bytelen" is not NULL, it is set to the byte length of the match in the |
6410 * last line. | |
4891
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6411 */ |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6412 static int |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
6413 match_with_backref( |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
6414 linenr_T start_lnum, |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
6415 colnr_T start_col, |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
6416 linenr_T end_lnum, |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
6417 colnr_T end_col, |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
6418 int *bytelen) |
4891
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6419 { |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6420 linenr_T clnum = start_lnum; |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6421 colnr_T ccol = start_col; |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6422 int len; |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6423 char_u *p; |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6424 |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6425 if (bytelen != NULL) |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6426 *bytelen = 0; |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6427 for (;;) |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6428 { |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6429 /* Since getting one line may invalidate the other, need to make copy. |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6430 * Slow! */ |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6431 if (regline != reg_tofree) |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6432 { |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6433 len = (int)STRLEN(regline); |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6434 if (reg_tofree == NULL || len >= (int)reg_tofreelen) |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6435 { |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6436 len += 50; /* get some extra */ |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6437 vim_free(reg_tofree); |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6438 reg_tofree = alloc(len); |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6439 if (reg_tofree == NULL) |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6440 return RA_FAIL; /* out of memory!*/ |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6441 reg_tofreelen = len; |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6442 } |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6443 STRCPY(reg_tofree, regline); |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6444 reginput = reg_tofree + (reginput - regline); |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6445 regline = reg_tofree; |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6446 } |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6447 |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6448 /* Get the line to compare with. */ |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6449 p = reg_getline(clnum); |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6450 if (clnum == end_lnum) |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6451 len = end_col - ccol; |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6452 else |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6453 len = (int)STRLEN(p + ccol); |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6454 |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6455 if (cstrncmp(p + ccol, reginput, &len) != 0) |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6456 return RA_NOMATCH; /* doesn't match */ |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6457 if (bytelen != NULL) |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6458 *bytelen += len; |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6459 if (clnum == end_lnum) |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6460 break; /* match and at end! */ |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
6461 if (reglnum >= rex.reg_maxline) |
4891
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6462 return RA_NOMATCH; /* text too short */ |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6463 |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6464 /* Advance to next line. */ |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6465 reg_nextline(); |
5504 | 6466 if (bytelen != NULL) |
6467 *bytelen = 0; | |
4891
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6468 ++clnum; |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6469 ccol = 0; |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6470 if (got_int) |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6471 return RA_FAIL; |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6472 } |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6473 |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6474 /* found a match! Note that regline may now point to a copy of the line, |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6475 * that should not matter. */ |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6476 return RA_MATCH; |
4c42efb4c098
updated for version 7.3.1191
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
6477 } |
7 | 6478 |
4444 | 6479 #ifdef BT_REGEXP_DUMP |
7 | 6480 |
6481 /* | |
6482 * regdump - dump a regexp onto stdout in vaguely comprehensible form | |
6483 */ | |
6484 static void | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
6485 regdump(char_u *pattern, bt_regprog_T *r) |
7 | 6486 { |
6487 char_u *s; | |
6488 int op = EXACTLY; /* Arbitrary non-END op. */ | |
6489 char_u *next; | |
6490 char_u *end = NULL; | |
4444 | 6491 FILE *f; |
6492 | |
6493 #ifdef BT_REGEXP_LOG | |
6494 f = fopen("bt_regexp_log.log", "a"); | |
6495 #else | |
6496 f = stdout; | |
6497 #endif | |
6498 if (f == NULL) | |
6499 return; | |
6500 fprintf(f, "-------------------------------------\n\r\nregcomp(%s):\r\n", pattern); | |
7 | 6501 |
6502 s = r->program + 1; | |
6503 /* | |
6504 * Loop until we find the END that isn't before a referred next (an END | |
6505 * can also appear in a NOMATCH operand). | |
6506 */ | |
6507 while (op != END || s <= end) | |
6508 { | |
6509 op = OP(s); | |
4444 | 6510 fprintf(f, "%2d%s", (int)(s - r->program), regprop(s)); /* Where, what. */ |
7 | 6511 next = regnext(s); |
6512 if (next == NULL) /* Next ptr. */ | |
4444 | 6513 fprintf(f, "(0)"); |
7 | 6514 else |
4444 | 6515 fprintf(f, "(%d)", (int)((s - r->program) + (next - s))); |
7 | 6516 if (end < next) |
6517 end = next; | |
6518 if (op == BRACE_LIMITS) | |
6519 { | |
4746
d1376091d18b
updated for version 7.3.1120
Bram Moolenaar <bram@vim.org>
parents:
4744
diff
changeset
|
6520 /* Two ints */ |
4444 | 6521 fprintf(f, " minval %ld, maxval %ld", OPERAND_MIN(s), OPERAND_MAX(s)); |
7 | 6522 s += 8; |
6523 } | |
4746
d1376091d18b
updated for version 7.3.1120
Bram Moolenaar <bram@vim.org>
parents:
4744
diff
changeset
|
6524 else if (op == BEHIND || op == NOBEHIND) |
d1376091d18b
updated for version 7.3.1120
Bram Moolenaar <bram@vim.org>
parents:
4744
diff
changeset
|
6525 { |
d1376091d18b
updated for version 7.3.1120
Bram Moolenaar <bram@vim.org>
parents:
4744
diff
changeset
|
6526 /* one int */ |
d1376091d18b
updated for version 7.3.1120
Bram Moolenaar <bram@vim.org>
parents:
4744
diff
changeset
|
6527 fprintf(f, " count %ld", OPERAND_MIN(s)); |
d1376091d18b
updated for version 7.3.1120
Bram Moolenaar <bram@vim.org>
parents:
4744
diff
changeset
|
6528 s += 4; |
d1376091d18b
updated for version 7.3.1120
Bram Moolenaar <bram@vim.org>
parents:
4744
diff
changeset
|
6529 } |
4770
b20dbf3a5370
updated for version 7.3.1132
Bram Moolenaar <bram@vim.org>
parents:
4762
diff
changeset
|
6530 else if (op == RE_LNUM || op == RE_COL || op == RE_VCOL) |
b20dbf3a5370
updated for version 7.3.1132
Bram Moolenaar <bram@vim.org>
parents:
4762
diff
changeset
|
6531 { |
b20dbf3a5370
updated for version 7.3.1132
Bram Moolenaar <bram@vim.org>
parents:
4762
diff
changeset
|
6532 /* one int plus comperator */ |
b20dbf3a5370
updated for version 7.3.1132
Bram Moolenaar <bram@vim.org>
parents:
4762
diff
changeset
|
6533 fprintf(f, " count %ld", OPERAND_MIN(s)); |
b20dbf3a5370
updated for version 7.3.1132
Bram Moolenaar <bram@vim.org>
parents:
4762
diff
changeset
|
6534 s += 5; |
b20dbf3a5370
updated for version 7.3.1132
Bram Moolenaar <bram@vim.org>
parents:
4762
diff
changeset
|
6535 } |
7 | 6536 s += 3; |
6537 if (op == ANYOF || op == ANYOF + ADD_NL | |
6538 || op == ANYBUT || op == ANYBUT + ADD_NL | |
6539 || op == EXACTLY) | |
6540 { | |
6541 /* Literal string, where present. */ | |
4444 | 6542 fprintf(f, "\nxxxxxxxxx\n"); |
7 | 6543 while (*s != NUL) |
4444 | 6544 fprintf(f, "%c", *s++); |
6545 fprintf(f, "\nxxxxxxxxx\n"); | |
7 | 6546 s++; |
6547 } | |
4444 | 6548 fprintf(f, "\r\n"); |
7 | 6549 } |
6550 | |
6551 /* Header fields of interest. */ | |
6552 if (r->regstart != NUL) | |
4444 | 6553 fprintf(f, "start `%s' 0x%x; ", r->regstart < 256 |
7 | 6554 ? (char *)transchar(r->regstart) |
6555 : "multibyte", r->regstart); | |
6556 if (r->reganch) | |
4444 | 6557 fprintf(f, "anchored; "); |
7 | 6558 if (r->regmust != NULL) |
4444 | 6559 fprintf(f, "must have \"%s\"", r->regmust); |
6560 fprintf(f, "\r\n"); | |
6561 | |
6562 #ifdef BT_REGEXP_LOG | |
6563 fclose(f); | |
6564 #endif | |
7 | 6565 } |
4444 | 6566 #endif /* BT_REGEXP_DUMP */ |
6567 | |
6568 #ifdef DEBUG | |
7 | 6569 /* |
6570 * regprop - printable representation of opcode | |
6571 */ | |
6572 static char_u * | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
6573 regprop(char_u *op) |
7 | 6574 { |
4444 | 6575 char *p; |
6576 static char buf[50]; | |
6577 | |
6578 STRCPY(buf, ":"); | |
6579 | |
6580 switch ((int) OP(op)) | |
7 | 6581 { |
6582 case BOL: | |
6583 p = "BOL"; | |
6584 break; | |
6585 case EOL: | |
6586 p = "EOL"; | |
6587 break; | |
6588 case RE_BOF: | |
6589 p = "BOF"; | |
6590 break; | |
6591 case RE_EOF: | |
6592 p = "EOF"; | |
6593 break; | |
6594 case CURSOR: | |
6595 p = "CURSOR"; | |
6596 break; | |
639 | 6597 case RE_VISUAL: |
6598 p = "RE_VISUAL"; | |
6599 break; | |
7 | 6600 case RE_LNUM: |
6601 p = "RE_LNUM"; | |
6602 break; | |
639 | 6603 case RE_MARK: |
6604 p = "RE_MARK"; | |
6605 break; | |
7 | 6606 case RE_COL: |
6607 p = "RE_COL"; | |
6608 break; | |
6609 case RE_VCOL: | |
6610 p = "RE_VCOL"; | |
6611 break; | |
6612 case BOW: | |
6613 p = "BOW"; | |
6614 break; | |
6615 case EOW: | |
6616 p = "EOW"; | |
6617 break; | |
6618 case ANY: | |
6619 p = "ANY"; | |
6620 break; | |
6621 case ANY + ADD_NL: | |
6622 p = "ANY+NL"; | |
6623 break; | |
6624 case ANYOF: | |
6625 p = "ANYOF"; | |
6626 break; | |
6627 case ANYOF + ADD_NL: | |
6628 p = "ANYOF+NL"; | |
6629 break; | |
6630 case ANYBUT: | |
6631 p = "ANYBUT"; | |
6632 break; | |
6633 case ANYBUT + ADD_NL: | |
6634 p = "ANYBUT+NL"; | |
6635 break; | |
6636 case IDENT: | |
6637 p = "IDENT"; | |
6638 break; | |
6639 case IDENT + ADD_NL: | |
6640 p = "IDENT+NL"; | |
6641 break; | |
6642 case SIDENT: | |
6643 p = "SIDENT"; | |
6644 break; | |
6645 case SIDENT + ADD_NL: | |
6646 p = "SIDENT+NL"; | |
6647 break; | |
6648 case KWORD: | |
6649 p = "KWORD"; | |
6650 break; | |
6651 case KWORD + ADD_NL: | |
6652 p = "KWORD+NL"; | |
6653 break; | |
6654 case SKWORD: | |
6655 p = "SKWORD"; | |
6656 break; | |
6657 case SKWORD + ADD_NL: | |
6658 p = "SKWORD+NL"; | |
6659 break; | |
6660 case FNAME: | |
6661 p = "FNAME"; | |
6662 break; | |
6663 case FNAME + ADD_NL: | |
6664 p = "FNAME+NL"; | |
6665 break; | |
6666 case SFNAME: | |
6667 p = "SFNAME"; | |
6668 break; | |
6669 case SFNAME + ADD_NL: | |
6670 p = "SFNAME+NL"; | |
6671 break; | |
6672 case PRINT: | |
6673 p = "PRINT"; | |
6674 break; | |
6675 case PRINT + ADD_NL: | |
6676 p = "PRINT+NL"; | |
6677 break; | |
6678 case SPRINT: | |
6679 p = "SPRINT"; | |
6680 break; | |
6681 case SPRINT + ADD_NL: | |
6682 p = "SPRINT+NL"; | |
6683 break; | |
6684 case WHITE: | |
6685 p = "WHITE"; | |
6686 break; | |
6687 case WHITE + ADD_NL: | |
6688 p = "WHITE+NL"; | |
6689 break; | |
6690 case NWHITE: | |
6691 p = "NWHITE"; | |
6692 break; | |
6693 case NWHITE + ADD_NL: | |
6694 p = "NWHITE+NL"; | |
6695 break; | |
6696 case DIGIT: | |
6697 p = "DIGIT"; | |
6698 break; | |
6699 case DIGIT + ADD_NL: | |
6700 p = "DIGIT+NL"; | |
6701 break; | |
6702 case NDIGIT: | |
6703 p = "NDIGIT"; | |
6704 break; | |
6705 case NDIGIT + ADD_NL: | |
6706 p = "NDIGIT+NL"; | |
6707 break; | |
6708 case HEX: | |
6709 p = "HEX"; | |
6710 break; | |
6711 case HEX + ADD_NL: | |
6712 p = "HEX+NL"; | |
6713 break; | |
6714 case NHEX: | |
6715 p = "NHEX"; | |
6716 break; | |
6717 case NHEX + ADD_NL: | |
6718 p = "NHEX+NL"; | |
6719 break; | |
6720 case OCTAL: | |
6721 p = "OCTAL"; | |
6722 break; | |
6723 case OCTAL + ADD_NL: | |
6724 p = "OCTAL+NL"; | |
6725 break; | |
6726 case NOCTAL: | |
6727 p = "NOCTAL"; | |
6728 break; | |
6729 case NOCTAL + ADD_NL: | |
6730 p = "NOCTAL+NL"; | |
6731 break; | |
6732 case WORD: | |
6733 p = "WORD"; | |
6734 break; | |
6735 case WORD + ADD_NL: | |
6736 p = "WORD+NL"; | |
6737 break; | |
6738 case NWORD: | |
6739 p = "NWORD"; | |
6740 break; | |
6741 case NWORD + ADD_NL: | |
6742 p = "NWORD+NL"; | |
6743 break; | |
6744 case HEAD: | |
6745 p = "HEAD"; | |
6746 break; | |
6747 case HEAD + ADD_NL: | |
6748 p = "HEAD+NL"; | |
6749 break; | |
6750 case NHEAD: | |
6751 p = "NHEAD"; | |
6752 break; | |
6753 case NHEAD + ADD_NL: | |
6754 p = "NHEAD+NL"; | |
6755 break; | |
6756 case ALPHA: | |
6757 p = "ALPHA"; | |
6758 break; | |
6759 case ALPHA + ADD_NL: | |
6760 p = "ALPHA+NL"; | |
6761 break; | |
6762 case NALPHA: | |
6763 p = "NALPHA"; | |
6764 break; | |
6765 case NALPHA + ADD_NL: | |
6766 p = "NALPHA+NL"; | |
6767 break; | |
6768 case LOWER: | |
6769 p = "LOWER"; | |
6770 break; | |
6771 case LOWER + ADD_NL: | |
6772 p = "LOWER+NL"; | |
6773 break; | |
6774 case NLOWER: | |
6775 p = "NLOWER"; | |
6776 break; | |
6777 case NLOWER + ADD_NL: | |
6778 p = "NLOWER+NL"; | |
6779 break; | |
6780 case UPPER: | |
6781 p = "UPPER"; | |
6782 break; | |
6783 case UPPER + ADD_NL: | |
6784 p = "UPPER+NL"; | |
6785 break; | |
6786 case NUPPER: | |
6787 p = "NUPPER"; | |
6788 break; | |
6789 case NUPPER + ADD_NL: | |
6790 p = "NUPPER+NL"; | |
6791 break; | |
6792 case BRANCH: | |
6793 p = "BRANCH"; | |
6794 break; | |
6795 case EXACTLY: | |
6796 p = "EXACTLY"; | |
6797 break; | |
6798 case NOTHING: | |
6799 p = "NOTHING"; | |
6800 break; | |
6801 case BACK: | |
6802 p = "BACK"; | |
6803 break; | |
6804 case END: | |
6805 p = "END"; | |
6806 break; | |
6807 case MOPEN + 0: | |
6808 p = "MATCH START"; | |
6809 break; | |
6810 case MOPEN + 1: | |
6811 case MOPEN + 2: | |
6812 case MOPEN + 3: | |
6813 case MOPEN + 4: | |
6814 case MOPEN + 5: | |
6815 case MOPEN + 6: | |
6816 case MOPEN + 7: | |
6817 case MOPEN + 8: | |
6818 case MOPEN + 9: | |
6819 sprintf(buf + STRLEN(buf), "MOPEN%d", OP(op) - MOPEN); | |
6820 p = NULL; | |
6821 break; | |
6822 case MCLOSE + 0: | |
6823 p = "MATCH END"; | |
6824 break; | |
6825 case MCLOSE + 1: | |
6826 case MCLOSE + 2: | |
6827 case MCLOSE + 3: | |
6828 case MCLOSE + 4: | |
6829 case MCLOSE + 5: | |
6830 case MCLOSE + 6: | |
6831 case MCLOSE + 7: | |
6832 case MCLOSE + 8: | |
6833 case MCLOSE + 9: | |
6834 sprintf(buf + STRLEN(buf), "MCLOSE%d", OP(op) - MCLOSE); | |
6835 p = NULL; | |
6836 break; | |
6837 case BACKREF + 1: | |
6838 case BACKREF + 2: | |
6839 case BACKREF + 3: | |
6840 case BACKREF + 4: | |
6841 case BACKREF + 5: | |
6842 case BACKREF + 6: | |
6843 case BACKREF + 7: | |
6844 case BACKREF + 8: | |
6845 case BACKREF + 9: | |
6846 sprintf(buf + STRLEN(buf), "BACKREF%d", OP(op) - BACKREF); | |
6847 p = NULL; | |
6848 break; | |
6849 case NOPEN: | |
6850 p = "NOPEN"; | |
6851 break; | |
6852 case NCLOSE: | |
6853 p = "NCLOSE"; | |
6854 break; | |
6855 #ifdef FEAT_SYN_HL | |
6856 case ZOPEN + 1: | |
6857 case ZOPEN + 2: | |
6858 case ZOPEN + 3: | |
6859 case ZOPEN + 4: | |
6860 case ZOPEN + 5: | |
6861 case ZOPEN + 6: | |
6862 case ZOPEN + 7: | |
6863 case ZOPEN + 8: | |
6864 case ZOPEN + 9: | |
6865 sprintf(buf + STRLEN(buf), "ZOPEN%d", OP(op) - ZOPEN); | |
6866 p = NULL; | |
6867 break; | |
6868 case ZCLOSE + 1: | |
6869 case ZCLOSE + 2: | |
6870 case ZCLOSE + 3: | |
6871 case ZCLOSE + 4: | |
6872 case ZCLOSE + 5: | |
6873 case ZCLOSE + 6: | |
6874 case ZCLOSE + 7: | |
6875 case ZCLOSE + 8: | |
6876 case ZCLOSE + 9: | |
6877 sprintf(buf + STRLEN(buf), "ZCLOSE%d", OP(op) - ZCLOSE); | |
6878 p = NULL; | |
6879 break; | |
6880 case ZREF + 1: | |
6881 case ZREF + 2: | |
6882 case ZREF + 3: | |
6883 case ZREF + 4: | |
6884 case ZREF + 5: | |
6885 case ZREF + 6: | |
6886 case ZREF + 7: | |
6887 case ZREF + 8: | |
6888 case ZREF + 9: | |
6889 sprintf(buf + STRLEN(buf), "ZREF%d", OP(op) - ZREF); | |
6890 p = NULL; | |
6891 break; | |
6892 #endif | |
6893 case STAR: | |
6894 p = "STAR"; | |
6895 break; | |
6896 case PLUS: | |
6897 p = "PLUS"; | |
6898 break; | |
6899 case NOMATCH: | |
6900 p = "NOMATCH"; | |
6901 break; | |
6902 case MATCH: | |
6903 p = "MATCH"; | |
6904 break; | |
6905 case BEHIND: | |
6906 p = "BEHIND"; | |
6907 break; | |
6908 case NOBEHIND: | |
6909 p = "NOBEHIND"; | |
6910 break; | |
6911 case SUBPAT: | |
6912 p = "SUBPAT"; | |
6913 break; | |
6914 case BRACE_LIMITS: | |
6915 p = "BRACE_LIMITS"; | |
6916 break; | |
6917 case BRACE_SIMPLE: | |
6918 p = "BRACE_SIMPLE"; | |
6919 break; | |
6920 case BRACE_COMPLEX + 0: | |
6921 case BRACE_COMPLEX + 1: | |
6922 case BRACE_COMPLEX + 2: | |
6923 case BRACE_COMPLEX + 3: | |
6924 case BRACE_COMPLEX + 4: | |
6925 case BRACE_COMPLEX + 5: | |
6926 case BRACE_COMPLEX + 6: | |
6927 case BRACE_COMPLEX + 7: | |
6928 case BRACE_COMPLEX + 8: | |
6929 case BRACE_COMPLEX + 9: | |
6930 sprintf(buf + STRLEN(buf), "BRACE_COMPLEX%d", OP(op) - BRACE_COMPLEX); | |
6931 p = NULL; | |
6932 break; | |
6933 #ifdef FEAT_MBYTE | |
6934 case MULTIBYTECODE: | |
6935 p = "MULTIBYTECODE"; | |
6936 break; | |
6937 #endif | |
6938 case NEWL: | |
6939 p = "NEWL"; | |
6940 break; | |
6941 default: | |
6942 sprintf(buf + STRLEN(buf), "corrupt %d", OP(op)); | |
6943 p = NULL; | |
6944 break; | |
6945 } | |
6946 if (p != NULL) | |
4444 | 6947 STRCAT(buf, p); |
6948 return (char_u *)buf; | |
7 | 6949 } |
4444 | 6950 #endif /* DEBUG */ |
7 | 6951 |
6203 | 6952 /* |
6953 * Used in a place where no * or \+ can follow. | |
6954 */ | |
6955 static int | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
6956 re_mult_next(char *what) |
6203 | 6957 { |
6958 if (re_multi_type(peekchr()) == MULTI_MULT) | |
6959 EMSG2_RET_FAIL(_("E888: (NFA regexp) cannot repeat %s"), what); | |
6960 return OK; | |
6961 } | |
6962 | |
7 | 6963 #ifdef FEAT_MBYTE |
7805
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
6964 static void mb_decompose(int c, int *c1, int *c2, int *c3); |
7 | 6965 |
6966 typedef struct | |
6967 { | |
6968 int a, b, c; | |
6969 } decomp_T; | |
6970 | |
6971 | |
6972 /* 0xfb20 - 0xfb4f */ | |
297 | 6973 static decomp_T decomp_table[0xfb4f-0xfb20+1] = |
7 | 6974 { |
6975 {0x5e2,0,0}, /* 0xfb20 alt ayin */ | |
6976 {0x5d0,0,0}, /* 0xfb21 alt alef */ | |
6977 {0x5d3,0,0}, /* 0xfb22 alt dalet */ | |
6978 {0x5d4,0,0}, /* 0xfb23 alt he */ | |
6979 {0x5db,0,0}, /* 0xfb24 alt kaf */ | |
6980 {0x5dc,0,0}, /* 0xfb25 alt lamed */ | |
6981 {0x5dd,0,0}, /* 0xfb26 alt mem-sofit */ | |
6982 {0x5e8,0,0}, /* 0xfb27 alt resh */ | |
6983 {0x5ea,0,0}, /* 0xfb28 alt tav */ | |
6984 {'+', 0, 0}, /* 0xfb29 alt plus */ | |
6985 {0x5e9, 0x5c1, 0}, /* 0xfb2a shin+shin-dot */ | |
6986 {0x5e9, 0x5c2, 0}, /* 0xfb2b shin+sin-dot */ | |
6987 {0x5e9, 0x5c1, 0x5bc}, /* 0xfb2c shin+shin-dot+dagesh */ | |
6988 {0x5e9, 0x5c2, 0x5bc}, /* 0xfb2d shin+sin-dot+dagesh */ | |
6989 {0x5d0, 0x5b7, 0}, /* 0xfb2e alef+patah */ | |
6990 {0x5d0, 0x5b8, 0}, /* 0xfb2f alef+qamats */ | |
6991 {0x5d0, 0x5b4, 0}, /* 0xfb30 alef+hiriq */ | |
6992 {0x5d1, 0x5bc, 0}, /* 0xfb31 bet+dagesh */ | |
6993 {0x5d2, 0x5bc, 0}, /* 0xfb32 gimel+dagesh */ | |
6994 {0x5d3, 0x5bc, 0}, /* 0xfb33 dalet+dagesh */ | |
6995 {0x5d4, 0x5bc, 0}, /* 0xfb34 he+dagesh */ | |
6996 {0x5d5, 0x5bc, 0}, /* 0xfb35 vav+dagesh */ | |
6997 {0x5d6, 0x5bc, 0}, /* 0xfb36 zayin+dagesh */ | |
6998 {0xfb37, 0, 0}, /* 0xfb37 -- UNUSED */ | |
6999 {0x5d8, 0x5bc, 0}, /* 0xfb38 tet+dagesh */ | |
7000 {0x5d9, 0x5bc, 0}, /* 0xfb39 yud+dagesh */ | |
7001 {0x5da, 0x5bc, 0}, /* 0xfb3a kaf sofit+dagesh */ | |
7002 {0x5db, 0x5bc, 0}, /* 0xfb3b kaf+dagesh */ | |
7003 {0x5dc, 0x5bc, 0}, /* 0xfb3c lamed+dagesh */ | |
7004 {0xfb3d, 0, 0}, /* 0xfb3d -- UNUSED */ | |
7005 {0x5de, 0x5bc, 0}, /* 0xfb3e mem+dagesh */ | |
7006 {0xfb3f, 0, 0}, /* 0xfb3f -- UNUSED */ | |
7007 {0x5e0, 0x5bc, 0}, /* 0xfb40 nun+dagesh */ | |
7008 {0x5e1, 0x5bc, 0}, /* 0xfb41 samech+dagesh */ | |
7009 {0xfb42, 0, 0}, /* 0xfb42 -- UNUSED */ | |
7010 {0x5e3, 0x5bc, 0}, /* 0xfb43 pe sofit+dagesh */ | |
7011 {0x5e4, 0x5bc,0}, /* 0xfb44 pe+dagesh */ | |
7012 {0xfb45, 0, 0}, /* 0xfb45 -- UNUSED */ | |
7013 {0x5e6, 0x5bc, 0}, /* 0xfb46 tsadi+dagesh */ | |
7014 {0x5e7, 0x5bc, 0}, /* 0xfb47 qof+dagesh */ | |
7015 {0x5e8, 0x5bc, 0}, /* 0xfb48 resh+dagesh */ | |
7016 {0x5e9, 0x5bc, 0}, /* 0xfb49 shin+dagesh */ | |
7017 {0x5ea, 0x5bc, 0}, /* 0xfb4a tav+dagesh */ | |
7018 {0x5d5, 0x5b9, 0}, /* 0xfb4b vav+holam */ | |
7019 {0x5d1, 0x5bf, 0}, /* 0xfb4c bet+rafe */ | |
7020 {0x5db, 0x5bf, 0}, /* 0xfb4d kaf+rafe */ | |
7021 {0x5e4, 0x5bf, 0}, /* 0xfb4e pe+rafe */ | |
7022 {0x5d0, 0x5dc, 0} /* 0xfb4f alef-lamed */ | |
7023 }; | |
7024 | |
7025 static void | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
7026 mb_decompose(int c, int *c1, int *c2, int *c3) |
7 | 7027 { |
7028 decomp_T d; | |
7029 | |
4505
d037b9cbdaaa
updated for version 7.3.1000
Bram Moolenaar <bram@vim.org>
parents:
4466
diff
changeset
|
7030 if (c >= 0xfb20 && c <= 0xfb4f) |
7 | 7031 { |
7032 d = decomp_table[c - 0xfb20]; | |
7033 *c1 = d.a; | |
7034 *c2 = d.b; | |
7035 *c3 = d.c; | |
7036 } | |
7037 else | |
7038 { | |
7039 *c1 = c; | |
7040 *c2 = *c3 = 0; | |
7041 } | |
7042 } | |
7043 #endif | |
7044 | |
7045 /* | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7046 * Compare two strings, ignore case if rex.reg_ic set. |
7 | 7047 * Return 0 if strings match, non-zero otherwise. |
7048 * Correct the length "*n" when composing characters are ignored. | |
7049 */ | |
7050 static int | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
7051 cstrncmp(char_u *s1, char_u *s2, int *n) |
7 | 7052 { |
7053 int result; | |
7054 | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7055 if (!rex.reg_ic) |
7 | 7056 result = STRNCMP(s1, s2, *n); |
7057 else | |
7058 result = MB_STRNICMP(s1, s2, *n); | |
7059 | |
7060 #ifdef FEAT_MBYTE | |
7061 /* if it failed and it's utf8 and we want to combineignore: */ | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7062 if (result != 0 && enc_utf8 && rex.reg_icombine) |
7 | 7063 { |
7064 char_u *str1, *str2; | |
7065 int c1, c2, c11, c12; | |
7066 int junk; | |
7067 | |
7068 /* we have to handle the strcmp ourselves, since it is necessary to | |
7069 * deal with the composing characters by ignoring them: */ | |
7070 str1 = s1; | |
7071 str2 = s2; | |
7072 c1 = c2 = 0; | |
507 | 7073 while ((int)(str1 - s1) < *n) |
7 | 7074 { |
7075 c1 = mb_ptr2char_adv(&str1); | |
7076 c2 = mb_ptr2char_adv(&str2); | |
7077 | |
7078 /* decompose the character if necessary, into 'base' characters | |
7079 * because I don't care about Arabic, I will hard-code the Hebrew | |
7080 * which I *do* care about! So sue me... */ | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7081 if (c1 != c2 && (!rex.reg_ic || utf_fold(c1) != utf_fold(c2))) |
7 | 7082 { |
7083 /* decomposition necessary? */ | |
7084 mb_decompose(c1, &c11, &junk, &junk); | |
7085 mb_decompose(c2, &c12, &junk, &junk); | |
7086 c1 = c11; | |
7087 c2 = c12; | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7088 if (c11 != c12 |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7089 && (!rex.reg_ic || utf_fold(c11) != utf_fold(c12))) |
7 | 7090 break; |
7091 } | |
7092 } | |
7093 result = c2 - c1; | |
7094 if (result == 0) | |
7095 *n = (int)(str2 - s2); | |
7096 } | |
7097 #endif | |
7098 | |
7099 return result; | |
7100 } | |
7101 | |
7102 /* | |
7103 * cstrchr: This function is used a lot for simple searches, keep it fast! | |
7104 */ | |
7105 static char_u * | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
7106 cstrchr(char_u *s, int c) |
7 | 7107 { |
7108 char_u *p; | |
7109 int cc; | |
7110 | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7111 if (!rex.reg_ic |
7 | 7112 #ifdef FEAT_MBYTE |
7113 || (!enc_utf8 && mb_char2len(c) > 1) | |
7114 #endif | |
7115 ) | |
7116 return vim_strchr(s, c); | |
7117 | |
7118 /* tolower() and toupper() can be slow, comparing twice should be a lot | |
7119 * faster (esp. when using MS Visual C++!). | |
7120 * For UTF-8 need to use folded case. */ | |
7121 #ifdef FEAT_MBYTE | |
7122 if (enc_utf8 && c > 0x80) | |
7123 cc = utf_fold(c); | |
7124 else | |
7125 #endif | |
1347 | 7126 if (MB_ISUPPER(c)) |
7127 cc = MB_TOLOWER(c); | |
7128 else if (MB_ISLOWER(c)) | |
7129 cc = MB_TOUPPER(c); | |
7 | 7130 else |
7131 return vim_strchr(s, c); | |
7132 | |
7133 #ifdef FEAT_MBYTE | |
7134 if (has_mbyte) | |
7135 { | |
474 | 7136 for (p = s; *p != NUL; p += (*mb_ptr2len)(p)) |
7 | 7137 { |
7138 if (enc_utf8 && c > 0x80) | |
7139 { | |
7140 if (utf_fold(utf_ptr2char(p)) == cc) | |
7141 return p; | |
7142 } | |
7143 else if (*p == c || *p == cc) | |
7144 return p; | |
7145 } | |
7146 } | |
7147 else | |
7148 #endif | |
7149 /* Faster version for when there are no multi-byte characters. */ | |
7150 for (p = s; *p != NUL; ++p) | |
7151 if (*p == c || *p == cc) | |
7152 return p; | |
7153 | |
7154 return NULL; | |
7155 } | |
7156 | |
7157 /*************************************************************** | |
7158 * regsub stuff * | |
7159 ***************************************************************/ | |
7160 | |
7161 /* | |
7162 * We should define ftpr as a pointer to a function returning a pointer to | |
7163 * a function returning a pointer to a function ... | |
7164 * This is impossible, so we declare a pointer to a function returning a | |
7165 * pointer to a function returning void. This should work for all compilers. | |
7166 */ | |
7805
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
7167 typedef void (*(*fptr_T)(int *, int))(); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
7168 |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
7169 static fptr_T do_upper(int *, int); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
7170 static fptr_T do_Upper(int *, int); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
7171 static fptr_T do_lower(int *, int); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
7172 static fptr_T do_Lower(int *, int); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
7173 |
9589
bf204ab1ce7d
commit https://github.com/vim/vim/commit/72ab729c3dcdea0fba44d8e676602c847e841bcd
Christian Brabandt <cb@256bit.org>
parents:
9015
diff
changeset
|
7174 static int vim_regsub_both(char_u *source, typval_T *expr, char_u *dest, int copy, int magic, int backslash); |
7 | 7175 |
772 | 7176 static fptr_T |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
7177 do_upper(int *d, int c) |
7 | 7178 { |
772 | 7179 *d = MB_TOUPPER(c); |
7180 | |
7181 return (fptr_T)NULL; | |
7 | 7182 } |
7183 | |
772 | 7184 static fptr_T |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
7185 do_Upper(int *d, int c) |
7 | 7186 { |
772 | 7187 *d = MB_TOUPPER(c); |
7188 | |
7189 return (fptr_T)do_Upper; | |
7190 } | |
7191 | |
7192 static fptr_T | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
7193 do_lower(int *d, int c) |
772 | 7194 { |
7195 *d = MB_TOLOWER(c); | |
7196 | |
7197 return (fptr_T)NULL; | |
7198 } | |
7199 | |
7200 static fptr_T | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
7201 do_Lower(int *d, int c) |
772 | 7202 { |
7203 *d = MB_TOLOWER(c); | |
7204 | |
7205 return (fptr_T)do_Lower; | |
7 | 7206 } |
7207 | |
7208 /* | |
7209 * regtilde(): Replace tildes in the pattern by the old pattern. | |
7210 * | |
7211 * Short explanation of the tilde: It stands for the previous replacement | |
7212 * pattern. If that previous pattern also contains a ~ we should go back a | |
7213 * step further... But we insert the previous pattern into the current one | |
7214 * and remember that. | |
772 | 7215 * This still does not handle the case where "magic" changes. So require the |
7216 * user to keep his hands off of "magic". | |
7 | 7217 * |
7218 * The tildes are parsed once before the first call to vim_regsub(). | |
7219 */ | |
7220 char_u * | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
7221 regtilde(char_u *source, int magic) |
7 | 7222 { |
7223 char_u *newsub = source; | |
7224 char_u *tmpsub; | |
7225 char_u *p; | |
7226 int len; | |
7227 int prevlen; | |
7228 | |
7229 for (p = newsub; *p; ++p) | |
7230 { | |
7231 if ((*p == '~' && magic) || (*p == '\\' && *(p + 1) == '~' && !magic)) | |
7232 { | |
7233 if (reg_prev_sub != NULL) | |
7234 { | |
7235 /* length = len(newsub) - 1 + len(prev_sub) + 1 */ | |
7236 prevlen = (int)STRLEN(reg_prev_sub); | |
7237 tmpsub = alloc((unsigned)(STRLEN(newsub) + prevlen)); | |
7238 if (tmpsub != NULL) | |
7239 { | |
7240 /* copy prefix */ | |
7241 len = (int)(p - newsub); /* not including ~ */ | |
7242 mch_memmove(tmpsub, newsub, (size_t)len); | |
1209 | 7243 /* interpret tilde */ |
7 | 7244 mch_memmove(tmpsub + len, reg_prev_sub, (size_t)prevlen); |
7245 /* copy postfix */ | |
7246 if (!magic) | |
7247 ++p; /* back off \ */ | |
7248 STRCPY(tmpsub + len + prevlen, p + 1); | |
7249 | |
7250 if (newsub != source) /* already allocated newsub */ | |
7251 vim_free(newsub); | |
7252 newsub = tmpsub; | |
7253 p = newsub + len + prevlen; | |
7254 } | |
7255 } | |
7256 else if (magic) | |
1621 | 7257 STRMOVE(p, p + 1); /* remove '~' */ |
7 | 7258 else |
1621 | 7259 STRMOVE(p, p + 2); /* remove '\~' */ |
7 | 7260 --p; |
7261 } | |
7262 else | |
7263 { | |
7264 if (*p == '\\' && p[1]) /* skip escaped characters */ | |
7265 ++p; | |
7266 #ifdef FEAT_MBYTE | |
7267 if (has_mbyte) | |
474 | 7268 p += (*mb_ptr2len)(p) - 1; |
7 | 7269 #endif |
7270 } | |
7271 } | |
7272 | |
7273 vim_free(reg_prev_sub); | |
7274 if (newsub != source) /* newsub was allocated, just keep it */ | |
7275 reg_prev_sub = newsub; | |
7276 else /* no ~ found, need to save newsub */ | |
7277 reg_prev_sub = vim_strsave(newsub); | |
7278 return newsub; | |
7279 } | |
7280 | |
7281 #ifdef FEAT_EVAL | |
7282 static int can_f_submatch = FALSE; /* TRUE when submatch() can be used */ | |
7283 | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7284 /* These pointers are used for reg_submatch(). Needed for when the |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7285 * substitution string is an expression that contains a call to substitute() |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7286 * and submatch(). */ |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7287 typedef struct { |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7288 regmatch_T *sm_match; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7289 regmmatch_T *sm_mmatch; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7290 linenr_T sm_firstlnum; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7291 linenr_T sm_maxline; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7292 int sm_line_lbr; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7293 } regsubmatch_T; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7294 |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7295 static regsubmatch_T rsm; /* can only be used when can_f_submatch is TRUE */ |
7 | 7296 #endif |
7297 | |
7298 #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO) | |
9626
172131507c85
commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents:
9589
diff
changeset
|
7299 |
172131507c85
commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents:
9589
diff
changeset
|
7300 /* |
172131507c85
commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents:
9589
diff
changeset
|
7301 * Put the submatches in "argv[0]" which is a list passed into call_func() by |
172131507c85
commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents:
9589
diff
changeset
|
7302 * vim_regsub_both(). |
172131507c85
commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents:
9589
diff
changeset
|
7303 */ |
172131507c85
commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents:
9589
diff
changeset
|
7304 static int |
172131507c85
commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents:
9589
diff
changeset
|
7305 fill_submatch_list(int argc UNUSED, typval_T *argv, int argcount) |
172131507c85
commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents:
9589
diff
changeset
|
7306 { |
172131507c85
commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents:
9589
diff
changeset
|
7307 listitem_T *li; |
172131507c85
commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents:
9589
diff
changeset
|
7308 int i; |
172131507c85
commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents:
9589
diff
changeset
|
7309 char_u *s; |
172131507c85
commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents:
9589
diff
changeset
|
7310 |
172131507c85
commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents:
9589
diff
changeset
|
7311 if (argcount == 0) |
172131507c85
commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents:
9589
diff
changeset
|
7312 /* called function doesn't take an argument */ |
172131507c85
commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents:
9589
diff
changeset
|
7313 return 0; |
172131507c85
commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents:
9589
diff
changeset
|
7314 |
172131507c85
commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents:
9589
diff
changeset
|
7315 /* Relies on sl_list to be the first item in staticList10_T. */ |
172131507c85
commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents:
9589
diff
changeset
|
7316 init_static_list((staticList10_T *)(argv->vval.v_list)); |
172131507c85
commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents:
9589
diff
changeset
|
7317 |
172131507c85
commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents:
9589
diff
changeset
|
7318 /* There are always 10 list items in staticList10_T. */ |
172131507c85
commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents:
9589
diff
changeset
|
7319 li = argv->vval.v_list->lv_first; |
172131507c85
commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents:
9589
diff
changeset
|
7320 for (i = 0; i < 10; ++i) |
172131507c85
commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents:
9589
diff
changeset
|
7321 { |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7322 s = rsm.sm_match->startp[i]; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7323 if (s == NULL || rsm.sm_match->endp[i] == NULL) |
9626
172131507c85
commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents:
9589
diff
changeset
|
7324 s = NULL; |
172131507c85
commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents:
9589
diff
changeset
|
7325 else |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7326 s = vim_strnsave(s, (int)(rsm.sm_match->endp[i] - s)); |
9626
172131507c85
commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents:
9589
diff
changeset
|
7327 li->li_tv.v_type = VAR_STRING; |
172131507c85
commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents:
9589
diff
changeset
|
7328 li->li_tv.vval.v_string = s; |
172131507c85
commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents:
9589
diff
changeset
|
7329 li = li->li_next; |
172131507c85
commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents:
9589
diff
changeset
|
7330 } |
172131507c85
commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents:
9589
diff
changeset
|
7331 return 1; |
172131507c85
commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents:
9589
diff
changeset
|
7332 } |
172131507c85
commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents:
9589
diff
changeset
|
7333 |
172131507c85
commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents:
9589
diff
changeset
|
7334 static void |
172131507c85
commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents:
9589
diff
changeset
|
7335 clear_submatch_list(staticList10_T *sl) |
172131507c85
commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents:
9589
diff
changeset
|
7336 { |
172131507c85
commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents:
9589
diff
changeset
|
7337 int i; |
172131507c85
commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents:
9589
diff
changeset
|
7338 |
172131507c85
commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents:
9589
diff
changeset
|
7339 for (i = 0; i < 10; ++i) |
172131507c85
commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents:
9589
diff
changeset
|
7340 vim_free(sl->sl_items[i].li_tv.vval.v_string); |
172131507c85
commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents:
9589
diff
changeset
|
7341 } |
172131507c85
commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents:
9589
diff
changeset
|
7342 |
7 | 7343 /* |
7344 * vim_regsub() - perform substitutions after a vim_regexec() or | |
7345 * vim_regexec_multi() match. | |
7346 * | |
7347 * If "copy" is TRUE really copy into "dest". | |
7348 * If "copy" is FALSE nothing is copied, this is just to find out the length | |
7349 * of the result. | |
7350 * | |
7351 * If "backslash" is TRUE, a backslash will be removed later, need to double | |
7352 * them to keep them, and insert a backslash before a CR to avoid it being | |
7353 * replaced with a line break later. | |
7354 * | |
7355 * Note: The matched text must not change between the call of | |
7356 * vim_regexec()/vim_regexec_multi() and vim_regsub()! It would make the back | |
7357 * references invalid! | |
7358 * | |
7359 * Returns the size of the replacement, including terminating NUL. | |
7360 */ | |
7361 int | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
7362 vim_regsub( |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
7363 regmatch_T *rmp, |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
7364 char_u *source, |
9589
bf204ab1ce7d
commit https://github.com/vim/vim/commit/72ab729c3dcdea0fba44d8e676602c847e841bcd
Christian Brabandt <cb@256bit.org>
parents:
9015
diff
changeset
|
7365 typval_T *expr, |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
7366 char_u *dest, |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
7367 int copy, |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
7368 int magic, |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
7369 int backslash) |
7 | 7370 { |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7371 int result; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7372 regexec_T rex_save; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7373 int rex_in_use_save = rex_in_use; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7374 |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7375 if (rex_in_use) |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7376 /* Being called recursively, save the state. */ |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7377 rex_save = rex; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7378 rex_in_use = TRUE; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7379 |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7380 rex.reg_match = rmp; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7381 rex.reg_mmatch = NULL; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7382 rex.reg_maxline = 0; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7383 rex.reg_buf = curbuf; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7384 rex.reg_line_lbr = TRUE; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7385 result = vim_regsub_both(source, expr, dest, copy, magic, backslash); |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7386 |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7387 rex_in_use = rex_in_use_save; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7388 if (rex_in_use) |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7389 rex = rex_save; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7390 |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7391 return result; |
7 | 7392 } |
7393 #endif | |
7394 | |
7395 int | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
7396 vim_regsub_multi( |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
7397 regmmatch_T *rmp, |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
7398 linenr_T lnum, |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
7399 char_u *source, |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
7400 char_u *dest, |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
7401 int copy, |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
7402 int magic, |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
7403 int backslash) |
7 | 7404 { |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7405 int result; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7406 regexec_T rex_save; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7407 int rex_in_use_save = rex_in_use; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7408 |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7409 if (rex_in_use) |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7410 /* Being called recursively, save the state. */ |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7411 rex_save = rex; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7412 rex_in_use = TRUE; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7413 |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7414 rex.reg_match = NULL; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7415 rex.reg_mmatch = rmp; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7416 rex.reg_buf = curbuf; /* always works on the current buffer! */ |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7417 rex.reg_firstlnum = lnum; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7418 rex.reg_maxline = curbuf->b_ml.ml_line_count - lnum; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7419 rex.reg_line_lbr = FALSE; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7420 result = vim_regsub_both(source, NULL, dest, copy, magic, backslash); |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7421 |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7422 rex_in_use = rex_in_use_save; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7423 if (rex_in_use) |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7424 rex = rex_save; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7425 |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7426 return result; |
7 | 7427 } |
7428 | |
7429 static int | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
7430 vim_regsub_both( |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
7431 char_u *source, |
9589
bf204ab1ce7d
commit https://github.com/vim/vim/commit/72ab729c3dcdea0fba44d8e676602c847e841bcd
Christian Brabandt <cb@256bit.org>
parents:
9015
diff
changeset
|
7432 typval_T *expr, |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
7433 char_u *dest, |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
7434 int copy, |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
7435 int magic, |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
7436 int backslash) |
7 | 7437 { |
7438 char_u *src; | |
7439 char_u *dst; | |
7440 char_u *s; | |
7441 int c; | |
772 | 7442 int cc; |
7 | 7443 int no = -1; |
4244 | 7444 fptr_T func_all = (fptr_T)NULL; |
7445 fptr_T func_one = (fptr_T)NULL; | |
7 | 7446 linenr_T clnum = 0; /* init for GCC */ |
7447 int len = 0; /* init for GCC */ | |
7448 #ifdef FEAT_EVAL | |
9589
bf204ab1ce7d
commit https://github.com/vim/vim/commit/72ab729c3dcdea0fba44d8e676602c847e841bcd
Christian Brabandt <cb@256bit.org>
parents:
9015
diff
changeset
|
7449 static char_u *eval_result = NULL; |
7 | 7450 #endif |
7451 | |
7452 /* Be paranoid... */ | |
9589
bf204ab1ce7d
commit https://github.com/vim/vim/commit/72ab729c3dcdea0fba44d8e676602c847e841bcd
Christian Brabandt <cb@256bit.org>
parents:
9015
diff
changeset
|
7453 if ((source == NULL && expr == NULL) || dest == NULL) |
7 | 7454 { |
7455 EMSG(_(e_null)); | |
7456 return 0; | |
7457 } | |
7458 if (prog_magic_wrong()) | |
7459 return 0; | |
7460 src = source; | |
7461 dst = dest; | |
7462 | |
7463 /* | |
7464 * When the substitute part starts with "\=" evaluate it as an expression. | |
7465 */ | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7466 if (expr != NULL || (source[0] == '\\' && source[1] == '=')) |
7 | 7467 { |
7468 #ifdef FEAT_EVAL | |
7469 /* To make sure that the length doesn't change between checking the | |
7470 * length and copying the string, and to speed up things, the | |
7471 * resulting string is saved from the call with "copy" == FALSE to the | |
7472 * call with "copy" == TRUE. */ | |
7473 if (copy) | |
7474 { | |
7475 if (eval_result != NULL) | |
7476 { | |
7477 STRCPY(dest, eval_result); | |
7478 dst += STRLEN(eval_result); | |
7479 vim_free(eval_result); | |
7480 eval_result = NULL; | |
7481 } | |
7482 } | |
7483 else | |
7484 { | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7485 int prev_can_f_submatch = can_f_submatch; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7486 regsubmatch_T rsm_save; |
7 | 7487 |
7488 vim_free(eval_result); | |
7489 | |
7490 /* The expression may contain substitute(), which calls us | |
7491 * recursively. Make sure submatch() gets the text from the first | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7492 * level. */ |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7493 if (can_f_submatch) |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7494 rsm_save = rsm; |
7 | 7495 can_f_submatch = TRUE; |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7496 rsm.sm_match = rex.reg_match; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7497 rsm.sm_mmatch = rex.reg_mmatch; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7498 rsm.sm_firstlnum = rex.reg_firstlnum; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7499 rsm.sm_maxline = rex.reg_maxline; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7500 rsm.sm_line_lbr = rex.reg_line_lbr; |
7 | 7501 |
9589
bf204ab1ce7d
commit https://github.com/vim/vim/commit/72ab729c3dcdea0fba44d8e676602c847e841bcd
Christian Brabandt <cb@256bit.org>
parents:
9015
diff
changeset
|
7502 if (expr != NULL) |
bf204ab1ce7d
commit https://github.com/vim/vim/commit/72ab729c3dcdea0fba44d8e676602c847e841bcd
Christian Brabandt <cb@256bit.org>
parents:
9015
diff
changeset
|
7503 { |
9626
172131507c85
commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents:
9589
diff
changeset
|
7504 typval_T argv[2]; |
9589
bf204ab1ce7d
commit https://github.com/vim/vim/commit/72ab729c3dcdea0fba44d8e676602c847e841bcd
Christian Brabandt <cb@256bit.org>
parents:
9015
diff
changeset
|
7505 int dummy; |
bf204ab1ce7d
commit https://github.com/vim/vim/commit/72ab729c3dcdea0fba44d8e676602c847e841bcd
Christian Brabandt <cb@256bit.org>
parents:
9015
diff
changeset
|
7506 char_u buf[NUMBUFLEN]; |
bf204ab1ce7d
commit https://github.com/vim/vim/commit/72ab729c3dcdea0fba44d8e676602c847e841bcd
Christian Brabandt <cb@256bit.org>
parents:
9015
diff
changeset
|
7507 typval_T rettv; |
9626
172131507c85
commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents:
9589
diff
changeset
|
7508 staticList10_T matchList; |
9589
bf204ab1ce7d
commit https://github.com/vim/vim/commit/72ab729c3dcdea0fba44d8e676602c847e841bcd
Christian Brabandt <cb@256bit.org>
parents:
9015
diff
changeset
|
7509 |
bf204ab1ce7d
commit https://github.com/vim/vim/commit/72ab729c3dcdea0fba44d8e676602c847e841bcd
Christian Brabandt <cb@256bit.org>
parents:
9015
diff
changeset
|
7510 rettv.v_type = VAR_STRING; |
bf204ab1ce7d
commit https://github.com/vim/vim/commit/72ab729c3dcdea0fba44d8e676602c847e841bcd
Christian Brabandt <cb@256bit.org>
parents:
9015
diff
changeset
|
7511 rettv.vval.v_string = NULL; |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7512 argv[0].v_type = VAR_LIST; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7513 argv[0].vval.v_list = &matchList.sl_list; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7514 matchList.sl_list.lv_len = 0; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7515 if (expr->v_type == VAR_FUNC) |
9589
bf204ab1ce7d
commit https://github.com/vim/vim/commit/72ab729c3dcdea0fba44d8e676602c847e841bcd
Christian Brabandt <cb@256bit.org>
parents:
9015
diff
changeset
|
7516 { |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7517 s = expr->vval.v_string; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7518 call_func(s, (int)STRLEN(s), &rettv, |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7519 1, argv, fill_submatch_list, |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7520 0L, 0L, &dummy, TRUE, NULL, NULL); |
9589
bf204ab1ce7d
commit https://github.com/vim/vim/commit/72ab729c3dcdea0fba44d8e676602c847e841bcd
Christian Brabandt <cb@256bit.org>
parents:
9015
diff
changeset
|
7521 } |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7522 else if (expr->v_type == VAR_PARTIAL) |
9589
bf204ab1ce7d
commit https://github.com/vim/vim/commit/72ab729c3dcdea0fba44d8e676602c847e841bcd
Christian Brabandt <cb@256bit.org>
parents:
9015
diff
changeset
|
7523 { |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7524 partial_T *partial = expr->vval.v_partial; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7525 |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7526 s = partial_name(partial); |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7527 call_func(s, (int)STRLEN(s), &rettv, |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7528 1, argv, fill_submatch_list, |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7529 0L, 0L, &dummy, TRUE, partial, NULL); |
9589
bf204ab1ce7d
commit https://github.com/vim/vim/commit/72ab729c3dcdea0fba44d8e676602c847e841bcd
Christian Brabandt <cb@256bit.org>
parents:
9015
diff
changeset
|
7530 } |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7531 if (matchList.sl_list.lv_len > 0) |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7532 /* fill_submatch_list() was called */ |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7533 clear_submatch_list(&matchList); |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7534 |
9589
bf204ab1ce7d
commit https://github.com/vim/vim/commit/72ab729c3dcdea0fba44d8e676602c847e841bcd
Christian Brabandt <cb@256bit.org>
parents:
9015
diff
changeset
|
7535 eval_result = get_tv_string_buf_chk(&rettv, buf); |
bf204ab1ce7d
commit https://github.com/vim/vim/commit/72ab729c3dcdea0fba44d8e676602c847e841bcd
Christian Brabandt <cb@256bit.org>
parents:
9015
diff
changeset
|
7536 if (eval_result != NULL) |
bf204ab1ce7d
commit https://github.com/vim/vim/commit/72ab729c3dcdea0fba44d8e676602c847e841bcd
Christian Brabandt <cb@256bit.org>
parents:
9015
diff
changeset
|
7537 eval_result = vim_strsave(eval_result); |
9626
172131507c85
commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents:
9589
diff
changeset
|
7538 clear_tv(&rettv); |
9589
bf204ab1ce7d
commit https://github.com/vim/vim/commit/72ab729c3dcdea0fba44d8e676602c847e841bcd
Christian Brabandt <cb@256bit.org>
parents:
9015
diff
changeset
|
7539 } |
bf204ab1ce7d
commit https://github.com/vim/vim/commit/72ab729c3dcdea0fba44d8e676602c847e841bcd
Christian Brabandt <cb@256bit.org>
parents:
9015
diff
changeset
|
7540 else |
bf204ab1ce7d
commit https://github.com/vim/vim/commit/72ab729c3dcdea0fba44d8e676602c847e841bcd
Christian Brabandt <cb@256bit.org>
parents:
9015
diff
changeset
|
7541 eval_result = eval_to_string(source + 2, NULL, TRUE); |
bf204ab1ce7d
commit https://github.com/vim/vim/commit/72ab729c3dcdea0fba44d8e676602c847e841bcd
Christian Brabandt <cb@256bit.org>
parents:
9015
diff
changeset
|
7542 |
7 | 7543 if (eval_result != NULL) |
7544 { | |
2125
b8744d1982d1
updated for version 7.2.407
Bram Moolenaar <bram@zimbu.org>
parents:
2012
diff
changeset
|
7545 int had_backslash = FALSE; |
b8744d1982d1
updated for version 7.2.407
Bram Moolenaar <bram@zimbu.org>
parents:
2012
diff
changeset
|
7546 |
39 | 7547 for (s = eval_result; *s != NUL; mb_ptr_adv(s)) |
7 | 7548 { |
2904 | 7549 /* Change NL to CR, so that it becomes a line break, |
7550 * unless called from vim_regexec_nl(). | |
7 | 7551 * Skip over a backslashed character. */ |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7552 if (*s == NL && !rsm.sm_line_lbr) |
7 | 7553 *s = CAR; |
7554 else if (*s == '\\' && s[1] != NUL) | |
2125
b8744d1982d1
updated for version 7.2.407
Bram Moolenaar <bram@zimbu.org>
parents:
2012
diff
changeset
|
7555 { |
7 | 7556 ++s; |
2173 | 7557 /* Change NL to CR here too, so that this works: |
7558 * :s/abc\\\ndef/\="aaa\\\nbbb"/ on text: | |
7559 * abc\ | |
7560 * def | |
2904 | 7561 * Not when called from vim_regexec_nl(). |
2173 | 7562 */ |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7563 if (*s == NL && !rsm.sm_line_lbr) |
2173 | 7564 *s = CAR; |
2125
b8744d1982d1
updated for version 7.2.407
Bram Moolenaar <bram@zimbu.org>
parents:
2012
diff
changeset
|
7565 had_backslash = TRUE; |
b8744d1982d1
updated for version 7.2.407
Bram Moolenaar <bram@zimbu.org>
parents:
2012
diff
changeset
|
7566 } |
b8744d1982d1
updated for version 7.2.407
Bram Moolenaar <bram@zimbu.org>
parents:
2012
diff
changeset
|
7567 } |
b8744d1982d1
updated for version 7.2.407
Bram Moolenaar <bram@zimbu.org>
parents:
2012
diff
changeset
|
7568 if (had_backslash && backslash) |
b8744d1982d1
updated for version 7.2.407
Bram Moolenaar <bram@zimbu.org>
parents:
2012
diff
changeset
|
7569 { |
b8744d1982d1
updated for version 7.2.407
Bram Moolenaar <bram@zimbu.org>
parents:
2012
diff
changeset
|
7570 /* Backslashes will be consumed, need to double them. */ |
b8744d1982d1
updated for version 7.2.407
Bram Moolenaar <bram@zimbu.org>
parents:
2012
diff
changeset
|
7571 s = vim_strsave_escaped(eval_result, (char_u *)"\\"); |
b8744d1982d1
updated for version 7.2.407
Bram Moolenaar <bram@zimbu.org>
parents:
2012
diff
changeset
|
7572 if (s != NULL) |
b8744d1982d1
updated for version 7.2.407
Bram Moolenaar <bram@zimbu.org>
parents:
2012
diff
changeset
|
7573 { |
b8744d1982d1
updated for version 7.2.407
Bram Moolenaar <bram@zimbu.org>
parents:
2012
diff
changeset
|
7574 vim_free(eval_result); |
b8744d1982d1
updated for version 7.2.407
Bram Moolenaar <bram@zimbu.org>
parents:
2012
diff
changeset
|
7575 eval_result = s; |
b8744d1982d1
updated for version 7.2.407
Bram Moolenaar <bram@zimbu.org>
parents:
2012
diff
changeset
|
7576 } |
7 | 7577 } |
7578 | |
7579 dst += STRLEN(eval_result); | |
7580 } | |
7581 | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7582 can_f_submatch = prev_can_f_submatch; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7583 if (can_f_submatch) |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7584 rsm = rsm_save; |
7 | 7585 } |
7586 #endif | |
7587 } | |
7588 else | |
7589 while ((c = *src++) != NUL) | |
7590 { | |
7591 if (c == '&' && magic) | |
7592 no = 0; | |
7593 else if (c == '\\' && *src != NUL) | |
7594 { | |
7595 if (*src == '&' && !magic) | |
7596 { | |
7597 ++src; | |
7598 no = 0; | |
7599 } | |
7600 else if ('0' <= *src && *src <= '9') | |
7601 { | |
7602 no = *src++ - '0'; | |
7603 } | |
7604 else if (vim_strchr((char_u *)"uUlLeE", *src)) | |
7605 { | |
7606 switch (*src++) | |
7607 { | |
4244 | 7608 case 'u': func_one = (fptr_T)do_upper; |
7 | 7609 continue; |
4244 | 7610 case 'U': func_all = (fptr_T)do_Upper; |
7 | 7611 continue; |
4244 | 7612 case 'l': func_one = (fptr_T)do_lower; |
7 | 7613 continue; |
4244 | 7614 case 'L': func_all = (fptr_T)do_Lower; |
7 | 7615 continue; |
7616 case 'e': | |
4244 | 7617 case 'E': func_one = func_all = (fptr_T)NULL; |
7 | 7618 continue; |
7619 } | |
7620 } | |
7621 } | |
7622 if (no < 0) /* Ordinary character. */ | |
7623 { | |
798 | 7624 if (c == K_SPECIAL && src[0] != NUL && src[1] != NUL) |
7625 { | |
1209 | 7626 /* Copy a special key as-is. */ |
798 | 7627 if (copy) |
7628 { | |
7629 *dst++ = c; | |
7630 *dst++ = *src++; | |
7631 *dst++ = *src++; | |
7632 } | |
7633 else | |
7634 { | |
7635 dst += 3; | |
7636 src += 2; | |
7637 } | |
7638 continue; | |
7639 } | |
7640 | |
7 | 7641 if (c == '\\' && *src != NUL) |
7642 { | |
7643 /* Check for abbreviations -- webb */ | |
7644 switch (*src) | |
7645 { | |
7646 case 'r': c = CAR; ++src; break; | |
7647 case 'n': c = NL; ++src; break; | |
7648 case 't': c = TAB; ++src; break; | |
7649 /* Oh no! \e already has meaning in subst pat :-( */ | |
7650 /* case 'e': c = ESC; ++src; break; */ | |
7651 case 'b': c = Ctrl_H; ++src; break; | |
7652 | |
7653 /* If "backslash" is TRUE the backslash will be removed | |
7654 * later. Used to insert a literal CR. */ | |
7655 default: if (backslash) | |
7656 { | |
7657 if (copy) | |
7658 *dst = '\\'; | |
7659 ++dst; | |
7660 } | |
7661 c = *src++; | |
7662 } | |
7663 } | |
798 | 7664 #ifdef FEAT_MBYTE |
7665 else if (has_mbyte) | |
7666 c = mb_ptr2char(src - 1); | |
7667 #endif | |
7 | 7668 |
7669 /* Write to buffer, if copy is set. */ | |
4244 | 7670 if (func_one != (fptr_T)NULL) |
7671 /* Turbo C complains without the typecast */ | |
7672 func_one = (fptr_T)(func_one(&cc, c)); | |
7673 else if (func_all != (fptr_T)NULL) | |
7674 /* Turbo C complains without the typecast */ | |
7675 func_all = (fptr_T)(func_all(&cc, c)); | |
7676 else /* just copy */ | |
772 | 7677 cc = c; |
7678 | |
7679 #ifdef FEAT_MBYTE | |
7680 if (has_mbyte) | |
7 | 7681 { |
2307
81527f127fb1
Fix: Composing characters in :s substitute text were dropped.
Bram Moolenaar <bram@vim.org>
parents:
2247
diff
changeset
|
7682 int totlen = mb_ptr2len(src - 1); |
81527f127fb1
Fix: Composing characters in :s substitute text were dropped.
Bram Moolenaar <bram@vim.org>
parents:
2247
diff
changeset
|
7683 |
7 | 7684 if (copy) |
772 | 7685 mb_char2bytes(cc, dst); |
7686 dst += mb_char2len(cc) - 1; | |
2307
81527f127fb1
Fix: Composing characters in :s substitute text were dropped.
Bram Moolenaar <bram@vim.org>
parents:
2247
diff
changeset
|
7687 if (enc_utf8) |
81527f127fb1
Fix: Composing characters in :s substitute text were dropped.
Bram Moolenaar <bram@vim.org>
parents:
2247
diff
changeset
|
7688 { |
81527f127fb1
Fix: Composing characters in :s substitute text were dropped.
Bram Moolenaar <bram@vim.org>
parents:
2247
diff
changeset
|
7689 int clen = utf_ptr2len(src - 1); |
81527f127fb1
Fix: Composing characters in :s substitute text were dropped.
Bram Moolenaar <bram@vim.org>
parents:
2247
diff
changeset
|
7690 |
81527f127fb1
Fix: Composing characters in :s substitute text were dropped.
Bram Moolenaar <bram@vim.org>
parents:
2247
diff
changeset
|
7691 /* If the character length is shorter than "totlen", there |
81527f127fb1
Fix: Composing characters in :s substitute text were dropped.
Bram Moolenaar <bram@vim.org>
parents:
2247
diff
changeset
|
7692 * are composing characters; copy them as-is. */ |
81527f127fb1
Fix: Composing characters in :s substitute text were dropped.
Bram Moolenaar <bram@vim.org>
parents:
2247
diff
changeset
|
7693 if (clen < totlen) |
81527f127fb1
Fix: Composing characters in :s substitute text were dropped.
Bram Moolenaar <bram@vim.org>
parents:
2247
diff
changeset
|
7694 { |
81527f127fb1
Fix: Composing characters in :s substitute text were dropped.
Bram Moolenaar <bram@vim.org>
parents:
2247
diff
changeset
|
7695 if (copy) |
81527f127fb1
Fix: Composing characters in :s substitute text were dropped.
Bram Moolenaar <bram@vim.org>
parents:
2247
diff
changeset
|
7696 mch_memmove(dst + 1, src - 1 + clen, |
81527f127fb1
Fix: Composing characters in :s substitute text were dropped.
Bram Moolenaar <bram@vim.org>
parents:
2247
diff
changeset
|
7697 (size_t)(totlen - clen)); |
81527f127fb1
Fix: Composing characters in :s substitute text were dropped.
Bram Moolenaar <bram@vim.org>
parents:
2247
diff
changeset
|
7698 dst += totlen - clen; |
81527f127fb1
Fix: Composing characters in :s substitute text were dropped.
Bram Moolenaar <bram@vim.org>
parents:
2247
diff
changeset
|
7699 } |
81527f127fb1
Fix: Composing characters in :s substitute text were dropped.
Bram Moolenaar <bram@vim.org>
parents:
2247
diff
changeset
|
7700 } |
81527f127fb1
Fix: Composing characters in :s substitute text were dropped.
Bram Moolenaar <bram@vim.org>
parents:
2247
diff
changeset
|
7701 src += totlen - 1; |
7 | 7702 } |
7703 else | |
7704 #endif | |
7705 if (copy) | |
772 | 7706 *dst = cc; |
7 | 7707 dst++; |
7708 } | |
7709 else | |
7710 { | |
7711 if (REG_MULTI) | |
7712 { | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7713 clnum = rex.reg_mmatch->startpos[no].lnum; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7714 if (clnum < 0 || rex.reg_mmatch->endpos[no].lnum < 0) |
7 | 7715 s = NULL; |
7716 else | |
7717 { | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7718 s = reg_getline(clnum) + rex.reg_mmatch->startpos[no].col; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7719 if (rex.reg_mmatch->endpos[no].lnum == clnum) |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7720 len = rex.reg_mmatch->endpos[no].col |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7721 - rex.reg_mmatch->startpos[no].col; |
7 | 7722 else |
7723 len = (int)STRLEN(s); | |
7724 } | |
7725 } | |
7726 else | |
7727 { | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7728 s = rex.reg_match->startp[no]; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7729 if (rex.reg_match->endp[no] == NULL) |
7 | 7730 s = NULL; |
7731 else | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7732 len = (int)(rex.reg_match->endp[no] - s); |
7 | 7733 } |
7734 if (s != NULL) | |
7735 { | |
7736 for (;;) | |
7737 { | |
7738 if (len == 0) | |
7739 { | |
7740 if (REG_MULTI) | |
7741 { | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7742 if (rex.reg_mmatch->endpos[no].lnum == clnum) |
7 | 7743 break; |
7744 if (copy) | |
7745 *dst = CAR; | |
7746 ++dst; | |
7747 s = reg_getline(++clnum); | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7748 if (rex.reg_mmatch->endpos[no].lnum == clnum) |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7749 len = rex.reg_mmatch->endpos[no].col; |
7 | 7750 else |
7751 len = (int)STRLEN(s); | |
7752 } | |
7753 else | |
7754 break; | |
7755 } | |
7756 else if (*s == NUL) /* we hit NUL. */ | |
7757 { | |
7758 if (copy) | |
7759 EMSG(_(e_re_damg)); | |
7760 goto exit; | |
7761 } | |
7762 else | |
7763 { | |
7764 if (backslash && (*s == CAR || *s == '\\')) | |
7765 { | |
7766 /* | |
7767 * Insert a backslash in front of a CR, otherwise | |
7768 * it will be replaced by a line break. | |
7769 * Number of backslashes will be halved later, | |
7770 * double them here. | |
7771 */ | |
7772 if (copy) | |
7773 { | |
7774 dst[0] = '\\'; | |
7775 dst[1] = *s; | |
7776 } | |
7777 dst += 2; | |
7778 } | |
7779 else | |
7780 { | |
772 | 7781 #ifdef FEAT_MBYTE |
7782 if (has_mbyte) | |
7783 c = mb_ptr2char(s); | |
7784 else | |
7785 #endif | |
7786 c = *s; | |
7787 | |
4244 | 7788 if (func_one != (fptr_T)NULL) |
7789 /* Turbo C complains without the typecast */ | |
7790 func_one = (fptr_T)(func_one(&cc, c)); | |
7791 else if (func_all != (fptr_T)NULL) | |
7792 /* Turbo C complains without the typecast */ | |
7793 func_all = (fptr_T)(func_all(&cc, c)); | |
7794 else /* just copy */ | |
772 | 7795 cc = c; |
7796 | |
7797 #ifdef FEAT_MBYTE | |
7798 if (has_mbyte) | |
7 | 7799 { |
1332 | 7800 int l; |
7801 | |
7802 /* Copy composing characters separately, one | |
7803 * at a time. */ | |
7804 if (enc_utf8) | |
7805 l = utf_ptr2len(s) - 1; | |
7806 else | |
7807 l = mb_ptr2len(s) - 1; | |
772 | 7808 |
7809 s += l; | |
7810 len -= l; | |
7811 if (copy) | |
7812 mb_char2bytes(cc, dst); | |
7813 dst += mb_char2len(cc) - 1; | |
7 | 7814 } |
772 | 7815 else |
7816 #endif | |
7817 if (copy) | |
7818 *dst = cc; | |
7819 dst++; | |
7 | 7820 } |
772 | 7821 |
7 | 7822 ++s; |
7823 --len; | |
7824 } | |
7825 } | |
7826 } | |
7827 no = -1; | |
7828 } | |
7829 } | |
7830 if (copy) | |
7831 *dst = NUL; | |
7832 | |
7833 exit: | |
7834 return (int)((dst - dest) + 1); | |
7835 } | |
7836 | |
7837 #ifdef FEAT_EVAL | |
7805
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
7838 static char_u *reg_getline_submatch(linenr_T lnum); |
2012 | 7839 |
7 | 7840 /* |
2011 | 7841 * Call reg_getline() with the line numbers from the submatch. If a |
7842 * substitute() was used the reg_maxline and other values have been | |
7843 * overwritten. | |
7844 */ | |
7845 static char_u * | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
7846 reg_getline_submatch(linenr_T lnum) |
2011 | 7847 { |
7848 char_u *s; | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7849 linenr_T save_first = rex.reg_firstlnum; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7850 linenr_T save_max = rex.reg_maxline; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7851 |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7852 rex.reg_firstlnum = rsm.sm_firstlnum; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7853 rex.reg_maxline = rsm.sm_maxline; |
2011 | 7854 |
7855 s = reg_getline(lnum); | |
7856 | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7857 rex.reg_firstlnum = save_first; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7858 rex.reg_maxline = save_max; |
2011 | 7859 return s; |
7860 } | |
7861 | |
7862 /* | |
1209 | 7863 * Used for the submatch() function: get the string from the n'th submatch in |
7 | 7864 * allocated memory. |
7865 * Returns NULL when not in a ":s" command and for a non-existing submatch. | |
7866 */ | |
7867 char_u * | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
7868 reg_submatch(int no) |
7 | 7869 { |
7870 char_u *retval = NULL; | |
7871 char_u *s; | |
7872 int len; | |
7873 int round; | |
7874 linenr_T lnum; | |
7875 | |
840 | 7876 if (!can_f_submatch || no < 0) |
7 | 7877 return NULL; |
7878 | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7879 if (rsm.sm_match == NULL) |
7 | 7880 { |
7881 /* | |
7882 * First round: compute the length and allocate memory. | |
7883 * Second round: copy the text. | |
7884 */ | |
7885 for (round = 1; round <= 2; ++round) | |
7886 { | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7887 lnum = rsm.sm_mmatch->startpos[no].lnum; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7888 if (lnum < 0 || rsm.sm_mmatch->endpos[no].lnum < 0) |
7 | 7889 return NULL; |
7890 | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7891 s = reg_getline_submatch(lnum) + rsm.sm_mmatch->startpos[no].col; |
7 | 7892 if (s == NULL) /* anti-crash check, cannot happen? */ |
7893 break; | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7894 if (rsm.sm_mmatch->endpos[no].lnum == lnum) |
7 | 7895 { |
7896 /* Within one line: take form start to end col. */ | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7897 len = rsm.sm_mmatch->endpos[no].col |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7898 - rsm.sm_mmatch->startpos[no].col; |
7 | 7899 if (round == 2) |
418 | 7900 vim_strncpy(retval, s, len); |
7 | 7901 ++len; |
7902 } | |
7903 else | |
7904 { | |
7905 /* Multiple lines: take start line from start col, middle | |
7906 * lines completely and end line up to end col. */ | |
7907 len = (int)STRLEN(s); | |
7908 if (round == 2) | |
7909 { | |
7910 STRCPY(retval, s); | |
7911 retval[len] = '\n'; | |
7912 } | |
7913 ++len; | |
7914 ++lnum; | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7915 while (lnum < rsm.sm_mmatch->endpos[no].lnum) |
7 | 7916 { |
2011 | 7917 s = reg_getline_submatch(lnum++); |
7 | 7918 if (round == 2) |
7919 STRCPY(retval + len, s); | |
7920 len += (int)STRLEN(s); | |
7921 if (round == 2) | |
7922 retval[len] = '\n'; | |
7923 ++len; | |
7924 } | |
7925 if (round == 2) | |
2011 | 7926 STRNCPY(retval + len, reg_getline_submatch(lnum), |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7927 rsm.sm_mmatch->endpos[no].col); |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7928 len += rsm.sm_mmatch->endpos[no].col; |
7 | 7929 if (round == 2) |
7930 retval[len] = NUL; | |
7931 ++len; | |
7932 } | |
7933 | |
840 | 7934 if (retval == NULL) |
7 | 7935 { |
7936 retval = lalloc((long_u)len, TRUE); | |
840 | 7937 if (retval == NULL) |
7 | 7938 return NULL; |
7939 } | |
7940 } | |
7941 } | |
7942 else | |
7943 { | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7944 s = rsm.sm_match->startp[no]; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7945 if (s == NULL || rsm.sm_match->endp[no] == NULL) |
7 | 7946 retval = NULL; |
7947 else | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7948 retval = vim_strnsave(s, (int)(rsm.sm_match->endp[no] - s)); |
7 | 7949 } |
7950 | |
7951 return retval; | |
7952 } | |
5794 | 7953 |
7954 /* | |
7955 * Used for the submatch() function with the optional non-zero argument: get | |
7956 * the list of strings from the n'th submatch in allocated memory with NULs | |
7957 * represented in NLs. | |
7958 * Returns a list of allocated strings. Returns NULL when not in a ":s" | |
7959 * command, for a non-existing submatch and for any error. | |
7960 */ | |
7961 list_T * | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
7962 reg_submatch_list(int no) |
5794 | 7963 { |
7964 char_u *s; | |
7965 linenr_T slnum; | |
7966 linenr_T elnum; | |
7967 colnr_T scol; | |
7968 colnr_T ecol; | |
7969 int i; | |
7970 list_T *list; | |
7971 int error = FALSE; | |
7972 | |
7973 if (!can_f_submatch || no < 0) | |
7974 return NULL; | |
7975 | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7976 if (rsm.sm_match == NULL) |
5794 | 7977 { |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7978 slnum = rsm.sm_mmatch->startpos[no].lnum; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7979 elnum = rsm.sm_mmatch->endpos[no].lnum; |
5794 | 7980 if (slnum < 0 || elnum < 0) |
7981 return NULL; | |
7982 | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7983 scol = rsm.sm_mmatch->startpos[no].col; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
7984 ecol = rsm.sm_mmatch->endpos[no].col; |
5794 | 7985 |
7986 list = list_alloc(); | |
7987 if (list == NULL) | |
7988 return NULL; | |
7989 | |
7990 s = reg_getline_submatch(slnum) + scol; | |
7991 if (slnum == elnum) | |
7992 { | |
7993 if (list_append_string(list, s, ecol - scol) == FAIL) | |
7994 error = TRUE; | |
7995 } | |
7996 else | |
7997 { | |
7998 if (list_append_string(list, s, -1) == FAIL) | |
7999 error = TRUE; | |
8000 for (i = 1; i < elnum - slnum; i++) | |
8001 { | |
8002 s = reg_getline_submatch(slnum + i); | |
8003 if (list_append_string(list, s, -1) == FAIL) | |
8004 error = TRUE; | |
8005 } | |
8006 s = reg_getline_submatch(elnum); | |
8007 if (list_append_string(list, s, ecol) == FAIL) | |
8008 error = TRUE; | |
8009 } | |
8010 } | |
8011 else | |
8012 { | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
8013 s = rsm.sm_match->startp[no]; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
8014 if (s == NULL || rsm.sm_match->endp[no] == NULL) |
5794 | 8015 return NULL; |
8016 list = list_alloc(); | |
8017 if (list == NULL) | |
8018 return NULL; | |
8019 if (list_append_string(list, s, | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
8020 (int)(rsm.sm_match->endp[no] - s)) == FAIL) |
5794 | 8021 error = TRUE; |
8022 } | |
8023 | |
8024 if (error) | |
8025 { | |
8863
e1b84109506a
commit https://github.com/vim/vim/commit/107e1eef1df3b786ad3ad49fbdb9e058649303b5
Christian Brabandt <cb@256bit.org>
parents:
8825
diff
changeset
|
8026 list_free(list); |
5794 | 8027 return NULL; |
8028 } | |
8029 return list; | |
8030 } | |
7 | 8031 #endif |
4444 | 8032 |
8033 static regengine_T bt_regengine = | |
8034 { | |
8035 bt_regcomp, | |
4805
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4770
diff
changeset
|
8036 bt_regfree, |
4444 | 8037 bt_regexec_nl, |
6328 | 8038 bt_regexec_multi, |
8039 (char_u *)"" | |
4444 | 8040 }; |
8041 | |
8042 #include "regexp_nfa.c" | |
8043 | |
8044 static regengine_T nfa_regengine = | |
8045 { | |
8046 nfa_regcomp, | |
4805
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4770
diff
changeset
|
8047 nfa_regfree, |
4444 | 8048 nfa_regexec_nl, |
6328 | 8049 nfa_regexec_multi, |
8050 (char_u *)"" | |
4444 | 8051 }; |
8052 | |
8053 /* Which regexp engine to use? Needed for vim_regcomp(). | |
8054 * Must match with 'regexpengine'. */ | |
8055 static int regexp_engine = 0; | |
6328 | 8056 |
4444 | 8057 #ifdef DEBUG |
8058 static char_u regname[][30] = { | |
8059 "AUTOMATIC Regexp Engine", | |
4579
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
8060 "BACKTRACKING Regexp Engine", |
4444 | 8061 "NFA Regexp Engine" |
8062 }; | |
8063 #endif | |
8064 | |
8065 /* | |
8066 * Compile a regular expression into internal code. | |
4805
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4770
diff
changeset
|
8067 * Returns the program in allocated memory. |
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4770
diff
changeset
|
8068 * Use vim_regfree() to free the memory. |
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4770
diff
changeset
|
8069 * Returns NULL for an error. |
4444 | 8070 */ |
8071 regprog_T * | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
8072 vim_regcomp(char_u *expr_arg, int re_flags) |
4444 | 8073 { |
8074 regprog_T *prog = NULL; | |
8075 char_u *expr = expr_arg; | |
8076 | |
8077 regexp_engine = p_re; | |
8078 | |
8079 /* Check for prefix "\%#=", that sets the regexp engine */ | |
8080 if (STRNCMP(expr, "\\%#=", 4) == 0) | |
8081 { | |
8082 int newengine = expr[4] - '0'; | |
8083 | |
8084 if (newengine == AUTOMATIC_ENGINE | |
8085 || newengine == BACKTRACKING_ENGINE | |
8086 || newengine == NFA_ENGINE) | |
8087 { | |
8088 regexp_engine = expr[4] - '0'; | |
8089 expr += 5; | |
8090 #ifdef DEBUG | |
5897 | 8091 smsg((char_u *)"New regexp mode selected (%d): %s", |
8092 regexp_engine, regname[newengine]); | |
4444 | 8093 #endif |
8094 } | |
8095 else | |
8096 { | |
8097 EMSG(_("E864: \\%#= can only be followed by 0, 1, or 2. The automatic engine will be used ")); | |
8098 regexp_engine = AUTOMATIC_ENGINE; | |
8099 } | |
8100 } | |
8101 bt_regengine.expr = expr; | |
8102 nfa_regengine.expr = expr; | |
8103 | |
8104 /* | |
8105 * First try the NFA engine, unless backtracking was requested. | |
8106 */ | |
8107 if (regexp_engine != BACKTRACKING_ENGINE) | |
6533 | 8108 prog = nfa_regengine.regcomp(expr, |
8109 re_flags + (regexp_engine == AUTOMATIC_ENGINE ? RE_AUTO : 0)); | |
4444 | 8110 else |
8111 prog = bt_regengine.regcomp(expr, re_flags); | |
8112 | |
6328 | 8113 /* Check for error compiling regexp with initial engine. */ |
8114 if (prog == NULL) | |
4444 | 8115 { |
4460 | 8116 #ifdef BT_REGEXP_DEBUG_LOG |
4444 | 8117 if (regexp_engine != BACKTRACKING_ENGINE) /* debugging log for NFA */ |
8118 { | |
8119 FILE *f; | |
4460 | 8120 f = fopen(BT_REGEXP_DEBUG_LOG_NAME, "a"); |
4444 | 8121 if (f) |
8122 { | |
4762
47906f888725
updated for version 7.3.1128
Bram Moolenaar <bram@vim.org>
parents:
4760
diff
changeset
|
8123 fprintf(f, "Syntax error in \"%s\"\n", expr); |
4444 | 8124 fclose(f); |
8125 } | |
8126 else | |
4460 | 8127 EMSG2("(NFA) Could not open \"%s\" to write !!!", |
8128 BT_REGEXP_DEBUG_LOG_NAME); | |
4444 | 8129 } |
8130 #endif | |
8131 /* | |
6328 | 8132 * If the NFA engine failed, try the backtracking engine. |
6533 | 8133 * The NFA engine also fails for patterns that it can't handle well |
8134 * but are still valid patterns, thus a retry should work. | |
8135 */ | |
4444 | 8136 if (regexp_engine == AUTOMATIC_ENGINE) |
6328 | 8137 { |
6533 | 8138 regexp_engine = BACKTRACKING_ENGINE; |
4762
47906f888725
updated for version 7.3.1128
Bram Moolenaar <bram@vim.org>
parents:
4760
diff
changeset
|
8139 prog = bt_regengine.regcomp(expr, re_flags); |
6328 | 8140 } |
4762
47906f888725
updated for version 7.3.1128
Bram Moolenaar <bram@vim.org>
parents:
4760
diff
changeset
|
8141 } |
4444 | 8142 |
6328 | 8143 if (prog != NULL) |
8144 { | |
8145 /* Store the info needed to call regcomp() again when the engine turns | |
8146 * out to be very slow when executing it. */ | |
8147 prog->re_engine = regexp_engine; | |
8148 prog->re_flags = re_flags; | |
8149 } | |
8150 | |
4444 | 8151 return prog; |
8152 } | |
8153 | |
8154 /* | |
4805
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4770
diff
changeset
|
8155 * Free a compiled regexp program, returned by vim_regcomp(). |
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4770
diff
changeset
|
8156 */ |
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4770
diff
changeset
|
8157 void |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
8158 vim_regfree(regprog_T *prog) |
4805
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4770
diff
changeset
|
8159 { |
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4770
diff
changeset
|
8160 if (prog != NULL) |
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4770
diff
changeset
|
8161 prog->engine->regfree(prog); |
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4770
diff
changeset
|
8162 } |
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4770
diff
changeset
|
8163 |
6328 | 8164 #ifdef FEAT_EVAL |
7805
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
8165 static void report_re_switch(char_u *pat); |
6328 | 8166 |
8167 static void | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
8168 report_re_switch(char_u *pat) |
6328 | 8169 { |
8170 if (p_verbose > 0) | |
8171 { | |
8172 verbose_enter(); | |
8173 MSG_PUTS(_("Switching to backtracking RE engine for pattern: ")); | |
8174 MSG_PUTS(pat); | |
8175 verbose_leave(); | |
8176 } | |
8177 } | |
8178 #endif | |
8179 | |
7805
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
8180 static int vim_regexec_both(regmatch_T *rmp, char_u *line, colnr_T col, int nl); |
6328 | 8181 |
4805
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4770
diff
changeset
|
8182 /* |
4444 | 8183 * Match a regexp against a string. |
8184 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp(). | |
6375 | 8185 * Note: "rmp->regprog" may be freed and changed. |
4444 | 8186 * Uses curbuf for line count and 'iskeyword'. |
6328 | 8187 * When "nl" is TRUE consider a "\n" in "line" to be a line break. |
4444 | 8188 * |
8189 * Return TRUE if there is a match, FALSE if not. | |
8190 */ | |
6328 | 8191 static int |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
8192 vim_regexec_both( |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
8193 regmatch_T *rmp, |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
8194 char_u *line, /* string to match against */ |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
8195 colnr_T col, /* column to start looking for match */ |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
8196 int nl) |
6328 | 8197 { |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
8198 int result; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
8199 regexec_T rex_save; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
8200 int rex_in_use_save = rex_in_use; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
8201 |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
8202 if (rex_in_use) |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
8203 /* Being called recursively, save the state. */ |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
8204 rex_save = rex; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
8205 rex_in_use = TRUE; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
8206 rex.reg_startp = NULL; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
8207 rex.reg_endp = NULL; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
8208 rex.reg_startpos = NULL; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
8209 rex.reg_endpos = NULL; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
8210 |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
8211 result = rmp->regprog->engine->regexec_nl(rmp, line, col, nl); |
6328 | 8212 |
8213 /* NFA engine aborted because it's very slow. */ | |
8214 if (rmp->regprog->re_engine == AUTOMATIC_ENGINE | |
8215 && result == NFA_TOO_EXPENSIVE) | |
8216 { | |
8217 int save_p_re = p_re; | |
8218 int re_flags = rmp->regprog->re_flags; | |
8219 char_u *pat = vim_strsave(((nfa_regprog_T *)rmp->regprog)->pattern); | |
8220 | |
8221 p_re = BACKTRACKING_ENGINE; | |
8222 vim_regfree(rmp->regprog); | |
8223 if (pat != NULL) | |
8224 { | |
8225 #ifdef FEAT_EVAL | |
8226 report_re_switch(pat); | |
8227 #endif | |
8228 rmp->regprog = vim_regcomp(pat, re_flags); | |
8229 if (rmp->regprog != NULL) | |
8230 result = rmp->regprog->engine->regexec_nl(rmp, line, col, nl); | |
8231 vim_free(pat); | |
8232 } | |
8233 | |
8234 p_re = save_p_re; | |
8235 } | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
8236 |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
8237 rex_in_use = rex_in_use_save; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
8238 if (rex_in_use) |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
8239 rex = rex_save; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
8240 |
6390 | 8241 return result > 0; |
6328 | 8242 } |
8243 | |
6375 | 8244 /* |
8245 * Note: "*prog" may be freed and changed. | |
6390 | 8246 * Return TRUE if there is a match, FALSE if not. |
6375 | 8247 */ |
8248 int | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
8249 vim_regexec_prog( |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
8250 regprog_T **prog, |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
8251 int ignore_case, |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
8252 char_u *line, |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
8253 colnr_T col) |
6375 | 8254 { |
8255 int r; | |
8256 regmatch_T regmatch; | |
8257 | |
8258 regmatch.regprog = *prog; | |
8259 regmatch.rm_ic = ignore_case; | |
8260 r = vim_regexec_both(®match, line, col, FALSE); | |
8261 *prog = regmatch.regprog; | |
8262 return r; | |
8263 } | |
8264 | |
8265 /* | |
8266 * Note: "rmp->regprog" may be freed and changed. | |
6390 | 8267 * Return TRUE if there is a match, FALSE if not. |
6375 | 8268 */ |
4444 | 8269 int |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
8270 vim_regexec(regmatch_T *rmp, char_u *line, colnr_T col) |
4444 | 8271 { |
6328 | 8272 return vim_regexec_both(rmp, line, col, FALSE); |
4444 | 8273 } |
8274 | |
8275 #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \ | |
8276 || defined(FIND_REPLACE_DIALOG) || defined(PROTO) | |
8277 /* | |
8278 * Like vim_regexec(), but consider a "\n" in "line" to be a line break. | |
6375 | 8279 * Note: "rmp->regprog" may be freed and changed. |
6390 | 8280 * Return TRUE if there is a match, FALSE if not. |
4444 | 8281 */ |
8282 int | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
8283 vim_regexec_nl(regmatch_T *rmp, char_u *line, colnr_T col) |
4444 | 8284 { |
6328 | 8285 return vim_regexec_both(rmp, line, col, TRUE); |
4444 | 8286 } |
8287 #endif | |
8288 | |
8289 /* | |
8290 * Match a regexp against multiple lines. | |
8291 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp(). | |
6375 | 8292 * Note: "rmp->regprog" may be freed and changed. |
4444 | 8293 * Uses curbuf for line count and 'iskeyword'. |
8294 * | |
8295 * Return zero if there is no match. Return number of lines contained in the | |
8296 * match otherwise. | |
8297 */ | |
8298 long | |
7833
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
8299 vim_regexec_multi( |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
8300 regmmatch_T *rmp, |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
8301 win_T *win, /* window in which to search or NULL */ |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
8302 buf_T *buf, /* buffer in which to search */ |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
8303 linenr_T lnum, /* nr of line to start looking for match */ |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
8304 colnr_T col, /* column to start looking for match */ |
c079097365f3
commit https://github.com/vim/vim/commit/055409764ca5f7978d4c399d2c440af0ce971c4f
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
8305 proftime_T *tm) /* timeout limit or NULL */ |
4444 | 8306 { |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
8307 int result; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
8308 regexec_T rex_save; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
8309 int rex_in_use_save = rex_in_use; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
8310 |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
8311 if (rex_in_use) |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
8312 /* Being called recursively, save the state. */ |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
8313 rex_save = rex; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
8314 rex_in_use = TRUE; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
8315 |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
8316 result = rmp->regprog->engine->regexec_multi(rmp, win, buf, lnum, col, tm); |
6328 | 8317 |
8318 /* NFA engine aborted because it's very slow. */ | |
8319 if (rmp->regprog->re_engine == AUTOMATIC_ENGINE | |
8320 && result == NFA_TOO_EXPENSIVE) | |
8321 { | |
8322 int save_p_re = p_re; | |
8323 int re_flags = rmp->regprog->re_flags; | |
8324 char_u *pat = vim_strsave(((nfa_regprog_T *)rmp->regprog)->pattern); | |
8325 | |
8326 p_re = BACKTRACKING_ENGINE; | |
8327 vim_regfree(rmp->regprog); | |
8328 if (pat != NULL) | |
8329 { | |
8330 #ifdef FEAT_EVAL | |
8331 report_re_switch(pat); | |
8332 #endif | |
8333 rmp->regprog = vim_regcomp(pat, re_flags); | |
8334 if (rmp->regprog != NULL) | |
8335 result = rmp->regprog->engine->regexec_multi( | |
8336 rmp, win, buf, lnum, col, tm); | |
8337 vim_free(pat); | |
8338 } | |
8339 p_re = save_p_re; | |
8340 } | |
8341 | |
10245
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
8342 rex_in_use = rex_in_use_save; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
8343 if (rex_in_use) |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
8344 rex = rex_save; |
d76ccdacb41e
commit https://github.com/vim/vim/commit/6100d02aab7c8294b581cb299250eea164b50e9d
Christian Brabandt <cb@256bit.org>
parents:
10187
diff
changeset
|
8345 |
6390 | 8346 return result <= 0 ? 0 : result; |
4444 | 8347 } |