blob: 2d30171f421a511a473eb5e8c78ad85d3f7c7656 [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 Rossum74fb3031997-07-17 22:41:38 +0000489 re_syntax_table[a] = Sword | Sdigit;
490 re_syntax_table['_'] = Sword;
491 for (a = 9; a <= 13; a++)
492 re_syntax_table[a] = Swhitespace;
493 re_syntax_table[' '] = Swhitespace;
Guido van Rossumfaf49081997-07-15 01:47:08 +0000494 }
495 re_compile_initialized = 1;
496 for (a = 0; a < 256; a++)
497 {
498 regexp_plain_ops[a] = Rnormal;
499 regexp_quoted_ops[a] = Rnormal;
500 }
501 for (a = '0'; a <= '9'; a++)
502 regexp_quoted_ops[a] = Rmemory;
503 regexp_plain_ops['\134'] = Rquote;
504 if (regexp_syntax & RE_NO_BK_PARENS)
505 {
506 regexp_plain_ops['('] = Ropenpar;
507 regexp_plain_ops[')'] = Rclosepar;
508 }
509 else
510 {
511 regexp_quoted_ops['('] = Ropenpar;
512 regexp_quoted_ops[')'] = Rclosepar;
513 }
514 if (regexp_syntax & RE_NO_BK_VBAR)
515 regexp_plain_ops['\174'] = Ror;
516 else
517 regexp_quoted_ops['\174'] = Ror;
518 regexp_plain_ops['*'] = Rstar;
519 if (regexp_syntax & RE_BK_PLUS_QM)
520 {
521 regexp_quoted_ops['+'] = Rplus;
522 regexp_quoted_ops['?'] = Roptional;
523 }
524 else
525 {
526 regexp_plain_ops['+'] = Rplus;
527 regexp_plain_ops['?'] = Roptional;
528 }
529 if (regexp_syntax & RE_NEWLINE_OR)
530 regexp_plain_ops['\n'] = Ror;
531 regexp_plain_ops['\133'] = Ropenset;
532 regexp_plain_ops['\136'] = Rbol;
533 regexp_plain_ops['$'] = Reol;
534 regexp_plain_ops['.'] = Ranychar;
535 if (!(regexp_syntax & RE_NO_GNU_EXTENSIONS))
536 {
537 regexp_quoted_ops['w'] = Rwordchar;
538 regexp_quoted_ops['W'] = Rnotwordchar;
539 regexp_quoted_ops['<'] = Rwordbeg;
540 regexp_quoted_ops['>'] = Rwordend;
541 regexp_quoted_ops['b'] = Rwordbound;
542 regexp_quoted_ops['B'] = Rnotwordbound;
543 regexp_quoted_ops['`'] = Rbegbuf;
544 regexp_quoted_ops['\''] = Rendbuf;
545 }
546 if (regexp_syntax & RE_ANSI_HEX)
547 regexp_quoted_ops['v'] = Rextended_memory;
548 for (a = 0; a < Rnum_ops; a++)
549 regexp_precedences[a] = 4;
550 if (regexp_syntax & RE_TIGHT_VBAR)
551 {
552 regexp_precedences[Ror] = 3;
553 regexp_precedences[Rbol] = 2;
554 regexp_precedences[Reol] = 2;
555 }
556 else
557 {
558 regexp_precedences[Ror] = 2;
559 regexp_precedences[Rbol] = 3;
560 regexp_precedences[Reol] = 3;
561 }
562 regexp_precedences[Rclosepar] = 1;
563 regexp_precedences[Rend] = 0;
564 regexp_context_indep_ops = (regexp_syntax & RE_CONTEXT_INDEP_OPS) != 0;
565 regexp_ansi_sequences = (regexp_syntax & RE_ANSI_HEX) != 0;
Guido van Rossumb674c3b1992-01-19 16:32:47 +0000566}
567
Guido van Rossum004c1e11997-05-09 02:35:58 +0000568int re_set_syntax(int syntax)
Guido van Rossumb674c3b1992-01-19 16:32:47 +0000569{
Guido van Rossumfaf49081997-07-15 01:47:08 +0000570 int ret;
571
572 ret = regexp_syntax;
573 regexp_syntax = syntax;
574 re_syntax = syntax; /* Exported copy */
575 re_compile_initialize();
576 return ret;
Guido van Rossumb674c3b1992-01-19 16:32:47 +0000577}
578
Guido van Rossum004c1e11997-05-09 02:35:58 +0000579static int hex_char_to_decimal(int ch)
Guido van Rossumb674c3b1992-01-19 16:32:47 +0000580{
Guido van Rossumfaf49081997-07-15 01:47:08 +0000581 if (ch >= '0' && ch <= '9')
582 return ch - '0';
583 if (ch >= 'a' && ch <= 'f')
584 return ch - 'a' + 10;
585 if (ch >= 'A' && ch <= 'F')
586 return ch - 'A' + 10;
587 return 16;
Guido van Rossumb674c3b1992-01-19 16:32:47 +0000588}
589
Guido van Rossum004c1e11997-05-09 02:35:58 +0000590static void re_compile_fastmap_aux(char *code,
591 int pos,
592 char *visited,
593 char *can_be_null,
594 char *fastmap)
Guido van Rossumb674c3b1992-01-19 16:32:47 +0000595{
Guido van Rossumfaf49081997-07-15 01:47:08 +0000596 int a;
597 int b;
598 int syntaxcode;
599
600 if (visited[pos])
601 return; /* we have already been here */
602 visited[pos] = 1;
603 for (;;)
Guido van Rossum74fb3031997-07-17 22:41:38 +0000604 switch (code[pos++]) {
Guido van Rossumfaf49081997-07-15 01:47:08 +0000605 case Cend:
Guido van Rossum74fb3031997-07-17 22:41:38 +0000606 {
607 *can_be_null = 1;
608 return;
609 }
Guido van Rossumfaf49081997-07-15 01:47:08 +0000610 case Cbol:
611 case Cbegbuf:
612 case Cendbuf:
613 case Cwordbeg:
614 case Cwordend:
615 case Cwordbound:
616 case Cnotwordbound:
617 {
618 for (a = 0; a < 256; a++)
619 fastmap[a] = 1;
620 break;
621 }
622 case Csyntaxspec:
623 {
624 syntaxcode = code[pos++];
625 for (a = 0; a < 256; a++)
626 if (SYNTAX(a) == syntaxcode)
627 fastmap[a] = 1;
628 return;
629 }
630 case Cnotsyntaxspec:
631 {
632 syntaxcode = code[pos++];
633 for (a = 0; a < 256; a++)
634 if (SYNTAX(a) != syntaxcode)
635 fastmap[a] = 1;
636 return;
637 }
638 case Ceol:
639 {
640 fastmap['\n'] = 1;
641 if (*can_be_null == 0)
642 *can_be_null = 2; /* can match null, but only at end of buffer*/
643 return;
644 }
645 case Cset:
646 {
647 for (a = 0; a < 256/8; a++)
648 if (code[pos + a] != 0)
649 for (b = 0; b < 8; b++)
650 if (code[pos + a] & (1 << b))
651 fastmap[(a << 3) + b] = 1;
652 pos += 256/8;
653 return;
654 }
655 case Cexact:
656 {
657 fastmap[(unsigned char)code[pos]] = 1;
658 return;
659 }
660 case Canychar:
661 {
662 for (a = 0; a < 256; a++)
663 if (a != '\n')
664 fastmap[a] = 1;
665 return;
666 }
667 case Cstart_memory:
668 case Cend_memory:
669 {
670 pos++;
671 break;
672 }
673 case Cmatch_memory:
674 {
675 for (a = 0; a < 256; a++)
676 fastmap[a] = 1;
677 *can_be_null = 1;
678 return;
679 }
680 case Cjump:
681 case Cdummy_failure_jump:
682 case Cupdate_failure_jump:
683 case Cstar_jump:
684 {
685 a = (unsigned char)code[pos++];
686 a |= (unsigned char)code[pos++] << 8;
687 pos += (int)SHORT(a);
688 if (visited[pos])
689 {
690 /* argh... the regexp contains empty loops. This is not
691 good, as this may cause a failure stack overflow when
692 matching. Oh well. */
693 /* this path leads nowhere; pursue other paths. */
694 return;
695 }
696 visited[pos] = 1;
697 break;
698 }
699 case Cfailure_jump:
700 {
701 a = (unsigned char)code[pos++];
702 a |= (unsigned char)code[pos++] << 8;
703 a = pos + (int)SHORT(a);
704 re_compile_fastmap_aux(code, a, visited, can_be_null, fastmap);
705 break;
706 }
707 case Crepeat1:
708 {
709 pos += 2;
710 break;
711 }
712 default:
713 {
714 abort(); /* probably some opcode is missing from this switch */
715 /*NOTREACHED*/
716 }
717 }
Guido van Rossum004c1e11997-05-09 02:35:58 +0000718}
Guido van Rossumb674c3b1992-01-19 16:32:47 +0000719
Guido van Rossum004c1e11997-05-09 02:35:58 +0000720static int re_do_compile_fastmap(char *buffer,
721 int used,
722 int pos,
723 char *can_be_null,
724 char *fastmap)
725{
Guido van Rossumfaf49081997-07-15 01:47:08 +0000726 char small_visited[512], *visited;
Guido van Rossum004c1e11997-05-09 02:35:58 +0000727
Guido van Rossumfaf49081997-07-15 01:47:08 +0000728 if (used <= sizeof(small_visited))
729 visited = small_visited;
730 else
731 {
732 visited = malloc(used);
733 if (!visited)
734 return 0;
735 }
736 *can_be_null = 0;
737 memset(fastmap, 0, 256);
738 memset(visited, 0, used);
739 re_compile_fastmap_aux(buffer, pos, visited, can_be_null, fastmap);
740 if (visited != small_visited)
741 free(visited);
742 return 1;
Guido van Rossum004c1e11997-05-09 02:35:58 +0000743}
Guido van Rossumb674c3b1992-01-19 16:32:47 +0000744
Guido van Rossum004c1e11997-05-09 02:35:58 +0000745void re_compile_fastmap(regexp_t bufp)
746{
Guido van Rossumfaf49081997-07-15 01:47:08 +0000747 if (!bufp->fastmap || bufp->fastmap_accurate)
748 return;
749 assert(bufp->used > 0);
750 if (!re_do_compile_fastmap(bufp->buffer,
751 bufp->used,
752 0,
753 &bufp->can_be_null,
754 bufp->fastmap))
755 return;
756 if (bufp->buffer[0] == Cbol)
757 bufp->anchor = 1; /* begline */
758 else
759 if (bufp->buffer[0] == Cbegbuf)
760 bufp->anchor = 2; /* begbuf */
761 else
762 bufp->anchor = 0; /* none */
763 bufp->fastmap_accurate = 1;
Guido van Rossum004c1e11997-05-09 02:35:58 +0000764}
765
766/*
767 * star is coded as:
768 * 1: failure_jump 2
769 * ... code for operand of star
770 * star_jump 1
771 * 2: ... code after star
772 *
773 * We change the star_jump to update_failure_jump if we can determine
774 * that it is safe to do so; otherwise we change it to an ordinary
775 * jump.
776 *
777 * plus is coded as
778 *
779 * jump 2
780 * 1: failure_jump 3
781 * 2: ... code for operand of plus
782 * star_jump 1
783 * 3: ... code after plus
784 *
785 * For star_jump considerations this is processed identically to star.
786 *
787 */
788
789static int re_optimize_star_jump(regexp_t bufp, char *code)
790{
Guido van Rossumfaf49081997-07-15 01:47:08 +0000791 char map[256];
792 char can_be_null;
793 char *p1;
794 char *p2;
795 char ch;
796 int a;
797 int b;
798 int num_instructions = 0;
799
800 a = (unsigned char)*code++;
801 a |= (unsigned char)*code++ << 8;
802 a = (int)SHORT(a);
803
804 p1 = code + a + 3; /* skip the failure_jump */
805 assert(p1[-3] == Cfailure_jump);
806 p2 = code;
807 /* p1 points inside loop, p2 points to after loop */
808 if (!re_do_compile_fastmap(bufp->buffer, bufp->used,
809 p2 - bufp->buffer, &can_be_null, map))
810 goto make_normal_jump;
811
812 /* If we might introduce a new update point inside the
813 * loop, we can't optimize because then update_jump would
814 * update a wrong failure point. Thus we have to be
815 * quite careful here.
816 */
817
818 /* loop until we find something that consumes a character */
Guido van Rossum004c1e11997-05-09 02:35:58 +0000819 loop_p1:
Guido van Rossumfaf49081997-07-15 01:47:08 +0000820 num_instructions++;
821 switch (*p1++)
822 {
823 case Cbol:
824 case Ceol:
825 case Cbegbuf:
826 case Cendbuf:
827 case Cwordbeg:
828 case Cwordend:
829 case Cwordbound:
830 case Cnotwordbound:
831 {
832 goto loop_p1;
833 }
834 case Cstart_memory:
835 case Cend_memory:
836 {
837 p1++;
838 goto loop_p1;
839 }
840 case Cexact:
841 {
842 ch = (unsigned char)*p1++;
843 if (map[(int)ch])
844 goto make_normal_jump;
845 break;
846 }
847 case Canychar:
848 {
849 for (b = 0; b < 256; b++)
850 if (b != '\n' && map[b])
851 goto make_normal_jump;
852 break;
853 }
854 case Cset:
855 {
856 for (b = 0; b < 256; b++)
857 if ((p1[b >> 3] & (1 << (b & 7))) && map[b])
858 goto make_normal_jump;
859 p1 += 256/8;
860 break;
861 }
862 default:
863 {
864 goto make_normal_jump;
865 }
866 }
867 /* now we know that we can't backtrack. */
868 while (p1 != p2 - 3)
869 {
870 num_instructions++;
871 switch (*p1++)
872 {
873 case Cend:
874 {
875 return 0;
876 }
877 case Cbol:
878 case Ceol:
879 case Canychar:
880 case Cbegbuf:
881 case Cendbuf:
882 case Cwordbeg:
883 case Cwordend:
884 case Cwordbound:
885 case Cnotwordbound:
886 {
887 break;
888 }
889 case Cset:
890 {
891 p1 += 256/8;
892 break;
893 }
894 case Cexact:
895 case Cstart_memory:
896 case Cend_memory:
897 case Cmatch_memory:
898 case Csyntaxspec:
899 case Cnotsyntaxspec:
900 {
901 p1++;
902 break;
903 }
904 case Cjump:
905 case Cstar_jump:
906 case Cfailure_jump:
907 case Cupdate_failure_jump:
908 case Cdummy_failure_jump:
909 {
910 goto make_normal_jump;
911 }
912 default:
913 {
914 return 0;
915 break;
916 }
917 }
918 }
919
Guido van Rossumdb25f321997-07-10 14:31:32 +0000920 make_update_jump:
Guido van Rossumfaf49081997-07-15 01:47:08 +0000921 code -= 3;
922 a += 3; /* jump to after the Cfailure_jump */
923 code[0] = Cupdate_failure_jump;
924 code[1] = a & 0xff;
925 code[2] = a >> 8;
926 if (num_instructions > 1)
927 return 1;
928 assert(num_instructions == 1);
929 /* if the only instruction matches a single character, we can do
930 * better */
931 p1 = code + 3 + a; /* start of sole instruction */
932 if (*p1 == Cset || *p1 == Cexact || *p1 == Canychar ||
933 *p1 == Csyntaxspec || *p1 == Cnotsyntaxspec)
934 code[0] = Crepeat1;
935 return 1;
936
Guido van Rossum004c1e11997-05-09 02:35:58 +0000937 make_normal_jump:
Guido van Rossumfaf49081997-07-15 01:47:08 +0000938 code -= 3;
939 *code = Cjump;
940 return 1;
Guido van Rossum004c1e11997-05-09 02:35:58 +0000941}
942
943static int re_optimize(regexp_t bufp)
944{
Guido van Rossumfaf49081997-07-15 01:47:08 +0000945 char *code;
946
947 code = bufp->buffer;
948
949 while(1)
950 {
951 switch (*code++)
952 {
953 case Cend:
954 {
955 return 1;
956 }
957 case Canychar:
958 case Cbol:
959 case Ceol:
960 case Cbegbuf:
961 case Cendbuf:
962 case Cwordbeg:
963 case Cwordend:
964 case Cwordbound:
965 case Cnotwordbound:
966 {
967 break;
968 }
969 case Cset:
970 {
971 code += 256/8;
972 break;
973 }
974 case Cexact:
975 case Cstart_memory:
976 case Cend_memory:
977 case Cmatch_memory:
978 case Csyntaxspec:
979 case Cnotsyntaxspec:
980 {
981 code++;
982 break;
983 }
984 case Cstar_jump:
985 {
986 if (!re_optimize_star_jump(bufp, code))
987 {
988 return 0;
989 }
990 /* fall through */
991 }
992 case Cupdate_failure_jump:
993 case Cjump:
994 case Cdummy_failure_jump:
995 case Cfailure_jump:
996 case Crepeat1:
997 {
998 code += 2;
999 break;
1000 }
1001 default:
1002 {
1003 return 0;
1004 }
1005 }
1006 }
Guido van Rossum004c1e11997-05-09 02:35:58 +00001007}
1008
1009#define NEXTCHAR(var) \
1010{ \
Guido van Rossumfaf49081997-07-15 01:47:08 +00001011 if (pos >= size) \
1012 goto ends_prematurely; \
1013 (var) = regex[pos]; \
1014 pos++; \
Guido van Rossum004c1e11997-05-09 02:35:58 +00001015}
1016
1017#define ALLOC(amount) \
1018{ \
Guido van Rossumfaf49081997-07-15 01:47:08 +00001019 if (pattern_offset+(amount) > alloc) \
1020 { \
1021 alloc += 256 + (amount); \
1022 pattern = realloc(pattern, alloc); \
1023 if (!pattern) \
1024 goto out_of_memory; \
1025 } \
Guido van Rossum004c1e11997-05-09 02:35:58 +00001026}
Guido van Rossumb674c3b1992-01-19 16:32:47 +00001027
1028#define STORE(ch) pattern[pattern_offset++] = (ch)
1029
1030#define CURRENT_LEVEL_START (starts[starts_base + current_level])
1031
1032#define SET_LEVEL_START starts[starts_base + current_level] = pattern_offset
1033
Guido van Rossum004c1e11997-05-09 02:35:58 +00001034#define PUSH_LEVEL_STARTS \
Guido van Rossumfaf49081997-07-15 01:47:08 +00001035if (starts_base < (MAX_NESTING-1)*NUM_LEVELS) \
1036 starts_base += NUM_LEVELS; \
1037else \
1038 goto too_complex \
Guido van Rossumb674c3b1992-01-19 16:32:47 +00001039
1040#define POP_LEVEL_STARTS starts_base -= NUM_LEVELS
1041
Guido van Rossum004c1e11997-05-09 02:35:58 +00001042#define PUT_ADDR(offset,addr) \
1043{ \
Guido van Rossumfaf49081997-07-15 01:47:08 +00001044 int disp = (addr) - (offset) - 2; \
1045 pattern[(offset)] = disp & 0xff; \
1046 pattern[(offset)+1] = (disp>>8) & 0xff; \
Guido van Rossum004c1e11997-05-09 02:35:58 +00001047}
Guido van Rossumb674c3b1992-01-19 16:32:47 +00001048
Guido van Rossum004c1e11997-05-09 02:35:58 +00001049#define INSERT_JUMP(pos,type,addr) \
1050{ \
Guido van Rossumfaf49081997-07-15 01:47:08 +00001051 int a, p = (pos), t = (type), ad = (addr); \
1052 for (a = pattern_offset - 1; a >= p; a--) \
1053 pattern[a + 3] = pattern[a]; \
1054 pattern[p] = t; \
1055 PUT_ADDR(p+1,ad); \
1056 pattern_offset += 3; \
Guido van Rossum004c1e11997-05-09 02:35:58 +00001057}
Guido van Rossumfaf49081997-07-15 01:47:08 +00001058
Guido van Rossumb674c3b1992-01-19 16:32:47 +00001059#define SETBIT(buf,offset,bit) (buf)[(offset)+(bit)/8] |= (1<<((bit) & 7))
1060
Guido van Rossum004c1e11997-05-09 02:35:58 +00001061#define SET_FIELDS \
1062{ \
Guido van Rossumfaf49081997-07-15 01:47:08 +00001063 bufp->allocated = alloc; \
1064 bufp->buffer = pattern; \
1065 bufp->used = pattern_offset; \
Guido van Rossum004c1e11997-05-09 02:35:58 +00001066}
Guido van Rossumb674c3b1992-01-19 16:32:47 +00001067
Guido van Rossum004c1e11997-05-09 02:35:58 +00001068#define GETHEX(var) \
1069{ \
Guido van Rossumfaf49081997-07-15 01:47:08 +00001070 char gethex_ch, gethex_value; \
1071 NEXTCHAR(gethex_ch); \
1072 gethex_value = hex_char_to_decimal(gethex_ch); \
1073 if (gethex_value == 16) \
1074 goto hex_error; \
1075 NEXTCHAR(gethex_ch); \
1076 gethex_ch = hex_char_to_decimal(gethex_ch); \
1077 if (gethex_ch == 16) \
1078 goto hex_error; \
1079 (var) = gethex_value * 16 + gethex_ch; \
Guido van Rossum004c1e11997-05-09 02:35:58 +00001080}
Guido van Rossumb674c3b1992-01-19 16:32:47 +00001081
Guido van Rossumfaf49081997-07-15 01:47:08 +00001082#define ANSI_TRANSLATE(ch) \
Guido van Rossum004c1e11997-05-09 02:35:58 +00001083{ \
Guido van Rossumfaf49081997-07-15 01:47:08 +00001084 switch (ch) \
1085 { \
1086 case 'a': \
1087 case 'A': \
1088 { \
1089 ch = 7; /* audible bell */ \
1090 break; \
1091 } \
1092 case 'b': \
1093 case 'B': \
1094 { \
1095 ch = 8; /* backspace */ \
1096 break; \
1097 } \
1098 case 'f': \
1099 case 'F': \
1100 { \
1101 ch = 12; /* form feed */ \
1102 break; \
1103 } \
1104 case 'n': \
1105 case 'N': \
1106 { \
1107 ch = 10; /* line feed */ \
1108 break; \
1109 } \
1110 case 'r': \
1111 case 'R': \
1112 { \
1113 ch = 13; /* carriage return */ \
1114 break; \
1115 } \
1116 case 't': \
1117 case 'T': \
1118 { \
1119 ch = 9; /* tab */ \
1120 break; \
1121 } \
1122 case 'v': \
1123 case 'V': \
1124 { \
1125 ch = 11; /* vertical tab */ \
1126 break; \
1127 } \
1128 case 'x': /* hex code */ \
1129 case 'X': \
1130 { \
1131 GETHEX(ch); \
1132 break; \
1133 } \
1134 default: \
1135 { \
1136 /* other characters passed through */ \
1137 if (translate) \
1138 ch = translate[(unsigned char)ch]; \
1139 break; \
1140 } \
1141 } \
Guido van Rossum004c1e11997-05-09 02:35:58 +00001142}
Guido van Rossumb674c3b1992-01-19 16:32:47 +00001143
Guido van Rossum004c1e11997-05-09 02:35:58 +00001144char *re_compile_pattern(char *regex, int size, regexp_t bufp)
1145{
Guido van Rossumfaf49081997-07-15 01:47:08 +00001146 int a;
1147 int pos;
1148 int op;
1149 int current_level;
1150 int level;
1151 int opcode;
1152 int pattern_offset = 0, alloc;
1153 int starts[NUM_LEVELS * MAX_NESTING];
1154 int starts_base;
1155 int future_jumps[MAX_NESTING];
1156 int num_jumps;
1157 unsigned char ch = '\0';
1158 char *pattern;
1159 char *translate;
1160 int next_register;
1161 int paren_depth;
1162 int num_open_registers;
1163 int open_registers[RE_NREGS];
1164 int beginning_context;
1165
1166 if (!re_compile_initialized)
1167 re_compile_initialize();
1168 bufp->used = 0;
1169 bufp->fastmap_accurate = 0;
1170 bufp->uses_registers = 1;
1171 bufp->num_registers = 1;
1172 translate = bufp->translate;
1173 pattern = bufp->buffer;
1174 alloc = bufp->allocated;
1175 if (alloc == 0 || pattern == NULL)
1176 {
1177 alloc = 256;
1178 pattern = malloc(alloc);
1179 if (!pattern)
1180 goto out_of_memory;
1181 }
1182 pattern_offset = 0;
1183 starts_base = 0;
1184 num_jumps = 0;
1185 current_level = 0;
1186 SET_LEVEL_START;
1187 num_open_registers = 0;
1188 next_register = 1;
1189 paren_depth = 0;
1190 beginning_context = 1;
1191 op = -1;
1192 /* we use Rend dummy to ensure that pending jumps are updated
1193 (due to low priority of Rend) before exiting the loop. */
1194 pos = 0;
1195 while (op != Rend)
1196 {
1197 if (pos >= size)
1198 op = Rend;
1199 else
1200 {
1201 NEXTCHAR(ch);
1202 if (translate)
1203 ch = translate[(unsigned char)ch];
1204 op = regexp_plain_ops[(unsigned char)ch];
1205 if (op == Rquote)
1206 {
1207 NEXTCHAR(ch);
1208 op = regexp_quoted_ops[(unsigned char)ch];
1209 if (op == Rnormal && regexp_ansi_sequences)
1210 ANSI_TRANSLATE(ch);
1211 }
1212 }
1213 level = regexp_precedences[op];
1214 /* printf("ch='%c' op=%d level=%d current_level=%d
1215 curlevstart=%d\n", ch, op, level, current_level,
1216 CURRENT_LEVEL_START); */
1217 if (level > current_level)
1218 {
1219 for (current_level++; current_level < level; current_level++)
1220 SET_LEVEL_START;
1221 SET_LEVEL_START;
1222 }
1223 else
1224 if (level < current_level)
1225 {
1226 current_level = level;
1227 for (;num_jumps > 0 &&
1228 future_jumps[num_jumps-1] >= CURRENT_LEVEL_START;
1229 num_jumps--)
1230 PUT_ADDR(future_jumps[num_jumps-1], pattern_offset);
1231 }
1232 switch (op)
1233 {
1234 case Rend:
1235 {
1236 break;
1237 }
1238 case Rnormal:
1239 {
1240 normal_char:
1241 opcode = Cexact;
1242 store_opcode_and_arg: /* opcode & ch must be set */
1243 SET_LEVEL_START;
1244 ALLOC(2);
1245 STORE(opcode);
1246 STORE(ch);
1247 break;
1248 }
1249 case Ranychar:
1250 {
1251 opcode = Canychar;
1252 store_opcode:
1253 SET_LEVEL_START;
1254 ALLOC(1);
1255 STORE(opcode);
1256 break;
1257 }
1258 case Rquote:
1259 {
1260 abort();
1261 /*NOTREACHED*/
1262 }
1263 case Rbol:
1264 {
1265 if (!beginning_context)
1266 if (regexp_context_indep_ops)
1267 goto op_error;
1268 else
1269 goto normal_char;
1270 opcode = Cbol;
1271 goto store_opcode;
1272 }
1273 case Reol:
1274 {
1275 if (!((pos >= size) ||
1276 ((regexp_syntax & RE_NO_BK_VBAR) ?
1277 (regex[pos] == '\174') :
1278 (pos+1 < size && regex[pos] == '\134' &&
1279 regex[pos+1] == '\174')) ||
1280 ((regexp_syntax & RE_NO_BK_PARENS)?
1281 (regex[pos] == ')'):
1282 (pos+1 < size && regex[pos] == '\134' &&
1283 regex[pos+1] == ')'))))
1284 if (regexp_context_indep_ops)
1285 goto op_error;
1286 else
1287 goto normal_char;
1288 opcode = Ceol;
1289 goto store_opcode;
1290 /* NOTREACHED */
1291 break;
1292 }
1293 case Roptional:
1294 {
1295 if (beginning_context)
1296 if (regexp_context_indep_ops)
1297 goto op_error;
1298 else
1299 goto normal_char;
1300 if (CURRENT_LEVEL_START == pattern_offset)
1301 break; /* ignore empty patterns for ? */
1302 ALLOC(3);
1303 INSERT_JUMP(CURRENT_LEVEL_START, Cfailure_jump,
1304 pattern_offset + 3);
1305 break;
1306 }
1307 case Rstar:
1308 case Rplus:
1309 {
1310 if (beginning_context)
1311 if (regexp_context_indep_ops)
1312 goto op_error;
1313 else
1314 goto normal_char;
1315 if (CURRENT_LEVEL_START == pattern_offset)
1316 break; /* ignore empty patterns for + and * */
1317 ALLOC(9);
1318 INSERT_JUMP(CURRENT_LEVEL_START, Cfailure_jump,
1319 pattern_offset + 6);
1320 INSERT_JUMP(pattern_offset, Cstar_jump, CURRENT_LEVEL_START);
1321 if (op == Rplus) /* jump over initial failure_jump */
1322 INSERT_JUMP(CURRENT_LEVEL_START, Cdummy_failure_jump,
1323 CURRENT_LEVEL_START + 6);
1324 break;
1325 }
1326 case Ror:
1327 {
1328 ALLOC(6);
1329 INSERT_JUMP(CURRENT_LEVEL_START, Cfailure_jump,
1330 pattern_offset + 6);
1331 if (num_jumps >= MAX_NESTING)
1332 goto too_complex;
1333 STORE(Cjump);
1334 future_jumps[num_jumps++] = pattern_offset;
1335 STORE(0);
1336 STORE(0);
1337 SET_LEVEL_START;
1338 break;
1339 }
1340 case Ropenpar:
1341 {
1342 SET_LEVEL_START;
1343 if (next_register < RE_NREGS)
1344 {
1345 bufp->uses_registers = 1;
1346 ALLOC(2);
1347 STORE(Cstart_memory);
1348 STORE(next_register);
1349 open_registers[num_open_registers++] = next_register;
1350 bufp->num_registers++;
1351 next_register++;
1352 }
1353 paren_depth++;
1354 PUSH_LEVEL_STARTS;
1355 current_level = 0;
1356 SET_LEVEL_START;
1357 break;
1358 }
1359 case Rclosepar:
1360 {
1361 if (paren_depth <= 0)
1362 goto parenthesis_error;
1363 POP_LEVEL_STARTS;
1364 current_level = regexp_precedences[Ropenpar];
1365 paren_depth--;
1366 if (paren_depth < num_open_registers)
1367 {
1368 bufp->uses_registers = 1;
1369 ALLOC(2);
1370 STORE(Cend_memory);
1371 num_open_registers--;
1372 STORE(open_registers[num_open_registers]);
1373 }
1374 break;
1375 }
1376 case Rmemory:
1377 {
1378 if (ch == '0')
1379 goto bad_match_register;
1380 assert(ch >= '0' && ch <= '9');
1381 bufp->uses_registers = 1;
1382 opcode = Cmatch_memory;
1383 ch -= '0';
1384 goto store_opcode_and_arg;
1385 }
1386 case Rextended_memory:
1387 {
1388 NEXTCHAR(ch);
1389 if (ch < '0' || ch > '9')
1390 goto bad_match_register;
1391 NEXTCHAR(a);
1392 if (a < '0' || a > '9')
1393 goto bad_match_register;
1394 ch = 10 * (a - '0') + ch - '0';
1395 if (ch <= 0 || ch >= RE_NREGS)
1396 goto bad_match_register;
1397 bufp->uses_registers = 1;
1398 opcode = Cmatch_memory;
1399 goto store_opcode_and_arg;
1400 }
1401 case Ropenset:
1402 {
1403 int complement;
1404 int prev;
1405 int offset;
1406 int range;
1407 int firstchar;
Guido van Rossumb674c3b1992-01-19 16:32:47 +00001408
Guido van Rossumfaf49081997-07-15 01:47:08 +00001409 SET_LEVEL_START;
1410 ALLOC(1+256/8);
1411 STORE(Cset);
1412 offset = pattern_offset;
1413 for (a = 0; a < 256/8; a++)
1414 STORE(0);
1415 NEXTCHAR(ch);
1416 if (translate)
1417 ch = translate[(unsigned char)ch];
1418 if (ch == '\136')
1419 {
1420 complement = 1;
1421 NEXTCHAR(ch);
1422 if (translate)
1423 ch = translate[(unsigned char)ch];
1424 }
1425 else
1426 complement = 0;
1427 prev = -1;
1428 range = 0;
1429 firstchar = 1;
1430 while (ch != '\135' || firstchar)
1431 {
1432 firstchar = 0;
1433 if (regexp_ansi_sequences && ch == '\134')
1434 {
1435 NEXTCHAR(ch);
1436 ANSI_TRANSLATE(ch);
1437 }
1438 if (range)
1439 {
1440 for (a = prev; a <= (int)ch; a++)
1441 SETBIT(pattern, offset, a);
1442 prev = -1;
1443 range = 0;
1444 }
1445 else
1446 if (prev != -1 && ch == '-')
1447 range = 1;
1448 else
1449 {
1450 SETBIT(pattern, offset, ch);
1451 prev = ch;
1452 }
1453 NEXTCHAR(ch);
1454 if (translate)
1455 ch = translate[(unsigned char)ch];
1456 }
1457 if (range)
1458 SETBIT(pattern, offset, '-');
1459 if (complement)
1460 {
1461 for (a = 0; a < 256/8; a++)
1462 pattern[offset+a] ^= 0xff;
1463 }
1464 break;
1465 }
1466 case Rbegbuf:
1467 {
1468 opcode = Cbegbuf;
1469 goto store_opcode;
1470 }
1471 case Rendbuf:
1472 {
1473 opcode = Cendbuf;
1474 goto store_opcode;
1475 }
1476 case Rwordchar:
1477 {
1478 opcode = Csyntaxspec;
1479 ch = Sword;
1480 goto store_opcode_and_arg;
1481 }
1482 case Rnotwordchar:
1483 {
1484 opcode = Cnotsyntaxspec;
1485 ch = Sword;
1486 goto store_opcode_and_arg;
1487 }
1488 case Rwordbeg:
1489 {
1490 opcode = Cwordbeg;
1491 goto store_opcode;
1492 }
1493 case Rwordend:
1494 {
1495 opcode = Cwordend;
1496 goto store_opcode;
1497 }
1498 case Rwordbound:
1499 {
1500 opcode = Cwordbound;
1501 goto store_opcode;
1502 }
1503 case Rnotwordbound:
1504 {
1505 opcode = Cnotwordbound;
1506 goto store_opcode;
1507 }
1508 default:
1509 {
1510 abort();
1511 }
1512 }
1513 beginning_context = (op == Ropenpar || op == Ror);
1514 }
1515 if (starts_base != 0)
1516 goto parenthesis_error;
1517 assert(num_jumps == 0);
1518 ALLOC(1);
1519 STORE(Cend);
1520 SET_FIELDS;
1521 if(!re_optimize(bufp))
1522 return "Optimization error";
1523 return NULL;
Guido van Rossumb674c3b1992-01-19 16:32:47 +00001524
Guido van Rossum004c1e11997-05-09 02:35:58 +00001525 op_error:
Guido van Rossumfaf49081997-07-15 01:47:08 +00001526 SET_FIELDS;
1527 return "Badly placed special character";
Guido van Rossumb674c3b1992-01-19 16:32:47 +00001528
Guido van Rossum004c1e11997-05-09 02:35:58 +00001529 bad_match_register:
Guido van Rossumfaf49081997-07-15 01:47:08 +00001530 SET_FIELDS;
1531 return "Bad match register number";
Guido van Rossum004c1e11997-05-09 02:35:58 +00001532
1533 hex_error:
Guido van Rossumfaf49081997-07-15 01:47:08 +00001534 SET_FIELDS;
1535 return "Bad hexadecimal number";
Guido van Rossum004c1e11997-05-09 02:35:58 +00001536
1537 parenthesis_error:
Guido van Rossumfaf49081997-07-15 01:47:08 +00001538 SET_FIELDS;
1539 return "Badly placed parenthesis";
Guido van Rossum004c1e11997-05-09 02:35:58 +00001540
1541 out_of_memory:
Guido van Rossumfaf49081997-07-15 01:47:08 +00001542 SET_FIELDS;
1543 return "Out of memory";
Guido van Rossum004c1e11997-05-09 02:35:58 +00001544
1545 ends_prematurely:
Guido van Rossumfaf49081997-07-15 01:47:08 +00001546 SET_FIELDS;
1547 return "Regular expression ends prematurely";
Guido van Rossumb674c3b1992-01-19 16:32:47 +00001548
Guido van Rossum004c1e11997-05-09 02:35:58 +00001549 too_complex:
Guido van Rossumfaf49081997-07-15 01:47:08 +00001550 SET_FIELDS;
1551 return "Regular expression too complex";
Guido van Rossumb674c3b1992-01-19 16:32:47 +00001552}
Guido van Rossum004c1e11997-05-09 02:35:58 +00001553
Guido van Rossumb674c3b1992-01-19 16:32:47 +00001554#undef CHARAT
1555#undef NEXTCHAR
1556#undef GETHEX
1557#undef ALLOC
1558#undef STORE
1559#undef CURRENT_LEVEL_START
1560#undef SET_LEVEL_START
1561#undef PUSH_LEVEL_STARTS
1562#undef POP_LEVEL_STARTS
1563#undef PUT_ADDR
1564#undef INSERT_JUMP
1565#undef SETBIT
1566#undef SET_FIELDS
1567
Guido van Rossum004c1e11997-05-09 02:35:58 +00001568#define PREFETCH if (text == textend) goto fail
1569
1570#define NEXTCHAR(var) \
1571PREFETCH; \
1572var = (unsigned char)*text++; \
1573if (translate) \
Guido van Rossumfaf49081997-07-15 01:47:08 +00001574 var = translate[var]
Guido van Rossum004c1e11997-05-09 02:35:58 +00001575
1576int re_match(regexp_t bufp,
1577 char *string,
1578 int size,
1579 int pos,
1580 regexp_registers_t old_regs)
Guido van Rossumb674c3b1992-01-19 16:32:47 +00001581{
Guido van Rossumfaf49081997-07-15 01:47:08 +00001582 char *code;
1583 char *translate;
1584 char *text;
1585 char *textstart;
1586 char *textend;
1587 int a;
1588 int b;
1589 int ch;
1590 int reg;
1591 int match_end;
1592 char *regstart;
1593 char *regend;
1594 int regsize;
1595 match_state state;
Guido van Rossum004c1e11997-05-09 02:35:58 +00001596
Guido van Rossumfaf49081997-07-15 01:47:08 +00001597 assert(pos >= 0 && size >= 0);
1598 assert(pos <= size);
Guido van Rossum004c1e11997-05-09 02:35:58 +00001599
Guido van Rossumfaf49081997-07-15 01:47:08 +00001600 text = string + pos;
1601 textstart = string;
1602 textend = string + size;
Guido van Rossum004c1e11997-05-09 02:35:58 +00001603
Guido van Rossumfaf49081997-07-15 01:47:08 +00001604 code = bufp->buffer;
Guido van Rossum004c1e11997-05-09 02:35:58 +00001605
Guido van Rossumfaf49081997-07-15 01:47:08 +00001606 translate = bufp->translate;
Guido van Rossum004c1e11997-05-09 02:35:58 +00001607
Guido van Rossumfaf49081997-07-15 01:47:08 +00001608 NEW_STATE(state, bufp->num_registers);
1609
Guido van Rossum004c1e11997-05-09 02:35:58 +00001610 continue_matching:
Guido van Rossumfaf49081997-07-15 01:47:08 +00001611 switch (*code++)
Guido van Rossumb674c3b1992-01-19 16:32:47 +00001612 {
Guido van Rossumfaf49081997-07-15 01:47:08 +00001613 case Cend:
Guido van Rossum004c1e11997-05-09 02:35:58 +00001614 {
Guido van Rossumfaf49081997-07-15 01:47:08 +00001615 match_end = text - textstart;
1616 if (old_regs)
1617 {
1618 old_regs->start[0] = pos;
1619 old_regs->end[0] = match_end;
1620 if (!bufp->uses_registers)
1621 {
1622 for (a = 1; a < RE_NREGS; a++)
1623 {
1624 old_regs->start[a] = -1;
1625 old_regs->end[a] = -1;
1626 }
1627 }
1628 else
1629 {
1630 for (a = 1; a < bufp->num_registers; a++)
1631 {
1632 if ((GET_REG_START(state, a) == NULL) ||
1633 (GET_REG_END(state, a) == NULL))
1634 {
1635 old_regs->start[a] = -1;
1636 old_regs->end[a] = -1;
1637 continue;
1638 }
1639 old_regs->start[a] = GET_REG_START(state, a) - textstart;
1640 old_regs->end[a] = GET_REG_END(state, a) - textstart;
1641 }
1642 for (; a < RE_NREGS; a++)
1643 {
1644 old_regs->start[a] = -1;
1645 old_regs->end[a] = -1;
1646 }
1647 }
1648 }
1649 FREE_STATE(state);
1650 return match_end - pos;
Guido van Rossum004c1e11997-05-09 02:35:58 +00001651 }
Guido van Rossumfaf49081997-07-15 01:47:08 +00001652 case Cbol:
1653 {
1654 if (text == textstart || text[-1] == '\n')
1655 goto continue_matching;
1656 goto fail;
1657 }
1658 case Ceol:
1659 {
1660 if (text == textend || *text == '\n')
1661 goto continue_matching;
1662 goto fail;
1663 }
1664 case Cset:
1665 {
1666 NEXTCHAR(ch);
1667 if (code[ch/8] & (1<<(ch & 7)))
1668 {
1669 code += 256/8;
1670 goto continue_matching;
1671 }
1672 goto fail;
1673 }
1674 case Cexact:
1675 {
1676 NEXTCHAR(ch);
1677 if (ch != (unsigned char)*code++)
1678 goto fail;
1679 goto continue_matching;
1680 }
1681 case Canychar:
1682 {
1683 NEXTCHAR(ch);
1684 if (ch == '\n')
1685 goto fail;
1686 goto continue_matching;
1687 }
1688 case Cstart_memory:
1689 {
1690 reg = *code++;
1691 SET_REG_START(state, reg, text, goto error);
1692 goto continue_matching;
1693 }
1694 case Cend_memory:
1695 {
1696 reg = *code++;
1697 SET_REG_END(state, reg, text, goto error);
1698 goto continue_matching;
1699 }
1700 case Cmatch_memory:
1701 {
1702 reg = *code++;
1703 regstart = GET_REG_START(state, reg);
1704 regend = GET_REG_END(state, reg);
1705 if ((regstart == NULL) || (regend == NULL))
1706 goto fail; /* or should we just match nothing? */
1707 regsize = regend - regstart;
Guido van Rossum004c1e11997-05-09 02:35:58 +00001708
Guido van Rossumfaf49081997-07-15 01:47:08 +00001709 if (regsize > (textend - text))
1710 goto fail;
1711 if(translate)
1712 {
1713 for (; regstart < regend; regstart++, text++)
1714 if (translate[*regstart] != translate[*text])
1715 goto fail;
1716 }
1717 else
1718 for (; regstart < regend; regstart++, text++)
1719 if (*regstart != *text)
1720 goto fail;
1721 goto continue_matching;
Guido van Rossum004c1e11997-05-09 02:35:58 +00001722 }
Guido van Rossumfaf49081997-07-15 01:47:08 +00001723 case Cupdate_failure_jump:
Guido van Rossumdb25f321997-07-10 14:31:32 +00001724 {
Guido van Rossumfaf49081997-07-15 01:47:08 +00001725 UPDATE_FAILURE(state, text, goto error);
1726 /* fall to next case */
Guido van Rossumdb25f321997-07-10 14:31:32 +00001727 }
Guido van Rossumfaf49081997-07-15 01:47:08 +00001728 /* treat Cstar_jump just like Cjump if it hasn't been optimized */
1729 case Cstar_jump:
1730 case Cjump:
1731 {
1732 a = (unsigned char)*code++;
1733 a |= (unsigned char)*code++ << 8;
1734 code += (int)SHORT(a);
1735 goto continue_matching;
1736 }
1737 case Cdummy_failure_jump:
1738 {
1739 a = (unsigned char)*code++;
1740 a |= (unsigned char)*code++ << 8;
1741 a = (int)SHORT(a);
1742 assert(*code == Cfailure_jump);
1743 b = (unsigned char)code[1];
1744 b |= (unsigned char)code[2] << 8;
1745 PUSH_FAILURE(state, code + (int)SHORT(b) + 3, NULL, goto error);
1746 code += a;
1747 goto continue_matching;
1748 }
1749 case Cfailure_jump:
1750 {
1751 a = (unsigned char)*code++;
1752 a |= (unsigned char)*code++ << 8;
1753 a = (int)SHORT(a);
1754 PUSH_FAILURE(state, code + a, text, goto error);
1755 goto continue_matching;
1756 }
1757 case Crepeat1:
1758 {
1759 char *pinst;
1760 a = (unsigned char)*code++;
1761 a |= (unsigned char)*code++ << 8;
1762 a = (int)SHORT(a);
1763 pinst = code + a;
1764 /* pinst is sole instruction in loop, and it matches a
1765 * single character. Since Crepeat1 was originally a
1766 * Cupdate_failure_jump, we also know that backtracking
1767 * is useless: so long as the single-character
1768 * expression matches, it must be used. Also, in the
1769 * case of +, we've already matched one character, so +
1770 * can't fail: nothing here can cause a failure. */
1771 switch (*pinst++)
1772 {
1773 case Cset:
1774 {
1775 if (translate)
1776 {
1777 while (text < textend)
1778 {
1779 ch = translate[(unsigned char)*text];
1780 if (pinst[ch/8] & (1<<(ch & 7)))
1781 text++;
1782 else
1783 break;
1784 }
1785 }
1786 else
1787 {
1788 while (text < textend)
1789 {
1790 ch = (unsigned char)*text;
1791 if (pinst[ch/8] & (1<<(ch & 7)))
1792 text++;
1793 else
1794 break;
1795 }
1796 }
1797 break;
1798 }
1799 case Cexact:
1800 {
1801 ch = (unsigned char)*pinst;
1802 if (translate)
1803 {
1804 while (text < textend &&
1805 translate[(unsigned char)*text] == ch)
1806 text++;
1807 }
1808 else
1809 {
1810 while (text < textend && (unsigned char)*text == ch)
1811 text++;
1812 }
1813 break;
1814 }
1815 case Canychar:
1816 {
1817 while (text < textend && (unsigned char)*text != '\n')
1818 text++;
1819 break;
1820 }
1821 case Csyntaxspec:
1822 {
1823 a = (unsigned char)*pinst;
1824 if (translate)
1825 {
1826 while (text < textend &&
1827 translate[SYNTAX(*text)] == a)
1828 text++;
1829 }
1830 else
1831 {
1832 while (text < textend && SYNTAX(*text) == a)
1833 text++;
1834 }
1835 break;
1836 }
1837 case Cnotsyntaxspec:
1838 {
1839 a = (unsigned char)*pinst;
1840 if (translate)
1841 {
1842 while (text < textend &&
1843 translate[SYNTAX(*text)] != a)
1844 text++;
1845 }
1846 else
1847 {
1848 while (text < textend && SYNTAX(*text) != a)
1849 text++;
1850 }
1851 break;
1852 }
1853 default:
1854 {
1855 abort();
1856 /*NOTREACHED*/
1857 }
1858 }
1859 /* due to the funky way + and * are compiled, the top
1860 * failure- stack entry at this point is actually a
1861 * success entry -- update it & pop it */
1862 UPDATE_FAILURE(state, text, goto error);
1863 goto fail; /* i.e., succeed <wink/sigh> */
1864 }
1865 case Cbegbuf:
1866 {
1867 if (text == textstart)
1868 goto continue_matching;
1869 goto fail;
1870 }
1871 case Cendbuf:
1872 {
1873 if (text == textend)
1874 goto continue_matching;
1875 goto fail;
1876 }
1877 case Cwordbeg:
1878 {
1879 if (text == textend)
1880 goto fail;
Guido van Rossum74fb3031997-07-17 22:41:38 +00001881 if (SYNTAX(*text) & Sword)
Guido van Rossumfaf49081997-07-15 01:47:08 +00001882 goto fail;
1883 if (text == textstart)
1884 goto continue_matching;
Guido van Rossum74fb3031997-07-17 22:41:38 +00001885 if (!(SYNTAX(text[-1]) & Sword))
Guido van Rossumfaf49081997-07-15 01:47:08 +00001886 goto continue_matching;
1887 goto fail;
1888 }
1889 case Cwordend:
1890 {
1891 if (text == textstart)
1892 goto fail;
Guido van Rossum74fb3031997-07-17 22:41:38 +00001893 if (!(SYNTAX(text[-1]) & Sword))
Guido van Rossumfaf49081997-07-15 01:47:08 +00001894 goto fail;
1895 if (text == textend)
1896 goto continue_matching;
Guido van Rossum74fb3031997-07-17 22:41:38 +00001897 if (SYNTAX(*text) & Sword)
Guido van Rossumfaf49081997-07-15 01:47:08 +00001898 goto fail;
1899 goto continue_matching;
1900 }
1901 case Cwordbound:
1902 {
1903 /* Note: as in gnu regexp, this also matches at the
1904 * beginning and end of buffer. */
Guido van Rossum004c1e11997-05-09 02:35:58 +00001905
Guido van Rossumfaf49081997-07-15 01:47:08 +00001906 if (text == textstart || text == textend)
1907 goto continue_matching;
Guido van Rossum74fb3031997-07-17 22:41:38 +00001908 if ((SYNTAX(text[-1]) & Sword) ^ (SYNTAX(*text) & Sword))
Guido van Rossumfaf49081997-07-15 01:47:08 +00001909 goto continue_matching;
1910 goto fail;
1911 }
1912 case Cnotwordbound:
1913 {
1914 /* Note: as in gnu regexp, this never matches at the
1915 * beginning and end of buffer. */
1916 if (text == textstart || text == textend)
1917 goto fail;
Guido van Rossum74fb3031997-07-17 22:41:38 +00001918 if (!((SYNTAX(text[-1]) & Sword) ^ (SYNTAX(*text) & Sword)))
Guido van Rossumfaf49081997-07-15 01:47:08 +00001919 goto fail;
1920 goto continue_matching;
1921 }
1922 case Csyntaxspec:
1923 {
1924 NEXTCHAR(ch);
Guido van Rossum74fb3031997-07-17 22:41:38 +00001925 if (!(SYNTAX(ch) & (unsigned char)*code++))
Guido van Rossumfaf49081997-07-15 01:47:08 +00001926 goto fail;
1927 goto continue_matching;
1928 }
1929 case Cnotsyntaxspec:
1930 {
1931 NEXTCHAR(ch);
Guido van Rossum74fb3031997-07-17 22:41:38 +00001932 if (SYNTAX(ch) & (unsigned char)*code++)
Guido van Rossumfaf49081997-07-15 01:47:08 +00001933 break;
1934 goto continue_matching;
1935 }
1936 default:
1937 {
1938 abort();
1939 /*NOTREACHED*/
1940 }
1941 }
Guido van Rossum004c1e11997-05-09 02:35:58 +00001942
Guido van Rossum3b1a57a1992-01-27 16:47:46 +00001943#if 0 /* This line is never reached --Guido */
Guido van Rossumfaf49081997-07-15 01:47:08 +00001944 abort();
Guido van Rossum5f21dd11992-01-19 16:49:14 +00001945#endif
Guido van Rossumfaf49081997-07-15 01:47:08 +00001946 /*
1947 *NOTREACHED
1948 */
Guido van Rossum004c1e11997-05-09 02:35:58 +00001949
1950 fail:
Guido van Rossumfaf49081997-07-15 01:47:08 +00001951 POP_FAILURE(state, code, text, goto done_matching, goto error);
1952 goto continue_matching;
Guido van Rossum004c1e11997-05-09 02:35:58 +00001953
1954 done_matching:
1955/* if(translated != NULL) */
1956/* free(translated); */
Guido van Rossumfaf49081997-07-15 01:47:08 +00001957 FREE_STATE(state);
1958 return -1;
Guido van Rossumb674c3b1992-01-19 16:32:47 +00001959
Guido van Rossum004c1e11997-05-09 02:35:58 +00001960 error:
1961/* if (translated != NULL) */
1962/* free(translated); */
Guido van Rossumfaf49081997-07-15 01:47:08 +00001963 FREE_STATE(state);
1964 return -2;
Guido van Rossumb674c3b1992-01-19 16:32:47 +00001965}
1966
1967#undef PREFETCH
1968#undef NEXTCHAR
Guido van Rossumb674c3b1992-01-19 16:32:47 +00001969
Guido van Rossum004c1e11997-05-09 02:35:58 +00001970int re_search(regexp_t bufp,
1971 char *string,
1972 int size,
1973 int pos,
1974 int range,
1975 regexp_registers_t regs)
Guido van Rossumb674c3b1992-01-19 16:32:47 +00001976{
Guido van Rossumfaf49081997-07-15 01:47:08 +00001977 char *fastmap;
1978 char *translate;
1979 char *text;
1980 char *partstart;
1981 char *partend;
1982 int dir;
1983 int ret;
1984 char anchor;
Guido van Rossumb674c3b1992-01-19 16:32:47 +00001985
Guido van Rossumfaf49081997-07-15 01:47:08 +00001986 assert(size >= 0 && pos >= 0);
1987 assert(pos + range >= 0 && pos + range <= size); /* Bugfix by ylo */
Guido van Rossumb674c3b1992-01-19 16:32:47 +00001988
Guido van Rossumfaf49081997-07-15 01:47:08 +00001989 fastmap = bufp->fastmap;
1990 translate = bufp->translate;
1991 if (fastmap && !bufp->fastmap_accurate)
1992 re_compile_fastmap(bufp);
1993 anchor = bufp->anchor;
1994 if (bufp->can_be_null == 1) /* can_be_null == 2: can match null at eob */
1995 fastmap = NULL;
Guido van Rossum004c1e11997-05-09 02:35:58 +00001996
Guido van Rossumfaf49081997-07-15 01:47:08 +00001997 if (range < 0)
1998 {
1999 dir = -1;
2000 range = -range;
Guido van Rossumb674c3b1992-01-19 16:32:47 +00002001 }
Guido van Rossum004c1e11997-05-09 02:35:58 +00002002 else
Guido van Rossumfaf49081997-07-15 01:47:08 +00002003 dir = 1;
2004
2005 if (anchor == 2)
2006 if (pos != 0)
2007 return -1;
2008 else
2009 range = 0;
2010
2011 for (; range >= 0; range--, pos += dir)
2012 {
2013 if (fastmap)
2014 {
2015 if (dir == 1)
2016 { /* searching forwards */
2017
2018 text = string + pos;
2019 partend = string + size;
2020 partstart = text;
2021 if (translate)
2022 while (text != partend &&
2023 !fastmap[(unsigned char) translate[(unsigned char)*text]])
2024 text++;
2025 else
2026 while (text != partend && !fastmap[(unsigned char)*text])
2027 text++;
2028 pos += text - partstart;
2029 range -= text - partstart;
2030 if (pos == size && bufp->can_be_null == 0)
2031 return -1;
2032 }
2033 else
2034 { /* searching backwards */
2035 text = string + pos;
2036 partstart = string + pos - range;
2037 partend = text;
2038 if (translate)
2039 while (text != partstart &&
2040 !fastmap[(unsigned char)
2041 translate[(unsigned char)*text]])
2042 text--;
2043 else
2044 while (text != partstart &&
2045 !fastmap[(unsigned char)*text])
2046 text--;
2047 pos -= partend - text;
2048 range -= partend - text;
2049 }
2050 }
2051 if (anchor == 1)
2052 { /* anchored to begline */
2053 if (pos > 0 && (string[pos - 1] != '\n'))
2054 continue;
2055 }
2056 assert(pos >= 0 && pos <= size);
2057 ret = re_match(bufp, string, size, pos, regs);
2058 if (ret >= 0)
2059 return pos;
2060 if (ret == -2)
2061 return -2;
Guido van Rossumb674c3b1992-01-19 16:32:47 +00002062 }
Guido van Rossumfaf49081997-07-15 01:47:08 +00002063 return -1;
Guido van Rossumb674c3b1992-01-19 16:32:47 +00002064}
Guido van Rossum74fb3031997-07-17 22:41:38 +00002065
2066/*
2067** Local Variables:
2068** mode: c
2069** c-file-style: "python"
2070** End:
2071*/