PostgreSQL Source Code git master
Loading...
Searching...
No Matches
regc_cvec.c
Go to the documentation of this file.
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
18 * of 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 * src/backend/regex/regc_cvec.c
32 *
33 */
34
35/*
36 * Notes:
37 * Only (selected) functions in _this_ file should treat the chr arrays
38 * of a cvec as non-constant.
39 */
40
41/*
42 * newcvec - allocate a new cvec
43 *
44 * Note: in current usage, nchrs and nranges are never so large that we risk
45 * integer overflow in these size calculations, even with 32-bit size_t.
46 */
47static struct cvec *
48newcvec(int nchrs, /* to hold this many chrs... */
49 int nranges) /* ... and this many ranges */
50{
51 size_t nc = (size_t) nchrs + (size_t) nranges * 2;
52 size_t n = sizeof(struct cvec) + nc * sizeof(chr);
53 struct cvec *cv = (struct cvec *) MALLOC(n);
54
55 if (cv == NULL)
56 return NULL;
57 cv->chrspace = nchrs;
58 cv->chrs = (chr *) (((char *) cv) + sizeof(struct cvec));
59 cv->ranges = cv->chrs + nchrs;
60 cv->rangespace = nranges;
61 return clearcvec(cv);
62}
63
64/*
65 * clearcvec - clear a possibly-new cvec
66 * Returns pointer as convenience.
67 */
68static struct cvec *
69clearcvec(struct cvec *cv)
70{
71 assert(cv != NULL);
72 cv->nchrs = 0;
73 cv->nranges = 0;
74 cv->cclasscode = -1;
75 return cv;
76}
77
78/*
79 * addchr - add a chr to a cvec
80 */
81static void
82addchr(struct cvec *cv, /* character vector */
83 chr c) /* character to add */
84{
85 assert(cv->nchrs < cv->chrspace);
86 cv->chrs[cv->nchrs++] = c;
87}
88
89/*
90 * addrange - add a range to a cvec
91 */
92static void
93addrange(struct cvec *cv, /* character vector */
94 chr from, /* first character of range */
95 chr to) /* last character of range */
96{
97 assert(cv->nranges < cv->rangespace);
98 cv->ranges[cv->nranges * 2] = from;
99 cv->ranges[cv->nranges * 2 + 1] = to;
100 cv->nranges++;
101}
102
103/*
104 * getcvec - get a transient cvec, initialized to empty
105 *
106 * The returned cvec is valid only until the next call of getcvec, which
107 * typically will recycle the space. Callers should *not* free the cvec
108 * explicitly; it will be cleaned up when the struct vars is destroyed.
109 *
110 * This is typically used while interpreting bracket expressions. In that
111 * usage the cvec is only needed momentarily until we build arcs from it,
112 * so transientness is a convenient behavior.
113 */
114static struct cvec *
115getcvec(struct vars *v, /* context */
116 int nchrs, /* to hold this many chrs... */
117 int nranges) /* ... and this many ranges */
118{
119 /* recycle existing transient cvec if large enough */
120 if (v->cv != NULL && nchrs <= v->cv->chrspace &&
121 nranges <= v->cv->rangespace)
122 return clearcvec(v->cv);
123
124 /* nope, make a new one */
125 if (v->cv != NULL)
126 freecvec(v->cv);
127 v->cv = newcvec(nchrs, nranges);
128 if (v->cv == NULL)
130
131 return v->cv;
132}
133
134/*
135 * freecvec - free a cvec
136 */
137static void
138freecvec(struct cvec *cv)
139{
140 FREE(cv);
141}
#define ERR
Definition _int.h:161
#define FREE(ptr)
Definition cryptohash.c:37
char * c
static int fb(int x)
static void freecvec(struct cvec *cv)
Definition regc_cvec.c:138
static struct cvec * newcvec(int nchrs, int nranges)
Definition regc_cvec.c:48
static void addchr(struct cvec *cv, chr c)
Definition regc_cvec.c:82
static void addrange(struct cvec *cv, chr from, chr to)
Definition regc_cvec.c:93
static struct cvec * clearcvec(struct cvec *cv)
Definition regc_cvec.c:69
static struct cvec * getcvec(struct vars *v, int nchrs, int nranges)
Definition regc_cvec.c:115
#define MALLOC(n)
Definition regcustom.h:52
pg_wchar chr
Definition regcustom.h:62
#define assert(x)
Definition regcustom.h:59
#define REG_ESPACE
Definition regex.h:227
int chrspace
Definition regguts.h:289
int nchrs
Definition regguts.h:288
int rangespace
Definition regguts.h:292
chr * chrs
Definition regguts.h:290
chr * ranges
Definition regguts.h:293
int cclasscode
Definition regguts.h:294
int nranges
Definition regguts.h:291
struct cvec * cv
Definition regcomp.c:304