Mercurial > vim
annotate src/regexp.c @ 4727:85c04c7963d1
Added tag v7-3-1110 for changeset 3849c811cc0b
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Tue, 04 Jun 2013 14:23:05 +0200 |
parents | bd6bef0bd0fb |
children | 749e2b2755d5 |
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 | |
39 | 4168 #define ADVANCE_REGINPUT() mb_ptr_adv(reginput) |
7 | 4169 |
4170 /* | |
4171 * The arguments from BRACE_LIMITS are stored here. They are actually local | |
4172 * to regmatch(), but they are here to reduce the amount of stack space used | |
4173 * (it can be called recursively many times). | |
4174 */ | |
4175 static long bl_minval; | |
4176 static long bl_maxval; | |
4177 | |
4178 /* | |
4179 * regmatch - main matching routine | |
4180 * | |
180 | 4181 * Conceptually the strategy is simple: Check to see whether the current node |
4182 * matches, push an item onto the regstack and loop to see whether the rest | |
4183 * matches, and then act accordingly. In practice we make some effort to | |
4184 * avoid using the regstack, in particular by going through "ordinary" nodes | |
4185 * (that don't need to know whether the rest of the match failed) by a nested | |
4186 * loop. | |
7 | 4187 * |
4188 * Returns TRUE when there is a match. Leaves reginput and reglnum just after | |
4189 * the last matched character. | |
4190 * Returns FALSE when there is no match. Leaves reginput and reglnum in an | |
4191 * undefined state! | |
4192 */ | |
4193 static int | |
180 | 4194 regmatch(scan) |
7 | 4195 char_u *scan; /* Current node. */ |
4196 { | |
180 | 4197 char_u *next; /* Next node. */ |
4198 int op; | |
4199 int c; | |
4200 regitem_T *rp; | |
4201 int no; | |
4202 int status; /* one of the RA_ values: */ | |
4203 #define RA_FAIL 1 /* something failed, abort */ | |
4204 #define RA_CONT 2 /* continue in inner loop */ | |
4205 #define RA_BREAK 3 /* break inner loop */ | |
4206 #define RA_MATCH 4 /* successful match */ | |
4207 #define RA_NOMATCH 5 /* didn't match */ | |
270 | 4208 |
1520 | 4209 /* Make "regstack" and "backpos" empty. They are allocated and freed in |
4444 | 4210 * bt_regexec_both() to reduce malloc()/free() calls. */ |
270 | 4211 regstack.ga_len = 0; |
4212 backpos.ga_len = 0; | |
233 | 4213 |
180 | 4214 /* |
233 | 4215 * Repeat until "regstack" is empty. |
180 | 4216 */ |
4217 for (;;) | |
4218 { | |
4444 | 4219 /* Some patterns may cause a long time to match, even though they are not |
7 | 4220 * illegal. E.g., "\([a-z]\+\)\+Q". Allow breaking them with CTRL-C. */ |
4221 fast_breakcheck(); | |
4222 | |
4223 #ifdef DEBUG | |
4224 if (scan != NULL && regnarrate) | |
4225 { | |
4444 | 4226 mch_errmsg((char *)regprop(scan)); |
7 | 4227 mch_errmsg("(\n"); |
4228 } | |
4229 #endif | |
180 | 4230 |
4231 /* | |
233 | 4232 * Repeat for items that can be matched sequentially, without using the |
180 | 4233 * regstack. |
4234 */ | |
4235 for (;;) | |
7 | 4236 { |
180 | 4237 if (got_int || scan == NULL) |
4238 { | |
4239 status = RA_FAIL; | |
4240 break; | |
4241 } | |
4242 status = RA_CONT; | |
4243 | |
7 | 4244 #ifdef DEBUG |
4245 if (regnarrate) | |
4246 { | |
4444 | 4247 mch_errmsg((char *)regprop(scan)); |
7 | 4248 mch_errmsg("...\n"); |
4249 # ifdef FEAT_SYN_HL | |
4250 if (re_extmatch_in != NULL) | |
4251 { | |
4252 int i; | |
4253 | |
4254 mch_errmsg(_("External submatches:\n")); | |
4255 for (i = 0; i < NSUBEXP; i++) | |
4256 { | |
4257 mch_errmsg(" \""); | |
4258 if (re_extmatch_in->matches[i] != NULL) | |
4444 | 4259 mch_errmsg((char *)re_extmatch_in->matches[i]); |
7 | 4260 mch_errmsg("\"\n"); |
4261 } | |
4262 } | |
4263 # endif | |
4264 } | |
4265 #endif | |
4266 next = regnext(scan); | |
4267 | |
4268 op = OP(scan); | |
4269 /* Check for character class with NL added. */ | |
1018 | 4270 if (!reg_line_lbr && WITH_NL(op) && REG_MULTI |
4271 && *reginput == NUL && reglnum <= reg_maxline) | |
7 | 4272 { |
4273 reg_nextline(); | |
4274 } | |
4275 else if (reg_line_lbr && WITH_NL(op) && *reginput == '\n') | |
4276 { | |
4277 ADVANCE_REGINPUT(); | |
4278 } | |
4279 else | |
4280 { | |
4281 if (WITH_NL(op)) | |
180 | 4282 op -= ADD_NL; |
7 | 4283 #ifdef FEAT_MBYTE |
4284 if (has_mbyte) | |
4285 c = (*mb_ptr2char)(reginput); | |
4286 else | |
4287 #endif | |
4288 c = *reginput; | |
4289 switch (op) | |
4290 { | |
4291 case BOL: | |
4292 if (reginput != regline) | |
180 | 4293 status = RA_NOMATCH; |
7 | 4294 break; |
4295 | |
4296 case EOL: | |
4297 if (c != NUL) | |
180 | 4298 status = RA_NOMATCH; |
7 | 4299 break; |
4300 | |
4301 case RE_BOF: | |
1458 | 4302 /* We're not at the beginning of the file when below the first |
4303 * line where we started, not at the start of the line or we | |
4304 * didn't start at the first line of the buffer. */ | |
7 | 4305 if (reglnum != 0 || reginput != regline |
1458 | 4306 || (REG_MULTI && reg_firstlnum > 1)) |
180 | 4307 status = RA_NOMATCH; |
7 | 4308 break; |
4309 | |
4310 case RE_EOF: | |
4311 if (reglnum != reg_maxline || c != NUL) | |
180 | 4312 status = RA_NOMATCH; |
7 | 4313 break; |
4314 | |
4315 case CURSOR: | |
4316 /* Check if the buffer is in a window and compare the | |
4317 * reg_win->w_cursor position to the match position. */ | |
4318 if (reg_win == NULL | |
4319 || (reglnum + reg_firstlnum != reg_win->w_cursor.lnum) | |
4320 || ((colnr_T)(reginput - regline) != reg_win->w_cursor.col)) | |
180 | 4321 status = RA_NOMATCH; |
7 | 4322 break; |
4323 | |
639 | 4324 case RE_MARK: |
4325 /* Compare the mark position to the match position. NOTE: Always | |
4326 * uses the current buffer. */ | |
4327 { | |
4328 int mark = OPERAND(scan)[0]; | |
4329 int cmp = OPERAND(scan)[1]; | |
4330 pos_T *pos; | |
4331 | |
4043 | 4332 pos = getmark_buf(reg_buf, mark, FALSE); |
1148 | 4333 if (pos == NULL /* mark doesn't exist */ |
639 | 4334 || pos->lnum <= 0 /* mark isn't set (in curbuf) */ |
4335 || (pos->lnum == reglnum + reg_firstlnum | |
4336 ? (pos->col == (colnr_T)(reginput - regline) | |
4337 ? (cmp == '<' || cmp == '>') | |
4338 : (pos->col < (colnr_T)(reginput - regline) | |
4339 ? cmp != '>' | |
4340 : cmp != '<')) | |
4341 : (pos->lnum < reglnum + reg_firstlnum | |
4342 ? cmp != '>' | |
4343 : cmp != '<'))) | |
4344 status = RA_NOMATCH; | |
4345 } | |
4346 break; | |
4347 | |
4348 case RE_VISUAL: | |
4349 #ifdef FEAT_VISUAL | |
4350 /* Check if the buffer is the current buffer. and whether the | |
4351 * position is inside the Visual area. */ | |
4352 if (reg_buf != curbuf || VIsual.lnum == 0) | |
4353 status = RA_NOMATCH; | |
4354 else | |
4355 { | |
4356 pos_T top, bot; | |
4357 linenr_T lnum; | |
4358 colnr_T col; | |
4359 win_T *wp = reg_win == NULL ? curwin : reg_win; | |
4360 int mode; | |
4361 | |
4362 if (VIsual_active) | |
4363 { | |
4364 if (lt(VIsual, wp->w_cursor)) | |
4365 { | |
4366 top = VIsual; | |
4367 bot = wp->w_cursor; | |
4368 } | |
4369 else | |
4370 { | |
4371 top = wp->w_cursor; | |
4372 bot = VIsual; | |
4373 } | |
4374 mode = VIsual_mode; | |
4375 } | |
4376 else | |
4377 { | |
692 | 4378 if (lt(curbuf->b_visual.vi_start, curbuf->b_visual.vi_end)) |
685 | 4379 { |
692 | 4380 top = curbuf->b_visual.vi_start; |
4381 bot = curbuf->b_visual.vi_end; | |
685 | 4382 } |
4383 else | |
4384 { | |
692 | 4385 top = curbuf->b_visual.vi_end; |
4386 bot = curbuf->b_visual.vi_start; | |
685 | 4387 } |
692 | 4388 mode = curbuf->b_visual.vi_mode; |
639 | 4389 } |
4390 lnum = reglnum + reg_firstlnum; | |
4391 col = (colnr_T)(reginput - regline); | |
4392 if (lnum < top.lnum || lnum > bot.lnum) | |
4393 status = RA_NOMATCH; | |
4394 else if (mode == 'v') | |
4395 { | |
4396 if ((lnum == top.lnum && col < top.col) | |
4397 || (lnum == bot.lnum | |
4398 && col >= bot.col + (*p_sel != 'e'))) | |
4399 status = RA_NOMATCH; | |
4400 } | |
4401 else if (mode == Ctrl_V) | |
4402 { | |
4403 colnr_T start, end; | |
4404 colnr_T start2, end2; | |
944 | 4405 colnr_T cols; |
639 | 4406 |
4407 getvvcol(wp, &top, &start, NULL, &end); | |
4408 getvvcol(wp, &bot, &start2, NULL, &end2); | |
4409 if (start2 < start) | |
4410 start = start2; | |
4411 if (end2 > end) | |
4412 end = end2; | |
4413 if (top.col == MAXCOL || bot.col == MAXCOL) | |
4414 end = MAXCOL; | |
944 | 4415 cols = win_linetabsize(wp, |
639 | 4416 regline, (colnr_T)(reginput - regline)); |
944 | 4417 if (cols < start || cols > end - (*p_sel == 'e')) |
639 | 4418 status = RA_NOMATCH; |
4419 } | |
4420 } | |
4421 #else | |
4422 status = RA_NOMATCH; | |
4423 #endif | |
4424 break; | |
4425 | |
7 | 4426 case RE_LNUM: |
4427 if (!REG_MULTI || !re_num_cmp((long_u)(reglnum + reg_firstlnum), | |
4428 scan)) | |
180 | 4429 status = RA_NOMATCH; |
7 | 4430 break; |
4431 | |
4432 case RE_COL: | |
4433 if (!re_num_cmp((long_u)(reginput - regline) + 1, scan)) | |
180 | 4434 status = RA_NOMATCH; |
7 | 4435 break; |
4436 | |
4437 case RE_VCOL: | |
4438 if (!re_num_cmp((long_u)win_linetabsize( | |
4439 reg_win == NULL ? curwin : reg_win, | |
4440 regline, (colnr_T)(reginput - regline)) + 1, scan)) | |
180 | 4441 status = RA_NOMATCH; |
7 | 4442 break; |
4443 | |
4444 case BOW: /* \<word; reginput points to w */ | |
4445 if (c == NUL) /* Can't match at end of line */ | |
180 | 4446 status = RA_NOMATCH; |
7 | 4447 #ifdef FEAT_MBYTE |
180 | 4448 else if (has_mbyte) |
7 | 4449 { |
4450 int this_class; | |
4451 | |
4452 /* Get class of current and previous char (if it exists). */ | |
4069 | 4453 this_class = mb_get_class_buf(reginput, reg_buf); |
7 | 4454 if (this_class <= 1) |
180 | 4455 status = RA_NOMATCH; /* not on a word at all */ |
4456 else if (reg_prev_class() == this_class) | |
4457 status = RA_NOMATCH; /* previous char is in same word */ | |
7 | 4458 } |
4459 #endif | |
4460 else | |
4461 { | |
4061 | 4462 if (!vim_iswordc_buf(c, reg_buf) || (reginput > regline |
4463 && vim_iswordc_buf(reginput[-1], reg_buf))) | |
180 | 4464 status = RA_NOMATCH; |
7 | 4465 } |
4466 break; | |
4467 | |
4468 case EOW: /* word\>; reginput points after d */ | |
4469 if (reginput == regline) /* Can't match at start of line */ | |
180 | 4470 status = RA_NOMATCH; |
7 | 4471 #ifdef FEAT_MBYTE |
180 | 4472 else if (has_mbyte) |
7 | 4473 { |
4474 int this_class, prev_class; | |
4475 | |
4476 /* Get class of current and previous char (if it exists). */ | |
4069 | 4477 this_class = mb_get_class_buf(reginput, reg_buf); |
7 | 4478 prev_class = reg_prev_class(); |
180 | 4479 if (this_class == prev_class |
4480 || prev_class == 0 || prev_class == 1) | |
4481 status = RA_NOMATCH; | |
7 | 4482 } |
180 | 4483 #endif |
7 | 4484 else |
4485 { | |
4043 | 4486 if (!vim_iswordc_buf(reginput[-1], reg_buf) |
4487 || (reginput[0] != NUL && vim_iswordc_buf(c, reg_buf))) | |
180 | 4488 status = RA_NOMATCH; |
7 | 4489 } |
4490 break; /* Matched with EOW */ | |
4491 | |
4492 case ANY: | |
4084 | 4493 /* ANY does not match new lines. */ |
7 | 4494 if (c == NUL) |
180 | 4495 status = RA_NOMATCH; |
4496 else | |
4497 ADVANCE_REGINPUT(); | |
7 | 4498 break; |
4499 | |
4500 case IDENT: | |
4501 if (!vim_isIDc(c)) | |
180 | 4502 status = RA_NOMATCH; |
4503 else | |
4504 ADVANCE_REGINPUT(); | |
7 | 4505 break; |
4506 | |
4507 case SIDENT: | |
4508 if (VIM_ISDIGIT(*reginput) || !vim_isIDc(c)) | |
180 | 4509 status = RA_NOMATCH; |
4510 else | |
4511 ADVANCE_REGINPUT(); | |
7 | 4512 break; |
4513 | |
4514 case KWORD: | |
4069 | 4515 if (!vim_iswordp_buf(reginput, reg_buf)) |
180 | 4516 status = RA_NOMATCH; |
4517 else | |
4518 ADVANCE_REGINPUT(); | |
7 | 4519 break; |
4520 | |
4521 case SKWORD: | |
4069 | 4522 if (VIM_ISDIGIT(*reginput) || !vim_iswordp_buf(reginput, reg_buf)) |
180 | 4523 status = RA_NOMATCH; |
4524 else | |
4525 ADVANCE_REGINPUT(); | |
7 | 4526 break; |
4527 | |
4528 case FNAME: | |
4529 if (!vim_isfilec(c)) | |
180 | 4530 status = RA_NOMATCH; |
4531 else | |
4532 ADVANCE_REGINPUT(); | |
7 | 4533 break; |
4534 | |
4535 case SFNAME: | |
4536 if (VIM_ISDIGIT(*reginput) || !vim_isfilec(c)) | |
180 | 4537 status = RA_NOMATCH; |
4538 else | |
4539 ADVANCE_REGINPUT(); | |
7 | 4540 break; |
4541 | |
4542 case PRINT: | |
4543 if (ptr2cells(reginput) != 1) | |
180 | 4544 status = RA_NOMATCH; |
4545 else | |
4546 ADVANCE_REGINPUT(); | |
7 | 4547 break; |
4548 | |
4549 case SPRINT: | |
4550 if (VIM_ISDIGIT(*reginput) || ptr2cells(reginput) != 1) | |
180 | 4551 status = RA_NOMATCH; |
4552 else | |
4553 ADVANCE_REGINPUT(); | |
7 | 4554 break; |
4555 | |
4556 case WHITE: | |
4557 if (!vim_iswhite(c)) | |
180 | 4558 status = RA_NOMATCH; |
4559 else | |
4560 ADVANCE_REGINPUT(); | |
7 | 4561 break; |
4562 | |
4563 case NWHITE: | |
4564 if (c == NUL || vim_iswhite(c)) | |
180 | 4565 status = RA_NOMATCH; |
4566 else | |
4567 ADVANCE_REGINPUT(); | |
7 | 4568 break; |
4569 | |
4570 case DIGIT: | |
4571 if (!ri_digit(c)) | |
180 | 4572 status = RA_NOMATCH; |
4573 else | |
4574 ADVANCE_REGINPUT(); | |
7 | 4575 break; |
4576 | |
4577 case NDIGIT: | |
4578 if (c == NUL || ri_digit(c)) | |
180 | 4579 status = RA_NOMATCH; |
4580 else | |
4581 ADVANCE_REGINPUT(); | |
7 | 4582 break; |
4583 | |
4584 case HEX: | |
4585 if (!ri_hex(c)) | |
180 | 4586 status = RA_NOMATCH; |
4587 else | |
4588 ADVANCE_REGINPUT(); | |
7 | 4589 break; |
4590 | |
4591 case NHEX: | |
4592 if (c == NUL || ri_hex(c)) | |
180 | 4593 status = RA_NOMATCH; |
4594 else | |
4595 ADVANCE_REGINPUT(); | |
7 | 4596 break; |
4597 | |
4598 case OCTAL: | |
4599 if (!ri_octal(c)) | |
180 | 4600 status = RA_NOMATCH; |
4601 else | |
4602 ADVANCE_REGINPUT(); | |
7 | 4603 break; |
4604 | |
4605 case NOCTAL: | |
4606 if (c == NUL || ri_octal(c)) | |
180 | 4607 status = RA_NOMATCH; |
4608 else | |
4609 ADVANCE_REGINPUT(); | |
7 | 4610 break; |
4611 | |
4612 case WORD: | |
4613 if (!ri_word(c)) | |
180 | 4614 status = RA_NOMATCH; |
4615 else | |
4616 ADVANCE_REGINPUT(); | |
7 | 4617 break; |
4618 | |
4619 case NWORD: | |
4620 if (c == NUL || ri_word(c)) | |
180 | 4621 status = RA_NOMATCH; |
4622 else | |
4623 ADVANCE_REGINPUT(); | |
7 | 4624 break; |
4625 | |
4626 case HEAD: | |
4627 if (!ri_head(c)) | |
180 | 4628 status = RA_NOMATCH; |
4629 else | |
4630 ADVANCE_REGINPUT(); | |
7 | 4631 break; |
4632 | |
4633 case NHEAD: | |
4634 if (c == NUL || ri_head(c)) | |
180 | 4635 status = RA_NOMATCH; |
4636 else | |
4637 ADVANCE_REGINPUT(); | |
7 | 4638 break; |
4639 | |
4640 case ALPHA: | |
4641 if (!ri_alpha(c)) | |
180 | 4642 status = RA_NOMATCH; |
4643 else | |
4644 ADVANCE_REGINPUT(); | |
7 | 4645 break; |
4646 | |
4647 case NALPHA: | |
4648 if (c == NUL || ri_alpha(c)) | |
180 | 4649 status = RA_NOMATCH; |
4650 else | |
4651 ADVANCE_REGINPUT(); | |
7 | 4652 break; |
4653 | |
4654 case LOWER: | |
4655 if (!ri_lower(c)) | |
180 | 4656 status = RA_NOMATCH; |
4657 else | |
4658 ADVANCE_REGINPUT(); | |
7 | 4659 break; |
4660 | |
4661 case NLOWER: | |
4662 if (c == NUL || ri_lower(c)) | |
180 | 4663 status = RA_NOMATCH; |
4664 else | |
4665 ADVANCE_REGINPUT(); | |
7 | 4666 break; |
4667 | |
4668 case UPPER: | |
4669 if (!ri_upper(c)) | |
180 | 4670 status = RA_NOMATCH; |
4671 else | |
4672 ADVANCE_REGINPUT(); | |
7 | 4673 break; |
4674 | |
4675 case NUPPER: | |
4676 if (c == NUL || ri_upper(c)) | |
180 | 4677 status = RA_NOMATCH; |
4678 else | |
4679 ADVANCE_REGINPUT(); | |
7 | 4680 break; |
4681 | |
4682 case EXACTLY: | |
4683 { | |
4684 int len; | |
4685 char_u *opnd; | |
4686 | |
4687 opnd = OPERAND(scan); | |
4688 /* Inline the first byte, for speed. */ | |
4689 if (*opnd != *reginput | |
4690 && (!ireg_ic || ( | |
4691 #ifdef FEAT_MBYTE | |
4692 !enc_utf8 && | |
4693 #endif | |
1347 | 4694 MB_TOLOWER(*opnd) != MB_TOLOWER(*reginput)))) |
180 | 4695 status = RA_NOMATCH; |
4696 else if (*opnd == NUL) | |
7 | 4697 { |
4698 /* match empty string always works; happens when "~" is | |
4699 * empty. */ | |
4700 } | |
4701 else if (opnd[1] == NUL | |
4702 #ifdef FEAT_MBYTE | |
4703 && !(enc_utf8 && ireg_ic) | |
4704 #endif | |
4705 ) | |
4706 ++reginput; /* matched a single char */ | |
4707 else | |
4708 { | |
4709 len = (int)STRLEN(opnd); | |
4710 /* Need to match first byte again for multi-byte. */ | |
4711 if (cstrncmp(opnd, reginput, &len) != 0) | |
180 | 4712 status = RA_NOMATCH; |
7 | 4713 #ifdef FEAT_MBYTE |
4714 /* Check for following composing character. */ | |
180 | 4715 else if (enc_utf8 |
4716 && UTF_COMPOSINGLIKE(reginput, reginput + len)) | |
7 | 4717 { |
4718 /* raaron: This code makes a composing character get | |
4719 * ignored, which is the correct behavior (sometimes) | |
4720 * for voweled Hebrew texts. */ | |
4721 if (!ireg_icombine) | |
180 | 4722 status = RA_NOMATCH; |
7 | 4723 } |
180 | 4724 #endif |
7 | 4725 else |
180 | 4726 reginput += len; |
7 | 4727 } |
4728 } | |
4729 break; | |
4730 | |
4731 case ANYOF: | |
4732 case ANYBUT: | |
4733 if (c == NUL) | |
180 | 4734 status = RA_NOMATCH; |
4735 else if ((cstrchr(OPERAND(scan), c) == NULL) == (op == ANYOF)) | |
4736 status = RA_NOMATCH; | |
4737 else | |
4738 ADVANCE_REGINPUT(); | |
7 | 4739 break; |
4740 | |
4741 #ifdef FEAT_MBYTE | |
4742 case MULTIBYTECODE: | |
4743 if (has_mbyte) | |
4744 { | |
4745 int i, len; | |
4746 char_u *opnd; | |
944 | 4747 int opndc = 0, inpc; |
7 | 4748 |
4749 opnd = OPERAND(scan); | |
4750 /* Safety check (just in case 'encoding' was changed since | |
4751 * compiling the program). */ | |
474 | 4752 if ((len = (*mb_ptr2len)(opnd)) < 2) |
180 | 4753 { |
4754 status = RA_NOMATCH; | |
4755 break; | |
4756 } | |
714 | 4757 if (enc_utf8) |
4758 opndc = mb_ptr2char(opnd); | |
4759 if (enc_utf8 && utf_iscomposing(opndc)) | |
4760 { | |
4761 /* When only a composing char is given match at any | |
4762 * position where that composing char appears. */ | |
4763 status = RA_NOMATCH; | |
4764 for (i = 0; reginput[i] != NUL; i += utf_char2len(inpc)) | |
180 | 4765 { |
714 | 4766 inpc = mb_ptr2char(reginput + i); |
4767 if (!utf_iscomposing(inpc)) | |
4768 { | |
4769 if (i > 0) | |
4770 break; | |
4771 } | |
4772 else if (opndc == inpc) | |
4773 { | |
4774 /* Include all following composing chars. */ | |
4775 len = i + mb_ptr2len(reginput + i); | |
4776 status = RA_MATCH; | |
4777 break; | |
4778 } | |
180 | 4779 } |
714 | 4780 } |
4781 else | |
4782 for (i = 0; i < len; ++i) | |
4783 if (opnd[i] != reginput[i]) | |
4784 { | |
4785 status = RA_NOMATCH; | |
4786 break; | |
4787 } | |
7 | 4788 reginput += len; |
4789 } | |
4790 else | |
180 | 4791 status = RA_NOMATCH; |
7 | 4792 break; |
4793 #endif | |
4794 | |
4795 case NOTHING: | |
4796 break; | |
4797 | |
4798 case BACK: | |
233 | 4799 { |
4800 int i; | |
4801 backpos_T *bp; | |
4802 | |
4803 /* | |
4804 * When we run into BACK we need to check if we don't keep | |
4805 * looping without matching any input. The second and later | |
4806 * times a BACK is encountered it fails if the input is still | |
4807 * at the same position as the previous time. | |
4808 * The positions are stored in "backpos" and found by the | |
4809 * current value of "scan", the position in the RE program. | |
4810 */ | |
4811 bp = (backpos_T *)backpos.ga_data; | |
4812 for (i = 0; i < backpos.ga_len; ++i) | |
4813 if (bp[i].bp_scan == scan) | |
4814 break; | |
4815 if (i == backpos.ga_len) | |
4816 { | |
4817 /* First time at this BACK, make room to store the pos. */ | |
4818 if (ga_grow(&backpos, 1) == FAIL) | |
4819 status = RA_FAIL; | |
4820 else | |
4821 { | |
4822 /* get "ga_data" again, it may have changed */ | |
4823 bp = (backpos_T *)backpos.ga_data; | |
4824 bp[i].bp_scan = scan; | |
4825 ++backpos.ga_len; | |
4826 } | |
4827 } | |
4828 else if (reg_save_equal(&bp[i].bp_pos)) | |
4829 /* Still at same position as last time, fail. */ | |
4830 status = RA_NOMATCH; | |
4831 | |
4832 if (status != RA_FAIL && status != RA_NOMATCH) | |
4833 reg_save(&bp[i].bp_pos, &backpos); | |
4834 } | |
179 | 4835 break; |
4836 | |
7 | 4837 case MOPEN + 0: /* Match start: \zs */ |
4838 case MOPEN + 1: /* \( */ | |
4839 case MOPEN + 2: | |
4840 case MOPEN + 3: | |
4841 case MOPEN + 4: | |
4842 case MOPEN + 5: | |
4843 case MOPEN + 6: | |
4844 case MOPEN + 7: | |
4845 case MOPEN + 8: | |
4846 case MOPEN + 9: | |
4847 { | |
4848 no = op - MOPEN; | |
4849 cleanup_subexpr(); | |
270 | 4850 rp = regstack_push(RS_MOPEN, scan); |
180 | 4851 if (rp == NULL) |
4852 status = RA_FAIL; | |
4853 else | |
4854 { | |
4855 rp->rs_no = no; | |
4856 save_se(&rp->rs_un.sesave, ®_startpos[no], | |
4857 ®_startp[no]); | |
4858 /* We simply continue and handle the result when done. */ | |
4859 } | |
7 | 4860 } |
180 | 4861 break; |
7 | 4862 |
4863 case NOPEN: /* \%( */ | |
4864 case NCLOSE: /* \) after \%( */ | |
270 | 4865 if (regstack_push(RS_NOPEN, scan) == NULL) |
180 | 4866 status = RA_FAIL; |
4867 /* We simply continue and handle the result when done. */ | |
4868 break; | |
7 | 4869 |
4870 #ifdef FEAT_SYN_HL | |
4871 case ZOPEN + 1: | |
4872 case ZOPEN + 2: | |
4873 case ZOPEN + 3: | |
4874 case ZOPEN + 4: | |
4875 case ZOPEN + 5: | |
4876 case ZOPEN + 6: | |
4877 case ZOPEN + 7: | |
4878 case ZOPEN + 8: | |
4879 case ZOPEN + 9: | |
4880 { | |
4881 no = op - ZOPEN; | |
4882 cleanup_zsubexpr(); | |
270 | 4883 rp = regstack_push(RS_ZOPEN, scan); |
180 | 4884 if (rp == NULL) |
4885 status = RA_FAIL; | |
4886 else | |
4887 { | |
4888 rp->rs_no = no; | |
4889 save_se(&rp->rs_un.sesave, ®_startzpos[no], | |
4890 ®_startzp[no]); | |
4891 /* We simply continue and handle the result when done. */ | |
4892 } | |
7 | 4893 } |
180 | 4894 break; |
7 | 4895 #endif |
4896 | |
4897 case MCLOSE + 0: /* Match end: \ze */ | |
4898 case MCLOSE + 1: /* \) */ | |
4899 case MCLOSE + 2: | |
4900 case MCLOSE + 3: | |
4901 case MCLOSE + 4: | |
4902 case MCLOSE + 5: | |
4903 case MCLOSE + 6: | |
4904 case MCLOSE + 7: | |
4905 case MCLOSE + 8: | |
4906 case MCLOSE + 9: | |
4907 { | |
4908 no = op - MCLOSE; | |
4909 cleanup_subexpr(); | |
270 | 4910 rp = regstack_push(RS_MCLOSE, scan); |
180 | 4911 if (rp == NULL) |
4912 status = RA_FAIL; | |
4913 else | |
4914 { | |
4915 rp->rs_no = no; | |
4916 save_se(&rp->rs_un.sesave, ®_endpos[no], ®_endp[no]); | |
4917 /* We simply continue and handle the result when done. */ | |
4918 } | |
7 | 4919 } |
180 | 4920 break; |
7 | 4921 |
4922 #ifdef FEAT_SYN_HL | |
4923 case ZCLOSE + 1: /* \) after \z( */ | |
4924 case ZCLOSE + 2: | |
4925 case ZCLOSE + 3: | |
4926 case ZCLOSE + 4: | |
4927 case ZCLOSE + 5: | |
4928 case ZCLOSE + 6: | |
4929 case ZCLOSE + 7: | |
4930 case ZCLOSE + 8: | |
4931 case ZCLOSE + 9: | |
4932 { | |
4933 no = op - ZCLOSE; | |
4934 cleanup_zsubexpr(); | |
270 | 4935 rp = regstack_push(RS_ZCLOSE, scan); |
180 | 4936 if (rp == NULL) |
4937 status = RA_FAIL; | |
4938 else | |
4939 { | |
4940 rp->rs_no = no; | |
4941 save_se(&rp->rs_un.sesave, ®_endzpos[no], | |
4942 ®_endzp[no]); | |
4943 /* We simply continue and handle the result when done. */ | |
4944 } | |
7 | 4945 } |
180 | 4946 break; |
7 | 4947 #endif |
4948 | |
4949 case BACKREF + 1: | |
4950 case BACKREF + 2: | |
4951 case BACKREF + 3: | |
4952 case BACKREF + 4: | |
4953 case BACKREF + 5: | |
4954 case BACKREF + 6: | |
4955 case BACKREF + 7: | |
4956 case BACKREF + 8: | |
4957 case BACKREF + 9: | |
4958 { | |
4959 int len; | |
4960 linenr_T clnum; | |
4961 colnr_T ccol; | |
4962 char_u *p; | |
4963 | |
4964 no = op - BACKREF; | |
4965 cleanup_subexpr(); | |
4966 if (!REG_MULTI) /* Single-line regexp */ | |
4967 { | |
1815 | 4968 if (reg_startp[no] == NULL || reg_endp[no] == NULL) |
7 | 4969 { |
4970 /* Backref was not set: Match an empty string. */ | |
4971 len = 0; | |
4972 } | |
4973 else | |
4974 { | |
4975 /* Compare current input with back-ref in the same | |
4976 * line. */ | |
4977 len = (int)(reg_endp[no] - reg_startp[no]); | |
4978 if (cstrncmp(reg_startp[no], reginput, &len) != 0) | |
180 | 4979 status = RA_NOMATCH; |
7 | 4980 } |
4981 } | |
4982 else /* Multi-line regexp */ | |
4983 { | |
1815 | 4984 if (reg_startpos[no].lnum < 0 || reg_endpos[no].lnum < 0) |
7 | 4985 { |
4986 /* Backref was not set: Match an empty string. */ | |
4987 len = 0; | |
4988 } | |
4989 else | |
4990 { | |
4991 if (reg_startpos[no].lnum == reglnum | |
4992 && reg_endpos[no].lnum == reglnum) | |
4993 { | |
4994 /* Compare back-ref within the current line. */ | |
4995 len = reg_endpos[no].col - reg_startpos[no].col; | |
4996 if (cstrncmp(regline + reg_startpos[no].col, | |
4997 reginput, &len) != 0) | |
180 | 4998 status = RA_NOMATCH; |
7 | 4999 } |
5000 else | |
5001 { | |
5002 /* Messy situation: Need to compare between two | |
5003 * lines. */ | |
5004 ccol = reg_startpos[no].col; | |
5005 clnum = reg_startpos[no].lnum; | |
5006 for (;;) | |
5007 { | |
5008 /* Since getting one line may invalidate | |
5009 * the other, need to make copy. Slow! */ | |
5010 if (regline != reg_tofree) | |
5011 { | |
5012 len = (int)STRLEN(regline); | |
5013 if (reg_tofree == NULL | |
5014 || len >= (int)reg_tofreelen) | |
5015 { | |
5016 len += 50; /* get some extra */ | |
5017 vim_free(reg_tofree); | |
5018 reg_tofree = alloc(len); | |
5019 if (reg_tofree == NULL) | |
180 | 5020 { |
5021 status = RA_FAIL; /* outof memory!*/ | |
5022 break; | |
5023 } | |
7 | 5024 reg_tofreelen = len; |
5025 } | |
5026 STRCPY(reg_tofree, regline); | |
5027 reginput = reg_tofree | |
5028 + (reginput - regline); | |
5029 regline = reg_tofree; | |
5030 } | |
5031 | |
5032 /* Get the line to compare with. */ | |
5033 p = reg_getline(clnum); | |
5034 if (clnum == reg_endpos[no].lnum) | |
5035 len = reg_endpos[no].col - ccol; | |
5036 else | |
5037 len = (int)STRLEN(p + ccol); | |
5038 | |
5039 if (cstrncmp(p + ccol, reginput, &len) != 0) | |
180 | 5040 { |
5041 status = RA_NOMATCH; /* doesn't match */ | |
5042 break; | |
5043 } | |
7 | 5044 if (clnum == reg_endpos[no].lnum) |
5045 break; /* match and at end! */ | |
481 | 5046 if (reglnum >= reg_maxline) |
180 | 5047 { |
5048 status = RA_NOMATCH; /* text too short */ | |
5049 break; | |
5050 } | |
7 | 5051 |
5052 /* Advance to next line. */ | |
5053 reg_nextline(); | |
5054 ++clnum; | |
5055 ccol = 0; | |
180 | 5056 if (got_int) |
5057 { | |
5058 status = RA_FAIL; | |
5059 break; | |
5060 } | |
7 | 5061 } |
5062 | |
5063 /* found a match! Note that regline may now point | |
5064 * to a copy of the line, that should not matter. */ | |
5065 } | |
5066 } | |
5067 } | |
5068 | |
5069 /* Matched the backref, skip over it. */ | |
5070 reginput += len; | |
5071 } | |
5072 break; | |
5073 | |
5074 #ifdef FEAT_SYN_HL | |
5075 case ZREF + 1: | |
5076 case ZREF + 2: | |
5077 case ZREF + 3: | |
5078 case ZREF + 4: | |
5079 case ZREF + 5: | |
5080 case ZREF + 6: | |
5081 case ZREF + 7: | |
5082 case ZREF + 8: | |
5083 case ZREF + 9: | |
5084 { | |
5085 int len; | |
5086 | |
5087 cleanup_zsubexpr(); | |
5088 no = op - ZREF; | |
5089 if (re_extmatch_in != NULL | |
5090 && re_extmatch_in->matches[no] != NULL) | |
5091 { | |
5092 len = (int)STRLEN(re_extmatch_in->matches[no]); | |
5093 if (cstrncmp(re_extmatch_in->matches[no], | |
5094 reginput, &len) != 0) | |
180 | 5095 status = RA_NOMATCH; |
5096 else | |
5097 reginput += len; | |
7 | 5098 } |
5099 else | |
5100 { | |
5101 /* Backref was not set: Match an empty string. */ | |
5102 } | |
5103 } | |
5104 break; | |
5105 #endif | |
5106 | |
5107 case BRANCH: | |
5108 { | |
5109 if (OP(next) != BRANCH) /* No choice. */ | |
5110 next = OPERAND(scan); /* Avoid recursion. */ | |
5111 else | |
5112 { | |
270 | 5113 rp = regstack_push(RS_BRANCH, scan); |
180 | 5114 if (rp == NULL) |
5115 status = RA_FAIL; | |
5116 else | |
5117 status = RA_BREAK; /* rest is below */ | |
7 | 5118 } |
5119 } | |
5120 break; | |
5121 | |
5122 case BRACE_LIMITS: | |
5123 { | |
5124 if (OP(next) == BRACE_SIMPLE) | |
5125 { | |
5126 bl_minval = OPERAND_MIN(scan); | |
5127 bl_maxval = OPERAND_MAX(scan); | |
5128 } | |
5129 else if (OP(next) >= BRACE_COMPLEX | |
5130 && OP(next) < BRACE_COMPLEX + 10) | |
5131 { | |
5132 no = OP(next) - BRACE_COMPLEX; | |
5133 brace_min[no] = OPERAND_MIN(scan); | |
5134 brace_max[no] = OPERAND_MAX(scan); | |
5135 brace_count[no] = 0; | |
5136 } | |
5137 else | |
5138 { | |
5139 EMSG(_(e_internal)); /* Shouldn't happen */ | |
180 | 5140 status = RA_FAIL; |
7 | 5141 } |
5142 } | |
5143 break; | |
5144 | |
5145 case BRACE_COMPLEX + 0: | |
5146 case BRACE_COMPLEX + 1: | |
5147 case BRACE_COMPLEX + 2: | |
5148 case BRACE_COMPLEX + 3: | |
5149 case BRACE_COMPLEX + 4: | |
5150 case BRACE_COMPLEX + 5: | |
5151 case BRACE_COMPLEX + 6: | |
5152 case BRACE_COMPLEX + 7: | |
5153 case BRACE_COMPLEX + 8: | |
5154 case BRACE_COMPLEX + 9: | |
5155 { | |
5156 no = op - BRACE_COMPLEX; | |
5157 ++brace_count[no]; | |
5158 | |
5159 /* If not matched enough times yet, try one more */ | |
5160 if (brace_count[no] <= (brace_min[no] <= brace_max[no] | |
180 | 5161 ? brace_min[no] : brace_max[no])) |
7 | 5162 { |
270 | 5163 rp = regstack_push(RS_BRCPLX_MORE, scan); |
180 | 5164 if (rp == NULL) |
5165 status = RA_FAIL; | |
5166 else | |
5167 { | |
5168 rp->rs_no = no; | |
233 | 5169 reg_save(&rp->rs_un.regsave, &backpos); |
180 | 5170 next = OPERAND(scan); |
5171 /* We continue and handle the result when done. */ | |
5172 } | |
5173 break; | |
7 | 5174 } |
5175 | |
5176 /* If matched enough times, may try matching some more */ | |
5177 if (brace_min[no] <= brace_max[no]) | |
5178 { | |
5179 /* Range is the normal way around, use longest match */ | |
5180 if (brace_count[no] <= brace_max[no]) | |
5181 { | |
270 | 5182 rp = regstack_push(RS_BRCPLX_LONG, scan); |
180 | 5183 if (rp == NULL) |
5184 status = RA_FAIL; | |
5185 else | |
5186 { | |
5187 rp->rs_no = no; | |
233 | 5188 reg_save(&rp->rs_un.regsave, &backpos); |
180 | 5189 next = OPERAND(scan); |
5190 /* We continue and handle the result when done. */ | |
5191 } | |
7 | 5192 } |
5193 } | |
5194 else | |
5195 { | |
5196 /* Range is backwards, use shortest match first */ | |
5197 if (brace_count[no] <= brace_min[no]) | |
5198 { | |
270 | 5199 rp = regstack_push(RS_BRCPLX_SHORT, scan); |
180 | 5200 if (rp == NULL) |
5201 status = RA_FAIL; | |
5202 else | |
5203 { | |
233 | 5204 reg_save(&rp->rs_un.regsave, &backpos); |
180 | 5205 /* We continue and handle the result when done. */ |
5206 } | |
7 | 5207 } |
5208 } | |
5209 } | |
5210 break; | |
5211 | |
5212 case BRACE_SIMPLE: | |
5213 case STAR: | |
5214 case PLUS: | |
5215 { | |
180 | 5216 regstar_T rst; |
7 | 5217 |
5218 /* | |
5219 * Lookahead to avoid useless match attempts when we know | |
5220 * what character comes next. | |
5221 */ | |
5222 if (OP(next) == EXACTLY) | |
5223 { | |
180 | 5224 rst.nextb = *OPERAND(next); |
7 | 5225 if (ireg_ic) |
5226 { | |
1347 | 5227 if (MB_ISUPPER(rst.nextb)) |
5228 rst.nextb_ic = MB_TOLOWER(rst.nextb); | |
7 | 5229 else |
1347 | 5230 rst.nextb_ic = MB_TOUPPER(rst.nextb); |
7 | 5231 } |
5232 else | |
180 | 5233 rst.nextb_ic = rst.nextb; |
7 | 5234 } |
5235 else | |
5236 { | |
180 | 5237 rst.nextb = NUL; |
5238 rst.nextb_ic = NUL; | |
7 | 5239 } |
5240 if (op != BRACE_SIMPLE) | |
5241 { | |
180 | 5242 rst.minval = (op == STAR) ? 0 : 1; |
5243 rst.maxval = MAX_LIMIT; | |
7 | 5244 } |
5245 else | |
5246 { | |
180 | 5247 rst.minval = bl_minval; |
5248 rst.maxval = bl_maxval; | |
7 | 5249 } |
5250 | |
5251 /* | |
5252 * When maxval > minval, try matching as much as possible, up | |
5253 * to maxval. When maxval < minval, try matching at least the | |
5254 * minimal number (since the range is backwards, that's also | |
5255 * maxval!). | |
5256 */ | |
180 | 5257 rst.count = regrepeat(OPERAND(scan), rst.maxval); |
7 | 5258 if (got_int) |
180 | 5259 { |
5260 status = RA_FAIL; | |
5261 break; | |
5262 } | |
5263 if (rst.minval <= rst.maxval | |
5264 ? rst.count >= rst.minval : rst.count >= rst.maxval) | |
7 | 5265 { |
180 | 5266 /* It could match. Prepare for trying to match what |
5267 * follows. The code is below. Parameters are stored in | |
5268 * a regstar_T on the regstack. */ | |
212 | 5269 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp) |
189 | 5270 { |
5271 EMSG(_(e_maxmempat)); | |
5272 status = RA_FAIL; | |
5273 } | |
5274 else if (ga_grow(®stack, sizeof(regstar_T)) == FAIL) | |
180 | 5275 status = RA_FAIL; |
5276 else | |
7 | 5277 { |
180 | 5278 regstack.ga_len += sizeof(regstar_T); |
270 | 5279 rp = regstack_push(rst.minval <= rst.maxval |
233 | 5280 ? RS_STAR_LONG : RS_STAR_SHORT, scan); |
180 | 5281 if (rp == NULL) |
5282 status = RA_FAIL; | |
5283 else | |
7 | 5284 { |
180 | 5285 *(((regstar_T *)rp) - 1) = rst; |
5286 status = RA_BREAK; /* skip the restore bits */ | |
7 | 5287 } |
5288 } | |
5289 } | |
5290 else | |
180 | 5291 status = RA_NOMATCH; |
5292 | |
7 | 5293 } |
180 | 5294 break; |
7 | 5295 |
5296 case NOMATCH: | |
5297 case MATCH: | |
5298 case SUBPAT: | |
270 | 5299 rp = regstack_push(RS_NOMATCH, scan); |
180 | 5300 if (rp == NULL) |
5301 status = RA_FAIL; | |
5302 else | |
7 | 5303 { |
180 | 5304 rp->rs_no = op; |
233 | 5305 reg_save(&rp->rs_un.regsave, &backpos); |
180 | 5306 next = OPERAND(scan); |
5307 /* We continue and handle the result when done. */ | |
7 | 5308 } |
5309 break; | |
5310 | |
5311 case BEHIND: | |
5312 case NOBEHIND: | |
180 | 5313 /* Need a bit of room to store extra positions. */ |
212 | 5314 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp) |
189 | 5315 { |
5316 EMSG(_(e_maxmempat)); | |
5317 status = RA_FAIL; | |
5318 } | |
5319 else if (ga_grow(®stack, sizeof(regbehind_T)) == FAIL) | |
180 | 5320 status = RA_FAIL; |
5321 else | |
7 | 5322 { |
180 | 5323 regstack.ga_len += sizeof(regbehind_T); |
270 | 5324 rp = regstack_push(RS_BEHIND1, scan); |
180 | 5325 if (rp == NULL) |
5326 status = RA_FAIL; | |
5327 else | |
7 | 5328 { |
1579 | 5329 /* Need to save the subexpr to be able to restore them |
5330 * when there is a match but we don't use it. */ | |
5331 save_subexpr(((regbehind_T *)rp) - 1); | |
5332 | |
180 | 5333 rp->rs_no = op; |
233 | 5334 reg_save(&rp->rs_un.regsave, &backpos); |
180 | 5335 /* First try if what follows matches. If it does then we |
5336 * check the behind match by looping. */ | |
7 | 5337 } |
5338 } | |
180 | 5339 break; |
7 | 5340 |
5341 case BHPOS: | |
5342 if (REG_MULTI) | |
5343 { | |
5344 if (behind_pos.rs_u.pos.col != (colnr_T)(reginput - regline) | |
5345 || behind_pos.rs_u.pos.lnum != reglnum) | |
180 | 5346 status = RA_NOMATCH; |
7 | 5347 } |
5348 else if (behind_pos.rs_u.ptr != reginput) | |
180 | 5349 status = RA_NOMATCH; |
7 | 5350 break; |
5351 | |
5352 case NEWL: | |
1018 | 5353 if ((c != NUL || !REG_MULTI || reglnum > reg_maxline |
5354 || reg_line_lbr) && (c != '\n' || !reg_line_lbr)) | |
180 | 5355 status = RA_NOMATCH; |
5356 else if (reg_line_lbr) | |
7 | 5357 ADVANCE_REGINPUT(); |
5358 else | |
5359 reg_nextline(); | |
5360 break; | |
5361 | |
5362 case END: | |
180 | 5363 status = RA_MATCH; /* Success! */ |
5364 break; | |
7 | 5365 |
5366 default: | |
5367 EMSG(_(e_re_corr)); | |
5368 #ifdef DEBUG | |
5369 printf("Illegal op code %d\n", op); | |
5370 #endif | |
180 | 5371 status = RA_FAIL; |
5372 break; | |
7 | 5373 } |
5374 } | |
5375 | |
180 | 5376 /* If we can't continue sequentially, break the inner loop. */ |
5377 if (status != RA_CONT) | |
5378 break; | |
5379 | |
5380 /* Continue in inner loop, advance to next item. */ | |
7 | 5381 scan = next; |
180 | 5382 |
5383 } /* end of inner loop */ | |
7 | 5384 |
5385 /* | |
180 | 5386 * If there is something on the regstack execute the code for the state. |
233 | 5387 * If the state is popped then loop and use the older state. |
7 | 5388 */ |
180 | 5389 while (regstack.ga_len > 0 && status != RA_FAIL) |
5390 { | |
5391 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len) - 1; | |
5392 switch (rp->rs_state) | |
5393 { | |
5394 case RS_NOPEN: | |
5395 /* Result is passed on as-is, simply pop the state. */ | |
270 | 5396 regstack_pop(&scan); |
180 | 5397 break; |
5398 | |
5399 case RS_MOPEN: | |
5400 /* Pop the state. Restore pointers when there is no match. */ | |
5401 if (status == RA_NOMATCH) | |
5402 restore_se(&rp->rs_un.sesave, ®_startpos[rp->rs_no], | |
5403 ®_startp[rp->rs_no]); | |
270 | 5404 regstack_pop(&scan); |
180 | 5405 break; |
5406 | |
5407 #ifdef FEAT_SYN_HL | |
5408 case RS_ZOPEN: | |
5409 /* Pop the state. Restore pointers when there is no match. */ | |
5410 if (status == RA_NOMATCH) | |
5411 restore_se(&rp->rs_un.sesave, ®_startzpos[rp->rs_no], | |
5412 ®_startzp[rp->rs_no]); | |
270 | 5413 regstack_pop(&scan); |
180 | 5414 break; |
5415 #endif | |
5416 | |
5417 case RS_MCLOSE: | |
5418 /* Pop the state. Restore pointers when there is no match. */ | |
5419 if (status == RA_NOMATCH) | |
5420 restore_se(&rp->rs_un.sesave, ®_endpos[rp->rs_no], | |
5421 ®_endp[rp->rs_no]); | |
270 | 5422 regstack_pop(&scan); |
180 | 5423 break; |
5424 | |
5425 #ifdef FEAT_SYN_HL | |
5426 case RS_ZCLOSE: | |
5427 /* Pop the state. Restore pointers when there is no match. */ | |
5428 if (status == RA_NOMATCH) | |
5429 restore_se(&rp->rs_un.sesave, ®_endzpos[rp->rs_no], | |
5430 ®_endzp[rp->rs_no]); | |
270 | 5431 regstack_pop(&scan); |
180 | 5432 break; |
5433 #endif | |
5434 | |
5435 case RS_BRANCH: | |
5436 if (status == RA_MATCH) | |
5437 /* this branch matched, use it */ | |
270 | 5438 regstack_pop(&scan); |
180 | 5439 else |
5440 { | |
5441 if (status != RA_BREAK) | |
5442 { | |
5443 /* After a non-matching branch: try next one. */ | |
233 | 5444 reg_restore(&rp->rs_un.regsave, &backpos); |
180 | 5445 scan = rp->rs_scan; |
5446 } | |
5447 if (scan == NULL || OP(scan) != BRANCH) | |
5448 { | |
5449 /* no more branches, didn't find a match */ | |
5450 status = RA_NOMATCH; | |
270 | 5451 regstack_pop(&scan); |
180 | 5452 } |
5453 else | |
5454 { | |
5455 /* Prepare to try a branch. */ | |
5456 rp->rs_scan = regnext(scan); | |
233 | 5457 reg_save(&rp->rs_un.regsave, &backpos); |
180 | 5458 scan = OPERAND(scan); |
5459 } | |
5460 } | |
5461 break; | |
5462 | |
5463 case RS_BRCPLX_MORE: | |
5464 /* Pop the state. Restore pointers when there is no match. */ | |
5465 if (status == RA_NOMATCH) | |
5466 { | |
233 | 5467 reg_restore(&rp->rs_un.regsave, &backpos); |
180 | 5468 --brace_count[rp->rs_no]; /* decrement match count */ |
5469 } | |
270 | 5470 regstack_pop(&scan); |
180 | 5471 break; |
5472 | |
5473 case RS_BRCPLX_LONG: | |
5474 /* Pop the state. Restore pointers when there is no match. */ | |
5475 if (status == RA_NOMATCH) | |
5476 { | |
5477 /* There was no match, but we did find enough matches. */ | |
233 | 5478 reg_restore(&rp->rs_un.regsave, &backpos); |
180 | 5479 --brace_count[rp->rs_no]; |
5480 /* continue with the items after "\{}" */ | |
5481 status = RA_CONT; | |
5482 } | |
270 | 5483 regstack_pop(&scan); |
180 | 5484 if (status == RA_CONT) |
5485 scan = regnext(scan); | |
5486 break; | |
5487 | |
5488 case RS_BRCPLX_SHORT: | |
5489 /* Pop the state. Restore pointers when there is no match. */ | |
5490 if (status == RA_NOMATCH) | |
5491 /* There was no match, try to match one more item. */ | |
233 | 5492 reg_restore(&rp->rs_un.regsave, &backpos); |
270 | 5493 regstack_pop(&scan); |
180 | 5494 if (status == RA_NOMATCH) |
5495 { | |
5496 scan = OPERAND(scan); | |
5497 status = RA_CONT; | |
5498 } | |
5499 break; | |
5500 | |
5501 case RS_NOMATCH: | |
5502 /* Pop the state. If the operand matches for NOMATCH or | |
5503 * doesn't match for MATCH/SUBPAT, we fail. Otherwise backup, | |
5504 * except for SUBPAT, and continue with the next item. */ | |
5505 if (status == (rp->rs_no == NOMATCH ? RA_MATCH : RA_NOMATCH)) | |
5506 status = RA_NOMATCH; | |
5507 else | |
5508 { | |
5509 status = RA_CONT; | |
233 | 5510 if (rp->rs_no != SUBPAT) /* zero-width */ |
5511 reg_restore(&rp->rs_un.regsave, &backpos); | |
180 | 5512 } |
270 | 5513 regstack_pop(&scan); |
180 | 5514 if (status == RA_CONT) |
5515 scan = regnext(scan); | |
5516 break; | |
5517 | |
5518 case RS_BEHIND1: | |
5519 if (status == RA_NOMATCH) | |
5520 { | |
270 | 5521 regstack_pop(&scan); |
180 | 5522 regstack.ga_len -= sizeof(regbehind_T); |
5523 } | |
5524 else | |
5525 { | |
5526 /* The stuff after BEHIND/NOBEHIND matches. Now try if | |
5527 * the behind part does (not) match before the current | |
5528 * position in the input. This must be done at every | |
5529 * position in the input and checking if the match ends at | |
5530 * the current position. */ | |
5531 | |
5532 /* save the position after the found match for next */ | |
233 | 5533 reg_save(&(((regbehind_T *)rp) - 1)->save_after, &backpos); |
180 | 5534 |
4579
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5535 /* Start looking for a match with operand at the current |
1209 | 5536 * position. Go back one character until we find the |
180 | 5537 * result, hitting the start of the line or the previous |
5538 * line (for multi-line matching). | |
5539 * Set behind_pos to where the match should end, BHPOS | |
5540 * will match it. Save the current value. */ | |
5541 (((regbehind_T *)rp) - 1)->save_behind = behind_pos; | |
5542 behind_pos = rp->rs_un.regsave; | |
5543 | |
5544 rp->rs_state = RS_BEHIND2; | |
5545 | |
233 | 5546 reg_restore(&rp->rs_un.regsave, &backpos); |
4579
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5547 scan = OPERAND(rp->rs_scan) + 4; |
180 | 5548 } |
5549 break; | |
5550 | |
5551 case RS_BEHIND2: | |
5552 /* | |
5553 * Looping for BEHIND / NOBEHIND match. | |
5554 */ | |
5555 if (status == RA_MATCH && reg_save_equal(&behind_pos)) | |
5556 { | |
5557 /* found a match that ends where "next" started */ | |
5558 behind_pos = (((regbehind_T *)rp) - 1)->save_behind; | |
5559 if (rp->rs_no == BEHIND) | |
233 | 5560 reg_restore(&(((regbehind_T *)rp) - 1)->save_after, |
5561 &backpos); | |
180 | 5562 else |
1579 | 5563 { |
5564 /* But we didn't want a match. Need to restore the | |
5565 * subexpr, because what follows matched, so they have | |
5566 * been set. */ | |
180 | 5567 status = RA_NOMATCH; |
1579 | 5568 restore_subexpr(((regbehind_T *)rp) - 1); |
5569 } | |
270 | 5570 regstack_pop(&scan); |
180 | 5571 regstack.ga_len -= sizeof(regbehind_T); |
5572 } | |
5573 else | |
5574 { | |
4579
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5575 long limit; |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5576 |
1579 | 5577 /* No match or a match that doesn't end where we want it: Go |
5578 * back one character. May go to previous line once. */ | |
180 | 5579 no = OK; |
4579
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5580 limit = OPERAND_MIN(rp->rs_scan); |
180 | 5581 if (REG_MULTI) |
5582 { | |
4682
2f51ee8825db
updated for version 7.3.1088
Bram Moolenaar <bram@vim.org>
parents:
4679
diff
changeset
|
5583 if (limit > 0 |
2f51ee8825db
updated for version 7.3.1088
Bram Moolenaar <bram@vim.org>
parents:
4679
diff
changeset
|
5584 && ((rp->rs_un.regsave.rs_u.pos.lnum |
2f51ee8825db
updated for version 7.3.1088
Bram Moolenaar <bram@vim.org>
parents:
4679
diff
changeset
|
5585 < behind_pos.rs_u.pos.lnum |
2f51ee8825db
updated for version 7.3.1088
Bram Moolenaar <bram@vim.org>
parents:
4679
diff
changeset
|
5586 ? (colnr_T)STRLEN(regline) |
2f51ee8825db
updated for version 7.3.1088
Bram Moolenaar <bram@vim.org>
parents:
4679
diff
changeset
|
5587 : behind_pos.rs_u.pos.col) |
2f51ee8825db
updated for version 7.3.1088
Bram Moolenaar <bram@vim.org>
parents:
4679
diff
changeset
|
5588 - 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
|
5589 no = FAIL; |
2f51ee8825db
updated for version 7.3.1088
Bram Moolenaar <bram@vim.org>
parents:
4679
diff
changeset
|
5590 else if (rp->rs_un.regsave.rs_u.pos.col == 0) |
180 | 5591 { |
5592 if (rp->rs_un.regsave.rs_u.pos.lnum | |
5593 < behind_pos.rs_u.pos.lnum | |
5594 || reg_getline( | |
5595 --rp->rs_un.regsave.rs_u.pos.lnum) | |
5596 == NULL) | |
5597 no = FAIL; | |
5598 else | |
5599 { | |
233 | 5600 reg_restore(&rp->rs_un.regsave, &backpos); |
180 | 5601 rp->rs_un.regsave.rs_u.pos.col = |
5602 (colnr_T)STRLEN(regline); | |
5603 } | |
5604 } | |
5605 else | |
4579
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5606 { |
4176 | 5607 #ifdef FEAT_MBYTE |
4579
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5608 if (has_mbyte) |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5609 rp->rs_un.regsave.rs_u.pos.col -= |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5610 (*mb_head_off)(regline, regline |
4176 | 5611 + 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
|
5612 else |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5613 #endif |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5614 --rp->rs_un.regsave.rs_u.pos.col; |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5615 } |
180 | 5616 } |
5617 else | |
5618 { | |
5619 if (rp->rs_un.regsave.rs_u.ptr == regline) | |
5620 no = FAIL; | |
5621 else | |
4579
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5622 { |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5623 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
|
5624 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
|
5625 - rp->rs_un.regsave.rs_u.ptr) > limit) |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5626 no = FAIL; |
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5627 } |
180 | 5628 } |
5629 if (no == OK) | |
5630 { | |
5631 /* Advanced, prepare for finding match again. */ | |
233 | 5632 reg_restore(&rp->rs_un.regsave, &backpos); |
4579
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
5633 scan = OPERAND(rp->rs_scan) + 4; |
1579 | 5634 if (status == RA_MATCH) |
5635 { | |
5636 /* We did match, so subexpr may have been changed, | |
5637 * need to restore them for the next try. */ | |
5638 status = RA_NOMATCH; | |
5639 restore_subexpr(((regbehind_T *)rp) - 1); | |
5640 } | |
180 | 5641 } |
5642 else | |
5643 { | |
5644 /* Can't advance. For NOBEHIND that's a match. */ | |
5645 behind_pos = (((regbehind_T *)rp) - 1)->save_behind; | |
5646 if (rp->rs_no == NOBEHIND) | |
5647 { | |
233 | 5648 reg_restore(&(((regbehind_T *)rp) - 1)->save_after, |
5649 &backpos); | |
180 | 5650 status = RA_MATCH; |
5651 } | |
5652 else | |
1579 | 5653 { |
5654 /* We do want a proper match. Need to restore the | |
5655 * subexpr if we had a match, because they may have | |
5656 * been set. */ | |
5657 if (status == RA_MATCH) | |
5658 { | |
5659 status = RA_NOMATCH; | |
5660 restore_subexpr(((regbehind_T *)rp) - 1); | |
5661 } | |
5662 } | |
270 | 5663 regstack_pop(&scan); |
180 | 5664 regstack.ga_len -= sizeof(regbehind_T); |
5665 } | |
5666 } | |
5667 break; | |
5668 | |
5669 case RS_STAR_LONG: | |
5670 case RS_STAR_SHORT: | |
5671 { | |
5672 regstar_T *rst = ((regstar_T *)rp) - 1; | |
5673 | |
5674 if (status == RA_MATCH) | |
5675 { | |
270 | 5676 regstack_pop(&scan); |
180 | 5677 regstack.ga_len -= sizeof(regstar_T); |
5678 break; | |
5679 } | |
5680 | |
5681 /* Tried once already, restore input pointers. */ | |
5682 if (status != RA_BREAK) | |
233 | 5683 reg_restore(&rp->rs_un.regsave, &backpos); |
180 | 5684 |
5685 /* Repeat until we found a position where it could match. */ | |
5686 for (;;) | |
5687 { | |
5688 if (status != RA_BREAK) | |
5689 { | |
5690 /* Tried first position already, advance. */ | |
5691 if (rp->rs_state == RS_STAR_LONG) | |
5692 { | |
685 | 5693 /* Trying for longest match, but couldn't or |
5694 * didn't match -- back up one char. */ | |
180 | 5695 if (--rst->count < rst->minval) |
5696 break; | |
5697 if (reginput == regline) | |
5698 { | |
5699 /* backup to last char of previous line */ | |
5700 --reglnum; | |
5701 regline = reg_getline(reglnum); | |
5702 /* Just in case regrepeat() didn't count | |
5703 * right. */ | |
5704 if (regline == NULL) | |
5705 break; | |
5706 reginput = regline + STRLEN(regline); | |
5707 fast_breakcheck(); | |
5708 } | |
5709 else | |
5710 mb_ptr_back(regline, reginput); | |
5711 } | |
5712 else | |
5713 { | |
5714 /* Range is backwards, use shortest match first. | |
5715 * Careful: maxval and minval are exchanged! | |
5716 * Couldn't or didn't match: try advancing one | |
5717 * char. */ | |
5718 if (rst->count == rst->minval | |
5719 || regrepeat(OPERAND(rp->rs_scan), 1L) == 0) | |
5720 break; | |
5721 ++rst->count; | |
5722 } | |
5723 if (got_int) | |
5724 break; | |
5725 } | |
5726 else | |
5727 status = RA_NOMATCH; | |
5728 | |
5729 /* If it could match, try it. */ | |
5730 if (rst->nextb == NUL || *reginput == rst->nextb | |
5731 || *reginput == rst->nextb_ic) | |
5732 { | |
233 | 5733 reg_save(&rp->rs_un.regsave, &backpos); |
180 | 5734 scan = regnext(rp->rs_scan); |
5735 status = RA_CONT; | |
5736 break; | |
5737 } | |
5738 } | |
5739 if (status != RA_CONT) | |
5740 { | |
5741 /* Failed. */ | |
270 | 5742 regstack_pop(&scan); |
180 | 5743 regstack.ga_len -= sizeof(regstar_T); |
5744 status = RA_NOMATCH; | |
5745 } | |
5746 } | |
5747 break; | |
5748 } | |
5749 | |
685 | 5750 /* If we want to continue the inner loop or didn't pop a state |
5751 * continue matching loop */ | |
180 | 5752 if (status == RA_CONT || rp == (regitem_T *) |
5753 ((char *)regstack.ga_data + regstack.ga_len) - 1) | |
5754 break; | |
5755 } | |
5756 | |
189 | 5757 /* May need to continue with the inner loop, starting at "scan". */ |
180 | 5758 if (status == RA_CONT) |
5759 continue; | |
5760 | |
5761 /* | |
5762 * If the regstack is empty or something failed we are done. | |
5763 */ | |
5764 if (regstack.ga_len == 0 || status == RA_FAIL) | |
5765 { | |
5766 if (scan == NULL) | |
5767 { | |
5768 /* | |
5769 * We get here only if there's trouble -- normally "case END" is | |
5770 * the terminating point. | |
5771 */ | |
5772 EMSG(_(e_re_corr)); | |
7 | 5773 #ifdef DEBUG |
180 | 5774 printf("Premature EOL\n"); |
7 | 5775 #endif |
180 | 5776 } |
189 | 5777 if (status == RA_FAIL) |
5778 got_int = TRUE; | |
180 | 5779 return (status == RA_MATCH); |
5780 } | |
5781 | |
5782 } /* End of loop until the regstack is empty. */ | |
5783 | |
5784 /* NOTREACHED */ | |
5785 } | |
5786 | |
5787 /* | |
5788 * Push an item onto the regstack. | |
5789 * Returns pointer to new item. Returns NULL when out of memory. | |
5790 */ | |
5791 static regitem_T * | |
270 | 5792 regstack_push(state, scan) |
180 | 5793 regstate_T state; |
5794 char_u *scan; | |
5795 { | |
5796 regitem_T *rp; | |
5797 | |
270 | 5798 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp) |
189 | 5799 { |
5800 EMSG(_(e_maxmempat)); | |
5801 return NULL; | |
5802 } | |
270 | 5803 if (ga_grow(®stack, sizeof(regitem_T)) == FAIL) |
180 | 5804 return NULL; |
5805 | |
270 | 5806 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len); |
180 | 5807 rp->rs_state = state; |
5808 rp->rs_scan = scan; | |
5809 | |
270 | 5810 regstack.ga_len += sizeof(regitem_T); |
180 | 5811 return rp; |
5812 } | |
5813 | |
5814 /* | |
5815 * Pop an item from the regstack. | |
5816 */ | |
5817 static void | |
270 | 5818 regstack_pop(scan) |
180 | 5819 char_u **scan; |
5820 { | |
5821 regitem_T *rp; | |
5822 | |
270 | 5823 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len) - 1; |
180 | 5824 *scan = rp->rs_scan; |
5825 | |
270 | 5826 regstack.ga_len -= sizeof(regitem_T); |
7 | 5827 } |
5828 | |
5829 /* | |
5830 * regrepeat - repeatedly match something simple, return how many. | |
5831 * Advances reginput (and reglnum) to just after the matched chars. | |
5832 */ | |
5833 static int | |
5834 regrepeat(p, maxcount) | |
5835 char_u *p; | |
5836 long maxcount; /* maximum number of matches allowed */ | |
5837 { | |
5838 long count = 0; | |
5839 char_u *scan; | |
5840 char_u *opnd; | |
5841 int mask; | |
5842 int testval = 0; | |
5843 | |
5844 scan = reginput; /* Make local copy of reginput for speed. */ | |
5845 opnd = OPERAND(p); | |
5846 switch (OP(p)) | |
5847 { | |
5848 case ANY: | |
5849 case ANY + ADD_NL: | |
5850 while (count < maxcount) | |
5851 { | |
5852 /* Matching anything means we continue until end-of-line (or | |
5853 * end-of-file for ANY + ADD_NL), only limited by maxcount. */ | |
5854 while (*scan != NUL && count < maxcount) | |
5855 { | |
5856 ++count; | |
39 | 5857 mb_ptr_adv(scan); |
7 | 5858 } |
1018 | 5859 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline |
5860 || reg_line_lbr || count == maxcount) | |
7 | 5861 break; |
5862 ++count; /* count the line-break */ | |
5863 reg_nextline(); | |
5864 scan = reginput; | |
5865 if (got_int) | |
5866 break; | |
5867 } | |
5868 break; | |
5869 | |
5870 case IDENT: | |
5871 case IDENT + ADD_NL: | |
5872 testval = TRUE; | |
5873 /*FALLTHROUGH*/ | |
5874 case SIDENT: | |
5875 case SIDENT + ADD_NL: | |
5876 while (count < maxcount) | |
5877 { | |
4466 | 5878 if (vim_isIDc(PTR2CHAR(scan)) && (testval || !VIM_ISDIGIT(*scan))) |
7 | 5879 { |
39 | 5880 mb_ptr_adv(scan); |
7 | 5881 } |
5882 else if (*scan == NUL) | |
5883 { | |
1018 | 5884 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline |
5885 || reg_line_lbr) | |
7 | 5886 break; |
5887 reg_nextline(); | |
5888 scan = reginput; | |
5889 if (got_int) | |
5890 break; | |
5891 } | |
5892 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p))) | |
5893 ++scan; | |
5894 else | |
5895 break; | |
5896 ++count; | |
5897 } | |
5898 break; | |
5899 | |
5900 case KWORD: | |
5901 case KWORD + ADD_NL: | |
5902 testval = TRUE; | |
5903 /*FALLTHROUGH*/ | |
5904 case SKWORD: | |
5905 case SKWORD + ADD_NL: | |
5906 while (count < maxcount) | |
5907 { | |
4069 | 5908 if (vim_iswordp_buf(scan, reg_buf) |
5909 && (testval || !VIM_ISDIGIT(*scan))) | |
7 | 5910 { |
39 | 5911 mb_ptr_adv(scan); |
7 | 5912 } |
5913 else if (*scan == NUL) | |
5914 { | |
1018 | 5915 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline |
5916 || reg_line_lbr) | |
7 | 5917 break; |
5918 reg_nextline(); | |
5919 scan = reginput; | |
5920 if (got_int) | |
5921 break; | |
5922 } | |
5923 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p))) | |
5924 ++scan; | |
5925 else | |
5926 break; | |
5927 ++count; | |
5928 } | |
5929 break; | |
5930 | |
5931 case FNAME: | |
5932 case FNAME + ADD_NL: | |
5933 testval = TRUE; | |
5934 /*FALLTHROUGH*/ | |
5935 case SFNAME: | |
5936 case SFNAME + ADD_NL: | |
5937 while (count < maxcount) | |
5938 { | |
4466 | 5939 if (vim_isfilec(PTR2CHAR(scan)) && (testval || !VIM_ISDIGIT(*scan))) |
7 | 5940 { |
39 | 5941 mb_ptr_adv(scan); |
7 | 5942 } |
5943 else if (*scan == NUL) | |
5944 { | |
1018 | 5945 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline |
5946 || reg_line_lbr) | |
7 | 5947 break; |
5948 reg_nextline(); | |
5949 scan = reginput; | |
5950 if (got_int) | |
5951 break; | |
5952 } | |
5953 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p))) | |
5954 ++scan; | |
5955 else | |
5956 break; | |
5957 ++count; | |
5958 } | |
5959 break; | |
5960 | |
5961 case PRINT: | |
5962 case PRINT + ADD_NL: | |
5963 testval = TRUE; | |
5964 /*FALLTHROUGH*/ | |
5965 case SPRINT: | |
5966 case SPRINT + ADD_NL: | |
5967 while (count < maxcount) | |
5968 { | |
5969 if (*scan == NUL) | |
5970 { | |
1018 | 5971 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline |
5972 || reg_line_lbr) | |
7 | 5973 break; |
5974 reg_nextline(); | |
5975 scan = reginput; | |
5976 if (got_int) | |
5977 break; | |
5978 } | |
5979 else if (ptr2cells(scan) == 1 && (testval || !VIM_ISDIGIT(*scan))) | |
5980 { | |
39 | 5981 mb_ptr_adv(scan); |
7 | 5982 } |
5983 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p))) | |
5984 ++scan; | |
5985 else | |
5986 break; | |
5987 ++count; | |
5988 } | |
5989 break; | |
5990 | |
5991 case WHITE: | |
5992 case WHITE + ADD_NL: | |
5993 testval = mask = RI_WHITE; | |
5994 do_class: | |
5995 while (count < maxcount) | |
5996 { | |
5997 #ifdef FEAT_MBYTE | |
5998 int l; | |
5999 #endif | |
6000 if (*scan == NUL) | |
6001 { | |
1018 | 6002 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline |
6003 || reg_line_lbr) | |
7 | 6004 break; |
6005 reg_nextline(); | |
6006 scan = reginput; | |
6007 if (got_int) | |
6008 break; | |
6009 } | |
6010 #ifdef FEAT_MBYTE | |
474 | 6011 else if (has_mbyte && (l = (*mb_ptr2len)(scan)) > 1) |
7 | 6012 { |
6013 if (testval != 0) | |
6014 break; | |
6015 scan += l; | |
6016 } | |
6017 #endif | |
6018 else if ((class_tab[*scan] & mask) == testval) | |
6019 ++scan; | |
6020 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p))) | |
6021 ++scan; | |
6022 else | |
6023 break; | |
6024 ++count; | |
6025 } | |
6026 break; | |
6027 | |
6028 case NWHITE: | |
6029 case NWHITE + ADD_NL: | |
6030 mask = RI_WHITE; | |
6031 goto do_class; | |
6032 case DIGIT: | |
6033 case DIGIT + ADD_NL: | |
6034 testval = mask = RI_DIGIT; | |
6035 goto do_class; | |
6036 case NDIGIT: | |
6037 case NDIGIT + ADD_NL: | |
6038 mask = RI_DIGIT; | |
6039 goto do_class; | |
6040 case HEX: | |
6041 case HEX + ADD_NL: | |
6042 testval = mask = RI_HEX; | |
6043 goto do_class; | |
6044 case NHEX: | |
6045 case NHEX + ADD_NL: | |
6046 mask = RI_HEX; | |
6047 goto do_class; | |
6048 case OCTAL: | |
6049 case OCTAL + ADD_NL: | |
6050 testval = mask = RI_OCTAL; | |
6051 goto do_class; | |
6052 case NOCTAL: | |
6053 case NOCTAL + ADD_NL: | |
6054 mask = RI_OCTAL; | |
6055 goto do_class; | |
6056 case WORD: | |
6057 case WORD + ADD_NL: | |
6058 testval = mask = RI_WORD; | |
6059 goto do_class; | |
6060 case NWORD: | |
6061 case NWORD + ADD_NL: | |
6062 mask = RI_WORD; | |
6063 goto do_class; | |
6064 case HEAD: | |
6065 case HEAD + ADD_NL: | |
6066 testval = mask = RI_HEAD; | |
6067 goto do_class; | |
6068 case NHEAD: | |
6069 case NHEAD + ADD_NL: | |
6070 mask = RI_HEAD; | |
6071 goto do_class; | |
6072 case ALPHA: | |
6073 case ALPHA + ADD_NL: | |
6074 testval = mask = RI_ALPHA; | |
6075 goto do_class; | |
6076 case NALPHA: | |
6077 case NALPHA + ADD_NL: | |
6078 mask = RI_ALPHA; | |
6079 goto do_class; | |
6080 case LOWER: | |
6081 case LOWER + ADD_NL: | |
6082 testval = mask = RI_LOWER; | |
6083 goto do_class; | |
6084 case NLOWER: | |
6085 case NLOWER + ADD_NL: | |
6086 mask = RI_LOWER; | |
6087 goto do_class; | |
6088 case UPPER: | |
6089 case UPPER + ADD_NL: | |
6090 testval = mask = RI_UPPER; | |
6091 goto do_class; | |
6092 case NUPPER: | |
6093 case NUPPER + ADD_NL: | |
6094 mask = RI_UPPER; | |
6095 goto do_class; | |
6096 | |
6097 case EXACTLY: | |
6098 { | |
6099 int cu, cl; | |
6100 | |
6101 /* This doesn't do a multi-byte character, because a MULTIBYTECODE | |
1347 | 6102 * would have been used for it. It does handle single-byte |
6103 * characters, such as latin1. */ | |
7 | 6104 if (ireg_ic) |
6105 { | |
1347 | 6106 cu = MB_TOUPPER(*opnd); |
6107 cl = MB_TOLOWER(*opnd); | |
7 | 6108 while (count < maxcount && (*scan == cu || *scan == cl)) |
6109 { | |
6110 count++; | |
6111 scan++; | |
6112 } | |
6113 } | |
6114 else | |
6115 { | |
6116 cu = *opnd; | |
6117 while (count < maxcount && *scan == cu) | |
6118 { | |
6119 count++; | |
6120 scan++; | |
6121 } | |
6122 } | |
6123 break; | |
6124 } | |
6125 | |
6126 #ifdef FEAT_MBYTE | |
6127 case MULTIBYTECODE: | |
6128 { | |
6129 int i, len, cf = 0; | |
6130 | |
6131 /* Safety check (just in case 'encoding' was changed since | |
6132 * compiling the program). */ | |
474 | 6133 if ((len = (*mb_ptr2len)(opnd)) > 1) |
7 | 6134 { |
6135 if (ireg_ic && enc_utf8) | |
6136 cf = utf_fold(utf_ptr2char(opnd)); | |
6137 while (count < maxcount) | |
6138 { | |
6139 for (i = 0; i < len; ++i) | |
6140 if (opnd[i] != scan[i]) | |
6141 break; | |
6142 if (i < len && (!ireg_ic || !enc_utf8 | |
6143 || utf_fold(utf_ptr2char(scan)) != cf)) | |
6144 break; | |
6145 scan += len; | |
6146 ++count; | |
6147 } | |
6148 } | |
6149 } | |
6150 break; | |
6151 #endif | |
6152 | |
6153 case ANYOF: | |
6154 case ANYOF + ADD_NL: | |
6155 testval = TRUE; | |
6156 /*FALLTHROUGH*/ | |
6157 | |
6158 case ANYBUT: | |
6159 case ANYBUT + ADD_NL: | |
6160 while (count < maxcount) | |
6161 { | |
6162 #ifdef FEAT_MBYTE | |
6163 int len; | |
6164 #endif | |
6165 if (*scan == NUL) | |
6166 { | |
1018 | 6167 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline |
6168 || reg_line_lbr) | |
7 | 6169 break; |
6170 reg_nextline(); | |
6171 scan = reginput; | |
6172 if (got_int) | |
6173 break; | |
6174 } | |
6175 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p))) | |
6176 ++scan; | |
6177 #ifdef FEAT_MBYTE | |
474 | 6178 else if (has_mbyte && (len = (*mb_ptr2len)(scan)) > 1) |
7 | 6179 { |
6180 if ((cstrchr(opnd, (*mb_ptr2char)(scan)) == NULL) == testval) | |
6181 break; | |
6182 scan += len; | |
6183 } | |
6184 #endif | |
6185 else | |
6186 { | |
6187 if ((cstrchr(opnd, *scan) == NULL) == testval) | |
6188 break; | |
6189 ++scan; | |
6190 } | |
6191 ++count; | |
6192 } | |
6193 break; | |
6194 | |
6195 case NEWL: | |
6196 while (count < maxcount | |
1018 | 6197 && ((*scan == NUL && reglnum <= reg_maxline && !reg_line_lbr |
6198 && REG_MULTI) || (*scan == '\n' && reg_line_lbr))) | |
7 | 6199 { |
6200 count++; | |
6201 if (reg_line_lbr) | |
6202 ADVANCE_REGINPUT(); | |
6203 else | |
6204 reg_nextline(); | |
6205 scan = reginput; | |
6206 if (got_int) | |
6207 break; | |
6208 } | |
6209 break; | |
6210 | |
6211 default: /* Oh dear. Called inappropriately. */ | |
6212 EMSG(_(e_re_corr)); | |
6213 #ifdef DEBUG | |
6214 printf("Called regrepeat with op code %d\n", OP(p)); | |
6215 #endif | |
6216 break; | |
6217 } | |
6218 | |
6219 reginput = scan; | |
6220 | |
6221 return (int)count; | |
6222 } | |
6223 | |
6224 /* | |
6225 * regnext - dig the "next" pointer out of a node | |
2010 | 6226 * Returns NULL when calculating size, when there is no next item and when |
6227 * there is an error. | |
7 | 6228 */ |
6229 static char_u * | |
6230 regnext(p) | |
6231 char_u *p; | |
6232 { | |
6233 int offset; | |
6234 | |
2010 | 6235 if (p == JUST_CALC_SIZE || reg_toolong) |
7 | 6236 return NULL; |
6237 | |
6238 offset = NEXT(p); | |
6239 if (offset == 0) | |
6240 return NULL; | |
6241 | |
233 | 6242 if (OP(p) == BACK) |
7 | 6243 return p - offset; |
6244 else | |
6245 return p + offset; | |
6246 } | |
6247 | |
6248 /* | |
6249 * Check the regexp program for its magic number. | |
6250 * Return TRUE if it's wrong. | |
6251 */ | |
6252 static int | |
6253 prog_magic_wrong() | |
6254 { | |
4444 | 6255 regprog_T *prog; |
6256 | |
6257 prog = REG_MULTI ? reg_mmatch->regprog : reg_match->regprog; | |
6258 if (prog->engine == &nfa_regengine) | |
6259 /* For NFA matcher we don't check the magic */ | |
6260 return FALSE; | |
6261 | |
6262 if (UCHARAT(((bt_regprog_T *)prog)->program) != REGMAGIC) | |
7 | 6263 { |
6264 EMSG(_(e_re_corr)); | |
6265 return TRUE; | |
6266 } | |
6267 return FALSE; | |
6268 } | |
6269 | |
6270 /* | |
6271 * Cleanup the subexpressions, if this wasn't done yet. | |
6272 * This construction is used to clear the subexpressions only when they are | |
6273 * used (to increase speed). | |
6274 */ | |
6275 static void | |
6276 cleanup_subexpr() | |
6277 { | |
6278 if (need_clear_subexpr) | |
6279 { | |
6280 if (REG_MULTI) | |
6281 { | |
6282 /* Use 0xff to set lnum to -1 */ | |
6283 vim_memset(reg_startpos, 0xff, sizeof(lpos_T) * NSUBEXP); | |
6284 vim_memset(reg_endpos, 0xff, sizeof(lpos_T) * NSUBEXP); | |
6285 } | |
6286 else | |
6287 { | |
6288 vim_memset(reg_startp, 0, sizeof(char_u *) * NSUBEXP); | |
6289 vim_memset(reg_endp, 0, sizeof(char_u *) * NSUBEXP); | |
6290 } | |
6291 need_clear_subexpr = FALSE; | |
6292 } | |
6293 } | |
6294 | |
6295 #ifdef FEAT_SYN_HL | |
6296 static void | |
6297 cleanup_zsubexpr() | |
6298 { | |
6299 if (need_clear_zsubexpr) | |
6300 { | |
6301 if (REG_MULTI) | |
6302 { | |
6303 /* Use 0xff to set lnum to -1 */ | |
6304 vim_memset(reg_startzpos, 0xff, sizeof(lpos_T) * NSUBEXP); | |
6305 vim_memset(reg_endzpos, 0xff, sizeof(lpos_T) * NSUBEXP); | |
6306 } | |
6307 else | |
6308 { | |
6309 vim_memset(reg_startzp, 0, sizeof(char_u *) * NSUBEXP); | |
6310 vim_memset(reg_endzp, 0, sizeof(char_u *) * NSUBEXP); | |
6311 } | |
6312 need_clear_zsubexpr = FALSE; | |
6313 } | |
6314 } | |
6315 #endif | |
6316 | |
6317 /* | |
1579 | 6318 * Save the current subexpr to "bp", so that they can be restored |
6319 * later by restore_subexpr(). | |
6320 */ | |
6321 static void | |
6322 save_subexpr(bp) | |
6323 regbehind_T *bp; | |
6324 { | |
6325 int i; | |
6326 | |
1602 | 6327 /* When "need_clear_subexpr" is set we don't need to save the values, only |
6328 * remember that this flag needs to be set again when restoring. */ | |
6329 bp->save_need_clear_subexpr = need_clear_subexpr; | |
6330 if (!need_clear_subexpr) | |
1579 | 6331 { |
1602 | 6332 for (i = 0; i < NSUBEXP; ++i) |
1579 | 6333 { |
1602 | 6334 if (REG_MULTI) |
6335 { | |
6336 bp->save_start[i].se_u.pos = reg_startpos[i]; | |
6337 bp->save_end[i].se_u.pos = reg_endpos[i]; | |
6338 } | |
6339 else | |
6340 { | |
6341 bp->save_start[i].se_u.ptr = reg_startp[i]; | |
6342 bp->save_end[i].se_u.ptr = reg_endp[i]; | |
6343 } | |
1579 | 6344 } |
6345 } | |
6346 } | |
6347 | |
6348 /* | |
6349 * Restore the subexpr from "bp". | |
6350 */ | |
6351 static void | |
6352 restore_subexpr(bp) | |
6353 regbehind_T *bp; | |
6354 { | |
6355 int i; | |
6356 | |
1602 | 6357 /* Only need to restore saved values when they are not to be cleared. */ |
6358 need_clear_subexpr = bp->save_need_clear_subexpr; | |
6359 if (!need_clear_subexpr) | |
1579 | 6360 { |
1602 | 6361 for (i = 0; i < NSUBEXP; ++i) |
1579 | 6362 { |
1602 | 6363 if (REG_MULTI) |
6364 { | |
6365 reg_startpos[i] = bp->save_start[i].se_u.pos; | |
6366 reg_endpos[i] = bp->save_end[i].se_u.pos; | |
6367 } | |
6368 else | |
6369 { | |
6370 reg_startp[i] = bp->save_start[i].se_u.ptr; | |
6371 reg_endp[i] = bp->save_end[i].se_u.ptr; | |
6372 } | |
1579 | 6373 } |
6374 } | |
6375 } | |
6376 | |
6377 /* | |
7 | 6378 * Advance reglnum, regline and reginput to the next line. |
6379 */ | |
6380 static void | |
6381 reg_nextline() | |
6382 { | |
6383 regline = reg_getline(++reglnum); | |
6384 reginput = regline; | |
6385 fast_breakcheck(); | |
6386 } | |
6387 | |
6388 /* | |
6389 * Save the input line and position in a regsave_T. | |
6390 */ | |
6391 static void | |
233 | 6392 reg_save(save, gap) |
7 | 6393 regsave_T *save; |
233 | 6394 garray_T *gap; |
7 | 6395 { |
6396 if (REG_MULTI) | |
6397 { | |
6398 save->rs_u.pos.col = (colnr_T)(reginput - regline); | |
6399 save->rs_u.pos.lnum = reglnum; | |
6400 } | |
6401 else | |
6402 save->rs_u.ptr = reginput; | |
233 | 6403 save->rs_len = gap->ga_len; |
7 | 6404 } |
6405 | |
6406 /* | |
6407 * Restore the input line and position from a regsave_T. | |
6408 */ | |
6409 static void | |
233 | 6410 reg_restore(save, gap) |
7 | 6411 regsave_T *save; |
233 | 6412 garray_T *gap; |
7 | 6413 { |
6414 if (REG_MULTI) | |
6415 { | |
6416 if (reglnum != save->rs_u.pos.lnum) | |
6417 { | |
6418 /* only call reg_getline() when the line number changed to save | |
6419 * a bit of time */ | |
6420 reglnum = save->rs_u.pos.lnum; | |
6421 regline = reg_getline(reglnum); | |
6422 } | |
6423 reginput = regline + save->rs_u.pos.col; | |
6424 } | |
6425 else | |
6426 reginput = save->rs_u.ptr; | |
233 | 6427 gap->ga_len = save->rs_len; |
7 | 6428 } |
6429 | |
6430 /* | |
6431 * Return TRUE if current position is equal to saved position. | |
6432 */ | |
6433 static int | |
6434 reg_save_equal(save) | |
6435 regsave_T *save; | |
6436 { | |
6437 if (REG_MULTI) | |
6438 return reglnum == save->rs_u.pos.lnum | |
6439 && reginput == regline + save->rs_u.pos.col; | |
6440 return reginput == save->rs_u.ptr; | |
6441 } | |
6442 | |
6443 /* | |
6444 * Tentatively set the sub-expression start to the current position (after | |
6445 * calling regmatch() they will have changed). Need to save the existing | |
6446 * values for when there is no match. | |
6447 * Use se_save() to use pointer (save_se_multi()) or position (save_se_one()), | |
6448 * depending on REG_MULTI. | |
6449 */ | |
6450 static void | |
6451 save_se_multi(savep, posp) | |
6452 save_se_T *savep; | |
6453 lpos_T *posp; | |
6454 { | |
6455 savep->se_u.pos = *posp; | |
6456 posp->lnum = reglnum; | |
6457 posp->col = (colnr_T)(reginput - regline); | |
6458 } | |
6459 | |
6460 static void | |
6461 save_se_one(savep, pp) | |
6462 save_se_T *savep; | |
6463 char_u **pp; | |
6464 { | |
6465 savep->se_u.ptr = *pp; | |
6466 *pp = reginput; | |
6467 } | |
6468 | |
6469 /* | |
6470 * Compare a number with the operand of RE_LNUM, RE_COL or RE_VCOL. | |
6471 */ | |
6472 static int | |
6473 re_num_cmp(val, scan) | |
6474 long_u val; | |
6475 char_u *scan; | |
6476 { | |
6477 long_u n = OPERAND_MIN(scan); | |
6478 | |
6479 if (OPERAND_CMP(scan) == '>') | |
6480 return val > n; | |
6481 if (OPERAND_CMP(scan) == '<') | |
6482 return val < n; | |
6483 return val == n; | |
6484 } | |
6485 | |
6486 | |
4444 | 6487 #ifdef BT_REGEXP_DUMP |
7 | 6488 |
6489 /* | |
6490 * regdump - dump a regexp onto stdout in vaguely comprehensible form | |
6491 */ | |
6492 static void | |
6493 regdump(pattern, r) | |
6494 char_u *pattern; | |
4444 | 6495 bt_regprog_T *r; |
7 | 6496 { |
6497 char_u *s; | |
6498 int op = EXACTLY; /* Arbitrary non-END op. */ | |
6499 char_u *next; | |
6500 char_u *end = NULL; | |
4444 | 6501 FILE *f; |
6502 | |
6503 #ifdef BT_REGEXP_LOG | |
6504 f = fopen("bt_regexp_log.log", "a"); | |
6505 #else | |
6506 f = stdout; | |
6507 #endif | |
6508 if (f == NULL) | |
6509 return; | |
6510 fprintf(f, "-------------------------------------\n\r\nregcomp(%s):\r\n", pattern); | |
7 | 6511 |
6512 s = r->program + 1; | |
6513 /* | |
6514 * Loop until we find the END that isn't before a referred next (an END | |
6515 * can also appear in a NOMATCH operand). | |
6516 */ | |
6517 while (op != END || s <= end) | |
6518 { | |
6519 op = OP(s); | |
4444 | 6520 fprintf(f, "%2d%s", (int)(s - r->program), regprop(s)); /* Where, what. */ |
7 | 6521 next = regnext(s); |
6522 if (next == NULL) /* Next ptr. */ | |
4444 | 6523 fprintf(f, "(0)"); |
7 | 6524 else |
4444 | 6525 fprintf(f, "(%d)", (int)((s - r->program) + (next - s))); |
7 | 6526 if (end < next) |
6527 end = next; | |
6528 if (op == BRACE_LIMITS) | |
6529 { | |
6530 /* Two short ints */ | |
4444 | 6531 fprintf(f, " minval %ld, maxval %ld", OPERAND_MIN(s), OPERAND_MAX(s)); |
7 | 6532 s += 8; |
6533 } | |
6534 s += 3; | |
6535 if (op == ANYOF || op == ANYOF + ADD_NL | |
6536 || op == ANYBUT || op == ANYBUT + ADD_NL | |
6537 || op == EXACTLY) | |
6538 { | |
6539 /* Literal string, where present. */ | |
4444 | 6540 fprintf(f, "\nxxxxxxxxx\n"); |
7 | 6541 while (*s != NUL) |
4444 | 6542 fprintf(f, "%c", *s++); |
6543 fprintf(f, "\nxxxxxxxxx\n"); | |
7 | 6544 s++; |
6545 } | |
4444 | 6546 fprintf(f, "\r\n"); |
7 | 6547 } |
6548 | |
6549 /* Header fields of interest. */ | |
6550 if (r->regstart != NUL) | |
4444 | 6551 fprintf(f, "start `%s' 0x%x; ", r->regstart < 256 |
7 | 6552 ? (char *)transchar(r->regstart) |
6553 : "multibyte", r->regstart); | |
6554 if (r->reganch) | |
4444 | 6555 fprintf(f, "anchored; "); |
7 | 6556 if (r->regmust != NULL) |
4444 | 6557 fprintf(f, "must have \"%s\"", r->regmust); |
6558 fprintf(f, "\r\n"); | |
6559 | |
6560 #ifdef BT_REGEXP_LOG | |
6561 fclose(f); | |
6562 #endif | |
7 | 6563 } |
4444 | 6564 #endif /* BT_REGEXP_DUMP */ |
6565 | |
6566 #ifdef DEBUG | |
7 | 6567 /* |
6568 * regprop - printable representation of opcode | |
6569 */ | |
6570 static char_u * | |
6571 regprop(op) | |
6572 char_u *op; | |
6573 { | |
4444 | 6574 char *p; |
6575 static char buf[50]; | |
6576 | |
6577 STRCPY(buf, ":"); | |
6578 | |
6579 switch ((int) OP(op)) | |
7 | 6580 { |
6581 case BOL: | |
6582 p = "BOL"; | |
6583 break; | |
6584 case EOL: | |
6585 p = "EOL"; | |
6586 break; | |
6587 case RE_BOF: | |
6588 p = "BOF"; | |
6589 break; | |
6590 case RE_EOF: | |
6591 p = "EOF"; | |
6592 break; | |
6593 case CURSOR: | |
6594 p = "CURSOR"; | |
6595 break; | |
639 | 6596 case RE_VISUAL: |
6597 p = "RE_VISUAL"; | |
6598 break; | |
7 | 6599 case RE_LNUM: |
6600 p = "RE_LNUM"; | |
6601 break; | |
639 | 6602 case RE_MARK: |
6603 p = "RE_MARK"; | |
6604 break; | |
7 | 6605 case RE_COL: |
6606 p = "RE_COL"; | |
6607 break; | |
6608 case RE_VCOL: | |
6609 p = "RE_VCOL"; | |
6610 break; | |
6611 case BOW: | |
6612 p = "BOW"; | |
6613 break; | |
6614 case EOW: | |
6615 p = "EOW"; | |
6616 break; | |
6617 case ANY: | |
6618 p = "ANY"; | |
6619 break; | |
6620 case ANY + ADD_NL: | |
6621 p = "ANY+NL"; | |
6622 break; | |
6623 case ANYOF: | |
6624 p = "ANYOF"; | |
6625 break; | |
6626 case ANYOF + ADD_NL: | |
6627 p = "ANYOF+NL"; | |
6628 break; | |
6629 case ANYBUT: | |
6630 p = "ANYBUT"; | |
6631 break; | |
6632 case ANYBUT + ADD_NL: | |
6633 p = "ANYBUT+NL"; | |
6634 break; | |
6635 case IDENT: | |
6636 p = "IDENT"; | |
6637 break; | |
6638 case IDENT + ADD_NL: | |
6639 p = "IDENT+NL"; | |
6640 break; | |
6641 case SIDENT: | |
6642 p = "SIDENT"; | |
6643 break; | |
6644 case SIDENT + ADD_NL: | |
6645 p = "SIDENT+NL"; | |
6646 break; | |
6647 case KWORD: | |
6648 p = "KWORD"; | |
6649 break; | |
6650 case KWORD + ADD_NL: | |
6651 p = "KWORD+NL"; | |
6652 break; | |
6653 case SKWORD: | |
6654 p = "SKWORD"; | |
6655 break; | |
6656 case SKWORD + ADD_NL: | |
6657 p = "SKWORD+NL"; | |
6658 break; | |
6659 case FNAME: | |
6660 p = "FNAME"; | |
6661 break; | |
6662 case FNAME + ADD_NL: | |
6663 p = "FNAME+NL"; | |
6664 break; | |
6665 case SFNAME: | |
6666 p = "SFNAME"; | |
6667 break; | |
6668 case SFNAME + ADD_NL: | |
6669 p = "SFNAME+NL"; | |
6670 break; | |
6671 case PRINT: | |
6672 p = "PRINT"; | |
6673 break; | |
6674 case PRINT + ADD_NL: | |
6675 p = "PRINT+NL"; | |
6676 break; | |
6677 case SPRINT: | |
6678 p = "SPRINT"; | |
6679 break; | |
6680 case SPRINT + ADD_NL: | |
6681 p = "SPRINT+NL"; | |
6682 break; | |
6683 case WHITE: | |
6684 p = "WHITE"; | |
6685 break; | |
6686 case WHITE + ADD_NL: | |
6687 p = "WHITE+NL"; | |
6688 break; | |
6689 case NWHITE: | |
6690 p = "NWHITE"; | |
6691 break; | |
6692 case NWHITE + ADD_NL: | |
6693 p = "NWHITE+NL"; | |
6694 break; | |
6695 case DIGIT: | |
6696 p = "DIGIT"; | |
6697 break; | |
6698 case DIGIT + ADD_NL: | |
6699 p = "DIGIT+NL"; | |
6700 break; | |
6701 case NDIGIT: | |
6702 p = "NDIGIT"; | |
6703 break; | |
6704 case NDIGIT + ADD_NL: | |
6705 p = "NDIGIT+NL"; | |
6706 break; | |
6707 case HEX: | |
6708 p = "HEX"; | |
6709 break; | |
6710 case HEX + ADD_NL: | |
6711 p = "HEX+NL"; | |
6712 break; | |
6713 case NHEX: | |
6714 p = "NHEX"; | |
6715 break; | |
6716 case NHEX + ADD_NL: | |
6717 p = "NHEX+NL"; | |
6718 break; | |
6719 case OCTAL: | |
6720 p = "OCTAL"; | |
6721 break; | |
6722 case OCTAL + ADD_NL: | |
6723 p = "OCTAL+NL"; | |
6724 break; | |
6725 case NOCTAL: | |
6726 p = "NOCTAL"; | |
6727 break; | |
6728 case NOCTAL + ADD_NL: | |
6729 p = "NOCTAL+NL"; | |
6730 break; | |
6731 case WORD: | |
6732 p = "WORD"; | |
6733 break; | |
6734 case WORD + ADD_NL: | |
6735 p = "WORD+NL"; | |
6736 break; | |
6737 case NWORD: | |
6738 p = "NWORD"; | |
6739 break; | |
6740 case NWORD + ADD_NL: | |
6741 p = "NWORD+NL"; | |
6742 break; | |
6743 case HEAD: | |
6744 p = "HEAD"; | |
6745 break; | |
6746 case HEAD + ADD_NL: | |
6747 p = "HEAD+NL"; | |
6748 break; | |
6749 case NHEAD: | |
6750 p = "NHEAD"; | |
6751 break; | |
6752 case NHEAD + ADD_NL: | |
6753 p = "NHEAD+NL"; | |
6754 break; | |
6755 case ALPHA: | |
6756 p = "ALPHA"; | |
6757 break; | |
6758 case ALPHA + ADD_NL: | |
6759 p = "ALPHA+NL"; | |
6760 break; | |
6761 case NALPHA: | |
6762 p = "NALPHA"; | |
6763 break; | |
6764 case NALPHA + ADD_NL: | |
6765 p = "NALPHA+NL"; | |
6766 break; | |
6767 case LOWER: | |
6768 p = "LOWER"; | |
6769 break; | |
6770 case LOWER + ADD_NL: | |
6771 p = "LOWER+NL"; | |
6772 break; | |
6773 case NLOWER: | |
6774 p = "NLOWER"; | |
6775 break; | |
6776 case NLOWER + ADD_NL: | |
6777 p = "NLOWER+NL"; | |
6778 break; | |
6779 case UPPER: | |
6780 p = "UPPER"; | |
6781 break; | |
6782 case UPPER + ADD_NL: | |
6783 p = "UPPER+NL"; | |
6784 break; | |
6785 case NUPPER: | |
6786 p = "NUPPER"; | |
6787 break; | |
6788 case NUPPER + ADD_NL: | |
6789 p = "NUPPER+NL"; | |
6790 break; | |
6791 case BRANCH: | |
6792 p = "BRANCH"; | |
6793 break; | |
6794 case EXACTLY: | |
6795 p = "EXACTLY"; | |
6796 break; | |
6797 case NOTHING: | |
6798 p = "NOTHING"; | |
6799 break; | |
6800 case BACK: | |
6801 p = "BACK"; | |
6802 break; | |
6803 case END: | |
6804 p = "END"; | |
6805 break; | |
6806 case MOPEN + 0: | |
6807 p = "MATCH START"; | |
6808 break; | |
6809 case MOPEN + 1: | |
6810 case MOPEN + 2: | |
6811 case MOPEN + 3: | |
6812 case MOPEN + 4: | |
6813 case MOPEN + 5: | |
6814 case MOPEN + 6: | |
6815 case MOPEN + 7: | |
6816 case MOPEN + 8: | |
6817 case MOPEN + 9: | |
6818 sprintf(buf + STRLEN(buf), "MOPEN%d", OP(op) - MOPEN); | |
6819 p = NULL; | |
6820 break; | |
6821 case MCLOSE + 0: | |
6822 p = "MATCH END"; | |
6823 break; | |
6824 case MCLOSE + 1: | |
6825 case MCLOSE + 2: | |
6826 case MCLOSE + 3: | |
6827 case MCLOSE + 4: | |
6828 case MCLOSE + 5: | |
6829 case MCLOSE + 6: | |
6830 case MCLOSE + 7: | |
6831 case MCLOSE + 8: | |
6832 case MCLOSE + 9: | |
6833 sprintf(buf + STRLEN(buf), "MCLOSE%d", OP(op) - MCLOSE); | |
6834 p = NULL; | |
6835 break; | |
6836 case BACKREF + 1: | |
6837 case BACKREF + 2: | |
6838 case BACKREF + 3: | |
6839 case BACKREF + 4: | |
6840 case BACKREF + 5: | |
6841 case BACKREF + 6: | |
6842 case BACKREF + 7: | |
6843 case BACKREF + 8: | |
6844 case BACKREF + 9: | |
6845 sprintf(buf + STRLEN(buf), "BACKREF%d", OP(op) - BACKREF); | |
6846 p = NULL; | |
6847 break; | |
6848 case NOPEN: | |
6849 p = "NOPEN"; | |
6850 break; | |
6851 case NCLOSE: | |
6852 p = "NCLOSE"; | |
6853 break; | |
6854 #ifdef FEAT_SYN_HL | |
6855 case ZOPEN + 1: | |
6856 case ZOPEN + 2: | |
6857 case ZOPEN + 3: | |
6858 case ZOPEN + 4: | |
6859 case ZOPEN + 5: | |
6860 case ZOPEN + 6: | |
6861 case ZOPEN + 7: | |
6862 case ZOPEN + 8: | |
6863 case ZOPEN + 9: | |
6864 sprintf(buf + STRLEN(buf), "ZOPEN%d", OP(op) - ZOPEN); | |
6865 p = NULL; | |
6866 break; | |
6867 case ZCLOSE + 1: | |
6868 case ZCLOSE + 2: | |
6869 case ZCLOSE + 3: | |
6870 case ZCLOSE + 4: | |
6871 case ZCLOSE + 5: | |
6872 case ZCLOSE + 6: | |
6873 case ZCLOSE + 7: | |
6874 case ZCLOSE + 8: | |
6875 case ZCLOSE + 9: | |
6876 sprintf(buf + STRLEN(buf), "ZCLOSE%d", OP(op) - ZCLOSE); | |
6877 p = NULL; | |
6878 break; | |
6879 case ZREF + 1: | |
6880 case ZREF + 2: | |
6881 case ZREF + 3: | |
6882 case ZREF + 4: | |
6883 case ZREF + 5: | |
6884 case ZREF + 6: | |
6885 case ZREF + 7: | |
6886 case ZREF + 8: | |
6887 case ZREF + 9: | |
6888 sprintf(buf + STRLEN(buf), "ZREF%d", OP(op) - ZREF); | |
6889 p = NULL; | |
6890 break; | |
6891 #endif | |
6892 case STAR: | |
6893 p = "STAR"; | |
6894 break; | |
6895 case PLUS: | |
6896 p = "PLUS"; | |
6897 break; | |
6898 case NOMATCH: | |
6899 p = "NOMATCH"; | |
6900 break; | |
6901 case MATCH: | |
6902 p = "MATCH"; | |
6903 break; | |
6904 case BEHIND: | |
6905 p = "BEHIND"; | |
6906 break; | |
6907 case NOBEHIND: | |
6908 p = "NOBEHIND"; | |
6909 break; | |
6910 case SUBPAT: | |
6911 p = "SUBPAT"; | |
6912 break; | |
6913 case BRACE_LIMITS: | |
6914 p = "BRACE_LIMITS"; | |
6915 break; | |
6916 case BRACE_SIMPLE: | |
6917 p = "BRACE_SIMPLE"; | |
6918 break; | |
6919 case BRACE_COMPLEX + 0: | |
6920 case BRACE_COMPLEX + 1: | |
6921 case BRACE_COMPLEX + 2: | |
6922 case BRACE_COMPLEX + 3: | |
6923 case BRACE_COMPLEX + 4: | |
6924 case BRACE_COMPLEX + 5: | |
6925 case BRACE_COMPLEX + 6: | |
6926 case BRACE_COMPLEX + 7: | |
6927 case BRACE_COMPLEX + 8: | |
6928 case BRACE_COMPLEX + 9: | |
6929 sprintf(buf + STRLEN(buf), "BRACE_COMPLEX%d", OP(op) - BRACE_COMPLEX); | |
6930 p = NULL; | |
6931 break; | |
6932 #ifdef FEAT_MBYTE | |
6933 case MULTIBYTECODE: | |
6934 p = "MULTIBYTECODE"; | |
6935 break; | |
6936 #endif | |
6937 case NEWL: | |
6938 p = "NEWL"; | |
6939 break; | |
6940 default: | |
6941 sprintf(buf + STRLEN(buf), "corrupt %d", OP(op)); | |
6942 p = NULL; | |
6943 break; | |
6944 } | |
6945 if (p != NULL) | |
4444 | 6946 STRCAT(buf, p); |
6947 return (char_u *)buf; | |
7 | 6948 } |
4444 | 6949 #endif /* DEBUG */ |
7 | 6950 |
6951 #ifdef FEAT_MBYTE | |
6952 static void mb_decompose __ARGS((int c, int *c1, int *c2, int *c3)); | |
6953 | |
6954 typedef struct | |
6955 { | |
6956 int a, b, c; | |
6957 } decomp_T; | |
6958 | |
6959 | |
6960 /* 0xfb20 - 0xfb4f */ | |
297 | 6961 static decomp_T decomp_table[0xfb4f-0xfb20+1] = |
7 | 6962 { |
6963 {0x5e2,0,0}, /* 0xfb20 alt ayin */ | |
6964 {0x5d0,0,0}, /* 0xfb21 alt alef */ | |
6965 {0x5d3,0,0}, /* 0xfb22 alt dalet */ | |
6966 {0x5d4,0,0}, /* 0xfb23 alt he */ | |
6967 {0x5db,0,0}, /* 0xfb24 alt kaf */ | |
6968 {0x5dc,0,0}, /* 0xfb25 alt lamed */ | |
6969 {0x5dd,0,0}, /* 0xfb26 alt mem-sofit */ | |
6970 {0x5e8,0,0}, /* 0xfb27 alt resh */ | |
6971 {0x5ea,0,0}, /* 0xfb28 alt tav */ | |
6972 {'+', 0, 0}, /* 0xfb29 alt plus */ | |
6973 {0x5e9, 0x5c1, 0}, /* 0xfb2a shin+shin-dot */ | |
6974 {0x5e9, 0x5c2, 0}, /* 0xfb2b shin+sin-dot */ | |
6975 {0x5e9, 0x5c1, 0x5bc}, /* 0xfb2c shin+shin-dot+dagesh */ | |
6976 {0x5e9, 0x5c2, 0x5bc}, /* 0xfb2d shin+sin-dot+dagesh */ | |
6977 {0x5d0, 0x5b7, 0}, /* 0xfb2e alef+patah */ | |
6978 {0x5d0, 0x5b8, 0}, /* 0xfb2f alef+qamats */ | |
6979 {0x5d0, 0x5b4, 0}, /* 0xfb30 alef+hiriq */ | |
6980 {0x5d1, 0x5bc, 0}, /* 0xfb31 bet+dagesh */ | |
6981 {0x5d2, 0x5bc, 0}, /* 0xfb32 gimel+dagesh */ | |
6982 {0x5d3, 0x5bc, 0}, /* 0xfb33 dalet+dagesh */ | |
6983 {0x5d4, 0x5bc, 0}, /* 0xfb34 he+dagesh */ | |
6984 {0x5d5, 0x5bc, 0}, /* 0xfb35 vav+dagesh */ | |
6985 {0x5d6, 0x5bc, 0}, /* 0xfb36 zayin+dagesh */ | |
6986 {0xfb37, 0, 0}, /* 0xfb37 -- UNUSED */ | |
6987 {0x5d8, 0x5bc, 0}, /* 0xfb38 tet+dagesh */ | |
6988 {0x5d9, 0x5bc, 0}, /* 0xfb39 yud+dagesh */ | |
6989 {0x5da, 0x5bc, 0}, /* 0xfb3a kaf sofit+dagesh */ | |
6990 {0x5db, 0x5bc, 0}, /* 0xfb3b kaf+dagesh */ | |
6991 {0x5dc, 0x5bc, 0}, /* 0xfb3c lamed+dagesh */ | |
6992 {0xfb3d, 0, 0}, /* 0xfb3d -- UNUSED */ | |
6993 {0x5de, 0x5bc, 0}, /* 0xfb3e mem+dagesh */ | |
6994 {0xfb3f, 0, 0}, /* 0xfb3f -- UNUSED */ | |
6995 {0x5e0, 0x5bc, 0}, /* 0xfb40 nun+dagesh */ | |
6996 {0x5e1, 0x5bc, 0}, /* 0xfb41 samech+dagesh */ | |
6997 {0xfb42, 0, 0}, /* 0xfb42 -- UNUSED */ | |
6998 {0x5e3, 0x5bc, 0}, /* 0xfb43 pe sofit+dagesh */ | |
6999 {0x5e4, 0x5bc,0}, /* 0xfb44 pe+dagesh */ | |
7000 {0xfb45, 0, 0}, /* 0xfb45 -- UNUSED */ | |
7001 {0x5e6, 0x5bc, 0}, /* 0xfb46 tsadi+dagesh */ | |
7002 {0x5e7, 0x5bc, 0}, /* 0xfb47 qof+dagesh */ | |
7003 {0x5e8, 0x5bc, 0}, /* 0xfb48 resh+dagesh */ | |
7004 {0x5e9, 0x5bc, 0}, /* 0xfb49 shin+dagesh */ | |
7005 {0x5ea, 0x5bc, 0}, /* 0xfb4a tav+dagesh */ | |
7006 {0x5d5, 0x5b9, 0}, /* 0xfb4b vav+holam */ | |
7007 {0x5d1, 0x5bf, 0}, /* 0xfb4c bet+rafe */ | |
7008 {0x5db, 0x5bf, 0}, /* 0xfb4d kaf+rafe */ | |
7009 {0x5e4, 0x5bf, 0}, /* 0xfb4e pe+rafe */ | |
7010 {0x5d0, 0x5dc, 0} /* 0xfb4f alef-lamed */ | |
7011 }; | |
7012 | |
7013 static void | |
7014 mb_decompose(c, c1, c2, c3) | |
7015 int c, *c1, *c2, *c3; | |
7016 { | |
7017 decomp_T d; | |
7018 | |
4505
d037b9cbdaaa
updated for version 7.3.1000
Bram Moolenaar <bram@vim.org>
parents:
4466
diff
changeset
|
7019 if (c >= 0xfb20 && c <= 0xfb4f) |
7 | 7020 { |
7021 d = decomp_table[c - 0xfb20]; | |
7022 *c1 = d.a; | |
7023 *c2 = d.b; | |
7024 *c3 = d.c; | |
7025 } | |
7026 else | |
7027 { | |
7028 *c1 = c; | |
7029 *c2 = *c3 = 0; | |
7030 } | |
7031 } | |
7032 #endif | |
7033 | |
7034 /* | |
7035 * Compare two strings, ignore case if ireg_ic set. | |
7036 * Return 0 if strings match, non-zero otherwise. | |
7037 * Correct the length "*n" when composing characters are ignored. | |
7038 */ | |
7039 static int | |
7040 cstrncmp(s1, s2, n) | |
7041 char_u *s1, *s2; | |
7042 int *n; | |
7043 { | |
7044 int result; | |
7045 | |
7046 if (!ireg_ic) | |
7047 result = STRNCMP(s1, s2, *n); | |
7048 else | |
7049 result = MB_STRNICMP(s1, s2, *n); | |
7050 | |
7051 #ifdef FEAT_MBYTE | |
7052 /* if it failed and it's utf8 and we want to combineignore: */ | |
7053 if (result != 0 && enc_utf8 && ireg_icombine) | |
7054 { | |
7055 char_u *str1, *str2; | |
7056 int c1, c2, c11, c12; | |
7057 int junk; | |
7058 | |
7059 /* we have to handle the strcmp ourselves, since it is necessary to | |
7060 * deal with the composing characters by ignoring them: */ | |
7061 str1 = s1; | |
7062 str2 = s2; | |
7063 c1 = c2 = 0; | |
507 | 7064 while ((int)(str1 - s1) < *n) |
7 | 7065 { |
7066 c1 = mb_ptr2char_adv(&str1); | |
7067 c2 = mb_ptr2char_adv(&str2); | |
7068 | |
7069 /* decompose the character if necessary, into 'base' characters | |
7070 * because I don't care about Arabic, I will hard-code the Hebrew | |
7071 * which I *do* care about! So sue me... */ | |
7072 if (c1 != c2 && (!ireg_ic || utf_fold(c1) != utf_fold(c2))) | |
7073 { | |
7074 /* decomposition necessary? */ | |
7075 mb_decompose(c1, &c11, &junk, &junk); | |
7076 mb_decompose(c2, &c12, &junk, &junk); | |
7077 c1 = c11; | |
7078 c2 = c12; | |
7079 if (c11 != c12 && (!ireg_ic || utf_fold(c11) != utf_fold(c12))) | |
7080 break; | |
7081 } | |
7082 } | |
7083 result = c2 - c1; | |
7084 if (result == 0) | |
7085 *n = (int)(str2 - s2); | |
7086 } | |
7087 #endif | |
7088 | |
7089 return result; | |
7090 } | |
7091 | |
7092 /* | |
7093 * cstrchr: This function is used a lot for simple searches, keep it fast! | |
7094 */ | |
7095 static char_u * | |
7096 cstrchr(s, c) | |
7097 char_u *s; | |
7098 int c; | |
7099 { | |
7100 char_u *p; | |
7101 int cc; | |
7102 | |
7103 if (!ireg_ic | |
7104 #ifdef FEAT_MBYTE | |
7105 || (!enc_utf8 && mb_char2len(c) > 1) | |
7106 #endif | |
7107 ) | |
7108 return vim_strchr(s, c); | |
7109 | |
7110 /* tolower() and toupper() can be slow, comparing twice should be a lot | |
7111 * faster (esp. when using MS Visual C++!). | |
7112 * For UTF-8 need to use folded case. */ | |
7113 #ifdef FEAT_MBYTE | |
7114 if (enc_utf8 && c > 0x80) | |
7115 cc = utf_fold(c); | |
7116 else | |
7117 #endif | |
1347 | 7118 if (MB_ISUPPER(c)) |
7119 cc = MB_TOLOWER(c); | |
7120 else if (MB_ISLOWER(c)) | |
7121 cc = MB_TOUPPER(c); | |
7 | 7122 else |
7123 return vim_strchr(s, c); | |
7124 | |
7125 #ifdef FEAT_MBYTE | |
7126 if (has_mbyte) | |
7127 { | |
474 | 7128 for (p = s; *p != NUL; p += (*mb_ptr2len)(p)) |
7 | 7129 { |
7130 if (enc_utf8 && c > 0x80) | |
7131 { | |
7132 if (utf_fold(utf_ptr2char(p)) == cc) | |
7133 return p; | |
7134 } | |
7135 else if (*p == c || *p == cc) | |
7136 return p; | |
7137 } | |
7138 } | |
7139 else | |
7140 #endif | |
7141 /* Faster version for when there are no multi-byte characters. */ | |
7142 for (p = s; *p != NUL; ++p) | |
7143 if (*p == c || *p == cc) | |
7144 return p; | |
7145 | |
7146 return NULL; | |
7147 } | |
7148 | |
7149 /*************************************************************** | |
7150 * regsub stuff * | |
7151 ***************************************************************/ | |
7152 | |
7153 /* This stuff below really confuses cc on an SGI -- webb */ | |
7154 #ifdef __sgi | |
7155 # undef __ARGS | |
7156 # define __ARGS(x) () | |
7157 #endif | |
7158 | |
7159 /* | |
7160 * We should define ftpr as a pointer to a function returning a pointer to | |
7161 * a function returning a pointer to a function ... | |
7162 * This is impossible, so we declare a pointer to a function returning a | |
7163 * pointer to a function returning void. This should work for all compilers. | |
7164 */ | |
772 | 7165 typedef void (*(*fptr_T) __ARGS((int *, int)))(); |
7166 | |
7167 static fptr_T do_upper __ARGS((int *, int)); | |
7168 static fptr_T do_Upper __ARGS((int *, int)); | |
7169 static fptr_T do_lower __ARGS((int *, int)); | |
7170 static fptr_T do_Lower __ARGS((int *, int)); | |
7 | 7171 |
7172 static int vim_regsub_both __ARGS((char_u *source, char_u *dest, int copy, int magic, int backslash)); | |
7173 | |
772 | 7174 static fptr_T |
7 | 7175 do_upper(d, c) |
772 | 7176 int *d; |
7177 int c; | |
7 | 7178 { |
772 | 7179 *d = MB_TOUPPER(c); |
7180 | |
7181 return (fptr_T)NULL; | |
7 | 7182 } |
7183 | |
772 | 7184 static fptr_T |
7185 do_Upper(d, c) | |
7186 int *d; | |
7 | 7187 int c; |
7188 { | |
772 | 7189 *d = MB_TOUPPER(c); |
7190 | |
7191 return (fptr_T)do_Upper; | |
7192 } | |
7193 | |
7194 static fptr_T | |
7195 do_lower(d, c) | |
7196 int *d; | |
7197 int c; | |
7198 { | |
7199 *d = MB_TOLOWER(c); | |
7200 | |
7201 return (fptr_T)NULL; | |
7202 } | |
7203 | |
7204 static fptr_T | |
7205 do_Lower(d, c) | |
7206 int *d; | |
7207 int c; | |
7208 { | |
7209 *d = MB_TOLOWER(c); | |
7210 | |
7211 return (fptr_T)do_Lower; | |
7 | 7212 } |
7213 | |
7214 /* | |
7215 * regtilde(): Replace tildes in the pattern by the old pattern. | |
7216 * | |
7217 * Short explanation of the tilde: It stands for the previous replacement | |
7218 * pattern. If that previous pattern also contains a ~ we should go back a | |
7219 * step further... But we insert the previous pattern into the current one | |
7220 * and remember that. | |
772 | 7221 * This still does not handle the case where "magic" changes. So require the |
7222 * user to keep his hands off of "magic". | |
7 | 7223 * |
7224 * The tildes are parsed once before the first call to vim_regsub(). | |
7225 */ | |
7226 char_u * | |
7227 regtilde(source, magic) | |
7228 char_u *source; | |
7229 int magic; | |
7230 { | |
7231 char_u *newsub = source; | |
7232 char_u *tmpsub; | |
7233 char_u *p; | |
7234 int len; | |
7235 int prevlen; | |
7236 | |
7237 for (p = newsub; *p; ++p) | |
7238 { | |
7239 if ((*p == '~' && magic) || (*p == '\\' && *(p + 1) == '~' && !magic)) | |
7240 { | |
7241 if (reg_prev_sub != NULL) | |
7242 { | |
7243 /* length = len(newsub) - 1 + len(prev_sub) + 1 */ | |
7244 prevlen = (int)STRLEN(reg_prev_sub); | |
7245 tmpsub = alloc((unsigned)(STRLEN(newsub) + prevlen)); | |
7246 if (tmpsub != NULL) | |
7247 { | |
7248 /* copy prefix */ | |
7249 len = (int)(p - newsub); /* not including ~ */ | |
7250 mch_memmove(tmpsub, newsub, (size_t)len); | |
1209 | 7251 /* interpret tilde */ |
7 | 7252 mch_memmove(tmpsub + len, reg_prev_sub, (size_t)prevlen); |
7253 /* copy postfix */ | |
7254 if (!magic) | |
7255 ++p; /* back off \ */ | |
7256 STRCPY(tmpsub + len + prevlen, p + 1); | |
7257 | |
7258 if (newsub != source) /* already allocated newsub */ | |
7259 vim_free(newsub); | |
7260 newsub = tmpsub; | |
7261 p = newsub + len + prevlen; | |
7262 } | |
7263 } | |
7264 else if (magic) | |
1621 | 7265 STRMOVE(p, p + 1); /* remove '~' */ |
7 | 7266 else |
1621 | 7267 STRMOVE(p, p + 2); /* remove '\~' */ |
7 | 7268 --p; |
7269 } | |
7270 else | |
7271 { | |
7272 if (*p == '\\' && p[1]) /* skip escaped characters */ | |
7273 ++p; | |
7274 #ifdef FEAT_MBYTE | |
7275 if (has_mbyte) | |
474 | 7276 p += (*mb_ptr2len)(p) - 1; |
7 | 7277 #endif |
7278 } | |
7279 } | |
7280 | |
7281 vim_free(reg_prev_sub); | |
7282 if (newsub != source) /* newsub was allocated, just keep it */ | |
7283 reg_prev_sub = newsub; | |
7284 else /* no ~ found, need to save newsub */ | |
7285 reg_prev_sub = vim_strsave(newsub); | |
7286 return newsub; | |
7287 } | |
7288 | |
7289 #ifdef FEAT_EVAL | |
7290 static int can_f_submatch = FALSE; /* TRUE when submatch() can be used */ | |
7291 | |
7292 /* These pointers are used instead of reg_match and reg_mmatch for | |
7293 * reg_submatch(). Needed for when the substitution string is an expression | |
7294 * that contains a call to substitute() and submatch(). */ | |
7295 static regmatch_T *submatch_match; | |
7296 static regmmatch_T *submatch_mmatch; | |
2011 | 7297 static linenr_T submatch_firstlnum; |
7298 static linenr_T submatch_maxline; | |
2904 | 7299 static int submatch_line_lbr; |
7 | 7300 #endif |
7301 | |
7302 #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO) | |
7303 /* | |
7304 * vim_regsub() - perform substitutions after a vim_regexec() or | |
7305 * vim_regexec_multi() match. | |
7306 * | |
7307 * If "copy" is TRUE really copy into "dest". | |
7308 * If "copy" is FALSE nothing is copied, this is just to find out the length | |
7309 * of the result. | |
7310 * | |
7311 * If "backslash" is TRUE, a backslash will be removed later, need to double | |
7312 * them to keep them, and insert a backslash before a CR to avoid it being | |
7313 * replaced with a line break later. | |
7314 * | |
7315 * Note: The matched text must not change between the call of | |
7316 * vim_regexec()/vim_regexec_multi() and vim_regsub()! It would make the back | |
7317 * references invalid! | |
7318 * | |
7319 * Returns the size of the replacement, including terminating NUL. | |
7320 */ | |
7321 int | |
7322 vim_regsub(rmp, source, dest, copy, magic, backslash) | |
7323 regmatch_T *rmp; | |
7324 char_u *source; | |
7325 char_u *dest; | |
7326 int copy; | |
7327 int magic; | |
7328 int backslash; | |
7329 { | |
7330 reg_match = rmp; | |
7331 reg_mmatch = NULL; | |
7332 reg_maxline = 0; | |
4061 | 7333 reg_buf = curbuf; |
7 | 7334 return vim_regsub_both(source, dest, copy, magic, backslash); |
7335 } | |
7336 #endif | |
7337 | |
7338 int | |
7339 vim_regsub_multi(rmp, lnum, source, dest, copy, magic, backslash) | |
7340 regmmatch_T *rmp; | |
7341 linenr_T lnum; | |
7342 char_u *source; | |
7343 char_u *dest; | |
7344 int copy; | |
7345 int magic; | |
7346 int backslash; | |
7347 { | |
7348 reg_match = NULL; | |
7349 reg_mmatch = rmp; | |
7350 reg_buf = curbuf; /* always works on the current buffer! */ | |
7351 reg_firstlnum = lnum; | |
7352 reg_maxline = curbuf->b_ml.ml_line_count - lnum; | |
7353 return vim_regsub_both(source, dest, copy, magic, backslash); | |
7354 } | |
7355 | |
7356 static int | |
7357 vim_regsub_both(source, dest, copy, magic, backslash) | |
7358 char_u *source; | |
7359 char_u *dest; | |
7360 int copy; | |
7361 int magic; | |
7362 int backslash; | |
7363 { | |
7364 char_u *src; | |
7365 char_u *dst; | |
7366 char_u *s; | |
7367 int c; | |
772 | 7368 int cc; |
7 | 7369 int no = -1; |
4244 | 7370 fptr_T func_all = (fptr_T)NULL; |
7371 fptr_T func_one = (fptr_T)NULL; | |
7 | 7372 linenr_T clnum = 0; /* init for GCC */ |
7373 int len = 0; /* init for GCC */ | |
7374 #ifdef FEAT_EVAL | |
7375 static char_u *eval_result = NULL; | |
7376 #endif | |
7377 | |
7378 /* Be paranoid... */ | |
7379 if (source == NULL || dest == NULL) | |
7380 { | |
7381 EMSG(_(e_null)); | |
7382 return 0; | |
7383 } | |
7384 if (prog_magic_wrong()) | |
7385 return 0; | |
7386 src = source; | |
7387 dst = dest; | |
7388 | |
7389 /* | |
7390 * When the substitute part starts with "\=" evaluate it as an expression. | |
7391 */ | |
7392 if (source[0] == '\\' && source[1] == '=' | |
7393 #ifdef FEAT_EVAL | |
7394 && !can_f_submatch /* can't do this recursively */ | |
7395 #endif | |
7396 ) | |
7397 { | |
7398 #ifdef FEAT_EVAL | |
7399 /* To make sure that the length doesn't change between checking the | |
7400 * length and copying the string, and to speed up things, the | |
7401 * resulting string is saved from the call with "copy" == FALSE to the | |
7402 * call with "copy" == TRUE. */ | |
7403 if (copy) | |
7404 { | |
7405 if (eval_result != NULL) | |
7406 { | |
7407 STRCPY(dest, eval_result); | |
7408 dst += STRLEN(eval_result); | |
7409 vim_free(eval_result); | |
7410 eval_result = NULL; | |
7411 } | |
7412 } | |
7413 else | |
7414 { | |
7415 win_T *save_reg_win; | |
7416 int save_ireg_ic; | |
7417 | |
7418 vim_free(eval_result); | |
7419 | |
7420 /* The expression may contain substitute(), which calls us | |
7421 * recursively. Make sure submatch() gets the text from the first | |
7422 * level. Don't need to save "reg_buf", because | |
7423 * vim_regexec_multi() can't be called recursively. */ | |
7424 submatch_match = reg_match; | |
7425 submatch_mmatch = reg_mmatch; | |
2011 | 7426 submatch_firstlnum = reg_firstlnum; |
7427 submatch_maxline = reg_maxline; | |
2904 | 7428 submatch_line_lbr = reg_line_lbr; |
7 | 7429 save_reg_win = reg_win; |
7430 save_ireg_ic = ireg_ic; | |
7431 can_f_submatch = TRUE; | |
7432 | |
714 | 7433 eval_result = eval_to_string(source + 2, NULL, TRUE); |
7 | 7434 if (eval_result != NULL) |
7435 { | |
2125
b8744d1982d1
updated for version 7.2.407
Bram Moolenaar <bram@zimbu.org>
parents:
2012
diff
changeset
|
7436 int had_backslash = FALSE; |
b8744d1982d1
updated for version 7.2.407
Bram Moolenaar <bram@zimbu.org>
parents:
2012
diff
changeset
|
7437 |
39 | 7438 for (s = eval_result; *s != NUL; mb_ptr_adv(s)) |
7 | 7439 { |
2904 | 7440 /* Change NL to CR, so that it becomes a line break, |
7441 * unless called from vim_regexec_nl(). | |
7 | 7442 * Skip over a backslashed character. */ |
2904 | 7443 if (*s == NL && !submatch_line_lbr) |
7 | 7444 *s = CAR; |
7445 else if (*s == '\\' && s[1] != NUL) | |
2125
b8744d1982d1
updated for version 7.2.407
Bram Moolenaar <bram@zimbu.org>
parents:
2012
diff
changeset
|
7446 { |
7 | 7447 ++s; |
2173 | 7448 /* Change NL to CR here too, so that this works: |
7449 * :s/abc\\\ndef/\="aaa\\\nbbb"/ on text: | |
7450 * abc\ | |
7451 * def | |
2904 | 7452 * Not when called from vim_regexec_nl(). |
2173 | 7453 */ |
2904 | 7454 if (*s == NL && !submatch_line_lbr) |
2173 | 7455 *s = CAR; |
2125
b8744d1982d1
updated for version 7.2.407
Bram Moolenaar <bram@zimbu.org>
parents:
2012
diff
changeset
|
7456 had_backslash = TRUE; |
b8744d1982d1
updated for version 7.2.407
Bram Moolenaar <bram@zimbu.org>
parents:
2012
diff
changeset
|
7457 } |
b8744d1982d1
updated for version 7.2.407
Bram Moolenaar <bram@zimbu.org>
parents:
2012
diff
changeset
|
7458 } |
b8744d1982d1
updated for version 7.2.407
Bram Moolenaar <bram@zimbu.org>
parents:
2012
diff
changeset
|
7459 if (had_backslash && backslash) |
b8744d1982d1
updated for version 7.2.407
Bram Moolenaar <bram@zimbu.org>
parents:
2012
diff
changeset
|
7460 { |
b8744d1982d1
updated for version 7.2.407
Bram Moolenaar <bram@zimbu.org>
parents:
2012
diff
changeset
|
7461 /* Backslashes will be consumed, need to double them. */ |
b8744d1982d1
updated for version 7.2.407
Bram Moolenaar <bram@zimbu.org>
parents:
2012
diff
changeset
|
7462 s = vim_strsave_escaped(eval_result, (char_u *)"\\"); |
b8744d1982d1
updated for version 7.2.407
Bram Moolenaar <bram@zimbu.org>
parents:
2012
diff
changeset
|
7463 if (s != NULL) |
b8744d1982d1
updated for version 7.2.407
Bram Moolenaar <bram@zimbu.org>
parents:
2012
diff
changeset
|
7464 { |
b8744d1982d1
updated for version 7.2.407
Bram Moolenaar <bram@zimbu.org>
parents:
2012
diff
changeset
|
7465 vim_free(eval_result); |
b8744d1982d1
updated for version 7.2.407
Bram Moolenaar <bram@zimbu.org>
parents:
2012
diff
changeset
|
7466 eval_result = s; |
b8744d1982d1
updated for version 7.2.407
Bram Moolenaar <bram@zimbu.org>
parents:
2012
diff
changeset
|
7467 } |
7 | 7468 } |
7469 | |
7470 dst += STRLEN(eval_result); | |
7471 } | |
7472 | |
7473 reg_match = submatch_match; | |
7474 reg_mmatch = submatch_mmatch; | |
2011 | 7475 reg_firstlnum = submatch_firstlnum; |
7476 reg_maxline = submatch_maxline; | |
2904 | 7477 reg_line_lbr = submatch_line_lbr; |
7 | 7478 reg_win = save_reg_win; |
7479 ireg_ic = save_ireg_ic; | |
7480 can_f_submatch = FALSE; | |
7481 } | |
7482 #endif | |
7483 } | |
7484 else | |
7485 while ((c = *src++) != NUL) | |
7486 { | |
7487 if (c == '&' && magic) | |
7488 no = 0; | |
7489 else if (c == '\\' && *src != NUL) | |
7490 { | |
7491 if (*src == '&' && !magic) | |
7492 { | |
7493 ++src; | |
7494 no = 0; | |
7495 } | |
7496 else if ('0' <= *src && *src <= '9') | |
7497 { | |
7498 no = *src++ - '0'; | |
7499 } | |
7500 else if (vim_strchr((char_u *)"uUlLeE", *src)) | |
7501 { | |
7502 switch (*src++) | |
7503 { | |
4244 | 7504 case 'u': func_one = (fptr_T)do_upper; |
7 | 7505 continue; |
4244 | 7506 case 'U': func_all = (fptr_T)do_Upper; |
7 | 7507 continue; |
4244 | 7508 case 'l': func_one = (fptr_T)do_lower; |
7 | 7509 continue; |
4244 | 7510 case 'L': func_all = (fptr_T)do_Lower; |
7 | 7511 continue; |
7512 case 'e': | |
4244 | 7513 case 'E': func_one = func_all = (fptr_T)NULL; |
7 | 7514 continue; |
7515 } | |
7516 } | |
7517 } | |
7518 if (no < 0) /* Ordinary character. */ | |
7519 { | |
798 | 7520 if (c == K_SPECIAL && src[0] != NUL && src[1] != NUL) |
7521 { | |
1209 | 7522 /* Copy a special key as-is. */ |
798 | 7523 if (copy) |
7524 { | |
7525 *dst++ = c; | |
7526 *dst++ = *src++; | |
7527 *dst++ = *src++; | |
7528 } | |
7529 else | |
7530 { | |
7531 dst += 3; | |
7532 src += 2; | |
7533 } | |
7534 continue; | |
7535 } | |
7536 | |
7 | 7537 if (c == '\\' && *src != NUL) |
7538 { | |
7539 /* Check for abbreviations -- webb */ | |
7540 switch (*src) | |
7541 { | |
7542 case 'r': c = CAR; ++src; break; | |
7543 case 'n': c = NL; ++src; break; | |
7544 case 't': c = TAB; ++src; break; | |
7545 /* Oh no! \e already has meaning in subst pat :-( */ | |
7546 /* case 'e': c = ESC; ++src; break; */ | |
7547 case 'b': c = Ctrl_H; ++src; break; | |
7548 | |
7549 /* If "backslash" is TRUE the backslash will be removed | |
7550 * later. Used to insert a literal CR. */ | |
7551 default: if (backslash) | |
7552 { | |
7553 if (copy) | |
7554 *dst = '\\'; | |
7555 ++dst; | |
7556 } | |
7557 c = *src++; | |
7558 } | |
7559 } | |
798 | 7560 #ifdef FEAT_MBYTE |
7561 else if (has_mbyte) | |
7562 c = mb_ptr2char(src - 1); | |
7563 #endif | |
7 | 7564 |
7565 /* Write to buffer, if copy is set. */ | |
4244 | 7566 if (func_one != (fptr_T)NULL) |
7567 /* Turbo C complains without the typecast */ | |
7568 func_one = (fptr_T)(func_one(&cc, c)); | |
7569 else if (func_all != (fptr_T)NULL) | |
7570 /* Turbo C complains without the typecast */ | |
7571 func_all = (fptr_T)(func_all(&cc, c)); | |
7572 else /* just copy */ | |
772 | 7573 cc = c; |
7574 | |
7575 #ifdef FEAT_MBYTE | |
7576 if (has_mbyte) | |
7 | 7577 { |
2307
81527f127fb1
Fix: Composing characters in :s substitute text were dropped.
Bram Moolenaar <bram@vim.org>
parents:
2247
diff
changeset
|
7578 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
|
7579 |
7 | 7580 if (copy) |
772 | 7581 mb_char2bytes(cc, dst); |
7582 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
|
7583 if (enc_utf8) |
81527f127fb1
Fix: Composing characters in :s substitute text were dropped.
Bram Moolenaar <bram@vim.org>
parents:
2247
diff
changeset
|
7584 { |
81527f127fb1
Fix: Composing characters in :s substitute text were dropped.
Bram Moolenaar <bram@vim.org>
parents:
2247
diff
changeset
|
7585 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
|
7586 |
81527f127fb1
Fix: Composing characters in :s substitute text were dropped.
Bram Moolenaar <bram@vim.org>
parents:
2247
diff
changeset
|
7587 /* 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
|
7588 * 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
|
7589 if (clen < totlen) |
81527f127fb1
Fix: Composing characters in :s substitute text were dropped.
Bram Moolenaar <bram@vim.org>
parents:
2247
diff
changeset
|
7590 { |
81527f127fb1
Fix: Composing characters in :s substitute text were dropped.
Bram Moolenaar <bram@vim.org>
parents:
2247
diff
changeset
|
7591 if (copy) |
81527f127fb1
Fix: Composing characters in :s substitute text were dropped.
Bram Moolenaar <bram@vim.org>
parents:
2247
diff
changeset
|
7592 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
|
7593 (size_t)(totlen - clen)); |
81527f127fb1
Fix: Composing characters in :s substitute text were dropped.
Bram Moolenaar <bram@vim.org>
parents:
2247
diff
changeset
|
7594 dst += totlen - clen; |
81527f127fb1
Fix: Composing characters in :s substitute text were dropped.
Bram Moolenaar <bram@vim.org>
parents:
2247
diff
changeset
|
7595 } |
81527f127fb1
Fix: Composing characters in :s substitute text were dropped.
Bram Moolenaar <bram@vim.org>
parents:
2247
diff
changeset
|
7596 } |
81527f127fb1
Fix: Composing characters in :s substitute text were dropped.
Bram Moolenaar <bram@vim.org>
parents:
2247
diff
changeset
|
7597 src += totlen - 1; |
7 | 7598 } |
7599 else | |
7600 #endif | |
7601 if (copy) | |
772 | 7602 *dst = cc; |
7 | 7603 dst++; |
7604 } | |
7605 else | |
7606 { | |
7607 if (REG_MULTI) | |
7608 { | |
7609 clnum = reg_mmatch->startpos[no].lnum; | |
7610 if (clnum < 0 || reg_mmatch->endpos[no].lnum < 0) | |
7611 s = NULL; | |
7612 else | |
7613 { | |
7614 s = reg_getline(clnum) + reg_mmatch->startpos[no].col; | |
7615 if (reg_mmatch->endpos[no].lnum == clnum) | |
7616 len = reg_mmatch->endpos[no].col | |
7617 - reg_mmatch->startpos[no].col; | |
7618 else | |
7619 len = (int)STRLEN(s); | |
7620 } | |
7621 } | |
7622 else | |
7623 { | |
7624 s = reg_match->startp[no]; | |
7625 if (reg_match->endp[no] == NULL) | |
7626 s = NULL; | |
7627 else | |
7628 len = (int)(reg_match->endp[no] - s); | |
7629 } | |
7630 if (s != NULL) | |
7631 { | |
7632 for (;;) | |
7633 { | |
7634 if (len == 0) | |
7635 { | |
7636 if (REG_MULTI) | |
7637 { | |
7638 if (reg_mmatch->endpos[no].lnum == clnum) | |
7639 break; | |
7640 if (copy) | |
7641 *dst = CAR; | |
7642 ++dst; | |
7643 s = reg_getline(++clnum); | |
7644 if (reg_mmatch->endpos[no].lnum == clnum) | |
7645 len = reg_mmatch->endpos[no].col; | |
7646 else | |
7647 len = (int)STRLEN(s); | |
7648 } | |
7649 else | |
7650 break; | |
7651 } | |
7652 else if (*s == NUL) /* we hit NUL. */ | |
7653 { | |
7654 if (copy) | |
7655 EMSG(_(e_re_damg)); | |
7656 goto exit; | |
7657 } | |
7658 else | |
7659 { | |
7660 if (backslash && (*s == CAR || *s == '\\')) | |
7661 { | |
7662 /* | |
7663 * Insert a backslash in front of a CR, otherwise | |
7664 * it will be replaced by a line break. | |
7665 * Number of backslashes will be halved later, | |
7666 * double them here. | |
7667 */ | |
7668 if (copy) | |
7669 { | |
7670 dst[0] = '\\'; | |
7671 dst[1] = *s; | |
7672 } | |
7673 dst += 2; | |
7674 } | |
7675 else | |
7676 { | |
772 | 7677 #ifdef FEAT_MBYTE |
7678 if (has_mbyte) | |
7679 c = mb_ptr2char(s); | |
7680 else | |
7681 #endif | |
7682 c = *s; | |
7683 | |
4244 | 7684 if (func_one != (fptr_T)NULL) |
7685 /* Turbo C complains without the typecast */ | |
7686 func_one = (fptr_T)(func_one(&cc, c)); | |
7687 else if (func_all != (fptr_T)NULL) | |
7688 /* Turbo C complains without the typecast */ | |
7689 func_all = (fptr_T)(func_all(&cc, c)); | |
7690 else /* just copy */ | |
772 | 7691 cc = c; |
7692 | |
7693 #ifdef FEAT_MBYTE | |
7694 if (has_mbyte) | |
7 | 7695 { |
1332 | 7696 int l; |
7697 | |
7698 /* Copy composing characters separately, one | |
7699 * at a time. */ | |
7700 if (enc_utf8) | |
7701 l = utf_ptr2len(s) - 1; | |
7702 else | |
7703 l = mb_ptr2len(s) - 1; | |
772 | 7704 |
7705 s += l; | |
7706 len -= l; | |
7707 if (copy) | |
7708 mb_char2bytes(cc, dst); | |
7709 dst += mb_char2len(cc) - 1; | |
7 | 7710 } |
772 | 7711 else |
7712 #endif | |
7713 if (copy) | |
7714 *dst = cc; | |
7715 dst++; | |
7 | 7716 } |
772 | 7717 |
7 | 7718 ++s; |
7719 --len; | |
7720 } | |
7721 } | |
7722 } | |
7723 no = -1; | |
7724 } | |
7725 } | |
7726 if (copy) | |
7727 *dst = NUL; | |
7728 | |
7729 exit: | |
7730 return (int)((dst - dest) + 1); | |
7731 } | |
7732 | |
7733 #ifdef FEAT_EVAL | |
2012 | 7734 static char_u *reg_getline_submatch __ARGS((linenr_T lnum)); |
7735 | |
7 | 7736 /* |
2011 | 7737 * Call reg_getline() with the line numbers from the submatch. If a |
7738 * substitute() was used the reg_maxline and other values have been | |
7739 * overwritten. | |
7740 */ | |
7741 static char_u * | |
7742 reg_getline_submatch(lnum) | |
7743 linenr_T lnum; | |
7744 { | |
7745 char_u *s; | |
7746 linenr_T save_first = reg_firstlnum; | |
7747 linenr_T save_max = reg_maxline; | |
7748 | |
7749 reg_firstlnum = submatch_firstlnum; | |
7750 reg_maxline = submatch_maxline; | |
7751 | |
7752 s = reg_getline(lnum); | |
7753 | |
7754 reg_firstlnum = save_first; | |
7755 reg_maxline = save_max; | |
7756 return s; | |
7757 } | |
7758 | |
7759 /* | |
1209 | 7760 * Used for the submatch() function: get the string from the n'th submatch in |
7 | 7761 * allocated memory. |
7762 * Returns NULL when not in a ":s" command and for a non-existing submatch. | |
7763 */ | |
7764 char_u * | |
7765 reg_submatch(no) | |
7766 int no; | |
7767 { | |
7768 char_u *retval = NULL; | |
7769 char_u *s; | |
7770 int len; | |
7771 int round; | |
7772 linenr_T lnum; | |
7773 | |
840 | 7774 if (!can_f_submatch || no < 0) |
7 | 7775 return NULL; |
7776 | |
7777 if (submatch_match == NULL) | |
7778 { | |
7779 /* | |
7780 * First round: compute the length and allocate memory. | |
7781 * Second round: copy the text. | |
7782 */ | |
7783 for (round = 1; round <= 2; ++round) | |
7784 { | |
7785 lnum = submatch_mmatch->startpos[no].lnum; | |
7786 if (lnum < 0 || submatch_mmatch->endpos[no].lnum < 0) | |
7787 return NULL; | |
7788 | |
2011 | 7789 s = reg_getline_submatch(lnum) + submatch_mmatch->startpos[no].col; |
7 | 7790 if (s == NULL) /* anti-crash check, cannot happen? */ |
7791 break; | |
7792 if (submatch_mmatch->endpos[no].lnum == lnum) | |
7793 { | |
7794 /* Within one line: take form start to end col. */ | |
7795 len = submatch_mmatch->endpos[no].col | |
7796 - submatch_mmatch->startpos[no].col; | |
7797 if (round == 2) | |
418 | 7798 vim_strncpy(retval, s, len); |
7 | 7799 ++len; |
7800 } | |
7801 else | |
7802 { | |
7803 /* Multiple lines: take start line from start col, middle | |
7804 * lines completely and end line up to end col. */ | |
7805 len = (int)STRLEN(s); | |
7806 if (round == 2) | |
7807 { | |
7808 STRCPY(retval, s); | |
7809 retval[len] = '\n'; | |
7810 } | |
7811 ++len; | |
7812 ++lnum; | |
7813 while (lnum < submatch_mmatch->endpos[no].lnum) | |
7814 { | |
2011 | 7815 s = reg_getline_submatch(lnum++); |
7 | 7816 if (round == 2) |
7817 STRCPY(retval + len, s); | |
7818 len += (int)STRLEN(s); | |
7819 if (round == 2) | |
7820 retval[len] = '\n'; | |
7821 ++len; | |
7822 } | |
7823 if (round == 2) | |
2011 | 7824 STRNCPY(retval + len, reg_getline_submatch(lnum), |
7 | 7825 submatch_mmatch->endpos[no].col); |
7826 len += submatch_mmatch->endpos[no].col; | |
7827 if (round == 2) | |
7828 retval[len] = NUL; | |
7829 ++len; | |
7830 } | |
7831 | |
840 | 7832 if (retval == NULL) |
7 | 7833 { |
7834 retval = lalloc((long_u)len, TRUE); | |
840 | 7835 if (retval == NULL) |
7 | 7836 return NULL; |
7837 } | |
7838 } | |
7839 } | |
7840 else | |
7841 { | |
1815 | 7842 s = submatch_match->startp[no]; |
7843 if (s == NULL || submatch_match->endp[no] == NULL) | |
7 | 7844 retval = NULL; |
7845 else | |
7846 retval = vim_strnsave(s, (int)(submatch_match->endp[no] - s)); | |
7847 } | |
7848 | |
7849 return retval; | |
7850 } | |
7851 #endif | |
4444 | 7852 |
7853 static regengine_T bt_regengine = | |
7854 { | |
7855 bt_regcomp, | |
7856 bt_regexec, | |
7857 #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \ | |
7858 || defined(FIND_REPLACE_DIALOG) || defined(PROTO) | |
7859 bt_regexec_nl, | |
7860 #endif | |
7861 bt_regexec_multi | |
7862 #ifdef DEBUG | |
7863 ,(char_u *)"" | |
7864 #endif | |
7865 }; | |
7866 | |
7867 | |
7868 #include "regexp_nfa.c" | |
7869 | |
7870 static regengine_T nfa_regengine = | |
7871 { | |
7872 nfa_regcomp, | |
7873 nfa_regexec, | |
7874 #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \ | |
7875 || defined(FIND_REPLACE_DIALOG) || defined(PROTO) | |
7876 nfa_regexec_nl, | |
7877 #endif | |
7878 nfa_regexec_multi | |
7879 #ifdef DEBUG | |
7880 ,(char_u *)"" | |
7881 #endif | |
7882 }; | |
7883 | |
7884 /* Which regexp engine to use? Needed for vim_regcomp(). | |
7885 * Must match with 'regexpengine'. */ | |
7886 static int regexp_engine = 0; | |
7887 #define AUTOMATIC_ENGINE 0 | |
7888 #define BACKTRACKING_ENGINE 1 | |
7889 #define NFA_ENGINE 2 | |
7890 #ifdef DEBUG | |
7891 static char_u regname[][30] = { | |
7892 "AUTOMATIC Regexp Engine", | |
4579
7a2be4a39423
updated for version 7.3.1037
Bram Moolenaar <bram@vim.org>
parents:
4505
diff
changeset
|
7893 "BACKTRACKING Regexp Engine", |
4444 | 7894 "NFA Regexp Engine" |
7895 }; | |
7896 #endif | |
7897 | |
7898 /* | |
7899 * Compile a regular expression into internal code. | |
7900 * Returns the program in allocated memory. Returns NULL for an error. | |
7901 */ | |
7902 regprog_T * | |
7903 vim_regcomp(expr_arg, re_flags) | |
7904 char_u *expr_arg; | |
7905 int re_flags; | |
7906 { | |
7907 regprog_T *prog = NULL; | |
7908 char_u *expr = expr_arg; | |
7909 | |
7910 syntax_error = FALSE; | |
7911 regexp_engine = p_re; | |
7912 | |
7913 /* Check for prefix "\%#=", that sets the regexp engine */ | |
7914 if (STRNCMP(expr, "\\%#=", 4) == 0) | |
7915 { | |
7916 int newengine = expr[4] - '0'; | |
7917 | |
7918 if (newengine == AUTOMATIC_ENGINE | |
7919 || newengine == BACKTRACKING_ENGINE | |
7920 || newengine == NFA_ENGINE) | |
7921 { | |
7922 regexp_engine = expr[4] - '0'; | |
7923 expr += 5; | |
7924 #ifdef DEBUG | |
7925 EMSG3("New regexp mode selected (%d): %s", regexp_engine, | |
7926 regname[newengine]); | |
7927 #endif | |
7928 } | |
7929 else | |
7930 { | |
7931 EMSG(_("E864: \\%#= can only be followed by 0, 1, or 2. The automatic engine will be used ")); | |
7932 regexp_engine = AUTOMATIC_ENGINE; | |
7933 } | |
7934 } | |
7935 #ifdef DEBUG | |
7936 bt_regengine.expr = expr; | |
7937 nfa_regengine.expr = expr; | |
7938 #endif | |
7939 | |
7940 /* | |
7941 * First try the NFA engine, unless backtracking was requested. | |
7942 */ | |
7943 if (regexp_engine != BACKTRACKING_ENGINE) | |
7944 prog = nfa_regengine.regcomp(expr, re_flags); | |
7945 else | |
7946 prog = bt_regengine.regcomp(expr, re_flags); | |
7947 | |
7948 if (prog == NULL) /* error compiling regexp with initial engine */ | |
7949 { | |
4460 | 7950 #ifdef BT_REGEXP_DEBUG_LOG |
4444 | 7951 if (regexp_engine != BACKTRACKING_ENGINE) /* debugging log for NFA */ |
7952 { | |
7953 FILE *f; | |
4460 | 7954 f = fopen(BT_REGEXP_DEBUG_LOG_NAME, "a"); |
4444 | 7955 if (f) |
7956 { | |
7957 if (!syntax_error) | |
7958 fprintf(f, "NFA engine could not handle \"%s\"\n", expr); | |
7959 else | |
7960 fprintf(f, "Syntax error in \"%s\"\n", expr); | |
7961 fclose(f); | |
7962 } | |
7963 else | |
4460 | 7964 EMSG2("(NFA) Could not open \"%s\" to write !!!", |
7965 BT_REGEXP_DEBUG_LOG_NAME); | |
4444 | 7966 /* |
7967 if (syntax_error) | |
7968 EMSG("NFA Regexp: Syntax Error !"); | |
7969 */ | |
7970 } | |
7971 #endif | |
7972 /* | |
7973 * If NFA engine failed, then revert to the backtracking engine. | |
7974 * Except when there was a syntax error, which was properly handled by | |
7975 * NFA engine. | |
7976 */ | |
7977 if (regexp_engine == AUTOMATIC_ENGINE) | |
7978 if (!syntax_error) | |
7979 prog = bt_regengine.regcomp(expr, re_flags); | |
7980 | |
7981 } /* endif prog==NULL */ | |
7982 | |
7983 | |
7984 return prog; | |
7985 } | |
7986 | |
7987 /* | |
7988 * Match a regexp against a string. | |
7989 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp(). | |
7990 * Uses curbuf for line count and 'iskeyword'. | |
7991 * | |
7992 * Return TRUE if there is a match, FALSE if not. | |
7993 */ | |
7994 int | |
7995 vim_regexec(rmp, line, col) | |
7996 regmatch_T *rmp; | |
7997 char_u *line; /* string to match against */ | |
7998 colnr_T col; /* column to start looking for match */ | |
7999 { | |
8000 return rmp->regprog->engine->regexec(rmp, line, col); | |
8001 } | |
8002 | |
8003 #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \ | |
8004 || defined(FIND_REPLACE_DIALOG) || defined(PROTO) | |
8005 /* | |
8006 * Like vim_regexec(), but consider a "\n" in "line" to be a line break. | |
8007 */ | |
8008 int | |
8009 vim_regexec_nl(rmp, line, col) | |
8010 regmatch_T *rmp; | |
8011 char_u *line; | |
8012 colnr_T col; | |
8013 { | |
8014 return rmp->regprog->engine->regexec_nl(rmp, line, col); | |
8015 } | |
8016 #endif | |
8017 | |
8018 /* | |
8019 * Match a regexp against multiple lines. | |
8020 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp(). | |
8021 * Uses curbuf for line count and 'iskeyword'. | |
8022 * | |
8023 * Return zero if there is no match. Return number of lines contained in the | |
8024 * match otherwise. | |
8025 */ | |
8026 long | |
8027 vim_regexec_multi(rmp, win, buf, lnum, col, tm) | |
8028 regmmatch_T *rmp; | |
8029 win_T *win; /* window in which to search or NULL */ | |
8030 buf_T *buf; /* buffer in which to search */ | |
8031 linenr_T lnum; /* nr of line to start looking for match */ | |
8032 colnr_T col; /* column to start looking for match */ | |
8033 proftime_T *tm; /* timeout limit or NULL */ | |
8034 { | |
8035 return rmp->regprog->engine->regexec_multi(rmp, win, buf, lnum, col, tm); | |
8036 } |