4 // Copyright (C) 2004, 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};
62 //static const UnicodeSet *gRuleDigits = NULL;
67 // Here are the backslash escape characters that ICU's unescape() function
70 static const UChar gUnescapeCharPattern
[] = {
71 // [ a c e f n r t u U x ]
72 0x5b, 0x61, 0x63, 0x65, 0x66, 0x6e, 0x72, 0x74, 0x75, 0x55, 0x78, 0x5d, 0};
76 // White space characters that may appear within a pattern in free-form mode
78 static const UChar gRuleWhiteSpacePattern
[] = {
79 /* "[[:Cf:][:WSpace:]]" */
80 91, 91, 58, 67, 102, 58, 93, 91, 58, 87,
81 83, 112, 97, 99, 101, 58, 93, 93, 0 };
86 // Unicode Set Definitions for Regular Expression \w
88 static const UChar gIsWordPattern
[] = {
89 // [ \ p { A l p h a b e t i c }
90 0x5b, 0x5c, 0x70, 0x7b, 0x61, 0x6c, 0x70, 0x68, 0x61, 0x62, 0x65, 0x74, 0x69, 0x63, 0x7d,
92 0x5c, 0x70, 0x7b, 0x4d, 0x7d,
93 // \ p { N d } Digit_Numeric
94 0x5c, 0x70, 0x7b, 0x4e, 0x64, 0x7d,
95 // \ p { P c } ] Connector_Punctuation
96 0x5c, 0x70, 0x7b, 0x50, 0x63, 0x7d, 0x5d, 0};
100 // Unicode Set Definitions for Regular Expression \s
102 static const UChar gIsSpacePattern
[] = {
103 // [ \ p { W h i t e S p a c e } ]
104 0x5b, 0x5c, 0x70, 0x7b, 0x57, 0x68, 0x69, 0x74, 0x65, 0x53, 0x70, 0x61, 0x63, 0x65, 0x7d, 0x5d, 0};
108 // UnicodeSets used in implementation of Grapheme Cluster detection, \X
110 static const UChar gGC_ControlPattern
[] = {
111 // [ [ : Z l : ] [ : Z p : ]
112 0x5b, 0x5b, 0x3a, 0x5A, 0x6c, 0x3a, 0x5d, 0x5b, 0x3a, 0x5A, 0x70, 0x3a, 0x5d,
113 // [ : C c : ] [ : C f : ] -
114 0x5b, 0x3a, 0x43, 0x63, 0x3a, 0x5d, 0x5b, 0x3a, 0x43, 0x66, 0x3a, 0x5d, 0x2d,
115 // [ : G r a p h e m e _
116 0x5b, 0x3a, 0x47, 0x72, 0x61, 0x70, 0x68, 0x65, 0x6d, 0x65, 0x5f,
118 0x45, 0x78, 0x74, 0x65, 0x6e, 0x64, 0x3a, 0x5d, 0x5d, 0};
120 static const UChar gGC_ExtendPattern
[] = {
121 // [ \ p { G r a p h e m e _
122 0x5b, 0x5c, 0x70, 0x7b, 0x47, 0x72, 0x61, 0x70, 0x68, 0x65, 0x6d, 0x65, 0x5f,
124 0x45, 0x78, 0x74, 0x65, 0x6e, 0x64, 0x7d, 0x5d, 0};
126 static const UChar gGC_LPattern
[] = {
127 // [ \ p { H a n g u l _ S y l
128 0x5b, 0x5c, 0x70, 0x7b, 0x48, 0x61, 0x6e, 0x67, 0x75, 0x6c, 0x5f, 0x53, 0x79, 0x6c,
129 // l a b l e _ T y p e = L } ]
130 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x54, 0x79, 0x70, 0x65, 0x3d, 0x4c, 0x7d, 0x5d, 0};
132 static const UChar gGC_VPattern
[] = {
133 // [ \ p { H a n g u l _ S y l
134 0x5b, 0x5c, 0x70, 0x7b, 0x48, 0x61, 0x6e, 0x67, 0x75, 0x6c, 0x5f, 0x53, 0x79, 0x6c,
135 // l a b l e _ T y p e = V } ]
136 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x54, 0x79, 0x70, 0x65, 0x3d, 0x56, 0x7d, 0x5d, 0};
138 static const UChar gGC_TPattern
[] = {
139 // [ \ p { H a n g u l _ S y l
140 0x5b, 0x5c, 0x70, 0x7b, 0x48, 0x61, 0x6e, 0x67, 0x75, 0x6c, 0x5f, 0x53, 0x79, 0x6c,
141 // l a b l e _ T y p e = T } ]
142 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x54, 0x79, 0x70, 0x65, 0x3d, 0x54, 0x7d, 0x5d, 0};
144 static const UChar gGC_LVPattern
[] = {
145 // [ \ p { H a n g u l _ S y l
146 0x5b, 0x5c, 0x70, 0x7b, 0x48, 0x61, 0x6e, 0x67, 0x75, 0x6c, 0x5f, 0x53, 0x79, 0x6c,
147 // l a b l e _ T y p e = L V } ]
148 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x54, 0x79, 0x70, 0x65, 0x3d, 0x4c, 0x56, 0x7d, 0x5d, 0};
150 static const UChar gGC_LVTPattern
[] = {
151 // [ \ p { H a n g u l _ S y l
152 0x5b, 0x5c, 0x70, 0x7b, 0x48, 0x61, 0x6e, 0x67, 0x75, 0x6c, 0x5f, 0x53, 0x79, 0x6c,
153 // l a b l e _ T y p e = L V T } ]
154 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x54, 0x79, 0x70, 0x65, 0x3d, 0x4c, 0x56, 0x54, 0x7d, 0x5d, 0};
156 RegexStaticSets
*RegexStaticSets::gStaticSets
= NULL
;
158 RegexStaticSets::RegexStaticSets(UErrorCode
*status
) {
159 // First zero out everything
161 for (i
=0; i
<URX_LAST_SET
; i
++) {
164 for (i
=0; i
<10; i
++) {
167 fUnescapeCharSet
= NULL
;
171 // Then init the sets to their correct values.
172 fPropSets
[URX_ISWORD_SET
] = new UnicodeSet(gIsWordPattern
, *status
);
173 fPropSets
[URX_ISSPACE_SET
] = new UnicodeSet(gIsSpacePattern
, *status
);
174 fPropSets
[URX_GC_EXTEND
] = new UnicodeSet(gGC_ExtendPattern
, *status
);
175 fPropSets
[URX_GC_CONTROL
] = new UnicodeSet(gGC_ControlPattern
, *status
);
176 fPropSets
[URX_GC_L
] = new UnicodeSet(gGC_LPattern
, *status
);
177 fPropSets
[URX_GC_V
] = new UnicodeSet(gGC_VPattern
, *status
);
178 fPropSets
[URX_GC_T
] = new UnicodeSet(gGC_TPattern
, *status
);
179 fPropSets
[URX_GC_LV
] = new UnicodeSet(gGC_LVPattern
, *status
);
180 fPropSets
[URX_GC_LVT
] = new UnicodeSet(gGC_LVTPattern
, *status
);
181 if (U_FAILURE(*status
)) {
182 // Bail out if we were unable to create the above sets.
183 // The rest of the initialization needs them, so we cannot proceed.
189 // The following sets are dynamically constructed, because their
190 // intialization strings would be unreasonable.
195 // "Normal" is the set of characters that don't need special handling
196 // when finding grapheme cluster boundaries.
198 fPropSets
[URX_GC_NORMAL
] = new UnicodeSet
;
199 fPropSets
[URX_GC_NORMAL
]->complement();
200 fPropSets
[URX_GC_NORMAL
]->remove(0xac00, 0xd7a4);
201 fPropSets
[URX_GC_NORMAL
]->removeAll(*fPropSets
[URX_GC_CONTROL
]);
202 fPropSets
[URX_GC_NORMAL
]->removeAll(*fPropSets
[URX_GC_L
]);
203 fPropSets
[URX_GC_NORMAL
]->removeAll(*fPropSets
[URX_GC_V
]);
204 fPropSets
[URX_GC_NORMAL
]->removeAll(*fPropSets
[URX_GC_T
]);
206 // Initialize the 8-bit fast bit sets from the parallel full
208 for (i
=0; i
<URX_LAST_SET
; i
++) {
209 fPropSets8
[i
].init(fPropSets
[i
]);
212 // Sets used while parsing rules, but not referenced from the parse state table
213 fRuleSets
[kRuleSet_rule_char
-128] = new UnicodeSet(gRuleSet_rule_char_pattern
, *status
);
214 fRuleSets
[kRuleSet_white_space
-128] = new UnicodeSet(gRuleWhiteSpacePattern
, *status
);
215 fRuleSets
[kRuleSet_digit_char
-128] = new UnicodeSet(gRuleSet_digit_char_pattern
, *status
);
216 fRuleDigits
= new UnicodeSet(gRuleSet_digit_char_pattern
, *status
);
217 fUnescapeCharSet
= new UnicodeSet(gUnescapeCharPattern
, *status
);
219 // Empty UnicodeString, for use by matchers with NULL input.
220 fEmptyString
= new UnicodeString
;
224 RegexStaticSets::~RegexStaticSets() {
227 for (i
=0; i
<URX_LAST_SET
; i
++) {
231 for (i
=0; i
<10; i
++) {
235 delete fUnescapeCharSet
;
236 fUnescapeCharSet
= NULL
;
244 //----------------------------------------------------------------------------------
246 // regex_cleanup Memory cleanup function, free/delete all
247 // cached memory. Called by ICU's u_cleanup() function.
249 //----------------------------------------------------------------------------------
251 RegexStaticSets::cleanup(void) {
252 delete RegexStaticSets::gStaticSets
;
253 RegexStaticSets::gStaticSets
= NULL
;
258 static UBool U_CALLCONV
259 regex_cleanup(void) {
260 return RegexStaticSets::cleanup();
264 void RegexStaticSets::initGlobals(UErrorCode
*status
) {
266 RegexStaticSets
*p
= gStaticSets
;
269 p
= new RegexStaticSets(status
);
270 if (U_FAILURE(*status
)) {
275 if (gStaticSets
== NULL
) {
283 ucln_i18n_registerCleanup(UCLN_I18N_REGEX
, regex_cleanup
);
289 #endif // !UCONFIG_NO_REGULAR_EXPRESSIONS