1 |
/* $Header$ */ |
2 |
/* |
3 |
* Utility functions for handling cvecs |
4 |
* This file is #included by regcomp.c. |
5 |
* |
6 |
* Copyright (c) 1998, 1999 Henry Spencer. All rights reserved. |
7 |
* |
8 |
* Development of this software was funded, in part, by Cray Research Inc., |
9 |
* UUNET Communications Services Inc., Sun Microsystems Inc., and Scriptics |
10 |
* Corporation, none of whom are responsible for the results. The author |
11 |
* thanks all of them. |
12 |
* |
13 |
* Redistribution and use in source and binary forms -- with or without |
14 |
* modification -- are permitted for any purpose, provided that |
15 |
* redistributions in source form retain this entire copyright notice and |
16 |
* indicate the origin and nature of any modifications. |
17 |
* |
18 |
* I'd appreciate being given credit for this package in the documentation |
19 |
* of software which uses it, but that is not a requirement. |
20 |
* |
21 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, |
22 |
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY |
23 |
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
24 |
* HENRY SPENCER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
25 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
26 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
27 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
28 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
29 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
30 |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
31 |
* |
32 |
*/ |
33 |
|
34 |
/* |
35 |
- newcvec - allocate a new cvec |
36 |
^ static struct cvec *newcvec(int, int, int); |
37 |
*/ |
38 |
static struct cvec * |
39 |
newcvec(nchrs, nranges, nmcces) |
40 |
int nchrs; /* to hold this many chrs... */ |
41 |
int nranges; /* ... and this many ranges... */ |
42 |
int nmcces; /* ... and this many MCCEs */ |
43 |
{ |
44 |
size_t n; |
45 |
size_t nc; |
46 |
struct cvec *cv; |
47 |
|
48 |
nc = (size_t)nchrs + (size_t)nmcces*(MAXMCCE+1) + (size_t)nranges*2; |
49 |
n = sizeof(struct cvec) + (size_t)(nmcces-1)*sizeof(chr *) + |
50 |
nc*sizeof(chr); |
51 |
cv = (struct cvec *)MALLOC(n); |
52 |
if (cv == NULL) |
53 |
return NULL; |
54 |
cv->chrspace = nc; |
55 |
cv->chrs = (chr *)&cv->mcces[nmcces]; /* chrs just after MCCE ptrs */ |
56 |
cv->mccespace = nmcces; |
57 |
cv->ranges = cv->chrs + nchrs + nmcces*(MAXMCCE+1); |
58 |
cv->rangespace = nranges; |
59 |
return clearcvec(cv); |
60 |
} |
61 |
|
62 |
/* |
63 |
- clearcvec - clear a possibly-new cvec |
64 |
* Returns pointer as convenience. |
65 |
^ static struct cvec *clearcvec(struct cvec *); |
66 |
*/ |
67 |
static struct cvec * |
68 |
clearcvec(cv) |
69 |
struct cvec *cv; |
70 |
{ |
71 |
int i; |
72 |
|
73 |
assert(cv != NULL); |
74 |
cv->nchrs = 0; |
75 |
assert(cv->chrs == (chr *)&cv->mcces[cv->mccespace]); |
76 |
cv->nmcces = 0; |
77 |
cv->nmccechrs = 0; |
78 |
cv->nranges = 0; |
79 |
for (i = 0; i < cv->mccespace; i++) |
80 |
cv->mcces[i] = NULL; |
81 |
|
82 |
return cv; |
83 |
} |
84 |
|
85 |
/* |
86 |
- addchr - add a chr to a cvec |
87 |
^ static VOID addchr(struct cvec *, pchr); |
88 |
*/ |
89 |
static VOID |
90 |
addchr(cv, c) |
91 |
struct cvec *cv; |
92 |
pchr c; |
93 |
{ |
94 |
assert(cv->nchrs < cv->chrspace - cv->nmccechrs); |
95 |
cv->chrs[cv->nchrs++] = (chr)c; |
96 |
} |
97 |
|
98 |
/* |
99 |
- addrange - add a range to a cvec |
100 |
^ static VOID addrange(struct cvec *, pchr, pchr); |
101 |
*/ |
102 |
static VOID |
103 |
addrange(cv, from, to) |
104 |
struct cvec *cv; |
105 |
pchr from; |
106 |
pchr to; |
107 |
{ |
108 |
assert(cv->nranges < cv->rangespace); |
109 |
cv->ranges[cv->nranges*2] = (chr)from; |
110 |
cv->ranges[cv->nranges*2 + 1] = (chr)to; |
111 |
cv->nranges++; |
112 |
} |
113 |
|
114 |
/* |
115 |
- addmcce - add an MCCE to a cvec |
116 |
^ static VOID addmcce(struct cvec *, chr *, chr *); |
117 |
*/ |
118 |
static VOID |
119 |
addmcce(cv, startp, endp) |
120 |
struct cvec *cv; |
121 |
chr *startp; /* beginning of text */ |
122 |
chr *endp; /* just past end of text */ |
123 |
{ |
124 |
int len; |
125 |
int i; |
126 |
chr *s; |
127 |
chr *d; |
128 |
|
129 |
if (startp == NULL && endp == NULL) |
130 |
return; |
131 |
len = endp - startp; |
132 |
assert(len > 0); |
133 |
assert(cv->nchrs + len < cv->chrspace - cv->nmccechrs); |
134 |
assert(cv->nmcces < cv->mccespace); |
135 |
d = &cv->chrs[cv->chrspace - cv->nmccechrs - len - 1]; |
136 |
cv->mcces[cv->nmcces++] = d; |
137 |
for (s = startp, i = len; i > 0; s++, i--) |
138 |
*d++ = *s; |
139 |
*d++ = 0; /* endmarker */ |
140 |
assert(d == &cv->chrs[cv->chrspace - cv->nmccechrs]); |
141 |
cv->nmccechrs += len + 1; |
142 |
} |
143 |
|
144 |
/* |
145 |
- haschr - does a cvec contain this chr? |
146 |
^ static int haschr(struct cvec *, pchr); |
147 |
*/ |
148 |
static int /* predicate */ |
149 |
haschr(cv, c) |
150 |
struct cvec *cv; |
151 |
pchr c; |
152 |
{ |
153 |
int i; |
154 |
chr *p; |
155 |
|
156 |
for (p = cv->chrs, i = cv->nchrs; i > 0; p++, i--) |
157 |
if (*p == c) |
158 |
return 1; |
159 |
for (p = cv->ranges, i = cv->nranges; i > 0; p += 2, i--) |
160 |
if (*p <= c && c <= *(p+1)) |
161 |
return 1; |
162 |
return 0; |
163 |
} |
164 |
|
165 |
/* |
166 |
- getcvec - get a cvec, remembering it as v->cv |
167 |
^ static struct cvec *getcvec(struct vars *, int, int, int); |
168 |
*/ |
169 |
static struct cvec * |
170 |
getcvec(v, nchrs, nranges, nmcces) |
171 |
struct vars *v; |
172 |
int nchrs; /* to hold this many chrs... */ |
173 |
int nranges; /* ... and this many ranges... */ |
174 |
int nmcces; /* ... and this many MCCEs */ |
175 |
{ |
176 |
if (v->cv != NULL && nchrs <= v->cv->chrspace && |
177 |
nranges <= v->cv->rangespace && |
178 |
nmcces <= v->cv->mccespace) |
179 |
return clearcvec(v->cv); |
180 |
|
181 |
if (v->cv != NULL) |
182 |
freecvec(v->cv); |
183 |
v->cv = newcvec(nchrs, nranges, nmcces); |
184 |
if (v->cv == NULL) |
185 |
ERR(REG_ESPACE); |
186 |
|
187 |
return v->cv; |
188 |
} |
189 |
|
190 |
/* |
191 |
- freecvec - free a cvec |
192 |
^ static VOID freecvec(struct cvec *); |
193 |
*/ |
194 |
static VOID |
195 |
freecvec(cv) |
196 |
struct cvec *cv; |
197 |
{ |
198 |
FREE(cv); |
199 |
} |
200 |
|
201 |
/* End of regc_cvec.c */ |