]> git.saurik.com Git - apple/libc.git/blob - regex.subproj/regex2.h
d68f62b30bb41992ed69747c951c4ed043f1ce82
[apple/libc.git] / regex.subproj / regex2.h
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * Copyright (c) 1992, 1993, 1994
24 * The Regents of the University of California. All rights reserved.
25 *
26 * This code is derived from software contributed to Berkeley by
27 * Henry Spencer.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * SUCH DAMAGE.
56 */
57
58 /*
59 * First, the stuff that ends up in the outside-world include file
60 = typedef off_t regoff_t;
61 = typedef struct {
62 = int re_magic;
63 = size_t re_nsub; // number of parenthesized subexpressions
64 = const char *re_endp; // end pointer for REG_PEND
65 = struct re_guts *re_g; // none of your business :-)
66 = } regex_t;
67 = typedef struct {
68 = regoff_t rm_so; // start of match
69 = regoff_t rm_eo; // end of match
70 = } regmatch_t;
71 */
72 /*
73 * internals of regex_t
74 */
75 #define MAGIC1 ((('r'^0200)<<8) | 'e')
76
77 /*
78 * The internal representation is a *strip*, a sequence of
79 * operators ending with an endmarker. (Some terminology etc. is a
80 * historical relic of earlier versions which used multiple strips.)
81 * Certain oddities in the representation are there to permit running
82 * the machinery backwards; in particular, any deviation from sequential
83 * flow must be marked at both its source and its destination. Some
84 * fine points:
85 *
86 * - OPLUS_ and O_PLUS are *inside* the loop they create.
87 * - OQUEST_ and O_QUEST are *outside* the bypass they create.
88 * - OCH_ and O_CH are *outside* the multi-way branch they create, while
89 * OOR1 and OOR2 are respectively the end and the beginning of one of
90 * the branches. Note that there is an implicit OOR2 following OCH_
91 * and an implicit OOR1 preceding O_CH.
92 *
93 * In state representations, an operator's bit is on to signify a state
94 * immediately *preceding* "execution" of that operator.
95 */
96 typedef unsigned long sop; /* strip operator */
97 typedef long sopno;
98 #define OPRMASK 0xf8000000
99 #define OPDMASK 0x07ffffff
100 #define OPSHIFT ((unsigned)27)
101 #define OP(n) ((n)&OPRMASK)
102 #define OPND(n) ((n)&OPDMASK)
103 #define SOP(op, opnd) ((op)|(opnd))
104 /* operators meaning operand */
105 /* (back, fwd are offsets) */
106 #define OEND (1<<OPSHIFT) /* endmarker - */
107 #define OCHAR (2<<OPSHIFT) /* character unsigned char */
108 #define OBOL (3<<OPSHIFT) /* left anchor - */
109 #define OEOL (4<<OPSHIFT) /* right anchor - */
110 #define OANY (5<<OPSHIFT) /* . - */
111 #define OANYOF (6<<OPSHIFT) /* [...] set number */
112 #define OBACK_ (7<<OPSHIFT) /* begin \d paren number */
113 #define O_BACK (8<<OPSHIFT) /* end \d paren number */
114 #define OPLUS_ (9<<OPSHIFT) /* + prefix fwd to suffix */
115 #define O_PLUS (10<<OPSHIFT) /* + suffix back to prefix */
116 #define OQUEST_ (11<<OPSHIFT) /* ? prefix fwd to suffix */
117 #define O_QUEST (12<<OPSHIFT) /* ? suffix back to prefix */
118 #define OLPAREN (13<<OPSHIFT) /* ( fwd to ) */
119 #define ORPAREN (14<<OPSHIFT) /* ) back to ( */
120 #define OCH_ (15<<OPSHIFT) /* begin choice fwd to OOR2 */
121 #define OOR1 (16<<OPSHIFT) /* | pt. 1 back to OOR1 or OCH_ */
122 #define OOR2 (17<<OPSHIFT) /* | pt. 2 fwd to OOR2 or O_CH */
123 #define O_CH (18<<OPSHIFT) /* end choice back to OOR1 */
124 #define OBOW (19<<OPSHIFT) /* begin word - */
125 #define OEOW (20<<OPSHIFT) /* end word - */
126
127 /*
128 * Structure for [] character-set representation. Character sets are
129 * done as bit vectors, grouped 8 to a byte vector for compactness.
130 * The individual set therefore has both a pointer to the byte vector
131 * and a mask to pick out the relevant bit of each byte. A hash code
132 * simplifies testing whether two sets could be identical.
133 *
134 * This will get trickier for multicharacter collating elements. As
135 * preliminary hooks for dealing with such things, we also carry along
136 * a string of multi-character elements, and decide the size of the
137 * vectors at run time.
138 */
139 typedef struct {
140 uch *ptr; /* -> uch [csetsize] */
141 uch mask; /* bit within array */
142 uch hash; /* hash code */
143 size_t smultis;
144 char *multis; /* -> char[smulti] ab\0cd\0ef\0\0 */
145 } cset;
146 /* note that CHadd and CHsub are unsafe, and CHIN doesn't yield 0/1 */
147 #define CHadd(cs, c) ((cs)->ptr[(uch)(c)] |= (cs)->mask, (cs)->hash += (c))
148 #define CHsub(cs, c) ((cs)->ptr[(uch)(c)] &= ~(cs)->mask, (cs)->hash -= (c))
149 #define CHIN(cs, c) ((cs)->ptr[(uch)(c)] & (cs)->mask)
150 #define MCadd(p, cs, cp) mcadd(p, cs, cp) /* regcomp() internal fns */
151 #define MCsub(p, cs, cp) mcsub(p, cs, cp)
152 #define MCin(p, cs, cp) mcin(p, cs, cp)
153
154 /* stuff for character categories */
155 typedef unsigned char cat_t;
156
157 /*
158 * main compiled-expression structure
159 */
160 struct re_guts {
161 int magic;
162 # define MAGIC2 ((('R'^0200)<<8)|'E')
163 sop *strip; /* malloced area for strip */
164 int csetsize; /* number of bits in a cset vector */
165 int ncsets; /* number of csets in use */
166 cset *sets; /* -> cset [ncsets] */
167 uch *setbits; /* -> uch[csetsize][ncsets/CHAR_BIT] */
168 int cflags; /* copy of regcomp() cflags argument */
169 sopno nstates; /* = number of sops */
170 sopno firststate; /* the initial OEND (normally 0) */
171 sopno laststate; /* the final OEND */
172 int iflags; /* internal flags */
173 # define USEBOL 01 /* used ^ */
174 # define USEEOL 02 /* used $ */
175 # define BAD 04 /* something wrong */
176 int nbol; /* number of ^ used */
177 int neol; /* number of $ used */
178 int ncategories; /* how many character categories */
179 cat_t *categories; /* ->catspace[-CHAR_MIN] */
180 char *must; /* match must contain this string */
181 int mlen; /* length of must */
182 size_t nsub; /* copy of re_nsub */
183 int backrefs; /* does it use back references? */
184 sopno nplus; /* how deep does it nest +s? */
185 /* catspace must be last */
186 cat_t catspace[1]; /* actually [NC] */
187 };
188
189 /* misc utilities */
190 #define OUT (CHAR_MAX+1) /* a non-character value */
191 #define ISWORD(c) (isalnum(c) || (c) == '_')