]>
Commit | Line | Data |
---|---|---|
b75a7d8f A |
1 | // |
2 | // regexst.h | |
3 | // | |
374ca955 | 4 | // Copyright (C) 2004, International Business Machines Corporation and others. |
b75a7d8f A |
5 | // All Rights Reserved. |
6 | // | |
7 | // This file contains class RegexStaticSets | |
8 | // | |
9 | // This class is internal to the regular expression implementation. | |
10 | // For the public Regular Expression API, see the file "unicode/regex.h" | |
11 | // | |
12 | // RegexStaticSets groups together the common UnicodeSets that are needed | |
13 | // for compiling or executing RegularExpressions. This grouping simplifies | |
14 | // the thread safe lazy creation and sharing of these sets across | |
15 | // all instances of regular expressions. | |
16 | // | |
17 | #include "unicode/utypes.h" | |
18 | ||
19 | #if !UCONFIG_NO_REGULAR_EXPRESSIONS | |
20 | ||
21 | #include "unicode/unistr.h" | |
22 | #include "unicode/uniset.h" | |
23 | #include "unicode/uchar.h" | |
24 | #include "unicode/regex.h" | |
25 | #include "uprops.h" | |
26 | #include "cmemory.h" | |
27 | #include "cstring.h" | |
28 | #include "uassert.h" | |
29 | #include "ucln_in.h" | |
30 | #include "umutex.h" | |
31 | ||
32 | #include "regexcst.h" // Contains state table for the regex pattern parser. | |
33 | // generated by a Perl script. | |
34 | #include "regexst.h" | |
35 | ||
36 | ||
37 | ||
38 | U_NAMESPACE_BEGIN | |
39 | ||
40 | ||
41 | //---------------------------------------------------------------------------------------- | |
42 | // | |
43 | // Unicode Set pattern strings for all of the required constant sets. | |
44 | // Initialized with hex values for portability to EBCDIC based machines. | |
45 | // Really ugly, but there's no good way to avoid it. | |
46 | // | |
47 | //---------------------------------------------------------------------------------------- | |
48 | ||
49 | // "Rule Char" Characters are those with no special meaning, and therefore do not | |
50 | // need to be escaped to appear as literals in a regexp. Expressed | |
51 | // as the inverse of those needing escaping -- [^\*\?\+\[\(\)\{\}\^\$\|\\\.] | |
52 | static const UChar gRuleSet_rule_char_pattern[] = { | |
53 | // [ ^ \ * \ ? \ + \ [ \ ( / ) | |
54 | 0x5b, 0x5e, 0x5c, 0x2a, 0x5c, 0x3f, 0x5c, 0x2b, 0x5c, 0x5b, 0x5c, 0x28, 0x5c, 0x29, | |
55 | // \ { \ } \ ^ \ $ \ | \ \ \ . ] | |
56 | 0x5c, 0x7b,0x5c, 0x7d, 0x5c, 0x5e, 0x5c, 0x24, 0x5c, 0x7c, 0x5c, 0x5c, 0x5c, 0x2e, 0x5d, 0}; | |
57 | ||
58 | ||
59 | static const UChar gRuleSet_digit_char_pattern[] = { | |
60 | // [ 0 - 9 ] | |
61 | 0x5b, 0x30, 0x2d, 0x39, 0x5d, 0}; | |
62 | //static const UnicodeSet *gRuleDigits = NULL; | |
63 | ||
64 | ||
65 | ||
66 | // | |
67 | // Here are the backslash escape characters that ICU's unescape() function | |
68 | // will handle. | |
69 | // | |
70 | static const UChar gUnescapeCharPattern[] = { | |
71 | // [ a c e f n r t u U x ] | |
72 | 0x5b, 0x61, 0x63, 0x65, 0x66, 0x6e, 0x72, 0x74, 0x75, 0x55, 0x78, 0x5d, 0}; | |
73 | ||
74 | ||
75 | // | |
76 | // White space characters that may appear within a pattern in free-form mode | |
77 | // | |
78 | static const UChar gRuleWhiteSpacePattern[] = { | |
79 | /* "[[:Cf:][:WSpace:]]" */ | |
80 | 91, 91, 58, 67, 102, 58, 93, 91, 58, 87, | |
81 | 83, 112, 97, 99, 101, 58, 93, 93, 0 }; | |
82 | ||
83 | ||
84 | ||
85 | // | |
86 | // Unicode Set Definitions for Regular Expression \w | |
87 | // | |
88 | static const UChar gIsWordPattern[] = { | |
374ca955 A |
89 | // [ \ p { A l p h a b e t i c } |
90 | 0x5b, 0x5c, 0x70, 0x7b, 0x61, 0x6c, 0x70, 0x68, 0x61, 0x62, 0x65, 0x74, 0x69, 0x63, 0x7d, | |
91 | // \ p { M } Mark | |
92 | 0x5c, 0x70, 0x7b, 0x4d, 0x7d, | |
93 | // \ p { N d } Digit_Numeric | |
94 | 0x5c, 0x70, 0x7b, 0x4e, 0x64, 0x7d, | |
95 | // \ p { P c } ] Connector_Punctuation | |
96 | 0x5c, 0x70, 0x7b, 0x50, 0x63, 0x7d, 0x5d, 0}; | |
b75a7d8f A |
97 | |
98 | ||
99 | // | |
100 | // Unicode Set Definitions for Regular Expression \s | |
101 | // | |
102 | static const UChar gIsSpacePattern[] = { | |
374ca955 A |
103 | // [ \ p { W h i t e S p a c e } ] |
104 | 0x5b, 0x5c, 0x70, 0x7b, 0x57, 0x68, 0x69, 0x74, 0x65, 0x53, 0x70, 0x61, 0x63, 0x65, 0x7d, 0x5d, 0}; | |
b75a7d8f A |
105 | |
106 | ||
107 | // | |
108 | // UnicodeSets used in implementation of Grapheme Cluster detection, \X | |
109 | // | |
110 | static const UChar gGC_ControlPattern[] = { | |
111 | // [ [ : Z l : ] [ : Z p : ] | |
112 | 0x5b, 0x5b, 0x3a, 0x5A, 0x6c, 0x3a, 0x5d, 0x5b, 0x3a, 0x5A, 0x70, 0x3a, 0x5d, | |
374ca955 A |
113 | // [ : C c : ] [ : C f : ] - |
114 | 0x5b, 0x3a, 0x43, 0x63, 0x3a, 0x5d, 0x5b, 0x3a, 0x43, 0x66, 0x3a, 0x5d, 0x2d, | |
115 | // [ : G r a p h e m e _ | |
116 | 0x5b, 0x3a, 0x47, 0x72, 0x61, 0x70, 0x68, 0x65, 0x6d, 0x65, 0x5f, | |
117 | // E x t e n d : ] ] | |
118 | 0x45, 0x78, 0x74, 0x65, 0x6e, 0x64, 0x3a, 0x5d, 0x5d, 0}; | |
b75a7d8f A |
119 | |
120 | static const UChar gGC_ExtendPattern[] = { | |
121 | // [ \ p { G r a p h e m e _ | |
122 | 0x5b, 0x5c, 0x70, 0x7b, 0x47, 0x72, 0x61, 0x70, 0x68, 0x65, 0x6d, 0x65, 0x5f, | |
123 | // E x t e n d } ] | |
124 | 0x45, 0x78, 0x74, 0x65, 0x6e, 0x64, 0x7d, 0x5d, 0}; | |
125 | ||
126 | static const UChar gGC_LPattern[] = { | |
127 | // [ \ p { H a n g u l _ S y l | |
128 | 0x5b, 0x5c, 0x70, 0x7b, 0x48, 0x61, 0x6e, 0x67, 0x75, 0x6c, 0x5f, 0x53, 0x79, 0x6c, | |
129 | // l a b l e _ T y p e = L } ] | |
130 | 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x54, 0x79, 0x70, 0x65, 0x3d, 0x4c, 0x7d, 0x5d, 0}; | |
131 | ||
132 | static const UChar gGC_VPattern[] = { | |
133 | // [ \ p { H a n g u l _ S y l | |
134 | 0x5b, 0x5c, 0x70, 0x7b, 0x48, 0x61, 0x6e, 0x67, 0x75, 0x6c, 0x5f, 0x53, 0x79, 0x6c, | |
135 | // l a b l e _ T y p e = V } ] | |
136 | 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x54, 0x79, 0x70, 0x65, 0x3d, 0x56, 0x7d, 0x5d, 0}; | |
137 | ||
138 | static const UChar gGC_TPattern[] = { | |
139 | // [ \ p { H a n g u l _ S y l | |
140 | 0x5b, 0x5c, 0x70, 0x7b, 0x48, 0x61, 0x6e, 0x67, 0x75, 0x6c, 0x5f, 0x53, 0x79, 0x6c, | |
141 | // l a b l e _ T y p e = T } ] | |
142 | 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x54, 0x79, 0x70, 0x65, 0x3d, 0x54, 0x7d, 0x5d, 0}; | |
143 | ||
144 | static const UChar gGC_LVPattern[] = { | |
145 | // [ \ p { H a n g u l _ S y l | |
146 | 0x5b, 0x5c, 0x70, 0x7b, 0x48, 0x61, 0x6e, 0x67, 0x75, 0x6c, 0x5f, 0x53, 0x79, 0x6c, | |
147 | // l a b l e _ T y p e = L V } ] | |
148 | 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x54, 0x79, 0x70, 0x65, 0x3d, 0x4c, 0x56, 0x7d, 0x5d, 0}; | |
149 | ||
150 | static const UChar gGC_LVTPattern[] = { | |
151 | // [ \ p { H a n g u l _ S y l | |
152 | 0x5b, 0x5c, 0x70, 0x7b, 0x48, 0x61, 0x6e, 0x67, 0x75, 0x6c, 0x5f, 0x53, 0x79, 0x6c, | |
153 | // l a b l e _ T y p e = L V T } ] | |
154 | 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x54, 0x79, 0x70, 0x65, 0x3d, 0x4c, 0x56, 0x54, 0x7d, 0x5d, 0}; | |
155 | ||
156 | RegexStaticSets *RegexStaticSets::gStaticSets = NULL; | |
157 | ||
158 | RegexStaticSets::RegexStaticSets(UErrorCode *status) { | |
159 | // First zero out everything | |
160 | int i; | |
161 | for (i=0; i<URX_LAST_SET; i++) { | |
162 | fPropSets[i] = NULL; | |
163 | } | |
164 | for (i=0; i<10; i++) { | |
165 | fRuleSets[i] = NULL; | |
166 | } | |
167 | fUnescapeCharSet = NULL; | |
168 | fRuleDigits = NULL; | |
169 | fEmptyString = NULL; | |
170 | ||
171 | // Then init the sets to their correct values. | |
172 | fPropSets[URX_ISWORD_SET] = new UnicodeSet(gIsWordPattern, *status); | |
173 | fPropSets[URX_ISSPACE_SET] = new UnicodeSet(gIsSpacePattern, *status); | |
174 | fPropSets[URX_GC_EXTEND] = new UnicodeSet(gGC_ExtendPattern, *status); | |
175 | fPropSets[URX_GC_CONTROL] = new UnicodeSet(gGC_ControlPattern, *status); | |
176 | fPropSets[URX_GC_L] = new UnicodeSet(gGC_LPattern, *status); | |
177 | fPropSets[URX_GC_V] = new UnicodeSet(gGC_VPattern, *status); | |
178 | fPropSets[URX_GC_T] = new UnicodeSet(gGC_TPattern, *status); | |
179 | fPropSets[URX_GC_LV] = new UnicodeSet(gGC_LVPattern, *status); | |
180 | fPropSets[URX_GC_LVT] = new UnicodeSet(gGC_LVTPattern, *status); | |
181 | if (U_FAILURE(*status)) { | |
182 | // Bail out if we were unable to create the above sets. | |
183 | // The rest of the initialization needs them, so we cannot proceed. | |
184 | return; | |
185 | } | |
186 | ||
187 | ||
188 | // | |
189 | // The following sets are dynamically constructed, because their | |
190 | // intialization strings would be unreasonable. | |
191 | // | |
192 | ||
193 | ||
194 | // | |
195 | // "Normal" is the set of characters that don't need special handling | |
196 | // when finding grapheme cluster boundaries. | |
197 | // | |
198 | fPropSets[URX_GC_NORMAL] = new UnicodeSet; | |
199 | fPropSets[URX_GC_NORMAL]->complement(); | |
200 | fPropSets[URX_GC_NORMAL]->remove(0xac00, 0xd7a4); | |
201 | fPropSets[URX_GC_NORMAL]->removeAll(*fPropSets[URX_GC_CONTROL]); | |
202 | fPropSets[URX_GC_NORMAL]->removeAll(*fPropSets[URX_GC_L]); | |
203 | fPropSets[URX_GC_NORMAL]->removeAll(*fPropSets[URX_GC_V]); | |
204 | fPropSets[URX_GC_NORMAL]->removeAll(*fPropSets[URX_GC_T]); | |
205 | ||
206 | // Initialize the 8-bit fast bit sets from the parallel full | |
207 | // UnicodeSets. | |
208 | for (i=0; i<URX_LAST_SET; i++) { | |
209 | fPropSets8[i].init(fPropSets[i]); | |
210 | } | |
211 | ||
212 | // Sets used while parsing rules, but not referenced from the parse state table | |
213 | fRuleSets[kRuleSet_rule_char-128] = new UnicodeSet(gRuleSet_rule_char_pattern, *status); | |
214 | fRuleSets[kRuleSet_white_space-128] = new UnicodeSet(gRuleWhiteSpacePattern, *status); | |
215 | fRuleSets[kRuleSet_digit_char-128] = new UnicodeSet(gRuleSet_digit_char_pattern, *status); | |
216 | fRuleDigits = new UnicodeSet(gRuleSet_digit_char_pattern, *status); | |
217 | fUnescapeCharSet = new UnicodeSet(gUnescapeCharPattern, *status); | |
218 | ||
219 | // Empty UnicodeString, for use by matchers with NULL input. | |
220 | fEmptyString = new UnicodeString; | |
374ca955 | 221 | } |
b75a7d8f A |
222 | |
223 | ||
224 | RegexStaticSets::~RegexStaticSets() { | |
225 | int i; | |
226 | ||
227 | for (i=0; i<URX_LAST_SET; i++) { | |
228 | delete fPropSets[i]; | |
229 | fPropSets[i] = NULL; | |
230 | } | |
231 | for (i=0; i<10; i++) { | |
232 | delete fRuleSets[i]; | |
233 | fRuleSets[i] = NULL; | |
234 | } | |
235 | delete fUnescapeCharSet; | |
236 | fUnescapeCharSet = NULL; | |
237 | delete fRuleDigits; | |
238 | fRuleDigits = NULL; | |
239 | delete fEmptyString; | |
240 | fEmptyString = NULL; | |
374ca955 A |
241 | } |
242 | ||
243 | ||
244 | //---------------------------------------------------------------------------------- | |
245 | // | |
246 | // regex_cleanup Memory cleanup function, free/delete all | |
247 | // cached memory. Called by ICU's u_cleanup() function. | |
248 | // | |
249 | //---------------------------------------------------------------------------------- | |
250 | UBool | |
251 | RegexStaticSets::cleanup(void) { | |
252 | delete RegexStaticSets::gStaticSets; | |
253 | RegexStaticSets::gStaticSets = NULL; | |
254 | return TRUE; | |
255 | } | |
b75a7d8f | 256 | |
374ca955 A |
257 | U_CDECL_BEGIN |
258 | static UBool U_CALLCONV | |
259 | regex_cleanup(void) { | |
260 | return RegexStaticSets::cleanup(); | |
261 | } | |
262 | U_CDECL_END | |
b75a7d8f A |
263 | |
264 | void RegexStaticSets::initGlobals(UErrorCode *status) { | |
265 | umtx_lock(NULL); | |
266 | RegexStaticSets *p = gStaticSets; | |
267 | umtx_unlock(NULL); | |
268 | if (p == NULL) { | |
269 | p = new RegexStaticSets(status); | |
270 | if (U_FAILURE(*status)) { | |
271 | delete p; | |
272 | return; | |
273 | } | |
274 | umtx_lock(NULL); | |
275 | if (gStaticSets == NULL) { | |
276 | gStaticSets = p; | |
277 | p = NULL; | |
278 | } | |
279 | umtx_unlock(NULL); | |
280 | if (p) { | |
281 | delete p; | |
282 | } | |
374ca955 | 283 | ucln_i18n_registerCleanup(UCLN_I18N_REGEX, regex_cleanup); |
b75a7d8f A |
284 | } |
285 | } | |
286 | ||
287 | ||
288 | U_NAMESPACE_END | |
289 | #endif // !UCONFIG_NO_REGULAR_EXPRESSIONS |