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