📄 regex.c
字号:
jump, /* Same as jump, but marks the end of an alternative. */ jump_past_alt, /* Followed by two-byte relative address of place to resume at in case of failure. */ on_failure_jump, /* Like on_failure_jump, but pushes a placeholder instead of the current string position when executed. */ on_failure_keep_string_jump, /* Throw away latest failure point and then jump to following two-byte relative address. */ pop_failure_jump, /* Change to pop_failure_jump if know won't have to backtrack to match; otherwise change to jump. This is used to jump back to the beginning of a repeat. If what follows this jump clearly won't match what the repeat does, such that we can be sure that there is no use backtracking out of repetitions already matched, then we change it to a pop_failure_jump. Followed by two-byte address. */ maybe_pop_jump, /* Jump to following two-byte address, and push a dummy failure point. This failure point will be thrown away if an attempt is made to use it for a failure. A `+' construct makes this before the first repeat. Also used as an intermediary kind of jump when compiling an alternative. */ dummy_failure_jump, /* Push a dummy failure point and continue. Used at the end of alternatives. */ push_dummy_failure, /* Followed by two-byte relative address and two-byte number n. After matching N times, jump to the address upon failure. */ succeed_n, /* Followed by two-byte relative address, and two-byte number n. Jump to the address N times, then fail. */ jump_n, /* Set the following two-byte relative address to the subsequent two-byte number. The address *includes* the two bytes of number. */ set_number_at, wordchar, /* Matches any word-constituent character. */ notwordchar, /* Matches any char that is not a word-constituent. */ wordbeg, /* Succeeds if at word beginning. */ wordend, /* Succeeds if at word end. */ wordbound, /* Succeeds if at a word boundary. */ notwordbound /* Succeeds if not at a word boundary. */#ifdef emacs ,before_dot, /* Succeeds if before point. */ at_dot, /* Succeeds if at point. */ after_dot, /* Succeeds if after point. */ /* Matches any character whose syntax is specified. Followed by a byte which contains a syntax code, e.g., Sword. */ syntaxspec, /* Matches any character whose syntax is not that specified. */ notsyntaxspec, /* Matches any character whose category-set contains the specified category. The operator is followed by a byte which contains a category code (mnemonic ASCII character). */ categoryspec, /* Matches any character whose category-set does not contain the specified category. The operator is followed by a byte which contains the category code (mnemonic ASCII character). */ notcategoryspec#endif /* emacs */} re_opcode_t;/* Common operations on the compiled pattern. *//* Store NUMBER in two contiguous bytes starting at DESTINATION. */#define STORE_NUMBER(destination, number) \ do { \ (destination)[0] = (number) & 0377; \ (destination)[1] = (number) >> 8; \ } while (0)/* Same as STORE_NUMBER, except increment DESTINATION to the byte after where the number is stored. Therefore, DESTINATION must be an lvalue. */#define STORE_NUMBER_AND_INCR(destination, number) \ do { \ STORE_NUMBER (destination, number); \ (destination) += 2; \ } while (0)/* Put into DESTINATION a number stored in two contiguous bytes starting at SOURCE. */#define EXTRACT_NUMBER(destination, source) \ do { \ (destination) = *(source) & 0377; \ (destination) += SIGN_EXTEND_CHAR (*((source) + 1)) << 8; \ } while (0)#ifdef DEBUGstatic voidextract_number (dest, source) int *dest; unsigned char *source;{ int temp = SIGN_EXTEND_CHAR (*(source + 1)); *dest = *source & 0377; *dest += temp << 8;}#ifndef EXTRACT_MACROS /* To debug the macros. */#undef EXTRACT_NUMBER#define EXTRACT_NUMBER(dest, src) extract_number (&dest, src)#endif /* not EXTRACT_MACROS */#endif /* DEBUG *//* Same as EXTRACT_NUMBER, except increment SOURCE to after the number. SOURCE must be an lvalue. */#define EXTRACT_NUMBER_AND_INCR(destination, source) \ do { \ EXTRACT_NUMBER (destination, source); \ (source) += 2; \ } while (0)#ifdef DEBUGstatic voidextract_number_and_incr (destination, source) int *destination; unsigned char **source;{ extract_number (destination, *source); *source += 2;}#ifndef EXTRACT_MACROS#undef EXTRACT_NUMBER_AND_INCR#define EXTRACT_NUMBER_AND_INCR(dest, src) \ extract_number_and_incr (&dest, &src)#endif /* not EXTRACT_MACROS */#endif /* DEBUG *//* Store a multibyte character in three contiguous bytes starting DESTINATION, and increment DESTINATION to the byte after where the character is stored. Therefore, DESTINATION must be an lvalue. */#define STORE_CHARACTER_AND_INCR(destination, character) \ do { \ (destination)[0] = (character) & 0377; \ (destination)[1] = ((character) >> 8) & 0377; \ (destination)[2] = (character) >> 16; \ (destination) += 3; \ } while (0)/* Put into DESTINATION a character stored in three contiguous bytes starting at SOURCE. */#define EXTRACT_CHARACTER(destination, source) \ do { \ (destination) = ((source)[0] \ | ((source)[1] << 8) \ | ((source)[2] << 16)); \ } while (0)/* Macros for charset. *//* Size of bitmap of charset P in bytes. P is a start of charset, i.e. *P is (re_opcode_t) charset or (re_opcode_t) charset_not. */#define CHARSET_BITMAP_SIZE(p) ((p)[1] & 0x7F)/* Nonzero if charset P has range table. */#define CHARSET_RANGE_TABLE_EXISTS_P(p) ((p)[1] & 0x80)/* Return the address of range table of charset P. But not the start of table itself, but the before where the number of ranges is stored. `2 +' means to skip re_opcode_t and size of bitmap. */#define CHARSET_RANGE_TABLE(p) (&(p)[2 + CHARSET_BITMAP_SIZE (p)])/* Test if C is listed in the bitmap of charset P. */#define CHARSET_LOOKUP_BITMAP(p, c) \ ((c) < CHARSET_BITMAP_SIZE (p) * BYTEWIDTH \ && (p)[2 + (c) / BYTEWIDTH] & (1 << ((c) % BYTEWIDTH)))/* Return the address of end of RANGE_TABLE. COUNT is number of ranges (which is a pair of (start, end)) in the RANGE_TABLE. `* 2' is start of range and end of range. `* 3' is size of each start and end. */#define CHARSET_RANGE_TABLE_END(range_table, count) \ ((range_table) + (count) * 2 * 3)/* Test if C is in RANGE_TABLE. A flag NOT is negated if C is in. COUNT is number of ranges in RANGE_TABLE. */#define CHARSET_LOOKUP_RANGE_TABLE_RAW(not, c, range_table, count) \ do \ { \ int range_start, range_end; \ unsigned char *p; \ unsigned char *range_table_end \ = CHARSET_RANGE_TABLE_END ((range_table), (count)); \ \ for (p = (range_table); p < range_table_end; p += 2 * 3) \ { \ EXTRACT_CHARACTER (range_start, p); \ EXTRACT_CHARACTER (range_end, p + 3); \ \ if (range_start <= (c) && (c) <= range_end) \ { \ (not) = !(not); \ break; \ } \ } \ } \ while (0)/* Test if C is in range table of CHARSET. The flag NOT is negated if C is listed in it. */#define CHARSET_LOOKUP_RANGE_TABLE(not, c, charset) \ do \ { \ /* Number of ranges in range table. */ \ int count; \ unsigned char *range_table = CHARSET_RANGE_TABLE (charset); \ \ EXTRACT_NUMBER_AND_INCR (count, range_table); \ CHARSET_LOOKUP_RANGE_TABLE_RAW ((not), (c), range_table, count); \ } \ while (0)/* If DEBUG is defined, Regex prints many voluminous messages about what it is doing (if the variable `debug' is nonzero). If linked with the main program in `iregex.c', you can enter patterns and strings interactively. And if linked with the main program in `main.c' and the other test files, you can run the already-written tests. */#ifdef DEBUG/* We use standard I/O for debugging. */#include <stdio.h>/* It is useful to test things that ``must'' be true when debugging. */#include <assert.h>static int debug = 0;#define DEBUG_STATEMENT(e) e#define DEBUG_PRINT1(x) if (debug) printf (x)#define DEBUG_PRINT2(x1, x2) if (debug) printf (x1, x2)#define DEBUG_PRINT3(x1, x2, x3) if (debug) printf (x1, x2, x3)#define DEBUG_PRINT4(x1, x2, x3, x4) if (debug) printf (x1, x2, x3, x4)#define DEBUG_PRINT_COMPILED_PATTERN(p, s, e) \ if (debug) print_partial_compiled_pattern (s, e)#define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2) \ if (debug) print_double_string (w, s1, sz1, s2, sz2)/* Print the fastmap in human-readable form. */voidprint_fastmap (fastmap) char *fastmap;{ unsigned was_a_range = 0; unsigned i = 0; while (i < (1 << BYTEWIDTH)) { if (fastmap[i++]) { was_a_range = 0; putchar (i - 1); while (i < (1 << BYTEWIDTH) && fastmap[i]) { was_a_range = 1; i++; } if (was_a_range) { printf ("-"); putchar (i - 1); } } } putchar ('\n');}/* Print a compiled pattern string in human-readable form, starting at the START pointer into it and ending just before the pointer END. */voidprint_partial_compiled_pattern (start, end) unsigned char *start; unsigned char *end;{ int mcnt, mcnt2; unsigned char *p = start; unsigned char *pend = end; if (start == NULL) { printf ("(null)\n"); return; } /* Loop over pattern commands. */ while (p < pend) { printf ("%d:\t", p - start); switch ((re_opcode_t) *p++) { case no_op: printf ("/no_op"); break; case exactn: mcnt = *p++; printf ("/exactn/%d", mcnt); do { putchar ('/'); putchar (*p++); } while (--mcnt); break; case start_memory: mcnt = *p++; printf ("/start_memory/%d/%d", mcnt, *p++); break; case stop_memory: mcnt = *p++; printf ("/stop_memory/%d/%d", mcnt, *p++); break; case duplicate: printf ("/duplicate/%d", *p++); break; case anychar: printf ("/anychar"); break; case charset: case charset_not: { register int c, last = -100; register int in_range = 0; printf ("/charset [%s", (re_opcode_t) *(p - 1) == charset_not ? "^" : ""); assert (p + *p < pend); for (c = 0; c < 256; c++) if (c / 8 < *p && (p[1 + (c/8)] & (1 << (c % 8)))) { /* Are we starting a range? */ if (last + 1 == c && ! in_range) { putchar ('-'); in_range = 1; } /* Have we broken a range? */ else if (last + 1 != c && in_range) { putchar (last); in_range = 0; } if (! in_range) putchar (c); last = c; } if (in_range) putchar (last); putchar (']'); p += 1 + *p; } break; case begline: printf ("/begline"); break; case endline: printf ("/endline"); break; case on_failure_jump: extract_number_and_incr (&mcnt, &p); printf ("/on_failure_jump to %d", p + mcnt - start); break; case on_failure_keep_string_jump: extract_number_and_incr (&mcnt, &p); printf ("/on_failure_keep_string_jump to %d", p + mcnt - start); break; case dummy_failure_jump: extract_number_and_incr (&mcnt, &p); printf ("/dummy_failure_jump to %d", p + mcnt - start); break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -