]>
Commit | Line | Data |
---|---|---|
b75a7d8f A |
1 | // |
2 | // regexst.h | |
3 | // | |
b331163b | 4 | // Copyright (C) 2004-2015, 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 | ||
46f4442e | 41 | //------------------------------------------------------------------------------ |
b75a7d8f A |
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 | // | |
46f4442e | 47 | //------------------------------------------------------------------------------ |
b75a7d8f A |
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 | ||
b75a7d8f A |
58 | // |
59 | // Here are the backslash escape characters that ICU's unescape() function | |
60 | // will handle. | |
61 | // | |
62 | static const UChar gUnescapeCharPattern[] = { | |
63 | // [ a c e f n r t u U x ] | |
64 | 0x5b, 0x61, 0x63, 0x65, 0x66, 0x6e, 0x72, 0x74, 0x75, 0x55, 0x78, 0x5d, 0}; | |
65 | ||
66 | ||
b75a7d8f A |
67 | // |
68 | // Unicode Set Definitions for Regular Expression \w | |
69 | // | |
70 | static const UChar gIsWordPattern[] = { | |
374ca955 A |
71 | // [ \ p { A l p h a b e t i c } |
72 | 0x5b, 0x5c, 0x70, 0x7b, 0x61, 0x6c, 0x70, 0x68, 0x61, 0x62, 0x65, 0x74, 0x69, 0x63, 0x7d, | |
73 | // \ p { M } Mark | |
46f4442e | 74 | 0x5c, 0x70, 0x7b, 0x4d, 0x7d, |
374ca955 A |
75 | // \ p { N d } Digit_Numeric |
76 | 0x5c, 0x70, 0x7b, 0x4e, 0x64, 0x7d, | |
4388f060 A |
77 | // \ p { P c } Connector_Punctuation |
78 | 0x5c, 0x70, 0x7b, 0x50, 0x63, 0x7d, | |
79 | // \ u 2 0 0 c \ u 2 0 0 d ] | |
80 | 0x5c, 0x75, 0x32, 0x30, 0x30, 0x63, 0x5c, 0x75, 0x32, 0x30, 0x30, 0x64, 0x5d, 0}; | |
b75a7d8f A |
81 | |
82 | ||
83 | // | |
84 | // Unicode Set Definitions for Regular Expression \s | |
85 | // | |
46f4442e | 86 | static const UChar gIsSpacePattern[] = { |
374ca955 A |
87 | // [ \ p { W h i t e S p a c e } ] |
88 | 0x5b, 0x5c, 0x70, 0x7b, 0x57, 0x68, 0x69, 0x74, 0x65, 0x53, 0x70, 0x61, 0x63, 0x65, 0x7d, 0x5d, 0}; | |
b75a7d8f A |
89 | |
90 | ||
91 | // | |
92 | // UnicodeSets used in implementation of Grapheme Cluster detection, \X | |
93 | // | |
46f4442e A |
94 | static const UChar gGC_ControlPattern[] = { |
95 | // [ [ : Z l : ] [ : Z p : ] | |
96 | 0x5b, 0x5b, 0x3a, 0x5A, 0x6c, 0x3a, 0x5d, 0x5b, 0x3a, 0x5A, 0x70, 0x3a, 0x5d, | |
374ca955 A |
97 | // [ : C c : ] [ : C f : ] - |
98 | 0x5b, 0x3a, 0x43, 0x63, 0x3a, 0x5d, 0x5b, 0x3a, 0x43, 0x66, 0x3a, 0x5d, 0x2d, | |
99 | // [ : G r a p h e m e _ | |
100 | 0x5b, 0x3a, 0x47, 0x72, 0x61, 0x70, 0x68, 0x65, 0x6d, 0x65, 0x5f, | |
101 | // E x t e n d : ] ] | |
102 | 0x45, 0x78, 0x74, 0x65, 0x6e, 0x64, 0x3a, 0x5d, 0x5d, 0}; | |
b75a7d8f | 103 | |
46f4442e | 104 | static const UChar gGC_ExtendPattern[] = { |
b75a7d8f A |
105 | // [ \ p { G r a p h e m e _ |
106 | 0x5b, 0x5c, 0x70, 0x7b, 0x47, 0x72, 0x61, 0x70, 0x68, 0x65, 0x6d, 0x65, 0x5f, | |
107 | // E x t e n d } ] | |
108 | 0x45, 0x78, 0x74, 0x65, 0x6e, 0x64, 0x7d, 0x5d, 0}; | |
109 | ||
46f4442e A |
110 | static const UChar gGC_LPattern[] = { |
111 | // [ \ p { H a n g u l _ S y l | |
b75a7d8f A |
112 | 0x5b, 0x5c, 0x70, 0x7b, 0x48, 0x61, 0x6e, 0x67, 0x75, 0x6c, 0x5f, 0x53, 0x79, 0x6c, |
113 | // l a b l e _ T y p e = L } ] | |
46f4442e | 114 | 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x54, 0x79, 0x70, 0x65, 0x3d, 0x4c, 0x7d, 0x5d, 0}; |
b75a7d8f | 115 | |
46f4442e A |
116 | static const UChar gGC_VPattern[] = { |
117 | // [ \ p { H a n g u l _ S y l | |
b75a7d8f A |
118 | 0x5b, 0x5c, 0x70, 0x7b, 0x48, 0x61, 0x6e, 0x67, 0x75, 0x6c, 0x5f, 0x53, 0x79, 0x6c, |
119 | // l a b l e _ T y p e = V } ] | |
46f4442e | 120 | 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x54, 0x79, 0x70, 0x65, 0x3d, 0x56, 0x7d, 0x5d, 0}; |
b75a7d8f | 121 | |
46f4442e A |
122 | static const UChar gGC_TPattern[] = { |
123 | // [ \ p { H a n g u l _ S y l | |
b75a7d8f A |
124 | 0x5b, 0x5c, 0x70, 0x7b, 0x48, 0x61, 0x6e, 0x67, 0x75, 0x6c, 0x5f, 0x53, 0x79, 0x6c, |
125 | // l a b l e _ T y p e = T } ] | |
46f4442e | 126 | 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x54, 0x79, 0x70, 0x65, 0x3d, 0x54, 0x7d, 0x5d, 0}; |
b75a7d8f | 127 | |
46f4442e A |
128 | static const UChar gGC_LVPattern[] = { |
129 | // [ \ p { H a n g u l _ S y l | |
b75a7d8f A |
130 | 0x5b, 0x5c, 0x70, 0x7b, 0x48, 0x61, 0x6e, 0x67, 0x75, 0x6c, 0x5f, 0x53, 0x79, 0x6c, |
131 | // l a b l e _ T y p e = L V } ] | |
46f4442e | 132 | 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x54, 0x79, 0x70, 0x65, 0x3d, 0x4c, 0x56, 0x7d, 0x5d, 0}; |
b75a7d8f | 133 | |
46f4442e A |
134 | static const UChar gGC_LVTPattern[] = { |
135 | // [ \ p { H a n g u l _ S y l | |
b75a7d8f A |
136 | 0x5b, 0x5c, 0x70, 0x7b, 0x48, 0x61, 0x6e, 0x67, 0x75, 0x6c, 0x5f, 0x53, 0x79, 0x6c, |
137 | // l a b l e _ T y p e = L V T } ] | |
46f4442e A |
138 | 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x54, 0x79, 0x70, 0x65, 0x3d, 0x4c, 0x56, 0x54, 0x7d, 0x5d, 0}; |
139 | ||
b75a7d8f A |
140 | |
141 | RegexStaticSets *RegexStaticSets::gStaticSets = NULL; | |
57a6839d | 142 | UInitOnce gStaticSetsInitOnce = U_INITONCE_INITIALIZER; |
b75a7d8f | 143 | |
46f4442e A |
144 | RegexStaticSets::RegexStaticSets(UErrorCode *status) |
145 | : | |
146 | fUnescapeCharSet(UnicodeString(TRUE, gUnescapeCharPattern, -1), *status), | |
729e4ab9 A |
147 | fRuleDigitsAlias(NULL), |
148 | fEmptyText(NULL) | |
46f4442e A |
149 | { |
150 | // First zero out everything | |
b75a7d8f A |
151 | int i; |
152 | for (i=0; i<URX_LAST_SET; i++) { | |
153 | fPropSets[i] = NULL; | |
154 | } | |
b75a7d8f | 155 | // Then init the sets to their correct values. |
46f4442e A |
156 | fPropSets[URX_ISWORD_SET] = new UnicodeSet(UnicodeString(TRUE, gIsWordPattern, -1), *status); |
157 | fPropSets[URX_ISSPACE_SET] = new UnicodeSet(UnicodeString(TRUE, gIsSpacePattern, -1), *status); | |
158 | fPropSets[URX_GC_EXTEND] = new UnicodeSet(UnicodeString(TRUE, gGC_ExtendPattern, -1), *status); | |
159 | fPropSets[URX_GC_CONTROL] = new UnicodeSet(UnicodeString(TRUE, gGC_ControlPattern, -1), *status); | |
160 | fPropSets[URX_GC_L] = new UnicodeSet(UnicodeString(TRUE, gGC_LPattern, -1), *status); | |
161 | fPropSets[URX_GC_V] = new UnicodeSet(UnicodeString(TRUE, gGC_VPattern, -1), *status); | |
162 | fPropSets[URX_GC_T] = new UnicodeSet(UnicodeString(TRUE, gGC_TPattern, -1), *status); | |
163 | fPropSets[URX_GC_LV] = new UnicodeSet(UnicodeString(TRUE, gGC_LVPattern, -1), *status); | |
164 | fPropSets[URX_GC_LVT] = new UnicodeSet(UnicodeString(TRUE, gGC_LVTPattern, -1), *status); | |
165 | ||
166 | // Check for null pointers | |
167 | if (fPropSets[URX_ISWORD_SET] == NULL || fPropSets[URX_ISSPACE_SET] == NULL || fPropSets[URX_GC_EXTEND] == NULL || | |
168 | fPropSets[URX_GC_CONTROL] == NULL || fPropSets[URX_GC_L] == NULL || fPropSets[URX_GC_V] == NULL || | |
169 | fPropSets[URX_GC_T] == NULL || fPropSets[URX_GC_LV] == NULL || fPropSets[URX_GC_LVT] == NULL) { | |
170 | goto ExitConstrDeleteAll; | |
171 | } | |
b75a7d8f A |
172 | if (U_FAILURE(*status)) { |
173 | // Bail out if we were unable to create the above sets. | |
174 | // The rest of the initialization needs them, so we cannot proceed. | |
175 | return; | |
176 | } | |
46f4442e A |
177 | |
178 | ||
b75a7d8f A |
179 | // |
180 | // The following sets are dynamically constructed, because their | |
46f4442e | 181 | // initialization strings would be unreasonable. |
b75a7d8f | 182 | // |
46f4442e A |
183 | |
184 | ||
b75a7d8f A |
185 | // |
186 | // "Normal" is the set of characters that don't need special handling | |
187 | // when finding grapheme cluster boundaries. | |
188 | // | |
46f4442e A |
189 | fPropSets[URX_GC_NORMAL] = new UnicodeSet(0, UnicodeSet::MAX_VALUE); |
190 | // Null pointer check | |
191 | if (fPropSets[URX_GC_NORMAL] == NULL) { | |
192 | goto ExitConstrDeleteAll; | |
193 | } | |
b75a7d8f A |
194 | fPropSets[URX_GC_NORMAL]->remove(0xac00, 0xd7a4); |
195 | fPropSets[URX_GC_NORMAL]->removeAll(*fPropSets[URX_GC_CONTROL]); | |
196 | fPropSets[URX_GC_NORMAL]->removeAll(*fPropSets[URX_GC_L]); | |
197 | fPropSets[URX_GC_NORMAL]->removeAll(*fPropSets[URX_GC_V]); | |
198 | fPropSets[URX_GC_NORMAL]->removeAll(*fPropSets[URX_GC_T]); | |
46f4442e | 199 | |
b75a7d8f A |
200 | // Initialize the 8-bit fast bit sets from the parallel full |
201 | // UnicodeSets. | |
202 | for (i=0; i<URX_LAST_SET; i++) { | |
46f4442e A |
203 | if (fPropSets[i]) { |
204 | fPropSets[i]->compact(); | |
205 | fPropSets8[i].init(fPropSets[i]); | |
206 | } | |
b75a7d8f A |
207 | } |
208 | ||
209 | // Sets used while parsing rules, but not referenced from the parse state table | |
46f4442e | 210 | fRuleSets[kRuleSet_rule_char-128] = UnicodeSet(UnicodeString(TRUE, gRuleSet_rule_char_pattern, -1), *status); |
b331163b A |
211 | fRuleSets[kRuleSet_digit_char-128].add((UChar)0x30, (UChar)0x39); // [0-9] |
212 | fRuleSets[kRuleSet_ascii_letter-128].add((UChar)0x41, (UChar)0x5A); // [A-Z] | |
213 | fRuleSets[kRuleSet_ascii_letter-128].add((UChar)0x61, (UChar)0x7A); // [a-z] | |
46f4442e | 214 | fRuleDigitsAlias = &fRuleSets[kRuleSet_digit_char-128]; |
b331163b | 215 | for (i=0; i<UPRV_LENGTHOF(fRuleSets); i++) { |
46f4442e A |
216 | fRuleSets[i].compact(); |
217 | } | |
729e4ab9 A |
218 | |
219 | // Finally, initialize an empty string for utility purposes | |
220 | fEmptyText = utext_openUChars(NULL, NULL, 0, status); | |
221 | ||
b331163b A |
222 | if (U_SUCCESS(*status)) { |
223 | return; | |
224 | } | |
46f4442e A |
225 | |
226 | ExitConstrDeleteAll: // Remove fPropSets and fRuleSets and return error | |
227 | for (i=0; i<URX_LAST_SET; i++) { | |
228 | delete fPropSets[i]; | |
229 | fPropSets[i] = NULL; | |
230 | } | |
b331163b A |
231 | if (U_SUCCESS(*status)) { |
232 | *status = U_MEMORY_ALLOCATION_ERROR; | |
233 | } | |
374ca955 | 234 | } |
b75a7d8f A |
235 | |
236 | ||
237 | RegexStaticSets::~RegexStaticSets() { | |
46f4442e | 238 | int32_t i; |
b75a7d8f A |
239 | |
240 | for (i=0; i<URX_LAST_SET; i++) { | |
241 | delete fPropSets[i]; | |
242 | fPropSets[i] = NULL; | |
243 | } | |
46f4442e | 244 | fRuleDigitsAlias = NULL; |
729e4ab9 A |
245 | |
246 | utext_close(fEmptyText); | |
374ca955 A |
247 | } |
248 | ||
249 | ||
46f4442e | 250 | //------------------------------------------------------------------------------ |
374ca955 A |
251 | // |
252 | // regex_cleanup Memory cleanup function, free/delete all | |
253 | // cached memory. Called by ICU's u_cleanup() function. | |
254 | // | |
46f4442e | 255 | //------------------------------------------------------------------------------ |
374ca955 A |
256 | UBool |
257 | RegexStaticSets::cleanup(void) { | |
258 | delete RegexStaticSets::gStaticSets; | |
259 | RegexStaticSets::gStaticSets = NULL; | |
57a6839d | 260 | gStaticSetsInitOnce.reset(); |
374ca955 A |
261 | return TRUE; |
262 | } | |
b75a7d8f | 263 | |
374ca955 A |
264 | U_CDECL_BEGIN |
265 | static UBool U_CALLCONV | |
266 | regex_cleanup(void) { | |
267 | return RegexStaticSets::cleanup(); | |
268 | } | |
b75a7d8f | 269 | |
57a6839d A |
270 | static void U_CALLCONV initStaticSets(UErrorCode &status) { |
271 | U_ASSERT(RegexStaticSets::gStaticSets == NULL); | |
272 | ucln_i18n_registerCleanup(UCLN_I18N_REGEX, regex_cleanup); | |
273 | RegexStaticSets::gStaticSets = new RegexStaticSets(&status); | |
274 | if (U_FAILURE(status)) { | |
275 | delete RegexStaticSets::gStaticSets; | |
276 | RegexStaticSets::gStaticSets = NULL; | |
277 | } | |
278 | if (RegexStaticSets::gStaticSets == NULL && U_SUCCESS(status)) { | |
279 | status = U_MEMORY_ALLOCATION_ERROR; | |
b75a7d8f A |
280 | } |
281 | } | |
57a6839d | 282 | U_CDECL_END |
46f4442e | 283 | |
57a6839d A |
284 | void RegexStaticSets::initGlobals(UErrorCode *status) { |
285 | umtx_initOnce(gStaticSetsInitOnce, &initStaticSets, *status); | |
286 | } | |
b75a7d8f A |
287 | |
288 | U_NAMESPACE_END | |
289 | #endif // !UCONFIG_NO_REGULAR_EXPRESSIONS |