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 chr* as non-constant.
38  */
39 
40 /*
41  * newcvec - allocate a new cvec
42  */
43 static struct cvec *
newcvec(int nchrs,int nranges)44 newcvec(int nchrs,				/* to hold this many chrs... */
45 		int nranges)			/* ... and this many ranges */
46 {
47 	size_t		nc = (size_t) nchrs + (size_t) nranges * 2;
48 	size_t		n = sizeof(struct cvec) + nc * sizeof(chr);
49 	struct cvec *cv = (struct cvec *) MALLOC(n);
50 
51 	if (cv == NULL)
52 		return NULL;
53 	cv->chrspace = nchrs;
54 	cv->chrs = (chr *) (((char *) cv) + sizeof(struct cvec));
55 	cv->ranges = cv->chrs + nchrs;
56 	cv->rangespace = nranges;
57 	return clearcvec(cv);
58 }
59 
60 /*
61  * clearcvec - clear a possibly-new cvec
62  * Returns pointer as convenience.
63  */
64 static struct cvec *
clearcvec(struct cvec * cv)65 clearcvec(struct cvec * cv)
66 {
67 	assert(cv != NULL);
68 	cv->nchrs = 0;
69 	cv->nranges = 0;
70 	return cv;
71 }
72 
73 /*
74  * addchr - add a chr to a cvec
75  */
76 static void
addchr(struct cvec * cv,chr c)77 addchr(struct cvec * cv,		/* character vector */
78 	   chr c)					/* character to add */
79 {
80 	assert(cv->nchrs < cv->chrspace);
81 	cv->chrs[cv->nchrs++] = (chr) c;
82 }
83 
84 /*
85  * addrange - add a range to a cvec
86  */
87 static void
addrange(struct cvec * cv,chr from,chr to)88 addrange(struct cvec * cv,		/* character vector */
89 		 chr from,				/* first character of range */
90 		 chr to)				/* last character of range */
91 {
92 	assert(cv->nranges < cv->rangespace);
93 	cv->ranges[cv->nranges * 2] = (chr) from;
94 	cv->ranges[cv->nranges * 2 + 1] = (chr) to;
95 	cv->nranges++;
96 }
97 
98 /*
99  * getcvec - get a transient cvec, initialized to empty
100  *
101  * The returned cvec is valid only until the next call of getcvec, which
102  * typically will recycle the space.  Callers should *not* free the cvec
103  * explicitly; it will be cleaned up when the struct vars is destroyed.
104  *
105  * This is typically used while interpreting bracket expressions.  In that
106  * usage the cvec is only needed momentarily until we build arcs from it,
107  * so transientness is a convenient behavior.
108  */
109 static struct cvec *
getcvec(struct vars * v,int nchrs,int nranges)110 getcvec(struct vars * v,		/* context */
111 		int nchrs,				/* to hold this many chrs... */
112 		int nranges)			/* ... and this many ranges */
113 {
114 	/* recycle existing transient cvec if large enough */
115 	if (v->cv != NULL && nchrs <= v->cv->chrspace &&
116 		nranges <= v->cv->rangespace)
117 		return clearcvec(v->cv);
118 
119 	/* nope, make a new one */
120 	if (v->cv != NULL)
121 		freecvec(v->cv);
122 	v->cv = newcvec(nchrs, nranges);
123 	if (v->cv == NULL)
124 		ERR(REG_ESPACE);
125 
126 	return v->cv;
127 }
128 
129 /*
130  * freecvec - free a cvec
131  */
132 static void
freecvec(struct cvec * cv)133 freecvec(struct cvec * cv)
134 {
135 	FREE(cv);
136 }
137