]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/regexst.cpp
ICU-3.13.tar.gz
[apple/icu.git] / icuSources / i18n / regexst.cpp
CommitLineData
b75a7d8f
A
1//
2// regexst.h
3//
4// Copyright (C) 2003, International Business Machines Corporation and others.
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
41//----------------------------------------------------------------------------------------
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//
47//----------------------------------------------------------------------------------------
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};
62//static const UnicodeSet *gRuleDigits = NULL;
63
64
65
66//
67// Here are the backslash escape characters that ICU's unescape() function
68// will handle.
69//
70static 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};
73
74
75//
76// White space characters that may appear within a pattern in free-form mode
77//
78static 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 };
82
83
84
85//
86// Unicode Set Definitions for Regular Expression \w
87//
88static 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,
93// \ p { N d } _ ]
94 0x5c, 0x70, 0x7b, 0x4e, 0x64, 0x7d, 0x5f, 0x5d, 0};
95
96
97//
98// Unicode Set Definitions for Regular Expression \s
99//
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};
103
104
105//
106// UnicodeSets used in implementation of Grapheme Cluster detection, \X
107//
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};
113
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,
117// E x t e n d } ]
118 0x45, 0x78, 0x74, 0x65, 0x6e, 0x64, 0x7d, 0x5d, 0};
119
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};
125
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};
131
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};
137
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};
143
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};
149
150RegexStaticSets *RegexStaticSets::gStaticSets = NULL;
151
152RegexStaticSets::RegexStaticSets(UErrorCode *status) {
153 // First zero out everything
154 int i;
155 for (i=0; i<URX_LAST_SET; i++) {
156 fPropSets[i] = NULL;
157 }
158 for (i=0; i<10; i++) {
159 fRuleSets[i] = NULL;
160 }
161 fUnescapeCharSet = NULL;
162 fRuleDigits = NULL;
163 fEmptyString = NULL;
164
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.
178 return;
179 }
180
181
182 //
183 // The following sets are dynamically constructed, because their
184 // intialization strings would be unreasonable.
185 //
186
187
188 //
189 // "Normal" is the set of characters that don't need special handling
190 // when finding grapheme cluster boundaries.
191 //
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]);
199
200 // Initialize the 8-bit fast bit sets from the parallel full
201 // UnicodeSets.
202 for (i=0; i<URX_LAST_SET; i++) {
203 fPropSets8[i].init(fPropSets[i]);
204 }
205
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);
212
213 // Empty UnicodeString, for use by matchers with NULL input.
214 fEmptyString = new UnicodeString;
215};
216
217
218RegexStaticSets::~RegexStaticSets() {
219 int i;
220
221 for (i=0; i<URX_LAST_SET; i++) {
222 delete fPropSets[i];
223 fPropSets[i] = NULL;
224 }
225 for (i=0; i<10; i++) {
226 delete fRuleSets[i];
227 fRuleSets[i] = NULL;
228 }
229 delete fUnescapeCharSet;
230 fUnescapeCharSet = NULL;
231 delete fRuleDigits;
232 fRuleDigits = NULL;
233 delete fEmptyString;
234 fEmptyString = NULL;
235};
236
237
238void RegexStaticSets::initGlobals(UErrorCode *status) {
239 umtx_lock(NULL);
240 RegexStaticSets *p = gStaticSets;
241 umtx_unlock(NULL);
242 if (p == NULL) {
243 p = new RegexStaticSets(status);
244 if (U_FAILURE(*status)) {
245 delete p;
246 return;
247 }
248 umtx_lock(NULL);
249 if (gStaticSets == NULL) {
250 gStaticSets = p;
251 p = NULL;
252 }
253 umtx_unlock(NULL);
254 if (p) {
255 delete p;
256 }
257 ucln_i18n_registerCleanup();
258 }
259}
260
261
262U_NAMESPACE_END
263#endif // !UCONFIG_NO_REGULAR_EXPRESSIONS