4 // Copyright (C) 2003, 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 { L l } \ p { L u }
90 0x5b, 0x5c, 0x70, 0x7b, 0x4c, 0x6c, 0x7d, 0x5c, 0x70, 0x7b, 0x4c, 0x75, 0x7d,
91 // \ p { L t } \ p { L o }
92 0x5c, 0x70, 0x7b, 0x4c, 0x74, 0x7d, 0x5c, 0x70, 0x7b, 0x4c, 0x6f, 0x7d,
94 0x5c, 0x70, 0x7b, 0x4e, 0x64, 0x7d, 0x5f, 0x5d, 0};
98 // Unicode Set Definitions for Regular Expression \s
100 static const UChar gIsSpacePattern
[] = {
101 // [ \ t \ n \ f \ r \ p { Z } ]
102 0x5b, 0x5c, 0x74, 0x5c, 0x6e, 0x5c, 0x66, 0x5c, 0x72, 0x5c, 0x70, 0x7b, 0x5a, 0x7d, 0x5d, 0};
106 // UnicodeSets used in implementation of Grapheme Cluster detection, \X
108 static const UChar gGC_ControlPattern
[] = {
109 // [ [ : Z l : ] [ : Z p : ]
110 0x5b, 0x5b, 0x3a, 0x5A, 0x6c, 0x3a, 0x5d, 0x5b, 0x3a, 0x5A, 0x70, 0x3a, 0x5d,
111 // [ : C c : ] [ : C f : ] ]
112 0x5b, 0x3a, 0x43, 0x63, 0x3a, 0x5d, 0x5b, 0x3a, 0x43, 0x66, 0x3a, 0x5d, 0x5d, 0};
114 static const UChar gGC_ExtendPattern
[] = {
115 // [ \ p { G r a p h e m e _
116 0x5b, 0x5c, 0x70, 0x7b, 0x47, 0x72, 0x61, 0x70, 0x68, 0x65, 0x6d, 0x65, 0x5f,
118 0x45, 0x78, 0x74, 0x65, 0x6e, 0x64, 0x7d, 0x5d, 0};
120 static const UChar gGC_LPattern
[] = {
121 // [ \ p { H a n g u l _ S y l
122 0x5b, 0x5c, 0x70, 0x7b, 0x48, 0x61, 0x6e, 0x67, 0x75, 0x6c, 0x5f, 0x53, 0x79, 0x6c,
123 // l a b l e _ T y p e = L } ]
124 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x54, 0x79, 0x70, 0x65, 0x3d, 0x4c, 0x7d, 0x5d, 0};
126 static const UChar gGC_VPattern
[] = {
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 = V } ]
130 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x54, 0x79, 0x70, 0x65, 0x3d, 0x56, 0x7d, 0x5d, 0};
132 static const UChar gGC_TPattern
[] = {
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 = T } ]
136 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x54, 0x79, 0x70, 0x65, 0x3d, 0x54, 0x7d, 0x5d, 0};
138 static const UChar gGC_LVPattern
[] = {
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 = L V } ]
142 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x54, 0x79, 0x70, 0x65, 0x3d, 0x4c, 0x56, 0x7d, 0x5d, 0};
144 static const UChar gGC_LVTPattern
[] = {
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 T } ]
148 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x54, 0x79, 0x70, 0x65, 0x3d, 0x4c, 0x56, 0x54, 0x7d, 0x5d, 0};
150 RegexStaticSets
*RegexStaticSets::gStaticSets
= NULL
;
152 RegexStaticSets::RegexStaticSets(UErrorCode
*status
) {
153 // First zero out everything
155 for (i
=0; i
<URX_LAST_SET
; i
++) {
158 for (i
=0; i
<10; i
++) {
161 fUnescapeCharSet
= NULL
;
165 // Then init the sets to their correct values.
166 fPropSets
[URX_ISWORD_SET
] = new UnicodeSet(gIsWordPattern
, *status
);
167 fPropSets
[URX_ISSPACE_SET
] = new UnicodeSet(gIsSpacePattern
, *status
);
168 fPropSets
[URX_GC_EXTEND
] = new UnicodeSet(gGC_ExtendPattern
, *status
);
169 fPropSets
[URX_GC_CONTROL
] = new UnicodeSet(gGC_ControlPattern
, *status
);
170 fPropSets
[URX_GC_L
] = new UnicodeSet(gGC_LPattern
, *status
);
171 fPropSets
[URX_GC_V
] = new UnicodeSet(gGC_VPattern
, *status
);
172 fPropSets
[URX_GC_T
] = new UnicodeSet(gGC_TPattern
, *status
);
173 fPropSets
[URX_GC_LV
] = new UnicodeSet(gGC_LVPattern
, *status
);
174 fPropSets
[URX_GC_LVT
] = new UnicodeSet(gGC_LVTPattern
, *status
);
175 if (U_FAILURE(*status
)) {
176 // Bail out if we were unable to create the above sets.
177 // The rest of the initialization needs them, so we cannot proceed.
183 // The following sets are dynamically constructed, because their
184 // intialization strings would be unreasonable.
189 // "Normal" is the set of characters that don't need special handling
190 // when finding grapheme cluster boundaries.
192 fPropSets
[URX_GC_NORMAL
] = new UnicodeSet
;
193 fPropSets
[URX_GC_NORMAL
]->complement();
194 fPropSets
[URX_GC_NORMAL
]->remove(0xac00, 0xd7a4);
195 fPropSets
[URX_GC_NORMAL
]->removeAll(*fPropSets
[URX_GC_CONTROL
]);
196 fPropSets
[URX_GC_NORMAL
]->removeAll(*fPropSets
[URX_GC_L
]);
197 fPropSets
[URX_GC_NORMAL
]->removeAll(*fPropSets
[URX_GC_V
]);
198 fPropSets
[URX_GC_NORMAL
]->removeAll(*fPropSets
[URX_GC_T
]);
200 // Initialize the 8-bit fast bit sets from the parallel full
202 for (i
=0; i
<URX_LAST_SET
; i
++) {
203 fPropSets8
[i
].init(fPropSets
[i
]);
206 // Sets used while parsing rules, but not referenced from the parse state table
207 fRuleSets
[kRuleSet_rule_char
-128] = new UnicodeSet(gRuleSet_rule_char_pattern
, *status
);
208 fRuleSets
[kRuleSet_white_space
-128] = new UnicodeSet(gRuleWhiteSpacePattern
, *status
);
209 fRuleSets
[kRuleSet_digit_char
-128] = new UnicodeSet(gRuleSet_digit_char_pattern
, *status
);
210 fRuleDigits
= new UnicodeSet(gRuleSet_digit_char_pattern
, *status
);
211 fUnescapeCharSet
= new UnicodeSet(gUnescapeCharPattern
, *status
);
213 // Empty UnicodeString, for use by matchers with NULL input.
214 fEmptyString
= new UnicodeString
;
218 RegexStaticSets::~RegexStaticSets() {
221 for (i
=0; i
<URX_LAST_SET
; i
++) {
225 for (i
=0; i
<10; i
++) {
229 delete fUnescapeCharSet
;
230 fUnescapeCharSet
= NULL
;
238 void RegexStaticSets::initGlobals(UErrorCode
*status
) {
240 RegexStaticSets
*p
= gStaticSets
;
243 p
= new RegexStaticSets(status
);
244 if (U_FAILURE(*status
)) {
249 if (gStaticSets
== NULL
) {
257 ucln_i18n_registerCleanup();
263 #endif // !UCONFIG_NO_REGULAR_EXPRESSIONS