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

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

dashley@gmail.com
ViewVC Help
Powered by ViewVC 1.1.25