]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/regexst.cpp
ICU-64232.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / regexst.cpp
CommitLineData
f3c0d7a5
A
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
b75a7d8f
A
3//
4// regexst.h
5//
b331163b 6// Copyright (C) 2004-2015, International Business Machines Corporation and others.
b75a7d8f
A
7// All Rights Reserved.
8//
9// This file contains class RegexStaticSets
10//
11// This class is internal to the regular expression implementation.
12// For the public Regular Expression API, see the file "unicode/regex.h"
13//
14// RegexStaticSets groups together the common UnicodeSets that are needed
15// for compiling or executing RegularExpressions. This grouping simplifies
16// the thread safe lazy creation and sharing of these sets across
17// all instances of regular expressions.
18//
19#include "unicode/utypes.h"
20
21#if !UCONFIG_NO_REGULAR_EXPRESSIONS
22
23#include "unicode/unistr.h"
24#include "unicode/uniset.h"
25#include "unicode/uchar.h"
26#include "unicode/regex.h"
27#include "uprops.h"
28#include "cmemory.h"
29#include "cstring.h"
30#include "uassert.h"
31#include "ucln_in.h"
32#include "umutex.h"
33
34#include "regexcst.h" // Contains state table for the regex pattern parser.
35 // generated by a Perl script.
36#include "regexst.h"
37
38
39
40U_NAMESPACE_BEGIN
41
42
46f4442e 43//------------------------------------------------------------------------------
b75a7d8f
A
44//
45// Unicode Set pattern strings for all of the required constant sets.
46// Initialized with hex values for portability to EBCDIC based machines.
47// Really ugly, but there's no good way to avoid it.
48//
46f4442e 49//------------------------------------------------------------------------------
b75a7d8f
A
50
51// "Rule Char" Characters are those with no special meaning, and therefore do not
52// need to be escaped to appear as literals in a regexp. Expressed
53// as the inverse of those needing escaping -- [^\*\?\+\[\(\)\{\}\^\$\|\\\.]
54static const UChar gRuleSet_rule_char_pattern[] = {
55 // [ ^ \ * \ ? \ + \ [ \ ( / )
56 0x5b, 0x5e, 0x5c, 0x2a, 0x5c, 0x3f, 0x5c, 0x2b, 0x5c, 0x5b, 0x5c, 0x28, 0x5c, 0x29,
57 // \ { \ } \ ^ \ $ \ | \ \ \ . ]
58 0x5c, 0x7b,0x5c, 0x7d, 0x5c, 0x5e, 0x5c, 0x24, 0x5c, 0x7c, 0x5c, 0x5c, 0x5c, 0x2e, 0x5d, 0};
59
b75a7d8f
A
60//
61// Here are the backslash escape characters that ICU's unescape() function
62// will handle.
63//
64static const UChar gUnescapeCharPattern[] = {
65// [ a c e f n r t u U x ]
66 0x5b, 0x61, 0x63, 0x65, 0x66, 0x6e, 0x72, 0x74, 0x75, 0x55, 0x78, 0x5d, 0};
67
68
b75a7d8f
A
69//
70// Unicode Set Definitions for Regular Expression \w
71//
72static const UChar gIsWordPattern[] = {
374ca955
A
73// [ \ p { A l p h a b e t i c }
74 0x5b, 0x5c, 0x70, 0x7b, 0x61, 0x6c, 0x70, 0x68, 0x61, 0x62, 0x65, 0x74, 0x69, 0x63, 0x7d,
75// \ p { M } Mark
46f4442e 76 0x5c, 0x70, 0x7b, 0x4d, 0x7d,
374ca955
A
77// \ p { N d } Digit_Numeric
78 0x5c, 0x70, 0x7b, 0x4e, 0x64, 0x7d,
4388f060
A
79// \ p { P c } Connector_Punctuation
80 0x5c, 0x70, 0x7b, 0x50, 0x63, 0x7d,
81// \ u 2 0 0 c \ u 2 0 0 d ]
82 0x5c, 0x75, 0x32, 0x30, 0x30, 0x63, 0x5c, 0x75, 0x32, 0x30, 0x30, 0x64, 0x5d, 0};
b75a7d8f
A
83
84
85//
86// Unicode Set Definitions for Regular Expression \s
87//
46f4442e 88static const UChar gIsSpacePattern[] = {
374ca955
A
89// [ \ p { W h i t e S p a c e } ]
90 0x5b, 0x5c, 0x70, 0x7b, 0x57, 0x68, 0x69, 0x74, 0x65, 0x53, 0x70, 0x61, 0x63, 0x65, 0x7d, 0x5d, 0};
b75a7d8f
A
91
92
93//
94// UnicodeSets used in implementation of Grapheme Cluster detection, \X
95//
46f4442e
A
96static const UChar gGC_ControlPattern[] = {
97// [ [ : Z l : ] [ : Z p : ]
98 0x5b, 0x5b, 0x3a, 0x5A, 0x6c, 0x3a, 0x5d, 0x5b, 0x3a, 0x5A, 0x70, 0x3a, 0x5d,
374ca955
A
99// [ : C c : ] [ : C f : ] -
100 0x5b, 0x3a, 0x43, 0x63, 0x3a, 0x5d, 0x5b, 0x3a, 0x43, 0x66, 0x3a, 0x5d, 0x2d,
101// [ : G r a p h e m e _
102 0x5b, 0x3a, 0x47, 0x72, 0x61, 0x70, 0x68, 0x65, 0x6d, 0x65, 0x5f,
103// E x t e n d : ] ]
104 0x45, 0x78, 0x74, 0x65, 0x6e, 0x64, 0x3a, 0x5d, 0x5d, 0};
b75a7d8f 105
46f4442e 106static const UChar gGC_ExtendPattern[] = {
b75a7d8f
A
107// [ \ p { G r a p h e m e _
108 0x5b, 0x5c, 0x70, 0x7b, 0x47, 0x72, 0x61, 0x70, 0x68, 0x65, 0x6d, 0x65, 0x5f,
109// E x t e n d } ]
110 0x45, 0x78, 0x74, 0x65, 0x6e, 0x64, 0x7d, 0x5d, 0};
111
46f4442e
A
112static const UChar gGC_LPattern[] = {
113// [ \ p { H a n g u l _ S y l
b75a7d8f
A
114 0x5b, 0x5c, 0x70, 0x7b, 0x48, 0x61, 0x6e, 0x67, 0x75, 0x6c, 0x5f, 0x53, 0x79, 0x6c,
115// l a b l e _ T y p e = L } ]
46f4442e 116 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x54, 0x79, 0x70, 0x65, 0x3d, 0x4c, 0x7d, 0x5d, 0};
b75a7d8f 117
46f4442e
A
118static const UChar gGC_VPattern[] = {
119// [ \ p { H a n g u l _ S y l
b75a7d8f
A
120 0x5b, 0x5c, 0x70, 0x7b, 0x48, 0x61, 0x6e, 0x67, 0x75, 0x6c, 0x5f, 0x53, 0x79, 0x6c,
121// l a b l e _ T y p e = V } ]
46f4442e 122 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x54, 0x79, 0x70, 0x65, 0x3d, 0x56, 0x7d, 0x5d, 0};
b75a7d8f 123
46f4442e
A
124static const UChar gGC_TPattern[] = {
125// [ \ p { H a n g u l _ S y l
b75a7d8f
A
126 0x5b, 0x5c, 0x70, 0x7b, 0x48, 0x61, 0x6e, 0x67, 0x75, 0x6c, 0x5f, 0x53, 0x79, 0x6c,
127// l a b l e _ T y p e = T } ]
46f4442e 128 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x54, 0x79, 0x70, 0x65, 0x3d, 0x54, 0x7d, 0x5d, 0};
b75a7d8f 129
46f4442e
A
130static const UChar gGC_LVPattern[] = {
131// [ \ p { H a n g u l _ S y l
b75a7d8f
A
132 0x5b, 0x5c, 0x70, 0x7b, 0x48, 0x61, 0x6e, 0x67, 0x75, 0x6c, 0x5f, 0x53, 0x79, 0x6c,
133// l a b l e _ T y p e = L V } ]
46f4442e 134 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x54, 0x79, 0x70, 0x65, 0x3d, 0x4c, 0x56, 0x7d, 0x5d, 0};
b75a7d8f 135
46f4442e
A
136static const UChar gGC_LVTPattern[] = {
137// [ \ p { H a n g u l _ S y l
b75a7d8f
A
138 0x5b, 0x5c, 0x70, 0x7b, 0x48, 0x61, 0x6e, 0x67, 0x75, 0x6c, 0x5f, 0x53, 0x79, 0x6c,
139// l a b l e _ T y p e = L V T } ]
46f4442e
A
140 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x54, 0x79, 0x70, 0x65, 0x3d, 0x4c, 0x56, 0x54, 0x7d, 0x5d, 0};
141
b75a7d8f
A
142
143RegexStaticSets *RegexStaticSets::gStaticSets = NULL;
57a6839d 144UInitOnce gStaticSetsInitOnce = U_INITONCE_INITIALIZER;
b75a7d8f 145
46f4442e
A
146RegexStaticSets::RegexStaticSets(UErrorCode *status)
147:
148fUnescapeCharSet(UnicodeString(TRUE, gUnescapeCharPattern, -1), *status),
729e4ab9
A
149fRuleDigitsAlias(NULL),
150fEmptyText(NULL)
46f4442e
A
151{
152 // First zero out everything
b75a7d8f
A
153 int i;
154 for (i=0; i<URX_LAST_SET; i++) {
155 fPropSets[i] = NULL;
156 }
b75a7d8f 157 // Then init the sets to their correct values.
46f4442e
A
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 }
b75a7d8f
A
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 }
46f4442e
A
179
180
b75a7d8f
A
181 //
182 // The following sets are dynamically constructed, because their
46f4442e 183 // initialization strings would be unreasonable.
b75a7d8f 184 //
46f4442e
A
185
186
b75a7d8f
A
187 //
188 // "Normal" is the set of characters that don't need special handling
189 // when finding grapheme cluster boundaries.
190 //
46f4442e
A
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 }
b75a7d8f
A
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]);
46f4442e 201
b75a7d8f
A
202 // Initialize the 8-bit fast bit sets from the parallel full
203 // UnicodeSets.
204 for (i=0; i<URX_LAST_SET; i++) {
46f4442e
A
205 if (fPropSets[i]) {
206 fPropSets[i]->compact();
207 fPropSets8[i].init(fPropSets[i]);
208 }
b75a7d8f
A
209 }
210
211 // Sets used while parsing rules, but not referenced from the parse state table
46f4442e 212 fRuleSets[kRuleSet_rule_char-128] = UnicodeSet(UnicodeString(TRUE, gRuleSet_rule_char_pattern, -1), *status);
b331163b
A
213 fRuleSets[kRuleSet_digit_char-128].add((UChar)0x30, (UChar)0x39); // [0-9]
214 fRuleSets[kRuleSet_ascii_letter-128].add((UChar)0x41, (UChar)0x5A); // [A-Z]
215 fRuleSets[kRuleSet_ascii_letter-128].add((UChar)0x61, (UChar)0x7A); // [a-z]
46f4442e 216 fRuleDigitsAlias = &fRuleSets[kRuleSet_digit_char-128];
b331163b 217 for (i=0; i<UPRV_LENGTHOF(fRuleSets); i++) {
46f4442e
A
218 fRuleSets[i].compact();
219 }
729e4ab9
A
220
221 // Finally, initialize an empty string for utility purposes
222 fEmptyText = utext_openUChars(NULL, NULL, 0, status);
223
b331163b
A
224 if (U_SUCCESS(*status)) {
225 return;
226 }
46f4442e
A
227
228ExitConstrDeleteAll: // Remove fPropSets and fRuleSets and return error
229 for (i=0; i<URX_LAST_SET; i++) {
230 delete fPropSets[i];
231 fPropSets[i] = NULL;
232 }
b331163b
A
233 if (U_SUCCESS(*status)) {
234 *status = U_MEMORY_ALLOCATION_ERROR;
235 }
374ca955 236}
b75a7d8f
A
237
238
239RegexStaticSets::~RegexStaticSets() {
46f4442e 240 int32_t i;
b75a7d8f
A
241
242 for (i=0; i<URX_LAST_SET; i++) {
243 delete fPropSets[i];
244 fPropSets[i] = NULL;
245 }
46f4442e 246 fRuleDigitsAlias = NULL;
729e4ab9
A
247
248 utext_close(fEmptyText);
374ca955
A
249}
250
251
46f4442e 252//------------------------------------------------------------------------------
374ca955
A
253//
254// regex_cleanup Memory cleanup function, free/delete all
255// cached memory. Called by ICU's u_cleanup() function.
256//
46f4442e 257//------------------------------------------------------------------------------
374ca955
A
258UBool
259RegexStaticSets::cleanup(void) {
260 delete RegexStaticSets::gStaticSets;
261 RegexStaticSets::gStaticSets = NULL;
57a6839d 262 gStaticSetsInitOnce.reset();
374ca955
A
263 return TRUE;
264}
b75a7d8f 265
374ca955
A
266U_CDECL_BEGIN
267static UBool U_CALLCONV
268regex_cleanup(void) {
269 return RegexStaticSets::cleanup();
270}
b75a7d8f 271
57a6839d
A
272static void U_CALLCONV initStaticSets(UErrorCode &status) {
273 U_ASSERT(RegexStaticSets::gStaticSets == NULL);
274 ucln_i18n_registerCleanup(UCLN_I18N_REGEX, regex_cleanup);
275 RegexStaticSets::gStaticSets = new RegexStaticSets(&status);
276 if (U_FAILURE(status)) {
277 delete RegexStaticSets::gStaticSets;
278 RegexStaticSets::gStaticSets = NULL;
279 }
280 if (RegexStaticSets::gStaticSets == NULL && U_SUCCESS(status)) {
281 status = U_MEMORY_ALLOCATION_ERROR;
b75a7d8f
A
282 }
283}
57a6839d 284U_CDECL_END
46f4442e 285
57a6839d
A
286void RegexStaticSets::initGlobals(UErrorCode *status) {
287 umtx_initOnce(gStaticSetsInitOnce, &initStaticSets, *status);
288}
b75a7d8f
A
289
290U_NAMESPACE_END
291#endif // !UCONFIG_NO_REGULAR_EXPRESSIONS