blob: 663e1de00a02ebcb3948581d227cc352afb2f5b9 [file] [log] [blame]
Guido van Rossum004c1e11997-05-09 02:35:58 +00001/* regexpr.c
2 *
3 * Author: Tatu Ylonen <ylo@ngs.fi>
4 *
5 * Copyright (c) 1991 Tatu Ylonen, Espoo, Finland
6 *
7 * Permission to use, copy, modify, distribute, and sell this software
8 * and its documentation for any purpose is hereby granted without
9 * fee, provided that the above copyright notice appear in all copies.
10 * This software is provided "as is" without express or implied
11 * warranty.
12 *
13 * Created: Thu Sep 26 17:14:05 1991 ylo
14 * Last modified: Mon Nov 4 17:06:48 1991 ylo
15 * Ported to Think C: 19 Jan 1992 guido@cwi.nl
16 *
17 * This code draws many ideas from the regular expression packages by
18 * Henry Spencer of the University of Toronto and Richard Stallman of
19 * the Free Software Foundation.
20 *
21 * Emacs-specific code and syntax table code is almost directly borrowed
22 * from GNU regexp.
23 *
24 * Bugs fixed and lots of reorganization by Jeffrey C. Ollie, April
25 * 1997 Thanks for bug reports and ideas from Andrew Kuchling, Tim
26 * Peters, Guido van Rossum, Ka-Ping Yee, Sjoerd Mullender, and
27 * probably one or two others that I'm forgetting.
28 *
29 * $Id$ */
Guido van Rossumb674c3b1992-01-19 16:32:47 +000030
Guido van Rossum339cfa31996-08-08 19:12:37 +000031#include "config.h" /* For Win* specific redefinition of printf c.s. */
Guido van Rossum339cfa31996-08-08 19:12:37 +000032
Guido van Rossum004c1e11997-05-09 02:35:58 +000033#include "myproto.h" /* For PROTO macro --Guido */
Guido van Rossum3b1a57a1992-01-27 16:47:46 +000034
Guido van Rossumb674c3b1992-01-19 16:32:47 +000035#include <stdio.h>
Guido van Rossum004c1e11997-05-09 02:35:58 +000036
37#ifndef NDEBUG
38#define NDEBUG 1
39#endif
40
Guido van Rossumb674c3b1992-01-19 16:32:47 +000041#include <assert.h>
42#include "regexpr.h"
43
Guido van Rossum3b1a57a1992-01-27 16:47:46 +000044#ifdef THINK_C
45/* Think C on the Mac really needs these headers... --Guido */
46#include <stdlib.h>
47#include <string.h>
48#else
Guido van Rossum88661e81996-05-23 22:55:58 +000049#if defined(__STDC__) || defined(_MSC_VER)
Guido van Rossum9abc5391992-03-27 17:24:37 +000050/* Don't mess around, use the standard headers */
51#include <stdlib.h>
52#include <string.h>
53#else
Guido van Rossumb674c3b1992-01-19 16:32:47 +000054char *malloc();
55void free();
56char *realloc();
Guido van Rossum9abc5391992-03-27 17:24:37 +000057#endif /* __STDC__ */
58#endif /* THINK_C */
Guido van Rossumb674c3b1992-01-19 16:32:47 +000059
Guido van Rossumdb25f321997-07-10 14:31:32 +000060/* The original code blithely assumed that sizeof(short) == 2. Not
61 * always true. Original instances of "(short)x" were replaced by
62 * SHORT(x), where SHORT is #defined below. */
63
64#define SHORT(x) ((x) & 0x8000 ? (x) - 0x10000 : (x))
65
Guido van Rossum004c1e11997-05-09 02:35:58 +000066/* The stack implementation is taken from an idea by Andrew Kuchling.
67 * It's a doubly linked list of arrays. The advantages of this over a
68 * simple linked list are that the number of mallocs required are
69 * reduced. It also makes it possible to statically allocate enough
70 * space so that small patterns don't ever need to call malloc.
71 *
72 * The advantages over a single array is that is periodically
73 * realloced when more space is needed is that we avoid ever copying
74 * the stack. */
75
76/* item_t is the basic stack element. Defined as a union of
77 * structures so that both registers, failure points, and counters can
78 * be pushed/popped from the stack. There's nothing built into the
79 * item to keep track of whether a certain stack item is a register, a
80 * failure point, or a counter. */
81
82typedef union item_t
83{
Guido van Rossumdb25f321997-07-10 14:31:32 +000084 struct
85 {
86 int num;
87 int level;
88 char *start;
89 char *end;
90 } reg;
91 struct
92 {
93 int count;
94 int level;
95 int phantom;
96 char *code;
97 char *text;
98 } fail;
99 struct
100 {
101 int num;
102 int level;
103 int count;
104 } cntr;
Guido van Rossum004c1e11997-05-09 02:35:58 +0000105} item_t;
106
107#define STACK_PAGE_SIZE 256
108#define NUM_REGISTERS 256
109
110/* A 'page' of stack items. */
111
112typedef struct item_page_t
113{
Guido van Rossumdb25f321997-07-10 14:31:32 +0000114 item_t items[STACK_PAGE_SIZE];
115 struct item_page_t *prev;
116 struct item_page_t *next;
Guido van Rossum004c1e11997-05-09 02:35:58 +0000117} item_page_t;
118
119
120typedef struct match_state
121{
Guido van Rossumdb25f321997-07-10 14:31:32 +0000122 /* The number of registers that have been pushed onto the stack
123 * since the last failure point. */
Guido van Rossum004c1e11997-05-09 02:35:58 +0000124
Guido van Rossumdb25f321997-07-10 14:31:32 +0000125 int count;
126
127 /* Used to control when registers need to be pushed onto the
128 * stack. */
129
130 int level;
131
132 /* The number of failure points on the stack. */
133
134 int point;
135
136 /* Storage for the registers. Each register consists of two
137 * pointers to characters. So register N is represented as
138 * start[N] and end[N]. The pointers must be converted to
139 * offsets from the beginning of the string before returning the
140 * registers to the calling program. */
141
142 char *start[NUM_REGISTERS];
143 char *end[NUM_REGISTERS];
144
145 /* Keeps track of whether a register has changed recently. */
146
147 int changed[NUM_REGISTERS];
148
149 /* Structure to encapsulate the stack. */
150 struct
151 {
152 /* index into the curent page. If index == 0 and you need
153 * to pop an item, move to the previous page and set index
154 * = STACK_PAGE_SIZE - 1. Otherwise decrement index to
155 * push a page. If index == STACK_PAGE_SIZE and you need
156 * to push a page move to the next page and set index =
157 * 0. If there is no new next page, allocate a new page
158 * and link it in. Otherwise, increment index to push a
159 * page. */
160
161 int index;
162 item_page_t *current; /* Pointer to the current page. */
163 item_page_t first; /* First page is statically allocated. */
164 } stack;
Guido van Rossum004c1e11997-05-09 02:35:58 +0000165} match_state;
166
Guido van Rossumdb25f321997-07-10 14:31:32 +0000167/* Initialize a state object */
168
169/* #define NEW_STATE(state) \ */
170/* memset(&state, 0, (void *)(&state.stack) - (void *)(&state)); \ */
171/* state.stack.current = &state.stack.first; \ */
172/* state.stack.first.prev = NULL; \ */
173/* state.stack.first.next = NULL; \ */
174/* state.stack.index = 0; \ */
175/* state.level = 1 */
176
177#define NEW_STATE(state, nregs) \
178{ \
179 int i; \
180 for (i = 0; i < nregs; i++) \
181 { \
182 state.start[i] = NULL; \
183 state.end[i] = NULL; \
184 state.changed[i] = 0; \
185 } \
186 state.stack.current = &state.stack.first; \
187 state.stack.first.prev = NULL; \
188 state.stack.first.next = NULL; \
189 state.stack.index = 0; \
190 state.level = 1; \
191 state.count = 0; \
192 state.level = 0; \
193 state.point = 0; \
194}
195
196/* Free any memory that might have been malloc'd */
197
198#define FREE_STATE(state) \
199while(state.stack.first.next != NULL) \
200{ \
201 state.stack.current = state.stack.first.next; \
202 state.stack.first.next = state.stack.current->next; \
203 free(state.stack.current); \
204}
205
Guido van Rossum004c1e11997-05-09 02:35:58 +0000206/* Discard the top 'count' stack items. */
207
208#define STACK_DISCARD(stack, count, on_error) \
209stack.index -= count; \
210while (stack.index < 0) \
211{ \
Guido van Rossumfaf49081997-07-15 01:47:08 +0000212 if (stack.current->prev == NULL) \
213 on_error; \
214 stack.current = stack.current->prev; \
215 stack.index += STACK_PAGE_SIZE; \
Guido van Rossum004c1e11997-05-09 02:35:58 +0000216}
217
218/* Store a pointer to the previous item on the stack. Used to pop an
219 * item off of the stack. */
220
221#define STACK_PREV(stack, top, on_error) \
222if (stack.index == 0) \
223{ \
Guido van Rossumfaf49081997-07-15 01:47:08 +0000224 if (stack.current->prev == NULL) \
225 on_error; \
226 stack.current = stack.current->prev; \
227 stack.index = STACK_PAGE_SIZE - 1; \
Guido van Rossum004c1e11997-05-09 02:35:58 +0000228} \
229else \
Guido van Rossumfaf49081997-07-15 01:47:08 +0000230{ \
231 stack.index--; \
232} \
Guido van Rossum004c1e11997-05-09 02:35:58 +0000233top = &(stack.current->items[stack.index])
234
235/* Store a pointer to the next item on the stack. Used to push an item
236 * on to the stack. */
237
238#define STACK_NEXT(stack, top, on_error) \
239if (stack.index == STACK_PAGE_SIZE) \
240{ \
Guido van Rossumfaf49081997-07-15 01:47:08 +0000241 if (stack.current->next == NULL) \
242 { \
243 stack.current->next = (item_page_t *)malloc(sizeof(item_page_t)); \
244 if (stack.current->next == NULL) \
245 on_error; \
246 stack.current->next->prev = stack.current; \
247 stack.current->next->next = NULL; \
248 } \
249 stack.current = stack.current->next; \
250 stack.index = 0; \
Guido van Rossum004c1e11997-05-09 02:35:58 +0000251} \
252top = &(stack.current->items[stack.index++])
253
254/* Store a pointer to the item that is 'count' items back in the
255 * stack. STACK_BACK(stack, top, 1, on_error) is equivalent to
256 * STACK_TOP(stack, top, on_error). */
257
258#define STACK_BACK(stack, top, count, on_error) \
259{ \
Guido van Rossumfaf49081997-07-15 01:47:08 +0000260 int index; \
261 item_page_t *current; \
262 current = stack.current; \
263 index = stack.index - (count); \
264 while (index < 0) \
265 { \
266 if (current->prev == NULL) \
267 on_error; \
268 current = current->prev; \
269 index += STACK_PAGE_SIZE; \
270 } \
271 top = &(current->items[index]); \
Guido van Rossum004c1e11997-05-09 02:35:58 +0000272}
273
274/* Store a pointer to the top item on the stack. Execute the
275 * 'on_error' code if there are no items on the stack. */
276
277#define STACK_TOP(stack, top, on_error) \
278if (stack.index == 0) \
279{ \
Guido van Rossumfaf49081997-07-15 01:47:08 +0000280 if (stack.current->prev == NULL) \
281 on_error; \
282 top = &(stack.current->prev->items[STACK_PAGE_SIZE - 1]); \
Guido van Rossum004c1e11997-05-09 02:35:58 +0000283} \
284else \
Guido van Rossumfaf49081997-07-15 01:47:08 +0000285{ \
286 top = &(stack.current->items[stack.index - 1]); \
287}
Guido van Rossum004c1e11997-05-09 02:35:58 +0000288
289/* Test to see if the stack is empty */
290
291#define STACK_EMPTY(stack) ((stack.index == 0) && \
292 (stack.current->prev == NULL))
293
Guido van Rossum004c1e11997-05-09 02:35:58 +0000294/* Return the start of register 'reg' */
295
296#define GET_REG_START(state, reg) (state.start[reg])
297
298/* Return the end of register 'reg' */
299
300#define GET_REG_END(state, reg) (state.end[reg])
301
302/* Set the start of register 'reg'. If the state of the register needs
303 * saving, push it on the stack. */
304
305#define SET_REG_START(state, reg, text, on_error) \
306if(state.changed[reg] < state.level) \
307{ \
Guido van Rossumfaf49081997-07-15 01:47:08 +0000308 item_t *item; \
309 STACK_NEXT(state.stack, item, on_error); \
310 item->reg.num = reg; \
311 item->reg.start = state.start[reg]; \
312 item->reg.end = state.end[reg]; \
313 item->reg.level = state.changed[reg]; \
314 state.changed[reg] = state.level; \
315 state.count++; \
Guido van Rossum004c1e11997-05-09 02:35:58 +0000316} \
317state.start[reg] = text
318
319/* Set the end of register 'reg'. If the state of the register needs
320 * saving, push it on the stack. */
321
322#define SET_REG_END(state, reg, text, on_error) \
323if(state.changed[reg] < state.level) \
324{ \
Guido van Rossumfaf49081997-07-15 01:47:08 +0000325 item_t *item; \
326 STACK_NEXT(state.stack, item, on_error); \
327 item->reg.num = reg; \
328 item->reg.start = state.start[reg]; \
329 item->reg.end = state.end[reg]; \
330 item->reg.level = state.changed[reg]; \
331 state.changed[reg] = state.level; \
332 state.count++; \
Guido van Rossum004c1e11997-05-09 02:35:58 +0000333} \
334state.end[reg] = text
335
336#define PUSH_FAILURE(state, xcode, xtext, on_error) \
337{ \
Guido van Rossumfaf49081997-07-15 01:47:08 +0000338 item_t *item; \
339 STACK_NEXT(state.stack, item, on_error); \
340 item->fail.code = xcode; \
341 item->fail.text = xtext; \
342 item->fail.count = state.count; \
343 item->fail.level = state.level; \
344 item->fail.phantom = 0; \
345 state.count = 0; \
346 state.level++; \
347 state.point++; \
Guido van Rossum004c1e11997-05-09 02:35:58 +0000348}
349
350/* Update the last failure point with a new position in the text. */
351
Guido van Rossum004c1e11997-05-09 02:35:58 +0000352#define UPDATE_FAILURE(state, xtext, on_error) \
353{ \
Guido van Rossumfaf49081997-07-15 01:47:08 +0000354 item_t *item; \
355 STACK_BACK(state.stack, item, state.count + 1, on_error); \
356 if (!item->fail.phantom) \
357 { \
358 item_t *item2; \
359 STACK_NEXT(state.stack, item2, on_error); \
360 item2->fail.code = item->fail.code; \
361 item2->fail.text = xtext; \
362 item2->fail.count = state.count; \
363 item2->fail.level = state.level; \
364 item2->fail.phantom = 1; \
365 state.count = 0; \
366 state.level++; \
367 state.point++; \
368 } \
369 else \
370 { \
371 STACK_DISCARD(state.stack, state.count, on_error); \
372 STACK_TOP(state.stack, item, on_error); \
373 item->fail.text = xtext; \
374 state.count = 0; \
375 state.level++; \
376 } \
Guido van Rossum004c1e11997-05-09 02:35:58 +0000377}
378
379#define POP_FAILURE(state, xcode, xtext, on_empty, on_error) \
380{ \
Guido van Rossumfaf49081997-07-15 01:47:08 +0000381 item_t *item; \
382 do \
383 { \
384 while(state.count > 0) \
385 { \
386 STACK_PREV(state.stack, item, on_error); \
387 state.start[item->reg.num] = item->reg.start; \
388 state.end[item->reg.num] = item->reg.end; \
389 state.changed[item->reg.num] = item->reg.level; \
390 state.count--; \
391 } \
392 STACK_PREV(state.stack, item, on_empty); \
393 xcode = item->fail.code; \
394 xtext = item->fail.text; \
395 state.count = item->fail.count; \
396 state.level = item->fail.level; \
397 state.point--; \
398 } \
399 while (item->fail.text == NULL); \
Guido van Rossum004c1e11997-05-09 02:35:58 +0000400}
Guido van Rossumb674c3b1992-01-19 16:32:47 +0000401
402enum regexp_compiled_ops /* opcodes for compiled regexp */
403{
Guido van Rossumfaf49081997-07-15 01:47:08 +0000404 Cend, /* end of pattern reached */
405 Cbol, /* beginning of line */
406 Ceol, /* end of line */
407 Cset, /* character set. Followed by 32 bytes of set. */
408 Cexact, /* followed by a byte to match */
409 Canychar, /* matches any character except newline */
410 Cstart_memory, /* set register start addr (followed by reg number) */
411 Cend_memory, /* set register end addr (followed by reg number) */
412 Cmatch_memory, /* match a duplicate of reg contents (regnum follows)*/
413 Cjump, /* followed by two bytes (lsb,msb) of displacement. */
414 Cstar_jump, /* will change to jump/update_failure_jump at runtime */
415 Cfailure_jump, /* jump to addr on failure */
416 Cupdate_failure_jump, /* update topmost failure point and jump */
417 Cdummy_failure_jump, /* push a dummy failure point and jump */
418 Cbegbuf, /* match at beginning of buffer */
419 Cendbuf, /* match at end of buffer */
420 Cwordbeg, /* match at beginning of word */
421 Cwordend, /* match at end of word */
422 Cwordbound, /* match if at word boundary */
423 Cnotwordbound, /* match if not at word boundary */
424 Csyntaxspec, /* matches syntax code (1 byte follows) */
425 Cnotsyntaxspec, /* matches if syntax code does not match (1 byte foll)*/
426 Crepeat1
Guido van Rossumb674c3b1992-01-19 16:32:47 +0000427};
428
429enum regexp_syntax_op /* syntax codes for plain and quoted characters */
430{
Guido van Rossumfaf49081997-07-15 01:47:08 +0000431 Rend, /* special code for end of regexp */
432 Rnormal, /* normal character */
433 Ranychar, /* any character except newline */
434 Rquote, /* the quote character */
435 Rbol, /* match beginning of line */
436 Reol, /* match end of line */
437 Roptional, /* match preceding expression optionally */
438 Rstar, /* match preceding expr zero or more times */
439 Rplus, /* match preceding expr one or more times */
440 Ror, /* match either of alternatives */
441 Ropenpar, /* opening parenthesis */
442 Rclosepar, /* closing parenthesis */
443 Rmemory, /* match memory register */
444 Rextended_memory, /* \vnn to match registers 10-99 */
445 Ropenset, /* open set. Internal syntax hard-coded below. */
446 /* the following are gnu extensions to "normal" regexp syntax */
447 Rbegbuf, /* beginning of buffer */
448 Rendbuf, /* end of buffer */
449 Rwordchar, /* word character */
450 Rnotwordchar, /* not word character */
451 Rwordbeg, /* beginning of word */
452 Rwordend, /* end of word */
453 Rwordbound, /* word bound */
454 Rnotwordbound, /* not word bound */
455 Rnum_ops
Guido van Rossumb674c3b1992-01-19 16:32:47 +0000456};
457
458static int re_compile_initialized = 0;
459static int regexp_syntax = 0;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000460int re_syntax = 0; /* Exported copy of regexp_syntax */
Guido van Rossumb674c3b1992-01-19 16:32:47 +0000461static unsigned char regexp_plain_ops[256];
462static unsigned char regexp_quoted_ops[256];
463static unsigned char regexp_precedences[Rnum_ops];
464static int regexp_context_indep_ops;
465static int regexp_ansi_sequences;
466
467#define NUM_LEVELS 5 /* number of precedence levels in use */
468#define MAX_NESTING 100 /* max nesting level of operators */
469
Guido van Rossumb674c3b1992-01-19 16:32:47 +0000470#define SYNTAX(ch) re_syntax_table[(unsigned char)(ch)]
Guido van Rossumb674c3b1992-01-19 16:32:47 +0000471
Guido van Rossum74fb3031997-07-17 22:41:38 +0000472char re_syntax_table[256];
Guido van Rossumb674c3b1992-01-19 16:32:47 +0000473
Guido van Rossum74fb3031997-07-17 22:41:38 +0000474void re_compile_initialize(void)
Guido van Rossumb674c3b1992-01-19 16:32:47 +0000475{
Guido van Rossumfaf49081997-07-15 01:47:08 +0000476 int a;
Guido van Rossumb674c3b1992-01-19 16:32:47 +0000477
Guido van Rossumfaf49081997-07-15 01:47:08 +0000478 static int syntax_table_inited = 0;
Guido van Rossum74fb3031997-07-17 22:41:38 +0000479
Guido van Rossumfaf49081997-07-15 01:47:08 +0000480 if (!syntax_table_inited)
481 {
482 syntax_table_inited = 1;
483 memset(re_syntax_table, 0, 256);
484 for (a = 'a'; a <= 'z'; a++)
485 re_syntax_table[a] = Sword;
486 for (a = 'A'; a <= 'Z'; a++)
487 re_syntax_table[a] = Sword;
488 for (a = '0'; a <= '9'; a++)
Guido van Rossum52d68321997-08-13 03:21:14 +0000489 re_syntax_table[a] = Sword | Sdigit | Shexdigit;
490 for (a = '0'; a <= '7'; a++)
491 re_syntax_table[a] |= Soctaldigit;
492 for (a = 'A'; a <= 'F'; a++)
493 re_syntax_table[a] |= Shexdigit;
494 for (a = 'a'; a <= 'f'; a++)
495 re_syntax_table[a] |= Shexdigit;
Guido van Rossum74fb3031997-07-17 22:41:38 +0000496 re_syntax_table['_'] = Sword;
497 for (a = 9; a <= 13; a++)
498 re_syntax_table[a] = Swhitespace;
499 re_syntax_table[' '] = Swhitespace;
Guido van Rossumfaf49081997-07-15 01:47:08 +0000500 }
501 re_compile_initialized = 1;
502 for (a = 0; a < 256; a++)
503 {
504 regexp_plain_ops[a] = Rnormal;
505 regexp_quoted_ops[a] = Rnormal;
506 }
507 for (a = '0'; a <= '9'; a++)
508 regexp_quoted_ops[a] = Rmemory;
509 regexp_plain_ops['\134'] = Rquote;
510 if (regexp_syntax & RE_NO_BK_PARENS)
511 {
512 regexp_plain_ops['('] = Ropenpar;
513 regexp_plain_ops[')'] = Rclosepar;
514 }
515 else
516 {
517 regexp_quoted_ops['('] = Ropenpar;
518 regexp_quoted_ops[')'] = Rclosepar;
519 }
520 if (regexp_syntax & RE_NO_BK_VBAR)
521 regexp_plain_ops['\174'] = Ror;
522 else
523 regexp_quoted_ops['\174'] = Ror;
524 regexp_plain_ops['*'] = Rstar;
525 if (regexp_syntax & RE_BK_PLUS_QM)
526 {
527 regexp_quoted_ops['+'] = Rplus;
528 regexp_quoted_ops['?'] = Roptional;
529 }
530 else
531 {
532 regexp_plain_ops['+'] = Rplus;
533 regexp_plain_ops['?'] = Roptional;
534 }
535 if (regexp_syntax & RE_NEWLINE_OR)
536 regexp_plain_ops['\n'] = Ror;
537 regexp_plain_ops['\133'] = Ropenset;
538 regexp_plain_ops['\136'] = Rbol;
539 regexp_plain_ops['$'] = Reol;
540 regexp_plain_ops['.'] = Ranychar;
541 if (!(regexp_syntax & RE_NO_GNU_EXTENSIONS))
542 {
543 regexp_quoted_ops['w'] = Rwordchar;
544 regexp_quoted_ops['W'] = Rnotwordchar;
545 regexp_quoted_ops['<'] = Rwordbeg;
546 regexp_quoted_ops['>'] = Rwordend;
547 regexp_quoted_ops['b'] = Rwordbound;
548 regexp_quoted_ops['B'] = Rnotwordbound;
549 regexp_quoted_ops['`'] = Rbegbuf;
550 regexp_quoted_ops['\''] = Rendbuf;
551 }
552 if (regexp_syntax & RE_ANSI_HEX)
553 regexp_quoted_ops['v'] = Rextended_memory;
554 for (a = 0; a < Rnum_ops; a++)
555 regexp_precedences[a] = 4;
556 if (regexp_syntax & RE_TIGHT_VBAR)
557 {
558 regexp_precedences[Ror] = 3;
559 regexp_precedences[Rbol] = 2;
560 regexp_precedences[Reol] = 2;
561 }
562 else
563 {
564 regexp_precedences[Ror] = 2;
565 regexp_precedences[Rbol] = 3;
566 regexp_precedences[Reol] = 3;
567 }
568 regexp_precedences[Rclosepar] = 1;
569 regexp_precedences[Rend] = 0;
570 regexp_context_indep_ops = (regexp_syntax & RE_CONTEXT_INDEP_OPS) != 0;
571 regexp_ansi_sequences = (regexp_syntax & RE_ANSI_HEX) != 0;
Guido van Rossumb674c3b1992-01-19 16:32:47 +0000572}
573
Guido van Rossum004c1e11997-05-09 02:35:58 +0000574int re_set_syntax(int syntax)
Guido van Rossumb674c3b1992-01-19 16:32:47 +0000575{
Guido van Rossumfaf49081997-07-15 01:47:08 +0000576 int ret;
577
578 ret = regexp_syntax;
579 regexp_syntax = syntax;
580 re_syntax = syntax; /* Exported copy */
581 re_compile_initialize();
582 return ret;
Guido van Rossumb674c3b1992-01-19 16:32:47 +0000583}
584
Guido van Rossum004c1e11997-05-09 02:35:58 +0000585static int hex_char_to_decimal(int ch)
Guido van Rossumb674c3b1992-01-19 16:32:47 +0000586{
Guido van Rossumfaf49081997-07-15 01:47:08 +0000587 if (ch >= '0' && ch <= '9')
588 return ch - '0';
589 if (ch >= 'a' && ch <= 'f')
590 return ch - 'a' + 10;
591 if (ch >= 'A' && ch <= 'F')
592 return ch - 'A' + 10;
593 return 16;
Guido van Rossumb674c3b1992-01-19 16:32:47 +0000594}
595
Guido van Rossum004c1e11997-05-09 02:35:58 +0000596static void re_compile_fastmap_aux(char *code,
597 int pos,
598 char *visited,
599 char *can_be_null,
600 char *fastmap)
Guido van Rossumb674c3b1992-01-19 16:32:47 +0000601{
Guido van Rossumfaf49081997-07-15 01:47:08 +0000602 int a;
603 int b;
604 int syntaxcode;
605
606 if (visited[pos])
607 return; /* we have already been here */
608 visited[pos] = 1;
609 for (;;)
Guido van Rossum74fb3031997-07-17 22:41:38 +0000610 switch (code[pos++]) {
Guido van Rossumfaf49081997-07-15 01:47:08 +0000611 case Cend:
Guido van Rossum74fb3031997-07-17 22:41:38 +0000612 {
613 *can_be_null = 1;
614 return;
615 }
Guido van Rossumfaf49081997-07-15 01:47:08 +0000616 case Cbol:
617 case Cbegbuf:
618 case Cendbuf:
619 case Cwordbeg:
620 case Cwordend:
621 case Cwordbound:
622 case Cnotwordbound:
623 {
624 for (a = 0; a < 256; a++)
625 fastmap[a] = 1;
626 break;
627 }
628 case Csyntaxspec:
629 {
630 syntaxcode = code[pos++];
631 for (a = 0; a < 256; a++)
632 if (SYNTAX(a) == syntaxcode)
633 fastmap[a] = 1;
634 return;
635 }
636 case Cnotsyntaxspec:
637 {
638 syntaxcode = code[pos++];
639 for (a = 0; a < 256; a++)
640 if (SYNTAX(a) != syntaxcode)
641 fastmap[a] = 1;
642 return;
643 }
644 case Ceol:
645 {
646 fastmap['\n'] = 1;
647 if (*can_be_null == 0)
648 *can_be_null = 2; /* can match null, but only at end of buffer*/
649 return;
650 }
651 case Cset:
652 {
653 for (a = 0; a < 256/8; a++)
654 if (code[pos + a] != 0)
655 for (b = 0; b < 8; b++)
656 if (code[pos + a] & (1 << b))
657 fastmap[(a << 3) + b] = 1;
658 pos += 256/8;
659 return;
660 }
661 case Cexact:
662 {
663 fastmap[(unsigned char)code[pos]] = 1;
664 return;
665 }
666 case Canychar:
667 {
668 for (a = 0; a < 256; a++)
669 if (a != '\n')
670 fastmap[a] = 1;
671 return;
672 }
673 case Cstart_memory:
674 case Cend_memory:
675 {
676 pos++;
677 break;
678 }
679 case Cmatch_memory:
680 {
681 for (a = 0; a < 256; a++)
682 fastmap[a] = 1;
683 *can_be_null = 1;
684 return;
685 }
686 case Cjump:
687 case Cdummy_failure_jump:
688 case Cupdate_failure_jump:
689 case Cstar_jump:
690 {
691 a = (unsigned char)code[pos++];
692 a |= (unsigned char)code[pos++] << 8;
693 pos += (int)SHORT(a);
694 if (visited[pos])
695 {
696 /* argh... the regexp contains empty loops. This is not
697 good, as this may cause a failure stack overflow when
698 matching. Oh well. */
699 /* this path leads nowhere; pursue other paths. */
700 return;
701 }
702 visited[pos] = 1;
703 break;
704 }
705 case Cfailure_jump:
706 {
707 a = (unsigned char)code[pos++];
708 a |= (unsigned char)code[pos++] << 8;
709 a = pos + (int)SHORT(a);
710 re_compile_fastmap_aux(code, a, visited, can_be_null, fastmap);
711 break;
712 }
713 case Crepeat1:
714 {
715 pos += 2;
716 break;
717 }
718 default:
719 {
720 abort(); /* probably some opcode is missing from this switch */
721 /*NOTREACHED*/
722 }
723 }
Guido van Rossum004c1e11997-05-09 02:35:58 +0000724}
Guido van Rossumb674c3b1992-01-19 16:32:47 +0000725
Guido van Rossum004c1e11997-05-09 02:35:58 +0000726static int re_do_compile_fastmap(char *buffer,
727 int used,
728 int pos,
729 char *can_be_null,
730 char *fastmap)
731{
Guido van Rossumfaf49081997-07-15 01:47:08 +0000732 char small_visited[512], *visited;
Guido van Rossum004c1e11997-05-09 02:35:58 +0000733
Guido van Rossumfaf49081997-07-15 01:47:08 +0000734 if (used <= sizeof(small_visited))
735 visited = small_visited;
736 else
737 {
738 visited = malloc(used);
739 if (!visited)
740 return 0;
741 }
742 *can_be_null = 0;
743 memset(fastmap, 0, 256);
744 memset(visited, 0, used);
745 re_compile_fastmap_aux(buffer, pos, visited, can_be_null, fastmap);
746 if (visited != small_visited)
747 free(visited);
748 return 1;
Guido van Rossum004c1e11997-05-09 02:35:58 +0000749}
Guido van Rossumb674c3b1992-01-19 16:32:47 +0000750
Guido van Rossum004c1e11997-05-09 02:35:58 +0000751void re_compile_fastmap(regexp_t bufp)
752{
Guido van Rossumfaf49081997-07-15 01:47:08 +0000753 if (!bufp->fastmap || bufp->fastmap_accurate)
754 return;
755 assert(bufp->used > 0);
756 if (!re_do_compile_fastmap(bufp->buffer,
757 bufp->used,
758 0,
759 &bufp->can_be_null,
760 bufp->fastmap))
761 return;
762 if (bufp->buffer[0] == Cbol)
763 bufp->anchor = 1; /* begline */
764 else
765 if (bufp->buffer[0] == Cbegbuf)
766 bufp->anchor = 2; /* begbuf */
767 else
768 bufp->anchor = 0; /* none */
769 bufp->fastmap_accurate = 1;
Guido van Rossum004c1e11997-05-09 02:35:58 +0000770}
771
772/*
773 * star is coded as:
774 * 1: failure_jump 2
775 * ... code for operand of star
776 * star_jump 1
777 * 2: ... code after star
778 *
779 * We change the star_jump to update_failure_jump if we can determine
780 * that it is safe to do so; otherwise we change it to an ordinary
781 * jump.
782 *
783 * plus is coded as
784 *
785 * jump 2
786 * 1: failure_jump 3
787 * 2: ... code for operand of plus
788 * star_jump 1
789 * 3: ... code after plus
790 *
791 * For star_jump considerations this is processed identically to star.
792 *
793 */
794
795static int re_optimize_star_jump(regexp_t bufp, char *code)
796{
Guido van Rossumfaf49081997-07-15 01:47:08 +0000797 char map[256];
798 char can_be_null;
799 char *p1;
800 char *p2;
801 char ch;
802 int a;
803 int b;
804 int num_instructions = 0;
805
806 a = (unsigned char)*code++;
807 a |= (unsigned char)*code++ << 8;
808 a = (int)SHORT(a);
809
810 p1 = code + a + 3; /* skip the failure_jump */
811 assert(p1[-3] == Cfailure_jump);
812 p2 = code;
813 /* p1 points inside loop, p2 points to after loop */
814 if (!re_do_compile_fastmap(bufp->buffer, bufp->used,
815 p2 - bufp->buffer, &can_be_null, map))
816 goto make_normal_jump;
817
818 /* If we might introduce a new update point inside the
819 * loop, we can't optimize because then update_jump would
820 * update a wrong failure point. Thus we have to be
821 * quite careful here.
822 */
823
824 /* loop until we find something that consumes a character */
Guido van Rossum004c1e11997-05-09 02:35:58 +0000825 loop_p1:
Guido van Rossumfaf49081997-07-15 01:47:08 +0000826 num_instructions++;
827 switch (*p1++)
828 {
829 case Cbol:
830 case Ceol:
831 case Cbegbuf:
832 case Cendbuf:
833 case Cwordbeg:
834 case Cwordend:
835 case Cwordbound:
836 case Cnotwordbound:
837 {
838 goto loop_p1;
839 }
840 case Cstart_memory:
841 case Cend_memory:
842 {
843 p1++;
844 goto loop_p1;
845 }
846 case Cexact:
847 {
848 ch = (unsigned char)*p1++;
849 if (map[(int)ch])
850 goto make_normal_jump;
851 break;
852 }
853 case Canychar:
854 {
855 for (b = 0; b < 256; b++)
856 if (b != '\n' && map[b])
857 goto make_normal_jump;
858 break;
859 }
860 case Cset:
861 {
862 for (b = 0; b < 256; b++)
863 if ((p1[b >> 3] & (1 << (b & 7))) && map[b])
864 goto make_normal_jump;
865 p1 += 256/8;
866 break;
867 }
868 default:
869 {
870 goto make_normal_jump;
871 }
872 }
873 /* now we know that we can't backtrack. */
874 while (p1 != p2 - 3)
875 {
876 num_instructions++;
877 switch (*p1++)
878 {
879 case Cend:
880 {
881 return 0;
882 }
883 case Cbol:
884 case Ceol:
885 case Canychar:
886 case Cbegbuf:
887 case Cendbuf:
888 case Cwordbeg:
889 case Cwordend:
890 case Cwordbound:
891 case Cnotwordbound:
892 {
893 break;
894 }
895 case Cset:
896 {
897 p1 += 256/8;
898 break;
899 }
900 case Cexact:
901 case Cstart_memory:
902 case Cend_memory:
903 case Cmatch_memory:
904 case Csyntaxspec:
905 case Cnotsyntaxspec:
906 {
907 p1++;
908 break;
909 }
910 case Cjump:
911 case Cstar_jump:
912 case Cfailure_jump:
913 case Cupdate_failure_jump:
914 case Cdummy_failure_jump:
915 {
916 goto make_normal_jump;
917 }
918 default:
919 {
920 return 0;
921 break;
922 }
923 }
924 }
925
Guido van Rossumdb25f321997-07-10 14:31:32 +0000926 make_update_jump:
Guido van Rossumfaf49081997-07-15 01:47:08 +0000927 code -= 3;
928 a += 3; /* jump to after the Cfailure_jump */
929 code[0] = Cupdate_failure_jump;
930 code[1] = a & 0xff;
931 code[2] = a >> 8;
932 if (num_instructions > 1)
933 return 1;
934 assert(num_instructions == 1);
935 /* if the only instruction matches a single character, we can do
936 * better */
937 p1 = code + 3 + a; /* start of sole instruction */
938 if (*p1 == Cset || *p1 == Cexact || *p1 == Canychar ||
939 *p1 == Csyntaxspec || *p1 == Cnotsyntaxspec)
940 code[0] = Crepeat1;
941 return 1;
942
Guido van Rossum004c1e11997-05-09 02:35:58 +0000943 make_normal_jump:
Guido van Rossumfaf49081997-07-15 01:47:08 +0000944 code -= 3;
945 *code = Cjump;
946 return 1;
Guido van Rossum004c1e11997-05-09 02:35:58 +0000947}
948
949static int re_optimize(regexp_t bufp)
950{
Guido van Rossumfaf49081997-07-15 01:47:08 +0000951 char *code;
952
953 code = bufp->buffer;
954
955 while(1)
956 {
957 switch (*code++)
958 {
959 case Cend:
960 {
961 return 1;
962 }
963 case Canychar:
964 case Cbol:
965 case Ceol:
966 case Cbegbuf:
967 case Cendbuf:
968 case Cwordbeg:
969 case Cwordend:
970 case Cwordbound:
971 case Cnotwordbound:
972 {
973 break;
974 }
975 case Cset:
976 {
977 code += 256/8;
978 break;
979 }
980 case Cexact:
981 case Cstart_memory:
982 case Cend_memory:
983 case Cmatch_memory:
984 case Csyntaxspec:
985 case Cnotsyntaxspec:
986 {
987 code++;
988 break;
989 }
990 case Cstar_jump:
991 {
992 if (!re_optimize_star_jump(bufp, code))
993 {
994 return 0;
995 }
996 /* fall through */
997 }
998 case Cupdate_failure_jump:
999 case Cjump:
1000 case Cdummy_failure_jump:
1001 case Cfailure_jump:
1002 case Crepeat1:
1003 {
1004 code += 2;
1005 break;
1006 }
1007 default:
1008 {
1009 return 0;
1010 }
1011 }
1012 }
Guido van Rossum004c1e11997-05-09 02:35:58 +00001013}
1014
1015#define NEXTCHAR(var) \
1016{ \
Guido van Rossumfaf49081997-07-15 01:47:08 +00001017 if (pos >= size) \
1018 goto ends_prematurely; \
1019 (var) = regex[pos]; \
1020 pos++; \
Guido van Rossum004c1e11997-05-09 02:35:58 +00001021}
1022
1023#define ALLOC(amount) \
1024{ \
Guido van Rossumfaf49081997-07-15 01:47:08 +00001025 if (pattern_offset+(amount) > alloc) \
1026 { \
1027 alloc += 256 + (amount); \
1028 pattern = realloc(pattern, alloc); \
1029 if (!pattern) \
1030 goto out_of_memory; \
1031 } \
Guido van Rossum004c1e11997-05-09 02:35:58 +00001032}
Guido van Rossumb674c3b1992-01-19 16:32:47 +00001033
1034#define STORE(ch) pattern[pattern_offset++] = (ch)
1035
1036#define CURRENT_LEVEL_START (starts[starts_base + current_level])
1037
1038#define SET_LEVEL_START starts[starts_base + current_level] = pattern_offset
1039
Guido van Rossum004c1e11997-05-09 02:35:58 +00001040#define PUSH_LEVEL_STARTS \
Guido van Rossumfaf49081997-07-15 01:47:08 +00001041if (starts_base < (MAX_NESTING-1)*NUM_LEVELS) \
1042 starts_base += NUM_LEVELS; \
1043else \
1044 goto too_complex \
Guido van Rossumb674c3b1992-01-19 16:32:47 +00001045
1046#define POP_LEVEL_STARTS starts_base -= NUM_LEVELS
1047
Guido van Rossum004c1e11997-05-09 02:35:58 +00001048#define PUT_ADDR(offset,addr) \
1049{ \
Guido van Rossumfaf49081997-07-15 01:47:08 +00001050 int disp = (addr) - (offset) - 2; \
1051 pattern[(offset)] = disp & 0xff; \
1052 pattern[(offset)+1] = (disp>>8) & 0xff; \
Guido van Rossum004c1e11997-05-09 02:35:58 +00001053}
Guido van Rossumb674c3b1992-01-19 16:32:47 +00001054
Guido van Rossum004c1e11997-05-09 02:35:58 +00001055#define INSERT_JUMP(pos,type,addr) \
1056{ \
Guido van Rossumfaf49081997-07-15 01:47:08 +00001057 int a, p = (pos), t = (type), ad = (addr); \
1058 for (a = pattern_offset - 1; a >= p; a--) \
1059 pattern[a + 3] = pattern[a]; \
1060 pattern[p] = t; \
1061 PUT_ADDR(p+1,ad); \
1062 pattern_offset += 3; \
Guido van Rossum004c1e11997-05-09 02:35:58 +00001063}
Guido van Rossumfaf49081997-07-15 01:47:08 +00001064
Guido van Rossumb674c3b1992-01-19 16:32:47 +00001065#define SETBIT(buf,offset,bit) (buf)[(offset)+(bit)/8] |= (1<<((bit) & 7))
1066
Guido van Rossum004c1e11997-05-09 02:35:58 +00001067#define SET_FIELDS \
1068{ \
Guido van Rossumfaf49081997-07-15 01:47:08 +00001069 bufp->allocated = alloc; \
1070 bufp->buffer = pattern; \
1071 bufp->used = pattern_offset; \
Guido van Rossum004c1e11997-05-09 02:35:58 +00001072}
Guido van Rossumb674c3b1992-01-19 16:32:47 +00001073
Guido van Rossum004c1e11997-05-09 02:35:58 +00001074#define GETHEX(var) \
1075{ \
Guido van Rossumfaf49081997-07-15 01:47:08 +00001076 char gethex_ch, gethex_value; \
1077 NEXTCHAR(gethex_ch); \
1078 gethex_value = hex_char_to_decimal(gethex_ch); \
1079 if (gethex_value == 16) \
1080 goto hex_error; \
1081 NEXTCHAR(gethex_ch); \
1082 gethex_ch = hex_char_to_decimal(gethex_ch); \
1083 if (gethex_ch == 16) \
1084 goto hex_error; \
1085 (var) = gethex_value * 16 + gethex_ch; \
Guido van Rossum004c1e11997-05-09 02:35:58 +00001086}
Guido van Rossumb674c3b1992-01-19 16:32:47 +00001087
Guido van Rossumfaf49081997-07-15 01:47:08 +00001088#define ANSI_TRANSLATE(ch) \
Guido van Rossum004c1e11997-05-09 02:35:58 +00001089{ \
Guido van Rossumfaf49081997-07-15 01:47:08 +00001090 switch (ch) \
1091 { \
1092 case 'a': \
1093 case 'A': \
1094 { \
1095 ch = 7; /* audible bell */ \
1096 break; \
1097 } \
1098 case 'b': \
1099 case 'B': \
1100 { \
1101 ch = 8; /* backspace */ \
1102 break; \
1103 } \
1104 case 'f': \
1105 case 'F': \
1106 { \
1107 ch = 12; /* form feed */ \
1108 break; \
1109 } \
1110 case 'n': \
1111 case 'N': \
1112 { \
1113 ch = 10; /* line feed */ \
1114 break; \
1115 } \
1116 case 'r': \
1117 case 'R': \
1118 { \
1119 ch = 13; /* carriage return */ \
1120 break; \
1121 } \
1122 case 't': \
1123 case 'T': \
1124 { \
1125 ch = 9; /* tab */ \
1126 break; \
1127 } \
1128 case 'v': \
1129 case 'V': \
1130 { \
1131 ch = 11; /* vertical tab */ \
1132 break; \
1133 } \
1134 case 'x': /* hex code */ \
1135 case 'X': \
1136 { \
1137 GETHEX(ch); \
1138 break; \
1139 } \
1140 default: \
1141 { \
1142 /* other characters passed through */ \
1143 if (translate) \
1144 ch = translate[(unsigned char)ch]; \
1145 break; \
1146 } \
1147 } \
Guido van Rossum004c1e11997-05-09 02:35:58 +00001148}
Guido van Rossumb674c3b1992-01-19 16:32:47 +00001149
Guido van Rossum004c1e11997-05-09 02:35:58 +00001150char *re_compile_pattern(char *regex, int size, regexp_t bufp)
1151{
Guido van Rossumfaf49081997-07-15 01:47:08 +00001152 int a;
1153 int pos;
1154 int op;
1155 int current_level;
1156 int level;
1157 int opcode;
1158 int pattern_offset = 0, alloc;
1159 int starts[NUM_LEVELS * MAX_NESTING];
1160 int starts_base;
1161 int future_jumps[MAX_NESTING];
1162 int num_jumps;
1163 unsigned char ch = '\0';
1164 char *pattern;
1165 char *translate;
1166 int next_register;
1167 int paren_depth;
1168 int num_open_registers;
1169 int open_registers[RE_NREGS];
1170 int beginning_context;
1171
1172 if (!re_compile_initialized)
1173 re_compile_initialize();
1174 bufp->used = 0;
1175 bufp->fastmap_accurate = 0;
1176 bufp->uses_registers = 1;
1177 bufp->num_registers = 1;
1178 translate = bufp->translate;
1179 pattern = bufp->buffer;
1180 alloc = bufp->allocated;
1181 if (alloc == 0 || pattern == NULL)
1182 {
1183 alloc = 256;
1184 pattern = malloc(alloc);
1185 if (!pattern)
1186 goto out_of_memory;
1187 }
1188 pattern_offset = 0;
1189 starts_base = 0;
1190 num_jumps = 0;
1191 current_level = 0;
1192 SET_LEVEL_START;
1193 num_open_registers = 0;
1194 next_register = 1;
1195 paren_depth = 0;
1196 beginning_context = 1;
1197 op = -1;
1198 /* we use Rend dummy to ensure that pending jumps are updated
1199 (due to low priority of Rend) before exiting the loop. */
1200 pos = 0;
1201 while (op != Rend)
1202 {
1203 if (pos >= size)
1204 op = Rend;
1205 else
1206 {
1207 NEXTCHAR(ch);
1208 if (translate)
1209 ch = translate[(unsigned char)ch];
1210 op = regexp_plain_ops[(unsigned char)ch];
1211 if (op == Rquote)
1212 {
1213 NEXTCHAR(ch);
1214 op = regexp_quoted_ops[(unsigned char)ch];
1215 if (op == Rnormal && regexp_ansi_sequences)
1216 ANSI_TRANSLATE(ch);
1217 }
1218 }
1219 level = regexp_precedences[op];
1220 /* printf("ch='%c' op=%d level=%d current_level=%d
1221 curlevstart=%d\n", ch, op, level, current_level,
1222 CURRENT_LEVEL_START); */
1223 if (level > current_level)
1224 {
1225 for (current_level++; current_level < level; current_level++)
1226 SET_LEVEL_START;
1227 SET_LEVEL_START;
1228 }
1229 else
1230 if (level < current_level)
1231 {
1232 current_level = level;
1233 for (;num_jumps > 0 &&
1234 future_jumps[num_jumps-1] >= CURRENT_LEVEL_START;
1235 num_jumps--)
1236 PUT_ADDR(future_jumps[num_jumps-1], pattern_offset);
1237 }
1238 switch (op)
1239 {
1240 case Rend:
1241 {
1242 break;
1243 }
1244 case Rnormal:
1245 {
1246 normal_char:
1247 opcode = Cexact;
1248 store_opcode_and_arg: /* opcode & ch must be set */
1249 SET_LEVEL_START;
1250 ALLOC(2);
1251 STORE(opcode);
1252 STORE(ch);
1253 break;
1254 }
1255 case Ranychar:
1256 {
1257 opcode = Canychar;
1258 store_opcode:
1259 SET_LEVEL_START;
1260 ALLOC(1);
1261 STORE(opcode);
1262 break;
1263 }
1264 case Rquote:
1265 {
1266 abort();
1267 /*NOTREACHED*/
1268 }
1269 case Rbol:
1270 {
1271 if (!beginning_context)
1272 if (regexp_context_indep_ops)
1273 goto op_error;
1274 else
1275 goto normal_char;
1276 opcode = Cbol;
1277 goto store_opcode;
1278 }
1279 case Reol:
1280 {
1281 if (!((pos >= size) ||
1282 ((regexp_syntax & RE_NO_BK_VBAR) ?
1283 (regex[pos] == '\174') :
1284 (pos+1 < size && regex[pos] == '\134' &&
1285 regex[pos+1] == '\174')) ||
1286 ((regexp_syntax & RE_NO_BK_PARENS)?
1287 (regex[pos] == ')'):
1288 (pos+1 < size && regex[pos] == '\134' &&
1289 regex[pos+1] == ')'))))
1290 if (regexp_context_indep_ops)
1291 goto op_error;
1292 else
1293 goto normal_char;
1294 opcode = Ceol;
1295 goto store_opcode;
1296 /* NOTREACHED */
1297 break;
1298 }
1299 case Roptional:
1300 {
1301 if (beginning_context)
1302 if (regexp_context_indep_ops)
1303 goto op_error;
1304 else
1305 goto normal_char;
1306 if (CURRENT_LEVEL_START == pattern_offset)
1307 break; /* ignore empty patterns for ? */
1308 ALLOC(3);
1309 INSERT_JUMP(CURRENT_LEVEL_START, Cfailure_jump,
1310 pattern_offset + 3);
1311 break;
1312 }
1313 case Rstar:
1314 case Rplus:
1315 {
1316 if (beginning_context)
1317 if (regexp_context_indep_ops)
1318 goto op_error;
1319 else
1320 goto normal_char;
1321 if (CURRENT_LEVEL_START == pattern_offset)
1322 break; /* ignore empty patterns for + and * */
1323 ALLOC(9);
1324 INSERT_JUMP(CURRENT_LEVEL_START, Cfailure_jump,
1325 pattern_offset + 6);
1326 INSERT_JUMP(pattern_offset, Cstar_jump, CURRENT_LEVEL_START);
1327 if (op == Rplus) /* jump over initial failure_jump */
1328 INSERT_JUMP(CURRENT_LEVEL_START, Cdummy_failure_jump,
1329 CURRENT_LEVEL_START + 6);
1330 break;
1331 }
1332 case Ror:
1333 {
1334 ALLOC(6);
1335 INSERT_JUMP(CURRENT_LEVEL_START, Cfailure_jump,
1336 pattern_offset + 6);
1337 if (num_jumps >= MAX_NESTING)
1338 goto too_complex;
1339 STORE(Cjump);
1340 future_jumps[num_jumps++] = pattern_offset;
1341 STORE(0);
1342 STORE(0);
1343 SET_LEVEL_START;
1344 break;
1345 }
1346 case Ropenpar:
1347 {
1348 SET_LEVEL_START;
1349 if (next_register < RE_NREGS)
1350 {
1351 bufp->uses_registers = 1;
1352 ALLOC(2);
1353 STORE(Cstart_memory);
1354 STORE(next_register);
1355 open_registers[num_open_registers++] = next_register;
1356 bufp->num_registers++;
1357 next_register++;
1358 }
1359 paren_depth++;
1360 PUSH_LEVEL_STARTS;
1361 current_level = 0;
1362 SET_LEVEL_START;
1363 break;
1364 }
1365 case Rclosepar:
1366 {
1367 if (paren_depth <= 0)
1368 goto parenthesis_error;
1369 POP_LEVEL_STARTS;
1370 current_level = regexp_precedences[Ropenpar];
1371 paren_depth--;
1372 if (paren_depth < num_open_registers)
1373 {
1374 bufp->uses_registers = 1;
1375 ALLOC(2);
1376 STORE(Cend_memory);
1377 num_open_registers--;
1378 STORE(open_registers[num_open_registers]);
1379 }
1380 break;
1381 }
1382 case Rmemory:
1383 {
1384 if (ch == '0')
1385 goto bad_match_register;
1386 assert(ch >= '0' && ch <= '9');
1387 bufp->uses_registers = 1;
1388 opcode = Cmatch_memory;
1389 ch -= '0';
1390 goto store_opcode_and_arg;
1391 }
1392 case Rextended_memory:
1393 {
1394 NEXTCHAR(ch);
1395 if (ch < '0' || ch > '9')
1396 goto bad_match_register;
1397 NEXTCHAR(a);
1398 if (a < '0' || a > '9')
1399 goto bad_match_register;
1400 ch = 10 * (a - '0') + ch - '0';
1401 if (ch <= 0 || ch >= RE_NREGS)
1402 goto bad_match_register;
1403 bufp->uses_registers = 1;
1404 opcode = Cmatch_memory;
1405 goto store_opcode_and_arg;
1406 }
1407 case Ropenset:
1408 {
1409 int complement;
1410 int prev;
1411 int offset;
1412 int range;
1413 int firstchar;
Guido van Rossumb674c3b1992-01-19 16:32:47 +00001414
Guido van Rossumfaf49081997-07-15 01:47:08 +00001415 SET_LEVEL_START;
1416 ALLOC(1+256/8);
1417 STORE(Cset);
1418 offset = pattern_offset;
1419 for (a = 0; a < 256/8; a++)
1420 STORE(0);
1421 NEXTCHAR(ch);
1422 if (translate)
1423 ch = translate[(unsigned char)ch];
1424 if (ch == '\136')
1425 {
1426 complement = 1;
1427 NEXTCHAR(ch);
1428 if (translate)
1429 ch = translate[(unsigned char)ch];
1430 }
1431 else
1432 complement = 0;
1433 prev = -1;
1434 range = 0;
1435 firstchar = 1;
1436 while (ch != '\135' || firstchar)
1437 {
1438 firstchar = 0;
1439 if (regexp_ansi_sequences && ch == '\134')
1440 {
1441 NEXTCHAR(ch);
1442 ANSI_TRANSLATE(ch);
1443 }
1444 if (range)
1445 {
1446 for (a = prev; a <= (int)ch; a++)
1447 SETBIT(pattern, offset, a);
1448 prev = -1;
1449 range = 0;
1450 }
1451 else
1452 if (prev != -1 && ch == '-')
1453 range = 1;
1454 else
1455 {
1456 SETBIT(pattern, offset, ch);
1457 prev = ch;
1458 }
1459 NEXTCHAR(ch);
1460 if (translate)
1461 ch = translate[(unsigned char)ch];
1462 }
1463 if (range)
1464 SETBIT(pattern, offset, '-');
1465 if (complement)
1466 {
1467 for (a = 0; a < 256/8; a++)
1468 pattern[offset+a] ^= 0xff;
1469 }
1470 break;
1471 }
1472 case Rbegbuf:
1473 {
1474 opcode = Cbegbuf;
1475 goto store_opcode;
1476 }
1477 case Rendbuf:
1478 {
1479 opcode = Cendbuf;
1480 goto store_opcode;
1481 }
1482 case Rwordchar:
1483 {
1484 opcode = Csyntaxspec;
1485 ch = Sword;
1486 goto store_opcode_and_arg;
1487 }
1488 case Rnotwordchar:
1489 {
1490 opcode = Cnotsyntaxspec;
1491 ch = Sword;
1492 goto store_opcode_and_arg;
1493 }
1494 case Rwordbeg:
1495 {
1496 opcode = Cwordbeg;
1497 goto store_opcode;
1498 }
1499 case Rwordend:
1500 {
1501 opcode = Cwordend;
1502 goto store_opcode;
1503 }
1504 case Rwordbound:
1505 {
1506 opcode = Cwordbound;
1507 goto store_opcode;
1508 }
1509 case Rnotwordbound:
1510 {
1511 opcode = Cnotwordbound;
1512 goto store_opcode;
1513 }
1514 default:
1515 {
1516 abort();
1517 }
1518 }
1519 beginning_context = (op == Ropenpar || op == Ror);
1520 }
1521 if (starts_base != 0)
1522 goto parenthesis_error;
1523 assert(num_jumps == 0);
1524 ALLOC(1);
1525 STORE(Cend);
1526 SET_FIELDS;
1527 if(!re_optimize(bufp))
1528 return "Optimization error";
1529 return NULL;
Guido van Rossumb674c3b1992-01-19 16:32:47 +00001530
Guido van Rossum004c1e11997-05-09 02:35:58 +00001531 op_error:
Guido van Rossumfaf49081997-07-15 01:47:08 +00001532 SET_FIELDS;
1533 return "Badly placed special character";
Guido van Rossumb674c3b1992-01-19 16:32:47 +00001534
Guido van Rossum004c1e11997-05-09 02:35:58 +00001535 bad_match_register:
Guido van Rossumfaf49081997-07-15 01:47:08 +00001536 SET_FIELDS;
1537 return "Bad match register number";
Guido van Rossum004c1e11997-05-09 02:35:58 +00001538
1539 hex_error:
Guido van Rossumfaf49081997-07-15 01:47:08 +00001540 SET_FIELDS;
1541 return "Bad hexadecimal number";
Guido van Rossum004c1e11997-05-09 02:35:58 +00001542
1543 parenthesis_error:
Guido van Rossumfaf49081997-07-15 01:47:08 +00001544 SET_FIELDS;
1545 return "Badly placed parenthesis";
Guido van Rossum004c1e11997-05-09 02:35:58 +00001546
1547 out_of_memory:
Guido van Rossumfaf49081997-07-15 01:47:08 +00001548 SET_FIELDS;
1549 return "Out of memory";
Guido van Rossum004c1e11997-05-09 02:35:58 +00001550
1551 ends_prematurely:
Guido van Rossumfaf49081997-07-15 01:47:08 +00001552 SET_FIELDS;
1553 return "Regular expression ends prematurely";
Guido van Rossumb674c3b1992-01-19 16:32:47 +00001554
Guido van Rossum004c1e11997-05-09 02:35:58 +00001555 too_complex:
Guido van Rossumfaf49081997-07-15 01:47:08 +00001556 SET_FIELDS;
1557 return "Regular expression too complex";
Guido van Rossumb674c3b1992-01-19 16:32:47 +00001558}
Guido van Rossum004c1e11997-05-09 02:35:58 +00001559
Guido van Rossumb674c3b1992-01-19 16:32:47 +00001560#undef CHARAT
1561#undef NEXTCHAR
1562#undef GETHEX
1563#undef ALLOC
1564#undef STORE
1565#undef CURRENT_LEVEL_START
1566#undef SET_LEVEL_START
1567#undef PUSH_LEVEL_STARTS
1568#undef POP_LEVEL_STARTS
1569#undef PUT_ADDR
1570#undef INSERT_JUMP
1571#undef SETBIT
1572#undef SET_FIELDS
1573
Guido van Rossum004c1e11997-05-09 02:35:58 +00001574#define PREFETCH if (text == textend) goto fail
1575
1576#define NEXTCHAR(var) \
1577PREFETCH; \
1578var = (unsigned char)*text++; \
1579if (translate) \
Guido van Rossumfaf49081997-07-15 01:47:08 +00001580 var = translate[var]
Guido van Rossum004c1e11997-05-09 02:35:58 +00001581
1582int re_match(regexp_t bufp,
1583 char *string,
1584 int size,
1585 int pos,
1586 regexp_registers_t old_regs)
Guido van Rossumb674c3b1992-01-19 16:32:47 +00001587{
Guido van Rossumfaf49081997-07-15 01:47:08 +00001588 char *code;
1589 char *translate;
1590 char *text;
1591 char *textstart;
1592 char *textend;
1593 int a;
1594 int b;
1595 int ch;
1596 int reg;
1597 int match_end;
1598 char *regstart;
1599 char *regend;
1600 int regsize;
1601 match_state state;
Guido van Rossum004c1e11997-05-09 02:35:58 +00001602
Guido van Rossumfaf49081997-07-15 01:47:08 +00001603 assert(pos >= 0 && size >= 0);
1604 assert(pos <= size);
Guido van Rossum004c1e11997-05-09 02:35:58 +00001605
Guido van Rossumfaf49081997-07-15 01:47:08 +00001606 text = string + pos;
1607 textstart = string;
1608 textend = string + size;
Guido van Rossum004c1e11997-05-09 02:35:58 +00001609
Guido van Rossumfaf49081997-07-15 01:47:08 +00001610 code = bufp->buffer;
Guido van Rossum004c1e11997-05-09 02:35:58 +00001611
Guido van Rossumfaf49081997-07-15 01:47:08 +00001612 translate = bufp->translate;
Guido van Rossum004c1e11997-05-09 02:35:58 +00001613
Guido van Rossumfaf49081997-07-15 01:47:08 +00001614 NEW_STATE(state, bufp->num_registers);
1615
Guido van Rossum004c1e11997-05-09 02:35:58 +00001616 continue_matching:
Guido van Rossumfaf49081997-07-15 01:47:08 +00001617 switch (*code++)
Guido van Rossumb674c3b1992-01-19 16:32:47 +00001618 {
Guido van Rossumfaf49081997-07-15 01:47:08 +00001619 case Cend:
Guido van Rossum004c1e11997-05-09 02:35:58 +00001620 {
Guido van Rossumfaf49081997-07-15 01:47:08 +00001621 match_end = text - textstart;
1622 if (old_regs)
1623 {
1624 old_regs->start[0] = pos;
1625 old_regs->end[0] = match_end;
1626 if (!bufp->uses_registers)
1627 {
1628 for (a = 1; a < RE_NREGS; a++)
1629 {
1630 old_regs->start[a] = -1;
1631 old_regs->end[a] = -1;
1632 }
1633 }
1634 else
1635 {
1636 for (a = 1; a < bufp->num_registers; a++)
1637 {
1638 if ((GET_REG_START(state, a) == NULL) ||
1639 (GET_REG_END(state, a) == NULL))
1640 {
1641 old_regs->start[a] = -1;
1642 old_regs->end[a] = -1;
1643 continue;
1644 }
1645 old_regs->start[a] = GET_REG_START(state, a) - textstart;
1646 old_regs->end[a] = GET_REG_END(state, a) - textstart;
1647 }
1648 for (; a < RE_NREGS; a++)
1649 {
1650 old_regs->start[a] = -1;
1651 old_regs->end[a] = -1;
1652 }
1653 }
1654 }
1655 FREE_STATE(state);
1656 return match_end - pos;
Guido van Rossum004c1e11997-05-09 02:35:58 +00001657 }
Guido van Rossumfaf49081997-07-15 01:47:08 +00001658 case Cbol:
1659 {
1660 if (text == textstart || text[-1] == '\n')
1661 goto continue_matching;
1662 goto fail;
1663 }
1664 case Ceol:
1665 {
1666 if (text == textend || *text == '\n')
1667 goto continue_matching;
1668 goto fail;
1669 }
1670 case Cset:
1671 {
1672 NEXTCHAR(ch);
1673 if (code[ch/8] & (1<<(ch & 7)))
1674 {
1675 code += 256/8;
1676 goto continue_matching;
1677 }
1678 goto fail;
1679 }
1680 case Cexact:
1681 {
1682 NEXTCHAR(ch);
1683 if (ch != (unsigned char)*code++)
1684 goto fail;
1685 goto continue_matching;
1686 }
1687 case Canychar:
1688 {
1689 NEXTCHAR(ch);
1690 if (ch == '\n')
1691 goto fail;
1692 goto continue_matching;
1693 }
1694 case Cstart_memory:
1695 {
1696 reg = *code++;
1697 SET_REG_START(state, reg, text, goto error);
1698 goto continue_matching;
1699 }
1700 case Cend_memory:
1701 {
1702 reg = *code++;
1703 SET_REG_END(state, reg, text, goto error);
1704 goto continue_matching;
1705 }
1706 case Cmatch_memory:
1707 {
1708 reg = *code++;
1709 regstart = GET_REG_START(state, reg);
1710 regend = GET_REG_END(state, reg);
1711 if ((regstart == NULL) || (regend == NULL))
1712 goto fail; /* or should we just match nothing? */
1713 regsize = regend - regstart;
Guido van Rossum004c1e11997-05-09 02:35:58 +00001714
Guido van Rossumfaf49081997-07-15 01:47:08 +00001715 if (regsize > (textend - text))
1716 goto fail;
1717 if(translate)
1718 {
1719 for (; regstart < regend; regstart++, text++)
1720 if (translate[*regstart] != translate[*text])
1721 goto fail;
1722 }
1723 else
1724 for (; regstart < regend; regstart++, text++)
1725 if (*regstart != *text)
1726 goto fail;
1727 goto continue_matching;
Guido van Rossum004c1e11997-05-09 02:35:58 +00001728 }
Guido van Rossumfaf49081997-07-15 01:47:08 +00001729 case Cupdate_failure_jump:
Guido van Rossumdb25f321997-07-10 14:31:32 +00001730 {
Guido van Rossumfaf49081997-07-15 01:47:08 +00001731 UPDATE_FAILURE(state, text, goto error);
1732 /* fall to next case */
Guido van Rossumdb25f321997-07-10 14:31:32 +00001733 }
Guido van Rossumfaf49081997-07-15 01:47:08 +00001734 /* treat Cstar_jump just like Cjump if it hasn't been optimized */
1735 case Cstar_jump:
1736 case Cjump:
1737 {
1738 a = (unsigned char)*code++;
1739 a |= (unsigned char)*code++ << 8;
1740 code += (int)SHORT(a);
1741 goto continue_matching;
1742 }
1743 case Cdummy_failure_jump:
1744 {
1745 a = (unsigned char)*code++;
1746 a |= (unsigned char)*code++ << 8;
1747 a = (int)SHORT(a);
1748 assert(*code == Cfailure_jump);
1749 b = (unsigned char)code[1];
1750 b |= (unsigned char)code[2] << 8;
1751 PUSH_FAILURE(state, code + (int)SHORT(b) + 3, NULL, goto error);
1752 code += a;
1753 goto continue_matching;
1754 }
1755 case Cfailure_jump:
1756 {
1757 a = (unsigned char)*code++;
1758 a |= (unsigned char)*code++ << 8;
1759 a = (int)SHORT(a);
1760 PUSH_FAILURE(state, code + a, text, goto error);
1761 goto continue_matching;
1762 }
1763 case Crepeat1:
1764 {
1765 char *pinst;
1766 a = (unsigned char)*code++;
1767 a |= (unsigned char)*code++ << 8;
1768 a = (int)SHORT(a);
1769 pinst = code + a;
1770 /* pinst is sole instruction in loop, and it matches a
1771 * single character. Since Crepeat1 was originally a
1772 * Cupdate_failure_jump, we also know that backtracking
1773 * is useless: so long as the single-character
1774 * expression matches, it must be used. Also, in the
1775 * case of +, we've already matched one character, so +
1776 * can't fail: nothing here can cause a failure. */
1777 switch (*pinst++)
1778 {
1779 case Cset:
1780 {
1781 if (translate)
1782 {
1783 while (text < textend)
1784 {
1785 ch = translate[(unsigned char)*text];
1786 if (pinst[ch/8] & (1<<(ch & 7)))
1787 text++;
1788 else
1789 break;
1790 }
1791 }
1792 else
1793 {
1794 while (text < textend)
1795 {
1796 ch = (unsigned char)*text;
1797 if (pinst[ch/8] & (1<<(ch & 7)))
1798 text++;
1799 else
1800 break;
1801 }
1802 }
1803 break;
1804 }
1805 case Cexact:
1806 {
1807 ch = (unsigned char)*pinst;
1808 if (translate)
1809 {
1810 while (text < textend &&
1811 translate[(unsigned char)*text] == ch)
1812 text++;
1813 }
1814 else
1815 {
1816 while (text < textend && (unsigned char)*text == ch)
1817 text++;
1818 }
1819 break;
1820 }
1821 case Canychar:
1822 {
1823 while (text < textend && (unsigned char)*text != '\n')
1824 text++;
1825 break;
1826 }
1827 case Csyntaxspec:
1828 {
1829 a = (unsigned char)*pinst;
1830 if (translate)
1831 {
1832 while (text < textend &&
1833 translate[SYNTAX(*text)] == a)
1834 text++;
1835 }
1836 else
1837 {
1838 while (text < textend && SYNTAX(*text) == a)
1839 text++;
1840 }
1841 break;
1842 }
1843 case Cnotsyntaxspec:
1844 {
1845 a = (unsigned char)*pinst;
1846 if (translate)
1847 {
1848 while (text < textend &&
1849 translate[SYNTAX(*text)] != a)
1850 text++;
1851 }
1852 else
1853 {
1854 while (text < textend && SYNTAX(*text) != a)
1855 text++;
1856 }
1857 break;
1858 }
1859 default:
1860 {
1861 abort();
1862 /*NOTREACHED*/
1863 }
1864 }
1865 /* due to the funky way + and * are compiled, the top
1866 * failure- stack entry at this point is actually a
1867 * success entry -- update it & pop it */
1868 UPDATE_FAILURE(state, text, goto error);
1869 goto fail; /* i.e., succeed <wink/sigh> */
1870 }
1871 case Cbegbuf:
1872 {
1873 if (text == textstart)
1874 goto continue_matching;
1875 goto fail;
1876 }
1877 case Cendbuf:
1878 {
1879 if (text == textend)
1880 goto continue_matching;
1881 goto fail;
1882 }
1883 case Cwordbeg:
1884 {
1885 if (text == textend)
1886 goto fail;
Guido van Rossum74fb3031997-07-17 22:41:38 +00001887 if (SYNTAX(*text) & Sword)
Guido van Rossumfaf49081997-07-15 01:47:08 +00001888 goto fail;
1889 if (text == textstart)
1890 goto continue_matching;
Guido van Rossum74fb3031997-07-17 22:41:38 +00001891 if (!(SYNTAX(text[-1]) & Sword))
Guido van Rossumfaf49081997-07-15 01:47:08 +00001892 goto continue_matching;
1893 goto fail;
1894 }
1895 case Cwordend:
1896 {
1897 if (text == textstart)
1898 goto fail;
Guido van Rossum74fb3031997-07-17 22:41:38 +00001899 if (!(SYNTAX(text[-1]) & Sword))
Guido van Rossumfaf49081997-07-15 01:47:08 +00001900 goto fail;
1901 if (text == textend)
1902 goto continue_matching;
Guido van Rossum74fb3031997-07-17 22:41:38 +00001903 if (SYNTAX(*text) & Sword)
Guido van Rossumfaf49081997-07-15 01:47:08 +00001904 goto fail;
1905 goto continue_matching;
1906 }
1907 case Cwordbound:
1908 {
1909 /* Note: as in gnu regexp, this also matches at the
1910 * beginning and end of buffer. */
Guido van Rossum004c1e11997-05-09 02:35:58 +00001911
Guido van Rossumfaf49081997-07-15 01:47:08 +00001912 if (text == textstart || text == textend)
1913 goto continue_matching;
Guido van Rossum74fb3031997-07-17 22:41:38 +00001914 if ((SYNTAX(text[-1]) & Sword) ^ (SYNTAX(*text) & Sword))
Guido van Rossumfaf49081997-07-15 01:47:08 +00001915 goto continue_matching;
1916 goto fail;
1917 }
1918 case Cnotwordbound:
1919 {
1920 /* Note: as in gnu regexp, this never matches at the
1921 * beginning and end of buffer. */
1922 if (text == textstart || text == textend)
1923 goto fail;
Guido van Rossum74fb3031997-07-17 22:41:38 +00001924 if (!((SYNTAX(text[-1]) & Sword) ^ (SYNTAX(*text) & Sword)))
Guido van Rossumfaf49081997-07-15 01:47:08 +00001925 goto fail;
1926 goto continue_matching;
1927 }
1928 case Csyntaxspec:
1929 {
1930 NEXTCHAR(ch);
Guido van Rossum74fb3031997-07-17 22:41:38 +00001931 if (!(SYNTAX(ch) & (unsigned char)*code++))
Guido van Rossumfaf49081997-07-15 01:47:08 +00001932 goto fail;
1933 goto continue_matching;
1934 }
1935 case Cnotsyntaxspec:
1936 {
1937 NEXTCHAR(ch);
Guido van Rossum74fb3031997-07-17 22:41:38 +00001938 if (SYNTAX(ch) & (unsigned char)*code++)
Guido van Rossumfaf49081997-07-15 01:47:08 +00001939 break;
1940 goto continue_matching;
1941 }
1942 default:
1943 {
1944 abort();
1945 /*NOTREACHED*/
1946 }
1947 }
Guido van Rossum004c1e11997-05-09 02:35:58 +00001948
Guido van Rossum3b1a57a1992-01-27 16:47:46 +00001949#if 0 /* This line is never reached --Guido */
Guido van Rossumfaf49081997-07-15 01:47:08 +00001950 abort();
Guido van Rossum5f21dd11992-01-19 16:49:14 +00001951#endif
Guido van Rossumfaf49081997-07-15 01:47:08 +00001952 /*
1953 *NOTREACHED
1954 */
Guido van Rossum004c1e11997-05-09 02:35:58 +00001955
1956 fail:
Guido van Rossumfaf49081997-07-15 01:47:08 +00001957 POP_FAILURE(state, code, text, goto done_matching, goto error);
1958 goto continue_matching;
Guido van Rossum004c1e11997-05-09 02:35:58 +00001959
1960 done_matching:
1961/* if(translated != NULL) */
1962/* free(translated); */
Guido van Rossumfaf49081997-07-15 01:47:08 +00001963 FREE_STATE(state);
1964 return -1;
Guido van Rossumb674c3b1992-01-19 16:32:47 +00001965
Guido van Rossum004c1e11997-05-09 02:35:58 +00001966 error:
1967/* if (translated != NULL) */
1968/* free(translated); */
Guido van Rossumfaf49081997-07-15 01:47:08 +00001969 FREE_STATE(state);
1970 return -2;
Guido van Rossumb674c3b1992-01-19 16:32:47 +00001971}
1972
1973#undef PREFETCH
1974#undef NEXTCHAR
Guido van Rossumb674c3b1992-01-19 16:32:47 +00001975
Guido van Rossum004c1e11997-05-09 02:35:58 +00001976int re_search(regexp_t bufp,
1977 char *string,
1978 int size,
1979 int pos,
1980 int range,
1981 regexp_registers_t regs)
Guido van Rossumb674c3b1992-01-19 16:32:47 +00001982{
Guido van Rossumfaf49081997-07-15 01:47:08 +00001983 char *fastmap;
1984 char *translate;
1985 char *text;
1986 char *partstart;
1987 char *partend;
1988 int dir;
1989 int ret;
1990 char anchor;
Guido van Rossumb674c3b1992-01-19 16:32:47 +00001991
Guido van Rossumfaf49081997-07-15 01:47:08 +00001992 assert(size >= 0 && pos >= 0);
1993 assert(pos + range >= 0 && pos + range <= size); /* Bugfix by ylo */
Guido van Rossumb674c3b1992-01-19 16:32:47 +00001994
Guido van Rossumfaf49081997-07-15 01:47:08 +00001995 fastmap = bufp->fastmap;
1996 translate = bufp->translate;
1997 if (fastmap && !bufp->fastmap_accurate)
1998 re_compile_fastmap(bufp);
1999 anchor = bufp->anchor;
2000 if (bufp->can_be_null == 1) /* can_be_null == 2: can match null at eob */
2001 fastmap = NULL;
Guido van Rossum004c1e11997-05-09 02:35:58 +00002002
Guido van Rossumfaf49081997-07-15 01:47:08 +00002003 if (range < 0)
2004 {
2005 dir = -1;
2006 range = -range;
Guido van Rossumb674c3b1992-01-19 16:32:47 +00002007 }
Guido van Rossum004c1e11997-05-09 02:35:58 +00002008 else
Guido van Rossumfaf49081997-07-15 01:47:08 +00002009 dir = 1;
2010
2011 if (anchor == 2)
2012 if (pos != 0)
2013 return -1;
2014 else
2015 range = 0;
2016
2017 for (; range >= 0; range--, pos += dir)
2018 {
2019 if (fastmap)
2020 {
2021 if (dir == 1)
2022 { /* searching forwards */
2023
2024 text = string + pos;
2025 partend = string + size;
2026 partstart = text;
2027 if (translate)
2028 while (text != partend &&
2029 !fastmap[(unsigned char) translate[(unsigned char)*text]])
2030 text++;
2031 else
2032 while (text != partend && !fastmap[(unsigned char)*text])
2033 text++;
2034 pos += text - partstart;
2035 range -= text - partstart;
2036 if (pos == size && bufp->can_be_null == 0)
2037 return -1;
2038 }
2039 else
2040 { /* searching backwards */
2041 text = string + pos;
2042 partstart = string + pos - range;
2043 partend = text;
2044 if (translate)
2045 while (text != partstart &&
2046 !fastmap[(unsigned char)
2047 translate[(unsigned char)*text]])
2048 text--;
2049 else
2050 while (text != partstart &&
2051 !fastmap[(unsigned char)*text])
2052 text--;
2053 pos -= partend - text;
2054 range -= partend - text;
2055 }
2056 }
2057 if (anchor == 1)
2058 { /* anchored to begline */
2059 if (pos > 0 && (string[pos - 1] != '\n'))
2060 continue;
2061 }
2062 assert(pos >= 0 && pos <= size);
2063 ret = re_match(bufp, string, size, pos, regs);
2064 if (ret >= 0)
2065 return pos;
2066 if (ret == -2)
2067 return -2;
Guido van Rossumb674c3b1992-01-19 16:32:47 +00002068 }
Guido van Rossumfaf49081997-07-15 01:47:08 +00002069 return -1;
Guido van Rossumb674c3b1992-01-19 16:32:47 +00002070}
Guido van Rossum74fb3031997-07-17 22:41:38 +00002071
2072/*
2073** Local Variables:
2074** mode: c
2075** c-file-style: "python"
2076** End:
2077*/