/[dtapublic]/projs/trunk/shared_source/c_tcl_base_7_5_w_mods/regc_cvec.c
ViewVC logotype

Diff of /projs/trunk/shared_source/c_tcl_base_7_5_w_mods/regc_cvec.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 64 by dashley, Sun Oct 30 04:21:11 2016 UTC revision 71 by dashley, Sat Nov 5 11:07:06 2016 UTC
# Line 1  Line 1 
 /* $Header$ */  
   
 /*  
  * Utility functions for handling cvecs  
  * This file is #included by regcomp.c.  
  *  
  * Copyright (c) 1998, 1999 Henry Spencer.  All rights reserved.  
  *  
  * Development of this software was funded, in part, by Cray Research Inc.,  
  * UUNET Communications Services Inc., Sun Microsystems Inc., and Scriptics  
  * Corporation, none of whom are responsible for the results.  The author  
  * thanks all of them.  
  *  
  * Redistribution and use in source and binary forms -- with or without  
  * modification -- are permitted for any purpose, provided that  
  * redistributions in source form retain this entire copyright notice and  
  * indicate the origin and nature of any modifications.  
  *  
  * I'd appreciate being given credit for this package in the documentation  
  * of software which uses it, but that is not a requirement.  
  *  
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,  
  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY  
  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL  
  * HENRY SPENCER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  
  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,  
  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;  
  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR  
  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF  
  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
  *  
  */  
   
 /*  
  - newcvec - allocate a new cvec  
  ^ static struct cvec *newcvec(int, int, int);  
  */  
 static struct cvec *  
 newcvec(nchrs, nranges, nmcces)  
 int nchrs;                      /* to hold this many chrs... */  
 int nranges;                    /* ... and this many ranges... */  
 int nmcces;                     /* ... and this many MCCEs */  
 {  
         size_t n;  
         size_t nc;  
         struct cvec *cv;  
   
         nc = (size_t)nchrs + (size_t)nmcces*(MAXMCCE+1) + (size_t)nranges*2;  
         n = sizeof(struct cvec) + (size_t)(nmcces-1)*sizeof(chr *) +  
                                                                 nc*sizeof(chr);  
         cv = (struct cvec *)MALLOC(n);  
         if (cv == NULL)  
                 return NULL;  
         cv->chrspace = nc;  
         cv->chrs = (chr *)&cv->mcces[nmcces];   /* chrs just after MCCE ptrs */  
         cv->mccespace = nmcces;  
         cv->ranges = cv->chrs + nchrs + nmcces*(MAXMCCE+1);  
         cv->rangespace = nranges;  
         return clearcvec(cv);  
 }  
   
 /*  
  - clearcvec - clear a possibly-new cvec  
  * Returns pointer as convenience.  
  ^ static struct cvec *clearcvec(struct cvec *);  
  */  
 static struct cvec *  
 clearcvec(cv)  
 struct cvec *cv;  
 {  
         int i;  
   
         assert(cv != NULL);  
         cv->nchrs = 0;  
         assert(cv->chrs == (chr *)&cv->mcces[cv->mccespace]);  
         cv->nmcces = 0;  
         cv->nmccechrs = 0;  
         cv->nranges = 0;  
         for (i = 0; i < cv->mccespace; i++)  
                 cv->mcces[i] = NULL;  
   
         return cv;  
 }  
   
 /*  
  - addchr - add a chr to a cvec  
  ^ static VOID addchr(struct cvec *, pchr);  
  */  
 static VOID  
 addchr(cv, c)  
 struct cvec *cv;  
 pchr c;  
 {  
         assert(cv->nchrs < cv->chrspace - cv->nmccechrs);  
         cv->chrs[cv->nchrs++] = (chr)c;  
 }  
   
 /*  
  - addrange - add a range to a cvec  
  ^ static VOID addrange(struct cvec *, pchr, pchr);  
  */  
 static VOID  
 addrange(cv, from, to)  
 struct cvec *cv;  
 pchr from;  
 pchr to;  
 {  
         assert(cv->nranges < cv->rangespace);  
         cv->ranges[cv->nranges*2] = (chr)from;  
         cv->ranges[cv->nranges*2 + 1] = (chr)to;  
         cv->nranges++;  
 }  
   
 /*  
  - addmcce - add an MCCE to a cvec  
  ^ static VOID addmcce(struct cvec *, chr *, chr *);  
  */  
 static VOID  
 addmcce(cv, startp, endp)  
 struct cvec *cv;  
 chr *startp;                    /* beginning of text */  
 chr *endp;                      /* just past end of text */  
 {  
         int len;  
         int i;  
         chr *s;  
         chr *d;  
   
         if (startp == NULL && endp == NULL)  
                 return;  
         len = endp - startp;  
         assert(len > 0);  
         assert(cv->nchrs + len < cv->chrspace - cv->nmccechrs);  
         assert(cv->nmcces < cv->mccespace);  
         d = &cv->chrs[cv->chrspace - cv->nmccechrs - len - 1];  
         cv->mcces[cv->nmcces++] = d;  
         for (s = startp, i = len; i > 0; s++, i--)  
                 *d++ = *s;  
         *d++ = 0;               /* endmarker */  
         assert(d == &cv->chrs[cv->chrspace - cv->nmccechrs]);  
         cv->nmccechrs += len + 1;  
 }  
   
 /*  
  - haschr - does a cvec contain this chr?  
  ^ static int haschr(struct cvec *, pchr);  
  */  
 static int                      /* predicate */  
 haschr(cv, c)  
 struct cvec *cv;  
 pchr c;  
 {  
         int i;  
         chr *p;  
   
         for (p = cv->chrs, i = cv->nchrs; i > 0; p++, i--)  
                 if (*p == c)  
                         return 1;  
         for (p = cv->ranges, i = cv->nranges; i > 0; p += 2, i--)  
                 if (*p <= c && c <= *(p+1))  
                         return 1;  
         return 0;  
 }  
   
 /*  
  - getcvec - get a cvec, remembering it as v->cv  
  ^ static struct cvec *getcvec(struct vars *, int, int, int);  
  */  
 static struct cvec *  
 getcvec(v, nchrs, nranges, nmcces)  
 struct vars *v;  
 int nchrs;                      /* to hold this many chrs... */  
 int nranges;                    /* ... and this many ranges... */  
 int nmcces;                     /* ... and this many MCCEs */  
 {  
         if (v->cv != NULL && nchrs <= v->cv->chrspace &&  
                                         nranges <= v->cv->rangespace &&  
                                         nmcces <= v->cv->mccespace)  
                 return clearcvec(v->cv);  
   
         if (v->cv != NULL)  
                 freecvec(v->cv);  
         v->cv = newcvec(nchrs, nranges, nmcces);  
         if (v->cv == NULL)  
                 ERR(REG_ESPACE);  
   
         return v->cv;  
 }  
   
 /*  
  - freecvec - free a cvec  
  ^ static VOID freecvec(struct cvec *);  
  */  
 static VOID  
 freecvec(cv)  
 struct cvec *cv;  
 {  
         FREE(cv);  
 }  
   
 /* $History: regc_cvec.c $  
  *  
  * *****************  Version 1  *****************  
  * User: Dtashley     Date: 1/02/01    Time: 12:03a  
  * Created in $/IjuScripter, IjuConsole/Source/Tcl Base  
  * Initial check-in.  
  */  
   
 /* End of REGC_CVEC.C */  
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 */

Legend:
Removed from v.64  
changed lines
  Added in v.71

dashley@gmail.com
ViewVC Help
Powered by ViewVC 1.1.25