]> git.saurik.com Git - wxWidgets.git/blob - src/regex/regexec.c
more warning fixes
[wxWidgets.git] / src / regex / regexec.c
1 /*
2 * the outer shell of regexec()
3 *
4 * This file includes engine.c *twice*, after muchos fiddling with the
5 * macros that code uses. This lets the same code operate on two different
6 * representations for state sets.
7 */
8 #include <sys/types.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <limits.h>
13 #include <ctype.h>
14 #include <regex.h>
15
16 #include "utils.h"
17 #include "regex2.h"
18
19 /* macros for manipulating states, small version */
20 #define states unsigned
21 #define states1 unsigned /* for later use in regexec() decision */
22 #define CLEAR(v) ((v) = 0)
23 #define SET0(v, n) ((v) &= ~((unsigned)1 << (n)))
24 #define SET1(v, n) ((v) |= (unsigned)1 << (n))
25 #define ISSET(v, n) ((v) & ((unsigned)1 << (n)))
26 #define ASSIGN(d, s) ((d) = (s))
27 #define EQ(a, b) ((a) == (b))
28 #define STATEVARS int dummy /* dummy version */
29 #define STATESETUP(m, n) /* nothing */
30 #define STATETEARDOWN(m) /* nothing */
31 #define SETUP(v) ((v) = 0)
32 #define onestate unsigned
33 #define INIT(o, n) ((o) = (unsigned)1 << (n))
34 #define INC(o) ((o) <<= 1)
35 #define ISSTATEIN(v, o) ((v) & (o))
36 /* some abbreviations; note that some of these know variable names! */
37 /* do "if I'm here, I can also be there" etc without branches */
38 #define FWD(dst, src, n) ((dst) |= ((unsigned)(src)&(here)) << (n))
39 #define BACK(dst, src, n) ((dst) |= ((unsigned)(src)&(here)) >> (n))
40 #define ISSETBACK(v, n) ((v) & ((unsigned)here >> (n)))
41 /* function names */
42 #define SNAMES /* engine.c looks after details */
43
44 #include "engine.c"
45
46 /* now undo things */
47 #undef states
48 #undef CLEAR
49 #undef SET0
50 #undef SET1
51 #undef ISSET
52 #undef ASSIGN
53 #undef EQ
54 #undef STATEVARS
55 #undef STATESETUP
56 #undef STATETEARDOWN
57 #undef SETUP
58 #undef onestate
59 #undef INIT
60 #undef INC
61 #undef ISSTATEIN
62 #undef FWD
63 #undef BACK
64 #undef ISSETBACK
65 #undef SNAMES
66
67 /* macros for manipulating states, large version */
68 #define states char *
69 #define CLEAR(v) memset(v, 0, m->g->nstates)
70 #define SET0(v, n) ((v)[n] = 0)
71 #define SET1(v, n) ((v)[n] = 1)
72 #define ISSET(v, n) ((v)[n])
73 #define ASSIGN(d, s) memcpy(d, s, m->g->nstates)
74 #define EQ(a, b) (memcmp(a, b, m->g->nstates) == 0)
75 #define STATEVARS int vn; char *space
76 #define STATESETUP(m, nv) { (m)->space = malloc((nv)*(m)->g->nstates); \
77 if ((m)->space == NULL) return(REG_ESPACE); \
78 (m)->vn = 0; }
79 #define STATETEARDOWN(m) { free((m)->space); }
80 #define SETUP(v) ((v) = &m->space[m->vn++ * m->g->nstates])
81 #define onestate int
82 #define INIT(o, n) ((o) = (n))
83 #define INC(o) ((o)++)
84 #define ISSTATEIN(v, o) ((v)[o])
85 /* some abbreviations; note that some of these know variable names! */
86 /* do "if I'm here, I can also be there" etc without branches */
87 #define FWD(dst, src, n) ((dst)[here+(n)] |= (src)[here])
88 #define BACK(dst, src, n) ((dst)[here-(n)] |= (src)[here])
89 #define ISSETBACK(v, n) ((v)[here - (n)])
90 /* function names */
91 #define LNAMES /* flag */
92
93 #include "engine.c"
94
95 /*
96 - regexec - interface for matching
97 = extern int regexec(const regex_t *, const char *, size_t, \
98 = regmatch_t [], int);
99 = #define REG_NOTBOL 00001
100 = #define REG_NOTEOL 00002
101 = #define REG_STARTEND 00004
102 = #define REG_TRACE 00400 // tracing of execution
103 = #define REG_LARGE 01000 // force large representation
104 = #define REG_BACKR 02000 // force use of backref code
105 *
106 * We put this here so we can exploit knowledge of the state representation
107 * when choosing which matcher to call. Also, by this point the matchers
108 * have been prototyped.
109 */
110 int /* 0 success, REG_NOMATCH failure */
111 regexec(preg, string, nmatch, pmatch, eflags)
112 const regex_t *preg;
113 const char *string;
114 size_t nmatch;
115 regmatch_t pmatch[];
116 int eflags;
117 {
118 register struct re_guts *g = preg->re_g;
119 #ifdef REDEBUG
120 # define GOODFLAGS(f) (f)
121 #else
122 # define GOODFLAGS(f) ((f)&(REG_NOTBOL|REG_NOTEOL|REG_STARTEND))
123 #endif
124
125 if (preg->re_magic != MAGIC1 || g->magic != MAGIC2)
126 return(REG_BADPAT);
127 assert(!(g->iflags&BAD));
128 if (g->iflags&BAD) /* backstop for no-debug case */
129 return(REG_BADPAT);
130 eflags = GOODFLAGS(eflags);
131
132 if (g->nstates <= CHAR_BIT*sizeof(states1) && !(eflags&REG_LARGE))
133 return(smatcher(g, (char *)string, nmatch, pmatch, eflags));
134 else
135 return(lmatcher(g, (char *)string, nmatch, pmatch, eflags));
136 }