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