1 /* ccl - routines for character classes */
3 /* Copyright (c) 1990 The Regents of the University of California. */
4 /* All rights reserved. */
6 /* This code is derived from software contributed to Berkeley by */
9 /* The United States Government has rights in this work pursuant */
10 /* to contract no. DE-AC03-76SF00098 between the United States */
11 /* Department of Energy and the University of California. */
13 /* This file is part of flex. */
15 /* Redistribution and use in source and binary forms, with or without */
16 /* modification, are permitted provided that the following conditions */
19 /* 1. Redistributions of source code must retain the above copyright */
20 /* notice, this list of conditions and the following disclaimer. */
21 /* 2. Redistributions in binary form must reproduce the above copyright */
22 /* notice, this list of conditions and the following disclaimer in the */
23 /* documentation and/or other materials provided with the distribution. */
25 /* Neither the name of the University nor the names of its contributors */
26 /* may be used to endorse or promote products derived from this software */
27 /* without specific prior written permission. */
29 /* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR */
30 /* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */
31 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
36 /* ccladd - add a single character to a ccl */
38 void ccladd (cclp
, ch
)
42 int ind
, len
, newpos
, i
;
49 /* check to see if the character is already in the ccl */
51 for (i
= 0; i
< len
; ++i
)
52 if (ccltbl
[ind
+ i
] == ch
)
57 ccl_has_nl
[cclp
] = true;
61 if (newpos
>= current_max_ccl_tbl_size
) {
62 current_max_ccl_tbl_size
+= MAX_CCL_TBL_SIZE_INCREMENT
;
66 ccltbl
= reallocate_Character_array (ccltbl
,
67 current_max_ccl_tbl_size
);
70 ccllen
[cclp
] = len
+ 1;
75 /* cclinit - return an empty ccl */
79 if (++lastccl
>= current_maxccls
) {
80 current_maxccls
+= MAX_CCLS_INCREMENT
;
85 reallocate_integer_array (cclmap
, current_maxccls
);
87 reallocate_integer_array (ccllen
, current_maxccls
);
88 cclng
= reallocate_integer_array (cclng
, current_maxccls
);
90 reallocate_bool_array (ccl_has_nl
,
95 /* we're making the first ccl */
99 /* The new pointer is just past the end of the last ccl.
100 * Since the cclmap points to the \first/ character of a
101 * ccl, adding the length of the ccl to the cclmap pointer
102 * will produce a cursor to the first free space.
105 cclmap
[lastccl
- 1] + ccllen
[lastccl
- 1];
108 cclng
[lastccl
] = 0; /* ccl's start out life un-negated */
109 ccl_has_nl
[lastccl
] = false;
115 /* cclnegate - negate the given ccl */
117 void cclnegate (cclp
)
121 ccl_has_nl
[cclp
] = !ccl_has_nl
[cclp
];
125 /* list_character_set - list the members of a set of characters in CCL form
127 * Writes to the given file a character-class representation of those
128 * characters present in the given CCL. A character is present if it
129 * has a non-zero value in the cset array.
132 void list_character_set (file
, cset
)
140 for (i
= 0; i
< csize
; ++i
) {
142 register int start_char
= i
;
146 fputs (readable_form (i
), file
);
148 while (++i
< csize
&& cset
[i
]) ;
150 if (i
- 1 > start_char
)
152 fprintf (file
, "-%s",
153 readable_form (i
- 1));
162 /** Determines if the range [c1-c2] is unambiguous in a case-insensitive
163 * scanner. Specifically, if a lowercase or uppercase character, x, is in the
164 * range [c1-c2], then we require that UPPERCASE(x) and LOWERCASE(x) must also
165 * be in the range. If not, then this range is ambiguous, and the function
166 * returns false. For example, [@-_] spans [a-z] but not [A-Z]. Beware that
167 * [a-z] will be labeled ambiguous because it does not include [A-Z].
169 * @param c1 the lower end of the range
170 * @param c2 the upper end of the range
171 * @return true if [c1-c2] is not ambiguous for a caseless scanner.
173 bool range_covers_case (int c1
, int c2
)
177 for (i
= c1
; i
<= c2
; i
++) {
179 o
= reverse_case (i
);
180 if (o
< c1
|| c2
< o
)
187 /** Reverse the case of a character, if possible.
188 * @return c if case-reversal does not apply.
190 int reverse_case (int c
)
192 return isupper (c
) ? tolower (c
) : (islower (c
) ? toupper (c
) : c
);
195 /** Return true if c is uppercase or lowercase. */
196 bool has_case (int c
)
198 return (isupper (c
) || islower (c
)) ? true : false;