4 // Copyright (C) 2004-2012, International Business Machines Corporation and others.
5 // All Rights Reserved.
7 // This file contains class RegexStaticSets
9 // This class is internal to the regular expression implementation.
10 // For the public Regular Expression API, see the file "unicode/regex.h"
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.
17 #include "unicode/utypes.h"
19 #if !UCONFIG_NO_REGULAR_EXPRESSIONS
21 #include "unicode/unistr.h"
22 #include "unicode/uniset.h"
23 #include "unicode/uchar.h"
24 #include "unicode/regex.h"
32 #include "regexcst.h" // Contains state table for the regex pattern parser.
33 // generated by a Perl script.
41 //------------------------------------------------------------------------------
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.
47 //------------------------------------------------------------------------------
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};
59 static const UChar gRuleSet_digit_char_pattern
[] = {
61 0x5b, 0x30, 0x2d, 0x39, 0x5d, 0};
64 // Here are the backslash escape characters that ICU's unescape() function
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};
73 // Unicode Set Definitions for Regular Expression \w
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,
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,
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};
89 // Unicode Set Definitions for Regular Expression \s
91 static const UChar gIsSpacePattern
[] = {
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};
97 // UnicodeSets used in implementation of Grapheme Cluster detection, \X
99 static const UChar gGC_ControlPattern
[] = {
100 // [ [ : Z l : ] [ : Z p : ]
101 0x5b, 0x5b, 0x3a, 0x5A, 0x6c, 0x3a, 0x5d, 0x5b, 0x3a, 0x5A, 0x70, 0x3a, 0x5d,
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,
107 0x45, 0x78, 0x74, 0x65, 0x6e, 0x64, 0x3a, 0x5d, 0x5d, 0};
109 static const UChar gGC_ExtendPattern
[] = {
110 // [ \ p { G r a p h e m e _
111 0x5b, 0x5c, 0x70, 0x7b, 0x47, 0x72, 0x61, 0x70, 0x68, 0x65, 0x6d, 0x65, 0x5f,
113 0x45, 0x78, 0x74, 0x65, 0x6e, 0x64, 0x7d, 0x5d, 0};
115 static const UChar gGC_LPattern
[] = {
116 // [ \ p { H a n g u l _ S y l
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 } ]
119 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x54, 0x79, 0x70, 0x65, 0x3d, 0x4c, 0x7d, 0x5d, 0};
121 static const UChar gGC_VPattern
[] = {
122 // [ \ p { H a n g u l _ S y l
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 } ]
125 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x54, 0x79, 0x70, 0x65, 0x3d, 0x56, 0x7d, 0x5d, 0};
127 static const UChar gGC_TPattern
[] = {
128 // [ \ p { H a n g u l _ S y l
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 } ]
131 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x54, 0x79, 0x70, 0x65, 0x3d, 0x54, 0x7d, 0x5d, 0};
133 static const UChar gGC_LVPattern
[] = {
134 // [ \ p { H a n g u l _ S y l
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 } ]
137 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x54, 0x79, 0x70, 0x65, 0x3d, 0x4c, 0x56, 0x7d, 0x5d, 0};
139 static const UChar gGC_LVTPattern
[] = {
140 // [ \ p { H a n g u l _ S y l
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 } ]
143 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x54, 0x79, 0x70, 0x65, 0x3d, 0x4c, 0x56, 0x54, 0x7d, 0x5d, 0};
146 RegexStaticSets
*RegexStaticSets::gStaticSets
= NULL
;
148 RegexStaticSets::RegexStaticSets(UErrorCode
*status
)
150 fUnescapeCharSet(UnicodeString(TRUE
, gUnescapeCharPattern
, -1), *status
),
151 fRuleDigitsAlias(NULL
),
154 // First zero out everything
156 for (i
=0; i
<URX_LAST_SET
; i
++) {
159 // Then init the sets to their correct values.
160 fPropSets
[URX_ISWORD_SET
] = new UnicodeSet(UnicodeString(TRUE
, gIsWordPattern
, -1), *status
);
161 fPropSets
[URX_ISSPACE_SET
] = new UnicodeSet(UnicodeString(TRUE
, gIsSpacePattern
, -1), *status
);
162 fPropSets
[URX_GC_EXTEND
] = new UnicodeSet(UnicodeString(TRUE
, gGC_ExtendPattern
, -1), *status
);
163 fPropSets
[URX_GC_CONTROL
] = new UnicodeSet(UnicodeString(TRUE
, gGC_ControlPattern
, -1), *status
);
164 fPropSets
[URX_GC_L
] = new UnicodeSet(UnicodeString(TRUE
, gGC_LPattern
, -1), *status
);
165 fPropSets
[URX_GC_V
] = new UnicodeSet(UnicodeString(TRUE
, gGC_VPattern
, -1), *status
);
166 fPropSets
[URX_GC_T
] = new UnicodeSet(UnicodeString(TRUE
, gGC_TPattern
, -1), *status
);
167 fPropSets
[URX_GC_LV
] = new UnicodeSet(UnicodeString(TRUE
, gGC_LVPattern
, -1), *status
);
168 fPropSets
[URX_GC_LVT
] = new UnicodeSet(UnicodeString(TRUE
, gGC_LVTPattern
, -1), *status
);
170 // Check for null pointers
171 if (fPropSets
[URX_ISWORD_SET
] == NULL
|| fPropSets
[URX_ISSPACE_SET
] == NULL
|| fPropSets
[URX_GC_EXTEND
] == NULL
||
172 fPropSets
[URX_GC_CONTROL
] == NULL
|| fPropSets
[URX_GC_L
] == NULL
|| fPropSets
[URX_GC_V
] == NULL
||
173 fPropSets
[URX_GC_T
] == NULL
|| fPropSets
[URX_GC_LV
] == NULL
|| fPropSets
[URX_GC_LVT
] == NULL
) {
174 goto ExitConstrDeleteAll
;
176 if (U_FAILURE(*status
)) {
177 // Bail out if we were unable to create the above sets.
178 // The rest of the initialization needs them, so we cannot proceed.
184 // The following sets are dynamically constructed, because their
185 // initialization strings would be unreasonable.
190 // "Normal" is the set of characters that don't need special handling
191 // when finding grapheme cluster boundaries.
193 fPropSets
[URX_GC_NORMAL
] = new UnicodeSet(0, UnicodeSet::MAX_VALUE
);
194 // Null pointer check
195 if (fPropSets
[URX_GC_NORMAL
] == NULL
) {
196 goto ExitConstrDeleteAll
;
198 fPropSets
[URX_GC_NORMAL
]->remove(0xac00, 0xd7a4);
199 fPropSets
[URX_GC_NORMAL
]->removeAll(*fPropSets
[URX_GC_CONTROL
]);
200 fPropSets
[URX_GC_NORMAL
]->removeAll(*fPropSets
[URX_GC_L
]);
201 fPropSets
[URX_GC_NORMAL
]->removeAll(*fPropSets
[URX_GC_V
]);
202 fPropSets
[URX_GC_NORMAL
]->removeAll(*fPropSets
[URX_GC_T
]);
204 // Initialize the 8-bit fast bit sets from the parallel full
206 for (i
=0; i
<URX_LAST_SET
; i
++) {
208 fPropSets
[i
]->compact();
209 fPropSets8
[i
].init(fPropSets
[i
]);
213 // Sets used while parsing rules, but not referenced from the parse state table
214 fRuleSets
[kRuleSet_rule_char
-128] = UnicodeSet(UnicodeString(TRUE
, gRuleSet_rule_char_pattern
, -1), *status
);
215 fRuleSets
[kRuleSet_digit_char
-128] = UnicodeSet(UnicodeString(TRUE
, gRuleSet_digit_char_pattern
, -1), *status
);
216 fRuleDigitsAlias
= &fRuleSets
[kRuleSet_digit_char
-128];
217 for (i
=0; i
<(int32_t)(sizeof(fRuleSets
)/sizeof(fRuleSets
[0])); i
++) {
218 fRuleSets
[i
].compact();
221 // Finally, initialize an empty string for utility purposes
222 fEmptyText
= utext_openUChars(NULL
, NULL
, 0, status
);
224 return; // If we reached this point, everything is fine so just exit
226 ExitConstrDeleteAll
: // Remove fPropSets and fRuleSets and return error
227 for (i
=0; i
<URX_LAST_SET
; i
++) {
231 *status
= U_MEMORY_ALLOCATION_ERROR
;
235 RegexStaticSets::~RegexStaticSets() {
238 for (i
=0; i
<URX_LAST_SET
; i
++) {
242 fRuleDigitsAlias
= NULL
;
244 utext_close(fEmptyText
);
248 //------------------------------------------------------------------------------
250 // regex_cleanup Memory cleanup function, free/delete all
251 // cached memory. Called by ICU's u_cleanup() function.
253 //------------------------------------------------------------------------------
255 RegexStaticSets::cleanup(void) {
256 delete RegexStaticSets::gStaticSets
;
257 RegexStaticSets::gStaticSets
= NULL
;
262 static UBool U_CALLCONV
263 regex_cleanup(void) {
264 return RegexStaticSets::cleanup();
268 void RegexStaticSets::initGlobals(UErrorCode
*status
) {
270 UMTX_CHECK(NULL
, gStaticSets
, p
);
272 p
= new RegexStaticSets(status
);
274 *status
= U_MEMORY_ALLOCATION_ERROR
;
277 if (U_FAILURE(*status
)) {
282 if (gStaticSets
== NULL
) {
290 ucln_i18n_registerCleanup(UCLN_I18N_REGEX
, regex_cleanup
);
296 #endif // !UCONFIG_NO_REGULAR_EXPRESSIONS