Mercurial > vim
annotate src/regexp.c @ 4730:749e2b2755d5 v7.3.1112
updated for version 7.3.1112
Problem: New regexp engine: \%V not supported.
Solution: Implement \%V. Add tests.
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Tue, 04 Jun 2013 18:28:48 +0200 |
parents | bd6bef0bd0fb |
children | 0798b096bab3 |
rev | line source |
---|---|
7 | 1 /* vi:set ts=8 sts=4 sw=4: |
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 */ | |
247 | |
7 | 248 /* |
249 * Magic characters have a special meaning, they don't match literally. | |
250 * Magic characters are negative. This separates them from literal characters | |
251 * (possibly multi-byte). Only ASCII characters can be Magic. | |
252 */ | |
253 #define Magic(x) ((int)(x) - 256) | |
254 #define un_Magic(x) ((x) + 256) | |
255 #define is_Magic(x) ((x) < 0) | |
256 | |
257 static int no_Magic __ARGS((int x)); | |
258 static int toggle_Magic __ARGS((int x)); | |
259 | |
260 static int | |
261 no_Magic(x) | |
262 int x; | |
263 { | |
264 if (is_Magic(x)) | |
265 return un_Magic(x); | |
266 return x; | |
267 } | |
268 | |
269 static int | |
270 toggle_Magic(x) | |
271 int x; | |
272 { | |
273 if (is_Magic(x)) | |
274 return un_Magic(x); | |
275 return Magic(x); | |
276 } | |
277 | |
278 /* | |
279 * The first byte of the regexp internal "program" is actually this magic | |
280 * number; the start node begins in the second byte. It's used to catch the | |
281 * most severe mutilation of the program by the caller. | |
282 */ | |
283 | |
284 #define REGMAGIC 0234 | |
285 | |
286 /* | |
287 * Opcode notes: | |
288 * | |
289 * BRANCH The set of branches constituting a single choice are hooked | |
290 * together with their "next" pointers, since precedence prevents | |
291 * anything being concatenated to any individual branch. The | |
292 * "next" pointer of the last BRANCH in a choice points to the | |
293 * thing following the whole choice. This is also where the | |
294 * final "next" pointer of each individual branch points; each | |
295 * branch starts with the operand node of a BRANCH node. | |
296 * | |
297 * BACK Normal "next" pointers all implicitly point forward; BACK | |
298 * exists to make loop structures possible. | |
299 * | |
300 * STAR,PLUS '=', and complex '*' and '+', are implemented as circular | |
301 * BRANCH structures using BACK. Simple cases (one character | |
302 * per match) are implemented with STAR and PLUS for speed | |
303 * and to minimize recursive plunges. | |
304 * | |
305 * BRACE_LIMITS This is always followed by a BRACE_SIMPLE or BRACE_COMPLEX | |
306 * node, and defines the min and max limits to be used for that | |
307 * node. | |
308 * | |
309 * MOPEN,MCLOSE ...are numbered at compile time. | |
310 * ZOPEN,ZCLOSE ...ditto | |
311 */ | |
312 | |
313 /* | |
314 * A node is one char of opcode followed by two chars of "next" pointer. | |
315 * "Next" pointers are stored as two 8-bit bytes, high order first. The | |
316 * value is a positive offset from the opcode of the node containing it. | |
317 * An operand, if any, simply follows the node. (Note that much of the | |
318 * code generation knows about this implicit relationship.) | |
319 * | |
320 * Using two bytes for the "next" pointer is vast overkill for most things, | |
321 * but allows patterns to get big without disasters. | |
322 */ | |
323 #define OP(p) ((int)*(p)) | |
324 #define NEXT(p) (((*((p) + 1) & 0377) << 8) + (*((p) + 2) & 0377)) | |
325 #define OPERAND(p) ((p) + 3) | |
326 /* Obtain an operand that was stored as four bytes, MSB first. */ | |
327 #define OPERAND_MIN(p) (((long)(p)[3] << 24) + ((long)(p)[4] << 16) \ | |
328 + ((long)(p)[5] << 8) + (long)(p)[6]) | |
329 /* Obtain a second operand stored as four bytes. */ | |
330 #define OPERAND_MAX(p) OPERAND_MIN((p) + 4) | |
331 /* Obtain a second single-byte operand stored after a four bytes operand. */ | |
332 #define OPERAND_CMP(p) (p)[7] | |
333 | |
334 /* | |
335 * Utility definitions. | |
336 */ | |
337 #define UCHARAT(p) ((int)*(char_u *)(p)) | |
338 | |
339 /* Used for an error (down from) vim_regcomp(): give the error message, set | |
340 * rc_did_emsg and return NULL */ | |
653 | 341 #define EMSG_RET_NULL(m) return (EMSG(m), rc_did_emsg = TRUE, (void *)NULL) |
308 | 342 #define EMSG_RET_FAIL(m) return (EMSG(m), rc_did_emsg = TRUE, FAIL) |
4444 | 343 #define EMSG2_RET_NULL(m, c) return (EMSG2((m), (c) ? "" : "\\"), rc_did_emsg = TRUE, (void *)NULL) |
344 #define EMSG2_RET_FAIL(m, c) return (EMSG2((m), (c) ? "" : "\\"), rc_did_emsg = TRUE, FAIL) | |
345 #define EMSG_ONE_RET_NULL EMSG2_RET_NULL(_("E369: invalid item in %s%%[]"), reg_magic == MAGIC_ALL) | |
7 | 346 |
347 #define MAX_LIMIT (32767L << 16L) | |
348 | |
349 static int re_multi_type __ARGS((int)); | |
350 static int cstrncmp __ARGS((char_u *s1, char_u *s2, int *n)); | |
351 static char_u *cstrchr __ARGS((char_u *, int)); | |
352 | |
4444 | 353 #ifdef BT_REGEXP_DUMP |
354 static void regdump __ARGS((char_u *, bt_regprog_T *)); | |
355 #endif | |
7 | 356 #ifdef DEBUG |
357 static char_u *regprop __ARGS((char_u *)); | |
358 #endif | |
359 | |
4444 | 360 static char_u e_missingbracket[] = N_("E769: Missing ] after %s["); |
361 static char_u e_unmatchedpp[] = N_("E53: Unmatched %s%%("); | |
362 static char_u e_unmatchedp[] = N_("E54: Unmatched %s("); | |
363 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
|
364 #ifdef FEAT_SYN_HL |
4688
371cc0c44097
updated for version 7.3.1091
Bram Moolenaar <bram@vim.org>
parents:
4682
diff
changeset
|
365 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
|
366 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
|
367 #endif |
4444 | 368 |
7 | 369 #define NOT_MULTI 0 |
370 #define MULTI_ONE 1 | |
371 #define MULTI_MULT 2 | |
372 /* | |
373 * Return NOT_MULTI if c is not a "multi" operator. | |
374 * Return MULTI_ONE if c is a single "multi" operator. | |
375 * Return MULTI_MULT if c is a multi "multi" operator. | |
376 */ | |
377 static int | |
378 re_multi_type(c) | |
379 int c; | |
380 { | |
381 if (c == Magic('@') || c == Magic('=') || c == Magic('?')) | |
382 return MULTI_ONE; | |
383 if (c == Magic('*') || c == Magic('+') || c == Magic('{')) | |
384 return MULTI_MULT; | |
385 return NOT_MULTI; | |
386 } | |
387 | |
388 /* | |
389 * Flags to be passed up and down. | |
390 */ | |
391 #define HASWIDTH 0x1 /* Known never to match null string. */ | |
392 #define SIMPLE 0x2 /* Simple enough to be STAR/PLUS operand. */ | |
393 #define SPSTART 0x4 /* Starts with * or +. */ | |
394 #define HASNL 0x8 /* Contains some \n. */ | |
395 #define HASLOOKBH 0x10 /* Contains "\@<=" or "\@<!". */ | |
396 #define WORST 0 /* Worst case. */ | |
397 | |
398 /* | |
399 * When regcode is set to this value, code is not emitted and size is computed | |
400 * instead. | |
401 */ | |
402 #define JUST_CALC_SIZE ((char_u *) -1) | |
403 | |
359 | 404 static char_u *reg_prev_sub = NULL; |
405 | |
7 | 406 /* |
407 * REGEXP_INRANGE contains all characters which are always special in a [] | |
408 * range after '\'. | |
409 * REGEXP_ABBR contains all characters which act as abbreviations after '\'. | |
410 * These are: | |
411 * \n - New line (NL). | |
412 * \r - Carriage Return (CR). | |
413 * \t - Tab (TAB). | |
414 * \e - Escape (ESC). | |
415 * \b - Backspace (Ctrl_H). | |
24 | 416 * \d - Character code in decimal, eg \d123 |
417 * \o - Character code in octal, eg \o80 | |
418 * \x - Character code in hex, eg \x4a | |
419 * \u - Multibyte character code, eg \u20ac | |
420 * \U - Long multibyte character code, eg \U12345678 | |
7 | 421 */ |
422 static char_u REGEXP_INRANGE[] = "]^-n\\"; | |
24 | 423 static char_u REGEXP_ABBR[] = "nrtebdoxuU"; |
7 | 424 |
425 static int backslash_trans __ARGS((int c)); | |
167 | 426 static int get_char_class __ARGS((char_u **pp)); |
427 static int get_equi_class __ARGS((char_u **pp)); | |
428 static void reg_equi_class __ARGS((int c)); | |
429 static int get_coll_element __ARGS((char_u **pp)); | |
7 | 430 static char_u *skip_anyof __ARGS((char_u *p)); |
431 static void init_class_tab __ARGS((void)); | |
432 | |
433 /* | |
434 * Translate '\x' to its control character, except "\n", which is Magic. | |
435 */ | |
436 static int | |
437 backslash_trans(c) | |
438 int c; | |
439 { | |
440 switch (c) | |
441 { | |
442 case 'r': return CAR; | |
443 case 't': return TAB; | |
444 case 'e': return ESC; | |
445 case 'b': return BS; | |
446 } | |
447 return c; | |
448 } | |
449 | |
450 /* | |
167 | 451 * Check for a character class name "[:name:]". "pp" points to the '['. |
7 | 452 * Returns one of the CLASS_ items. CLASS_NONE means that no item was |
453 * recognized. Otherwise "pp" is advanced to after the item. | |
454 */ | |
455 static int | |
167 | 456 get_char_class(pp) |
7 | 457 char_u **pp; |
458 { | |
459 static const char *(class_names[]) = | |
460 { | |
461 "alnum:]", | |
462 #define CLASS_ALNUM 0 | |
463 "alpha:]", | |
464 #define CLASS_ALPHA 1 | |
465 "blank:]", | |
466 #define CLASS_BLANK 2 | |
467 "cntrl:]", | |
468 #define CLASS_CNTRL 3 | |
469 "digit:]", | |
470 #define CLASS_DIGIT 4 | |
471 "graph:]", | |
472 #define CLASS_GRAPH 5 | |
473 "lower:]", | |
474 #define CLASS_LOWER 6 | |
475 "print:]", | |
476 #define CLASS_PRINT 7 | |
477 "punct:]", | |
478 #define CLASS_PUNCT 8 | |
479 "space:]", | |
480 #define CLASS_SPACE 9 | |
481 "upper:]", | |
482 #define CLASS_UPPER 10 | |
483 "xdigit:]", | |
484 #define CLASS_XDIGIT 11 | |
485 "tab:]", | |
486 #define CLASS_TAB 12 | |
487 "return:]", | |
488 #define CLASS_RETURN 13 | |
489 "backspace:]", | |
490 #define CLASS_BACKSPACE 14 | |
491 "escape:]", | |
492 #define CLASS_ESCAPE 15 | |
493 }; | |
494 #define CLASS_NONE 99 | |
495 int i; | |
496 | |
497 if ((*pp)[1] == ':') | |
498 { | |
1877 | 499 for (i = 0; i < (int)(sizeof(class_names) / sizeof(*class_names)); ++i) |
7 | 500 if (STRNCMP(*pp + 2, class_names[i], STRLEN(class_names[i])) == 0) |
501 { | |
502 *pp += STRLEN(class_names[i]) + 2; | |
503 return i; | |
504 } | |
505 } | |
506 return CLASS_NONE; | |
507 } | |
508 | |
509 /* | |
510 * Specific version of character class functions. | |
511 * Using a table to keep this fast. | |
512 */ | |
513 static short class_tab[256]; | |
514 | |
515 #define RI_DIGIT 0x01 | |
516 #define RI_HEX 0x02 | |
517 #define RI_OCTAL 0x04 | |
518 #define RI_WORD 0x08 | |
519 #define RI_HEAD 0x10 | |
520 #define RI_ALPHA 0x20 | |
521 #define RI_LOWER 0x40 | |
522 #define RI_UPPER 0x80 | |
523 #define RI_WHITE 0x100 | |
524 | |
525 static void | |
526 init_class_tab() | |
527 { | |
528 int i; | |
529 static int done = FALSE; | |
530 | |
531 if (done) | |
532 return; | |
533 | |
534 for (i = 0; i < 256; ++i) | |
535 { | |
536 if (i >= '0' && i <= '7') | |
537 class_tab[i] = RI_DIGIT + RI_HEX + RI_OCTAL + RI_WORD; | |
538 else if (i >= '8' && i <= '9') | |
539 class_tab[i] = RI_DIGIT + RI_HEX + RI_WORD; | |
540 else if (i >= 'a' && i <= 'f') | |
541 class_tab[i] = RI_HEX + RI_WORD + RI_HEAD + RI_ALPHA + RI_LOWER; | |
542 #ifdef EBCDIC | |
543 else if ((i >= 'g' && i <= 'i') || (i >= 'j' && i <= 'r') | |
544 || (i >= 's' && i <= 'z')) | |
545 #else | |
546 else if (i >= 'g' && i <= 'z') | |
547 #endif | |
548 class_tab[i] = RI_WORD + RI_HEAD + RI_ALPHA + RI_LOWER; | |
549 else if (i >= 'A' && i <= 'F') | |
550 class_tab[i] = RI_HEX + RI_WORD + RI_HEAD + RI_ALPHA + RI_UPPER; | |
551 #ifdef EBCDIC | |
552 else if ((i >= 'G' && i <= 'I') || ( i >= 'J' && i <= 'R') | |
553 || (i >= 'S' && i <= 'Z')) | |
554 #else | |
555 else if (i >= 'G' && i <= 'Z') | |
556 #endif | |
557 class_tab[i] = RI_WORD + RI_HEAD + RI_ALPHA + RI_UPPER; | |
558 else if (i == '_') | |
559 class_tab[i] = RI_WORD + RI_HEAD; | |
560 else | |
561 class_tab[i] = 0; | |
562 } | |
563 class_tab[' '] |= RI_WHITE; | |
564 class_tab['\t'] |= RI_WHITE; | |
565 done = TRUE; | |
566 } | |
567 | |
568 #ifdef FEAT_MBYTE | |
569 # define ri_digit(c) (c < 0x100 && (class_tab[c] & RI_DIGIT)) | |
570 # define ri_hex(c) (c < 0x100 && (class_tab[c] & RI_HEX)) | |
571 # define ri_octal(c) (c < 0x100 && (class_tab[c] & RI_OCTAL)) | |
572 # define ri_word(c) (c < 0x100 && (class_tab[c] & RI_WORD)) | |
573 # define ri_head(c) (c < 0x100 && (class_tab[c] & RI_HEAD)) | |
574 # define ri_alpha(c) (c < 0x100 && (class_tab[c] & RI_ALPHA)) | |
575 # define ri_lower(c) (c < 0x100 && (class_tab[c] & RI_LOWER)) | |
576 # define ri_upper(c) (c < 0x100 && (class_tab[c] & RI_UPPER)) | |
577 # define ri_white(c) (c < 0x100 && (class_tab[c] & RI_WHITE)) | |
578 #else | |
579 # define ri_digit(c) (class_tab[c] & RI_DIGIT) | |
580 # define ri_hex(c) (class_tab[c] & RI_HEX) | |
581 # define ri_octal(c) (class_tab[c] & RI_OCTAL) | |
582 # define ri_word(c) (class_tab[c] & RI_WORD) | |
583 # define ri_head(c) (class_tab[c] & RI_HEAD) | |
584 # define ri_alpha(c) (class_tab[c] & RI_ALPHA) | |
585 # define ri_lower(c) (class_tab[c] & RI_LOWER) | |
586 # define ri_upper(c) (class_tab[c] & RI_UPPER) | |
587 # define ri_white(c) (class_tab[c] & RI_WHITE) | |
588 #endif | |
589 | |
590 /* flags for regflags */ | |
591 #define RF_ICASE 1 /* ignore case */ | |
592 #define RF_NOICASE 2 /* don't ignore case */ | |
593 #define RF_HASNL 4 /* can match a NL */ | |
594 #define RF_ICOMBINE 8 /* ignore combining characters */ | |
595 #define RF_LOOKBH 16 /* uses "\@<=" or "\@<!" */ | |
596 | |
597 /* | |
598 * Global work variables for vim_regcomp(). | |
599 */ | |
600 | |
601 static char_u *regparse; /* Input-scan pointer. */ | |
602 static int prevchr_len; /* byte length of previous char */ | |
603 static int num_complex_braces; /* Complex \{...} count */ | |
604 static int regnpar; /* () count. */ | |
605 #ifdef FEAT_SYN_HL | |
606 static int regnzpar; /* \z() count. */ | |
607 static int re_has_z; /* \z item detected */ | |
608 #endif | |
609 static char_u *regcode; /* Code-emit pointer, or JUST_CALC_SIZE */ | |
610 static long regsize; /* Code size. */ | |
2010 | 611 static int reg_toolong; /* TRUE when offset out of range */ |
7 | 612 static char_u had_endbrace[NSUBEXP]; /* flags, TRUE if end of () found */ |
613 static unsigned regflags; /* RF_ flags for prog */ | |
614 static long brace_min[10]; /* Minimums for complex brace repeats */ | |
615 static long brace_max[10]; /* Maximums for complex brace repeats */ | |
616 static int brace_count[10]; /* Current counts for complex brace repeats */ | |
617 #if defined(FEAT_SYN_HL) || defined(PROTO) | |
618 static int had_eol; /* TRUE when EOL found by vim_regcomp() */ | |
619 #endif | |
620 static int one_exactly = FALSE; /* only do one char for EXACTLY */ | |
621 | |
622 static int reg_magic; /* magicness of the pattern: */ | |
623 #define MAGIC_NONE 1 /* "\V" very unmagic */ | |
624 #define MAGIC_OFF 2 /* "\M" or 'magic' off */ | |
625 #define MAGIC_ON 3 /* "\m" or 'magic' */ | |
626 #define MAGIC_ALL 4 /* "\v" very magic */ | |
627 | |
628 static int reg_string; /* matching with a string instead of a buffer | |
629 line */ | |
481 | 630 static int reg_strict; /* "[abc" is illegal */ |
7 | 631 |
632 /* | |
633 * META contains all characters that may be magic, except '^' and '$'. | |
634 */ | |
635 | |
636 #ifdef EBCDIC | |
637 static char_u META[] = "%&()*+.123456789<=>?@ACDFHIKLMOPSUVWX[_acdfhiklmnopsuvwxz{|~"; | |
638 #else | |
639 /* META[] is used often enough to justify turning it into a table. */ | |
640 static char_u META_flags[] = { | |
641 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
642 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
643 /* % & ( ) * + . */ | |
644 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, | |
645 /* 1 2 3 4 5 6 7 8 9 < = > ? */ | |
646 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, | |
647 /* @ A C D F H I K L M O */ | |
648 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, | |
649 /* P S U V W X Z [ _ */ | |
650 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, | |
651 /* a c d f h i k l m n o */ | |
652 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, | |
653 /* p s u v w x z { | ~ */ | |
654 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1 | |
655 }; | |
656 #endif | |
657 | |
4444 | 658 static int curchr; /* currently parsed character */ |
659 /* Previous character. Note: prevchr is sometimes -1 when we are not at the | |
660 * start, eg in /[ ^I]^ the pattern was never found even if it existed, | |
661 * because ^ was taken to be magic -- webb */ | |
662 static int prevchr; | |
663 static int prevprevchr; /* previous-previous character */ | |
664 static int nextchr; /* used for ungetchr() */ | |
7 | 665 |
666 /* arguments for reg() */ | |
667 #define REG_NOPAREN 0 /* toplevel reg() */ | |
668 #define REG_PAREN 1 /* \(\) */ | |
669 #define REG_ZPAREN 2 /* \z(\) */ | |
670 #define REG_NPAREN 3 /* \%(\) */ | |
671 | |
4679
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
672 typedef struct |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
673 { |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
674 char_u *regparse; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
675 int prevchr_len; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
676 int curchr; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
677 int prevchr; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
678 int prevprevchr; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
679 int nextchr; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
680 int at_start; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
681 int prev_at_start; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
682 int regnpar; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
683 } parse_state_T; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
684 |
7 | 685 /* |
686 * Forward declarations for vim_regcomp()'s friends. | |
687 */ | |
688 static void initchr __ARGS((char_u *)); | |
4679
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
689 static void save_parse_state __ARGS((parse_state_T *ps)); |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
690 static void restore_parse_state __ARGS((parse_state_T *ps)); |
7 | 691 static int getchr __ARGS((void)); |
692 static void skipchr_keepstart __ARGS((void)); | |
693 static int peekchr __ARGS((void)); | |
694 static void skipchr __ARGS((void)); | |
695 static void ungetchr __ARGS((void)); | |
24 | 696 static int gethexchrs __ARGS((int maxinputlen)); |
697 static int getoctchrs __ARGS((void)); | |
698 static int getdecchrs __ARGS((void)); | |
699 static int coll_get_char __ARGS((void)); | |
7 | 700 static void regcomp_start __ARGS((char_u *expr, int flags)); |
701 static char_u *reg __ARGS((int, int *)); | |
702 static char_u *regbranch __ARGS((int *flagp)); | |
703 static char_u *regconcat __ARGS((int *flagp)); | |
704 static char_u *regpiece __ARGS((int *)); | |
705 static char_u *regatom __ARGS((int *)); | |
706 static char_u *regnode __ARGS((int)); | |
714 | 707 #ifdef FEAT_MBYTE |
708 static int use_multibytecode __ARGS((int c)); | |
709 #endif | |
7 | 710 static int prog_magic_wrong __ARGS((void)); |
711 static char_u *regnext __ARGS((char_u *)); | |
712 static void regc __ARGS((int b)); | |
713 #ifdef FEAT_MBYTE | |
714 static void regmbc __ARGS((int c)); | |
2974 | 715 # define REGMBC(x) regmbc(x); |
716 # define CASEMBC(x) case x: | |
167 | 717 #else |
718 # define regmbc(c) regc(c) | |
2974 | 719 # define REGMBC(x) |
720 # define CASEMBC(x) | |
7 | 721 #endif |
722 static void reginsert __ARGS((int, char_u *)); | |
4579
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
723 static void reginsert_nr __ARGS((int op, long val, char_u *opnd)); |
7 | 724 static void reginsert_limits __ARGS((int, long, long, char_u *)); |
725 static char_u *re_put_long __ARGS((char_u *pr, long_u val)); | |
726 static int read_limits __ARGS((long *, long *)); | |
727 static void regtail __ARGS((char_u *, char_u *)); | |
728 static void regoptail __ARGS((char_u *, char_u *)); | |
729 | |
4444 | 730 static regengine_T bt_regengine; |
731 static regengine_T nfa_regengine; | |
732 | |
7 | 733 /* |
734 * Return TRUE if compiled regular expression "prog" can match a line break. | |
735 */ | |
736 int | |
737 re_multiline(prog) | |
738 regprog_T *prog; | |
739 { | |
740 return (prog->regflags & RF_HASNL); | |
741 } | |
742 | |
743 /* | |
744 * Return TRUE if compiled regular expression "prog" looks before the start | |
745 * position (pattern contains "\@<=" or "\@<!"). | |
746 */ | |
747 int | |
748 re_lookbehind(prog) | |
749 regprog_T *prog; | |
750 { | |
751 return (prog->regflags & RF_LOOKBH); | |
752 } | |
753 | |
754 /* | |
167 | 755 * Check for an equivalence class name "[=a=]". "pp" points to the '['. |
756 * Returns a character representing the class. Zero means that no item was | |
757 * recognized. Otherwise "pp" is advanced to after the item. | |
758 */ | |
759 static int | |
760 get_equi_class(pp) | |
761 char_u **pp; | |
762 { | |
763 int c; | |
764 int l = 1; | |
765 char_u *p = *pp; | |
766 | |
767 if (p[1] == '=') | |
768 { | |
769 #ifdef FEAT_MBYTE | |
770 if (has_mbyte) | |
474 | 771 l = (*mb_ptr2len)(p + 2); |
167 | 772 #endif |
773 if (p[l + 2] == '=' && p[l + 3] == ']') | |
774 { | |
775 #ifdef FEAT_MBYTE | |
776 if (has_mbyte) | |
777 c = mb_ptr2char(p + 2); | |
778 else | |
779 #endif | |
780 c = p[2]; | |
781 *pp += l + 4; | |
782 return c; | |
783 } | |
784 } | |
785 return 0; | |
786 } | |
787 | |
2247
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
788 #ifdef EBCDIC |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
789 /* |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
790 * 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
|
791 */ |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
792 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
|
793 "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
|
794 "C\x68", |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
795 "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
|
796 "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
|
797 "N\x69", |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
798 "O\xEB\xEC\xED\xEE\xEF", |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
799 "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
|
800 "Y\xBA", |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
801 "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
|
802 "c\x48", |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
803 "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
|
804 "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
|
805 "n\x49", |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
806 "o\xCB\xCC\xCD\xCE\xCF", |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
807 "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
|
808 "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
|
809 }; |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
810 #endif |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
811 |
167 | 812 /* |
813 * Produce the bytes for equivalence class "c". | |
814 * Currently only handles latin1, latin9 and utf-8. | |
4444 | 815 * NOTE: When changing this function, also change nfa_emit_equi_class() |
167 | 816 */ |
817 static void | |
818 reg_equi_class(c) | |
819 int c; | |
820 { | |
821 #ifdef FEAT_MBYTE | |
822 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0 | |
492 | 823 || STRCMP(p_enc, "iso-8859-15") == 0) |
167 | 824 #endif |
825 { | |
2247
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
826 #ifdef EBCDIC |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
827 int i; |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
828 |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
829 /* 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
|
830 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
|
831 { |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
832 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
|
833 { |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
834 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
|
835 |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
836 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
|
837 regmbc(*p++); |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
838 return; |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
839 } |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
840 } |
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
841 #else |
167 | 842 switch (c) |
843 { | |
236 | 844 case 'A': case '\300': case '\301': case '\302': |
2974 | 845 CASEMBC(0x100) CASEMBC(0x102) CASEMBC(0x104) CASEMBC(0x1cd) |
846 CASEMBC(0x1de) CASEMBC(0x1e0) CASEMBC(0x1ea2) | |
236 | 847 case '\303': case '\304': case '\305': |
848 regmbc('A'); regmbc('\300'); regmbc('\301'); | |
849 regmbc('\302'); regmbc('\303'); regmbc('\304'); | |
850 regmbc('\305'); | |
2974 | 851 REGMBC(0x100) REGMBC(0x102) REGMBC(0x104) |
852 REGMBC(0x1cd) REGMBC(0x1de) REGMBC(0x1e0) | |
853 REGMBC(0x1ea2) | |
854 return; | |
855 case 'B': CASEMBC(0x1e02) CASEMBC(0x1e06) | |
856 regmbc('B'); REGMBC(0x1e02) REGMBC(0x1e06) | |
167 | 857 return; |
236 | 858 case 'C': case '\307': |
2974 | 859 CASEMBC(0x106) CASEMBC(0x108) CASEMBC(0x10a) CASEMBC(0x10c) |
236 | 860 regmbc('C'); regmbc('\307'); |
2974 | 861 REGMBC(0x106) REGMBC(0x108) REGMBC(0x10a) |
862 REGMBC(0x10c) | |
863 return; | |
864 case 'D': CASEMBC(0x10e) CASEMBC(0x110) CASEMBC(0x1e0a) | |
865 CASEMBC(0x1e0e) CASEMBC(0x1e10) | |
866 regmbc('D'); REGMBC(0x10e) REGMBC(0x110) | |
867 REGMBC(0x1e0a) REGMBC(0x1e0e) REGMBC(0x1e10) | |
167 | 868 return; |
236 | 869 case 'E': case '\310': case '\311': case '\312': case '\313': |
2974 | 870 CASEMBC(0x112) CASEMBC(0x114) CASEMBC(0x116) CASEMBC(0x118) |
871 CASEMBC(0x11a) CASEMBC(0x1eba) CASEMBC(0x1ebc) | |
236 | 872 regmbc('E'); regmbc('\310'); regmbc('\311'); |
873 regmbc('\312'); regmbc('\313'); | |
2974 | 874 REGMBC(0x112) REGMBC(0x114) REGMBC(0x116) |
875 REGMBC(0x118) REGMBC(0x11a) REGMBC(0x1eba) | |
876 REGMBC(0x1ebc) | |
877 return; | |
878 case 'F': CASEMBC(0x1e1e) | |
879 regmbc('F'); REGMBC(0x1e1e) | |
880 return; | |
881 case 'G': CASEMBC(0x11c) CASEMBC(0x11e) CASEMBC(0x120) | |
882 CASEMBC(0x122) CASEMBC(0x1e4) CASEMBC(0x1e6) CASEMBC(0x1f4) | |
883 CASEMBC(0x1e20) | |
884 regmbc('G'); REGMBC(0x11c) REGMBC(0x11e) | |
885 REGMBC(0x120) REGMBC(0x122) REGMBC(0x1e4) | |
886 REGMBC(0x1e6) REGMBC(0x1f4) REGMBC(0x1e20) | |
887 return; | |
888 case 'H': CASEMBC(0x124) CASEMBC(0x126) CASEMBC(0x1e22) | |
889 CASEMBC(0x1e26) CASEMBC(0x1e28) | |
890 regmbc('H'); REGMBC(0x124) REGMBC(0x126) | |
891 REGMBC(0x1e22) REGMBC(0x1e26) REGMBC(0x1e28) | |
167 | 892 return; |
236 | 893 case 'I': case '\314': case '\315': case '\316': case '\317': |
2974 | 894 CASEMBC(0x128) CASEMBC(0x12a) CASEMBC(0x12c) CASEMBC(0x12e) |
895 CASEMBC(0x130) CASEMBC(0x1cf) CASEMBC(0x1ec8) | |
236 | 896 regmbc('I'); regmbc('\314'); regmbc('\315'); |
897 regmbc('\316'); regmbc('\317'); | |
2974 | 898 REGMBC(0x128) REGMBC(0x12a) REGMBC(0x12c) |
899 REGMBC(0x12e) REGMBC(0x130) REGMBC(0x1cf) | |
900 REGMBC(0x1ec8) | |
901 return; | |
902 case 'J': CASEMBC(0x134) | |
903 regmbc('J'); REGMBC(0x134) | |
904 return; | |
905 case 'K': CASEMBC(0x136) CASEMBC(0x1e8) CASEMBC(0x1e30) | |
906 CASEMBC(0x1e34) | |
907 regmbc('K'); REGMBC(0x136) REGMBC(0x1e8) | |
908 REGMBC(0x1e30) REGMBC(0x1e34) | |
909 return; | |
910 case 'L': CASEMBC(0x139) CASEMBC(0x13b) CASEMBC(0x13d) | |
911 CASEMBC(0x13f) CASEMBC(0x141) CASEMBC(0x1e3a) | |
912 regmbc('L'); REGMBC(0x139) REGMBC(0x13b) | |
913 REGMBC(0x13d) REGMBC(0x13f) REGMBC(0x141) | |
914 REGMBC(0x1e3a) | |
915 return; | |
916 case 'M': CASEMBC(0x1e3e) CASEMBC(0x1e40) | |
917 regmbc('M'); REGMBC(0x1e3e) REGMBC(0x1e40) | |
167 | 918 return; |
236 | 919 case 'N': case '\321': |
2974 | 920 CASEMBC(0x143) CASEMBC(0x145) CASEMBC(0x147) CASEMBC(0x1e44) |
921 CASEMBC(0x1e48) | |
236 | 922 regmbc('N'); regmbc('\321'); |
2974 | 923 REGMBC(0x143) REGMBC(0x145) REGMBC(0x147) |
924 REGMBC(0x1e44) REGMBC(0x1e48) | |
167 | 925 return; |
236 | 926 case 'O': case '\322': case '\323': case '\324': case '\325': |
2974 | 927 case '\326': case '\330': |
928 CASEMBC(0x14c) CASEMBC(0x14e) CASEMBC(0x150) CASEMBC(0x1a0) | |
929 CASEMBC(0x1d1) CASEMBC(0x1ea) CASEMBC(0x1ec) CASEMBC(0x1ece) | |
236 | 930 regmbc('O'); regmbc('\322'); regmbc('\323'); |
931 regmbc('\324'); regmbc('\325'); regmbc('\326'); | |
2974 | 932 regmbc('\330'); |
933 REGMBC(0x14c) REGMBC(0x14e) REGMBC(0x150) | |
934 REGMBC(0x1a0) REGMBC(0x1d1) REGMBC(0x1ea) | |
935 REGMBC(0x1ec) REGMBC(0x1ece) | |
936 return; | |
937 case 'P': case 0x1e54: case 0x1e56: | |
938 regmbc('P'); REGMBC(0x1e54) REGMBC(0x1e56) | |
939 return; | |
940 case 'R': CASEMBC(0x154) CASEMBC(0x156) CASEMBC(0x158) | |
941 CASEMBC(0x1e58) CASEMBC(0x1e5e) | |
942 regmbc('R'); REGMBC(0x154) REGMBC(0x156) REGMBC(0x158) | |
943 REGMBC(0x1e58) REGMBC(0x1e5e) | |
944 return; | |
945 case 'S': CASEMBC(0x15a) CASEMBC(0x15c) CASEMBC(0x15e) | |
946 CASEMBC(0x160) CASEMBC(0x1e60) | |
947 regmbc('S'); REGMBC(0x15a) REGMBC(0x15c) | |
948 REGMBC(0x15e) REGMBC(0x160) REGMBC(0x1e60) | |
949 return; | |
950 case 'T': CASEMBC(0x162) CASEMBC(0x164) CASEMBC(0x166) | |
951 CASEMBC(0x1e6a) CASEMBC(0x1e6e) | |
952 regmbc('T'); REGMBC(0x162) REGMBC(0x164) | |
953 REGMBC(0x166) REGMBC(0x1e6a) REGMBC(0x1e6e) | |
167 | 954 return; |
236 | 955 case 'U': case '\331': case '\332': case '\333': case '\334': |
2974 | 956 CASEMBC(0x168) CASEMBC(0x16a) CASEMBC(0x16c) CASEMBC(0x16e) |
957 CASEMBC(0x170) CASEMBC(0x172) CASEMBC(0x1af) CASEMBC(0x1d3) | |
958 CASEMBC(0x1ee6) | |
236 | 959 regmbc('U'); regmbc('\331'); regmbc('\332'); |
960 regmbc('\333'); regmbc('\334'); | |
2974 | 961 REGMBC(0x168) REGMBC(0x16a) REGMBC(0x16c) |
962 REGMBC(0x16e) REGMBC(0x170) REGMBC(0x172) | |
963 REGMBC(0x1af) REGMBC(0x1d3) REGMBC(0x1ee6) | |
964 return; | |
965 case 'V': CASEMBC(0x1e7c) | |
966 regmbc('V'); REGMBC(0x1e7c) | |
967 return; | |
968 case 'W': CASEMBC(0x174) CASEMBC(0x1e80) CASEMBC(0x1e82) | |
969 CASEMBC(0x1e84) CASEMBC(0x1e86) | |
970 regmbc('W'); REGMBC(0x174) REGMBC(0x1e80) | |
971 REGMBC(0x1e82) REGMBC(0x1e84) REGMBC(0x1e86) | |
972 return; | |
973 case 'X': CASEMBC(0x1e8a) CASEMBC(0x1e8c) | |
974 regmbc('X'); REGMBC(0x1e8a) REGMBC(0x1e8c) | |
167 | 975 return; |
236 | 976 case 'Y': case '\335': |
2974 | 977 CASEMBC(0x176) CASEMBC(0x178) CASEMBC(0x1e8e) CASEMBC(0x1ef2) |
978 CASEMBC(0x1ef6) CASEMBC(0x1ef8) | |
236 | 979 regmbc('Y'); regmbc('\335'); |
2974 | 980 REGMBC(0x176) REGMBC(0x178) REGMBC(0x1e8e) |
981 REGMBC(0x1ef2) REGMBC(0x1ef6) REGMBC(0x1ef8) | |
982 return; | |
983 case 'Z': CASEMBC(0x179) CASEMBC(0x17b) CASEMBC(0x17d) | |
984 CASEMBC(0x1b5) CASEMBC(0x1e90) CASEMBC(0x1e94) | |
985 regmbc('Z'); REGMBC(0x179) REGMBC(0x17b) | |
986 REGMBC(0x17d) REGMBC(0x1b5) REGMBC(0x1e90) | |
987 REGMBC(0x1e94) | |
167 | 988 return; |
236 | 989 case 'a': case '\340': case '\341': case '\342': |
990 case '\343': case '\344': case '\345': | |
2974 | 991 CASEMBC(0x101) CASEMBC(0x103) CASEMBC(0x105) CASEMBC(0x1ce) |
992 CASEMBC(0x1df) CASEMBC(0x1e1) CASEMBC(0x1ea3) | |
236 | 993 regmbc('a'); regmbc('\340'); regmbc('\341'); |
994 regmbc('\342'); regmbc('\343'); regmbc('\344'); | |
995 regmbc('\345'); | |
2974 | 996 REGMBC(0x101) REGMBC(0x103) REGMBC(0x105) |
997 REGMBC(0x1ce) REGMBC(0x1df) REGMBC(0x1e1) | |
998 REGMBC(0x1ea3) | |
999 return; | |
1000 case 'b': CASEMBC(0x1e03) CASEMBC(0x1e07) | |
1001 regmbc('b'); REGMBC(0x1e03) REGMBC(0x1e07) | |
167 | 1002 return; |
236 | 1003 case 'c': case '\347': |
2974 | 1004 CASEMBC(0x107) CASEMBC(0x109) CASEMBC(0x10b) CASEMBC(0x10d) |
236 | 1005 regmbc('c'); regmbc('\347'); |
2974 | 1006 REGMBC(0x107) REGMBC(0x109) REGMBC(0x10b) |
1007 REGMBC(0x10d) | |
1008 return; | |
1009 case 'd': CASEMBC(0x10f) CASEMBC(0x111) CASEMBC(0x1d0b) | |
1010 CASEMBC(0x1e11) | |
1011 regmbc('d'); REGMBC(0x10f) REGMBC(0x111) | |
1012 REGMBC(0x1e0b) REGMBC(0x01e0f) REGMBC(0x1e11) | |
167 | 1013 return; |
236 | 1014 case 'e': case '\350': case '\351': case '\352': case '\353': |
2974 | 1015 CASEMBC(0x113) CASEMBC(0x115) CASEMBC(0x117) CASEMBC(0x119) |
1016 CASEMBC(0x11b) CASEMBC(0x1ebb) CASEMBC(0x1ebd) | |
236 | 1017 regmbc('e'); regmbc('\350'); regmbc('\351'); |
1018 regmbc('\352'); regmbc('\353'); | |
2974 | 1019 REGMBC(0x113) REGMBC(0x115) REGMBC(0x117) |
1020 REGMBC(0x119) REGMBC(0x11b) REGMBC(0x1ebb) | |
1021 REGMBC(0x1ebd) | |
1022 return; | |
1023 case 'f': CASEMBC(0x1e1f) | |
1024 regmbc('f'); REGMBC(0x1e1f) | |
1025 return; | |
1026 case 'g': CASEMBC(0x11d) CASEMBC(0x11f) CASEMBC(0x121) | |
1027 CASEMBC(0x123) CASEMBC(0x1e5) CASEMBC(0x1e7) CASEMBC(0x1f5) | |
1028 CASEMBC(0x1e21) | |
1029 regmbc('g'); REGMBC(0x11d) REGMBC(0x11f) | |
1030 REGMBC(0x121) REGMBC(0x123) REGMBC(0x1e5) | |
1031 REGMBC(0x1e7) REGMBC(0x1f5) REGMBC(0x1e21) | |
1032 return; | |
1033 case 'h': CASEMBC(0x125) CASEMBC(0x127) CASEMBC(0x1e23) | |
1034 CASEMBC(0x1e27) CASEMBC(0x1e29) CASEMBC(0x1e96) | |
1035 regmbc('h'); REGMBC(0x125) REGMBC(0x127) | |
1036 REGMBC(0x1e23) REGMBC(0x1e27) REGMBC(0x1e29) | |
1037 REGMBC(0x1e96) | |
167 | 1038 return; |
236 | 1039 case 'i': case '\354': case '\355': case '\356': case '\357': |
2974 | 1040 CASEMBC(0x129) CASEMBC(0x12b) CASEMBC(0x12d) CASEMBC(0x12f) |
1041 CASEMBC(0x1d0) CASEMBC(0x1ec9) | |
236 | 1042 regmbc('i'); regmbc('\354'); regmbc('\355'); |
1043 regmbc('\356'); regmbc('\357'); | |
2974 | 1044 REGMBC(0x129) REGMBC(0x12b) REGMBC(0x12d) |
1045 REGMBC(0x12f) REGMBC(0x1d0) REGMBC(0x1ec9) | |
1046 return; | |
1047 case 'j': CASEMBC(0x135) CASEMBC(0x1f0) | |
1048 regmbc('j'); REGMBC(0x135) REGMBC(0x1f0) | |
1049 return; | |
1050 case 'k': CASEMBC(0x137) CASEMBC(0x1e9) CASEMBC(0x1e31) | |
1051 CASEMBC(0x1e35) | |
1052 regmbc('k'); REGMBC(0x137) REGMBC(0x1e9) | |
1053 REGMBC(0x1e31) REGMBC(0x1e35) | |
1054 return; | |
1055 case 'l': CASEMBC(0x13a) CASEMBC(0x13c) CASEMBC(0x13e) | |
1056 CASEMBC(0x140) CASEMBC(0x142) CASEMBC(0x1e3b) | |
1057 regmbc('l'); REGMBC(0x13a) REGMBC(0x13c) | |
1058 REGMBC(0x13e) REGMBC(0x140) REGMBC(0x142) | |
1059 REGMBC(0x1e3b) | |
1060 return; | |
1061 case 'm': CASEMBC(0x1e3f) CASEMBC(0x1e41) | |
1062 regmbc('m'); REGMBC(0x1e3f) REGMBC(0x1e41) | |
167 | 1063 return; |
236 | 1064 case 'n': case '\361': |
2974 | 1065 CASEMBC(0x144) CASEMBC(0x146) CASEMBC(0x148) CASEMBC(0x149) |
1066 CASEMBC(0x1e45) CASEMBC(0x1e49) | |
236 | 1067 regmbc('n'); regmbc('\361'); |
2974 | 1068 REGMBC(0x144) REGMBC(0x146) REGMBC(0x148) |
1069 REGMBC(0x149) REGMBC(0x1e45) REGMBC(0x1e49) | |
167 | 1070 return; |
236 | 1071 case 'o': case '\362': case '\363': case '\364': case '\365': |
2974 | 1072 case '\366': case '\370': |
1073 CASEMBC(0x14d) CASEMBC(0x14f) CASEMBC(0x151) CASEMBC(0x1a1) | |
1074 CASEMBC(0x1d2) CASEMBC(0x1eb) CASEMBC(0x1ed) CASEMBC(0x1ecf) | |
236 | 1075 regmbc('o'); regmbc('\362'); regmbc('\363'); |
1076 regmbc('\364'); regmbc('\365'); regmbc('\366'); | |
2974 | 1077 regmbc('\370'); |
1078 REGMBC(0x14d) REGMBC(0x14f) REGMBC(0x151) | |
1079 REGMBC(0x1a1) REGMBC(0x1d2) REGMBC(0x1eb) | |
1080 REGMBC(0x1ed) REGMBC(0x1ecf) | |
1081 return; | |
1082 case 'p': CASEMBC(0x1e55) CASEMBC(0x1e57) | |
1083 regmbc('p'); REGMBC(0x1e55) REGMBC(0x1e57) | |
1084 return; | |
1085 case 'r': CASEMBC(0x155) CASEMBC(0x157) CASEMBC(0x159) | |
1086 CASEMBC(0x1e59) CASEMBC(0x1e5f) | |
1087 regmbc('r'); REGMBC(0x155) REGMBC(0x157) REGMBC(0x159) | |
1088 REGMBC(0x1e59) REGMBC(0x1e5f) | |
1089 return; | |
1090 case 's': CASEMBC(0x15b) CASEMBC(0x15d) CASEMBC(0x15f) | |
1091 CASEMBC(0x161) CASEMBC(0x1e61) | |
1092 regmbc('s'); REGMBC(0x15b) REGMBC(0x15d) | |
1093 REGMBC(0x15f) REGMBC(0x161) REGMBC(0x1e61) | |
1094 return; | |
1095 case 't': CASEMBC(0x163) CASEMBC(0x165) CASEMBC(0x167) | |
1096 CASEMBC(0x1e6b) CASEMBC(0x1e6f) CASEMBC(0x1e97) | |
1097 regmbc('t'); REGMBC(0x163) REGMBC(0x165) REGMBC(0x167) | |
1098 REGMBC(0x1e6b) REGMBC(0x1e6f) REGMBC(0x1e97) | |
167 | 1099 return; |
236 | 1100 case 'u': case '\371': case '\372': case '\373': case '\374': |
2974 | 1101 CASEMBC(0x169) CASEMBC(0x16b) CASEMBC(0x16d) CASEMBC(0x16f) |
1102 CASEMBC(0x171) CASEMBC(0x173) CASEMBC(0x1b0) CASEMBC(0x1d4) | |
1103 CASEMBC(0x1ee7) | |
236 | 1104 regmbc('u'); regmbc('\371'); regmbc('\372'); |
1105 regmbc('\373'); regmbc('\374'); | |
2974 | 1106 REGMBC(0x169) REGMBC(0x16b) REGMBC(0x16d) |
1107 REGMBC(0x16f) REGMBC(0x171) REGMBC(0x173) | |
1108 REGMBC(0x1b0) REGMBC(0x1d4) REGMBC(0x1ee7) | |
1109 return; | |
1110 case 'v': CASEMBC(0x1e7d) | |
1111 regmbc('v'); REGMBC(0x1e7d) | |
1112 return; | |
1113 case 'w': CASEMBC(0x175) CASEMBC(0x1e81) CASEMBC(0x1e83) | |
1114 CASEMBC(0x1e85) CASEMBC(0x1e87) CASEMBC(0x1e98) | |
1115 regmbc('w'); REGMBC(0x175) REGMBC(0x1e81) | |
1116 REGMBC(0x1e83) REGMBC(0x1e85) REGMBC(0x1e87) | |
1117 REGMBC(0x1e98) | |
1118 return; | |
1119 case 'x': CASEMBC(0x1e8b) CASEMBC(0x1e8d) | |
1120 regmbc('x'); REGMBC(0x1e8b) REGMBC(0x1e8d) | |
167 | 1121 return; |
236 | 1122 case 'y': case '\375': case '\377': |
2974 | 1123 CASEMBC(0x177) CASEMBC(0x1e8f) CASEMBC(0x1e99) |
1124 CASEMBC(0x1ef3) CASEMBC(0x1ef7) CASEMBC(0x1ef9) | |
236 | 1125 regmbc('y'); regmbc('\375'); regmbc('\377'); |
2974 | 1126 REGMBC(0x177) REGMBC(0x1e8f) REGMBC(0x1e99) |
1127 REGMBC(0x1ef3) REGMBC(0x1ef7) REGMBC(0x1ef9) | |
1128 return; | |
1129 case 'z': CASEMBC(0x17a) CASEMBC(0x17c) CASEMBC(0x17e) | |
1130 CASEMBC(0x1b6) CASEMBC(0x1e91) CASEMBC(0x1e95) | |
1131 regmbc('z'); REGMBC(0x17a) REGMBC(0x17c) | |
1132 REGMBC(0x17e) REGMBC(0x1b6) REGMBC(0x1e91) | |
1133 REGMBC(0x1e95) | |
167 | 1134 return; |
1135 } | |
2247
c40cd9aad546
Add patch to improve support of z/OS (OS/390). (Ralf Schandl)
Bram Moolenaar <bram@vim.org>
parents:
2173
diff
changeset
|
1136 #endif |
167 | 1137 } |
1138 regmbc(c); | |
1139 } | |
1140 | |
1141 /* | |
1142 * Check for a collating element "[.a.]". "pp" points to the '['. | |
1143 * Returns a character. Zero means that no item was recognized. Otherwise | |
1144 * "pp" is advanced to after the item. | |
1145 * Currently only single characters are recognized! | |
1146 */ | |
1147 static int | |
1148 get_coll_element(pp) | |
1149 char_u **pp; | |
1150 { | |
1151 int c; | |
1152 int l = 1; | |
1153 char_u *p = *pp; | |
1154 | |
1155 if (p[1] == '.') | |
1156 { | |
1157 #ifdef FEAT_MBYTE | |
1158 if (has_mbyte) | |
474 | 1159 l = (*mb_ptr2len)(p + 2); |
167 | 1160 #endif |
1161 if (p[l + 2] == '.' && p[l + 3] == ']') | |
1162 { | |
1163 #ifdef FEAT_MBYTE | |
1164 if (has_mbyte) | |
1165 c = mb_ptr2char(p + 2); | |
1166 else | |
1167 #endif | |
1168 c = p[2]; | |
1169 *pp += l + 4; | |
1170 return c; | |
1171 } | |
1172 } | |
1173 return 0; | |
1174 } | |
1175 | |
1176 | |
1177 /* | |
1178 * Skip over a "[]" range. | |
1179 * "p" must point to the character after the '['. | |
1180 * The returned pointer is on the matching ']', or the terminating NUL. | |
1181 */ | |
1182 static char_u * | |
1183 skip_anyof(p) | |
1184 char_u *p; | |
1185 { | |
1186 int cpo_lit; /* 'cpoptions' contains 'l' flag */ | |
1187 int cpo_bsl; /* 'cpoptions' contains '\' flag */ | |
1188 #ifdef FEAT_MBYTE | |
1189 int l; | |
1190 #endif | |
1191 | |
410 | 1192 cpo_lit = vim_strchr(p_cpo, CPO_LITERAL) != NULL; |
1193 cpo_bsl = vim_strchr(p_cpo, CPO_BACKSL) != NULL; | |
167 | 1194 |
1195 if (*p == '^') /* Complement of range. */ | |
1196 ++p; | |
1197 if (*p == ']' || *p == '-') | |
1198 ++p; | |
1199 while (*p != NUL && *p != ']') | |
1200 { | |
1201 #ifdef FEAT_MBYTE | |
474 | 1202 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1) |
167 | 1203 p += l; |
1204 else | |
1205 #endif | |
1206 if (*p == '-') | |
1207 { | |
1208 ++p; | |
1209 if (*p != ']' && *p != NUL) | |
1210 mb_ptr_adv(p); | |
1211 } | |
1212 else if (*p == '\\' | |
1213 && !cpo_bsl | |
1214 && (vim_strchr(REGEXP_INRANGE, p[1]) != NULL | |
1215 || (!cpo_lit && vim_strchr(REGEXP_ABBR, p[1]) != NULL))) | |
1216 p += 2; | |
1217 else if (*p == '[') | |
1218 { | |
1219 if (get_char_class(&p) == CLASS_NONE | |
1220 && get_equi_class(&p) == 0 | |
1221 && get_coll_element(&p) == 0) | |
1222 ++p; /* It was not a class name */ | |
1223 } | |
1224 else | |
1225 ++p; | |
1226 } | |
1227 | |
1228 return p; | |
1229 } | |
1230 | |
1231 /* | |
7 | 1232 * Skip past regular expression. |
153 | 1233 * Stop at end of "startp" or where "dirc" is found ('/', '?', etc). |
7 | 1234 * Take care of characters with a backslash in front of it. |
1235 * Skip strings inside [ and ]. | |
1236 * When "newp" is not NULL and "dirc" is '?', make an allocated copy of the | |
1237 * expression and change "\?" to "?". If "*newp" is not NULL the expression | |
1238 * is changed in-place. | |
1239 */ | |
1240 char_u * | |
1241 skip_regexp(startp, dirc, magic, newp) | |
1242 char_u *startp; | |
1243 int dirc; | |
1244 int magic; | |
1245 char_u **newp; | |
1246 { | |
1247 int mymagic; | |
1248 char_u *p = startp; | |
1249 | |
1250 if (magic) | |
1251 mymagic = MAGIC_ON; | |
1252 else | |
1253 mymagic = MAGIC_OFF; | |
1254 | |
39 | 1255 for (; p[0] != NUL; mb_ptr_adv(p)) |
7 | 1256 { |
1257 if (p[0] == dirc) /* found end of regexp */ | |
1258 break; | |
1259 if ((p[0] == '[' && mymagic >= MAGIC_ON) | |
1260 || (p[0] == '\\' && p[1] == '[' && mymagic <= MAGIC_OFF)) | |
1261 { | |
1262 p = skip_anyof(p + 1); | |
1263 if (p[0] == NUL) | |
1264 break; | |
1265 } | |
1266 else if (p[0] == '\\' && p[1] != NUL) | |
1267 { | |
1268 if (dirc == '?' && newp != NULL && p[1] == '?') | |
1269 { | |
1270 /* change "\?" to "?", make a copy first. */ | |
1271 if (*newp == NULL) | |
1272 { | |
1273 *newp = vim_strsave(startp); | |
1274 if (*newp != NULL) | |
1275 p = *newp + (p - startp); | |
1276 } | |
1277 if (*newp != NULL) | |
1621 | 1278 STRMOVE(p, p + 1); |
7 | 1279 else |
1280 ++p; | |
1281 } | |
1282 else | |
1283 ++p; /* skip next character */ | |
1284 if (*p == 'v') | |
1285 mymagic = MAGIC_ALL; | |
1286 else if (*p == 'V') | |
1287 mymagic = MAGIC_NONE; | |
1288 } | |
1289 } | |
1290 return p; | |
1291 } | |
1292 | |
4444 | 1293 static regprog_T *bt_regcomp __ARGS((char_u *expr, int re_flags)); |
1294 | |
7 | 1295 /* |
4444 | 1296 * bt_regcomp() - compile a regular expression into internal code for the |
1297 * traditional back track matcher. | |
41 | 1298 * Returns the program in allocated space. Returns NULL for an error. |
7 | 1299 * |
1300 * We can't allocate space until we know how big the compiled form will be, | |
1301 * but we can't compile it (and thus know how big it is) until we've got a | |
1302 * place to put the code. So we cheat: we compile it twice, once with code | |
1303 * generation turned off and size counting turned on, and once "for real". | |
1304 * This also means that we don't allocate space until we are sure that the | |
1305 * thing really will compile successfully, and we never have to move the | |
1306 * code and thus invalidate pointers into it. (Note that it has to be in | |
1307 * one piece because vim_free() must be able to free it all.) | |
1308 * | |
1309 * Whether upper/lower case is to be ignored is decided when executing the | |
1310 * program, it does not matter here. | |
1311 * | |
1312 * Beware that the optimization-preparation code in here knows about some | |
1313 * of the structure of the compiled regexp. | |
1314 * "re_flags": RE_MAGIC and/or RE_STRING. | |
1315 */ | |
4444 | 1316 static regprog_T * |
1317 bt_regcomp(expr, re_flags) | |
7 | 1318 char_u *expr; |
1319 int re_flags; | |
1320 { | |
4444 | 1321 bt_regprog_T *r; |
7 | 1322 char_u *scan; |
1323 char_u *longest; | |
1324 int len; | |
1325 int flags; | |
1326 | |
1327 if (expr == NULL) | |
1328 EMSG_RET_NULL(_(e_null)); | |
1329 | |
1330 init_class_tab(); | |
1331 | |
1332 /* | |
1333 * First pass: determine size, legality. | |
1334 */ | |
1335 regcomp_start(expr, re_flags); | |
1336 regcode = JUST_CALC_SIZE; | |
1337 regc(REGMAGIC); | |
1338 if (reg(REG_NOPAREN, &flags) == NULL) | |
1339 return NULL; | |
1340 | |
1341 /* Small enough for pointer-storage convention? */ | |
1342 #ifdef SMALL_MALLOC /* 16 bit storage allocation */ | |
1343 if (regsize >= 65536L - 256L) | |
1344 EMSG_RET_NULL(_("E339: Pattern too long")); | |
1345 #endif | |
1346 | |
1347 /* Allocate space. */ | |
4444 | 1348 r = (bt_regprog_T *)lalloc(sizeof(bt_regprog_T) + regsize, TRUE); |
7 | 1349 if (r == NULL) |
1350 return NULL; | |
1351 | |
1352 /* | |
1353 * Second pass: emit code. | |
1354 */ | |
1355 regcomp_start(expr, re_flags); | |
1356 regcode = r->program; | |
1357 regc(REGMAGIC); | |
2010 | 1358 if (reg(REG_NOPAREN, &flags) == NULL || reg_toolong) |
7 | 1359 { |
1360 vim_free(r); | |
2010 | 1361 if (reg_toolong) |
1362 EMSG_RET_NULL(_("E339: Pattern too long")); | |
7 | 1363 return NULL; |
1364 } | |
1365 | |
1366 /* Dig out information for optimizations. */ | |
1367 r->regstart = NUL; /* Worst-case defaults. */ | |
1368 r->reganch = 0; | |
1369 r->regmust = NULL; | |
1370 r->regmlen = 0; | |
1371 r->regflags = regflags; | |
1372 if (flags & HASNL) | |
1373 r->regflags |= RF_HASNL; | |
1374 if (flags & HASLOOKBH) | |
1375 r->regflags |= RF_LOOKBH; | |
1376 #ifdef FEAT_SYN_HL | |
1377 /* Remember whether this pattern has any \z specials in it. */ | |
1378 r->reghasz = re_has_z; | |
1379 #endif | |
1380 scan = r->program + 1; /* First BRANCH. */ | |
1381 if (OP(regnext(scan)) == END) /* Only one top-level choice. */ | |
1382 { | |
1383 scan = OPERAND(scan); | |
1384 | |
1385 /* Starting-point info. */ | |
1386 if (OP(scan) == BOL || OP(scan) == RE_BOF) | |
1387 { | |
1388 r->reganch++; | |
1389 scan = regnext(scan); | |
1390 } | |
1391 | |
1392 if (OP(scan) == EXACTLY) | |
1393 { | |
1394 #ifdef FEAT_MBYTE | |
1395 if (has_mbyte) | |
1396 r->regstart = (*mb_ptr2char)(OPERAND(scan)); | |
1397 else | |
1398 #endif | |
1399 r->regstart = *OPERAND(scan); | |
1400 } | |
1401 else if ((OP(scan) == BOW | |
1402 || OP(scan) == EOW | |
1403 || OP(scan) == NOTHING | |
1404 || OP(scan) == MOPEN + 0 || OP(scan) == NOPEN | |
1405 || OP(scan) == MCLOSE + 0 || OP(scan) == NCLOSE) | |
1406 && OP(regnext(scan)) == EXACTLY) | |
1407 { | |
1408 #ifdef FEAT_MBYTE | |
1409 if (has_mbyte) | |
1410 r->regstart = (*mb_ptr2char)(OPERAND(regnext(scan))); | |
1411 else | |
1412 #endif | |
1413 r->regstart = *OPERAND(regnext(scan)); | |
1414 } | |
1415 | |
1416 /* | |
1417 * If there's something expensive in the r.e., find the longest | |
1418 * literal string that must appear and make it the regmust. Resolve | |
1419 * ties in favor of later strings, since the regstart check works | |
1420 * with the beginning of the r.e. and avoiding duplication | |
1421 * strengthens checking. Not a strong reason, but sufficient in the | |
1422 * absence of others. | |
1423 */ | |
1424 /* | |
1425 * When the r.e. starts with BOW, it is faster to look for a regmust | |
1426 * first. Used a lot for "#" and "*" commands. (Added by mool). | |
1427 */ | |
1428 if ((flags & SPSTART || OP(scan) == BOW || OP(scan) == EOW) | |
1429 && !(flags & HASNL)) | |
1430 { | |
1431 longest = NULL; | |
1432 len = 0; | |
1433 for (; scan != NULL; scan = regnext(scan)) | |
1434 if (OP(scan) == EXACTLY && STRLEN(OPERAND(scan)) >= (size_t)len) | |
1435 { | |
1436 longest = OPERAND(scan); | |
1437 len = (int)STRLEN(OPERAND(scan)); | |
1438 } | |
1439 r->regmust = longest; | |
1440 r->regmlen = len; | |
1441 } | |
1442 } | |
4444 | 1443 #ifdef BT_REGEXP_DUMP |
7 | 1444 regdump(expr, r); |
1445 #endif | |
4444 | 1446 r->engine = &bt_regengine; |
1447 return (regprog_T *)r; | |
7 | 1448 } |
1449 | |
1450 /* | |
1451 * Setup to parse the regexp. Used once to get the length and once to do it. | |
1452 */ | |
1453 static void | |
1454 regcomp_start(expr, re_flags) | |
1455 char_u *expr; | |
1456 int re_flags; /* see vim_regcomp() */ | |
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); |
7 | 1465 |
1466 num_complex_braces = 0; | |
1467 regnpar = 1; | |
1468 vim_memset(had_endbrace, 0, sizeof(had_endbrace)); | |
1469 #ifdef FEAT_SYN_HL | |
1470 regnzpar = 1; | |
1471 re_has_z = 0; | |
1472 #endif | |
1473 regsize = 0L; | |
2010 | 1474 reg_toolong = FALSE; |
7 | 1475 regflags = 0; |
1476 #if defined(FEAT_SYN_HL) || defined(PROTO) | |
1477 had_eol = FALSE; | |
1478 #endif | |
1479 } | |
1480 | |
1481 #if defined(FEAT_SYN_HL) || defined(PROTO) | |
1482 /* | |
1483 * Check if during the previous call to vim_regcomp the EOL item "$" has been | |
1484 * found. This is messy, but it works fine. | |
1485 */ | |
1486 int | |
1487 vim_regcomp_had_eol() | |
1488 { | |
1489 return had_eol; | |
1490 } | |
1491 #endif | |
1492 | |
1493 /* | |
4444 | 1494 * Parse regular expression, i.e. main body or parenthesized thing. |
7 | 1495 * |
1496 * Caller must absorb opening parenthesis. | |
1497 * | |
1498 * Combining parenthesis handling with the base level of regular expression | |
1499 * is a trifle forced, but the need to tie the tails of the branches to what | |
1500 * follows makes it hard to avoid. | |
1501 */ | |
1502 static char_u * | |
1503 reg(paren, flagp) | |
1504 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */ | |
1505 int *flagp; | |
1506 { | |
1507 char_u *ret; | |
1508 char_u *br; | |
1509 char_u *ender; | |
1510 int parno = 0; | |
1511 int flags; | |
1512 | |
1513 *flagp = HASWIDTH; /* Tentatively. */ | |
1514 | |
1515 #ifdef FEAT_SYN_HL | |
1516 if (paren == REG_ZPAREN) | |
1517 { | |
1518 /* Make a ZOPEN node. */ | |
1519 if (regnzpar >= NSUBEXP) | |
1520 EMSG_RET_NULL(_("E50: Too many \\z(")); | |
1521 parno = regnzpar; | |
1522 regnzpar++; | |
1523 ret = regnode(ZOPEN + parno); | |
1524 } | |
1525 else | |
1526 #endif | |
1527 if (paren == REG_PAREN) | |
1528 { | |
1529 /* Make a MOPEN node. */ | |
1530 if (regnpar >= NSUBEXP) | |
4444 | 1531 EMSG2_RET_NULL(_("E51: Too many %s("), reg_magic == MAGIC_ALL); |
7 | 1532 parno = regnpar; |
1533 ++regnpar; | |
1534 ret = regnode(MOPEN + parno); | |
1535 } | |
1536 else if (paren == REG_NPAREN) | |
1537 { | |
1538 /* Make a NOPEN node. */ | |
1539 ret = regnode(NOPEN); | |
1540 } | |
1541 else | |
1542 ret = NULL; | |
1543 | |
1544 /* Pick up the branches, linking them together. */ | |
1545 br = regbranch(&flags); | |
1546 if (br == NULL) | |
1547 return NULL; | |
1548 if (ret != NULL) | |
1549 regtail(ret, br); /* [MZ]OPEN -> first. */ | |
1550 else | |
1551 ret = br; | |
1552 /* If one of the branches can be zero-width, the whole thing can. | |
1553 * If one of the branches has * at start or matches a line-break, the | |
1554 * whole thing can. */ | |
1555 if (!(flags & HASWIDTH)) | |
1556 *flagp &= ~HASWIDTH; | |
1557 *flagp |= flags & (SPSTART | HASNL | HASLOOKBH); | |
1558 while (peekchr() == Magic('|')) | |
1559 { | |
1560 skipchr(); | |
1561 br = regbranch(&flags); | |
2010 | 1562 if (br == NULL || reg_toolong) |
7 | 1563 return NULL; |
1564 regtail(ret, br); /* BRANCH -> BRANCH. */ | |
1565 if (!(flags & HASWIDTH)) | |
1566 *flagp &= ~HASWIDTH; | |
1567 *flagp |= flags & (SPSTART | HASNL | HASLOOKBH); | |
1568 } | |
1569 | |
1570 /* Make a closing node, and hook it on the end. */ | |
1571 ender = regnode( | |
1572 #ifdef FEAT_SYN_HL | |
1573 paren == REG_ZPAREN ? ZCLOSE + parno : | |
1574 #endif | |
1575 paren == REG_PAREN ? MCLOSE + parno : | |
1576 paren == REG_NPAREN ? NCLOSE : END); | |
1577 regtail(ret, ender); | |
1578 | |
1579 /* Hook the tails of the branches to the closing node. */ | |
1580 for (br = ret; br != NULL; br = regnext(br)) | |
1581 regoptail(br, ender); | |
1582 | |
1583 /* Check for proper termination. */ | |
1584 if (paren != REG_NOPAREN && getchr() != Magic(')')) | |
1585 { | |
1586 #ifdef FEAT_SYN_HL | |
1587 if (paren == REG_ZPAREN) | |
308 | 1588 EMSG_RET_NULL(_("E52: Unmatched \\z(")); |
7 | 1589 else |
1590 #endif | |
1591 if (paren == REG_NPAREN) | |
4444 | 1592 EMSG2_RET_NULL(_(e_unmatchedpp), reg_magic == MAGIC_ALL); |
7 | 1593 else |
4444 | 1594 EMSG2_RET_NULL(_(e_unmatchedp), reg_magic == MAGIC_ALL); |
7 | 1595 } |
1596 else if (paren == REG_NOPAREN && peekchr() != NUL) | |
1597 { | |
1598 if (curchr == Magic(')')) | |
4444 | 1599 EMSG2_RET_NULL(_(e_unmatchedpar), reg_magic == MAGIC_ALL); |
7 | 1600 else |
308 | 1601 EMSG_RET_NULL(_(e_trailing)); /* "Can't happen". */ |
7 | 1602 /* NOTREACHED */ |
1603 } | |
1604 /* | |
1605 * Here we set the flag allowing back references to this set of | |
1606 * parentheses. | |
1607 */ | |
1608 if (paren == REG_PAREN) | |
1609 had_endbrace[parno] = TRUE; /* have seen the close paren */ | |
1610 return ret; | |
1611 } | |
1612 | |
1613 /* | |
4444 | 1614 * Parse one alternative of an | operator. |
7 | 1615 * Implements the & operator. |
1616 */ | |
1617 static char_u * | |
1618 regbranch(flagp) | |
1619 int *flagp; | |
1620 { | |
1621 char_u *ret; | |
1622 char_u *chain = NULL; | |
1623 char_u *latest; | |
1624 int flags; | |
1625 | |
1626 *flagp = WORST | HASNL; /* Tentatively. */ | |
1627 | |
1628 ret = regnode(BRANCH); | |
1629 for (;;) | |
1630 { | |
1631 latest = regconcat(&flags); | |
1632 if (latest == NULL) | |
1633 return NULL; | |
1634 /* If one of the branches has width, the whole thing has. If one of | |
1635 * the branches anchors at start-of-line, the whole thing does. | |
1636 * If one of the branches uses look-behind, the whole thing does. */ | |
1637 *flagp |= flags & (HASWIDTH | SPSTART | HASLOOKBH); | |
1638 /* If one of the branches doesn't match a line-break, the whole thing | |
1639 * doesn't. */ | |
1640 *flagp &= ~HASNL | (flags & HASNL); | |
1641 if (chain != NULL) | |
1642 regtail(chain, latest); | |
1643 if (peekchr() != Magic('&')) | |
1644 break; | |
1645 skipchr(); | |
1646 regtail(latest, regnode(END)); /* operand ends */ | |
2010 | 1647 if (reg_toolong) |
1648 break; | |
7 | 1649 reginsert(MATCH, latest); |
1650 chain = latest; | |
1651 } | |
1652 | |
1653 return ret; | |
1654 } | |
1655 | |
1656 /* | |
4444 | 1657 * Parse one alternative of an | or & operator. |
7 | 1658 * Implements the concatenation operator. |
1659 */ | |
1660 static char_u * | |
1661 regconcat(flagp) | |
1662 int *flagp; | |
1663 { | |
1664 char_u *first = NULL; | |
1665 char_u *chain = NULL; | |
1666 char_u *latest; | |
1667 int flags; | |
1668 int cont = TRUE; | |
1669 | |
1670 *flagp = WORST; /* Tentatively. */ | |
1671 | |
1672 while (cont) | |
1673 { | |
1674 switch (peekchr()) | |
1675 { | |
1676 case NUL: | |
1677 case Magic('|'): | |
1678 case Magic('&'): | |
1679 case Magic(')'): | |
1680 cont = FALSE; | |
1681 break; | |
1682 case Magic('Z'): | |
1683 #ifdef FEAT_MBYTE | |
1684 regflags |= RF_ICOMBINE; | |
1685 #endif | |
1686 skipchr_keepstart(); | |
1687 break; | |
1688 case Magic('c'): | |
1689 regflags |= RF_ICASE; | |
1690 skipchr_keepstart(); | |
1691 break; | |
1692 case Magic('C'): | |
1693 regflags |= RF_NOICASE; | |
1694 skipchr_keepstart(); | |
1695 break; | |
1696 case Magic('v'): | |
1697 reg_magic = MAGIC_ALL; | |
1698 skipchr_keepstart(); | |
1699 curchr = -1; | |
1700 break; | |
1701 case Magic('m'): | |
1702 reg_magic = MAGIC_ON; | |
1703 skipchr_keepstart(); | |
1704 curchr = -1; | |
1705 break; | |
1706 case Magic('M'): | |
1707 reg_magic = MAGIC_OFF; | |
1708 skipchr_keepstart(); | |
1709 curchr = -1; | |
1710 break; | |
1711 case Magic('V'): | |
1712 reg_magic = MAGIC_NONE; | |
1713 skipchr_keepstart(); | |
1714 curchr = -1; | |
1715 break; | |
1716 default: | |
1717 latest = regpiece(&flags); | |
2010 | 1718 if (latest == NULL || reg_toolong) |
7 | 1719 return NULL; |
1720 *flagp |= flags & (HASWIDTH | HASNL | HASLOOKBH); | |
1721 if (chain == NULL) /* First piece. */ | |
1722 *flagp |= flags & SPSTART; | |
1723 else | |
1724 regtail(chain, latest); | |
1725 chain = latest; | |
1726 if (first == NULL) | |
1727 first = latest; | |
1728 break; | |
1729 } | |
1730 } | |
1731 if (first == NULL) /* Loop ran zero times. */ | |
1732 first = regnode(NOTHING); | |
1733 return first; | |
1734 } | |
1735 | |
1736 /* | |
4444 | 1737 * Parse something followed by possible [*+=]. |
7 | 1738 * |
1739 * Note that the branching code sequences used for = and the general cases | |
1740 * of * and + are somewhat optimized: they use the same NOTHING node as | |
1741 * both the endmarker for their branch list and the body of the last branch. | |
1742 * It might seem that this node could be dispensed with entirely, but the | |
1743 * endmarker role is not redundant. | |
1744 */ | |
1745 static char_u * | |
1746 regpiece(flagp) | |
1747 int *flagp; | |
1748 { | |
1749 char_u *ret; | |
1750 int op; | |
1751 char_u *next; | |
1752 int flags; | |
1753 long minval; | |
1754 long maxval; | |
1755 | |
1756 ret = regatom(&flags); | |
1757 if (ret == NULL) | |
1758 return NULL; | |
1759 | |
1760 op = peekchr(); | |
1761 if (re_multi_type(op) == NOT_MULTI) | |
1762 { | |
1763 *flagp = flags; | |
1764 return ret; | |
1765 } | |
1766 /* default flags */ | |
1767 *flagp = (WORST | SPSTART | (flags & (HASNL | HASLOOKBH))); | |
1768 | |
1769 skipchr(); | |
1770 switch (op) | |
1771 { | |
1772 case Magic('*'): | |
1773 if (flags & SIMPLE) | |
1774 reginsert(STAR, ret); | |
1775 else | |
1776 { | |
1777 /* Emit x* as (x&|), where & means "self". */ | |
1778 reginsert(BRANCH, ret); /* Either x */ | |
1779 regoptail(ret, regnode(BACK)); /* and loop */ | |
1780 regoptail(ret, ret); /* back */ | |
1781 regtail(ret, regnode(BRANCH)); /* or */ | |
1782 regtail(ret, regnode(NOTHING)); /* null. */ | |
1783 } | |
1784 break; | |
1785 | |
1786 case Magic('+'): | |
1787 if (flags & SIMPLE) | |
1788 reginsert(PLUS, ret); | |
1789 else | |
1790 { | |
1791 /* Emit x+ as x(&|), where & means "self". */ | |
1792 next = regnode(BRANCH); /* Either */ | |
1793 regtail(ret, next); | |
233 | 1794 regtail(regnode(BACK), ret); /* loop back */ |
7 | 1795 regtail(next, regnode(BRANCH)); /* or */ |
1796 regtail(ret, regnode(NOTHING)); /* null. */ | |
1797 } | |
1798 *flagp = (WORST | HASWIDTH | (flags & (HASNL | HASLOOKBH))); | |
1799 break; | |
1800 | |
1801 case Magic('@'): | |
1802 { | |
1803 int lop = END; | |
4579
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
1804 int nr; |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
1805 |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
1806 nr = getdecchrs(); |
7 | 1807 switch (no_Magic(getchr())) |
1808 { | |
1809 case '=': lop = MATCH; break; /* \@= */ | |
1810 case '!': lop = NOMATCH; break; /* \@! */ | |
1811 case '>': lop = SUBPAT; break; /* \@> */ | |
1812 case '<': switch (no_Magic(getchr())) | |
1813 { | |
1814 case '=': lop = BEHIND; break; /* \@<= */ | |
1815 case '!': lop = NOBEHIND; break; /* \@<! */ | |
1816 } | |
1817 } | |
1818 if (lop == END) | |
4444 | 1819 EMSG2_RET_NULL(_("E59: invalid character after %s@"), |
7 | 1820 reg_magic == MAGIC_ALL); |
1821 /* Look behind must match with behind_pos. */ | |
1822 if (lop == BEHIND || lop == NOBEHIND) | |
1823 { | |
1824 regtail(ret, regnode(BHPOS)); | |
1825 *flagp |= HASLOOKBH; | |
1826 } | |
1827 regtail(ret, regnode(END)); /* operand ends */ | |
4579
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
1828 if (lop == BEHIND || lop == NOBEHIND) |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
1829 { |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
1830 if (nr < 0) |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
1831 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
|
1832 reginsert_nr(lop, nr, ret); |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
1833 } |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
1834 else |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
1835 reginsert(lop, ret); |
7 | 1836 break; |
1837 } | |
1838 | |
1839 case Magic('?'): | |
1840 case Magic('='): | |
1841 /* Emit x= as (x|) */ | |
1842 reginsert(BRANCH, ret); /* Either x */ | |
1843 regtail(ret, regnode(BRANCH)); /* or */ | |
1844 next = regnode(NOTHING); /* null. */ | |
1845 regtail(ret, next); | |
1846 regoptail(ret, next); | |
1847 break; | |
1848 | |
1849 case Magic('{'): | |
1850 if (!read_limits(&minval, &maxval)) | |
1851 return NULL; | |
1852 if (flags & SIMPLE) | |
1853 { | |
1854 reginsert(BRACE_SIMPLE, ret); | |
1855 reginsert_limits(BRACE_LIMITS, minval, maxval, ret); | |
1856 } | |
1857 else | |
1858 { | |
1859 if (num_complex_braces >= 10) | |
4444 | 1860 EMSG2_RET_NULL(_("E60: Too many complex %s{...}s"), |
7 | 1861 reg_magic == MAGIC_ALL); |
1862 reginsert(BRACE_COMPLEX + num_complex_braces, ret); | |
1863 regoptail(ret, regnode(BACK)); | |
1864 regoptail(ret, ret); | |
1865 reginsert_limits(BRACE_LIMITS, minval, maxval, ret); | |
1866 ++num_complex_braces; | |
1867 } | |
1868 if (minval > 0 && maxval > 0) | |
1869 *flagp = (HASWIDTH | (flags & (HASNL | HASLOOKBH))); | |
1870 break; | |
1871 } | |
1872 if (re_multi_type(peekchr()) != NOT_MULTI) | |
1873 { | |
1874 /* Can't have a multi follow a multi. */ | |
1875 if (peekchr() == Magic('*')) | |
1876 sprintf((char *)IObuff, _("E61: Nested %s*"), | |
1877 reg_magic >= MAGIC_ON ? "" : "\\"); | |
1878 else | |
1879 sprintf((char *)IObuff, _("E62: Nested %s%c"), | |
1880 reg_magic == MAGIC_ALL ? "" : "\\", no_Magic(peekchr())); | |
1881 EMSG_RET_NULL(IObuff); | |
1882 } | |
1883 | |
1884 return ret; | |
1885 } | |
1886 | |
4444 | 1887 /* When making changes to classchars also change nfa_classcodes. */ |
1888 static char_u *classchars = (char_u *)".iIkKfFpPsSdDxXoOwWhHaAlLuU"; | |
1889 static int classcodes[] = { | |
1890 ANY, IDENT, SIDENT, KWORD, SKWORD, | |
1891 FNAME, SFNAME, PRINT, SPRINT, | |
1892 WHITE, NWHITE, DIGIT, NDIGIT, | |
1893 HEX, NHEX, OCTAL, NOCTAL, | |
1894 WORD, NWORD, HEAD, NHEAD, | |
1895 ALPHA, NALPHA, LOWER, NLOWER, | |
1896 UPPER, NUPPER | |
1897 }; | |
1898 | |
7 | 1899 /* |
4444 | 1900 * Parse the lowest level. |
7 | 1901 * |
1902 * Optimization: gobbles an entire sequence of ordinary characters so that | |
1903 * it can turn them into a single node, which is smaller to store and | |
1904 * faster to run. Don't do this when one_exactly is set. | |
1905 */ | |
1906 static char_u * | |
1907 regatom(flagp) | |
1908 int *flagp; | |
1909 { | |
1910 char_u *ret; | |
1911 int flags; | |
1912 int cpo_lit; /* 'cpoptions' contains 'l' flag */ | |
167 | 1913 int cpo_bsl; /* 'cpoptions' contains '\' flag */ |
7 | 1914 int c; |
1915 char_u *p; | |
1916 int extra = 0; | |
1917 | |
1918 *flagp = WORST; /* Tentatively. */ | |
410 | 1919 cpo_lit = vim_strchr(p_cpo, CPO_LITERAL) != NULL; |
1920 cpo_bsl = vim_strchr(p_cpo, CPO_BACKSL) != NULL; | |
7 | 1921 |
1922 c = getchr(); | |
1923 switch (c) | |
1924 { | |
1925 case Magic('^'): | |
1926 ret = regnode(BOL); | |
1927 break; | |
1928 | |
1929 case Magic('$'): | |
1930 ret = regnode(EOL); | |
1931 #if defined(FEAT_SYN_HL) || defined(PROTO) | |
1932 had_eol = TRUE; | |
1933 #endif | |
1934 break; | |
1935 | |
1936 case Magic('<'): | |
1937 ret = regnode(BOW); | |
1938 break; | |
1939 | |
1940 case Magic('>'): | |
1941 ret = regnode(EOW); | |
1942 break; | |
1943 | |
1944 case Magic('_'): | |
1945 c = no_Magic(getchr()); | |
1946 if (c == '^') /* "\_^" is start-of-line */ | |
1947 { | |
1948 ret = regnode(BOL); | |
1949 break; | |
1950 } | |
1951 if (c == '$') /* "\_$" is end-of-line */ | |
1952 { | |
1953 ret = regnode(EOL); | |
1954 #if defined(FEAT_SYN_HL) || defined(PROTO) | |
1955 had_eol = TRUE; | |
1956 #endif | |
1957 break; | |
1958 } | |
1959 | |
1960 extra = ADD_NL; | |
1961 *flagp |= HASNL; | |
1962 | |
1963 /* "\_[" is character range plus newline */ | |
1964 if (c == '[') | |
1965 goto collection; | |
1966 | |
1967 /* "\_x" is character class plus newline */ | |
1968 /*FALLTHROUGH*/ | |
1969 | |
1970 /* | |
1971 * Character classes. | |
1972 */ | |
1973 case Magic('.'): | |
1974 case Magic('i'): | |
1975 case Magic('I'): | |
1976 case Magic('k'): | |
1977 case Magic('K'): | |
1978 case Magic('f'): | |
1979 case Magic('F'): | |
1980 case Magic('p'): | |
1981 case Magic('P'): | |
1982 case Magic('s'): | |
1983 case Magic('S'): | |
1984 case Magic('d'): | |
1985 case Magic('D'): | |
1986 case Magic('x'): | |
1987 case Magic('X'): | |
1988 case Magic('o'): | |
1989 case Magic('O'): | |
1990 case Magic('w'): | |
1991 case Magic('W'): | |
1992 case Magic('h'): | |
1993 case Magic('H'): | |
1994 case Magic('a'): | |
1995 case Magic('A'): | |
1996 case Magic('l'): | |
1997 case Magic('L'): | |
1998 case Magic('u'): | |
1999 case Magic('U'): | |
2000 p = vim_strchr(classchars, no_Magic(c)); | |
2001 if (p == NULL) | |
2002 EMSG_RET_NULL(_("E63: invalid use of \\_")); | |
714 | 2003 #ifdef FEAT_MBYTE |
2004 /* When '.' is followed by a composing char ignore the dot, so that | |
2005 * the composing char is matched here. */ | |
2006 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr())) | |
2007 { | |
2008 c = getchr(); | |
2009 goto do_multibyte; | |
2010 } | |
2011 #endif | |
7 | 2012 ret = regnode(classcodes[p - classchars] + extra); |
2013 *flagp |= HASWIDTH | SIMPLE; | |
2014 break; | |
2015 | |
2016 case Magic('n'): | |
2017 if (reg_string) | |
2018 { | |
2019 /* In a string "\n" matches a newline character. */ | |
2020 ret = regnode(EXACTLY); | |
2021 regc(NL); | |
2022 regc(NUL); | |
2023 *flagp |= HASWIDTH | SIMPLE; | |
2024 } | |
2025 else | |
2026 { | |
2027 /* In buffer text "\n" matches the end of a line. */ | |
2028 ret = regnode(NEWL); | |
2029 *flagp |= HASWIDTH | HASNL; | |
2030 } | |
2031 break; | |
2032 | |
2033 case Magic('('): | |
2034 if (one_exactly) | |
2035 EMSG_ONE_RET_NULL; | |
2036 ret = reg(REG_PAREN, &flags); | |
2037 if (ret == NULL) | |
2038 return NULL; | |
2039 *flagp |= flags & (HASWIDTH | SPSTART | HASNL | HASLOOKBH); | |
2040 break; | |
2041 | |
2042 case NUL: | |
2043 case Magic('|'): | |
2044 case Magic('&'): | |
2045 case Magic(')'): | |
1468 | 2046 if (one_exactly) |
2047 EMSG_ONE_RET_NULL; | |
7 | 2048 EMSG_RET_NULL(_(e_internal)); /* Supposed to be caught earlier. */ |
2049 /* NOTREACHED */ | |
2050 | |
2051 case Magic('='): | |
2052 case Magic('?'): | |
2053 case Magic('+'): | |
2054 case Magic('@'): | |
2055 case Magic('{'): | |
2056 case Magic('*'): | |
2057 c = no_Magic(c); | |
2058 sprintf((char *)IObuff, _("E64: %s%c follows nothing"), | |
2059 (c == '*' ? reg_magic >= MAGIC_ON : reg_magic == MAGIC_ALL) | |
2060 ? "" : "\\", c); | |
2061 EMSG_RET_NULL(IObuff); | |
2062 /* NOTREACHED */ | |
2063 | |
2064 case Magic('~'): /* previous substitute pattern */ | |
359 | 2065 if (reg_prev_sub != NULL) |
7 | 2066 { |
2067 char_u *lp; | |
2068 | |
2069 ret = regnode(EXACTLY); | |
2070 lp = reg_prev_sub; | |
2071 while (*lp != NUL) | |
2072 regc(*lp++); | |
2073 regc(NUL); | |
2074 if (*reg_prev_sub != NUL) | |
2075 { | |
2076 *flagp |= HASWIDTH; | |
2077 if ((lp - reg_prev_sub) == 1) | |
2078 *flagp |= SIMPLE; | |
2079 } | |
2080 } | |
2081 else | |
2082 EMSG_RET_NULL(_(e_nopresub)); | |
2083 break; | |
2084 | |
2085 case Magic('1'): | |
2086 case Magic('2'): | |
2087 case Magic('3'): | |
2088 case Magic('4'): | |
2089 case Magic('5'): | |
2090 case Magic('6'): | |
2091 case Magic('7'): | |
2092 case Magic('8'): | |
2093 case Magic('9'): | |
2094 { | |
2095 int refnum; | |
2096 | |
2097 refnum = c - Magic('0'); | |
2098 /* | |
2099 * Check if the back reference is legal. We must have seen the | |
2100 * close brace. | |
2101 * TODO: Should also check that we don't refer to something | |
2102 * that is repeated (+*=): what instance of the repetition | |
2103 * should we match? | |
2104 */ | |
2105 if (!had_endbrace[refnum]) | |
2106 { | |
2107 /* Trick: check if "@<=" or "@<!" follows, in which case | |
2108 * the \1 can appear before the referenced match. */ | |
2109 for (p = regparse; *p != NUL; ++p) | |
2110 if (p[0] == '@' && p[1] == '<' | |
2111 && (p[2] == '!' || p[2] == '=')) | |
2112 break; | |
2113 if (*p == NUL) | |
2114 EMSG_RET_NULL(_("E65: Illegal back reference")); | |
2115 } | |
2116 ret = regnode(BACKREF + refnum); | |
2117 } | |
2118 break; | |
2119 | |
2120 case Magic('z'): | |
2121 { | |
2122 c = no_Magic(getchr()); | |
2123 switch (c) | |
2124 { | |
741 | 2125 #ifdef FEAT_SYN_HL |
7 | 2126 case '(': if (reg_do_extmatch != REX_SET) |
4688
371cc0c44097
updated for version 7.3.1091
Bram Moolenaar <bram@vim.org>
parents:
4682
diff
changeset
|
2127 EMSG_RET_NULL(_(e_z_not_allowed)); |
7 | 2128 if (one_exactly) |
2129 EMSG_ONE_RET_NULL; | |
2130 ret = reg(REG_ZPAREN, &flags); | |
2131 if (ret == NULL) | |
2132 return NULL; | |
2133 *flagp |= flags & (HASWIDTH|SPSTART|HASNL|HASLOOKBH); | |
2134 re_has_z = REX_SET; | |
2135 break; | |
2136 | |
2137 case '1': | |
2138 case '2': | |
2139 case '3': | |
2140 case '4': | |
2141 case '5': | |
2142 case '6': | |
2143 case '7': | |
2144 case '8': | |
2145 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
|
2146 EMSG_RET_NULL(_(e_z1_not_allowed)); |
7 | 2147 ret = regnode(ZREF + c - '0'); |
2148 re_has_z = REX_USE; | |
2149 break; | |
741 | 2150 #endif |
7 | 2151 |
2152 case 's': ret = regnode(MOPEN + 0); | |
2153 break; | |
2154 | |
2155 case 'e': ret = regnode(MCLOSE + 0); | |
2156 break; | |
2157 | |
2158 default: EMSG_RET_NULL(_("E68: Invalid character after \\z")); | |
2159 } | |
2160 } | |
2161 break; | |
2162 | |
2163 case Magic('%'): | |
2164 { | |
2165 c = no_Magic(getchr()); | |
2166 switch (c) | |
2167 { | |
2168 /* () without a back reference */ | |
2169 case '(': | |
2170 if (one_exactly) | |
2171 EMSG_ONE_RET_NULL; | |
2172 ret = reg(REG_NPAREN, &flags); | |
2173 if (ret == NULL) | |
2174 return NULL; | |
2175 *flagp |= flags & (HASWIDTH | SPSTART | HASNL | HASLOOKBH); | |
2176 break; | |
2177 | |
2178 /* Catch \%^ and \%$ regardless of where they appear in the | |
2179 * pattern -- regardless of whether or not it makes sense. */ | |
2180 case '^': | |
2181 ret = regnode(RE_BOF); | |
2182 break; | |
2183 | |
2184 case '$': | |
2185 ret = regnode(RE_EOF); | |
2186 break; | |
2187 | |
2188 case '#': | |
2189 ret = regnode(CURSOR); | |
2190 break; | |
2191 | |
639 | 2192 case 'V': |
2193 ret = regnode(RE_VISUAL); | |
2194 break; | |
2195 | |
7 | 2196 /* \%[abc]: Emit as a list of branches, all ending at the last |
2197 * branch which matches nothing. */ | |
2198 case '[': | |
2199 if (one_exactly) /* doesn't nest */ | |
2200 EMSG_ONE_RET_NULL; | |
2201 { | |
2202 char_u *lastbranch; | |
2203 char_u *lastnode = NULL; | |
2204 char_u *br; | |
2205 | |
2206 ret = NULL; | |
2207 while ((c = getchr()) != ']') | |
2208 { | |
2209 if (c == NUL) | |
4444 | 2210 EMSG2_RET_NULL(_("E69: Missing ] after %s%%["), |
7 | 2211 reg_magic == MAGIC_ALL); |
2212 br = regnode(BRANCH); | |
2213 if (ret == NULL) | |
2214 ret = br; | |
2215 else | |
2216 regtail(lastnode, br); | |
2217 | |
2218 ungetchr(); | |
2219 one_exactly = TRUE; | |
2220 lastnode = regatom(flagp); | |
2221 one_exactly = FALSE; | |
2222 if (lastnode == NULL) | |
2223 return NULL; | |
2224 } | |
2225 if (ret == NULL) | |
4444 | 2226 EMSG2_RET_NULL(_("E70: Empty %s%%[]"), |
7 | 2227 reg_magic == MAGIC_ALL); |
2228 lastbranch = regnode(BRANCH); | |
2229 br = regnode(NOTHING); | |
2230 if (ret != JUST_CALC_SIZE) | |
2231 { | |
2232 regtail(lastnode, br); | |
2233 regtail(lastbranch, br); | |
2234 /* connect all branches to the NOTHING | |
2235 * branch at the end */ | |
2236 for (br = ret; br != lastnode; ) | |
2237 { | |
2238 if (OP(br) == BRANCH) | |
2239 { | |
2240 regtail(br, lastbranch); | |
2241 br = OPERAND(br); | |
2242 } | |
2243 else | |
2244 br = regnext(br); | |
2245 } | |
2246 } | |
1701 | 2247 *flagp &= ~(HASWIDTH | SIMPLE); |
7 | 2248 break; |
2249 } | |
2250 | |
24 | 2251 case 'd': /* %d123 decimal */ |
2252 case 'o': /* %o123 octal */ | |
2253 case 'x': /* %xab hex 2 */ | |
2254 case 'u': /* %uabcd hex 4 */ | |
2255 case 'U': /* %U1234abcd hex 8 */ | |
2256 { | |
2257 int i; | |
2258 | |
2259 switch (c) | |
2260 { | |
2261 case 'd': i = getdecchrs(); break; | |
2262 case 'o': i = getoctchrs(); break; | |
2263 case 'x': i = gethexchrs(2); break; | |
2264 case 'u': i = gethexchrs(4); break; | |
2265 case 'U': i = gethexchrs(8); break; | |
2266 default: i = -1; break; | |
2267 } | |
2268 | |
2269 if (i < 0) | |
4444 | 2270 EMSG2_RET_NULL( |
24 | 2271 _("E678: Invalid character after %s%%[dxouU]"), |
2272 reg_magic == MAGIC_ALL); | |
714 | 2273 #ifdef FEAT_MBYTE |
2274 if (use_multibytecode(i)) | |
2275 ret = regnode(MULTIBYTECODE); | |
2276 else | |
2277 #endif | |
2278 ret = regnode(EXACTLY); | |
24 | 2279 if (i == 0) |
2280 regc(0x0a); | |
2281 else | |
2282 #ifdef FEAT_MBYTE | |
2283 regmbc(i); | |
2284 #else | |
2285 regc(i); | |
2286 #endif | |
2287 regc(NUL); | |
2288 *flagp |= HASWIDTH; | |
2289 break; | |
2290 } | |
2291 | |
7 | 2292 default: |
639 | 2293 if (VIM_ISDIGIT(c) || c == '<' || c == '>' |
2294 || c == '\'') | |
7 | 2295 { |
2296 long_u n = 0; | |
2297 int cmp; | |
2298 | |
2299 cmp = c; | |
2300 if (cmp == '<' || cmp == '>') | |
2301 c = getchr(); | |
2302 while (VIM_ISDIGIT(c)) | |
2303 { | |
2304 n = n * 10 + (c - '0'); | |
2305 c = getchr(); | |
2306 } | |
639 | 2307 if (c == '\'' && n == 0) |
2308 { | |
2309 /* "\%'m", "\%<'m" and "\%>'m": Mark */ | |
2310 c = getchr(); | |
2311 ret = regnode(RE_MARK); | |
2312 if (ret == JUST_CALC_SIZE) | |
2313 regsize += 2; | |
2314 else | |
2315 { | |
2316 *regcode++ = c; | |
2317 *regcode++ = cmp; | |
2318 } | |
2319 break; | |
2320 } | |
2321 else if (c == 'l' || c == 'c' || c == 'v') | |
7 | 2322 { |
2323 if (c == 'l') | |
2324 ret = regnode(RE_LNUM); | |
2325 else if (c == 'c') | |
2326 ret = regnode(RE_COL); | |
2327 else | |
2328 ret = regnode(RE_VCOL); | |
2329 if (ret == JUST_CALC_SIZE) | |
2330 regsize += 5; | |
2331 else | |
2332 { | |
2333 /* put the number and the optional | |
2334 * comparator after the opcode */ | |
2335 regcode = re_put_long(regcode, n); | |
2336 *regcode++ = cmp; | |
2337 } | |
2338 break; | |
2339 } | |
2340 } | |
2341 | |
4444 | 2342 EMSG2_RET_NULL(_("E71: Invalid character after %s%%"), |
7 | 2343 reg_magic == MAGIC_ALL); |
2344 } | |
2345 } | |
2346 break; | |
2347 | |
2348 case Magic('['): | |
2349 collection: | |
2350 { | |
2351 char_u *lp; | |
2352 | |
2353 /* | |
2354 * If there is no matching ']', we assume the '[' is a normal | |
2355 * character. This makes 'incsearch' and ":help [" work. | |
2356 */ | |
2357 lp = skip_anyof(regparse); | |
2358 if (*lp == ']') /* there is a matching ']' */ | |
2359 { | |
2360 int startc = -1; /* > 0 when next '-' is a range */ | |
2361 int endc; | |
2362 | |
2363 /* | |
2364 * In a character class, different parsing rules apply. | |
2365 * Not even \ is special anymore, nothing is. | |
2366 */ | |
2367 if (*regparse == '^') /* Complement of range. */ | |
2368 { | |
2369 ret = regnode(ANYBUT + extra); | |
2370 regparse++; | |
2371 } | |
2372 else | |
2373 ret = regnode(ANYOF + extra); | |
2374 | |
2375 /* At the start ']' and '-' mean the literal character. */ | |
2376 if (*regparse == ']' || *regparse == '-') | |
167 | 2377 { |
2378 startc = *regparse; | |
7 | 2379 regc(*regparse++); |
167 | 2380 } |
7 | 2381 |
2382 while (*regparse != NUL && *regparse != ']') | |
2383 { | |
2384 if (*regparse == '-') | |
2385 { | |
2386 ++regparse; | |
2387 /* The '-' is not used for a range at the end and | |
2388 * after or before a '\n'. */ | |
2389 if (*regparse == ']' || *regparse == NUL | |
2390 || startc == -1 | |
2391 || (regparse[0] == '\\' && regparse[1] == 'n')) | |
2392 { | |
2393 regc('-'); | |
2394 startc = '-'; /* [--x] is a range */ | |
2395 } | |
2396 else | |
2397 { | |
167 | 2398 /* Also accept "a-[.z.]" */ |
2399 endc = 0; | |
2400 if (*regparse == '[') | |
2401 endc = get_coll_element(®parse); | |
2402 if (endc == 0) | |
2403 { | |
7 | 2404 #ifdef FEAT_MBYTE |
167 | 2405 if (has_mbyte) |
2406 endc = mb_ptr2char_adv(®parse); | |
2407 else | |
7 | 2408 #endif |
167 | 2409 endc = *regparse++; |
2410 } | |
24 | 2411 |
2412 /* Handle \o40, \x20 and \u20AC style sequences */ | |
167 | 2413 if (endc == '\\' && !cpo_lit && !cpo_bsl) |
24 | 2414 endc = coll_get_char(); |
2415 | |
7 | 2416 if (startc > endc) |
2417 EMSG_RET_NULL(_(e_invrange)); | |
2418 #ifdef FEAT_MBYTE | |
2419 if (has_mbyte && ((*mb_char2len)(startc) > 1 | |
2420 || (*mb_char2len)(endc) > 1)) | |
2421 { | |
2422 /* Limit to a range of 256 chars */ | |
2423 if (endc > startc + 256) | |
2424 EMSG_RET_NULL(_(e_invrange)); | |
2425 while (++startc <= endc) | |
2426 regmbc(startc); | |
2427 } | |
2428 else | |
2429 #endif | |
2430 { | |
2431 #ifdef EBCDIC | |
2432 int alpha_only = FALSE; | |
2433 | |
2434 /* for alphabetical range skip the gaps | |
2435 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */ | |
2436 if (isalpha(startc) && isalpha(endc)) | |
2437 alpha_only = TRUE; | |
2438 #endif | |
2439 while (++startc <= endc) | |
2440 #ifdef EBCDIC | |
2441 if (!alpha_only || isalpha(startc)) | |
2442 #endif | |
2443 regc(startc); | |
2444 } | |
2445 startc = -1; | |
2446 } | |
2447 } | |
2448 /* | |
2449 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim | |
2450 * accepts "\t", "\e", etc., but only when the 'l' flag in | |
2451 * 'cpoptions' is not included. | |
167 | 2452 * Posix doesn't recognize backslash at all. |
7 | 2453 */ |
2454 else if (*regparse == '\\' | |
167 | 2455 && !cpo_bsl |
7 | 2456 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL |
2457 || (!cpo_lit | |
2458 && vim_strchr(REGEXP_ABBR, | |
2459 regparse[1]) != NULL))) | |
2460 { | |
2461 regparse++; | |
2462 if (*regparse == 'n') | |
2463 { | |
2464 /* '\n' in range: also match NL */ | |
2465 if (ret != JUST_CALC_SIZE) | |
2466 { | |
4084 | 2467 /* Using \n inside [^] does not change what |
2468 * matches. "[^\n]" is the same as ".". */ | |
2469 if (*ret == ANYOF) | |
2470 { | |
7 | 2471 *ret = ANYOF + ADD_NL; |
4084 | 2472 *flagp |= HASNL; |
2473 } | |
7 | 2474 /* else: must have had a \n already */ |
2475 } | |
2476 regparse++; | |
2477 startc = -1; | |
2478 } | |
24 | 2479 else if (*regparse == 'd' |
2480 || *regparse == 'o' | |
2481 || *regparse == 'x' | |
2482 || *regparse == 'u' | |
2483 || *regparse == 'U') | |
2484 { | |
2485 startc = coll_get_char(); | |
2486 if (startc == 0) | |
2487 regc(0x0a); | |
2488 else | |
2489 #ifdef FEAT_MBYTE | |
2490 regmbc(startc); | |
2491 #else | |
2492 regc(startc); | |
2493 #endif | |
2494 } | |
7 | 2495 else |
2496 { | |
2497 startc = backslash_trans(*regparse++); | |
2498 regc(startc); | |
2499 } | |
2500 } | |
2501 else if (*regparse == '[') | |
2502 { | |
2503 int c_class; | |
2504 int cu; | |
2505 | |
167 | 2506 c_class = get_char_class(®parse); |
7 | 2507 startc = -1; |
2508 /* Characters assumed to be 8 bits! */ | |
2509 switch (c_class) | |
2510 { | |
2511 case CLASS_NONE: | |
167 | 2512 c_class = get_equi_class(®parse); |
2513 if (c_class != 0) | |
2514 { | |
2515 /* produce equivalence class */ | |
2516 reg_equi_class(c_class); | |
2517 } | |
2518 else if ((c_class = | |
2519 get_coll_element(®parse)) != 0) | |
2520 { | |
2521 /* produce a collating element */ | |
2522 regmbc(c_class); | |
2523 } | |
2524 else | |
2525 { | |
2526 /* literal '[', allow [[-x] as a range */ | |
2527 startc = *regparse++; | |
2528 regc(startc); | |
2529 } | |
7 | 2530 break; |
2531 case CLASS_ALNUM: | |
2532 for (cu = 1; cu <= 255; cu++) | |
2533 if (isalnum(cu)) | |
2534 regc(cu); | |
2535 break; | |
2536 case CLASS_ALPHA: | |
2537 for (cu = 1; cu <= 255; cu++) | |
2538 if (isalpha(cu)) | |
2539 regc(cu); | |
2540 break; | |
2541 case CLASS_BLANK: | |
2542 regc(' '); | |
2543 regc('\t'); | |
2544 break; | |
2545 case CLASS_CNTRL: | |
2546 for (cu = 1; cu <= 255; cu++) | |
2547 if (iscntrl(cu)) | |
2548 regc(cu); | |
2549 break; | |
2550 case CLASS_DIGIT: | |
2551 for (cu = 1; cu <= 255; cu++) | |
2552 if (VIM_ISDIGIT(cu)) | |
2553 regc(cu); | |
2554 break; | |
2555 case CLASS_GRAPH: | |
2556 for (cu = 1; cu <= 255; cu++) | |
2557 if (isgraph(cu)) | |
2558 regc(cu); | |
2559 break; | |
2560 case CLASS_LOWER: | |
2561 for (cu = 1; cu <= 255; cu++) | |
1347 | 2562 if (MB_ISLOWER(cu)) |
7 | 2563 regc(cu); |
2564 break; | |
2565 case CLASS_PRINT: | |
2566 for (cu = 1; cu <= 255; cu++) | |
2567 if (vim_isprintc(cu)) | |
2568 regc(cu); | |
2569 break; | |
2570 case CLASS_PUNCT: | |
2571 for (cu = 1; cu <= 255; cu++) | |
2572 if (ispunct(cu)) | |
2573 regc(cu); | |
2574 break; | |
2575 case CLASS_SPACE: | |
2576 for (cu = 9; cu <= 13; cu++) | |
2577 regc(cu); | |
2578 regc(' '); | |
2579 break; | |
2580 case CLASS_UPPER: | |
2581 for (cu = 1; cu <= 255; cu++) | |
1347 | 2582 if (MB_ISUPPER(cu)) |
7 | 2583 regc(cu); |
2584 break; | |
2585 case CLASS_XDIGIT: | |
2586 for (cu = 1; cu <= 255; cu++) | |
2587 if (vim_isxdigit(cu)) | |
2588 regc(cu); | |
2589 break; | |
2590 case CLASS_TAB: | |
2591 regc('\t'); | |
2592 break; | |
2593 case CLASS_RETURN: | |
2594 regc('\r'); | |
2595 break; | |
2596 case CLASS_BACKSPACE: | |
2597 regc('\b'); | |
2598 break; | |
2599 case CLASS_ESCAPE: | |
2600 regc('\033'); | |
2601 break; | |
2602 } | |
2603 } | |
2604 else | |
2605 { | |
2606 #ifdef FEAT_MBYTE | |
2607 if (has_mbyte) | |
2608 { | |
2609 int len; | |
2610 | |
2611 /* produce a multibyte character, including any | |
2612 * following composing characters */ | |
2613 startc = mb_ptr2char(regparse); | |
474 | 2614 len = (*mb_ptr2len)(regparse); |
7 | 2615 if (enc_utf8 && utf_char2len(startc) != len) |
2616 startc = -1; /* composing chars */ | |
2617 while (--len >= 0) | |
2618 regc(*regparse++); | |
2619 } | |
2620 else | |
2621 #endif | |
2622 { | |
2623 startc = *regparse++; | |
2624 regc(startc); | |
2625 } | |
2626 } | |
2627 } | |
2628 regc(NUL); | |
2629 prevchr_len = 1; /* last char was the ']' */ | |
2630 if (*regparse != ']') | |
2631 EMSG_RET_NULL(_(e_toomsbra)); /* Cannot happen? */ | |
2632 skipchr(); /* let's be friends with the lexer again */ | |
2633 *flagp |= HASWIDTH | SIMPLE; | |
2634 break; | |
2635 } | |
481 | 2636 else if (reg_strict) |
4444 | 2637 EMSG2_RET_NULL(_(e_missingbracket), reg_magic > MAGIC_OFF); |
7 | 2638 } |
2639 /* FALLTHROUGH */ | |
2640 | |
2641 default: | |
2642 { | |
2643 int len; | |
2644 | |
2645 #ifdef FEAT_MBYTE | |
2646 /* A multi-byte character is handled as a separate atom if it's | |
714 | 2647 * before a multi and when it's a composing char. */ |
2648 if (use_multibytecode(c)) | |
7 | 2649 { |
714 | 2650 do_multibyte: |
7 | 2651 ret = regnode(MULTIBYTECODE); |
2652 regmbc(c); | |
2653 *flagp |= HASWIDTH | SIMPLE; | |
2654 break; | |
2655 } | |
2656 #endif | |
2657 | |
2658 ret = regnode(EXACTLY); | |
2659 | |
2660 /* | |
2661 * Append characters as long as: | |
2662 * - there is no following multi, we then need the character in | |
2663 * front of it as a single character operand | |
2664 * - not running into a Magic character | |
2665 * - "one_exactly" is not set | |
2666 * But always emit at least one character. Might be a Multi, | |
2667 * e.g., a "[" without matching "]". | |
2668 */ | |
2669 for (len = 0; c != NUL && (len == 0 | |
2670 || (re_multi_type(peekchr()) == NOT_MULTI | |
2671 && !one_exactly | |
2672 && !is_Magic(c))); ++len) | |
2673 { | |
2674 c = no_Magic(c); | |
2675 #ifdef FEAT_MBYTE | |
2676 if (has_mbyte) | |
2677 { | |
2678 regmbc(c); | |
2679 if (enc_utf8) | |
2680 { | |
2681 int l; | |
2682 | |
714 | 2683 /* Need to get composing character too. */ |
7 | 2684 for (;;) |
2685 { | |
714 | 2686 l = utf_ptr2len(regparse); |
2687 if (!UTF_COMPOSINGLIKE(regparse, regparse + l)) | |
7 | 2688 break; |
714 | 2689 regmbc(utf_ptr2char(regparse)); |
2690 skipchr(); | |
7 | 2691 } |
2692 } | |
2693 } | |
2694 else | |
2695 #endif | |
2696 regc(c); | |
2697 c = getchr(); | |
2698 } | |
2699 ungetchr(); | |
2700 | |
2701 regc(NUL); | |
2702 *flagp |= HASWIDTH; | |
2703 if (len == 1) | |
2704 *flagp |= SIMPLE; | |
2705 } | |
2706 break; | |
2707 } | |
2708 | |
2709 return ret; | |
2710 } | |
2711 | |
714 | 2712 #ifdef FEAT_MBYTE |
2713 /* | |
2714 * Return TRUE if MULTIBYTECODE should be used instead of EXACTLY for | |
2715 * character "c". | |
2716 */ | |
2717 static int | |
2718 use_multibytecode(c) | |
2719 int c; | |
2720 { | |
2721 return has_mbyte && (*mb_char2len)(c) > 1 | |
2722 && (re_multi_type(peekchr()) != NOT_MULTI | |
2723 || (enc_utf8 && utf_iscomposing(c))); | |
2724 } | |
2725 #endif | |
2726 | |
7 | 2727 /* |
4444 | 2728 * Emit a node. |
7 | 2729 * Return pointer to generated code. |
2730 */ | |
2731 static char_u * | |
2732 regnode(op) | |
2733 int op; | |
2734 { | |
2735 char_u *ret; | |
2736 | |
2737 ret = regcode; | |
2738 if (ret == JUST_CALC_SIZE) | |
2739 regsize += 3; | |
2740 else | |
2741 { | |
2742 *regcode++ = op; | |
2743 *regcode++ = NUL; /* Null "next" pointer. */ | |
2744 *regcode++ = NUL; | |
2745 } | |
2746 return ret; | |
2747 } | |
2748 | |
2749 /* | |
2750 * Emit (if appropriate) a byte of code | |
2751 */ | |
2752 static void | |
2753 regc(b) | |
2754 int b; | |
2755 { | |
2756 if (regcode == JUST_CALC_SIZE) | |
2757 regsize++; | |
2758 else | |
2759 *regcode++ = b; | |
2760 } | |
2761 | |
2762 #ifdef FEAT_MBYTE | |
2763 /* | |
2764 * Emit (if appropriate) a multi-byte character of code | |
2765 */ | |
2766 static void | |
2767 regmbc(c) | |
2768 int c; | |
2769 { | |
2974 | 2770 if (!has_mbyte && c > 0xff) |
2771 return; | |
7 | 2772 if (regcode == JUST_CALC_SIZE) |
2773 regsize += (*mb_char2len)(c); | |
2774 else | |
2775 regcode += (*mb_char2bytes)(c, regcode); | |
2776 } | |
2777 #endif | |
2778 | |
2779 /* | |
4444 | 2780 * Insert an operator in front of already-emitted operand |
7 | 2781 * |
2782 * Means relocating the operand. | |
2783 */ | |
2784 static void | |
2785 reginsert(op, opnd) | |
2786 int op; | |
2787 char_u *opnd; | |
2788 { | |
2789 char_u *src; | |
2790 char_u *dst; | |
2791 char_u *place; | |
2792 | |
2793 if (regcode == JUST_CALC_SIZE) | |
2794 { | |
2795 regsize += 3; | |
2796 return; | |
2797 } | |
2798 src = regcode; | |
2799 regcode += 3; | |
2800 dst = regcode; | |
2801 while (src > opnd) | |
2802 *--dst = *--src; | |
2803 | |
2804 place = opnd; /* Op node, where operand used to be. */ | |
2805 *place++ = op; | |
2806 *place++ = NUL; | |
2807 *place = NUL; | |
2808 } | |
2809 | |
2810 /* | |
4444 | 2811 * 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
|
2812 * Add a number to the operator. |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2813 */ |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2814 static void |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2815 reginsert_nr(op, val, opnd) |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2816 int op; |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2817 long val; |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2818 char_u *opnd; |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2819 { |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2820 char_u *src; |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2821 char_u *dst; |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2822 char_u *place; |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2823 |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2824 if (regcode == JUST_CALC_SIZE) |
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 regsize += 7; |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2827 return; |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2828 } |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2829 src = regcode; |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2830 regcode += 7; |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2831 dst = regcode; |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2832 while (src > opnd) |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2833 *--dst = *--src; |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2834 |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2835 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
|
2836 *place++ = op; |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2837 *place++ = NUL; |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2838 *place++ = NUL; |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2839 place = re_put_long(place, (long_u)val); |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2840 } |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
2841 |
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 * Insert an operator in front of already-emitted operand. |
7 | 2844 * The operator has the given limit values as operands. Also set next pointer. |
2845 * | |
2846 * Means relocating the operand. | |
2847 */ | |
2848 static void | |
2849 reginsert_limits(op, minval, maxval, opnd) | |
2850 int op; | |
2851 long minval; | |
2852 long maxval; | |
2853 char_u *opnd; | |
2854 { | |
2855 char_u *src; | |
2856 char_u *dst; | |
2857 char_u *place; | |
2858 | |
2859 if (regcode == JUST_CALC_SIZE) | |
2860 { | |
2861 regsize += 11; | |
2862 return; | |
2863 } | |
2864 src = regcode; | |
2865 regcode += 11; | |
2866 dst = regcode; | |
2867 while (src > opnd) | |
2868 *--dst = *--src; | |
2869 | |
2870 place = opnd; /* Op node, where operand used to be. */ | |
2871 *place++ = op; | |
2872 *place++ = NUL; | |
2873 *place++ = NUL; | |
2874 place = re_put_long(place, (long_u)minval); | |
2875 place = re_put_long(place, (long_u)maxval); | |
2876 regtail(opnd, place); | |
2877 } | |
2878 | |
2879 /* | |
2880 * Write a long as four bytes at "p" and return pointer to the next char. | |
2881 */ | |
2882 static char_u * | |
2883 re_put_long(p, val) | |
2884 char_u *p; | |
2885 long_u val; | |
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 | |
2898 regtail(p, val) | |
2899 char_u *p; | |
2900 char_u *val; | |
2901 { | |
2902 char_u *scan; | |
2903 char_u *temp; | |
2904 int offset; | |
2905 | |
2906 if (p == JUST_CALC_SIZE) | |
2907 return; | |
2908 | |
2909 /* Find last node. */ | |
2910 scan = p; | |
2911 for (;;) | |
2912 { | |
2913 temp = regnext(scan); | |
2914 if (temp == NULL) | |
2915 break; | |
2916 scan = temp; | |
2917 } | |
2918 | |
233 | 2919 if (OP(scan) == BACK) |
7 | 2920 offset = (int)(scan - val); |
2921 else | |
2922 offset = (int)(val - scan); | |
2010 | 2923 /* When the offset uses more than 16 bits it can no longer fit in the two |
2974 | 2924 * bytes available. Use a global flag to avoid having to check return |
2010 | 2925 * values in too many places. */ |
2926 if (offset > 0xffff) | |
2927 reg_toolong = TRUE; | |
2928 else | |
2929 { | |
2930 *(scan + 1) = (char_u) (((unsigned)offset >> 8) & 0377); | |
2931 *(scan + 2) = (char_u) (offset & 0377); | |
2932 } | |
7 | 2933 } |
2934 | |
2935 /* | |
4444 | 2936 * Like regtail, on item after a BRANCH; nop if none. |
7 | 2937 */ |
2938 static void | |
2939 regoptail(p, val) | |
2940 char_u *p; | |
2941 char_u *val; | |
2942 { | |
2943 /* When op is neither BRANCH nor BRACE_COMPLEX0-9, it is "operandless" */ | |
2944 if (p == NULL || p == JUST_CALC_SIZE | |
2945 || (OP(p) != BRANCH | |
2946 && (OP(p) < BRACE_COMPLEX || OP(p) > BRACE_COMPLEX + 9))) | |
2947 return; | |
2948 regtail(OPERAND(p), val); | |
2949 } | |
2950 | |
2951 /* | |
4444 | 2952 * Functions for getting characters from the regexp input. |
7 | 2953 */ |
2954 | |
2955 static int at_start; /* True when on the first character */ | |
2956 static int prev_at_start; /* True when on the second character */ | |
2957 | |
4444 | 2958 /* |
2959 * Start parsing at "str". | |
2960 */ | |
7 | 2961 static void |
2962 initchr(str) | |
2963 char_u *str; | |
2964 { | |
2965 regparse = str; | |
2966 prevchr_len = 0; | |
2967 curchr = prevprevchr = prevchr = nextchr = -1; | |
2968 at_start = TRUE; | |
2969 prev_at_start = FALSE; | |
2970 } | |
2971 | |
4444 | 2972 /* |
4679
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2973 * 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
|
2974 * starts in the same state again. |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2975 */ |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2976 static void |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2977 save_parse_state(ps) |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2978 parse_state_T *ps; |
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 ps->regparse = regparse; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2981 ps->prevchr_len = prevchr_len; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2982 ps->curchr = curchr; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2983 ps->prevchr = prevchr; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2984 ps->prevprevchr = prevprevchr; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2985 ps->nextchr = nextchr; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2986 ps->at_start = at_start; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2987 ps->prev_at_start = prev_at_start; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2988 ps->regnpar = regnpar; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2989 } |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2990 |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2991 /* |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2992 * Restore a previously saved parse state. |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2993 */ |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2994 static void |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2995 restore_parse_state(ps) |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2996 parse_state_T *ps; |
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 regparse = ps->regparse; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
2999 prevchr_len = ps->prevchr_len; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
3000 curchr = ps->curchr; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
3001 prevchr = ps->prevchr; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
3002 prevprevchr = ps->prevprevchr; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
3003 nextchr = ps->nextchr; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
3004 at_start = ps->at_start; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
3005 prev_at_start = ps->prev_at_start; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
3006 regnpar = ps->regnpar; |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
3007 } |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
3008 |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
3009 |
4d92b873acef
updated for version 7.3.1087
Bram Moolenaar <bram@vim.org>
parents:
4579
diff
changeset
|
3010 /* |
4444 | 3011 * Get the next character without advancing. |
3012 */ | |
7 | 3013 static int |
3014 peekchr() | |
3015 { | |
167 | 3016 static int after_slash = FALSE; |
3017 | |
7 | 3018 if (curchr == -1) |
3019 { | |
3020 switch (curchr = regparse[0]) | |
3021 { | |
3022 case '.': | |
3023 case '[': | |
3024 case '~': | |
3025 /* magic when 'magic' is on */ | |
3026 if (reg_magic >= MAGIC_ON) | |
3027 curchr = Magic(curchr); | |
3028 break; | |
3029 case '(': | |
3030 case ')': | |
3031 case '{': | |
3032 case '%': | |
3033 case '+': | |
3034 case '=': | |
3035 case '?': | |
3036 case '@': | |
3037 case '!': | |
3038 case '&': | |
3039 case '|': | |
3040 case '<': | |
3041 case '>': | |
3042 case '#': /* future ext. */ | |
3043 case '"': /* future ext. */ | |
3044 case '\'': /* future ext. */ | |
3045 case ',': /* future ext. */ | |
3046 case '-': /* future ext. */ | |
3047 case ':': /* future ext. */ | |
3048 case ';': /* future ext. */ | |
3049 case '`': /* future ext. */ | |
3050 case '/': /* Can't be used in / command */ | |
3051 /* magic only after "\v" */ | |
3052 if (reg_magic == MAGIC_ALL) | |
3053 curchr = Magic(curchr); | |
3054 break; | |
3055 case '*': | |
167 | 3056 /* * is not magic as the very first character, eg "?*ptr", when |
3057 * after '^', eg "/^*ptr" and when after "\(", "\|", "\&". But | |
3058 * "\(\*" is not magic, thus must be magic if "after_slash" */ | |
3059 if (reg_magic >= MAGIC_ON | |
3060 && !at_start | |
3061 && !(prev_at_start && prevchr == Magic('^')) | |
3062 && (after_slash | |
3063 || (prevchr != Magic('(') | |
3064 && prevchr != Magic('&') | |
3065 && prevchr != Magic('|')))) | |
7 | 3066 curchr = Magic('*'); |
3067 break; | |
3068 case '^': | |
3069 /* '^' is only magic as the very first character and if it's after | |
3070 * "\(", "\|", "\&' or "\n" */ | |
3071 if (reg_magic >= MAGIC_OFF | |
3072 && (at_start | |
3073 || reg_magic == MAGIC_ALL | |
3074 || prevchr == Magic('(') | |
3075 || prevchr == Magic('|') | |
3076 || prevchr == Magic('&') | |
3077 || prevchr == Magic('n') | |
3078 || (no_Magic(prevchr) == '(' | |
3079 && prevprevchr == Magic('%')))) | |
3080 { | |
3081 curchr = Magic('^'); | |
3082 at_start = TRUE; | |
3083 prev_at_start = FALSE; | |
3084 } | |
3085 break; | |
3086 case '$': | |
3087 /* '$' is only magic as the very last char and if it's in front of | |
3088 * either "\|", "\)", "\&", or "\n" */ | |
3089 if (reg_magic >= MAGIC_OFF) | |
3090 { | |
3091 char_u *p = regparse + 1; | |
3092 | |
3093 /* ignore \c \C \m and \M after '$' */ | |
3094 while (p[0] == '\\' && (p[1] == 'c' || p[1] == 'C' | |
3095 || p[1] == 'm' || p[1] == 'M' || p[1] == 'Z')) | |
3096 p += 2; | |
3097 if (p[0] == NUL | |
3098 || (p[0] == '\\' | |
3099 && (p[1] == '|' || p[1] == '&' || p[1] == ')' | |
3100 || p[1] == 'n')) | |
3101 || reg_magic == MAGIC_ALL) | |
3102 curchr = Magic('$'); | |
3103 } | |
3104 break; | |
3105 case '\\': | |
3106 { | |
3107 int c = regparse[1]; | |
3108 | |
3109 if (c == NUL) | |
3110 curchr = '\\'; /* trailing '\' */ | |
3111 else if ( | |
3112 #ifdef EBCDIC | |
3113 vim_strchr(META, c) | |
3114 #else | |
3115 c <= '~' && META_flags[c] | |
3116 #endif | |
3117 ) | |
3118 { | |
3119 /* | |
3120 * META contains everything that may be magic sometimes, | |
3121 * except ^ and $ ("\^" and "\$" are only magic after | |
3122 * "\v"). We now fetch the next character and toggle its | |
3123 * magicness. Therefore, \ is so meta-magic that it is | |
3124 * not in META. | |
3125 */ | |
3126 curchr = -1; | |
3127 prev_at_start = at_start; | |
3128 at_start = FALSE; /* be able to say "/\*ptr" */ | |
3129 ++regparse; | |
167 | 3130 ++after_slash; |
7 | 3131 peekchr(); |
3132 --regparse; | |
167 | 3133 --after_slash; |
7 | 3134 curchr = toggle_Magic(curchr); |
3135 } | |
3136 else if (vim_strchr(REGEXP_ABBR, c)) | |
3137 { | |
3138 /* | |
3139 * Handle abbreviations, like "\t" for TAB -- webb | |
3140 */ | |
3141 curchr = backslash_trans(c); | |
3142 } | |
3143 else if (reg_magic == MAGIC_NONE && (c == '$' || c == '^')) | |
3144 curchr = toggle_Magic(c); | |
3145 else | |
3146 { | |
3147 /* | |
3148 * Next character can never be (made) magic? | |
3149 * Then backslashing it won't do anything. | |
3150 */ | |
3151 #ifdef FEAT_MBYTE | |
3152 if (has_mbyte) | |
3153 curchr = (*mb_ptr2char)(regparse + 1); | |
3154 else | |
3155 #endif | |
3156 curchr = c; | |
3157 } | |
3158 break; | |
3159 } | |
3160 | |
3161 #ifdef FEAT_MBYTE | |
3162 default: | |
3163 if (has_mbyte) | |
3164 curchr = (*mb_ptr2char)(regparse); | |
3165 #endif | |
3166 } | |
3167 } | |
3168 | |
3169 return curchr; | |
3170 } | |
3171 | |
3172 /* | |
3173 * Eat one lexed character. Do this in a way that we can undo it. | |
3174 */ | |
3175 static void | |
3176 skipchr() | |
3177 { | |
3178 /* peekchr() eats a backslash, do the same here */ | |
3179 if (*regparse == '\\') | |
3180 prevchr_len = 1; | |
3181 else | |
3182 prevchr_len = 0; | |
3183 if (regparse[prevchr_len] != NUL) | |
3184 { | |
3185 #ifdef FEAT_MBYTE | |
714 | 3186 if (enc_utf8) |
1449 | 3187 /* exclude composing chars that mb_ptr2len does include */ |
3188 prevchr_len += utf_ptr2len(regparse + prevchr_len); | |
714 | 3189 else if (has_mbyte) |
474 | 3190 prevchr_len += (*mb_ptr2len)(regparse + prevchr_len); |
7 | 3191 else |
3192 #endif | |
3193 ++prevchr_len; | |
3194 } | |
3195 regparse += prevchr_len; | |
3196 prev_at_start = at_start; | |
3197 at_start = FALSE; | |
3198 prevprevchr = prevchr; | |
3199 prevchr = curchr; | |
3200 curchr = nextchr; /* use previously unget char, or -1 */ | |
3201 nextchr = -1; | |
3202 } | |
3203 | |
3204 /* | |
3205 * Skip a character while keeping the value of prev_at_start for at_start. | |
3206 * prevchr and prevprevchr are also kept. | |
3207 */ | |
3208 static void | |
3209 skipchr_keepstart() | |
3210 { | |
3211 int as = prev_at_start; | |
3212 int pr = prevchr; | |
3213 int prpr = prevprevchr; | |
3214 | |
3215 skipchr(); | |
3216 at_start = as; | |
3217 prevchr = pr; | |
3218 prevprevchr = prpr; | |
3219 } | |
3220 | |
4444 | 3221 /* |
3222 * Get the next character from the pattern. We know about magic and such, so | |
3223 * therefore we need a lexical analyzer. | |
3224 */ | |
7 | 3225 static int |
3226 getchr() | |
3227 { | |
3228 int chr = peekchr(); | |
3229 | |
3230 skipchr(); | |
3231 return chr; | |
3232 } | |
3233 | |
3234 /* | |
3235 * put character back. Works only once! | |
3236 */ | |
3237 static void | |
3238 ungetchr() | |
3239 { | |
3240 nextchr = curchr; | |
3241 curchr = prevchr; | |
3242 prevchr = prevprevchr; | |
3243 at_start = prev_at_start; | |
3244 prev_at_start = FALSE; | |
3245 | |
3246 /* Backup regparse, so that it's at the same position as before the | |
3247 * getchr(). */ | |
3248 regparse -= prevchr_len; | |
3249 } | |
3250 | |
3251 /* | |
29 | 3252 * Get and return the value of the hex string at the current position. |
3253 * Return -1 if there is no valid hex number. | |
3254 * The position is updated: | |
24 | 3255 * blahblah\%x20asdf |
856 | 3256 * before-^ ^-after |
24 | 3257 * The parameter controls the maximum number of input characters. This will be |
3258 * 2 when reading a \%x20 sequence and 4 when reading a \%u20AC sequence. | |
3259 */ | |
3260 static int | |
3261 gethexchrs(maxinputlen) | |
3262 int maxinputlen; | |
3263 { | |
3264 int nr = 0; | |
3265 int c; | |
3266 int i; | |
3267 | |
3268 for (i = 0; i < maxinputlen; ++i) | |
3269 { | |
3270 c = regparse[0]; | |
3271 if (!vim_isxdigit(c)) | |
3272 break; | |
3273 nr <<= 4; | |
3274 nr |= hex2nr(c); | |
3275 ++regparse; | |
3276 } | |
3277 | |
3278 if (i == 0) | |
3279 return -1; | |
3280 return nr; | |
3281 } | |
3282 | |
3283 /* | |
4579
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
3284 * Get and return the value of the decimal string immediately after the |
24 | 3285 * current position. Return -1 for invalid. Consumes all digits. |
3286 */ | |
3287 static int | |
3288 getdecchrs() | |
3289 { | |
3290 int nr = 0; | |
3291 int c; | |
3292 int i; | |
3293 | |
3294 for (i = 0; ; ++i) | |
3295 { | |
3296 c = regparse[0]; | |
3297 if (c < '0' || c > '9') | |
3298 break; | |
3299 nr *= 10; | |
3300 nr += c - '0'; | |
3301 ++regparse; | |
4579
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
3302 curchr = -1; /* no longer valid */ |
24 | 3303 } |
3304 | |
3305 if (i == 0) | |
3306 return -1; | |
3307 return nr; | |
3308 } | |
3309 | |
3310 /* | |
3311 * get and return the value of the octal string immediately after the current | |
3312 * position. Return -1 for invalid, or 0-255 for valid. Smart enough to handle | |
3313 * numbers > 377 correctly (for example, 400 is treated as 40) and doesn't | |
3314 * treat 8 or 9 as recognised characters. Position is updated: | |
3315 * blahblah\%o210asdf | |
856 | 3316 * before-^ ^-after |
24 | 3317 */ |
3318 static int | |
3319 getoctchrs() | |
3320 { | |
3321 int nr = 0; | |
3322 int c; | |
3323 int i; | |
3324 | |
3325 for (i = 0; i < 3 && nr < 040; ++i) | |
3326 { | |
3327 c = regparse[0]; | |
3328 if (c < '0' || c > '7') | |
3329 break; | |
3330 nr <<= 3; | |
3331 nr |= hex2nr(c); | |
3332 ++regparse; | |
3333 } | |
3334 | |
3335 if (i == 0) | |
3336 return -1; | |
3337 return nr; | |
3338 } | |
3339 | |
3340 /* | |
3341 * Get a number after a backslash that is inside []. | |
3342 * When nothing is recognized return a backslash. | |
3343 */ | |
3344 static int | |
3345 coll_get_char() | |
3346 { | |
3347 int nr = -1; | |
3348 | |
3349 switch (*regparse++) | |
3350 { | |
3351 case 'd': nr = getdecchrs(); break; | |
3352 case 'o': nr = getoctchrs(); break; | |
3353 case 'x': nr = gethexchrs(2); break; | |
3354 case 'u': nr = gethexchrs(4); break; | |
3355 case 'U': nr = gethexchrs(8); break; | |
3356 } | |
3357 if (nr < 0) | |
3358 { | |
3359 /* If getting the number fails be backwards compatible: the character | |
3360 * is a backslash. */ | |
3361 --regparse; | |
3362 nr = '\\'; | |
3363 } | |
3364 return nr; | |
3365 } | |
3366 | |
3367 /* | |
7 | 3368 * read_limits - Read two integers to be taken as a minimum and maximum. |
3369 * If the first character is '-', then the range is reversed. | |
3370 * Should end with 'end'. If minval is missing, zero is default, if maxval is | |
3371 * missing, a very big number is the default. | |
3372 */ | |
3373 static int | |
3374 read_limits(minval, maxval) | |
3375 long *minval; | |
3376 long *maxval; | |
3377 { | |
3378 int reverse = FALSE; | |
3379 char_u *first_char; | |
3380 long tmp; | |
3381 | |
3382 if (*regparse == '-') | |
3383 { | |
3384 /* Starts with '-', so reverse the range later */ | |
3385 regparse++; | |
3386 reverse = TRUE; | |
3387 } | |
3388 first_char = regparse; | |
3389 *minval = getdigits(®parse); | |
3390 if (*regparse == ',') /* There is a comma */ | |
3391 { | |
3392 if (vim_isdigit(*++regparse)) | |
3393 *maxval = getdigits(®parse); | |
3394 else | |
3395 *maxval = MAX_LIMIT; | |
3396 } | |
3397 else if (VIM_ISDIGIT(*first_char)) | |
3398 *maxval = *minval; /* It was \{n} or \{-n} */ | |
3399 else | |
3400 *maxval = MAX_LIMIT; /* It was \{} or \{-} */ | |
3401 if (*regparse == '\\') | |
3402 regparse++; /* Allow either \{...} or \{...\} */ | |
167 | 3403 if (*regparse != '}') |
7 | 3404 { |
3405 sprintf((char *)IObuff, _("E554: Syntax error in %s{...}"), | |
3406 reg_magic == MAGIC_ALL ? "" : "\\"); | |
3407 EMSG_RET_FAIL(IObuff); | |
3408 } | |
3409 | |
3410 /* | |
3411 * Reverse the range if there was a '-', or make sure it is in the right | |
3412 * order otherwise. | |
3413 */ | |
3414 if ((!reverse && *minval > *maxval) || (reverse && *minval < *maxval)) | |
3415 { | |
3416 tmp = *minval; | |
3417 *minval = *maxval; | |
3418 *maxval = tmp; | |
3419 } | |
3420 skipchr(); /* let's be friends with the lexer again */ | |
3421 return OK; | |
3422 } | |
3423 | |
3424 /* | |
3425 * vim_regexec and friends | |
3426 */ | |
3427 | |
3428 /* | |
3429 * Global work variables for vim_regexec(). | |
3430 */ | |
3431 | |
3432 /* The current match-position is remembered with these variables: */ | |
3433 static linenr_T reglnum; /* line number, relative to first line */ | |
3434 static char_u *regline; /* start of current line */ | |
3435 static char_u *reginput; /* current input, points into "regline" */ | |
3436 | |
3437 static int need_clear_subexpr; /* subexpressions still need to be | |
3438 * cleared */ | |
3439 #ifdef FEAT_SYN_HL | |
3440 static int need_clear_zsubexpr = FALSE; /* extmatch subexpressions | |
3441 * still need to be cleared */ | |
3442 #endif | |
3443 | |
3444 /* | |
3445 * Structure used to save the current input state, when it needs to be | |
3446 * restored after trying a match. Used by reg_save() and reg_restore(). | |
233 | 3447 * Also stores the length of "backpos". |
7 | 3448 */ |
3449 typedef struct | |
3450 { | |
3451 union | |
3452 { | |
3453 char_u *ptr; /* reginput pointer, for single-line regexp */ | |
3454 lpos_T pos; /* reginput pos, for multi-line regexp */ | |
3455 } rs_u; | |
233 | 3456 int rs_len; |
7 | 3457 } regsave_T; |
3458 | |
3459 /* struct to save start/end pointer/position in for \(\) */ | |
3460 typedef struct | |
3461 { | |
3462 union | |
3463 { | |
3464 char_u *ptr; | |
3465 lpos_T pos; | |
3466 } se_u; | |
3467 } save_se_T; | |
3468 | |
1579 | 3469 /* used for BEHIND and NOBEHIND matching */ |
3470 typedef struct regbehind_S | |
3471 { | |
3472 regsave_T save_after; | |
3473 regsave_T save_behind; | |
1602 | 3474 int save_need_clear_subexpr; |
1579 | 3475 save_se_T save_start[NSUBEXP]; |
3476 save_se_T save_end[NSUBEXP]; | |
3477 } regbehind_T; | |
3478 | |
7 | 3479 static char_u *reg_getline __ARGS((linenr_T lnum)); |
4444 | 3480 static long bt_regexec_both __ARGS((char_u *line, colnr_T col, proftime_T *tm)); |
3481 static long regtry __ARGS((bt_regprog_T *prog, colnr_T col)); | |
7 | 3482 static void cleanup_subexpr __ARGS((void)); |
3483 #ifdef FEAT_SYN_HL | |
3484 static void cleanup_zsubexpr __ARGS((void)); | |
3485 #endif | |
1579 | 3486 static void save_subexpr __ARGS((regbehind_T *bp)); |
3487 static void restore_subexpr __ARGS((regbehind_T *bp)); | |
7 | 3488 static void reg_nextline __ARGS((void)); |
233 | 3489 static void reg_save __ARGS((regsave_T *save, garray_T *gap)); |
3490 static void reg_restore __ARGS((regsave_T *save, garray_T *gap)); | |
7 | 3491 static int reg_save_equal __ARGS((regsave_T *save)); |
3492 static void save_se_multi __ARGS((save_se_T *savep, lpos_T *posp)); | |
3493 static void save_se_one __ARGS((save_se_T *savep, char_u **pp)); | |
3494 | |
3495 /* Save the sub-expressions before attempting a match. */ | |
3496 #define save_se(savep, posp, pp) \ | |
3497 REG_MULTI ? save_se_multi((savep), (posp)) : save_se_one((savep), (pp)) | |
3498 | |
3499 /* After a failed match restore the sub-expressions. */ | |
3500 #define restore_se(savep, posp, pp) { \ | |
3501 if (REG_MULTI) \ | |
3502 *(posp) = (savep)->se_u.pos; \ | |
3503 else \ | |
3504 *(pp) = (savep)->se_u.ptr; } | |
3505 | |
3506 static int re_num_cmp __ARGS((long_u val, char_u *scan)); | |
180 | 3507 static int regmatch __ARGS((char_u *prog)); |
7 | 3508 static int regrepeat __ARGS((char_u *p, long maxcount)); |
3509 | |
3510 #ifdef DEBUG | |
3511 int regnarrate = 0; | |
3512 #endif | |
3513 | |
3514 /* | |
3515 * Internal copy of 'ignorecase'. It is set at each call to vim_regexec(). | |
3516 * Normally it gets the value of "rm_ic" or "rmm_ic", but when the pattern | |
3517 * contains '\c' or '\C' the value is overruled. | |
3518 */ | |
3519 static int ireg_ic; | |
3520 | |
3521 #ifdef FEAT_MBYTE | |
3522 /* | |
3523 * Similar to ireg_ic, but only for 'combining' characters. Set with \Z flag | |
3524 * in the regexp. Defaults to false, always. | |
3525 */ | |
3526 static int ireg_icombine; | |
3527 #endif | |
3528 | |
3529 /* | |
410 | 3530 * Copy of "rmm_maxcol": maximum column to search for a match. Zero when |
3531 * there is no maximum. | |
3532 */ | |
418 | 3533 static colnr_T ireg_maxcol; |
410 | 3534 |
3535 /* | |
7 | 3536 * Sometimes need to save a copy of a line. Since alloc()/free() is very |
3537 * slow, we keep one allocated piece of memory and only re-allocate it when | |
4444 | 3538 * it's too small. It's freed in bt_regexec_both() when finished. |
7 | 3539 */ |
1468 | 3540 static char_u *reg_tofree = NULL; |
7 | 3541 static unsigned reg_tofreelen; |
3542 | |
3543 /* | |
3544 * These variables are set when executing a regexp to speed up the execution. | |
1209 | 3545 * Which ones are set depends on whether a single-line or multi-line match is |
7 | 3546 * done: |
3547 * single-line multi-line | |
3548 * reg_match ®match_T NULL | |
3549 * reg_mmatch NULL ®mmatch_T | |
3550 * reg_startp reg_match->startp <invalid> | |
3551 * reg_endp reg_match->endp <invalid> | |
3552 * reg_startpos <invalid> reg_mmatch->startpos | |
3553 * reg_endpos <invalid> reg_mmatch->endpos | |
3554 * reg_win NULL window in which to search | |
4061 | 3555 * reg_buf curbuf buffer in which to search |
7 | 3556 * reg_firstlnum <invalid> first line in which to search |
3557 * reg_maxline 0 last line nr | |
3558 * reg_line_lbr FALSE or TRUE FALSE | |
3559 */ | |
3560 static regmatch_T *reg_match; | |
3561 static regmmatch_T *reg_mmatch; | |
3562 static char_u **reg_startp = NULL; | |
3563 static char_u **reg_endp = NULL; | |
3564 static lpos_T *reg_startpos = NULL; | |
3565 static lpos_T *reg_endpos = NULL; | |
3566 static win_T *reg_win; | |
3567 static buf_T *reg_buf; | |
3568 static linenr_T reg_firstlnum; | |
3569 static linenr_T reg_maxline; | |
3570 static int reg_line_lbr; /* "\n" in string is line break */ | |
3571 | |
270 | 3572 /* Values for rs_state in regitem_T. */ |
3573 typedef enum regstate_E | |
3574 { | |
3575 RS_NOPEN = 0 /* NOPEN and NCLOSE */ | |
3576 , RS_MOPEN /* MOPEN + [0-9] */ | |
3577 , RS_MCLOSE /* MCLOSE + [0-9] */ | |
3578 #ifdef FEAT_SYN_HL | |
3579 , RS_ZOPEN /* ZOPEN + [0-9] */ | |
3580 , RS_ZCLOSE /* ZCLOSE + [0-9] */ | |
3581 #endif | |
3582 , RS_BRANCH /* BRANCH */ | |
3583 , RS_BRCPLX_MORE /* BRACE_COMPLEX and trying one more match */ | |
3584 , RS_BRCPLX_LONG /* BRACE_COMPLEX and trying longest match */ | |
3585 , RS_BRCPLX_SHORT /* BRACE_COMPLEX and trying shortest match */ | |
3586 , RS_NOMATCH /* NOMATCH */ | |
3587 , RS_BEHIND1 /* BEHIND / NOBEHIND matching rest */ | |
3588 , RS_BEHIND2 /* BEHIND / NOBEHIND matching behind part */ | |
3589 , RS_STAR_LONG /* STAR/PLUS/BRACE_SIMPLE longest match */ | |
3590 , RS_STAR_SHORT /* STAR/PLUS/BRACE_SIMPLE shortest match */ | |
3591 } regstate_T; | |
3592 | |
3593 /* | |
3594 * When there are alternatives a regstate_T is put on the regstack to remember | |
3595 * what we are doing. | |
3596 * Before it may be another type of item, depending on rs_state, to remember | |
3597 * more things. | |
3598 */ | |
3599 typedef struct regitem_S | |
3600 { | |
3601 regstate_T rs_state; /* what we are doing, one of RS_ above */ | |
3602 char_u *rs_scan; /* current node in program */ | |
3603 union | |
3604 { | |
3605 save_se_T sesave; | |
3606 regsave_T regsave; | |
3607 } rs_un; /* room for saving reginput */ | |
1579 | 3608 short rs_no; /* submatch nr or BEHIND/NOBEHIND */ |
270 | 3609 } regitem_T; |
3610 | |
3611 static regitem_T *regstack_push __ARGS((regstate_T state, char_u *scan)); | |
3612 static void regstack_pop __ARGS((char_u **scan)); | |
3613 | |
3614 /* used for STAR, PLUS and BRACE_SIMPLE matching */ | |
3615 typedef struct regstar_S | |
3616 { | |
3617 int nextb; /* next byte */ | |
3618 int nextb_ic; /* next byte reverse case */ | |
3619 long count; | |
3620 long minval; | |
3621 long maxval; | |
3622 } regstar_T; | |
3623 | |
3624 /* used to store input position when a BACK was encountered, so that we now if | |
3625 * we made any progress since the last time. */ | |
3626 typedef struct backpos_S | |
3627 { | |
3628 char_u *bp_scan; /* "scan" where BACK was encountered */ | |
3629 regsave_T bp_pos; /* last input position */ | |
3630 } backpos_T; | |
3631 | |
3632 /* | |
1520 | 3633 * "regstack" and "backpos" are used by regmatch(). They are kept over calls |
3634 * to avoid invoking malloc() and free() often. | |
3635 * "regstack" is a stack with regitem_T items, sometimes preceded by regstar_T | |
3636 * or regbehind_T. | |
3637 * "backpos_T" is a table with backpos_T for BACK | |
270 | 3638 */ |
1520 | 3639 static garray_T regstack = {0, 0, 0, 0, NULL}; |
3640 static garray_T backpos = {0, 0, 0, 0, NULL}; | |
3641 | |
3642 /* | |
3643 * Both for regstack and backpos tables we use the following strategy of | |
3644 * allocation (to reduce malloc/free calls): | |
3645 * - Initial size is fairly small. | |
3646 * - When needed, the tables are grown bigger (8 times at first, double after | |
3647 * that). | |
3648 * - After executing the match we free the memory only if the array has grown. | |
3649 * Thus the memory is kept allocated when it's at the initial size. | |
3650 * This makes it fast while not keeping a lot of memory allocated. | |
3651 * A three times speed increase was observed when using many simple patterns. | |
3652 */ | |
3653 #define REGSTACK_INITIAL 2048 | |
3654 #define BACKPOS_INITIAL 64 | |
3655 | |
3656 #if defined(EXITFREE) || defined(PROTO) | |
3657 void | |
3658 free_regexp_stuff() | |
3659 { | |
3660 ga_clear(®stack); | |
3661 ga_clear(&backpos); | |
3662 vim_free(reg_tofree); | |
3663 vim_free(reg_prev_sub); | |
3664 } | |
3665 #endif | |
270 | 3666 |
7 | 3667 /* |
3668 * Get pointer to the line "lnum", which is relative to "reg_firstlnum". | |
3669 */ | |
3670 static char_u * | |
3671 reg_getline(lnum) | |
3672 linenr_T lnum; | |
3673 { | |
3674 /* when looking behind for a match/no-match lnum is negative. But we | |
3675 * can't go before line 1 */ | |
3676 if (reg_firstlnum + lnum < 1) | |
3677 return NULL; | |
482 | 3678 if (lnum > reg_maxline) |
481 | 3679 /* Must have matched the "\n" in the last line. */ |
3680 return (char_u *)""; | |
7 | 3681 return ml_get_buf(reg_buf, reg_firstlnum + lnum, FALSE); |
3682 } | |
3683 | |
3684 static regsave_T behind_pos; | |
3685 | |
3686 #ifdef FEAT_SYN_HL | |
3687 static char_u *reg_startzp[NSUBEXP]; /* Workspace to mark beginning */ | |
3688 static char_u *reg_endzp[NSUBEXP]; /* and end of \z(...\) matches */ | |
3689 static lpos_T reg_startzpos[NSUBEXP]; /* idem, beginning pos */ | |
3690 static lpos_T reg_endzpos[NSUBEXP]; /* idem, end pos */ | |
3691 #endif | |
3692 | |
3693 /* TRUE if using multi-line regexp. */ | |
3694 #define REG_MULTI (reg_match == NULL) | |
3695 | |
4444 | 3696 static int bt_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col)); |
3697 | |
7 | 3698 /* |
3699 * Match a regexp against a string. | |
3700 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp(). | |
3701 * Uses curbuf for line count and 'iskeyword'. | |
3702 * | |
3703 * Return TRUE if there is a match, FALSE if not. | |
3704 */ | |
4444 | 3705 static int |
3706 bt_regexec(rmp, line, col) | |
7 | 3707 regmatch_T *rmp; |
3708 char_u *line; /* string to match against */ | |
3709 colnr_T col; /* column to start looking for match */ | |
3710 { | |
3711 reg_match = rmp; | |
3712 reg_mmatch = NULL; | |
3713 reg_maxline = 0; | |
3714 reg_line_lbr = FALSE; | |
4061 | 3715 reg_buf = curbuf; |
7 | 3716 reg_win = NULL; |
3717 ireg_ic = rmp->rm_ic; | |
3718 #ifdef FEAT_MBYTE | |
3719 ireg_icombine = FALSE; | |
3720 #endif | |
410 | 3721 ireg_maxcol = 0; |
4444 | 3722 return (bt_regexec_both(line, col, NULL) != 0); |
7 | 3723 } |
3724 | |
39 | 3725 #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \ |
3726 || defined(FIND_REPLACE_DIALOG) || defined(PROTO) | |
4444 | 3727 |
3728 static int bt_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col)); | |
3729 | |
7 | 3730 /* |
3731 * Like vim_regexec(), but consider a "\n" in "line" to be a line break. | |
3732 */ | |
4444 | 3733 static int |
3734 bt_regexec_nl(rmp, line, col) | |
7 | 3735 regmatch_T *rmp; |
3736 char_u *line; /* string to match against */ | |
3737 colnr_T col; /* column to start looking for match */ | |
3738 { | |
3739 reg_match = rmp; | |
3740 reg_mmatch = NULL; | |
3741 reg_maxline = 0; | |
3742 reg_line_lbr = TRUE; | |
4061 | 3743 reg_buf = curbuf; |
7 | 3744 reg_win = NULL; |
3745 ireg_ic = rmp->rm_ic; | |
3746 #ifdef FEAT_MBYTE | |
3747 ireg_icombine = FALSE; | |
3748 #endif | |
410 | 3749 ireg_maxcol = 0; |
4444 | 3750 return (bt_regexec_both(line, col, NULL) != 0); |
7 | 3751 } |
3752 #endif | |
3753 | |
4444 | 3754 static long bt_regexec_multi __ARGS((regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm)); |
3755 | |
7 | 3756 /* |
3757 * Match a regexp against multiple lines. | |
3758 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp(). | |
3759 * Uses curbuf for line count and 'iskeyword'. | |
3760 * | |
3761 * Return zero if there is no match. Return number of lines contained in the | |
3762 * match otherwise. | |
3763 */ | |
4444 | 3764 static long |
3765 bt_regexec_multi(rmp, win, buf, lnum, col, tm) | |
7 | 3766 regmmatch_T *rmp; |
3767 win_T *win; /* window in which to search or NULL */ | |
3768 buf_T *buf; /* buffer in which to search */ | |
3769 linenr_T lnum; /* nr of line to start looking for match */ | |
3770 colnr_T col; /* column to start looking for match */ | |
1521 | 3771 proftime_T *tm; /* timeout limit or NULL */ |
7 | 3772 { |
3773 long r; | |
3774 | |
3775 reg_match = NULL; | |
3776 reg_mmatch = rmp; | |
3777 reg_buf = buf; | |
3778 reg_win = win; | |
3779 reg_firstlnum = lnum; | |
3780 reg_maxline = reg_buf->b_ml.ml_line_count - lnum; | |
3781 reg_line_lbr = FALSE; | |
3782 ireg_ic = rmp->rmm_ic; | |
3783 #ifdef FEAT_MBYTE | |
3784 ireg_icombine = FALSE; | |
3785 #endif | |
410 | 3786 ireg_maxcol = rmp->rmm_maxcol; |
7 | 3787 |
4444 | 3788 r = bt_regexec_both(NULL, col, tm); |
7 | 3789 |
3790 return r; | |
3791 } | |
3792 | |
3793 /* | |
3794 * Match a regexp against a string ("line" points to the string) or multiple | |
3795 * lines ("line" is NULL, use reg_getline()). | |
3796 */ | |
3797 static long | |
4444 | 3798 bt_regexec_both(line, col, tm) |
7 | 3799 char_u *line; |
3800 colnr_T col; /* column to start looking for match */ | |
1877 | 3801 proftime_T *tm UNUSED; /* timeout limit or NULL */ |
7 | 3802 { |
4444 | 3803 bt_regprog_T *prog; |
7 | 3804 char_u *s; |
189 | 3805 long retval = 0L; |
7 | 3806 |
1520 | 3807 /* Create "regstack" and "backpos" if they are not allocated yet. |
3808 * We allocate *_INITIAL amount of bytes first and then set the grow size | |
3809 * to much bigger value to avoid many malloc calls in case of deep regular | |
3810 * expressions. */ | |
3811 if (regstack.ga_data == NULL) | |
3812 { | |
3813 /* Use an item size of 1 byte, since we push different things | |
3814 * onto the regstack. */ | |
3815 ga_init2(®stack, 1, REGSTACK_INITIAL); | |
3816 ga_grow(®stack, REGSTACK_INITIAL); | |
3817 regstack.ga_growsize = REGSTACK_INITIAL * 8; | |
3818 } | |
3819 | |
3820 if (backpos.ga_data == NULL) | |
3821 { | |
3822 ga_init2(&backpos, sizeof(backpos_T), BACKPOS_INITIAL); | |
3823 ga_grow(&backpos, BACKPOS_INITIAL); | |
3824 backpos.ga_growsize = BACKPOS_INITIAL * 8; | |
3825 } | |
270 | 3826 |
7 | 3827 if (REG_MULTI) |
3828 { | |
4444 | 3829 prog = (bt_regprog_T *)reg_mmatch->regprog; |
7 | 3830 line = reg_getline((linenr_T)0); |
3831 reg_startpos = reg_mmatch->startpos; | |
3832 reg_endpos = reg_mmatch->endpos; | |
3833 } | |
3834 else | |
3835 { | |
4444 | 3836 prog = (bt_regprog_T *)reg_match->regprog; |
7 | 3837 reg_startp = reg_match->startp; |
3838 reg_endp = reg_match->endp; | |
3839 } | |
3840 | |
3841 /* Be paranoid... */ | |
3842 if (prog == NULL || line == NULL) | |
3843 { | |
3844 EMSG(_(e_null)); | |
3845 goto theend; | |
3846 } | |
3847 | |
3848 /* Check validity of program. */ | |
3849 if (prog_magic_wrong()) | |
3850 goto theend; | |
3851 | |
410 | 3852 /* If the start column is past the maximum column: no need to try. */ |
3853 if (ireg_maxcol > 0 && col >= ireg_maxcol) | |
3854 goto theend; | |
3855 | |
7 | 3856 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */ |
3857 if (prog->regflags & RF_ICASE) | |
3858 ireg_ic = TRUE; | |
3859 else if (prog->regflags & RF_NOICASE) | |
3860 ireg_ic = FALSE; | |
3861 | |
3862 #ifdef FEAT_MBYTE | |
3863 /* If pattern contains "\Z" overrule value of ireg_icombine */ | |
3864 if (prog->regflags & RF_ICOMBINE) | |
3865 ireg_icombine = TRUE; | |
3866 #endif | |
3867 | |
3868 /* If there is a "must appear" string, look for it. */ | |
3869 if (prog->regmust != NULL) | |
3870 { | |
3871 int c; | |
3872 | |
3873 #ifdef FEAT_MBYTE | |
3874 if (has_mbyte) | |
3875 c = (*mb_ptr2char)(prog->regmust); | |
3876 else | |
3877 #endif | |
3878 c = *prog->regmust; | |
3879 s = line + col; | |
170 | 3880 |
3881 /* | |
3882 * This is used very often, esp. for ":global". Use three versions of | |
3883 * the loop to avoid overhead of conditions. | |
3884 */ | |
3885 if (!ireg_ic | |
3886 #ifdef FEAT_MBYTE | |
3887 && !has_mbyte | |
3888 #endif | |
3889 ) | |
3890 while ((s = vim_strbyte(s, c)) != NULL) | |
3891 { | |
3892 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0) | |
3893 break; /* Found it. */ | |
3894 ++s; | |
3895 } | |
3896 #ifdef FEAT_MBYTE | |
3897 else if (!ireg_ic || (!enc_utf8 && mb_char2len(c) > 1)) | |
3898 while ((s = vim_strchr(s, c)) != NULL) | |
3899 { | |
3900 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0) | |
3901 break; /* Found it. */ | |
3902 mb_ptr_adv(s); | |
3903 } | |
3904 #endif | |
3905 else | |
3906 while ((s = cstrchr(s, c)) != NULL) | |
3907 { | |
3908 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0) | |
3909 break; /* Found it. */ | |
3910 mb_ptr_adv(s); | |
3911 } | |
7 | 3912 if (s == NULL) /* Not present. */ |
3913 goto theend; | |
3914 } | |
3915 | |
3916 regline = line; | |
3917 reglnum = 0; | |
2578 | 3918 reg_toolong = FALSE; |
7 | 3919 |
3920 /* Simplest case: Anchored match need be tried only once. */ | |
3921 if (prog->reganch) | |
3922 { | |
3923 int c; | |
3924 | |
3925 #ifdef FEAT_MBYTE | |
3926 if (has_mbyte) | |
3927 c = (*mb_ptr2char)(regline + col); | |
3928 else | |
3929 #endif | |
3930 c = regline[col]; | |
3931 if (prog->regstart == NUL | |
3932 || prog->regstart == c | |
3933 || (ireg_ic && (( | |
3934 #ifdef FEAT_MBYTE | |
3935 (enc_utf8 && utf_fold(prog->regstart) == utf_fold(c))) | |
3936 || (c < 255 && prog->regstart < 255 && | |
3937 #endif | |
1347 | 3938 MB_TOLOWER(prog->regstart) == MB_TOLOWER(c))))) |
7 | 3939 retval = regtry(prog, col); |
3940 else | |
3941 retval = 0; | |
3942 } | |
3943 else | |
3944 { | |
1521 | 3945 #ifdef FEAT_RELTIME |
3946 int tm_count = 0; | |
3947 #endif | |
7 | 3948 /* Messy cases: unanchored match. */ |
180 | 3949 while (!got_int) |
7 | 3950 { |
3951 if (prog->regstart != NUL) | |
3952 { | |
170 | 3953 /* Skip until the char we know it must start with. |
3954 * Used often, do some work to avoid call overhead. */ | |
3955 if (!ireg_ic | |
3956 #ifdef FEAT_MBYTE | |
3957 && !has_mbyte | |
3958 #endif | |
3959 ) | |
3960 s = vim_strbyte(regline + col, prog->regstart); | |
3961 else | |
3962 s = cstrchr(regline + col, prog->regstart); | |
7 | 3963 if (s == NULL) |
3964 { | |
3965 retval = 0; | |
3966 break; | |
3967 } | |
3968 col = (int)(s - regline); | |
3969 } | |
3970 | |
410 | 3971 /* Check for maximum column to try. */ |
3972 if (ireg_maxcol > 0 && col >= ireg_maxcol) | |
3973 { | |
3974 retval = 0; | |
3975 break; | |
3976 } | |
3977 | |
7 | 3978 retval = regtry(prog, col); |
3979 if (retval > 0) | |
3980 break; | |
3981 | |
3982 /* if not currently on the first line, get it again */ | |
3983 if (reglnum != 0) | |
3984 { | |
481 | 3985 reglnum = 0; |
7 | 3986 regline = reg_getline((linenr_T)0); |
3987 } | |
3988 if (regline[col] == NUL) | |
3989 break; | |
3990 #ifdef FEAT_MBYTE | |
3991 if (has_mbyte) | |
474 | 3992 col += (*mb_ptr2len)(regline + col); |
7 | 3993 else |
3994 #endif | |
3995 ++col; | |
1521 | 3996 #ifdef FEAT_RELTIME |
3997 /* Check for timeout once in a twenty times to avoid overhead. */ | |
3998 if (tm != NULL && ++tm_count == 20) | |
3999 { | |
4000 tm_count = 0; | |
4001 if (profile_passed_limit(tm)) | |
4002 break; | |
4003 } | |
4004 #endif | |
7 | 4005 } |
4006 } | |
4007 | |
4008 theend: | |
1520 | 4009 /* Free "reg_tofree" when it's a bit big. |
4010 * Free regstack and backpos if they are bigger than their initial size. */ | |
4011 if (reg_tofreelen > 400) | |
4012 { | |
4013 vim_free(reg_tofree); | |
4014 reg_tofree = NULL; | |
4015 } | |
4016 if (regstack.ga_maxlen > REGSTACK_INITIAL) | |
4017 ga_clear(®stack); | |
4018 if (backpos.ga_maxlen > BACKPOS_INITIAL) | |
4019 ga_clear(&backpos); | |
270 | 4020 |
7 | 4021 return retval; |
4022 } | |
4023 | |
4024 #ifdef FEAT_SYN_HL | |
4025 static reg_extmatch_T *make_extmatch __ARGS((void)); | |
4026 | |
4027 /* | |
4028 * Create a new extmatch and mark it as referenced once. | |
4029 */ | |
4030 static reg_extmatch_T * | |
4031 make_extmatch() | |
4032 { | |
4033 reg_extmatch_T *em; | |
4034 | |
4035 em = (reg_extmatch_T *)alloc_clear((unsigned)sizeof(reg_extmatch_T)); | |
4036 if (em != NULL) | |
4037 em->refcnt = 1; | |
4038 return em; | |
4039 } | |
4040 | |
4041 /* | |
4042 * Add a reference to an extmatch. | |
4043 */ | |
4044 reg_extmatch_T * | |
4045 ref_extmatch(em) | |
4046 reg_extmatch_T *em; | |
4047 { | |
4048 if (em != NULL) | |
4049 em->refcnt++; | |
4050 return em; | |
4051 } | |
4052 | |
4053 /* | |
4054 * Remove a reference to an extmatch. If there are no references left, free | |
4055 * the info. | |
4056 */ | |
4057 void | |
4058 unref_extmatch(em) | |
4059 reg_extmatch_T *em; | |
4060 { | |
4061 int i; | |
4062 | |
4063 if (em != NULL && --em->refcnt <= 0) | |
4064 { | |
4065 for (i = 0; i < NSUBEXP; ++i) | |
4066 vim_free(em->matches[i]); | |
4067 vim_free(em); | |
4068 } | |
4069 } | |
4070 #endif | |
4071 | |
4072 /* | |
4073 * regtry - try match of "prog" with at regline["col"]. | |
4074 * Returns 0 for failure, number of lines contained in the match otherwise. | |
4075 */ | |
4076 static long | |
4077 regtry(prog, col) | |
4444 | 4078 bt_regprog_T *prog; |
7 | 4079 colnr_T col; |
4080 { | |
4081 reginput = regline + col; | |
4082 need_clear_subexpr = TRUE; | |
4083 #ifdef FEAT_SYN_HL | |
4084 /* Clear the external match subpointers if necessary. */ | |
4085 if (prog->reghasz == REX_SET) | |
4086 need_clear_zsubexpr = TRUE; | |
4087 #endif | |
4088 | |
189 | 4089 if (regmatch(prog->program + 1) == 0) |
4090 return 0; | |
4091 | |
4092 cleanup_subexpr(); | |
4093 if (REG_MULTI) | |
7 | 4094 { |
189 | 4095 if (reg_startpos[0].lnum < 0) |
7 | 4096 { |
189 | 4097 reg_startpos[0].lnum = 0; |
4098 reg_startpos[0].col = col; | |
4099 } | |
4100 if (reg_endpos[0].lnum < 0) | |
4101 { | |
4102 reg_endpos[0].lnum = reglnum; | |
4103 reg_endpos[0].col = (int)(reginput - regline); | |
7 | 4104 } |
4105 else | |
189 | 4106 /* Use line number of "\ze". */ |
4107 reglnum = reg_endpos[0].lnum; | |
4108 } | |
4109 else | |
4110 { | |
4111 if (reg_startp[0] == NULL) | |
4112 reg_startp[0] = regline + col; | |
4113 if (reg_endp[0] == NULL) | |
4114 reg_endp[0] = reginput; | |
4115 } | |
7 | 4116 #ifdef FEAT_SYN_HL |
189 | 4117 /* Package any found \z(...\) matches for export. Default is none. */ |
4118 unref_extmatch(re_extmatch_out); | |
4119 re_extmatch_out = NULL; | |
4120 | |
4121 if (prog->reghasz == REX_SET) | |
4122 { | |
4123 int i; | |
4124 | |
4125 cleanup_zsubexpr(); | |
4126 re_extmatch_out = make_extmatch(); | |
4127 for (i = 0; i < NSUBEXP; i++) | |
7 | 4128 { |
189 | 4129 if (REG_MULTI) |
7 | 4130 { |
189 | 4131 /* Only accept single line matches. */ |
4132 if (reg_startzpos[i].lnum >= 0 | |
4133 && reg_endzpos[i].lnum == reg_startzpos[i].lnum) | |
4134 re_extmatch_out->matches[i] = | |
4135 vim_strnsave(reg_getline(reg_startzpos[i].lnum) | |
7 | 4136 + reg_startzpos[i].col, |
189 | 4137 reg_endzpos[i].col - reg_startzpos[i].col); |
4138 } | |
4139 else | |
4140 { | |
4141 if (reg_startzp[i] != NULL && reg_endzp[i] != NULL) | |
4142 re_extmatch_out->matches[i] = | |
7 | 4143 vim_strnsave(reg_startzp[i], |
189 | 4144 (int)(reg_endzp[i] - reg_startzp[i])); |
7 | 4145 } |
4146 } | |
189 | 4147 } |
7 | 4148 #endif |
189 | 4149 return 1 + reglnum; |
7 | 4150 } |
4151 | |
4152 #ifdef FEAT_MBYTE | |
4153 static int reg_prev_class __ARGS((void)); | |
4154 | |
4155 /* | |
4156 * Get class of previous character. | |
4157 */ | |
4158 static int | |
4159 reg_prev_class() | |
4160 { | |
4161 if (reginput > regline) | |
4069 | 4162 return mb_get_class_buf(reginput - 1 |
4163 - (*mb_head_off)(regline, reginput - 1), reg_buf); | |
7 | 4164 return -1; |
4165 } | |
4166 | |
4167 #endif | |
4730
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4168 #ifdef FEAT_VISUAL |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4169 static int reg_match_visual __ARGS((void)); |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4170 |
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 * 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
|
4173 */ |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4174 static int |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4175 reg_match_visual() |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4176 { |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4177 pos_T top, bot; |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4178 linenr_T lnum; |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4179 colnr_T col; |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4180 win_T *wp = reg_win == NULL ? curwin : reg_win; |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4181 int mode; |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4182 colnr_T start, end; |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4183 colnr_T start2, end2; |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4184 colnr_T cols; |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4185 |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4186 /* Check if the buffer is the current buffer. */ |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4187 if (reg_buf != curbuf || VIsual.lnum == 0) |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4188 return FALSE; |
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 if (VIsual_active) |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4191 { |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4192 if (lt(VIsual, wp->w_cursor)) |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4193 { |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4194 top = VIsual; |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4195 bot = wp->w_cursor; |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4196 } |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4197 else |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4198 { |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4199 top = wp->w_cursor; |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4200 bot = VIsual; |
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 mode = VIsual_mode; |
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 else |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4205 { |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4206 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
|
4207 { |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4208 top = curbuf->b_visual.vi_start; |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4209 bot = curbuf->b_visual.vi_end; |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4210 } |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4211 else |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4212 { |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4213 top = curbuf->b_visual.vi_end; |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4214 bot = curbuf->b_visual.vi_start; |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4215 } |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4216 mode = curbuf->b_visual.vi_mode; |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4217 } |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4218 lnum = reglnum + reg_firstlnum; |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4219 if (lnum < top.lnum || lnum > bot.lnum) |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4220 return FALSE; |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4221 |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4222 if (mode == 'v') |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4223 { |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4224 col = (colnr_T)(reginput - regline); |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4225 if ((lnum == top.lnum && col < top.col) |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4226 || (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
|
4227 return FALSE; |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4228 } |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4229 else if (mode == Ctrl_V) |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4230 { |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4231 getvvcol(wp, &top, &start, NULL, &end); |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4232 getvvcol(wp, &bot, &start2, NULL, &end2); |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4233 if (start2 < start) |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4234 start = start2; |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4235 if (end2 > end) |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4236 end = end2; |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4237 if (top.col == MAXCOL || bot.col == MAXCOL) |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4238 end = MAXCOL; |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4239 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
|
4240 if (cols < start || cols > end - (*p_sel == 'e')) |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4241 return FALSE; |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4242 } |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4243 return TRUE; |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4244 } |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4245 #endif |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4246 |
39 | 4247 #define ADVANCE_REGINPUT() mb_ptr_adv(reginput) |
7 | 4248 |
4249 /* | |
4250 * The arguments from BRACE_LIMITS are stored here. They are actually local | |
4251 * to regmatch(), but they are here to reduce the amount of stack space used | |
4252 * (it can be called recursively many times). | |
4253 */ | |
4254 static long bl_minval; | |
4255 static long bl_maxval; | |
4256 | |
4257 /* | |
4258 * regmatch - main matching routine | |
4259 * | |
180 | 4260 * Conceptually the strategy is simple: Check to see whether the current node |
4261 * matches, push an item onto the regstack and loop to see whether the rest | |
4262 * matches, and then act accordingly. In practice we make some effort to | |
4263 * avoid using the regstack, in particular by going through "ordinary" nodes | |
4264 * (that don't need to know whether the rest of the match failed) by a nested | |
4265 * loop. | |
7 | 4266 * |
4267 * Returns TRUE when there is a match. Leaves reginput and reglnum just after | |
4268 * the last matched character. | |
4269 * Returns FALSE when there is no match. Leaves reginput and reglnum in an | |
4270 * undefined state! | |
4271 */ | |
4272 static int | |
180 | 4273 regmatch(scan) |
7 | 4274 char_u *scan; /* Current node. */ |
4275 { | |
180 | 4276 char_u *next; /* Next node. */ |
4277 int op; | |
4278 int c; | |
4279 regitem_T *rp; | |
4280 int no; | |
4281 int status; /* one of the RA_ values: */ | |
4282 #define RA_FAIL 1 /* something failed, abort */ | |
4283 #define RA_CONT 2 /* continue in inner loop */ | |
4284 #define RA_BREAK 3 /* break inner loop */ | |
4285 #define RA_MATCH 4 /* successful match */ | |
4286 #define RA_NOMATCH 5 /* didn't match */ | |
270 | 4287 |
1520 | 4288 /* Make "regstack" and "backpos" empty. They are allocated and freed in |
4444 | 4289 * bt_regexec_both() to reduce malloc()/free() calls. */ |
270 | 4290 regstack.ga_len = 0; |
4291 backpos.ga_len = 0; | |
233 | 4292 |
180 | 4293 /* |
233 | 4294 * Repeat until "regstack" is empty. |
180 | 4295 */ |
4296 for (;;) | |
4297 { | |
4444 | 4298 /* Some patterns may cause a long time to match, even though they are not |
7 | 4299 * illegal. E.g., "\([a-z]\+\)\+Q". Allow breaking them with CTRL-C. */ |
4300 fast_breakcheck(); | |
4301 | |
4302 #ifdef DEBUG | |
4303 if (scan != NULL && regnarrate) | |
4304 { | |
4444 | 4305 mch_errmsg((char *)regprop(scan)); |
7 | 4306 mch_errmsg("(\n"); |
4307 } | |
4308 #endif | |
180 | 4309 |
4310 /* | |
233 | 4311 * Repeat for items that can be matched sequentially, without using the |
180 | 4312 * regstack. |
4313 */ | |
4314 for (;;) | |
7 | 4315 { |
180 | 4316 if (got_int || scan == NULL) |
4317 { | |
4318 status = RA_FAIL; | |
4319 break; | |
4320 } | |
4321 status = RA_CONT; | |
4322 | |
7 | 4323 #ifdef DEBUG |
4324 if (regnarrate) | |
4325 { | |
4444 | 4326 mch_errmsg((char *)regprop(scan)); |
7 | 4327 mch_errmsg("...\n"); |
4328 # ifdef FEAT_SYN_HL | |
4329 if (re_extmatch_in != NULL) | |
4330 { | |
4331 int i; | |
4332 | |
4333 mch_errmsg(_("External submatches:\n")); | |
4334 for (i = 0; i < NSUBEXP; i++) | |
4335 { | |
4336 mch_errmsg(" \""); | |
4337 if (re_extmatch_in->matches[i] != NULL) | |
4444 | 4338 mch_errmsg((char *)re_extmatch_in->matches[i]); |
7 | 4339 mch_errmsg("\"\n"); |
4340 } | |
4341 } | |
4342 # endif | |
4343 } | |
4344 #endif | |
4345 next = regnext(scan); | |
4346 | |
4347 op = OP(scan); | |
4348 /* Check for character class with NL added. */ | |
1018 | 4349 if (!reg_line_lbr && WITH_NL(op) && REG_MULTI |
4350 && *reginput == NUL && reglnum <= reg_maxline) | |
7 | 4351 { |
4352 reg_nextline(); | |
4353 } | |
4354 else if (reg_line_lbr && WITH_NL(op) && *reginput == '\n') | |
4355 { | |
4356 ADVANCE_REGINPUT(); | |
4357 } | |
4358 else | |
4359 { | |
4360 if (WITH_NL(op)) | |
180 | 4361 op -= ADD_NL; |
7 | 4362 #ifdef FEAT_MBYTE |
4363 if (has_mbyte) | |
4364 c = (*mb_ptr2char)(reginput); | |
4365 else | |
4366 #endif | |
4367 c = *reginput; | |
4368 switch (op) | |
4369 { | |
4370 case BOL: | |
4371 if (reginput != regline) | |
180 | 4372 status = RA_NOMATCH; |
7 | 4373 break; |
4374 | |
4375 case EOL: | |
4376 if (c != NUL) | |
180 | 4377 status = RA_NOMATCH; |
7 | 4378 break; |
4379 | |
4380 case RE_BOF: | |
1458 | 4381 /* We're not at the beginning of the file when below the first |
4382 * line where we started, not at the start of the line or we | |
4383 * didn't start at the first line of the buffer. */ | |
7 | 4384 if (reglnum != 0 || reginput != regline |
1458 | 4385 || (REG_MULTI && reg_firstlnum > 1)) |
180 | 4386 status = RA_NOMATCH; |
7 | 4387 break; |
4388 | |
4389 case RE_EOF: | |
4390 if (reglnum != reg_maxline || c != NUL) | |
180 | 4391 status = RA_NOMATCH; |
7 | 4392 break; |
4393 | |
4394 case CURSOR: | |
4395 /* Check if the buffer is in a window and compare the | |
4396 * reg_win->w_cursor position to the match position. */ | |
4397 if (reg_win == NULL | |
4398 || (reglnum + reg_firstlnum != reg_win->w_cursor.lnum) | |
4399 || ((colnr_T)(reginput - regline) != reg_win->w_cursor.col)) | |
180 | 4400 status = RA_NOMATCH; |
7 | 4401 break; |
4402 | |
639 | 4403 case RE_MARK: |
4404 /* Compare the mark position to the match position. NOTE: Always | |
4405 * uses the current buffer. */ | |
4406 { | |
4407 int mark = OPERAND(scan)[0]; | |
4408 int cmp = OPERAND(scan)[1]; | |
4409 pos_T *pos; | |
4410 | |
4043 | 4411 pos = getmark_buf(reg_buf, mark, FALSE); |
1148 | 4412 if (pos == NULL /* mark doesn't exist */ |
639 | 4413 || pos->lnum <= 0 /* mark isn't set (in curbuf) */ |
4414 || (pos->lnum == reglnum + reg_firstlnum | |
4415 ? (pos->col == (colnr_T)(reginput - regline) | |
4416 ? (cmp == '<' || cmp == '>') | |
4417 : (pos->col < (colnr_T)(reginput - regline) | |
4418 ? cmp != '>' | |
4419 : cmp != '<')) | |
4420 : (pos->lnum < reglnum + reg_firstlnum | |
4421 ? cmp != '>' | |
4422 : cmp != '<'))) | |
4423 status = RA_NOMATCH; | |
4424 } | |
4425 break; | |
4426 | |
4427 case RE_VISUAL: | |
4428 #ifdef FEAT_VISUAL | |
4730
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4429 if (!reg_match_visual()) |
749e2b2755d5
updated for version 7.3.1112
Bram Moolenaar <bram@vim.org>
parents:
4720
diff
changeset
|
4430 #endif |
639 | 4431 status = RA_NOMATCH; |
4432 break; | |
4433 | |
7 | 4434 case RE_LNUM: |
4435 if (!REG_MULTI || !re_num_cmp((long_u)(reglnum + reg_firstlnum), | |
4436 scan)) | |
180 | 4437 status = RA_NOMATCH; |
7 | 4438 break; |
4439 | |
4440 case RE_COL: | |
4441 if (!re_num_cmp((long_u)(reginput - regline) + 1, scan)) | |
180 | 4442 status = RA_NOMATCH; |
7 | 4443 break; |
4444 | |
4445 case RE_VCOL: | |
4446 if (!re_num_cmp((long_u)win_linetabsize( | |
4447 reg_win == NULL ? curwin : reg_win, | |
4448 regline, (colnr_T)(reginput - regline)) + 1, scan)) | |
180 | 4449 status = RA_NOMATCH; |
7 | 4450 break; |
4451 | |
4452 case BOW: /* \<word; reginput points to w */ | |
4453 if (c == NUL) /* Can't match at end of line */ | |
180 | 4454 status = RA_NOMATCH; |
7 | 4455 #ifdef FEAT_MBYTE |
180 | 4456 else if (has_mbyte) |
7 | 4457 { |
4458 int this_class; | |
4459 | |
4460 /* Get class of current and previous char (if it exists). */ | |
4069 | 4461 this_class = mb_get_class_buf(reginput, reg_buf); |
7 | 4462 if (this_class <= 1) |
180 | 4463 status = RA_NOMATCH; /* not on a word at all */ |
4464 else if (reg_prev_class() == this_class) | |
4465 status = RA_NOMATCH; /* previous char is in same word */ | |
7 | 4466 } |
4467 #endif | |
4468 else | |
4469 { | |
4061 | 4470 if (!vim_iswordc_buf(c, reg_buf) || (reginput > regline |
4471 && vim_iswordc_buf(reginput[-1], reg_buf))) | |
180 | 4472 status = RA_NOMATCH; |
7 | 4473 } |
4474 break; | |
4475 | |
4476 case EOW: /* word\>; reginput points after d */ | |
4477 if (reginput == regline) /* Can't match at start of line */ | |
180 | 4478 status = RA_NOMATCH; |
7 | 4479 #ifdef FEAT_MBYTE |
180 | 4480 else if (has_mbyte) |
7 | 4481 { |
4482 int this_class, prev_class; | |
4483 | |
4484 /* Get class of current and previous char (if it exists). */ | |
4069 | 4485 this_class = mb_get_class_buf(reginput, reg_buf); |
7 | 4486 prev_class = reg_prev_class(); |
180 | 4487 if (this_class == prev_class |
4488 || prev_class == 0 || prev_class == 1) | |
4489 status = RA_NOMATCH; | |
7 | 4490 } |
180 | 4491 #endif |
7 | 4492 else |
4493 { | |
4043 | 4494 if (!vim_iswordc_buf(reginput[-1], reg_buf) |
4495 || (reginput[0] != NUL && vim_iswordc_buf(c, reg_buf))) | |
180 | 4496 status = RA_NOMATCH; |
7 | 4497 } |
4498 break; /* Matched with EOW */ | |
4499 | |
4500 case ANY: | |
4084 | 4501 /* ANY does not match new lines. */ |
7 | 4502 if (c == NUL) |
180 | 4503 status = RA_NOMATCH; |
4504 else | |
4505 ADVANCE_REGINPUT(); | |
7 | 4506 break; |
4507 | |
4508 case IDENT: | |
4509 if (!vim_isIDc(c)) | |
180 | 4510 status = RA_NOMATCH; |
4511 else | |
4512 ADVANCE_REGINPUT(); | |
7 | 4513 break; |
4514 | |
4515 case SIDENT: | |
4516 if (VIM_ISDIGIT(*reginput) || !vim_isIDc(c)) | |
180 | 4517 status = RA_NOMATCH; |
4518 else | |
4519 ADVANCE_REGINPUT(); | |
7 | 4520 break; |
4521 | |
4522 case KWORD: | |
4069 | 4523 if (!vim_iswordp_buf(reginput, reg_buf)) |
180 | 4524 status = RA_NOMATCH; |
4525 else | |
4526 ADVANCE_REGINPUT(); | |
7 | 4527 break; |
4528 | |
4529 case SKWORD: | |
4069 | 4530 if (VIM_ISDIGIT(*reginput) || !vim_iswordp_buf(reginput, reg_buf)) |
180 | 4531 status = RA_NOMATCH; |
4532 else | |
4533 ADVANCE_REGINPUT(); | |
7 | 4534 break; |
4535 | |
4536 case FNAME: | |
4537 if (!vim_isfilec(c)) | |
180 | 4538 status = RA_NOMATCH; |
4539 else | |
4540 ADVANCE_REGINPUT(); | |
7 | 4541 break; |
4542 | |
4543 case SFNAME: | |
4544 if (VIM_ISDIGIT(*reginput) || !vim_isfilec(c)) | |
180 | 4545 status = RA_NOMATCH; |
4546 else | |
4547 ADVANCE_REGINPUT(); | |
7 | 4548 break; |
4549 | |
4550 case PRINT: | |
4551 if (ptr2cells(reginput) != 1) | |
180 | 4552 status = RA_NOMATCH; |
4553 else | |
4554 ADVANCE_REGINPUT(); | |
7 | 4555 break; |
4556 | |
4557 case SPRINT: | |
4558 if (VIM_ISDIGIT(*reginput) || ptr2cells(reginput) != 1) | |
180 | 4559 status = RA_NOMATCH; |
4560 else | |
4561 ADVANCE_REGINPUT(); | |
7 | 4562 break; |
4563 | |
4564 case WHITE: | |
4565 if (!vim_iswhite(c)) | |
180 | 4566 status = RA_NOMATCH; |
4567 else | |
4568 ADVANCE_REGINPUT(); | |
7 | 4569 break; |
4570 | |
4571 case NWHITE: | |
4572 if (c == NUL || vim_iswhite(c)) | |
180 | 4573 status = RA_NOMATCH; |
4574 else | |
4575 ADVANCE_REGINPUT(); | |
7 | 4576 break; |
4577 | |
4578 case DIGIT: | |
4579 if (!ri_digit(c)) | |
180 | 4580 status = RA_NOMATCH; |
4581 else | |
4582 ADVANCE_REGINPUT(); | |
7 | 4583 break; |
4584 | |
4585 case NDIGIT: | |
4586 if (c == NUL || ri_digit(c)) | |
180 | 4587 status = RA_NOMATCH; |
4588 else | |
4589 ADVANCE_REGINPUT(); | |
7 | 4590 break; |
4591 | |
4592 case HEX: | |
4593 if (!ri_hex(c)) | |
180 | 4594 status = RA_NOMATCH; |
4595 else | |
4596 ADVANCE_REGINPUT(); | |
7 | 4597 break; |
4598 | |
4599 case NHEX: | |
4600 if (c == NUL || ri_hex(c)) | |
180 | 4601 status = RA_NOMATCH; |
4602 else | |
4603 ADVANCE_REGINPUT(); | |
7 | 4604 break; |
4605 | |
4606 case OCTAL: | |
4607 if (!ri_octal(c)) | |
180 | 4608 status = RA_NOMATCH; |
4609 else | |
4610 ADVANCE_REGINPUT(); | |
7 | 4611 break; |
4612 | |
4613 case NOCTAL: | |
4614 if (c == NUL || ri_octal(c)) | |
180 | 4615 status = RA_NOMATCH; |
4616 else | |
4617 ADVANCE_REGINPUT(); | |
7 | 4618 break; |
4619 | |
4620 case WORD: | |
4621 if (!ri_word(c)) | |
180 | 4622 status = RA_NOMATCH; |
4623 else | |
4624 ADVANCE_REGINPUT(); | |
7 | 4625 break; |
4626 | |
4627 case NWORD: | |
4628 if (c == NUL || ri_word(c)) | |
180 | 4629 status = RA_NOMATCH; |
4630 else | |
4631 ADVANCE_REGINPUT(); | |
7 | 4632 break; |
4633 | |
4634 case HEAD: | |
4635 if (!ri_head(c)) | |
180 | 4636 status = RA_NOMATCH; |
4637 else | |
4638 ADVANCE_REGINPUT(); | |
7 | 4639 break; |
4640 | |
4641 case NHEAD: | |
4642 if (c == NUL || ri_head(c)) | |
180 | 4643 status = RA_NOMATCH; |
4644 else | |
4645 ADVANCE_REGINPUT(); | |
7 | 4646 break; |
4647 | |
4648 case ALPHA: | |
4649 if (!ri_alpha(c)) | |
180 | 4650 status = RA_NOMATCH; |
4651 else | |
4652 ADVANCE_REGINPUT(); | |
7 | 4653 break; |
4654 | |
4655 case NALPHA: | |
4656 if (c == NUL || ri_alpha(c)) | |
180 | 4657 status = RA_NOMATCH; |
4658 else | |
4659 ADVANCE_REGINPUT(); | |
7 | 4660 break; |
4661 | |
4662 case LOWER: | |
4663 if (!ri_lower(c)) | |
180 | 4664 status = RA_NOMATCH; |
4665 else | |
4666 ADVANCE_REGINPUT(); | |
7 | 4667 break; |
4668 | |
4669 case NLOWER: | |
4670 if (c == NUL || ri_lower(c)) | |
180 | 4671 status = RA_NOMATCH; |
4672 else | |
4673 ADVANCE_REGINPUT(); | |
7 | 4674 break; |
4675 | |
4676 case UPPER: | |
4677 if (!ri_upper(c)) | |
180 | 4678 status = RA_NOMATCH; |
4679 else | |
4680 ADVANCE_REGINPUT(); | |
7 | 4681 break; |
4682 | |
4683 case NUPPER: | |
4684 if (c == NUL || ri_upper(c)) | |
180 | 4685 status = RA_NOMATCH; |
4686 else | |
4687 ADVANCE_REGINPUT(); | |
7 | 4688 break; |
4689 | |
4690 case EXACTLY: | |
4691 { | |
4692 int len; | |
4693 char_u *opnd; | |
4694 | |
4695 opnd = OPERAND(scan); | |
4696 /* Inline the first byte, for speed. */ | |
4697 if (*opnd != *reginput | |
4698 && (!ireg_ic || ( | |
4699 #ifdef FEAT_MBYTE | |
4700 !enc_utf8 && | |
4701 #endif | |
1347 | 4702 MB_TOLOWER(*opnd) != MB_TOLOWER(*reginput)))) |
180 | 4703 status = RA_NOMATCH; |
4704 else if (*opnd == NUL) | |
7 | 4705 { |
4706 /* match empty string always works; happens when "~" is | |
4707 * empty. */ | |
4708 } | |
4709 else if (opnd[1] == NUL | |
4710 #ifdef FEAT_MBYTE | |
4711 && !(enc_utf8 && ireg_ic) | |
4712 #endif | |
4713 ) | |
4714 ++reginput; /* matched a single char */ | |
4715 else | |
4716 { | |
4717 len = (int)STRLEN(opnd); | |
4718 /* Need to match first byte again for multi-byte. */ | |
4719 if (cstrncmp(opnd, reginput, &len) != 0) | |
180 | 4720 status = RA_NOMATCH; |
7 | 4721 #ifdef FEAT_MBYTE |
4722 /* Check for following composing character. */ | |
180 | 4723 else if (enc_utf8 |
4724 && UTF_COMPOSINGLIKE(reginput, reginput + len)) | |
7 | 4725 { |
4726 /* raaron: This code makes a composing character get | |
4727 * ignored, which is the correct behavior (sometimes) | |
4728 * for voweled Hebrew texts. */ | |
4729 if (!ireg_icombine) | |
180 | 4730 status = RA_NOMATCH; |
7 | 4731 } |
180 | 4732 #endif |
7 | 4733 else |
180 | 4734 reginput += len; |
7 | 4735 } |
4736 } | |
4737 break; | |
4738 | |
4739 case ANYOF: | |
4740 case ANYBUT: | |
4741 if (c == NUL) | |
180 | 4742 status = RA_NOMATCH; |
4743 else if ((cstrchr(OPERAND(scan), c) == NULL) == (op == ANYOF)) | |
4744 status = RA_NOMATCH; | |
4745 else | |
4746 ADVANCE_REGINPUT(); | |
7 | 4747 break; |
4748 | |
4749 #ifdef FEAT_MBYTE | |
4750 case MULTIBYTECODE: | |
4751 if (has_mbyte) | |
4752 { | |
4753 int i, len; | |
4754 char_u *opnd; | |
944 | 4755 int opndc = 0, inpc; |
7 | 4756 |
4757 opnd = OPERAND(scan); | |
4758 /* Safety check (just in case 'encoding' was changed since | |
4759 * compiling the program). */ | |
474 | 4760 if ((len = (*mb_ptr2len)(opnd)) < 2) |
180 | 4761 { |
4762 status = RA_NOMATCH; | |
4763 break; | |
4764 } | |
714 | 4765 if (enc_utf8) |
4766 opndc = mb_ptr2char(opnd); | |
4767 if (enc_utf8 && utf_iscomposing(opndc)) | |
4768 { | |
4769 /* When only a composing char is given match at any | |
4770 * position where that composing char appears. */ | |
4771 status = RA_NOMATCH; | |
4772 for (i = 0; reginput[i] != NUL; i += utf_char2len(inpc)) | |
180 | 4773 { |
714 | 4774 inpc = mb_ptr2char(reginput + i); |
4775 if (!utf_iscomposing(inpc)) | |
4776 { | |
4777 if (i > 0) | |
4778 break; | |
4779 } | |
4780 else if (opndc == inpc) | |
4781 { | |
4782 /* Include all following composing chars. */ | |
4783 len = i + mb_ptr2len(reginput + i); | |
4784 status = RA_MATCH; | |
4785 break; | |
4786 } | |
180 | 4787 } |
714 | 4788 } |
4789 else | |
4790 for (i = 0; i < len; ++i) | |
4791 if (opnd[i] != reginput[i]) | |
4792 { | |
4793 status = RA_NOMATCH; | |
4794 break; | |
4795 } | |
7 | 4796 reginput += len; |
4797 } | |
4798 else | |
180 | 4799 status = RA_NOMATCH; |
7 | 4800 break; |
4801 #endif | |
4802 | |
4803 case NOTHING: | |
4804 break; | |
4805 | |
4806 case BACK: | |
233 | 4807 { |
4808 int i; | |
4809 backpos_T *bp; | |
4810 | |
4811 /* | |
4812 * When we run into BACK we need to check if we don't keep | |
4813 * looping without matching any input. The second and later | |
4814 * times a BACK is encountered it fails if the input is still | |
4815 * at the same position as the previous time. | |
4816 * The positions are stored in "backpos" and found by the | |
4817 * current value of "scan", the position in the RE program. | |
4818 */ | |
4819 bp = (backpos_T *)backpos.ga_data; | |
4820 for (i = 0; i < backpos.ga_len; ++i) | |
4821 if (bp[i].bp_scan == scan) | |
4822 break; | |
4823 if (i == backpos.ga_len) | |
4824 { | |
4825 /* First time at this BACK, make room to store the pos. */ | |
4826 if (ga_grow(&backpos, 1) == FAIL) | |
4827 status = RA_FAIL; | |
4828 else | |
4829 { | |
4830 /* get "ga_data" again, it may have changed */ | |
4831 bp = (backpos_T *)backpos.ga_data; | |
4832 bp[i].bp_scan = scan; | |
4833 ++backpos.ga_len; | |
4834 } | |
4835 } | |
4836 else if (reg_save_equal(&bp[i].bp_pos)) | |
4837 /* Still at same position as last time, fail. */ | |
4838 status = RA_NOMATCH; | |
4839 | |
4840 if (status != RA_FAIL && status != RA_NOMATCH) | |
4841 reg_save(&bp[i].bp_pos, &backpos); | |
4842 } | |
179 | 4843 break; |
4844 | |
7 | 4845 case MOPEN + 0: /* Match start: \zs */ |
4846 case MOPEN + 1: /* \( */ | |
4847 case MOPEN + 2: | |
4848 case MOPEN + 3: | |
4849 case MOPEN + 4: | |
4850 case MOPEN + 5: | |
4851 case MOPEN + 6: | |
4852 case MOPEN + 7: | |
4853 case MOPEN + 8: | |
4854 case MOPEN + 9: | |
4855 { | |
4856 no = op - MOPEN; | |
4857 cleanup_subexpr(); | |
270 | 4858 rp = regstack_push(RS_MOPEN, scan); |
180 | 4859 if (rp == NULL) |
4860 status = RA_FAIL; | |
4861 else | |
4862 { | |
4863 rp->rs_no = no; | |
4864 save_se(&rp->rs_un.sesave, ®_startpos[no], | |
4865 ®_startp[no]); | |
4866 /* We simply continue and handle the result when done. */ | |
4867 } | |
7 | 4868 } |
180 | 4869 break; |
7 | 4870 |
4871 case NOPEN: /* \%( */ | |
4872 case NCLOSE: /* \) after \%( */ | |
270 | 4873 if (regstack_push(RS_NOPEN, scan) == NULL) |
180 | 4874 status = RA_FAIL; |
4875 /* We simply continue and handle the result when done. */ | |
4876 break; | |
7 | 4877 |
4878 #ifdef FEAT_SYN_HL | |
4879 case ZOPEN + 1: | |
4880 case ZOPEN + 2: | |
4881 case ZOPEN + 3: | |
4882 case ZOPEN + 4: | |
4883 case ZOPEN + 5: | |
4884 case ZOPEN + 6: | |
4885 case ZOPEN + 7: | |
4886 case ZOPEN + 8: | |
4887 case ZOPEN + 9: | |
4888 { | |
4889 no = op - ZOPEN; | |
4890 cleanup_zsubexpr(); | |
270 | 4891 rp = regstack_push(RS_ZOPEN, scan); |
180 | 4892 if (rp == NULL) |
4893 status = RA_FAIL; | |
4894 else | |
4895 { | |
4896 rp->rs_no = no; | |
4897 save_se(&rp->rs_un.sesave, ®_startzpos[no], | |
4898 ®_startzp[no]); | |
4899 /* We simply continue and handle the result when done. */ | |
4900 } | |
7 | 4901 } |
180 | 4902 break; |
7 | 4903 #endif |
4904 | |
4905 case MCLOSE + 0: /* Match end: \ze */ | |
4906 case MCLOSE + 1: /* \) */ | |
4907 case MCLOSE + 2: | |
4908 case MCLOSE + 3: | |
4909 case MCLOSE + 4: | |
4910 case MCLOSE + 5: | |
4911 case MCLOSE + 6: | |
4912 case MCLOSE + 7: | |
4913 case MCLOSE + 8: | |
4914 case MCLOSE + 9: | |
4915 { | |
4916 no = op - MCLOSE; | |
4917 cleanup_subexpr(); | |
270 | 4918 rp = regstack_push(RS_MCLOSE, scan); |
180 | 4919 if (rp == NULL) |
4920 status = RA_FAIL; | |
4921 else | |
4922 { | |
4923 rp->rs_no = no; | |
4924 save_se(&rp->rs_un.sesave, ®_endpos[no], ®_endp[no]); | |
4925 /* We simply continue and handle the result when done. */ | |
4926 } | |
7 | 4927 } |
180 | 4928 break; |
7 | 4929 |
4930 #ifdef FEAT_SYN_HL | |
4931 case ZCLOSE + 1: /* \) after \z( */ | |
4932 case ZCLOSE + 2: | |
4933 case ZCLOSE + 3: | |
4934 case ZCLOSE + 4: | |
4935 case ZCLOSE + 5: | |
4936 case ZCLOSE + 6: | |
4937 case ZCLOSE + 7: | |
4938 case ZCLOSE + 8: | |
4939 case ZCLOSE + 9: | |
4940 { | |
4941 no = op - ZCLOSE; | |
4942 cleanup_zsubexpr(); | |
270 | 4943 rp = regstack_push(RS_ZCLOSE, scan); |
180 | 4944 if (rp == NULL) |
4945 status = RA_FAIL; | |
4946 else | |
4947 { | |
4948 rp->rs_no = no; | |
4949 save_se(&rp->rs_un.sesave, ®_endzpos[no], | |
4950 ®_endzp[no]); | |
4951 /* We simply continue and handle the result when done. */ | |
4952 } | |
7 | 4953 } |
180 | 4954 break; |
7 | 4955 #endif |
4956 | |
4957 case BACKREF + 1: | |
4958 case BACKREF + 2: | |
4959 case BACKREF + 3: | |
4960 case BACKREF + 4: | |
4961 case BACKREF + 5: | |
4962 case BACKREF + 6: | |
4963 case BACKREF + 7: | |
4964 case BACKREF + 8: | |
4965 case BACKREF + 9: | |
4966 { | |
4967 int len; | |
4968 linenr_T clnum; | |
4969 colnr_T ccol; | |
4970 char_u *p; | |
4971 | |
4972 no = op - BACKREF; | |
4973 cleanup_subexpr(); | |
4974 if (!REG_MULTI) /* Single-line regexp */ | |
4975 { | |
1815 | 4976 if (reg_startp[no] == NULL || reg_endp[no] == NULL) |
7 | 4977 { |
4978 /* Backref was not set: Match an empty string. */ | |
4979 len = 0; | |
4980 } | |
4981 else | |
4982 { | |
4983 /* Compare current input with back-ref in the same | |
4984 * line. */ | |
4985 len = (int)(reg_endp[no] - reg_startp[no]); | |
4986 if (cstrncmp(reg_startp[no], reginput, &len) != 0) | |
180 | 4987 status = RA_NOMATCH; |
7 | 4988 } |
4989 } | |
4990 else /* Multi-line regexp */ | |
4991 { | |
1815 | 4992 if (reg_startpos[no].lnum < 0 || reg_endpos[no].lnum < 0) |
7 | 4993 { |
4994 /* Backref was not set: Match an empty string. */ | |
4995 len = 0; | |
4996 } | |
4997 else | |
4998 { | |
4999 if (reg_startpos[no].lnum == reglnum | |
5000 && reg_endpos[no].lnum == reglnum) | |
5001 { | |
5002 /* Compare back-ref within the current line. */ | |
5003 len = reg_endpos[no].col - reg_startpos[no].col; | |
5004 if (cstrncmp(regline + reg_startpos[no].col, | |
5005 reginput, &len) != 0) | |
180 | 5006 status = RA_NOMATCH; |
7 | 5007 } |
5008 else | |
5009 { | |
5010 /* Messy situation: Need to compare between two | |
5011 * lines. */ | |
5012 ccol = reg_startpos[no].col; | |
5013 clnum = reg_startpos[no].lnum; | |
5014 for (;;) | |
5015 { | |
5016 /* Since getting one line may invalidate | |
5017 * the other, need to make copy. Slow! */ | |
5018 if (regline != reg_tofree) | |
5019 { | |
5020 len = (int)STRLEN(regline); | |
5021 if (reg_tofree == NULL | |
5022 || len >= (int)reg_tofreelen) | |
5023 { | |
5024 len += 50; /* get some extra */ | |
5025 vim_free(reg_tofree); | |
5026 reg_tofree = alloc(len); | |
5027 if (reg_tofree == NULL) | |
180 | 5028 { |
5029 status = RA_FAIL; /* outof memory!*/ | |
5030 break; | |
5031 } | |
7 | 5032 reg_tofreelen = len; |
5033 } | |
5034 STRCPY(reg_tofree, regline); | |
5035 reginput = reg_tofree | |
5036 + (reginput - regline); | |
5037 regline = reg_tofree; | |
5038 } | |
5039 | |
5040 /* Get the line to compare with. */ | |
5041 p = reg_getline(clnum); | |
5042 if (clnum == reg_endpos[no].lnum) | |
5043 len = reg_endpos[no].col - ccol; | |
5044 else | |
5045 len = (int)STRLEN(p + ccol); | |
5046 | |
5047 if (cstrncmp(p + ccol, reginput, &len) != 0) | |
180 | 5048 { |
5049 status = RA_NOMATCH; /* doesn't match */ | |
5050 break; | |
5051 } | |
7 | 5052 if (clnum == reg_endpos[no].lnum) |
5053 break; /* match and at end! */ | |
481 | 5054 if (reglnum >= reg_maxline) |
180 | 5055 { |
5056 status = RA_NOMATCH; /* text too short */ | |
5057 break; | |
5058 } | |
7 | 5059 |
5060 /* Advance to next line. */ | |
5061 reg_nextline(); | |
5062 ++clnum; | |
5063 ccol = 0; | |
180 | 5064 if (got_int) |
5065 { | |
5066 status = RA_FAIL; | |
5067 break; | |
5068 } | |
7 | 5069 } |
5070 | |
5071 /* found a match! Note that regline may now point | |
5072 * to a copy of the line, that should not matter. */ | |
5073 } | |
5074 } | |
5075 } | |
5076 | |
5077 /* Matched the backref, skip over it. */ | |
5078 reginput += len; | |
5079 } | |
5080 break; | |
5081 | |
5082 #ifdef FEAT_SYN_HL | |
5083 case ZREF + 1: | |
5084 case ZREF + 2: | |
5085 case ZREF + 3: | |
5086 case ZREF + 4: | |
5087 case ZREF + 5: | |
5088 case ZREF + 6: | |
5089 case ZREF + 7: | |
5090 case ZREF + 8: | |
5091 case ZREF + 9: | |
5092 { | |
5093 int len; | |
5094 | |
5095 cleanup_zsubexpr(); | |
5096 no = op - ZREF; | |
5097 if (re_extmatch_in != NULL | |
5098 && re_extmatch_in->matches[no] != NULL) | |
5099 { | |
5100 len = (int)STRLEN(re_extmatch_in->matches[no]); | |
5101 if (cstrncmp(re_extmatch_in->matches[no], | |
5102 reginput, &len) != 0) | |
180 | 5103 status = RA_NOMATCH; |
5104 else | |
5105 reginput += len; | |
7 | 5106 } |
5107 else | |
5108 { | |
5109 /* Backref was not set: Match an empty string. */ | |
5110 } | |
5111 } | |
5112 break; | |
5113 #endif | |
5114 | |
5115 case BRANCH: | |
5116 { | |
5117 if (OP(next) != BRANCH) /* No choice. */ | |
5118 next = OPERAND(scan); /* Avoid recursion. */ | |
5119 else | |
5120 { | |
270 | 5121 rp = regstack_push(RS_BRANCH, scan); |
180 | 5122 if (rp == NULL) |
5123 status = RA_FAIL; | |
5124 else | |
5125 status = RA_BREAK; /* rest is below */ | |
7 | 5126 } |
5127 } | |
5128 break; | |
5129 | |
5130 case BRACE_LIMITS: | |
5131 { | |
5132 if (OP(next) == BRACE_SIMPLE) | |
5133 { | |
5134 bl_minval = OPERAND_MIN(scan); | |
5135 bl_maxval = OPERAND_MAX(scan); | |
5136 } | |
5137 else if (OP(next) >= BRACE_COMPLEX | |
5138 && OP(next) < BRACE_COMPLEX + 10) | |
5139 { | |
5140 no = OP(next) - BRACE_COMPLEX; | |
5141 brace_min[no] = OPERAND_MIN(scan); | |
5142 brace_max[no] = OPERAND_MAX(scan); | |
5143 brace_count[no] = 0; | |
5144 } | |
5145 else | |
5146 { | |
5147 EMSG(_(e_internal)); /* Shouldn't happen */ | |
180 | 5148 status = RA_FAIL; |
7 | 5149 } |
5150 } | |
5151 break; | |
5152 | |
5153 case BRACE_COMPLEX + 0: | |
5154 case BRACE_COMPLEX + 1: | |
5155 case BRACE_COMPLEX + 2: | |
5156 case BRACE_COMPLEX + 3: | |
5157 case BRACE_COMPLEX + 4: | |
5158 case BRACE_COMPLEX + 5: | |
5159 case BRACE_COMPLEX + 6: | |
5160 case BRACE_COMPLEX + 7: | |
5161 case BRACE_COMPLEX + 8: | |
5162 case BRACE_COMPLEX + 9: | |
5163 { | |
5164 no = op - BRACE_COMPLEX; | |
5165 ++brace_count[no]; | |
5166 | |
5167 /* If not matched enough times yet, try one more */ | |
5168 if (brace_count[no] <= (brace_min[no] <= brace_max[no] | |
180 | 5169 ? brace_min[no] : brace_max[no])) |
7 | 5170 { |
270 | 5171 rp = regstack_push(RS_BRCPLX_MORE, scan); |
180 | 5172 if (rp == NULL) |
5173 status = RA_FAIL; | |
5174 else | |
5175 { | |
5176 rp->rs_no = no; | |
233 | 5177 reg_save(&rp->rs_un.regsave, &backpos); |
180 | 5178 next = OPERAND(scan); |
5179 /* We continue and handle the result when done. */ | |
5180 } | |
5181 break; | |
7 | 5182 } |
5183 | |
5184 /* If matched enough times, may try matching some more */ | |
5185 if (brace_min[no] <= brace_max[no]) | |
5186 { | |
5187 /* Range is the normal way around, use longest match */ | |
5188 if (brace_count[no] <= brace_max[no]) | |
5189 { | |
270 | 5190 rp = regstack_push(RS_BRCPLX_LONG, scan); |
180 | 5191 if (rp == NULL) |
5192 status = RA_FAIL; | |
5193 else | |
5194 { | |
5195 rp->rs_no = no; | |
233 | 5196 reg_save(&rp->rs_un.regsave, &backpos); |
180 | 5197 next = OPERAND(scan); |
5198 /* We continue and handle the result when done. */ | |
5199 } | |
7 | 5200 } |
5201 } | |
5202 else | |
5203 { | |
5204 /* Range is backwards, use shortest match first */ | |
5205 if (brace_count[no] <= brace_min[no]) | |
5206 { | |
270 | 5207 rp = regstack_push(RS_BRCPLX_SHORT, scan); |
180 | 5208 if (rp == NULL) |
5209 status = RA_FAIL; | |
5210 else | |
5211 { | |
233 | 5212 reg_save(&rp->rs_un.regsave, &backpos); |
180 | 5213 /* We continue and handle the result when done. */ |
5214 } | |
7 | 5215 } |
5216 } | |
5217 } | |
5218 break; | |
5219 | |
5220 case BRACE_SIMPLE: | |
5221 case STAR: | |
5222 case PLUS: | |
5223 { | |
180 | 5224 regstar_T rst; |
7 | 5225 |
5226 /* | |
5227 * Lookahead to avoid useless match attempts when we know | |
5228 * what character comes next. | |
5229 */ | |
5230 if (OP(next) == EXACTLY) | |
5231 { | |
180 | 5232 rst.nextb = *OPERAND(next); |
7 | 5233 if (ireg_ic) |
5234 { | |
1347 | 5235 if (MB_ISUPPER(rst.nextb)) |
5236 rst.nextb_ic = MB_TOLOWER(rst.nextb); | |
7 | 5237 else |
1347 | 5238 rst.nextb_ic = MB_TOUPPER(rst.nextb); |
7 | 5239 } |
5240 else | |
180 | 5241 rst.nextb_ic = rst.nextb; |
7 | 5242 } |
5243 else | |
5244 { | |
180 | 5245 rst.nextb = NUL; |
5246 rst.nextb_ic = NUL; | |
7 | 5247 } |
5248 if (op != BRACE_SIMPLE) | |
5249 { | |
180 | 5250 rst.minval = (op == STAR) ? 0 : 1; |
5251 rst.maxval = MAX_LIMIT; | |
7 | 5252 } |
5253 else | |
5254 { | |
180 | 5255 rst.minval = bl_minval; |
5256 rst.maxval = bl_maxval; | |
7 | 5257 } |
5258 | |
5259 /* | |
5260 * When maxval > minval, try matching as much as possible, up | |
5261 * to maxval. When maxval < minval, try matching at least the | |
5262 * minimal number (since the range is backwards, that's also | |
5263 * maxval!). | |
5264 */ | |
180 | 5265 rst.count = regrepeat(OPERAND(scan), rst.maxval); |
7 | 5266 if (got_int) |
180 | 5267 { |
5268 status = RA_FAIL; | |
5269 break; | |
5270 } | |
5271 if (rst.minval <= rst.maxval | |
5272 ? rst.count >= rst.minval : rst.count >= rst.maxval) | |
7 | 5273 { |
180 | 5274 /* It could match. Prepare for trying to match what |
5275 * follows. The code is below. Parameters are stored in | |
5276 * a regstar_T on the regstack. */ | |
212 | 5277 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp) |
189 | 5278 { |
5279 EMSG(_(e_maxmempat)); | |
5280 status = RA_FAIL; | |
5281 } | |
5282 else if (ga_grow(®stack, sizeof(regstar_T)) == FAIL) | |
180 | 5283 status = RA_FAIL; |
5284 else | |
7 | 5285 { |
180 | 5286 regstack.ga_len += sizeof(regstar_T); |
270 | 5287 rp = regstack_push(rst.minval <= rst.maxval |
233 | 5288 ? RS_STAR_LONG : RS_STAR_SHORT, scan); |
180 | 5289 if (rp == NULL) |
5290 status = RA_FAIL; | |
5291 else | |
7 | 5292 { |
180 | 5293 *(((regstar_T *)rp) - 1) = rst; |
5294 status = RA_BREAK; /* skip the restore bits */ | |
7 | 5295 } |
5296 } | |
5297 } | |
5298 else | |
180 | 5299 status = RA_NOMATCH; |
5300 | |
7 | 5301 } |
180 | 5302 break; |
7 | 5303 |
5304 case NOMATCH: | |
5305 case MATCH: | |
5306 case SUBPAT: | |
270 | 5307 rp = regstack_push(RS_NOMATCH, scan); |
180 | 5308 if (rp == NULL) |
5309 status = RA_FAIL; | |
5310 else | |
7 | 5311 { |
180 | 5312 rp->rs_no = op; |
233 | 5313 reg_save(&rp->rs_un.regsave, &backpos); |
180 | 5314 next = OPERAND(scan); |
5315 /* We continue and handle the result when done. */ | |
7 | 5316 } |
5317 break; | |
5318 | |
5319 case BEHIND: | |
5320 case NOBEHIND: | |
180 | 5321 /* Need a bit of room to store extra positions. */ |
212 | 5322 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp) |
189 | 5323 { |
5324 EMSG(_(e_maxmempat)); | |
5325 status = RA_FAIL; | |
5326 } | |
5327 else if (ga_grow(®stack, sizeof(regbehind_T)) == FAIL) | |
180 | 5328 status = RA_FAIL; |
5329 else | |
7 | 5330 { |
180 | 5331 regstack.ga_len += sizeof(regbehind_T); |
270 | 5332 rp = regstack_push(RS_BEHIND1, scan); |
180 | 5333 if (rp == NULL) |
5334 status = RA_FAIL; | |
5335 else | |
7 | 5336 { |
1579 | 5337 /* Need to save the subexpr to be able to restore them |
5338 * when there is a match but we don't use it. */ | |
5339 save_subexpr(((regbehind_T *)rp) - 1); | |
5340 | |
180 | 5341 rp->rs_no = op; |
233 | 5342 reg_save(&rp->rs_un.regsave, &backpos); |
180 | 5343 /* First try if what follows matches. If it does then we |
5344 * check the behind match by looping. */ | |
7 | 5345 } |
5346 } | |
180 | 5347 break; |
7 | 5348 |
5349 case BHPOS: | |
5350 if (REG_MULTI) | |
5351 { | |
5352 if (behind_pos.rs_u.pos.col != (colnr_T)(reginput - regline) | |
5353 || behind_pos.rs_u.pos.lnum != reglnum) | |
180 | 5354 status = RA_NOMATCH; |
7 | 5355 } |
5356 else if (behind_pos.rs_u.ptr != reginput) | |
180 | 5357 status = RA_NOMATCH; |
7 | 5358 break; |
5359 | |
5360 case NEWL: | |
1018 | 5361 if ((c != NUL || !REG_MULTI || reglnum > reg_maxline |
5362 || reg_line_lbr) && (c != '\n' || !reg_line_lbr)) | |
180 | 5363 status = RA_NOMATCH; |
5364 else if (reg_line_lbr) | |
7 | 5365 ADVANCE_REGINPUT(); |
5366 else | |
5367 reg_nextline(); | |
5368 break; | |
5369 | |
5370 case END: | |
180 | 5371 status = RA_MATCH; /* Success! */ |
5372 break; | |
7 | 5373 |
5374 default: | |
5375 EMSG(_(e_re_corr)); | |
5376 #ifdef DEBUG | |
5377 printf("Illegal op code %d\n", op); | |
5378 #endif | |
180 | 5379 status = RA_FAIL; |
5380 break; | |
7 | 5381 } |
5382 } | |
5383 | |
180 | 5384 /* If we can't continue sequentially, break the inner loop. */ |
5385 if (status != RA_CONT) | |
5386 break; | |
5387 | |
5388 /* Continue in inner loop, advance to next item. */ | |
7 | 5389 scan = next; |
180 | 5390 |
5391 } /* end of inner loop */ | |
7 | 5392 |
5393 /* | |
180 | 5394 * If there is something on the regstack execute the code for the state. |
233 | 5395 * If the state is popped then loop and use the older state. |
7 | 5396 */ |
180 | 5397 while (regstack.ga_len > 0 && status != RA_FAIL) |
5398 { | |
5399 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len) - 1; | |
5400 switch (rp->rs_state) | |
5401 { | |
5402 case RS_NOPEN: | |
5403 /* Result is passed on as-is, simply pop the state. */ | |
270 | 5404 regstack_pop(&scan); |
180 | 5405 break; |
5406 | |
5407 case RS_MOPEN: | |
5408 /* Pop the state. Restore pointers when there is no match. */ | |
5409 if (status == RA_NOMATCH) | |
5410 restore_se(&rp->rs_un.sesave, ®_startpos[rp->rs_no], | |
5411 ®_startp[rp->rs_no]); | |
270 | 5412 regstack_pop(&scan); |
180 | 5413 break; |
5414 | |
5415 #ifdef FEAT_SYN_HL | |
5416 case RS_ZOPEN: | |
5417 /* Pop the state. Restore pointers when there is no match. */ | |
5418 if (status == RA_NOMATCH) | |
5419 restore_se(&rp->rs_un.sesave, ®_startzpos[rp->rs_no], | |
5420 ®_startzp[rp->rs_no]); | |
270 | 5421 regstack_pop(&scan); |
180 | 5422 break; |
5423 #endif | |
5424 | |
5425 case RS_MCLOSE: | |
5426 /* Pop the state. Restore pointers when there is no match. */ | |
5427 if (status == RA_NOMATCH) | |
5428 restore_se(&rp->rs_un.sesave, ®_endpos[rp->rs_no], | |
5429 ®_endp[rp->rs_no]); | |
270 | 5430 regstack_pop(&scan); |
180 | 5431 break; |
5432 | |
5433 #ifdef FEAT_SYN_HL | |
5434 case RS_ZCLOSE: | |
5435 /* Pop the state. Restore pointers when there is no match. */ | |
5436 if (status == RA_NOMATCH) | |
5437 restore_se(&rp->rs_un.sesave, ®_endzpos[rp->rs_no], | |
5438 ®_endzp[rp->rs_no]); | |
270 | 5439 regstack_pop(&scan); |
180 | 5440 break; |
5441 #endif | |
5442 | |
5443 case RS_BRANCH: | |
5444 if (status == RA_MATCH) | |
5445 /* this branch matched, use it */ | |
270 | 5446 regstack_pop(&scan); |
180 | 5447 else |
5448 { | |
5449 if (status != RA_BREAK) | |
5450 { | |
5451 /* After a non-matching branch: try next one. */ | |
233 | 5452 reg_restore(&rp->rs_un.regsave, &backpos); |
180 | 5453 scan = rp->rs_scan; |
5454 } | |
5455 if (scan == NULL || OP(scan) != BRANCH) | |
5456 { | |
5457 /* no more branches, didn't find a match */ | |
5458 status = RA_NOMATCH; | |
270 | 5459 regstack_pop(&scan); |
180 | 5460 } |
5461 else | |
5462 { | |
5463 /* Prepare to try a branch. */ | |
5464 rp->rs_scan = regnext(scan); | |
233 | 5465 reg_save(&rp->rs_un.regsave, &backpos); |
180 | 5466 scan = OPERAND(scan); |
5467 } | |
5468 } | |
5469 break; | |
5470 | |
5471 case RS_BRCPLX_MORE: | |
5472 /* Pop the state. Restore pointers when there is no match. */ | |
5473 if (status == RA_NOMATCH) | |
5474 { | |
233 | 5475 reg_restore(&rp->rs_un.regsave, &backpos); |
180 | 5476 --brace_count[rp->rs_no]; /* decrement match count */ |
5477 } | |
270 | 5478 regstack_pop(&scan); |
180 | 5479 break; |
5480 | |
5481 case RS_BRCPLX_LONG: | |
5482 /* Pop the state. Restore pointers when there is no match. */ | |
5483 if (status == RA_NOMATCH) | |
5484 { | |
5485 /* There was no match, but we did find enough matches. */ | |
233 | 5486 reg_restore(&rp->rs_un.regsave, &backpos); |
180 | 5487 --brace_count[rp->rs_no]; |
5488 /* continue with the items after "\{}" */ | |
5489 status = RA_CONT; | |
5490 } | |
270 | 5491 regstack_pop(&scan); |
180 | 5492 if (status == RA_CONT) |
5493 scan = regnext(scan); | |
5494 break; | |
5495 | |
5496 case RS_BRCPLX_SHORT: | |
5497 /* Pop the state. Restore pointers when there is no match. */ | |
5498 if (status == RA_NOMATCH) | |
5499 /* There was no match, try to match one more item. */ | |
233 | 5500 reg_restore(&rp->rs_un.regsave, &backpos); |
270 | 5501 regstack_pop(&scan); |
180 | 5502 if (status == RA_NOMATCH) |
5503 { | |
5504 scan = OPERAND(scan); | |
5505 status = RA_CONT; | |
5506 } | |
5507 break; | |
5508 | |
5509 case RS_NOMATCH: | |
5510 /* Pop the state. If the operand matches for NOMATCH or | |
5511 * doesn't match for MATCH/SUBPAT, we fail. Otherwise backup, | |
5512 * except for SUBPAT, and continue with the next item. */ | |
5513 if (status == (rp->rs_no == NOMATCH ? RA_MATCH : RA_NOMATCH)) | |
5514 status = RA_NOMATCH; | |
5515 else | |
5516 { | |
5517 status = RA_CONT; | |
233 | 5518 if (rp->rs_no != SUBPAT) /* zero-width */ |
5519 reg_restore(&rp->rs_un.regsave, &backpos); | |
180 | 5520 } |
270 | 5521 regstack_pop(&scan); |
180 | 5522 if (status == RA_CONT) |
5523 scan = regnext(scan); | |
5524 break; | |
5525 | |
5526 case RS_BEHIND1: | |
5527 if (status == RA_NOMATCH) | |
5528 { | |
270 | 5529 regstack_pop(&scan); |
180 | 5530 regstack.ga_len -= sizeof(regbehind_T); |
5531 } | |
5532 else | |
5533 { | |
5534 /* The stuff after BEHIND/NOBEHIND matches. Now try if | |
5535 * the behind part does (not) match before the current | |
5536 * position in the input. This must be done at every | |
5537 * position in the input and checking if the match ends at | |
5538 * the current position. */ | |
5539 | |
5540 /* save the position after the found match for next */ | |
233 | 5541 reg_save(&(((regbehind_T *)rp) - 1)->save_after, &backpos); |
180 | 5542 |
4579
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5543 /* Start looking for a match with operand at the current |
1209 | 5544 * position. Go back one character until we find the |
180 | 5545 * result, hitting the start of the line or the previous |
5546 * line (for multi-line matching). | |
5547 * Set behind_pos to where the match should end, BHPOS | |
5548 * will match it. Save the current value. */ | |
5549 (((regbehind_T *)rp) - 1)->save_behind = behind_pos; | |
5550 behind_pos = rp->rs_un.regsave; | |
5551 | |
5552 rp->rs_state = RS_BEHIND2; | |
5553 | |
233 | 5554 reg_restore(&rp->rs_un.regsave, &backpos); |
4579
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5555 scan = OPERAND(rp->rs_scan) + 4; |
180 | 5556 } |
5557 break; | |
5558 | |
5559 case RS_BEHIND2: | |
5560 /* | |
5561 * Looping for BEHIND / NOBEHIND match. | |
5562 */ | |
5563 if (status == RA_MATCH && reg_save_equal(&behind_pos)) | |
5564 { | |
5565 /* found a match that ends where "next" started */ | |
5566 behind_pos = (((regbehind_T *)rp) - 1)->save_behind; | |
5567 if (rp->rs_no == BEHIND) | |
233 | 5568 reg_restore(&(((regbehind_T *)rp) - 1)->save_after, |
5569 &backpos); | |
180 | 5570 else |
1579 | 5571 { |
5572 /* But we didn't want a match. Need to restore the | |
5573 * subexpr, because what follows matched, so they have | |
5574 * been set. */ | |
180 | 5575 status = RA_NOMATCH; |
1579 | 5576 restore_subexpr(((regbehind_T *)rp) - 1); |
5577 } | |
270 | 5578 regstack_pop(&scan); |
180 | 5579 regstack.ga_len -= sizeof(regbehind_T); |
5580 } | |
5581 else | |
5582 { | |
4579
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5583 long limit; |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5584 |
1579 | 5585 /* No match or a match that doesn't end where we want it: Go |
5586 * back one character. May go to previous line once. */ | |
180 | 5587 no = OK; |
4579
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5588 limit = OPERAND_MIN(rp->rs_scan); |
180 | 5589 if (REG_MULTI) |
5590 { | |
4682
2f51ee8825db
updated for version 7.3.1088
Bram Moolenaar <bram@vim.org>
parents:
4679
diff
changeset
|
5591 if (limit > 0 |
2f51ee8825db
updated for version 7.3.1088
Bram Moolenaar <bram@vim.org>
parents:
4679
diff
changeset
|
5592 && ((rp->rs_un.regsave.rs_u.pos.lnum |
2f51ee8825db
updated for version 7.3.1088
Bram Moolenaar <bram@vim.org>
parents:
4679
diff
changeset
|
5593 < behind_pos.rs_u.pos.lnum |
2f51ee8825db
updated for version 7.3.1088
Bram Moolenaar <bram@vim.org>
parents:
4679
diff
changeset
|
5594 ? (colnr_T)STRLEN(regline) |
2f51ee8825db
updated for version 7.3.1088
Bram Moolenaar <bram@vim.org>
parents:
4679
diff
changeset
|
5595 : behind_pos.rs_u.pos.col) |
2f51ee8825db
updated for version 7.3.1088
Bram Moolenaar <bram@vim.org>
parents:
4679
diff
changeset
|
5596 - 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
|
5597 no = FAIL; |
2f51ee8825db
updated for version 7.3.1088
Bram Moolenaar <bram@vim.org>
parents:
4679
diff
changeset
|
5598 else if (rp->rs_un.regsave.rs_u.pos.col == 0) |
180 | 5599 { |
5600 if (rp->rs_un.regsave.rs_u.pos.lnum | |
5601 < behind_pos.rs_u.pos.lnum | |
5602 || reg_getline( | |
5603 --rp->rs_un.regsave.rs_u.pos.lnum) | |
5604 == NULL) | |
5605 no = FAIL; | |
5606 else | |
5607 { | |
233 | 5608 reg_restore(&rp->rs_un.regsave, &backpos); |
180 | 5609 rp->rs_un.regsave.rs_u.pos.col = |
5610 (colnr_T)STRLEN(regline); | |
5611 } | |
5612 } | |
5613 else | |
4579
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5614 { |
4176 | 5615 #ifdef FEAT_MBYTE |
4579
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5616 if (has_mbyte) |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5617 rp->rs_un.regsave.rs_u.pos.col -= |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5618 (*mb_head_off)(regline, regline |
4176 | 5619 + 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
|
5620 else |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5621 #endif |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5622 --rp->rs_un.regsave.rs_u.pos.col; |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5623 } |
180 | 5624 } |
5625 else | |
5626 { | |
5627 if (rp->rs_un.regsave.rs_u.ptr == regline) | |
5628 no = FAIL; | |
5629 else | |
4579
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5630 { |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5631 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
|
5632 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
|
5633 - rp->rs_un.regsave.rs_u.ptr) > limit) |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5634 no = FAIL; |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5635 } |
180 | 5636 } |
5637 if (no == OK) | |
5638 { | |
5639 /* Advanced, prepare for finding match again. */ | |
233 | 5640 reg_restore(&rp->rs_un.regsave, &backpos); |
4579
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5641 scan = OPERAND(rp->rs_scan) + 4; |
1579 | 5642 if (status == RA_MATCH) |
5643 { | |
5644 /* We did match, so subexpr may have been changed, | |
5645 * need to restore them for the next try. */ | |
5646 status = RA_NOMATCH; | |
5647 restore_subexpr(((regbehind_T *)rp) - 1); | |
5648 } | |
180 | 5649 } |
5650 else | |
5651 { | |
5652 /* Can't advance. For NOBEHIND that's a match. */ | |
5653 behind_pos = (((regbehind_T *)rp) - 1)->save_behind; | |
5654 if (rp->rs_no == NOBEHIND) | |
5655 { | |
233 | 5656 reg_restore(&(((regbehind_T *)rp) - 1)->save_after, |
5657 &backpos); | |
180 | 5658 status = RA_MATCH; |
5659 } | |
5660 else | |
1579 | 5661 { |
5662 /* We do want a proper match. Need to restore the | |
5663 * subexpr if we had a match, because they may have | |
5664 * been set. */ | |
5665 if (status == RA_MATCH) | |
5666 { | |
5667 status = RA_NOMATCH; | |
5668 restore_subexpr(((regbehind_T *)rp) - 1); | |
5669 } | |
5670 } | |
270 | 5671 regstack_pop(&scan); |
180 | 5672 regstack.ga_len -= sizeof(regbehind_T); |
5673 } | |
5674 } | |
5675 break; | |
5676 | |
5677 case RS_STAR_LONG: | |
5678 case RS_STAR_SHORT: | |
5679 { | |
5680 regstar_T *rst = ((regstar_T *)rp) - 1; | |
5681 | |
5682 if (status == RA_MATCH) | |
5683 { | |
270 | 5684 regstack_pop(&scan); |
180 | 5685 regstack.ga_len -= sizeof(regstar_T); |
5686 break; | |
5687 } | |
5688 | |
5689 /* Tried once already, restore input pointers. */ | |
5690 if (status != RA_BREAK) | |
233 | 5691 reg_restore(&rp->rs_un.regsave, &backpos); |
180 | 5692 |
5693 /* Repeat until we found a position where it could match. */ | |
5694 for (;;) | |
5695 { | |
5696 if (status != RA_BREAK) | |
5697 { | |
5698 /* Tried first position already, advance. */ | |
5699 if (rp->rs_state == RS_STAR_LONG) | |
5700 { | |
685 | 5701 /* Trying for longest match, but couldn't or |
5702 * didn't match -- back up one char. */ | |
180 | 5703 if (--rst->count < rst->minval) |
5704 break; | |
5705 if (reginput == regline) | |
5706 { | |
5707 /* backup to last char of previous line */ | |
5708 --reglnum; | |
5709 regline = reg_getline(reglnum); | |
5710 /* Just in case regrepeat() didn't count | |
5711 * right. */ | |
5712 if (regline == NULL) | |
5713 break; | |
5714 reginput = regline + STRLEN(regline); | |
5715 fast_breakcheck(); | |
5716 } | |
5717 else | |
5718 mb_ptr_back(regline, reginput); | |
5719 } | |
5720 else | |
5721 { | |
5722 /* Range is backwards, use shortest match first. | |
5723 * Careful: maxval and minval are exchanged! | |
5724 * Couldn't or didn't match: try advancing one | |
5725 * char. */ | |
5726 if (rst->count == rst->minval | |
5727 || regrepeat(OPERAND(rp->rs_scan), 1L) == 0) | |
5728 break; | |
5729 ++rst->count; | |
5730 } | |
5731 if (got_int) | |
5732 break; | |
5733 } | |
5734 else | |
5735 status = RA_NOMATCH; | |
5736 | |
5737 /* If it could match, try it. */ | |
5738 if (rst->nextb == NUL || *reginput == rst->nextb | |
5739 || *reginput == rst->nextb_ic) | |
5740 { | |
233 | 5741 reg_save(&rp->rs_un.regsave, &backpos); |
180 | 5742 scan = regnext(rp->rs_scan); |
5743 status = RA_CONT; | |
5744 break; | |
5745 } | |
5746 } | |
5747 if (status != RA_CONT) | |
5748 { | |
5749 /* Failed. */ | |
270 | 5750 regstack_pop(&scan); |
180 | 5751 regstack.ga_len -= sizeof(regstar_T); |
5752 status = RA_NOMATCH; | |
5753 } | |
5754 } | |
5755 break; | |
5756 } | |
5757 | |
685 | 5758 /* If we want to continue the inner loop or didn't pop a state |
5759 * continue matching loop */ | |
180 | 5760 if (status == RA_CONT || rp == (regitem_T *) |
5761 ((char *)regstack.ga_data + regstack.ga_len) - 1) | |
5762 break; | |
5763 } | |
5764 | |
189 | 5765 /* May need to continue with the inner loop, starting at "scan". */ |
180 | 5766 if (status == RA_CONT) |
5767 continue; | |
5768 | |
5769 /* | |
5770 * If the regstack is empty or something failed we are done. | |
5771 */ | |
5772 if (regstack.ga_len == 0 || status == RA_FAIL) | |
5773 { | |
5774 if (scan == NULL) | |
5775 { | |
5776 /* | |
5777 * We get here only if there's trouble -- normally "case END" is | |
5778 * the terminating point. | |
5779 */ | |
5780 EMSG(_(e_re_corr)); | |
7 | 5781 #ifdef DEBUG |
180 | 5782 printf("Premature EOL\n"); |
7 | 5783 #endif |
180 | 5784 } |
189 | 5785 if (status == RA_FAIL) |
5786 got_int = TRUE; | |
180 | 5787 return (status == RA_MATCH); |
5788 } | |
5789 | |
5790 } /* End of loop until the regstack is empty. */ | |
5791 | |
5792 /* NOTREACHED */ | |
5793 } | |
5794 | |
5795 /* | |
5796 * Push an item onto the regstack. | |
5797 * Returns pointer to new item. Returns NULL when out of memory. | |
5798 */ | |
5799 static regitem_T * | |
270 | 5800 regstack_push(state, scan) |
180 | 5801 regstate_T state; |
5802 char_u *scan; | |
5803 { | |
5804 regitem_T *rp; | |
5805 | |
270 | 5806 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp) |
189 | 5807 { |
5808 EMSG(_(e_maxmempat)); | |
5809 return NULL; | |
5810 } | |
270 | 5811 if (ga_grow(®stack, sizeof(regitem_T)) == FAIL) |
180 | 5812 return NULL; |
5813 | |
270 | 5814 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len); |
180 | 5815 rp->rs_state = state; |
5816 rp->rs_scan = scan; | |
5817 | |
270 | 5818 regstack.ga_len += sizeof(regitem_T); |
180 | 5819 return rp; |
5820 } | |
5821 | |
5822 /* | |
5823 * Pop an item from the regstack. | |
5824 */ | |
5825 static void | |
270 | 5826 regstack_pop(scan) |
180 | 5827 char_u **scan; |
5828 { | |
5829 regitem_T *rp; | |
5830 | |
270 | 5831 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len) - 1; |
180 | 5832 *scan = rp->rs_scan; |
5833 | |
270 | 5834 regstack.ga_len -= sizeof(regitem_T); |
7 | 5835 } |
5836 | |
5837 /* | |
5838 * regrepeat - repeatedly match something simple, return how many. | |
5839 * Advances reginput (and reglnum) to just after the matched chars. | |
5840 */ | |
5841 static int | |
5842 regrepeat(p, maxcount) | |
5843 char_u *p; | |
5844 long maxcount; /* maximum number of matches allowed */ | |
5845 { | |
5846 long count = 0; | |
5847 char_u *scan; | |
5848 char_u *opnd; | |
5849 int mask; | |
5850 int testval = 0; | |
5851 | |
5852 scan = reginput; /* Make local copy of reginput for speed. */ | |
5853 opnd = OPERAND(p); | |
5854 switch (OP(p)) | |
5855 { | |
5856 case ANY: | |
5857 case ANY + ADD_NL: | |
5858 while (count < maxcount) | |
5859 { | |
5860 /* Matching anything means we continue until end-of-line (or | |
5861 * end-of-file for ANY + ADD_NL), only limited by maxcount. */ | |
5862 while (*scan != NUL && count < maxcount) | |
5863 { | |
5864 ++count; | |
39 | 5865 mb_ptr_adv(scan); |
7 | 5866 } |
1018 | 5867 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline |
5868 || reg_line_lbr || count == maxcount) | |
7 | 5869 break; |
5870 ++count; /* count the line-break */ | |
5871 reg_nextline(); | |
5872 scan = reginput; | |
5873 if (got_int) | |
5874 break; | |
5875 } | |
5876 break; | |
5877 | |
5878 case IDENT: | |
5879 case IDENT + ADD_NL: | |
5880 testval = TRUE; | |
5881 /*FALLTHROUGH*/ | |
5882 case SIDENT: | |
5883 case SIDENT + ADD_NL: | |
5884 while (count < maxcount) | |
5885 { | |
4466 | 5886 if (vim_isIDc(PTR2CHAR(scan)) && (testval || !VIM_ISDIGIT(*scan))) |
7 | 5887 { |
39 | 5888 mb_ptr_adv(scan); |
7 | 5889 } |
5890 else if (*scan == NUL) | |
5891 { | |
1018 | 5892 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline |
5893 || reg_line_lbr) | |
7 | 5894 break; |
5895 reg_nextline(); | |
5896 scan = reginput; | |
5897 if (got_int) | |
5898 break; | |
5899 } | |
5900 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p))) | |
5901 ++scan; | |
5902 else | |
5903 break; | |
5904 ++count; | |
5905 } | |
5906 break; | |
5907 | |
5908 case KWORD: | |
5909 case KWORD + ADD_NL: | |
5910 testval = TRUE; | |
5911 /*FALLTHROUGH*/ | |
5912 case SKWORD: | |
5913 case SKWORD + ADD_NL: | |
5914 while (count < maxcount) | |
5915 { | |
4069 | 5916 if (vim_iswordp_buf(scan, reg_buf) |
5917 && (testval || !VIM_ISDIGIT(*scan))) | |
7 | 5918 { |
39 | 5919 mb_ptr_adv(scan); |
7 | 5920 } |
5921 else if (*scan == NUL) | |
5922 { | |
1018 | 5923 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline |
5924 || reg_line_lbr) | |
7 | 5925 break; |
5926 reg_nextline(); | |
5927 scan = reginput; | |
5928 if (got_int) | |
5929 break; | |
5930 } | |
5931 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p))) | |
5932 ++scan; | |
5933 else | |
5934 break; | |
5935 ++count; | |
5936 } | |
5937 break; | |
5938 | |
5939 case FNAME: | |
5940 case FNAME + ADD_NL: | |
5941 testval = TRUE; | |
5942 /*FALLTHROUGH*/ | |
5943 case SFNAME: | |
5944 case SFNAME + ADD_NL: | |
5945 while (count < maxcount) | |
5946 { | |
4466 | 5947 if (vim_isfilec(PTR2CHAR(scan)) && (testval || !VIM_ISDIGIT(*scan))) |
7 | 5948 { |
39 | 5949 mb_ptr_adv(scan); |
7 | 5950 } |
5951 else if (*scan == NUL) | |
5952 { | |
1018 | 5953 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline |
5954 || reg_line_lbr) | |
7 | 5955 break; |
5956 reg_nextline(); | |
5957 scan = reginput; | |
5958 if (got_int) | |
5959 break; | |
5960 } | |
5961 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p))) | |
5962 ++scan; | |
5963 else | |
5964 break; | |
5965 ++count; | |
5966 } | |
5967 break; | |
5968 | |
5969 case PRINT: | |
5970 case PRINT + ADD_NL: | |
5971 testval = TRUE; | |
5972 /*FALLTHROUGH*/ | |
5973 case SPRINT: | |
5974 case SPRINT + ADD_NL: | |
5975 while (count < maxcount) | |
5976 { | |
5977 if (*scan == NUL) | |
5978 { | |
1018 | 5979 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline |
5980 || reg_line_lbr) | |
7 | 5981 break; |
5982 reg_nextline(); | |
5983 scan = reginput; | |
5984 if (got_int) | |
5985 break; | |
5986 } | |
5987 else if (ptr2cells(scan) == 1 && (testval || !VIM_ISDIGIT(*scan))) | |
5988 { | |
39 | 5989 mb_ptr_adv(scan); |
7 | 5990 } |
5991 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p))) | |
5992 ++scan; | |
5993 else | |
5994 break; | |
5995 ++count; | |
5996 } | |
5997 break; | |
5998 | |
5999 case WHITE: | |
6000 case WHITE + ADD_NL: | |
6001 testval = mask = RI_WHITE; | |
6002 do_class: | |
6003 while (count < maxcount) | |
6004 { | |
6005 #ifdef FEAT_MBYTE | |
6006 int l; | |
6007 #endif | |
6008 if (*scan == NUL) | |
6009 { | |
1018 | 6010 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline |
6011 || reg_line_lbr) | |
7 | 6012 break; |
6013 reg_nextline(); | |
6014 scan = reginput; | |
6015 if (got_int) | |
6016 break; | |
6017 } | |
6018 #ifdef FEAT_MBYTE | |
474 | 6019 else if (has_mbyte && (l = (*mb_ptr2len)(scan)) > 1) |
7 | 6020 { |
6021 if (testval != 0) | |
6022 break; | |
6023 scan += l; | |
6024 } | |
6025 #endif | |
6026 else if ((class_tab[*scan] & mask) == testval) | |
6027 ++scan; | |
6028 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p))) | |
6029 ++scan; | |
6030 else | |
6031 break; | |
6032 ++count; | |
6033 } | |
6034 break; | |
6035 | |
6036 case NWHITE: | |
6037 case NWHITE + ADD_NL: | |
6038 mask = RI_WHITE; | |
6039 goto do_class; | |
6040 case DIGIT: | |
6041 case DIGIT + ADD_NL: | |
6042 testval = mask = RI_DIGIT; | |
6043 goto do_class; | |
6044 case NDIGIT: | |
6045 case NDIGIT + ADD_NL: | |
6046 mask = RI_DIGIT; | |
6047 goto do_class; | |
6048 case HEX: | |
6049 case HEX + ADD_NL: | |
6050 testval = mask = RI_HEX; | |
6051 goto do_class; | |
6052 case NHEX: | |
6053 case NHEX + ADD_NL: | |
6054 mask = RI_HEX; | |
6055 goto do_class; | |
6056 case OCTAL: | |
6057 case OCTAL + ADD_NL: | |
6058 testval = mask = RI_OCTAL; | |
6059 goto do_class; | |
6060 case NOCTAL: | |
6061 case NOCTAL + ADD_NL: | |
6062 mask = RI_OCTAL; | |
6063 goto do_class; | |
6064 case WORD: | |
6065 case WORD + ADD_NL: | |
6066 testval = mask = RI_WORD; | |
6067 goto do_class; | |
6068 case NWORD: | |
6069 case NWORD + ADD_NL: | |
6070 mask = RI_WORD; | |
6071 goto do_class; | |
6072 case HEAD: | |
6073 case HEAD + ADD_NL: | |
6074 testval = mask = RI_HEAD; | |
6075 goto do_class; | |
6076 case NHEAD: | |
6077 case NHEAD + ADD_NL: | |
6078 mask = RI_HEAD; | |
6079 goto do_class; | |
6080 case ALPHA: | |
6081 case ALPHA + ADD_NL: | |
6082 testval = mask = RI_ALPHA; | |
6083 goto do_class; | |
6084 case NALPHA: | |
6085 case NALPHA + ADD_NL: | |
6086 mask = RI_ALPHA; | |
6087 goto do_class; | |
6088 case LOWER: | |
6089 case LOWER + ADD_NL: | |
6090 testval = mask = RI_LOWER; | |
6091 goto do_class; | |
6092 case NLOWER: | |
6093 case NLOWER + ADD_NL: | |
6094 mask = RI_LOWER; | |
6095 goto do_class; | |
6096 case UPPER: | |
6097 case UPPER + ADD_NL: | |
6098 testval = mask = RI_UPPER; | |
6099 goto do_class; | |
6100 case NUPPER: | |
6101 case NUPPER + ADD_NL: | |
6102 mask = RI_UPPER; | |
6103 goto do_class; | |
6104 | |
6105 case EXACTLY: | |
6106 { | |
6107 int cu, cl; | |
6108 | |
6109 /* This doesn't do a multi-byte character, because a MULTIBYTECODE | |
1347 | 6110 * would have been used for it. It does handle single-byte |
6111 * characters, such as latin1. */ | |
7 | 6112 if (ireg_ic) |
6113 { | |
1347 | 6114 cu = MB_TOUPPER(*opnd); |
6115 cl = MB_TOLOWER(*opnd); | |
7 | 6116 while (count < maxcount && (*scan == cu || *scan == cl)) |
6117 { | |
6118 count++; | |
6119 scan++; | |
6120 } | |
6121 } | |
6122 else | |
6123 { | |
6124 cu = *opnd; | |
6125 while (count < maxcount && *scan == cu) | |
6126 { | |
6127 count++; | |
6128 scan++; | |
6129 } | |
6130 } | |
6131 break; | |
6132 } | |
6133 | |
6134 #ifdef FEAT_MBYTE | |
6135 case MULTIBYTECODE: | |
6136 { | |
6137 int i, len, cf = 0; | |
6138 | |
6139 /* Safety check (just in case 'encoding' was changed since | |
6140 * compiling the program). */ | |
474 | 6141 if ((len = (*mb_ptr2len)(opnd)) > 1) |
7 | 6142 { |
6143 if (ireg_ic && enc_utf8) | |
6144 cf = utf_fold(utf_ptr2char(opnd)); | |
6145 while (count < maxcount) | |
6146 { | |
6147 for (i = 0; i < len; ++i) | |
6148 if (opnd[i] != scan[i]) | |
6149 break; | |
6150 if (i < len && (!ireg_ic || !enc_utf8 | |
6151 || utf_fold(utf_ptr2char(scan)) != cf)) | |
6152 break; | |
6153 scan += len; | |
6154 ++count; | |
6155 } | |
6156 } | |
6157 } | |
6158 break; | |
6159 #endif | |
6160 | |
6161 case ANYOF: | |
6162 case ANYOF + ADD_NL: | |
6163 testval = TRUE; | |
6164 /*FALLTHROUGH*/ | |
6165 | |
6166 case ANYBUT: | |
6167 case ANYBUT + ADD_NL: | |
6168 while (count < maxcount) | |
6169 { | |
6170 #ifdef FEAT_MBYTE | |
6171 int len; | |
6172 #endif | |
6173 if (*scan == NUL) | |
6174 { | |
1018 | 6175 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline |
6176 || reg_line_lbr) | |
7 | 6177 break; |
6178 reg_nextline(); | |
6179 scan = reginput; | |
6180 if (got_int) | |
6181 break; | |
6182 } | |
6183 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p))) | |
6184 ++scan; | |
6185 #ifdef FEAT_MBYTE | |
474 | 6186 else if (has_mbyte && (len = (*mb_ptr2len)(scan)) > 1) |
7 | 6187 { |
6188 if ((cstrchr(opnd, (*mb_ptr2char)(scan)) == NULL) == testval) | |
6189 break; | |
6190 scan += len; | |
6191 } | |
6192 #endif | |
6193 else | |
6194 { | |
6195 if ((cstrchr(opnd, *scan) == NULL) == testval) | |
6196 break; | |
6197 ++scan; | |
6198 } | |
6199 ++count; | |
6200 } | |
6201 break; | |
6202 | |
6203 case NEWL: | |
6204 while (count < maxcount | |
1018 | 6205 && ((*scan == NUL && reglnum <= reg_maxline && !reg_line_lbr |
6206 && REG_MULTI) || (*scan == '\n' && reg_line_lbr))) | |
7 | 6207 { |
6208 count++; | |
6209 if (reg_line_lbr) | |
6210 ADVANCE_REGINPUT(); | |
6211 else | |
6212 reg_nextline(); | |
6213 scan = reginput; | |
6214 if (got_int) | |
6215 break; | |
6216 } | |
6217 break; | |
6218 | |
6219 default: /* Oh dear. Called inappropriately. */ | |
6220 EMSG(_(e_re_corr)); | |
6221 #ifdef DEBUG | |
6222 printf("Called regrepeat with op code %d\n", OP(p)); | |
6223 #endif | |
6224 break; | |
6225 } | |
6226 | |
6227 reginput = scan; | |
6228 | |
6229 return (int)count; | |
6230 } | |
6231 | |
6232 /* | |
6233 * regnext - dig the "next" pointer out of a node | |
2010 | 6234 * Returns NULL when calculating size, when there is no next item and when |
6235 * there is an error. | |
7 | 6236 */ |
6237 static char_u * | |
6238 regnext(p) | |
6239 char_u *p; | |
6240 { | |
6241 int offset; | |
6242 | |
2010 | 6243 if (p == JUST_CALC_SIZE || reg_toolong) |
7 | 6244 return NULL; |
6245 | |
6246 offset = NEXT(p); | |
6247 if (offset == 0) | |
6248 return NULL; | |
6249 | |
233 | 6250 if (OP(p) == BACK) |
7 | 6251 return p - offset; |
6252 else | |
6253 return p + offset; | |
6254 } | |
6255 | |
6256 /* | |
6257 * Check the regexp program for its magic number. | |
6258 * Return TRUE if it's wrong. | |
6259 */ | |
6260 static int | |
6261 prog_magic_wrong() | |
6262 { | |
4444 | 6263 regprog_T *prog; |
6264 | |
6265 prog = REG_MULTI ? reg_mmatch->regprog : reg_match->regprog; | |
6266 if (prog->engine == &nfa_regengine) | |
6267 /* For NFA matcher we don't check the magic */ | |
6268 return FALSE; | |
6269 | |
6270 if (UCHARAT(((bt_regprog_T *)prog)->program) != REGMAGIC) | |
7 | 6271 { |
6272 EMSG(_(e_re_corr)); | |
6273 return TRUE; | |
6274 } | |
6275 return FALSE; | |
6276 } | |
6277 | |
6278 /* | |
6279 * Cleanup the subexpressions, if this wasn't done yet. | |
6280 * This construction is used to clear the subexpressions only when they are | |
6281 * used (to increase speed). | |
6282 */ | |
6283 static void | |
6284 cleanup_subexpr() | |
6285 { | |
6286 if (need_clear_subexpr) | |
6287 { | |
6288 if (REG_MULTI) | |
6289 { | |
6290 /* Use 0xff to set lnum to -1 */ | |
6291 vim_memset(reg_startpos, 0xff, sizeof(lpos_T) * NSUBEXP); | |
6292 vim_memset(reg_endpos, 0xff, sizeof(lpos_T) * NSUBEXP); | |
6293 } | |
6294 else | |
6295 { | |
6296 vim_memset(reg_startp, 0, sizeof(char_u *) * NSUBEXP); | |
6297 vim_memset(reg_endp, 0, sizeof(char_u *) * NSUBEXP); | |
6298 } | |
6299 need_clear_subexpr = FALSE; | |
6300 } | |
6301 } | |
6302 | |
6303 #ifdef FEAT_SYN_HL | |
6304 static void | |
6305 cleanup_zsubexpr() | |
6306 { | |
6307 if (need_clear_zsubexpr) | |
6308 { | |
6309 if (REG_MULTI) | |
6310 { | |
6311 /* Use 0xff to set lnum to -1 */ | |
6312 vim_memset(reg_startzpos, 0xff, sizeof(lpos_T) * NSUBEXP); | |
6313 vim_memset(reg_endzpos, 0xff, sizeof(lpos_T) * NSUBEXP); | |
6314 } | |
6315 else | |
6316 { | |
6317 vim_memset(reg_startzp, 0, sizeof(char_u *) * NSUBEXP); | |
6318 vim_memset(reg_endzp, 0, sizeof(char_u *) * NSUBEXP); | |
6319 } | |
6320 need_clear_zsubexpr = FALSE; | |
6321 } | |
6322 } | |
6323 #endif | |
6324 | |
6325 /* | |
1579 | 6326 * Save the current subexpr to "bp", so that they can be restored |
6327 * later by restore_subexpr(). | |
6328 */ | |
6329 static void | |
6330 save_subexpr(bp) | |
6331 regbehind_T *bp; | |
6332 { | |
6333 int i; | |
6334 | |
1602 | 6335 /* When "need_clear_subexpr" is set we don't need to save the values, only |
6336 * remember that this flag needs to be set again when restoring. */ | |
6337 bp->save_need_clear_subexpr = need_clear_subexpr; | |
6338 if (!need_clear_subexpr) | |
1579 | 6339 { |
1602 | 6340 for (i = 0; i < NSUBEXP; ++i) |
1579 | 6341 { |
1602 | 6342 if (REG_MULTI) |
6343 { | |
6344 bp->save_start[i].se_u.pos = reg_startpos[i]; | |
6345 bp->save_end[i].se_u.pos = reg_endpos[i]; | |
6346 } | |
6347 else | |
6348 { | |
6349 bp->save_start[i].se_u.ptr = reg_startp[i]; | |
6350 bp->save_end[i].se_u.ptr = reg_endp[i]; | |
6351 } | |
1579 | 6352 } |
6353 } | |
6354 } | |
6355 | |
6356 /* | |
6357 * Restore the subexpr from "bp". | |
6358 */ | |
6359 static void | |
6360 restore_subexpr(bp) | |
6361 regbehind_T *bp; | |
6362 { | |
6363 int i; | |
6364 | |
1602 | 6365 /* Only need to restore saved values when they are not to be cleared. */ |
6366 need_clear_subexpr = bp->save_need_clear_subexpr; | |
6367 if (!need_clear_subexpr) | |
1579 | 6368 { |
1602 | 6369 for (i = 0; i < NSUBEXP; ++i) |
1579 | 6370 { |
1602 | 6371 if (REG_MULTI) |
6372 { | |
6373 reg_startpos[i] = bp->save_start[i].se_u.pos; | |
6374 reg_endpos[i] = bp->save_end[i].se_u.pos; | |
6375 } | |
6376 else | |
6377 { | |
6378 reg_startp[i] = bp->save_start[i].se_u.ptr; | |
6379 reg_endp[i] = bp->save_end[i].se_u.ptr; | |
6380 } | |
1579 | 6381 } |
6382 } | |
6383 } | |
6384 | |
6385 /* | |
7 | 6386 * Advance reglnum, regline and reginput to the next line. |
6387 */ | |
6388 static void | |
6389 reg_nextline() | |
6390 { | |
6391 regline = reg_getline(++reglnum); | |
6392 reginput = regline; | |
6393 fast_breakcheck(); | |
6394 } | |
6395 | |
6396 /* | |
6397 * Save the input line and position in a regsave_T. | |
6398 */ | |
6399 static void | |
233 | 6400 reg_save(save, gap) |
7 | 6401 regsave_T *save; |
233 | 6402 garray_T *gap; |
7 | 6403 { |
6404 if (REG_MULTI) | |
6405 { | |
6406 save->rs_u.pos.col = (colnr_T)(reginput - regline); | |
6407 save->rs_u.pos.lnum = reglnum; | |
6408 } | |
6409 else | |
6410 save->rs_u.ptr = reginput; | |
233 | 6411 save->rs_len = gap->ga_len; |
7 | 6412 } |
6413 | |
6414 /* | |
6415 * Restore the input line and position from a regsave_T. | |
6416 */ | |
6417 static void | |
233 | 6418 reg_restore(save, gap) |
7 | 6419 regsave_T *save; |
233 | 6420 garray_T *gap; |
7 | 6421 { |
6422 if (REG_MULTI) | |
6423 { | |
6424 if (reglnum != save->rs_u.pos.lnum) | |
6425 { | |
6426 /* only call reg_getline() when the line number changed to save | |
6427 * a bit of time */ | |
6428 reglnum = save->rs_u.pos.lnum; | |
6429 regline = reg_getline(reglnum); | |
6430 } | |
6431 reginput = regline + save->rs_u.pos.col; | |
6432 } | |
6433 else | |
6434 reginput = save->rs_u.ptr; | |
233 | 6435 gap->ga_len = save->rs_len; |
7 | 6436 } |
6437 | |
6438 /* | |
6439 * Return TRUE if current position is equal to saved position. | |
6440 */ | |
6441 static int | |
6442 reg_save_equal(save) | |
6443 regsave_T *save; | |
6444 { | |
6445 if (REG_MULTI) | |
6446 return reglnum == save->rs_u.pos.lnum | |
6447 && reginput == regline + save->rs_u.pos.col; | |
6448 return reginput == save->rs_u.ptr; | |
6449 } | |
6450 | |
6451 /* | |
6452 * Tentatively set the sub-expression start to the current position (after | |
6453 * calling regmatch() they will have changed). Need to save the existing | |
6454 * values for when there is no match. | |
6455 * Use se_save() to use pointer (save_se_multi()) or position (save_se_one()), | |
6456 * depending on REG_MULTI. | |
6457 */ | |
6458 static void | |
6459 save_se_multi(savep, posp) | |
6460 save_se_T *savep; | |
6461 lpos_T *posp; | |
6462 { | |
6463 savep->se_u.pos = *posp; | |
6464 posp->lnum = reglnum; | |
6465 posp->col = (colnr_T)(reginput - regline); | |
6466 } | |
6467 | |
6468 static void | |
6469 save_se_one(savep, pp) | |
6470 save_se_T *savep; | |
6471 char_u **pp; | |
6472 { | |
6473 savep->se_u.ptr = *pp; | |
6474 *pp = reginput; | |
6475 } | |
6476 | |
6477 /* | |
6478 * Compare a number with the operand of RE_LNUM, RE_COL or RE_VCOL. | |
6479 */ | |
6480 static int | |
6481 re_num_cmp(val, scan) | |
6482 long_u val; | |
6483 char_u *scan; | |
6484 { | |
6485 long_u n = OPERAND_MIN(scan); | |
6486 | |
6487 if (OPERAND_CMP(scan) == '>') | |
6488 return val > n; | |
6489 if (OPERAND_CMP(scan) == '<') | |
6490 return val < n; | |
6491 return val == n; | |
6492 } | |
6493 | |
6494 | |
4444 | 6495 #ifdef BT_REGEXP_DUMP |
7 | 6496 |
6497 /* | |
6498 * regdump - dump a regexp onto stdout in vaguely comprehensible form | |
6499 */ | |
6500 static void | |
6501 regdump(pattern, r) | |
6502 char_u *pattern; | |
4444 | 6503 bt_regprog_T *r; |
7 | 6504 { |
6505 char_u *s; | |
6506 int op = EXACTLY; /* Arbitrary non-END op. */ | |
6507 char_u *next; | |
6508 char_u *end = NULL; | |
4444 | 6509 FILE *f; |
6510 | |
6511 #ifdef BT_REGEXP_LOG | |
6512 f = fopen("bt_regexp_log.log", "a"); | |
6513 #else | |
6514 f = stdout; | |
6515 #endif | |
6516 if (f == NULL) | |
6517 return; | |
6518 fprintf(f, "-------------------------------------\n\r\nregcomp(%s):\r\n", pattern); | |
7 | 6519 |
6520 s = r->program + 1; | |
6521 /* | |
6522 * Loop until we find the END that isn't before a referred next (an END | |
6523 * can also appear in a NOMATCH operand). | |
6524 */ | |
6525 while (op != END || s <= end) | |
6526 { | |
6527 op = OP(s); | |
4444 | 6528 fprintf(f, "%2d%s", (int)(s - r->program), regprop(s)); /* Where, what. */ |
7 | 6529 next = regnext(s); |
6530 if (next == NULL) /* Next ptr. */ | |
4444 | 6531 fprintf(f, "(0)"); |
7 | 6532 else |
4444 | 6533 fprintf(f, "(%d)", (int)((s - r->program) + (next - s))); |
7 | 6534 if (end < next) |
6535 end = next; | |
6536 if (op == BRACE_LIMITS) | |
6537 { | |
6538 /* Two short ints */ | |
4444 | 6539 fprintf(f, " minval %ld, maxval %ld", OPERAND_MIN(s), OPERAND_MAX(s)); |
7 | 6540 s += 8; |
6541 } | |
6542 s += 3; | |
6543 if (op == ANYOF || op == ANYOF + ADD_NL | |
6544 || op == ANYBUT || op == ANYBUT + ADD_NL | |
6545 || op == EXACTLY) | |
6546 { | |
6547 /* Literal string, where present. */ | |
4444 | 6548 fprintf(f, "\nxxxxxxxxx\n"); |
7 | 6549 while (*s != NUL) |
4444 | 6550 fprintf(f, "%c", *s++); |
6551 fprintf(f, "\nxxxxxxxxx\n"); | |
7 | 6552 s++; |
6553 } | |
4444 | 6554 fprintf(f, "\r\n"); |
7 | 6555 } |
6556 | |
6557 /* Header fields of interest. */ | |
6558 if (r->regstart != NUL) | |
4444 | 6559 fprintf(f, "start `%s' 0x%x; ", r->regstart < 256 |
7 | 6560 ? (char *)transchar(r->regstart) |
6561 : "multibyte", r->regstart); | |
6562 if (r->reganch) | |
4444 | 6563 fprintf(f, "anchored; "); |
7 | 6564 if (r->regmust != NULL) |
4444 | 6565 fprintf(f, "must have \"%s\"", r->regmust); |
6566 fprintf(f, "\r\n"); | |
6567 | |
6568 #ifdef BT_REGEXP_LOG | |
6569 fclose(f); | |
6570 #endif | |
7 | 6571 } |
4444 | 6572 #endif /* BT_REGEXP_DUMP */ |
6573 | |
6574 #ifdef DEBUG | |
7 | 6575 /* |
6576 * regprop - printable representation of opcode | |
6577 */ | |
6578 static char_u * | |
6579 regprop(op) | |
6580 char_u *op; | |
6581 { | |
4444 | 6582 char *p; |
6583 static char buf[50]; | |
6584 | |
6585 STRCPY(buf, ":"); | |
6586 | |
6587 switch ((int) OP(op)) | |
7 | 6588 { |
6589 case BOL: | |
6590 p = "BOL"; | |
6591 break; | |
6592 case EOL: | |
6593 p = "EOL"; | |
6594 break; | |
6595 case RE_BOF: | |
6596 p = "BOF"; | |
6597 break; | |
6598 case RE_EOF: | |
6599 p = "EOF"; | |
6600 break; | |
6601 case CURSOR: | |
6602 p = "CURSOR"; | |
6603 break; | |
639 | 6604 case RE_VISUAL: |
6605 p = "RE_VISUAL"; | |
6606 break; | |
7 | 6607 case RE_LNUM: |
6608 p = "RE_LNUM"; | |
6609 break; | |
639 | 6610 case RE_MARK: |
6611 p = "RE_MARK"; | |
6612 break; | |
7 | 6613 case RE_COL: |
6614 p = "RE_COL"; | |
6615 break; | |
6616 case RE_VCOL: | |
6617 p = "RE_VCOL"; | |
6618 break; | |
6619 case BOW: | |
6620 p = "BOW"; | |
6621 break; | |
6622 case EOW: | |
6623 p = "EOW"; | |
6624 break; | |
6625 case ANY: | |
6626 p = "ANY"; | |
6627 break; | |
6628 case ANY + ADD_NL: | |
6629 p = "ANY+NL"; | |
6630 break; | |
6631 case ANYOF: | |
6632 p = "ANYOF"; | |
6633 break; | |
6634 case ANYOF + ADD_NL: | |
6635 p = "ANYOF+NL"; | |
6636 break; | |
6637 case ANYBUT: | |
6638 p = "ANYBUT"; | |
6639 break; | |
6640 case ANYBUT + ADD_NL: | |
6641 p = "ANYBUT+NL"; | |
6642 break; | |
6643 case IDENT: | |
6644 p = "IDENT"; | |
6645 break; | |
6646 case IDENT + ADD_NL: | |
6647 p = "IDENT+NL"; | |
6648 break; | |
6649 case SIDENT: | |
6650 p = "SIDENT"; | |
6651 break; | |
6652 case SIDENT + ADD_NL: | |
6653 p = "SIDENT+NL"; | |
6654 break; | |
6655 case KWORD: | |
6656 p = "KWORD"; | |
6657 break; | |
6658 case KWORD + ADD_NL: | |
6659 p = "KWORD+NL"; | |
6660 break; | |
6661 case SKWORD: | |
6662 p = "SKWORD"; | |
6663 break; | |
6664 case SKWORD + ADD_NL: | |
6665 p = "SKWORD+NL"; | |
6666 break; | |
6667 case FNAME: | |
6668 p = "FNAME"; | |
6669 break; | |
6670 case FNAME + ADD_NL: | |
6671 p = "FNAME+NL"; | |
6672 break; | |
6673 case SFNAME: | |
6674 p = "SFNAME"; | |
6675 break; | |
6676 case SFNAME + ADD_NL: | |
6677 p = "SFNAME+NL"; | |
6678 break; | |
6679 case PRINT: | |
6680 p = "PRINT"; | |
6681 break; | |
6682 case PRINT + ADD_NL: | |
6683 p = "PRINT+NL"; | |
6684 break; | |
6685 case SPRINT: | |
6686 p = "SPRINT"; | |
6687 break; | |
6688 case SPRINT + ADD_NL: | |
6689 p = "SPRINT+NL"; | |
6690 break; | |
6691 case WHITE: | |
6692 p = "WHITE"; | |
6693 break; | |
6694 case WHITE + ADD_NL: | |
6695 p = "WHITE+NL"; | |
6696 break; | |
6697 case NWHITE: | |
6698 p = "NWHITE"; | |
6699 break; | |
6700 case NWHITE + ADD_NL: | |
6701 p = "NWHITE+NL"; | |
6702 break; | |
6703 case DIGIT: | |
6704 p = "DIGIT"; | |
6705 break; | |
6706 case DIGIT + ADD_NL: | |
6707 p = "DIGIT+NL"; | |
6708 break; | |
6709 case NDIGIT: | |
6710 p = "NDIGIT"; | |
6711 break; | |
6712 case NDIGIT + ADD_NL: | |
6713 p = "NDIGIT+NL"; | |
6714 break; | |
6715 case HEX: | |
6716 p = "HEX"; | |
6717 break; | |
6718 case HEX + ADD_NL: | |
6719 p = "HEX+NL"; | |
6720 break; | |
6721 case NHEX: | |
6722 p = "NHEX"; | |
6723 break; | |
6724 case NHEX + ADD_NL: | |
6725 p = "NHEX+NL"; | |
6726 break; | |
6727 case OCTAL: | |
6728 p = "OCTAL"; | |
6729 break; | |
6730 case OCTAL + ADD_NL: | |
6731 p = "OCTAL+NL"; | |
6732 break; | |
6733 case NOCTAL: | |
6734 p = "NOCTAL"; | |
6735 break; | |
6736 case NOCTAL + ADD_NL: | |
6737 p = "NOCTAL+NL"; | |
6738 break; | |
6739 case WORD: | |
6740 p = "WORD"; | |
6741 break; | |
6742 case WORD + ADD_NL: | |
6743 p = "WORD+NL"; | |
6744 break; | |
6745 case NWORD: | |
6746 p = "NWORD"; | |
6747 break; | |
6748 case NWORD + ADD_NL: | |
6749 p = "NWORD+NL"; | |
6750 break; | |
6751 case HEAD: | |
6752 p = "HEAD"; | |
6753 break; | |
6754 case HEAD + ADD_NL: | |
6755 p = "HEAD+NL"; | |
6756 break; | |
6757 case NHEAD: | |
6758 p = "NHEAD"; | |
6759 break; | |
6760 case NHEAD + ADD_NL: | |
6761 p = "NHEAD+NL"; | |
6762 break; | |
6763 case ALPHA: | |
6764 p = "ALPHA"; | |
6765 break; | |
6766 case ALPHA + ADD_NL: | |
6767 p = "ALPHA+NL"; | |
6768 break; | |
6769 case NALPHA: | |
6770 p = "NALPHA"; | |
6771 break; | |
6772 case NALPHA + ADD_NL: | |
6773 p = "NALPHA+NL"; | |
6774 break; | |
6775 case LOWER: | |
6776 p = "LOWER"; | |
6777 break; | |
6778 case LOWER + ADD_NL: | |
6779 p = "LOWER+NL"; | |
6780 break; | |
6781 case NLOWER: | |
6782 p = "NLOWER"; | |
6783 break; | |
6784 case NLOWER + ADD_NL: | |
6785 p = "NLOWER+NL"; | |
6786 break; | |
6787 case UPPER: | |
6788 p = "UPPER"; | |
6789 break; | |
6790 case UPPER + ADD_NL: | |
6791 p = "UPPER+NL"; | |
6792 break; | |
6793 case NUPPER: | |
6794 p = "NUPPER"; | |
6795 break; | |
6796 case NUPPER + ADD_NL: | |
6797 p = "NUPPER+NL"; | |
6798 break; | |
6799 case BRANCH: | |
6800 p = "BRANCH"; | |
6801 break; | |
6802 case EXACTLY: | |
6803 p = "EXACTLY"; | |
6804 break; | |
6805 case NOTHING: | |
6806 p = "NOTHING"; | |
6807 break; | |
6808 case BACK: | |
6809 p = "BACK"; | |
6810 break; | |
6811 case END: | |
6812 p = "END"; | |
6813 break; | |
6814 case MOPEN + 0: | |
6815 p = "MATCH START"; | |
6816 break; | |
6817 case MOPEN + 1: | |
6818 case MOPEN + 2: | |
6819 case MOPEN + 3: | |
6820 case MOPEN + 4: | |
6821 case MOPEN + 5: | |
6822 case MOPEN + 6: | |
6823 case MOPEN + 7: | |
6824 case MOPEN + 8: | |
6825 case MOPEN + 9: | |
6826 sprintf(buf + STRLEN(buf), "MOPEN%d", OP(op) - MOPEN); | |
6827 p = NULL; | |
6828 break; | |
6829 case MCLOSE + 0: | |
6830 p = "MATCH END"; | |
6831 break; | |
6832 case MCLOSE + 1: | |
6833 case MCLOSE + 2: | |
6834 case MCLOSE + 3: | |
6835 case MCLOSE + 4: | |
6836 case MCLOSE + 5: | |
6837 case MCLOSE + 6: | |
6838 case MCLOSE + 7: | |
6839 case MCLOSE + 8: | |
6840 case MCLOSE + 9: | |
6841 sprintf(buf + STRLEN(buf), "MCLOSE%d", OP(op) - MCLOSE); | |
6842 p = NULL; | |
6843 break; | |
6844 case BACKREF + 1: | |
6845 case BACKREF + 2: | |
6846 case BACKREF + 3: | |
6847 case BACKREF + 4: | |
6848 case BACKREF + 5: | |
6849 case BACKREF + 6: | |
6850 case BACKREF + 7: | |
6851 case BACKREF + 8: | |
6852 case BACKREF + 9: | |
6853 sprintf(buf + STRLEN(buf), "BACKREF%d", OP(op) - BACKREF); | |
6854 p = NULL; | |
6855 break; | |
6856 case NOPEN: | |
6857 p = "NOPEN"; | |
6858 break; | |
6859 case NCLOSE: | |
6860 p = "NCLOSE"; | |
6861 break; | |
6862 #ifdef FEAT_SYN_HL | |
6863 case ZOPEN + 1: | |
6864 case ZOPEN + 2: | |
6865 case ZOPEN + 3: | |
6866 case ZOPEN + 4: | |
6867 case ZOPEN + 5: | |
6868 case ZOPEN + 6: | |
6869 case ZOPEN + 7: | |
6870 case ZOPEN + 8: | |
6871 case ZOPEN + 9: | |
6872 sprintf(buf + STRLEN(buf), "ZOPEN%d", OP(op) - ZOPEN); | |
6873 p = NULL; | |
6874 break; | |
6875 case ZCLOSE + 1: | |
6876 case ZCLOSE + 2: | |
6877 case ZCLOSE + 3: | |
6878 case ZCLOSE + 4: | |
6879 case ZCLOSE + 5: | |
6880 case ZCLOSE + 6: | |
6881 case ZCLOSE + 7: | |
6882 case ZCLOSE + 8: | |
6883 case ZCLOSE + 9: | |
6884 sprintf(buf + STRLEN(buf), "ZCLOSE%d", OP(op) - ZCLOSE); | |
6885 p = NULL; | |
6886 break; | |
6887 case ZREF + 1: | |
6888 case ZREF + 2: | |
6889 case ZREF + 3: | |
6890 case ZREF + 4: | |
6891 case ZREF + 5: | |
6892 case ZREF + 6: | |
6893 case ZREF + 7: | |
6894 case ZREF + 8: | |
6895 case ZREF + 9: | |
6896 sprintf(buf + STRLEN(buf), "ZREF%d", OP(op) - ZREF); | |
6897 p = NULL; | |
6898 break; | |
6899 #endif | |
6900 case STAR: | |
6901 p = "STAR"; | |
6902 break; | |
6903 case PLUS: | |
6904 p = "PLUS"; | |
6905 break; | |
6906 case NOMATCH: | |
6907 p = "NOMATCH"; | |
6908 break; | |
6909 case MATCH: | |
6910 p = "MATCH"; | |
6911 break; | |
6912 case BEHIND: | |
6913 p = "BEHIND"; | |
6914 break; | |
6915 case NOBEHIND: | |
6916 p = "NOBEHIND"; | |
6917 break; | |
6918 case SUBPAT: | |
6919 p = "SUBPAT"; | |
6920 break; | |
6921 case BRACE_LIMITS: | |
6922 p = "BRACE_LIMITS"; | |
6923 break; | |
6924 case BRACE_SIMPLE: | |
6925 p = "BRACE_SIMPLE"; | |
6926 break; | |
6927 case BRACE_COMPLEX + 0: | |
6928 case BRACE_COMPLEX + 1: | |
6929 case BRACE_COMPLEX + 2: | |
6930 case BRACE_COMPLEX + 3: | |
6931 case BRACE_COMPLEX + 4: | |
6932 case BRACE_COMPLEX + 5: | |
6933 case BRACE_COMPLEX + 6: | |
6934 case BRACE_COMPLEX + 7: | |
6935 case BRACE_COMPLEX + 8: | |
6936 case BRACE_COMPLEX + 9: | |
6937 sprintf(buf + STRLEN(buf), "BRACE_COMPLEX%d", OP(op) - BRACE_COMPLEX); | |
6938 p = NULL; | |
6939 break; | |
6940 #ifdef FEAT_MBYTE | |
6941 case MULTIBYTECODE: | |
6942 p = "MULTIBYTECODE"; | |
6943 break; | |
6944 #endif | |
6945 case NEWL: | |
6946 p = "NEWL"; | |
6947 break; | |
6948 default: | |
6949 sprintf(buf + STRLEN(buf), "corrupt %d", OP(op)); | |
6950 p = NULL; | |
6951 break; | |
6952 } | |
6953 if (p != NULL) | |
4444 | 6954 STRCAT(buf, p); |
6955 return (char_u *)buf; | |
7 | 6956 } |
4444 | 6957 #endif /* DEBUG */ |
7 | 6958 |
6959 #ifdef FEAT_MBYTE | |
6960 static void mb_decompose __ARGS((int c, int *c1, int *c2, int *c3)); | |
6961 | |
6962 typedef struct | |
6963 { | |
6964 int a, b, c; | |
6965 } decomp_T; | |
6966 | |
6967 | |
6968 /* 0xfb20 - 0xfb4f */ | |
297 | 6969 static decomp_T decomp_table[0xfb4f-0xfb20+1] = |
7 | 6970 { |
6971 {0x5e2,0,0}, /* 0xfb20 alt ayin */ | |
6972 {0x5d0,0,0}, /* 0xfb21 alt alef */ | |
6973 {0x5d3,0,0}, /* 0xfb22 alt dalet */ | |
6974 {0x5d4,0,0}, /* 0xfb23 alt he */ | |
6975 {0x5db,0,0}, /* 0xfb24 alt kaf */ | |
6976 {0x5dc,0,0}, /* 0xfb25 alt lamed */ | |
6977 {0x5dd,0,0}, /* 0xfb26 alt mem-sofit */ | |
6978 {0x5e8,0,0}, /* 0xfb27 alt resh */ | |
6979 {0x5ea,0,0}, /* 0xfb28 alt tav */ | |
6980 {'+', 0, 0}, /* 0xfb29 alt plus */ | |
6981 {0x5e9, 0x5c1, 0}, /* 0xfb2a shin+shin-dot */ | |
6982 {0x5e9, 0x5c2, 0}, /* 0xfb2b shin+sin-dot */ | |
6983 {0x5e9, 0x5c1, 0x5bc}, /* 0xfb2c shin+shin-dot+dagesh */ | |
6984 {0x5e9, 0x5c2, 0x5bc}, /* 0xfb2d shin+sin-dot+dagesh */ | |
6985 {0x5d0, 0x5b7, 0}, /* 0xfb2e alef+patah */ | |
6986 {0x5d0, 0x5b8, 0}, /* 0xfb2f alef+qamats */ | |
6987 {0x5d0, 0x5b4, 0}, /* 0xfb30 alef+hiriq */ | |
6988 {0x5d1, 0x5bc, 0}, /* 0xfb31 bet+dagesh */ | |
6989 {0x5d2, 0x5bc, 0}, /* 0xfb32 gimel+dagesh */ | |
6990 {0x5d3, 0x5bc, 0}, /* 0xfb33 dalet+dagesh */ | |
6991 {0x5d4, 0x5bc, 0}, /* 0xfb34 he+dagesh */ | |
6992 {0x5d5, 0x5bc, 0}, /* 0xfb35 vav+dagesh */ | |
6993 {0x5d6, 0x5bc, 0}, /* 0xfb36 zayin+dagesh */ | |
6994 {0xfb37, 0, 0}, /* 0xfb37 -- UNUSED */ | |
6995 {0x5d8, 0x5bc, 0}, /* 0xfb38 tet+dagesh */ | |
6996 {0x5d9, 0x5bc, 0}, /* 0xfb39 yud+dagesh */ | |
6997 {0x5da, 0x5bc, 0}, /* 0xfb3a kaf sofit+dagesh */ | |
6998 {0x5db, 0x5bc, 0}, /* 0xfb3b kaf+dagesh */ | |
6999 {0x5dc, 0x5bc, 0}, /* 0xfb3c lamed+dagesh */ | |
7000 {0xfb3d, 0, 0}, /* 0xfb3d -- UNUSED */ | |
7001 {0x5de, 0x5bc, 0}, /* 0xfb3e mem+dagesh */ | |
7002 {0xfb3f, 0, 0}, /* 0xfb3f -- UNUSED */ | |
7003 {0x5e0, 0x5bc, 0}, /* 0xfb40 nun+dagesh */ | |
7004 {0x5e1, 0x5bc, 0}, /* 0xfb41 samech+dagesh */ | |
7005 {0xfb42, 0, 0}, /* 0xfb42 -- UNUSED */ | |
7006 {0x5e3, 0x5bc, 0}, /* 0xfb43 pe sofit+dagesh */ | |
7007 {0x5e4, 0x5bc,0}, /* 0xfb44 pe+dagesh */ | |
7008 {0xfb45, 0, 0}, /* 0xfb45 -- UNUSED */ | |
7009 {0x5e6, 0x5bc, 0}, /* 0xfb46 tsadi+dagesh */ | |
7010 {0x5e7, 0x5bc, 0}, /* 0xfb47 qof+dagesh */ | |
7011 {0x5e8, 0x5bc, 0}, /* 0xfb48 resh+dagesh */ | |
7012 {0x5e9, 0x5bc, 0}, /* 0xfb49 shin+dagesh */ | |
7013 {0x5ea, 0x5bc, 0}, /* 0xfb4a tav+dagesh */ | |
7014 {0x5d5, 0x5b9, 0}, /* 0xfb4b vav+holam */ | |
7015 {0x5d1, 0x5bf, 0}, /* 0xfb4c bet+rafe */ | |
7016 {0x5db, 0x5bf, 0}, /* 0xfb4d kaf+rafe */ | |
7017 {0x5e4, 0x5bf, 0}, /* 0xfb4e pe+rafe */ | |
7018 {0x5d0, 0x5dc, 0} /* 0xfb4f alef-lamed */ | |
7019 }; | |
7020 | |
7021 static void | |
7022 mb_decompose(c, c1, c2, c3) | |
7023 int c, *c1, *c2, *c3; | |
7024 { | |
7025 decomp_T d; | |
7026 | |
4505
d037b9cbdaaa
updated for version 7.3.1000
Bram Moolenaar <bram@vim.org>
parents:
4466
diff
changeset
|
7027 if (c >= 0xfb20 && c <= 0xfb4f) |
7 | 7028 { |
7029 d = decomp_table[c - 0xfb20]; | |
7030 *c1 = d.a; | |
7031 *c2 = d.b; | |
7032 *c3 = d.c; | |
7033 } | |
7034 else | |
7035 { | |
7036 *c1 = c; | |
7037 *c2 = *c3 = 0; | |
7038 } | |
7039 } | |
7040 #endif | |
7041 | |
7042 /* | |
7043 * Compare two strings, ignore case if ireg_ic set. | |
7044 * Return 0 if strings match, non-zero otherwise. | |
7045 * Correct the length "*n" when composing characters are ignored. | |
7046 */ | |
7047 static int | |
7048 cstrncmp(s1, s2, n) | |
7049 char_u *s1, *s2; | |
7050 int *n; | |
7051 { | |
7052 int result; | |
7053 | |
7054 if (!ireg_ic) | |
7055 result = STRNCMP(s1, s2, *n); | |
7056 else | |
7057 result = MB_STRNICMP(s1, s2, *n); | |
7058 | |
7059 #ifdef FEAT_MBYTE | |
7060 /* if it failed and it's utf8 and we want to combineignore: */ | |
7061 if (result != 0 && enc_utf8 && ireg_icombine) | |
7062 { | |
7063 char_u *str1, *str2; | |
7064 int c1, c2, c11, c12; | |
7065 int junk; | |
7066 | |
7067 /* we have to handle the strcmp ourselves, since it is necessary to | |
7068 * deal with the composing characters by ignoring them: */ | |
7069 str1 = s1; | |
7070 str2 = s2; | |
7071 c1 = c2 = 0; | |
507 | 7072 while ((int)(str1 - s1) < *n) |
7 | 7073 { |
7074 c1 = mb_ptr2char_adv(&str1); | |
7075 c2 = mb_ptr2char_adv(&str2); | |
7076 | |
7077 /* decompose the character if necessary, into 'base' characters | |
7078 * because I don't care about Arabic, I will hard-code the Hebrew | |
7079 * which I *do* care about! So sue me... */ | |
7080 if (c1 != c2 && (!ireg_ic || utf_fold(c1) != utf_fold(c2))) | |
7081 { | |
7082 /* decomposition necessary? */ | |
7083 mb_decompose(c1, &c11, &junk, &junk); | |
7084 mb_decompose(c2, &c12, &junk, &junk); | |
7085 c1 = c11; | |
7086 c2 = c12; | |
7087 if (c11 != c12 && (!ireg_ic || utf_fold(c11) != utf_fold(c12))) | |
7088 break; | |
7089 } | |
7090 } | |
7091 result = c2 - c1; | |
7092 if (result == 0) | |
7093 *n = (int)(str2 - s2); | |
7094 } | |
7095 #endif | |
7096 | |
7097 return result; | |
7098 } | |
7099 | |
7100 /* | |
7101 * cstrchr: This function is used a lot for simple searches, keep it fast! | |
7102 */ | |
7103 static char_u * | |
7104 cstrchr(s, c) | |
7105 char_u *s; | |
7106 int c; | |
7107 { | |
7108 char_u *p; | |
7109 int cc; | |
7110 | |
7111 if (!ireg_ic | |
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 /* This stuff below really confuses cc on an SGI -- webb */ | |
7162 #ifdef __sgi | |
7163 # undef __ARGS | |
7164 # define __ARGS(x) () | |
7165 #endif | |
7166 | |
7167 /* | |
7168 * We should define ftpr as a pointer to a function returning a pointer to | |
7169 * a function returning a pointer to a function ... | |
7170 * This is impossible, so we declare a pointer to a function returning a | |
7171 * pointer to a function returning void. This should work for all compilers. | |
7172 */ | |
772 | 7173 typedef void (*(*fptr_T) __ARGS((int *, int)))(); |
7174 | |
7175 static fptr_T do_upper __ARGS((int *, int)); | |
7176 static fptr_T do_Upper __ARGS((int *, int)); | |
7177 static fptr_T do_lower __ARGS((int *, int)); | |
7178 static fptr_T do_Lower __ARGS((int *, int)); | |
7 | 7179 |
7180 static int vim_regsub_both __ARGS((char_u *source, char_u *dest, int copy, int magic, int backslash)); | |
7181 | |
772 | 7182 static fptr_T |
7 | 7183 do_upper(d, c) |
772 | 7184 int *d; |
7185 int c; | |
7 | 7186 { |
772 | 7187 *d = MB_TOUPPER(c); |
7188 | |
7189 return (fptr_T)NULL; | |
7 | 7190 } |
7191 | |
772 | 7192 static fptr_T |
7193 do_Upper(d, c) | |
7194 int *d; | |
7 | 7195 int c; |
7196 { | |
772 | 7197 *d = MB_TOUPPER(c); |
7198 | |
7199 return (fptr_T)do_Upper; | |
7200 } | |
7201 | |
7202 static fptr_T | |
7203 do_lower(d, c) | |
7204 int *d; | |
7205 int c; | |
7206 { | |
7207 *d = MB_TOLOWER(c); | |
7208 | |
7209 return (fptr_T)NULL; | |
7210 } | |
7211 | |
7212 static fptr_T | |
7213 do_Lower(d, c) | |
7214 int *d; | |
7215 int c; | |
7216 { | |
7217 *d = MB_TOLOWER(c); | |
7218 | |
7219 return (fptr_T)do_Lower; | |
7 | 7220 } |
7221 | |
7222 /* | |
7223 * regtilde(): Replace tildes in the pattern by the old pattern. | |
7224 * | |
7225 * Short explanation of the tilde: It stands for the previous replacement | |
7226 * pattern. If that previous pattern also contains a ~ we should go back a | |
7227 * step further... But we insert the previous pattern into the current one | |
7228 * and remember that. | |
772 | 7229 * This still does not handle the case where "magic" changes. So require the |
7230 * user to keep his hands off of "magic". | |
7 | 7231 * |
7232 * The tildes are parsed once before the first call to vim_regsub(). | |
7233 */ | |
7234 char_u * | |
7235 regtilde(source, magic) | |
7236 char_u *source; | |
7237 int magic; | |
7238 { | |
7239 char_u *newsub = source; | |
7240 char_u *tmpsub; | |
7241 char_u *p; | |
7242 int len; | |
7243 int prevlen; | |
7244 | |
7245 for (p = newsub; *p; ++p) | |
7246 { | |
7247 if ((*p == '~' && magic) || (*p == '\\' && *(p + 1) == '~' && !magic)) | |
7248 { | |
7249 if (reg_prev_sub != NULL) | |
7250 { | |
7251 /* length = len(newsub) - 1 + len(prev_sub) + 1 */ | |
7252 prevlen = (int)STRLEN(reg_prev_sub); | |
7253 tmpsub = alloc((unsigned)(STRLEN(newsub) + prevlen)); | |
7254 if (tmpsub != NULL) | |
7255 { | |
7256 /* copy prefix */ | |
7257 len = (int)(p - newsub); /* not including ~ */ | |
7258 mch_memmove(tmpsub, newsub, (size_t)len); | |
1209 | 7259 /* interpret tilde */ |
7 | 7260 mch_memmove(tmpsub + len, reg_prev_sub, (size_t)prevlen); |
7261 /* copy postfix */ | |
7262 if (!magic) | |
7263 ++p; /* back off \ */ | |
7264 STRCPY(tmpsub + len + prevlen, p + 1); | |
7265 | |
7266 if (newsub != source) /* already allocated newsub */ | |
7267 vim_free(newsub); | |
7268 newsub = tmpsub; | |
7269 p = newsub + len + prevlen; | |
7270 } | |
7271 } | |
7272 else if (magic) | |
1621 | 7273 STRMOVE(p, p + 1); /* remove '~' */ |
7 | 7274 else |
1621 | 7275 STRMOVE(p, p + 2); /* remove '\~' */ |
7 | 7276 --p; |
7277 } | |
7278 else | |
7279 { | |
7280 if (*p == '\\' && p[1]) /* skip escaped characters */ | |
7281 ++p; | |
7282 #ifdef FEAT_MBYTE | |
7283 if (has_mbyte) | |
474 | 7284 p += (*mb_ptr2len)(p) - 1; |
7 | 7285 #endif |
7286 } | |
7287 } | |
7288 | |
7289 vim_free(reg_prev_sub); | |
7290 if (newsub != source) /* newsub was allocated, just keep it */ | |
7291 reg_prev_sub = newsub; | |
7292 else /* no ~ found, need to save newsub */ | |
7293 reg_prev_sub = vim_strsave(newsub); | |
7294 return newsub; | |
7295 } | |
7296 | |
7297 #ifdef FEAT_EVAL | |
7298 static int can_f_submatch = FALSE; /* TRUE when submatch() can be used */ | |
7299 | |
7300 /* These pointers are used instead of reg_match and reg_mmatch for | |
7301 * reg_submatch(). Needed for when the substitution string is an expression | |
7302 * that contains a call to substitute() and submatch(). */ | |
7303 static regmatch_T *submatch_match; | |
7304 static regmmatch_T *submatch_mmatch; | |
2011 | 7305 static linenr_T submatch_firstlnum; |
7306 static linenr_T submatch_maxline; | |
2904 | 7307 static int submatch_line_lbr; |
7 | 7308 #endif |
7309 | |
7310 #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO) | |
7311 /* | |
7312 * vim_regsub() - perform substitutions after a vim_regexec() or | |
7313 * vim_regexec_multi() match. | |
7314 * | |
7315 * If "copy" is TRUE really copy into "dest". | |
7316 * If "copy" is FALSE nothing is copied, this is just to find out the length | |
7317 * of the result. | |
7318 * | |
7319 * If "backslash" is TRUE, a backslash will be removed later, need to double | |
7320 * them to keep them, and insert a backslash before a CR to avoid it being | |
7321 * replaced with a line break later. | |
7322 * | |
7323 * Note: The matched text must not change between the call of | |
7324 * vim_regexec()/vim_regexec_multi() and vim_regsub()! It would make the back | |
7325 * references invalid! | |
7326 * | |
7327 * Returns the size of the replacement, including terminating NUL. | |
7328 */ | |
7329 int | |
7330 vim_regsub(rmp, source, dest, copy, magic, backslash) | |
7331 regmatch_T *rmp; | |
7332 char_u *source; | |
7333 char_u *dest; | |
7334 int copy; | |
7335 int magic; | |
7336 int backslash; | |
7337 { | |
7338 reg_match = rmp; | |
7339 reg_mmatch = NULL; | |
7340 reg_maxline = 0; | |
4061 | 7341 reg_buf = curbuf; |
7 | 7342 return vim_regsub_both(source, dest, copy, magic, backslash); |
7343 } | |
7344 #endif | |
7345 | |
7346 int | |
7347 vim_regsub_multi(rmp, lnum, source, dest, copy, magic, backslash) | |
7348 regmmatch_T *rmp; | |
7349 linenr_T lnum; | |
7350 char_u *source; | |
7351 char_u *dest; | |
7352 int copy; | |
7353 int magic; | |
7354 int backslash; | |
7355 { | |
7356 reg_match = NULL; | |
7357 reg_mmatch = rmp; | |
7358 reg_buf = curbuf; /* always works on the current buffer! */ | |
7359 reg_firstlnum = lnum; | |
7360 reg_maxline = curbuf->b_ml.ml_line_count - lnum; | |
7361 return vim_regsub_both(source, dest, copy, magic, backslash); | |
7362 } | |
7363 | |
7364 static int | |
7365 vim_regsub_both(source, dest, copy, magic, backslash) | |
7366 char_u *source; | |
7367 char_u *dest; | |
7368 int copy; | |
7369 int magic; | |
7370 int backslash; | |
7371 { | |
7372 char_u *src; | |
7373 char_u *dst; | |
7374 char_u *s; | |
7375 int c; | |
772 | 7376 int cc; |
7 | 7377 int no = -1; |
4244 | 7378 fptr_T func_all = (fptr_T)NULL; |
7379 fptr_T func_one = (fptr_T)NULL; | |
7 | 7380 linenr_T clnum = 0; /* init for GCC */ |
7381 int len = 0; /* init for GCC */ | |
7382 #ifdef FEAT_EVAL | |
7383 static char_u *eval_result = NULL; | |
7384 #endif | |
7385 | |
7386 /* Be paranoid... */ | |
7387 if (source == NULL || dest == NULL) | |
7388 { | |
7389 EMSG(_(e_null)); | |
7390 return 0; | |
7391 } | |
7392 if (prog_magic_wrong()) | |
7393 return 0; | |
7394 src = source; | |
7395 dst = dest; | |
7396 | |
7397 /* | |
7398 * When the substitute part starts with "\=" evaluate it as an expression. | |
7399 */ | |
7400 if (source[0] == '\\' && source[1] == '=' | |
7401 #ifdef FEAT_EVAL | |
7402 && !can_f_submatch /* can't do this recursively */ | |
7403 #endif | |
7404 ) | |
7405 { | |
7406 #ifdef FEAT_EVAL | |
7407 /* To make sure that the length doesn't change between checking the | |
7408 * length and copying the string, and to speed up things, the | |
7409 * resulting string is saved from the call with "copy" == FALSE to the | |
7410 * call with "copy" == TRUE. */ | |
7411 if (copy) | |
7412 { | |
7413 if (eval_result != NULL) | |
7414 { | |
7415 STRCPY(dest, eval_result); | |
7416 dst += STRLEN(eval_result); | |
7417 vim_free(eval_result); | |
7418 eval_result = NULL; | |
7419 } | |
7420 } | |
7421 else | |
7422 { | |
7423 win_T *save_reg_win; | |
7424 int save_ireg_ic; | |
7425 | |
7426 vim_free(eval_result); | |
7427 | |
7428 /* The expression may contain substitute(), which calls us | |
7429 * recursively. Make sure submatch() gets the text from the first | |
7430 * level. Don't need to save "reg_buf", because | |
7431 * vim_regexec_multi() can't be called recursively. */ | |
7432 submatch_match = reg_match; | |
7433 submatch_mmatch = reg_mmatch; | |
2011 | 7434 submatch_firstlnum = reg_firstlnum; |
7435 submatch_maxline = reg_maxline; | |
2904 | 7436 submatch_line_lbr = reg_line_lbr; |
7 | 7437 save_reg_win = reg_win; |
7438 save_ireg_ic = ireg_ic; | |
7439 can_f_submatch = TRUE; | |
7440 | |
714 | 7441 eval_result = eval_to_string(source + 2, NULL, TRUE); |
7 | 7442 if (eval_result != NULL) |
7443 { | |
2125
b8744d1982d1
updated for version 7.2.407
Bram Moolenaar <bram@zimbu.org>
parents:
2012
diff
changeset
|
7444 int had_backslash = FALSE; |
b8744d1982d1
updated for version 7.2.407
Bram Moolenaar <bram@zimbu.org>
parents:
2012
diff
changeset
|
7445 |
39 | 7446 for (s = eval_result; *s != NUL; mb_ptr_adv(s)) |
7 | 7447 { |
2904 | 7448 /* Change NL to CR, so that it becomes a line break, |
7449 * unless called from vim_regexec_nl(). | |
7 | 7450 * Skip over a backslashed character. */ |
2904 | 7451 if (*s == NL && !submatch_line_lbr) |
7 | 7452 *s = CAR; |
7453 else if (*s == '\\' && s[1] != NUL) | |
2125
b8744d1982d1
updated for version 7.2.407
Bram Moolenaar <bram@zimbu.org>
parents:
2012
diff
changeset
|
7454 { |
7 | 7455 ++s; |
2173 | 7456 /* Change NL to CR here too, so that this works: |
7457 * :s/abc\\\ndef/\="aaa\\\nbbb"/ on text: | |
7458 * abc\ | |
7459 * def | |
2904 | 7460 * Not when called from vim_regexec_nl(). |
2173 | 7461 */ |
2904 | 7462 if (*s == NL && !submatch_line_lbr) |
2173 | 7463 *s = CAR; |
2125
b8744d1982d1
updated for version 7.2.407
Bram Moolenaar <bram@zimbu.org>
parents:
2012
diff
changeset
|
7464 had_backslash = TRUE; |
b8744d1982d1
updated for version 7.2.407
Bram Moolenaar <bram@zimbu.org>
parents:
2012
diff
changeset
|
7465 } |
b8744d1982d1
updated for version 7.2.407
Bram Moolenaar <bram@zimbu.org>
parents:
2012
diff
changeset
|
7466 } |
b8744d1982d1
updated for version 7.2.407
Bram Moolenaar <bram@zimbu.org>
parents:
2012
diff
changeset
|
7467 if (had_backslash && backslash) |
b8744d1982d1
updated for version 7.2.407
Bram Moolenaar <bram@zimbu.org>
parents:
2012
diff
changeset
|
7468 { |
b8744d1982d1
updated for version 7.2.407
Bram Moolenaar <bram@zimbu.org>
parents:
2012
diff
changeset
|
7469 /* Backslashes will be consumed, need to double them. */ |
b8744d1982d1
updated for version 7.2.407
Bram Moolenaar <bram@zimbu.org>
parents:
2012
diff
changeset
|
7470 s = vim_strsave_escaped(eval_result, (char_u *)"\\"); |
b8744d1982d1
updated for version 7.2.407
Bram Moolenaar <bram@zimbu.org>
parents:
2012
diff
changeset
|
7471 if (s != NULL) |
b8744d1982d1
updated for version 7.2.407
Bram Moolenaar <bram@zimbu.org>
parents:
2012
diff
changeset
|
7472 { |
b8744d1982d1
updated for version 7.2.407
Bram Moolenaar <bram@zimbu.org>
parents:
2012
diff
changeset
|
7473 vim_free(eval_result); |
b8744d1982d1
updated for version 7.2.407
Bram Moolenaar <bram@zimbu.org>
parents:
2012
diff
changeset
|
7474 eval_result = s; |
b8744d1982d1
updated for version 7.2.407
Bram Moolenaar <bram@zimbu.org>
parents:
2012
diff
changeset
|
7475 } |
7 | 7476 } |
7477 | |
7478 dst += STRLEN(eval_result); | |
7479 } | |
7480 | |
7481 reg_match = submatch_match; | |
7482 reg_mmatch = submatch_mmatch; | |
2011 | 7483 reg_firstlnum = submatch_firstlnum; |
7484 reg_maxline = submatch_maxline; | |
2904 | 7485 reg_line_lbr = submatch_line_lbr; |
7 | 7486 reg_win = save_reg_win; |
7487 ireg_ic = save_ireg_ic; | |
7488 can_f_submatch = FALSE; | |
7489 } | |
7490 #endif | |
7491 } | |
7492 else | |
7493 while ((c = *src++) != NUL) | |
7494 { | |
7495 if (c == '&' && magic) | |
7496 no = 0; | |
7497 else if (c == '\\' && *src != NUL) | |
7498 { | |
7499 if (*src == '&' && !magic) | |
7500 { | |
7501 ++src; | |
7502 no = 0; | |
7503 } | |
7504 else if ('0' <= *src && *src <= '9') | |
7505 { | |
7506 no = *src++ - '0'; | |
7507 } | |
7508 else if (vim_strchr((char_u *)"uUlLeE", *src)) | |
7509 { | |
7510 switch (*src++) | |
7511 { | |
4244 | 7512 case 'u': func_one = (fptr_T)do_upper; |
7 | 7513 continue; |
4244 | 7514 case 'U': func_all = (fptr_T)do_Upper; |
7 | 7515 continue; |
4244 | 7516 case 'l': func_one = (fptr_T)do_lower; |
7 | 7517 continue; |
4244 | 7518 case 'L': func_all = (fptr_T)do_Lower; |
7 | 7519 continue; |
7520 case 'e': | |
4244 | 7521 case 'E': func_one = func_all = (fptr_T)NULL; |
7 | 7522 continue; |
7523 } | |
7524 } | |
7525 } | |
7526 if (no < 0) /* Ordinary character. */ | |
7527 { | |
798 | 7528 if (c == K_SPECIAL && src[0] != NUL && src[1] != NUL) |
7529 { | |
1209 | 7530 /* Copy a special key as-is. */ |
798 | 7531 if (copy) |
7532 { | |
7533 *dst++ = c; | |
7534 *dst++ = *src++; | |
7535 *dst++ = *src++; | |
7536 } | |
7537 else | |
7538 { | |
7539 dst += 3; | |
7540 src += 2; | |
7541 } | |
7542 continue; | |
7543 } | |
7544 | |
7 | 7545 if (c == '\\' && *src != NUL) |
7546 { | |
7547 /* Check for abbreviations -- webb */ | |
7548 switch (*src) | |
7549 { | |
7550 case 'r': c = CAR; ++src; break; | |
7551 case 'n': c = NL; ++src; break; | |
7552 case 't': c = TAB; ++src; break; | |
7553 /* Oh no! \e already has meaning in subst pat :-( */ | |
7554 /* case 'e': c = ESC; ++src; break; */ | |
7555 case 'b': c = Ctrl_H; ++src; break; | |
7556 | |
7557 /* If "backslash" is TRUE the backslash will be removed | |
7558 * later. Used to insert a literal CR. */ | |
7559 default: if (backslash) | |
7560 { | |
7561 if (copy) | |
7562 *dst = '\\'; | |
7563 ++dst; | |
7564 } | |
7565 c = *src++; | |
7566 } | |
7567 } | |
798 | 7568 #ifdef FEAT_MBYTE |
7569 else if (has_mbyte) | |
7570 c = mb_ptr2char(src - 1); | |
7571 #endif | |
7 | 7572 |
7573 /* Write to buffer, if copy is set. */ | |
4244 | 7574 if (func_one != (fptr_T)NULL) |
7575 /* Turbo C complains without the typecast */ | |
7576 func_one = (fptr_T)(func_one(&cc, c)); | |
7577 else if (func_all != (fptr_T)NULL) | |
7578 /* Turbo C complains without the typecast */ | |
7579 func_all = (fptr_T)(func_all(&cc, c)); | |
7580 else /* just copy */ | |
772 | 7581 cc = c; |
7582 | |
7583 #ifdef FEAT_MBYTE | |
7584 if (has_mbyte) | |
7 | 7585 { |
2307
81527f127fb1
Fix: Composing characters in :s substitute text were dropped.
Bram Moolenaar <bram@vim.org>
parents:
2247
diff
changeset
|
7586 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
|
7587 |
7 | 7588 if (copy) |
772 | 7589 mb_char2bytes(cc, dst); |
7590 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
|
7591 if (enc_utf8) |
81527f127fb1
Fix: Composing characters in :s substitute text were dropped.
Bram Moolenaar <bram@vim.org>
parents:
2247
diff
changeset
|
7592 { |
81527f127fb1
Fix: Composing characters in :s substitute text were dropped.
Bram Moolenaar <bram@vim.org>
parents:
2247
diff
changeset
|
7593 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
|
7594 |
81527f127fb1
Fix: Composing characters in :s substitute text were dropped.
Bram Moolenaar <bram@vim.org>
parents:
2247
diff
changeset
|
7595 /* 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
|
7596 * 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
|
7597 if (clen < totlen) |
81527f127fb1
Fix: Composing characters in :s substitute text were dropped.
Bram Moolenaar <bram@vim.org>
parents:
2247
diff
changeset
|
7598 { |
81527f127fb1
Fix: Composing characters in :s substitute text were dropped.
Bram Moolenaar <bram@vim.org>
parents:
2247
diff
changeset
|
7599 if (copy) |
81527f127fb1
Fix: Composing characters in :s substitute text were dropped.
Bram Moolenaar <bram@vim.org>
parents:
2247
diff
changeset
|
7600 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
|
7601 (size_t)(totlen - clen)); |
81527f127fb1
Fix: Composing characters in :s substitute text were dropped.
Bram Moolenaar <bram@vim.org>
parents:
2247
diff
changeset
|
7602 dst += totlen - clen; |
81527f127fb1
Fix: Composing characters in :s substitute text were dropped.
Bram Moolenaar <bram@vim.org>
parents:
2247
diff
changeset
|
7603 } |
81527f127fb1
Fix: Composing characters in :s substitute text were dropped.
Bram Moolenaar <bram@vim.org>
parents:
2247
diff
changeset
|
7604 } |
81527f127fb1
Fix: Composing characters in :s substitute text were dropped.
Bram Moolenaar <bram@vim.org>
parents:
2247
diff
changeset
|
7605 src += totlen - 1; |
7 | 7606 } |
7607 else | |
7608 #endif | |
7609 if (copy) | |
772 | 7610 *dst = cc; |
7 | 7611 dst++; |
7612 } | |
7613 else | |
7614 { | |
7615 if (REG_MULTI) | |
7616 { | |
7617 clnum = reg_mmatch->startpos[no].lnum; | |
7618 if (clnum < 0 || reg_mmatch->endpos[no].lnum < 0) | |
7619 s = NULL; | |
7620 else | |
7621 { | |
7622 s = reg_getline(clnum) + reg_mmatch->startpos[no].col; | |
7623 if (reg_mmatch->endpos[no].lnum == clnum) | |
7624 len = reg_mmatch->endpos[no].col | |
7625 - reg_mmatch->startpos[no].col; | |
7626 else | |
7627 len = (int)STRLEN(s); | |
7628 } | |
7629 } | |
7630 else | |
7631 { | |
7632 s = reg_match->startp[no]; | |
7633 if (reg_match->endp[no] == NULL) | |
7634 s = NULL; | |
7635 else | |
7636 len = (int)(reg_match->endp[no] - s); | |
7637 } | |
7638 if (s != NULL) | |
7639 { | |
7640 for (;;) | |
7641 { | |
7642 if (len == 0) | |
7643 { | |
7644 if (REG_MULTI) | |
7645 { | |
7646 if (reg_mmatch->endpos[no].lnum == clnum) | |
7647 break; | |
7648 if (copy) | |
7649 *dst = CAR; | |
7650 ++dst; | |
7651 s = reg_getline(++clnum); | |
7652 if (reg_mmatch->endpos[no].lnum == clnum) | |
7653 len = reg_mmatch->endpos[no].col; | |
7654 else | |
7655 len = (int)STRLEN(s); | |
7656 } | |
7657 else | |
7658 break; | |
7659 } | |
7660 else if (*s == NUL) /* we hit NUL. */ | |
7661 { | |
7662 if (copy) | |
7663 EMSG(_(e_re_damg)); | |
7664 goto exit; | |
7665 } | |
7666 else | |
7667 { | |
7668 if (backslash && (*s == CAR || *s == '\\')) | |
7669 { | |
7670 /* | |
7671 * Insert a backslash in front of a CR, otherwise | |
7672 * it will be replaced by a line break. | |
7673 * Number of backslashes will be halved later, | |
7674 * double them here. | |
7675 */ | |
7676 if (copy) | |
7677 { | |
7678 dst[0] = '\\'; | |
7679 dst[1] = *s; | |
7680 } | |
7681 dst += 2; | |
7682 } | |
7683 else | |
7684 { | |
772 | 7685 #ifdef FEAT_MBYTE |
7686 if (has_mbyte) | |
7687 c = mb_ptr2char(s); | |
7688 else | |
7689 #endif | |
7690 c = *s; | |
7691 | |
4244 | 7692 if (func_one != (fptr_T)NULL) |
7693 /* Turbo C complains without the typecast */ | |
7694 func_one = (fptr_T)(func_one(&cc, c)); | |
7695 else if (func_all != (fptr_T)NULL) | |
7696 /* Turbo C complains without the typecast */ | |
7697 func_all = (fptr_T)(func_all(&cc, c)); | |
7698 else /* just copy */ | |
772 | 7699 cc = c; |
7700 | |
7701 #ifdef FEAT_MBYTE | |
7702 if (has_mbyte) | |
7 | 7703 { |
1332 | 7704 int l; |
7705 | |
7706 /* Copy composing characters separately, one | |
7707 * at a time. */ | |
7708 if (enc_utf8) | |
7709 l = utf_ptr2len(s) - 1; | |
7710 else | |
7711 l = mb_ptr2len(s) - 1; | |
772 | 7712 |
7713 s += l; | |
7714 len -= l; | |
7715 if (copy) | |
7716 mb_char2bytes(cc, dst); | |
7717 dst += mb_char2len(cc) - 1; | |
7 | 7718 } |
772 | 7719 else |
7720 #endif | |
7721 if (copy) | |
7722 *dst = cc; | |
7723 dst++; | |
7 | 7724 } |
772 | 7725 |
7 | 7726 ++s; |
7727 --len; | |
7728 } | |
7729 } | |
7730 } | |
7731 no = -1; | |
7732 } | |
7733 } | |
7734 if (copy) | |
7735 *dst = NUL; | |
7736 | |
7737 exit: | |
7738 return (int)((dst - dest) + 1); | |
7739 } | |
7740 | |
7741 #ifdef FEAT_EVAL | |
2012 | 7742 static char_u *reg_getline_submatch __ARGS((linenr_T lnum)); |
7743 | |
7 | 7744 /* |
2011 | 7745 * Call reg_getline() with the line numbers from the submatch. If a |
7746 * substitute() was used the reg_maxline and other values have been | |
7747 * overwritten. | |
7748 */ | |
7749 static char_u * | |
7750 reg_getline_submatch(lnum) | |
7751 linenr_T lnum; | |
7752 { | |
7753 char_u *s; | |
7754 linenr_T save_first = reg_firstlnum; | |
7755 linenr_T save_max = reg_maxline; | |
7756 | |
7757 reg_firstlnum = submatch_firstlnum; | |
7758 reg_maxline = submatch_maxline; | |
7759 | |
7760 s = reg_getline(lnum); | |
7761 | |
7762 reg_firstlnum = save_first; | |
7763 reg_maxline = save_max; | |
7764 return s; | |
7765 } | |
7766 | |
7767 /* | |
1209 | 7768 * Used for the submatch() function: get the string from the n'th submatch in |
7 | 7769 * allocated memory. |
7770 * Returns NULL when not in a ":s" command and for a non-existing submatch. | |
7771 */ | |
7772 char_u * | |
7773 reg_submatch(no) | |
7774 int no; | |
7775 { | |
7776 char_u *retval = NULL; | |
7777 char_u *s; | |
7778 int len; | |
7779 int round; | |
7780 linenr_T lnum; | |
7781 | |
840 | 7782 if (!can_f_submatch || no < 0) |
7 | 7783 return NULL; |
7784 | |
7785 if (submatch_match == NULL) | |
7786 { | |
7787 /* | |
7788 * First round: compute the length and allocate memory. | |
7789 * Second round: copy the text. | |
7790 */ | |
7791 for (round = 1; round <= 2; ++round) | |
7792 { | |
7793 lnum = submatch_mmatch->startpos[no].lnum; | |
7794 if (lnum < 0 || submatch_mmatch->endpos[no].lnum < 0) | |
7795 return NULL; | |
7796 | |
2011 | 7797 s = reg_getline_submatch(lnum) + submatch_mmatch->startpos[no].col; |
7 | 7798 if (s == NULL) /* anti-crash check, cannot happen? */ |
7799 break; | |
7800 if (submatch_mmatch->endpos[no].lnum == lnum) | |
7801 { | |
7802 /* Within one line: take form start to end col. */ | |
7803 len = submatch_mmatch->endpos[no].col | |
7804 - submatch_mmatch->startpos[no].col; | |
7805 if (round == 2) | |
418 | 7806 vim_strncpy(retval, s, len); |
7 | 7807 ++len; |
7808 } | |
7809 else | |
7810 { | |
7811 /* Multiple lines: take start line from start col, middle | |
7812 * lines completely and end line up to end col. */ | |
7813 len = (int)STRLEN(s); | |
7814 if (round == 2) | |
7815 { | |
7816 STRCPY(retval, s); | |
7817 retval[len] = '\n'; | |
7818 } | |
7819 ++len; | |
7820 ++lnum; | |
7821 while (lnum < submatch_mmatch->endpos[no].lnum) | |
7822 { | |
2011 | 7823 s = reg_getline_submatch(lnum++); |
7 | 7824 if (round == 2) |
7825 STRCPY(retval + len, s); | |
7826 len += (int)STRLEN(s); | |
7827 if (round == 2) | |
7828 retval[len] = '\n'; | |
7829 ++len; | |
7830 } | |
7831 if (round == 2) | |
2011 | 7832 STRNCPY(retval + len, reg_getline_submatch(lnum), |
7 | 7833 submatch_mmatch->endpos[no].col); |
7834 len += submatch_mmatch->endpos[no].col; | |
7835 if (round == 2) | |
7836 retval[len] = NUL; | |
7837 ++len; | |
7838 } | |
7839 | |
840 | 7840 if (retval == NULL) |
7 | 7841 { |
7842 retval = lalloc((long_u)len, TRUE); | |
840 | 7843 if (retval == NULL) |
7 | 7844 return NULL; |
7845 } | |
7846 } | |
7847 } | |
7848 else | |
7849 { | |
1815 | 7850 s = submatch_match->startp[no]; |
7851 if (s == NULL || submatch_match->endp[no] == NULL) | |
7 | 7852 retval = NULL; |
7853 else | |
7854 retval = vim_strnsave(s, (int)(submatch_match->endp[no] - s)); | |
7855 } | |
7856 | |
7857 return retval; | |
7858 } | |
7859 #endif | |
4444 | 7860 |
7861 static regengine_T bt_regengine = | |
7862 { | |
7863 bt_regcomp, | |
7864 bt_regexec, | |
7865 #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \ | |
7866 || defined(FIND_REPLACE_DIALOG) || defined(PROTO) | |
7867 bt_regexec_nl, | |
7868 #endif | |
7869 bt_regexec_multi | |
7870 #ifdef DEBUG | |
7871 ,(char_u *)"" | |
7872 #endif | |
7873 }; | |
7874 | |
7875 | |
7876 #include "regexp_nfa.c" | |
7877 | |
7878 static regengine_T nfa_regengine = | |
7879 { | |
7880 nfa_regcomp, | |
7881 nfa_regexec, | |
7882 #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \ | |
7883 || defined(FIND_REPLACE_DIALOG) || defined(PROTO) | |
7884 nfa_regexec_nl, | |
7885 #endif | |
7886 nfa_regexec_multi | |
7887 #ifdef DEBUG | |
7888 ,(char_u *)"" | |
7889 #endif | |
7890 }; | |
7891 | |
7892 /* Which regexp engine to use? Needed for vim_regcomp(). | |
7893 * Must match with 'regexpengine'. */ | |
7894 static int regexp_engine = 0; | |
7895 #define AUTOMATIC_ENGINE 0 | |
7896 #define BACKTRACKING_ENGINE 1 | |
7897 #define NFA_ENGINE 2 | |
7898 #ifdef DEBUG | |
7899 static char_u regname[][30] = { | |
7900 "AUTOMATIC Regexp Engine", | |
4579
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
7901 "BACKTRACKING Regexp Engine", |
4444 | 7902 "NFA Regexp Engine" |
7903 }; | |
7904 #endif | |
7905 | |
7906 /* | |
7907 * Compile a regular expression into internal code. | |
7908 * Returns the program in allocated memory. Returns NULL for an error. | |
7909 */ | |
7910 regprog_T * | |
7911 vim_regcomp(expr_arg, re_flags) | |
7912 char_u *expr_arg; | |
7913 int re_flags; | |
7914 { | |
7915 regprog_T *prog = NULL; | |
7916 char_u *expr = expr_arg; | |
7917 | |
7918 syntax_error = FALSE; | |
7919 regexp_engine = p_re; | |
7920 | |
7921 /* Check for prefix "\%#=", that sets the regexp engine */ | |
7922 if (STRNCMP(expr, "\\%#=", 4) == 0) | |
7923 { | |
7924 int newengine = expr[4] - '0'; | |
7925 | |
7926 if (newengine == AUTOMATIC_ENGINE | |
7927 || newengine == BACKTRACKING_ENGINE | |
7928 || newengine == NFA_ENGINE) | |
7929 { | |
7930 regexp_engine = expr[4] - '0'; | |
7931 expr += 5; | |
7932 #ifdef DEBUG | |
7933 EMSG3("New regexp mode selected (%d): %s", regexp_engine, | |
7934 regname[newengine]); | |
7935 #endif | |
7936 } | |
7937 else | |
7938 { | |
7939 EMSG(_("E864: \\%#= can only be followed by 0, 1, or 2. The automatic engine will be used ")); | |
7940 regexp_engine = AUTOMATIC_ENGINE; | |
7941 } | |
7942 } | |
7943 #ifdef DEBUG | |
7944 bt_regengine.expr = expr; | |
7945 nfa_regengine.expr = expr; | |
7946 #endif | |
7947 | |
7948 /* | |
7949 * First try the NFA engine, unless backtracking was requested. | |
7950 */ | |
7951 if (regexp_engine != BACKTRACKING_ENGINE) | |
7952 prog = nfa_regengine.regcomp(expr, re_flags); | |
7953 else | |
7954 prog = bt_regengine.regcomp(expr, re_flags); | |
7955 | |
7956 if (prog == NULL) /* error compiling regexp with initial engine */ | |
7957 { | |
4460 | 7958 #ifdef BT_REGEXP_DEBUG_LOG |
4444 | 7959 if (regexp_engine != BACKTRACKING_ENGINE) /* debugging log for NFA */ |
7960 { | |
7961 FILE *f; | |
4460 | 7962 f = fopen(BT_REGEXP_DEBUG_LOG_NAME, "a"); |
4444 | 7963 if (f) |
7964 { | |
7965 if (!syntax_error) | |
7966 fprintf(f, "NFA engine could not handle \"%s\"\n", expr); | |
7967 else | |
7968 fprintf(f, "Syntax error in \"%s\"\n", expr); | |
7969 fclose(f); | |
7970 } | |
7971 else | |
4460 | 7972 EMSG2("(NFA) Could not open \"%s\" to write !!!", |
7973 BT_REGEXP_DEBUG_LOG_NAME); | |
4444 | 7974 /* |
7975 if (syntax_error) | |
7976 EMSG("NFA Regexp: Syntax Error !"); | |
7977 */ | |
7978 } | |
7979 #endif | |
7980 /* | |
7981 * If NFA engine failed, then revert to the backtracking engine. | |
7982 * Except when there was a syntax error, which was properly handled by | |
7983 * NFA engine. | |
7984 */ | |
7985 if (regexp_engine == AUTOMATIC_ENGINE) | |
7986 if (!syntax_error) | |
7987 prog = bt_regengine.regcomp(expr, re_flags); | |
7988 | |
7989 } /* endif prog==NULL */ | |
7990 | |
7991 | |
7992 return prog; | |
7993 } | |
7994 | |
7995 /* | |
7996 * Match a regexp against a string. | |
7997 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp(). | |
7998 * Uses curbuf for line count and 'iskeyword'. | |
7999 * | |
8000 * Return TRUE if there is a match, FALSE if not. | |
8001 */ | |
8002 int | |
8003 vim_regexec(rmp, line, col) | |
8004 regmatch_T *rmp; | |
8005 char_u *line; /* string to match against */ | |
8006 colnr_T col; /* column to start looking for match */ | |
8007 { | |
8008 return rmp->regprog->engine->regexec(rmp, line, col); | |
8009 } | |
8010 | |
8011 #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \ | |
8012 || defined(FIND_REPLACE_DIALOG) || defined(PROTO) | |
8013 /* | |
8014 * Like vim_regexec(), but consider a "\n" in "line" to be a line break. | |
8015 */ | |
8016 int | |
8017 vim_regexec_nl(rmp, line, col) | |
8018 regmatch_T *rmp; | |
8019 char_u *line; | |
8020 colnr_T col; | |
8021 { | |
8022 return rmp->regprog->engine->regexec_nl(rmp, line, col); | |
8023 } | |
8024 #endif | |
8025 | |
8026 /* | |
8027 * Match a regexp against multiple lines. | |
8028 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp(). | |
8029 * Uses curbuf for line count and 'iskeyword'. | |
8030 * | |
8031 * Return zero if there is no match. Return number of lines contained in the | |
8032 * match otherwise. | |
8033 */ | |
8034 long | |
8035 vim_regexec_multi(rmp, win, buf, lnum, col, tm) | |
8036 regmmatch_T *rmp; | |
8037 win_T *win; /* window in which to search or NULL */ | |
8038 buf_T *buf; /* buffer in which to search */ | |
8039 linenr_T lnum; /* nr of line to start looking for match */ | |
8040 colnr_T col; /* column to start looking for match */ | |
8041 proftime_T *tm; /* timeout limit or NULL */ | |
8042 { | |
8043 return rmp->regprog->engine->regexec_multi(rmp, win, buf, lnum, col, tm); | |
8044 } |