~ubuntu-branches/ubuntu/raring/tcl8.5/raring

« back to all changes in this revision

Viewing changes to generic/regc_cvec.c

  • Committer: Bazaar Package Importer
  • Author(s): Sergei Golovan
  • Date: 2007-10-26 22:04:32 UTC
  • Revision ID: james.westby@ubuntu.com-20071026220432-57je4z35i4ll6uit
Tags: upstream-0.b2
ImportĀ upstreamĀ versionĀ 0.b2

Show diffs side-by-side

added added

removed removed

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