ccl.c revision 50488
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/* $FreeBSD: head/usr.bin/lex/ccl.c 50488 1999-08-28 05:11:36Z peter $ */
30
31#include "flexdef.h"
32
33/* ccladd - add a single character to a ccl */
34
35void ccladd( cclp, ch )
36int cclp;
37int ch;
38	{
39	int ind, len, newpos, i;
40
41	check_char( ch );
42
43	len = ccllen[cclp];
44	ind = cclmap[cclp];
45
46	/* check to see if the character is already in the ccl */
47
48	for ( i = 0; i < len; ++i )
49		if ( ccltbl[ind + i] == ch )
50			return;
51
52	newpos = ind + len;
53
54	if ( newpos >= current_max_ccl_tbl_size )
55		{
56		current_max_ccl_tbl_size += MAX_CCL_TBL_SIZE_INCREMENT;
57
58		++num_reallocs;
59
60		ccltbl = reallocate_Character_array( ccltbl,
61						current_max_ccl_tbl_size );
62		}
63
64	ccllen[cclp] = len + 1;
65	ccltbl[newpos] = ch;
66	}
67
68
69/* cclinit - return an empty ccl */
70
71int cclinit()
72	{
73	if ( ++lastccl >= current_maxccls )
74		{
75		current_maxccls += MAX_CCLS_INCREMENT;
76
77		++num_reallocs;
78
79		cclmap = reallocate_integer_array( cclmap, current_maxccls );
80		ccllen = reallocate_integer_array( ccllen, current_maxccls );
81		cclng = reallocate_integer_array( cclng, current_maxccls );
82		}
83
84	if ( lastccl == 1 )
85		/* we're making the first ccl */
86		cclmap[lastccl] = 0;
87
88	else
89		/* The new pointer is just past the end of the last ccl.
90		 * Since the cclmap points to the \first/ character of a
91		 * ccl, adding the length of the ccl to the cclmap pointer
92		 * will produce a cursor to the first free space.
93		 */
94		cclmap[lastccl] = cclmap[lastccl - 1] + ccllen[lastccl - 1];
95
96	ccllen[lastccl] = 0;
97	cclng[lastccl] = 0;	/* ccl's start out life un-negated */
98
99	return lastccl;
100	}
101
102
103/* cclnegate - negate the given ccl */
104
105void cclnegate( cclp )
106int cclp;
107	{
108	cclng[cclp] = 1;
109	}
110
111
112/* list_character_set - list the members of a set of characters in CCL form
113 *
114 * Writes to the given file a character-class representation of those
115 * characters present in the given CCL.  A character is present if it
116 * has a non-zero value in the cset array.
117 */
118
119void list_character_set( file, cset )
120FILE *file;
121int cset[];
122	{
123	register int i;
124
125	putc( '[', file );
126
127	for ( i = 0; i < csize; ++i )
128		{
129		if ( cset[i] )
130			{
131			register int start_char = i;
132
133			putc( ' ', file );
134
135			fputs( readable_form( i ), file );
136
137			while ( ++i < csize && cset[i] )
138				;
139
140			if ( i - 1 > start_char )
141				/* this was a run */
142				fprintf( file, "-%s", readable_form( i - 1 ) );
143
144			putc( ' ', file );
145			}
146		}
147
148	putc( ']', file );
149	}
150