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