Pristine Ack-5.5
[Ack-5.5.git] / util / flex / ccl.c
1 /* ccl - routines for character classes */
2
3 /*-
4  * Copyright (c) 1990 The Regents of the University of California.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Vern Paxson.
9  * 
10  * The United States Government has rights in this work pursuant
11  * to contract no. DE-AC03-76SF00098 between the United States
12  * Department of Energy and the University of California.
13  *
14  * Redistribution and use in source and binary forms are permitted provided
15  * that: (1) source distributions retain this entire copyright notice and
16  * comment, and (2) distributions including binaries display the following
17  * acknowledgement:  ``This product includes software developed by the
18  * University of California, Berkeley and its contributors'' in the
19  * documentation or other materials provided with the distribution and in
20  * all advertising materials mentioning features or use of this software.
21  * Neither the name of the University nor the names of its contributors may
22  * be used to endorse or promote products derived from this software without
23  * specific prior written permission.
24  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
25  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
26  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27  */
28
29 #ifndef lint
30 static char rcsid[] =
31     "@(#) $Id: ccl.c,v 1.2 1994/06/24 10:56:38 ceriel Exp $ (LBL)";
32 #endif
33
34 #include "flexdef.h"
35
36 /* ccladd - add a single character to a ccl
37  *
38  * synopsis
39  *    int cclp;
40  *    int ch;
41  *    ccladd( cclp, ch );
42  */
43
44 void ccladd( cclp, ch )
45 int cclp;
46 int ch;
47
48     {
49     int ind, len, newpos, i;
50
51     len = ccllen[cclp];
52     ind = cclmap[cclp];
53
54     /* check to see if the character is already in the ccl */
55
56     for ( i = 0; i < len; ++i )
57         if ( ccltbl[ind + i] == ch )
58             return;
59
60     newpos = ind + len;
61
62     if ( newpos >= current_max_ccl_tbl_size )
63         {
64         current_max_ccl_tbl_size += MAX_CCL_TBL_SIZE_INCREMENT;
65
66         ++num_reallocs;
67
68         ccltbl = reallocate_character_array( ccltbl, current_max_ccl_tbl_size );
69         }
70
71     ccllen[cclp] = len + 1;
72     ccltbl[newpos] = ch;
73     }
74
75
76 /* cclinit - make an empty ccl
77  *
78  * synopsis
79  *    int cclinit();
80  *    new_ccl = cclinit();
81  */
82
83 int cclinit()
84
85     {
86     if ( ++lastccl >= current_maxccls )
87         {
88         current_maxccls += MAX_CCLS_INCREMENT;
89
90         ++num_reallocs;
91
92         cclmap = reallocate_integer_array( cclmap, current_maxccls );
93         ccllen = reallocate_integer_array( ccllen, current_maxccls );
94         cclng = reallocate_integer_array( cclng, current_maxccls );
95         }
96
97     if ( lastccl == 1 )
98         /* we're making the first ccl */
99         cclmap[lastccl] = 0;
100
101     else
102         /* the new pointer is just past the end of the last ccl.  Since
103          * the cclmap points to the \first/ character of a ccl, adding the
104          * length of the ccl to the cclmap pointer will produce a cursor
105          * to the first free space
106          */
107         cclmap[lastccl] = cclmap[lastccl - 1] + ccllen[lastccl - 1];
108
109     ccllen[lastccl] = 0;
110     cclng[lastccl] = 0; /* ccl's start out life un-negated */
111
112     return ( lastccl );
113     }
114
115
116 /* cclnegate - negate a ccl
117  *
118  * synopsis
119  *    int cclp;
120  *    cclnegate( ccl );
121  */
122
123 void cclnegate( cclp )
124 int cclp;
125
126     {
127     cclng[cclp] = 1;
128     }
129
130
131 /* list_character_set - list the members of a set of characters in CCL form
132  *
133  * synopsis
134  *     int cset[CSIZE];
135  *     FILE *file;
136  *     list_character_set( cset );
137  *
138  * writes to the given file a character-class representation of those
139  * characters present in the given set.  A character is present if it
140  * has a non-zero value in the set array.
141  */
142
143 void list_character_set( file, cset )
144 FILE *file;
145 int cset[];
146
147     {
148     register int i;
149     char *readable_form();
150
151     putc( '[', file );
152
153     for ( i = 0; i < csize; ++i )
154         {
155         if ( cset[i] )
156             {
157             register int start_char = i;
158
159             putc( ' ', file );
160
161             fputs( readable_form( i ), file );
162
163             while ( ++i < csize && cset[i] )
164                 ;
165
166             if ( i - 1 > start_char )
167                 /* this was a run */
168                 fprintf( file, "-%s", readable_form( i - 1 ) );
169
170             putc( ' ', file );
171             }
172         }
173
174     putc( ']', file );
175     }