/src/mozilla-central/intl/icu/source/common/uniset_props.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // © 2016 and later: Unicode, Inc. and others. |
2 | | // License & terms of use: http://www.unicode.org/copyright.html |
3 | | /* |
4 | | ******************************************************************************* |
5 | | * |
6 | | * Copyright (C) 1999-2014, International Business Machines |
7 | | * Corporation and others. All Rights Reserved. |
8 | | * |
9 | | ******************************************************************************* |
10 | | * file name: uniset_props.cpp |
11 | | * encoding: UTF-8 |
12 | | * tab size: 8 (not used) |
13 | | * indentation:4 |
14 | | * |
15 | | * created on: 2004aug25 |
16 | | * created by: Markus W. Scherer |
17 | | * |
18 | | * Character property dependent functions moved here from uniset.cpp |
19 | | */ |
20 | | |
21 | | #include "unicode/utypes.h" |
22 | | #include "unicode/uniset.h" |
23 | | #include "unicode/parsepos.h" |
24 | | #include "unicode/uchar.h" |
25 | | #include "unicode/uscript.h" |
26 | | #include "unicode/symtable.h" |
27 | | #include "unicode/uset.h" |
28 | | #include "unicode/locid.h" |
29 | | #include "unicode/brkiter.h" |
30 | | #include "uset_imp.h" |
31 | | #include "ruleiter.h" |
32 | | #include "cmemory.h" |
33 | | #include "ucln_cmn.h" |
34 | | #include "util.h" |
35 | | #include "uvector.h" |
36 | | #include "uprops.h" |
37 | | #include "propname.h" |
38 | | #include "normalizer2impl.h" |
39 | | #include "ucase.h" |
40 | | #include "ubidi_props.h" |
41 | | #include "uinvchar.h" |
42 | | #include "uprops.h" |
43 | | #include "charstr.h" |
44 | | #include "cstring.h" |
45 | | #include "mutex.h" |
46 | | #include "umutex.h" |
47 | | #include "uassert.h" |
48 | | #include "hash.h" |
49 | | |
50 | | U_NAMESPACE_USE |
51 | | |
52 | | // initial storage. Must be >= 0 |
53 | | // *** same as in uniset.cpp ! *** |
54 | | #define START_EXTRA 16 |
55 | | |
56 | | // Define UChar constants using hex for EBCDIC compatibility |
57 | | // Used #define to reduce private static exports and memory access time. |
58 | 0 | #define SET_OPEN ((UChar)0x005B) /*[*/ |
59 | | #define SET_CLOSE ((UChar)0x005D) /*]*/ |
60 | 0 | #define HYPHEN ((UChar)0x002D) /*-*/ |
61 | 0 | #define COMPLEMENT ((UChar)0x005E) /*^*/ |
62 | 0 | #define COLON ((UChar)0x003A) /*:*/ |
63 | 0 | #define BACKSLASH ((UChar)0x005C) /*\*/ |
64 | 0 | #define INTERSECTION ((UChar)0x0026) /*&*/ |
65 | | #define UPPER_U ((UChar)0x0055) /*U*/ |
66 | | #define LOWER_U ((UChar)0x0075) /*u*/ |
67 | 0 | #define OPEN_BRACE ((UChar)123) /*{*/ |
68 | 0 | #define CLOSE_BRACE ((UChar)125) /*}*/ |
69 | 0 | #define UPPER_P ((UChar)0x0050) /*P*/ |
70 | 0 | #define LOWER_P ((UChar)0x0070) /*p*/ |
71 | 0 | #define UPPER_N ((UChar)78) /*N*/ |
72 | 0 | #define EQUALS ((UChar)0x003D) /*=*/ |
73 | | |
74 | | //static const UChar POSIX_OPEN[] = { SET_OPEN,COLON,0 }; // "[:" |
75 | | static const UChar POSIX_CLOSE[] = { COLON,SET_CLOSE,0 }; // ":]" |
76 | | //static const UChar PERL_OPEN[] = { BACKSLASH,LOWER_P,0 }; // "\\p" |
77 | | //static const UChar PERL_CLOSE[] = { CLOSE_BRACE,0 }; // "}" |
78 | | //static const UChar NAME_OPEN[] = { BACKSLASH,UPPER_N,0 }; // "\\N" |
79 | | static const UChar HYPHEN_RIGHT_BRACE[] = {HYPHEN,SET_CLOSE,0}; /*-]*/ |
80 | | |
81 | | // Special property set IDs |
82 | | static const char ANY[] = "ANY"; // [\u0000-\U0010FFFF] |
83 | | static const char ASCII[] = "ASCII"; // [\u0000-\u007F] |
84 | | static const char ASSIGNED[] = "Assigned"; // [:^Cn:] |
85 | | |
86 | | // Unicode name property alias |
87 | 0 | #define NAME_PROP "na" |
88 | 0 | #define NAME_PROP_LENGTH 2 |
89 | | |
90 | | /** |
91 | | * Delimiter string used in patterns to close a category reference: |
92 | | * ":]". Example: "[:Lu:]". |
93 | | */ |
94 | | //static const UChar CATEGORY_CLOSE[] = {COLON, SET_CLOSE, 0x0000}; /* ":]" */ |
95 | | |
96 | | // Cached sets ------------------------------------------------------------- *** |
97 | | |
98 | | U_CDECL_BEGIN |
99 | | static UBool U_CALLCONV uset_cleanup(); |
100 | | |
101 | | struct Inclusion { |
102 | | UnicodeSet *fSet; |
103 | | UInitOnce fInitOnce; |
104 | | }; |
105 | | static Inclusion gInclusions[UPROPS_SRC_COUNT]; // cached getInclusions() |
106 | | |
107 | | static UnicodeSet *uni32Singleton; |
108 | | static icu::UInitOnce uni32InitOnce = U_INITONCE_INITIALIZER; |
109 | | |
110 | | //---------------------------------------------------------------- |
111 | | // Inclusions list |
112 | | //---------------------------------------------------------------- |
113 | | |
114 | | // USetAdder implementation |
115 | | // Does not use uset.h to reduce code dependencies |
116 | | static void U_CALLCONV |
117 | 0 | _set_add(USet *set, UChar32 c) { |
118 | 0 | ((UnicodeSet *)set)->add(c); |
119 | 0 | } |
120 | | |
121 | | static void U_CALLCONV |
122 | 0 | _set_addRange(USet *set, UChar32 start, UChar32 end) { |
123 | 0 | ((UnicodeSet *)set)->add(start, end); |
124 | 0 | } |
125 | | |
126 | | static void U_CALLCONV |
127 | 0 | _set_addString(USet *set, const UChar *str, int32_t length) { |
128 | 0 | ((UnicodeSet *)set)->add(UnicodeString((UBool)(length<0), str, length)); |
129 | 0 | } |
130 | | |
131 | | /** |
132 | | * Cleanup function for UnicodeSet |
133 | | */ |
134 | 0 | static UBool U_CALLCONV uset_cleanup(void) { |
135 | 0 | for(int32_t i = UPROPS_SRC_NONE; i < UPROPS_SRC_COUNT; ++i) { |
136 | 0 | Inclusion &in = gInclusions[i]; |
137 | 0 | delete in.fSet; |
138 | 0 | in.fSet = NULL; |
139 | 0 | in.fInitOnce.reset(); |
140 | 0 | } |
141 | 0 |
|
142 | 0 | delete uni32Singleton; |
143 | 0 | uni32Singleton = NULL; |
144 | 0 | uni32InitOnce.reset(); |
145 | 0 | return TRUE; |
146 | 0 | } |
147 | | |
148 | | U_CDECL_END |
149 | | |
150 | | U_NAMESPACE_BEGIN |
151 | | |
152 | | /* |
153 | | Reduce excessive reallocation, and make it easier to detect initialization problems. |
154 | | Usually you don't see smaller sets than this for Unicode 5.0. |
155 | | */ |
156 | 0 | #define DEFAULT_INCLUSION_CAPACITY 3072 |
157 | | |
158 | 0 | void U_CALLCONV UnicodeSet_initInclusion(int32_t src, UErrorCode &status) { |
159 | 0 | // This function is invoked only via umtx_initOnce(). |
160 | 0 | // This function is a friend of class UnicodeSet. |
161 | 0 |
|
162 | 0 | U_ASSERT(src >=0 && src<UPROPS_SRC_COUNT); |
163 | 0 | UnicodeSet * &incl = gInclusions[src].fSet; |
164 | 0 | U_ASSERT(incl == NULL); |
165 | 0 |
|
166 | 0 | incl = new UnicodeSet(); |
167 | 0 | if (incl == NULL) { |
168 | 0 | status = U_MEMORY_ALLOCATION_ERROR; |
169 | 0 | return; |
170 | 0 | } |
171 | 0 | USetAdder sa = { |
172 | 0 | (USet *)incl, |
173 | 0 | _set_add, |
174 | 0 | _set_addRange, |
175 | 0 | _set_addString, |
176 | 0 | NULL, // don't need remove() |
177 | 0 | NULL // don't need removeRange() |
178 | 0 | }; |
179 | 0 |
|
180 | 0 | incl->ensureCapacity(DEFAULT_INCLUSION_CAPACITY, status); |
181 | 0 | switch(src) { |
182 | 0 | case UPROPS_SRC_CHAR: |
183 | 0 | uchar_addPropertyStarts(&sa, &status); |
184 | 0 | break; |
185 | 0 | case UPROPS_SRC_PROPSVEC: |
186 | 0 | upropsvec_addPropertyStarts(&sa, &status); |
187 | 0 | break; |
188 | 0 | case UPROPS_SRC_CHAR_AND_PROPSVEC: |
189 | 0 | uchar_addPropertyStarts(&sa, &status); |
190 | 0 | upropsvec_addPropertyStarts(&sa, &status); |
191 | 0 | break; |
192 | 0 | #if !UCONFIG_NO_NORMALIZATION |
193 | 0 | case UPROPS_SRC_CASE_AND_NORM: { |
194 | 0 | const Normalizer2Impl *impl=Normalizer2Factory::getNFCImpl(status); |
195 | 0 | if(U_SUCCESS(status)) { |
196 | 0 | impl->addPropertyStarts(&sa, status); |
197 | 0 | } |
198 | 0 | ucase_addPropertyStarts(&sa, &status); |
199 | 0 | break; |
200 | 0 | } |
201 | 0 | case UPROPS_SRC_NFC: { |
202 | 0 | const Normalizer2Impl *impl=Normalizer2Factory::getNFCImpl(status); |
203 | 0 | if(U_SUCCESS(status)) { |
204 | 0 | impl->addPropertyStarts(&sa, status); |
205 | 0 | } |
206 | 0 | break; |
207 | 0 | } |
208 | 0 | case UPROPS_SRC_NFKC: { |
209 | 0 | const Normalizer2Impl *impl=Normalizer2Factory::getNFKCImpl(status); |
210 | 0 | if(U_SUCCESS(status)) { |
211 | 0 | impl->addPropertyStarts(&sa, status); |
212 | 0 | } |
213 | 0 | break; |
214 | 0 | } |
215 | 0 | case UPROPS_SRC_NFKC_CF: { |
216 | 0 | const Normalizer2Impl *impl=Normalizer2Factory::getNFKC_CFImpl(status); |
217 | 0 | if(U_SUCCESS(status)) { |
218 | 0 | impl->addPropertyStarts(&sa, status); |
219 | 0 | } |
220 | 0 | break; |
221 | 0 | } |
222 | 0 | case UPROPS_SRC_NFC_CANON_ITER: { |
223 | 0 | const Normalizer2Impl *impl=Normalizer2Factory::getNFCImpl(status); |
224 | 0 | if(U_SUCCESS(status)) { |
225 | 0 | impl->addCanonIterPropertyStarts(&sa, status); |
226 | 0 | } |
227 | 0 | break; |
228 | 0 | } |
229 | 0 | #endif |
230 | 0 | case UPROPS_SRC_CASE: |
231 | 0 | ucase_addPropertyStarts(&sa, &status); |
232 | 0 | break; |
233 | 0 | case UPROPS_SRC_BIDI: |
234 | 0 | ubidi_addPropertyStarts(&sa, &status); |
235 | 0 | break; |
236 | 0 | default: |
237 | 0 | status = U_INTERNAL_PROGRAM_ERROR; |
238 | 0 | break; |
239 | 0 | } |
240 | 0 | |
241 | 0 | if (U_FAILURE(status)) { |
242 | 0 | delete incl; |
243 | 0 | incl = NULL; |
244 | 0 | return; |
245 | 0 | } |
246 | 0 | // Compact for caching |
247 | 0 | incl->compact(); |
248 | 0 | ucln_common_registerCleanup(UCLN_COMMON_USET, uset_cleanup); |
249 | 0 | } |
250 | | |
251 | | |
252 | | |
253 | 0 | const UnicodeSet* UnicodeSet::getInclusions(int32_t src, UErrorCode &status) { |
254 | 0 | U_ASSERT(src >=0 && src<UPROPS_SRC_COUNT); |
255 | 0 | Inclusion &i = gInclusions[src]; |
256 | 0 | umtx_initOnce(i.fInitOnce, &UnicodeSet_initInclusion, src, status); |
257 | 0 | return i.fSet; |
258 | 0 | } |
259 | | |
260 | | namespace { |
261 | | |
262 | | // Cache some sets for other services -------------------------------------- *** |
263 | 0 | void U_CALLCONV createUni32Set(UErrorCode &errorCode) { |
264 | 0 | U_ASSERT(uni32Singleton == NULL); |
265 | 0 | uni32Singleton = new UnicodeSet(UNICODE_STRING_SIMPLE("[:age=3.2:]"), errorCode); |
266 | 0 | if(uni32Singleton==NULL) { |
267 | 0 | errorCode=U_MEMORY_ALLOCATION_ERROR; |
268 | 0 | } else { |
269 | 0 | uni32Singleton->freeze(); |
270 | 0 | } |
271 | 0 | ucln_common_registerCleanup(UCLN_COMMON_USET, uset_cleanup); |
272 | 0 | } |
273 | | |
274 | | |
275 | | U_CFUNC UnicodeSet * |
276 | 0 | uniset_getUnicode32Instance(UErrorCode &errorCode) { |
277 | 0 | umtx_initOnce(uni32InitOnce, &createUni32Set, errorCode); |
278 | 0 | return uni32Singleton; |
279 | 0 | } |
280 | | |
281 | | // helper functions for matching of pattern syntax pieces ------------------ *** |
282 | | // these functions are parallel to the PERL_OPEN etc. strings above |
283 | | |
284 | | // using these functions is not only faster than UnicodeString::compare() and |
285 | | // caseCompare(), but they also make UnicodeSet work for simple patterns when |
286 | | // no Unicode properties data is available - when caseCompare() fails |
287 | | |
288 | | static inline UBool |
289 | 0 | isPerlOpen(const UnicodeString &pattern, int32_t pos) { |
290 | 0 | UChar c; |
291 | 0 | return pattern.charAt(pos)==BACKSLASH && ((c=pattern.charAt(pos+1))==LOWER_P || c==UPPER_P); |
292 | 0 | } |
293 | | |
294 | | /*static inline UBool |
295 | | isPerlClose(const UnicodeString &pattern, int32_t pos) { |
296 | | return pattern.charAt(pos)==CLOSE_BRACE; |
297 | | }*/ |
298 | | |
299 | | static inline UBool |
300 | 0 | isNameOpen(const UnicodeString &pattern, int32_t pos) { |
301 | 0 | return pattern.charAt(pos)==BACKSLASH && pattern.charAt(pos+1)==UPPER_N; |
302 | 0 | } |
303 | | |
304 | | static inline UBool |
305 | 0 | isPOSIXOpen(const UnicodeString &pattern, int32_t pos) { |
306 | 0 | return pattern.charAt(pos)==SET_OPEN && pattern.charAt(pos+1)==COLON; |
307 | 0 | } |
308 | | |
309 | | /*static inline UBool |
310 | | isPOSIXClose(const UnicodeString &pattern, int32_t pos) { |
311 | | return pattern.charAt(pos)==COLON && pattern.charAt(pos+1)==SET_CLOSE; |
312 | | }*/ |
313 | | |
314 | | // TODO memory debugging provided inside uniset.cpp |
315 | | // could be made available here but probably obsolete with use of modern |
316 | | // memory leak checker tools |
317 | | #define _dbgct(me) |
318 | | |
319 | | } // namespace |
320 | | |
321 | | //---------------------------------------------------------------- |
322 | | // Constructors &c |
323 | | //---------------------------------------------------------------- |
324 | | |
325 | | /** |
326 | | * Constructs a set from the given pattern, optionally ignoring |
327 | | * white space. See the class description for the syntax of the |
328 | | * pattern language. |
329 | | * @param pattern a string specifying what characters are in the set |
330 | | */ |
331 | | UnicodeSet::UnicodeSet(const UnicodeString& pattern, |
332 | | UErrorCode& status) : |
333 | | len(0), capacity(START_EXTRA), list(0), bmpSet(0), buffer(0), |
334 | | bufferCapacity(0), patLen(0), pat(NULL), strings(NULL), stringSpan(NULL), |
335 | | fFlags(0) |
336 | 0 | { |
337 | 0 | if(U_SUCCESS(status)){ |
338 | 0 | list = (UChar32*) uprv_malloc(sizeof(UChar32) * capacity); |
339 | 0 | /* test for NULL */ |
340 | 0 | if(list == NULL) { |
341 | 0 | status = U_MEMORY_ALLOCATION_ERROR; |
342 | 0 | }else{ |
343 | 0 | allocateStrings(status); |
344 | 0 | applyPattern(pattern, status); |
345 | 0 | } |
346 | 0 | } |
347 | 0 | _dbgct(this); |
348 | 0 | } |
349 | | |
350 | | //---------------------------------------------------------------- |
351 | | // Public API |
352 | | //---------------------------------------------------------------- |
353 | | |
354 | | UnicodeSet& UnicodeSet::applyPattern(const UnicodeString& pattern, |
355 | 0 | UErrorCode& status) { |
356 | 0 | // Equivalent to |
357 | 0 | // return applyPattern(pattern, USET_IGNORE_SPACE, NULL, status); |
358 | 0 | // but without dependency on closeOver(). |
359 | 0 | ParsePosition pos(0); |
360 | 0 | applyPatternIgnoreSpace(pattern, pos, NULL, status); |
361 | 0 | if (U_FAILURE(status)) return *this; |
362 | 0 | |
363 | 0 | int32_t i = pos.getIndex(); |
364 | 0 | // Skip over trailing whitespace |
365 | 0 | ICU_Utility::skipWhitespace(pattern, i, TRUE); |
366 | 0 | if (i != pattern.length()) { |
367 | 0 | status = U_ILLEGAL_ARGUMENT_ERROR; |
368 | 0 | } |
369 | 0 | return *this; |
370 | 0 | } |
371 | | |
372 | | void |
373 | | UnicodeSet::applyPatternIgnoreSpace(const UnicodeString& pattern, |
374 | | ParsePosition& pos, |
375 | | const SymbolTable* symbols, |
376 | 0 | UErrorCode& status) { |
377 | 0 | if (U_FAILURE(status)) { |
378 | 0 | return; |
379 | 0 | } |
380 | 0 | if (isFrozen()) { |
381 | 0 | status = U_NO_WRITE_PERMISSION; |
382 | 0 | return; |
383 | 0 | } |
384 | 0 | // Need to build the pattern in a temporary string because |
385 | 0 | // _applyPattern calls add() etc., which set pat to empty. |
386 | 0 | UnicodeString rebuiltPat; |
387 | 0 | RuleCharacterIterator chars(pattern, symbols, pos); |
388 | 0 | applyPattern(chars, symbols, rebuiltPat, USET_IGNORE_SPACE, NULL, 0, status); |
389 | 0 | if (U_FAILURE(status)) return; |
390 | 0 | if (chars.inVariable()) { |
391 | 0 | // syntaxError(chars, "Extra chars in variable value"); |
392 | 0 | status = U_MALFORMED_SET; |
393 | 0 | return; |
394 | 0 | } |
395 | 0 | setPattern(rebuiltPat); |
396 | 0 | } |
397 | | |
398 | | /** |
399 | | * Return true if the given position, in the given pattern, appears |
400 | | * to be the start of a UnicodeSet pattern. |
401 | | */ |
402 | 0 | UBool UnicodeSet::resemblesPattern(const UnicodeString& pattern, int32_t pos) { |
403 | 0 | return ((pos+1) < pattern.length() && |
404 | 0 | pattern.charAt(pos) == (UChar)91/*[*/) || |
405 | 0 | resemblesPropertyPattern(pattern, pos); |
406 | 0 | } |
407 | | |
408 | | //---------------------------------------------------------------- |
409 | | // Implementation: Pattern parsing |
410 | | //---------------------------------------------------------------- |
411 | | |
412 | | namespace { |
413 | | |
414 | | /** |
415 | | * A small all-inline class to manage a UnicodeSet pointer. Add |
416 | | * operator->() etc. as needed. |
417 | | */ |
418 | | class UnicodeSetPointer { |
419 | | UnicodeSet* p; |
420 | | public: |
421 | 0 | inline UnicodeSetPointer() : p(0) {} |
422 | 0 | inline ~UnicodeSetPointer() { delete p; } |
423 | 0 | inline UnicodeSet* pointer() { return p; } |
424 | 0 | inline UBool allocate() { |
425 | 0 | if (p == 0) { |
426 | 0 | p = new UnicodeSet(); |
427 | 0 | } |
428 | 0 | return p != 0; |
429 | 0 | } |
430 | | }; |
431 | | |
432 | | constexpr int32_t MAX_DEPTH = 100; |
433 | | |
434 | | } // namespace |
435 | | |
436 | | /** |
437 | | * Parse the pattern from the given RuleCharacterIterator. The |
438 | | * iterator is advanced over the parsed pattern. |
439 | | * @param chars iterator over the pattern characters. Upon return |
440 | | * it will be advanced to the first character after the parsed |
441 | | * pattern, or the end of the iteration if all characters are |
442 | | * parsed. |
443 | | * @param symbols symbol table to use to parse and dereference |
444 | | * variables, or null if none. |
445 | | * @param rebuiltPat the pattern that was parsed, rebuilt or |
446 | | * copied from the input pattern, as appropriate. |
447 | | * @param options a bit mask of zero or more of the following: |
448 | | * IGNORE_SPACE, CASE. |
449 | | */ |
450 | | void UnicodeSet::applyPattern(RuleCharacterIterator& chars, |
451 | | const SymbolTable* symbols, |
452 | | UnicodeString& rebuiltPat, |
453 | | uint32_t options, |
454 | | UnicodeSet& (UnicodeSet::*caseClosure)(int32_t attribute), |
455 | | int32_t depth, |
456 | 0 | UErrorCode& ec) { |
457 | 0 | if (U_FAILURE(ec)) return; |
458 | 0 | if (depth > MAX_DEPTH) { |
459 | 0 | ec = U_ILLEGAL_ARGUMENT_ERROR; |
460 | 0 | return; |
461 | 0 | } |
462 | 0 | |
463 | 0 | // Syntax characters: [ ] ^ - & { } |
464 | 0 | |
465 | 0 | // Recognized special forms for chars, sets: c-c s-s s&s |
466 | 0 | |
467 | 0 | int32_t opts = RuleCharacterIterator::PARSE_VARIABLES | |
468 | 0 | RuleCharacterIterator::PARSE_ESCAPES; |
469 | 0 | if ((options & USET_IGNORE_SPACE) != 0) { |
470 | 0 | opts |= RuleCharacterIterator::SKIP_WHITESPACE; |
471 | 0 | } |
472 | 0 |
|
473 | 0 | UnicodeString patLocal, buf; |
474 | 0 | UBool usePat = FALSE; |
475 | 0 | UnicodeSetPointer scratch; |
476 | 0 | RuleCharacterIterator::Pos backup; |
477 | 0 |
|
478 | 0 | // mode: 0=before [, 1=between [...], 2=after ] |
479 | 0 | // lastItem: 0=none, 1=char, 2=set |
480 | 0 | int8_t lastItem = 0, mode = 0; |
481 | 0 | UChar32 lastChar = 0; |
482 | 0 | UChar op = 0; |
483 | 0 |
|
484 | 0 | UBool invert = FALSE; |
485 | 0 |
|
486 | 0 | clear(); |
487 | 0 |
|
488 | 0 | while (mode != 2 && !chars.atEnd()) { |
489 | 0 | U_ASSERT((lastItem == 0 && op == 0) || |
490 | 0 | (lastItem == 1 && (op == 0 || op == HYPHEN /*'-'*/)) || |
491 | 0 | (lastItem == 2 && (op == 0 || op == HYPHEN /*'-'*/ || |
492 | 0 | op == INTERSECTION /*'&'*/))); |
493 | 0 |
|
494 | 0 | UChar32 c = 0; |
495 | 0 | UBool literal = FALSE; |
496 | 0 | UnicodeSet* nested = 0; // alias - do not delete |
497 | 0 |
|
498 | 0 | // -------- Check for property pattern |
499 | 0 |
|
500 | 0 | // setMode: 0=none, 1=unicodeset, 2=propertypat, 3=preparsed |
501 | 0 | int8_t setMode = 0; |
502 | 0 | if (resemblesPropertyPattern(chars, opts)) { |
503 | 0 | setMode = 2; |
504 | 0 | } |
505 | 0 | |
506 | 0 | // -------- Parse '[' of opening delimiter OR nested set. |
507 | 0 | // If there is a nested set, use `setMode' to define how |
508 | 0 | // the set should be parsed. If the '[' is part of the |
509 | 0 | // opening delimiter for this pattern, parse special |
510 | 0 | // strings "[", "[^", "[-", and "[^-". Check for stand-in |
511 | 0 | // characters representing a nested set in the symbol |
512 | 0 | // table. |
513 | 0 | |
514 | 0 | else { |
515 | 0 | // Prepare to backup if necessary |
516 | 0 | chars.getPos(backup); |
517 | 0 | c = chars.next(opts, literal, ec); |
518 | 0 | if (U_FAILURE(ec)) return; |
519 | 0 | |
520 | 0 | if (c == 0x5B /*'['*/ && !literal) { |
521 | 0 | if (mode == 1) { |
522 | 0 | chars.setPos(backup); // backup |
523 | 0 | setMode = 1; |
524 | 0 | } else { |
525 | 0 | // Handle opening '[' delimiter |
526 | 0 | mode = 1; |
527 | 0 | patLocal.append((UChar) 0x5B /*'['*/); |
528 | 0 | chars.getPos(backup); // prepare to backup |
529 | 0 | c = chars.next(opts, literal, ec); |
530 | 0 | if (U_FAILURE(ec)) return; |
531 | 0 | if (c == 0x5E /*'^'*/ && !literal) { |
532 | 0 | invert = TRUE; |
533 | 0 | patLocal.append((UChar) 0x5E /*'^'*/); |
534 | 0 | chars.getPos(backup); // prepare to backup |
535 | 0 | c = chars.next(opts, literal, ec); |
536 | 0 | if (U_FAILURE(ec)) return; |
537 | 0 | } |
538 | 0 | // Fall through to handle special leading '-'; |
539 | 0 | // otherwise restart loop for nested [], \p{}, etc. |
540 | 0 | if (c == HYPHEN /*'-'*/) { |
541 | 0 | literal = TRUE; |
542 | 0 | // Fall through to handle literal '-' below |
543 | 0 | } else { |
544 | 0 | chars.setPos(backup); // backup |
545 | 0 | continue; |
546 | 0 | } |
547 | 0 | } |
548 | 0 | } else if (symbols != 0) { |
549 | 0 | const UnicodeFunctor *m = symbols->lookupMatcher(c); |
550 | 0 | if (m != 0) { |
551 | 0 | const UnicodeSet *ms = dynamic_cast<const UnicodeSet *>(m); |
552 | 0 | if (ms == NULL) { |
553 | 0 | ec = U_MALFORMED_SET; |
554 | 0 | return; |
555 | 0 | } |
556 | 0 | // casting away const, but `nested' won't be modified |
557 | 0 | // (important not to modify stored set) |
558 | 0 | nested = const_cast<UnicodeSet*>(ms); |
559 | 0 | setMode = 3; |
560 | 0 | } |
561 | 0 | } |
562 | 0 | } |
563 | 0 |
|
564 | 0 | // -------- Handle a nested set. This either is inline in |
565 | 0 | // the pattern or represented by a stand-in that has |
566 | 0 | // previously been parsed and was looked up in the symbol |
567 | 0 | // table. |
568 | 0 |
|
569 | 0 | if (setMode != 0) { |
570 | 0 | if (lastItem == 1) { |
571 | 0 | if (op != 0) { |
572 | 0 | // syntaxError(chars, "Char expected after operator"); |
573 | 0 | ec = U_MALFORMED_SET; |
574 | 0 | return; |
575 | 0 | } |
576 | 0 | add(lastChar, lastChar); |
577 | 0 | _appendToPat(patLocal, lastChar, FALSE); |
578 | 0 | lastItem = 0; |
579 | 0 | op = 0; |
580 | 0 | } |
581 | 0 |
|
582 | 0 | if (op == HYPHEN /*'-'*/ || op == INTERSECTION /*'&'*/) { |
583 | 0 | patLocal.append(op); |
584 | 0 | } |
585 | 0 |
|
586 | 0 | if (nested == 0) { |
587 | 0 | // lazy allocation |
588 | 0 | if (!scratch.allocate()) { |
589 | 0 | ec = U_MEMORY_ALLOCATION_ERROR; |
590 | 0 | return; |
591 | 0 | } |
592 | 0 | nested = scratch.pointer(); |
593 | 0 | } |
594 | 0 | switch (setMode) { |
595 | 0 | case 1: |
596 | 0 | nested->applyPattern(chars, symbols, patLocal, options, caseClosure, depth + 1, ec); |
597 | 0 | break; |
598 | 0 | case 2: |
599 | 0 | chars.skipIgnored(opts); |
600 | 0 | nested->applyPropertyPattern(chars, patLocal, ec); |
601 | 0 | if (U_FAILURE(ec)) return; |
602 | 0 | break; |
603 | 0 | case 3: // `nested' already parsed |
604 | 0 | nested->_toPattern(patLocal, FALSE); |
605 | 0 | break; |
606 | 0 | } |
607 | 0 |
|
608 | 0 | usePat = TRUE; |
609 | 0 |
|
610 | 0 | if (mode == 0) { |
611 | 0 | // Entire pattern is a category; leave parse loop |
612 | 0 | *this = *nested; |
613 | 0 | mode = 2; |
614 | 0 | break; |
615 | 0 | } |
616 | 0 | |
617 | 0 | switch (op) { |
618 | 0 | case HYPHEN: /*'-'*/ |
619 | 0 | removeAll(*nested); |
620 | 0 | break; |
621 | 0 | case INTERSECTION: /*'&'*/ |
622 | 0 | retainAll(*nested); |
623 | 0 | break; |
624 | 0 | case 0: |
625 | 0 | addAll(*nested); |
626 | 0 | break; |
627 | 0 | } |
628 | 0 | |
629 | 0 | op = 0; |
630 | 0 | lastItem = 2; |
631 | 0 |
|
632 | 0 | continue; |
633 | 0 | } |
634 | 0 | |
635 | 0 | if (mode == 0) { |
636 | 0 | // syntaxError(chars, "Missing '['"); |
637 | 0 | ec = U_MALFORMED_SET; |
638 | 0 | return; |
639 | 0 | } |
640 | 0 | |
641 | 0 | // -------- Parse special (syntax) characters. If the |
642 | 0 | // current character is not special, or if it is escaped, |
643 | 0 | // then fall through and handle it below. |
644 | 0 | |
645 | 0 | if (!literal) { |
646 | 0 | switch (c) { |
647 | 0 | case 0x5D /*']'*/: |
648 | 0 | if (lastItem == 1) { |
649 | 0 | add(lastChar, lastChar); |
650 | 0 | _appendToPat(patLocal, lastChar, FALSE); |
651 | 0 | } |
652 | 0 | // Treat final trailing '-' as a literal |
653 | 0 | if (op == HYPHEN /*'-'*/) { |
654 | 0 | add(op, op); |
655 | 0 | patLocal.append(op); |
656 | 0 | } else if (op == INTERSECTION /*'&'*/) { |
657 | 0 | // syntaxError(chars, "Trailing '&'"); |
658 | 0 | ec = U_MALFORMED_SET; |
659 | 0 | return; |
660 | 0 | } |
661 | 0 | patLocal.append((UChar) 0x5D /*']'*/); |
662 | 0 | mode = 2; |
663 | 0 | continue; |
664 | 0 | case HYPHEN /*'-'*/: |
665 | 0 | if (op == 0) { |
666 | 0 | if (lastItem != 0) { |
667 | 0 | op = (UChar) c; |
668 | 0 | continue; |
669 | 0 | } else { |
670 | 0 | // Treat final trailing '-' as a literal |
671 | 0 | add(c, c); |
672 | 0 | c = chars.next(opts, literal, ec); |
673 | 0 | if (U_FAILURE(ec)) return; |
674 | 0 | if (c == 0x5D /*']'*/ && !literal) { |
675 | 0 | patLocal.append(HYPHEN_RIGHT_BRACE, 2); |
676 | 0 | mode = 2; |
677 | 0 | continue; |
678 | 0 | } |
679 | 0 | } |
680 | 0 | } |
681 | 0 | // syntaxError(chars, "'-' not after char or set"); |
682 | 0 | ec = U_MALFORMED_SET; |
683 | 0 | return; |
684 | 0 | case INTERSECTION /*'&'*/: |
685 | 0 | if (lastItem == 2 && op == 0) { |
686 | 0 | op = (UChar) c; |
687 | 0 | continue; |
688 | 0 | } |
689 | 0 | // syntaxError(chars, "'&' not after set"); |
690 | 0 | ec = U_MALFORMED_SET; |
691 | 0 | return; |
692 | 0 | case 0x5E /*'^'*/: |
693 | 0 | // syntaxError(chars, "'^' not after '['"); |
694 | 0 | ec = U_MALFORMED_SET; |
695 | 0 | return; |
696 | 0 | case 0x7B /*'{'*/: |
697 | 0 | if (op != 0) { |
698 | 0 | // syntaxError(chars, "Missing operand after operator"); |
699 | 0 | ec = U_MALFORMED_SET; |
700 | 0 | return; |
701 | 0 | } |
702 | 0 | if (lastItem == 1) { |
703 | 0 | add(lastChar, lastChar); |
704 | 0 | _appendToPat(patLocal, lastChar, FALSE); |
705 | 0 | } |
706 | 0 | lastItem = 0; |
707 | 0 | buf.truncate(0); |
708 | 0 | { |
709 | 0 | UBool ok = FALSE; |
710 | 0 | while (!chars.atEnd()) { |
711 | 0 | c = chars.next(opts, literal, ec); |
712 | 0 | if (U_FAILURE(ec)) return; |
713 | 0 | if (c == 0x7D /*'}'*/ && !literal) { |
714 | 0 | ok = TRUE; |
715 | 0 | break; |
716 | 0 | } |
717 | 0 | buf.append(c); |
718 | 0 | } |
719 | 0 | if (buf.length() < 1 || !ok) { |
720 | 0 | // syntaxError(chars, "Invalid multicharacter string"); |
721 | 0 | ec = U_MALFORMED_SET; |
722 | 0 | return; |
723 | 0 | } |
724 | 0 | } |
725 | 0 | // We have new string. Add it to set and continue; |
726 | 0 | // we don't need to drop through to the further |
727 | 0 | // processing |
728 | 0 | add(buf); |
729 | 0 | patLocal.append((UChar) 0x7B /*'{'*/); |
730 | 0 | _appendToPat(patLocal, buf, FALSE); |
731 | 0 | patLocal.append((UChar) 0x7D /*'}'*/); |
732 | 0 | continue; |
733 | 0 | case SymbolTable::SYMBOL_REF: |
734 | 0 | // symbols nosymbols |
735 | 0 | // [a-$] error error (ambiguous) |
736 | 0 | // [a$] anchor anchor |
737 | 0 | // [a-$x] var "x"* literal '$' |
738 | 0 | // [a-$.] error literal '$' |
739 | 0 | // *We won't get here in the case of var "x" |
740 | 0 | { |
741 | 0 | chars.getPos(backup); |
742 | 0 | c = chars.next(opts, literal, ec); |
743 | 0 | if (U_FAILURE(ec)) return; |
744 | 0 | UBool anchor = (c == 0x5D /*']'*/ && !literal); |
745 | 0 | if (symbols == 0 && !anchor) { |
746 | 0 | c = SymbolTable::SYMBOL_REF; |
747 | 0 | chars.setPos(backup); |
748 | 0 | break; // literal '$' |
749 | 0 | } |
750 | 0 | if (anchor && op == 0) { |
751 | 0 | if (lastItem == 1) { |
752 | 0 | add(lastChar, lastChar); |
753 | 0 | _appendToPat(patLocal, lastChar, FALSE); |
754 | 0 | } |
755 | 0 | add(U_ETHER); |
756 | 0 | usePat = TRUE; |
757 | 0 | patLocal.append((UChar) SymbolTable::SYMBOL_REF); |
758 | 0 | patLocal.append((UChar) 0x5D /*']'*/); |
759 | 0 | mode = 2; |
760 | 0 | continue; |
761 | 0 | } |
762 | 0 | // syntaxError(chars, "Unquoted '$'"); |
763 | 0 | ec = U_MALFORMED_SET; |
764 | 0 | return; |
765 | 0 | } |
766 | 0 | default: |
767 | 0 | break; |
768 | 0 | } |
769 | 0 | } |
770 | 0 | |
771 | 0 | // -------- Parse literal characters. This includes both |
772 | 0 | // escaped chars ("\u4E01") and non-syntax characters |
773 | 0 | // ("a"). |
774 | 0 | |
775 | 0 | switch (lastItem) { |
776 | 0 | case 0: |
777 | 0 | lastItem = 1; |
778 | 0 | lastChar = c; |
779 | 0 | break; |
780 | 0 | case 1: |
781 | 0 | if (op == HYPHEN /*'-'*/) { |
782 | 0 | if (lastChar >= c) { |
783 | 0 | // Don't allow redundant (a-a) or empty (b-a) ranges; |
784 | 0 | // these are most likely typos. |
785 | 0 | // syntaxError(chars, "Invalid range"); |
786 | 0 | ec = U_MALFORMED_SET; |
787 | 0 | return; |
788 | 0 | } |
789 | 0 | add(lastChar, c); |
790 | 0 | _appendToPat(patLocal, lastChar, FALSE); |
791 | 0 | patLocal.append(op); |
792 | 0 | _appendToPat(patLocal, c, FALSE); |
793 | 0 | lastItem = 0; |
794 | 0 | op = 0; |
795 | 0 | } else { |
796 | 0 | add(lastChar, lastChar); |
797 | 0 | _appendToPat(patLocal, lastChar, FALSE); |
798 | 0 | lastChar = c; |
799 | 0 | } |
800 | 0 | break; |
801 | 0 | case 2: |
802 | 0 | if (op != 0) { |
803 | 0 | // syntaxError(chars, "Set expected after operator"); |
804 | 0 | ec = U_MALFORMED_SET; |
805 | 0 | return; |
806 | 0 | } |
807 | 0 | lastChar = c; |
808 | 0 | lastItem = 1; |
809 | 0 | break; |
810 | 0 | } |
811 | 0 | } |
812 | 0 |
|
813 | 0 | if (mode != 2) { |
814 | 0 | // syntaxError(chars, "Missing ']'"); |
815 | 0 | ec = U_MALFORMED_SET; |
816 | 0 | return; |
817 | 0 | } |
818 | 0 | |
819 | 0 | chars.skipIgnored(opts); |
820 | 0 |
|
821 | 0 | /** |
822 | 0 | * Handle global flags (invert, case insensitivity). If this |
823 | 0 | * pattern should be compiled case-insensitive, then we need |
824 | 0 | * to close over case BEFORE COMPLEMENTING. This makes |
825 | 0 | * patterns like /[^abc]/i work. |
826 | 0 | */ |
827 | 0 | if ((options & USET_CASE_INSENSITIVE) != 0) { |
828 | 0 | (this->*caseClosure)(USET_CASE_INSENSITIVE); |
829 | 0 | } |
830 | 0 | else if ((options & USET_ADD_CASE_MAPPINGS) != 0) { |
831 | 0 | (this->*caseClosure)(USET_ADD_CASE_MAPPINGS); |
832 | 0 | } |
833 | 0 | if (invert) { |
834 | 0 | complement(); |
835 | 0 | } |
836 | 0 |
|
837 | 0 | // Use the rebuilt pattern (patLocal) only if necessary. Prefer the |
838 | 0 | // generated pattern. |
839 | 0 | if (usePat) { |
840 | 0 | rebuiltPat.append(patLocal); |
841 | 0 | } else { |
842 | 0 | _generatePattern(rebuiltPat, FALSE); |
843 | 0 | } |
844 | 0 | if (isBogus() && U_SUCCESS(ec)) { |
845 | 0 | // We likely ran out of memory. AHHH! |
846 | 0 | ec = U_MEMORY_ALLOCATION_ERROR; |
847 | 0 | } |
848 | 0 | } |
849 | | |
850 | | //---------------------------------------------------------------- |
851 | | // Property set implementation |
852 | | //---------------------------------------------------------------- |
853 | | |
854 | | namespace { |
855 | | |
856 | 0 | static UBool numericValueFilter(UChar32 ch, void* context) { |
857 | 0 | return u_getNumericValue(ch) == *(double*)context; |
858 | 0 | } |
859 | | |
860 | 0 | static UBool generalCategoryMaskFilter(UChar32 ch, void* context) { |
861 | 0 | int32_t value = *(int32_t*)context; |
862 | 0 | return (U_GET_GC_MASK((UChar32) ch) & value) != 0; |
863 | 0 | } |
864 | | |
865 | 0 | static UBool versionFilter(UChar32 ch, void* context) { |
866 | 0 | static const UVersionInfo none = { 0, 0, 0, 0 }; |
867 | 0 | UVersionInfo v; |
868 | 0 | u_charAge(ch, v); |
869 | 0 | UVersionInfo* version = (UVersionInfo*)context; |
870 | 0 | return uprv_memcmp(&v, &none, sizeof(v)) > 0 && uprv_memcmp(&v, version, sizeof(v)) <= 0; |
871 | 0 | } |
872 | | |
873 | | typedef struct { |
874 | | UProperty prop; |
875 | | int32_t value; |
876 | | } IntPropertyContext; |
877 | | |
878 | 0 | static UBool intPropertyFilter(UChar32 ch, void* context) { |
879 | 0 | IntPropertyContext* c = (IntPropertyContext*)context; |
880 | 0 | return u_getIntPropertyValue((UChar32) ch, c->prop) == c->value; |
881 | 0 | } |
882 | | |
883 | 0 | static UBool scriptExtensionsFilter(UChar32 ch, void* context) { |
884 | 0 | return uscript_hasScript(ch, *(UScriptCode*)context); |
885 | 0 | } |
886 | | |
887 | | } // namespace |
888 | | |
889 | | /** |
890 | | * Generic filter-based scanning code for UCD property UnicodeSets. |
891 | | */ |
892 | | void UnicodeSet::applyFilter(UnicodeSet::Filter filter, |
893 | | void* context, |
894 | | int32_t src, |
895 | 0 | UErrorCode &status) { |
896 | 0 | if (U_FAILURE(status)) return; |
897 | 0 | |
898 | 0 | // Logically, walk through all Unicode characters, noting the start |
899 | 0 | // and end of each range for which filter.contain(c) is |
900 | 0 | // true. Add each range to a set. |
901 | 0 | // |
902 | 0 | // To improve performance, use an inclusions set which |
903 | 0 | // encodes information about character ranges that are known |
904 | 0 | // to have identical properties. |
905 | 0 | // getInclusions(src) contains exactly the first characters of |
906 | 0 | // same-value ranges for the given properties "source". |
907 | 0 | const UnicodeSet* inclusions = getInclusions(src, status); |
908 | 0 | if (U_FAILURE(status)) { |
909 | 0 | return; |
910 | 0 | } |
911 | 0 | |
912 | 0 | clear(); |
913 | 0 |
|
914 | 0 | UChar32 startHasProperty = -1; |
915 | 0 | int32_t limitRange = inclusions->getRangeCount(); |
916 | 0 |
|
917 | 0 | for (int j=0; j<limitRange; ++j) { |
918 | 0 | // get current range |
919 | 0 | UChar32 start = inclusions->getRangeStart(j); |
920 | 0 | UChar32 end = inclusions->getRangeEnd(j); |
921 | 0 |
|
922 | 0 | // for all the code points in the range, process |
923 | 0 | for (UChar32 ch = start; ch <= end; ++ch) { |
924 | 0 | // only add to this UnicodeSet on inflection points -- |
925 | 0 | // where the hasProperty value changes to false |
926 | 0 | if ((*filter)(ch, context)) { |
927 | 0 | if (startHasProperty < 0) { |
928 | 0 | startHasProperty = ch; |
929 | 0 | } |
930 | 0 | } else if (startHasProperty >= 0) { |
931 | 0 | add(startHasProperty, ch-1); |
932 | 0 | startHasProperty = -1; |
933 | 0 | } |
934 | 0 | } |
935 | 0 | } |
936 | 0 | if (startHasProperty >= 0) { |
937 | 0 | add((UChar32)startHasProperty, (UChar32)0x10FFFF); |
938 | 0 | } |
939 | 0 | if (isBogus() && U_SUCCESS(status)) { |
940 | 0 | // We likely ran out of memory. AHHH! |
941 | 0 | status = U_MEMORY_ALLOCATION_ERROR; |
942 | 0 | } |
943 | 0 | } |
944 | | |
945 | | namespace { |
946 | | |
947 | 0 | static UBool mungeCharName(char* dst, const char* src, int32_t dstCapacity) { |
948 | 0 | /* Note: we use ' ' in compiler code page */ |
949 | 0 | int32_t j = 0; |
950 | 0 | char ch; |
951 | 0 | --dstCapacity; /* make room for term. zero */ |
952 | 0 | while ((ch = *src++) != 0) { |
953 | 0 | if (ch == ' ' && (j==0 || (j>0 && dst[j-1]==' '))) { |
954 | 0 | continue; |
955 | 0 | } |
956 | 0 | if (j >= dstCapacity) return FALSE; |
957 | 0 | dst[j++] = ch; |
958 | 0 | } |
959 | 0 | if (j > 0 && dst[j-1] == ' ') --j; |
960 | 0 | dst[j] = 0; |
961 | 0 | return TRUE; |
962 | 0 | } |
963 | | |
964 | | } // namespace |
965 | | |
966 | | //---------------------------------------------------------------- |
967 | | // Property set API |
968 | | //---------------------------------------------------------------- |
969 | | |
970 | 0 | #define FAIL(ec) {ec=U_ILLEGAL_ARGUMENT_ERROR; return *this;} |
971 | | |
972 | | UnicodeSet& |
973 | 0 | UnicodeSet::applyIntPropertyValue(UProperty prop, int32_t value, UErrorCode& ec) { |
974 | 0 | if (U_FAILURE(ec) || isFrozen()) return *this; |
975 | 0 | |
976 | 0 | if (prop == UCHAR_GENERAL_CATEGORY_MASK) { |
977 | 0 | applyFilter(generalCategoryMaskFilter, &value, UPROPS_SRC_CHAR, ec); |
978 | 0 | } else if (prop == UCHAR_SCRIPT_EXTENSIONS) { |
979 | 0 | UScriptCode script = (UScriptCode)value; |
980 | 0 | applyFilter(scriptExtensionsFilter, &script, UPROPS_SRC_PROPSVEC, ec); |
981 | 0 | } else { |
982 | 0 | IntPropertyContext c = {prop, value}; |
983 | 0 | applyFilter(intPropertyFilter, &c, uprops_getSource(prop), ec); |
984 | 0 | } |
985 | 0 | return *this; |
986 | 0 | } |
987 | | |
988 | | UnicodeSet& |
989 | | UnicodeSet::applyPropertyAlias(const UnicodeString& prop, |
990 | | const UnicodeString& value, |
991 | 0 | UErrorCode& ec) { |
992 | 0 | if (U_FAILURE(ec) || isFrozen()) return *this; |
993 | 0 | |
994 | 0 | // prop and value used to be converted to char * using the default |
995 | 0 | // converter instead of the invariant conversion. |
996 | 0 | // This should not be necessary because all Unicode property and value |
997 | 0 | // names use only invariant characters. |
998 | 0 | // If there are any variant characters, then we won't find them anyway. |
999 | 0 | // Checking first avoids assertion failures in the conversion. |
1000 | 0 | if( !uprv_isInvariantUString(prop.getBuffer(), prop.length()) || |
1001 | 0 | !uprv_isInvariantUString(value.getBuffer(), value.length()) |
1002 | 0 | ) { |
1003 | 0 | FAIL(ec); |
1004 | 0 | } |
1005 | 0 | CharString pname, vname; |
1006 | 0 | pname.appendInvariantChars(prop, ec); |
1007 | 0 | vname.appendInvariantChars(value, ec); |
1008 | 0 | if (U_FAILURE(ec)) return *this; |
1009 | 0 | |
1010 | 0 | UProperty p; |
1011 | 0 | int32_t v; |
1012 | 0 | UBool invert = FALSE; |
1013 | 0 |
|
1014 | 0 | if (value.length() > 0) { |
1015 | 0 | p = u_getPropertyEnum(pname.data()); |
1016 | 0 | if (p == UCHAR_INVALID_CODE) FAIL(ec); |
1017 | 0 |
|
1018 | 0 | // Treat gc as gcm |
1019 | 0 | if (p == UCHAR_GENERAL_CATEGORY) { |
1020 | 0 | p = UCHAR_GENERAL_CATEGORY_MASK; |
1021 | 0 | } |
1022 | 0 |
|
1023 | 0 | if ((p >= UCHAR_BINARY_START && p < UCHAR_BINARY_LIMIT) || |
1024 | 0 | (p >= UCHAR_INT_START && p < UCHAR_INT_LIMIT) || |
1025 | 0 | (p >= UCHAR_MASK_START && p < UCHAR_MASK_LIMIT)) { |
1026 | 0 | v = u_getPropertyValueEnum(p, vname.data()); |
1027 | 0 | if (v == UCHAR_INVALID_CODE) { |
1028 | 0 | // Handle numeric CCC |
1029 | 0 | if (p == UCHAR_CANONICAL_COMBINING_CLASS || |
1030 | 0 | p == UCHAR_TRAIL_CANONICAL_COMBINING_CLASS || |
1031 | 0 | p == UCHAR_LEAD_CANONICAL_COMBINING_CLASS) { |
1032 | 0 | char* end; |
1033 | 0 | double value = uprv_strtod(vname.data(), &end); |
1034 | 0 | // Anything between 0 and 255 is valid even if unused. |
1035 | 0 | // Cast double->int only after range check. |
1036 | 0 | // We catch NaN here because comparing it with both 0 and 255 will be false |
1037 | 0 | // (as are all comparisons with NaN). |
1038 | 0 | if (*end != 0 || !(0 <= value && value <= 255) || |
1039 | 0 | (v = (int32_t)value) != value) { |
1040 | 0 | // non-integral value or outside 0..255, or trailing junk |
1041 | 0 | FAIL(ec); |
1042 | 0 | } |
1043 | 0 | } else { |
1044 | 0 | FAIL(ec); |
1045 | 0 | } |
1046 | 0 | } |
1047 | 0 | } |
1048 | 0 |
|
1049 | 0 | else { |
1050 | 0 |
|
1051 | 0 | switch (p) { |
1052 | 0 | case UCHAR_NUMERIC_VALUE: |
1053 | 0 | { |
1054 | 0 | char* end; |
1055 | 0 | double value = uprv_strtod(vname.data(), &end); |
1056 | 0 | if (*end != 0) { |
1057 | 0 | FAIL(ec); |
1058 | 0 | } |
1059 | 0 | applyFilter(numericValueFilter, &value, UPROPS_SRC_CHAR, ec); |
1060 | 0 | return *this; |
1061 | 0 | } |
1062 | 0 | case UCHAR_NAME: |
1063 | 0 | { |
1064 | 0 | // Must munge name, since u_charFromName() does not do |
1065 | 0 | // 'loose' matching. |
1066 | 0 | char buf[128]; // it suffices that this be > uprv_getMaxCharNameLength |
1067 | 0 | if (!mungeCharName(buf, vname.data(), sizeof(buf))) FAIL(ec); |
1068 | 0 | UChar32 ch = u_charFromName(U_EXTENDED_CHAR_NAME, buf, &ec); |
1069 | 0 | if (U_SUCCESS(ec)) { |
1070 | 0 | clear(); |
1071 | 0 | add(ch); |
1072 | 0 | return *this; |
1073 | 0 | } else { |
1074 | 0 | FAIL(ec); |
1075 | 0 | } |
1076 | 0 | } |
1077 | 0 | case UCHAR_UNICODE_1_NAME: |
1078 | 0 | // ICU 49 deprecates the Unicode_1_Name property APIs. |
1079 | 0 | FAIL(ec); |
1080 | 0 | case UCHAR_AGE: |
1081 | 0 | { |
1082 | 0 | // Must munge name, since u_versionFromString() does not do |
1083 | 0 | // 'loose' matching. |
1084 | 0 | char buf[128]; |
1085 | 0 | if (!mungeCharName(buf, vname.data(), sizeof(buf))) FAIL(ec); |
1086 | 0 | UVersionInfo version; |
1087 | 0 | u_versionFromString(version, buf); |
1088 | 0 | applyFilter(versionFilter, &version, UPROPS_SRC_PROPSVEC, ec); |
1089 | 0 | return *this; |
1090 | 0 | } |
1091 | 0 | case UCHAR_SCRIPT_EXTENSIONS: |
1092 | 0 | v = u_getPropertyValueEnum(UCHAR_SCRIPT, vname.data()); |
1093 | 0 | if (v == UCHAR_INVALID_CODE) { |
1094 | 0 | FAIL(ec); |
1095 | 0 | } |
1096 | 0 | // fall through to calling applyIntPropertyValue() |
1097 | 0 | break; |
1098 | 0 | default: |
1099 | 0 | // p is a non-binary, non-enumerated property that we |
1100 | 0 | // don't support (yet). |
1101 | 0 | FAIL(ec); |
1102 | 0 | } |
1103 | 0 | } |
1104 | 0 | } |
1105 | 0 |
|
1106 | 0 | else { |
1107 | 0 | // value is empty. Interpret as General Category, Script, or |
1108 | 0 | // Binary property. |
1109 | 0 | p = UCHAR_GENERAL_CATEGORY_MASK; |
1110 | 0 | v = u_getPropertyValueEnum(p, pname.data()); |
1111 | 0 | if (v == UCHAR_INVALID_CODE) { |
1112 | 0 | p = UCHAR_SCRIPT; |
1113 | 0 | v = u_getPropertyValueEnum(p, pname.data()); |
1114 | 0 | if (v == UCHAR_INVALID_CODE) { |
1115 | 0 | p = u_getPropertyEnum(pname.data()); |
1116 | 0 | if (p >= UCHAR_BINARY_START && p < UCHAR_BINARY_LIMIT) { |
1117 | 0 | v = 1; |
1118 | 0 | } else if (0 == uprv_comparePropertyNames(ANY, pname.data())) { |
1119 | 0 | set(MIN_VALUE, MAX_VALUE); |
1120 | 0 | return *this; |
1121 | 0 | } else if (0 == uprv_comparePropertyNames(ASCII, pname.data())) { |
1122 | 0 | set(0, 0x7F); |
1123 | 0 | return *this; |
1124 | 0 | } else if (0 == uprv_comparePropertyNames(ASSIGNED, pname.data())) { |
1125 | 0 | // [:Assigned:]=[:^Cn:] |
1126 | 0 | p = UCHAR_GENERAL_CATEGORY_MASK; |
1127 | 0 | v = U_GC_CN_MASK; |
1128 | 0 | invert = TRUE; |
1129 | 0 | } else { |
1130 | 0 | FAIL(ec); |
1131 | 0 | } |
1132 | 0 | } |
1133 | 0 | } |
1134 | 0 | } |
1135 | 0 |
|
1136 | 0 | applyIntPropertyValue(p, v, ec); |
1137 | 0 | if(invert) { |
1138 | 0 | complement(); |
1139 | 0 | } |
1140 | 0 |
|
1141 | 0 | if (isBogus() && U_SUCCESS(ec)) { |
1142 | 0 | // We likely ran out of memory. AHHH! |
1143 | 0 | ec = U_MEMORY_ALLOCATION_ERROR; |
1144 | 0 | } |
1145 | 0 | return *this; |
1146 | 0 | } |
1147 | | |
1148 | | //---------------------------------------------------------------- |
1149 | | // Property set patterns |
1150 | | //---------------------------------------------------------------- |
1151 | | |
1152 | | /** |
1153 | | * Return true if the given position, in the given pattern, appears |
1154 | | * to be the start of a property set pattern. |
1155 | | */ |
1156 | | UBool UnicodeSet::resemblesPropertyPattern(const UnicodeString& pattern, |
1157 | 0 | int32_t pos) { |
1158 | 0 | // Patterns are at least 5 characters long |
1159 | 0 | if ((pos+5) > pattern.length()) { |
1160 | 0 | return FALSE; |
1161 | 0 | } |
1162 | 0 |
|
1163 | 0 | // Look for an opening [:, [:^, \p, or \P |
1164 | 0 | return isPOSIXOpen(pattern, pos) || isPerlOpen(pattern, pos) || isNameOpen(pattern, pos); |
1165 | 0 | } |
1166 | | |
1167 | | /** |
1168 | | * Return true if the given iterator appears to point at a |
1169 | | * property pattern. Regardless of the result, return with the |
1170 | | * iterator unchanged. |
1171 | | * @param chars iterator over the pattern characters. Upon return |
1172 | | * it will be unchanged. |
1173 | | * @param iterOpts RuleCharacterIterator options |
1174 | | */ |
1175 | | UBool UnicodeSet::resemblesPropertyPattern(RuleCharacterIterator& chars, |
1176 | 0 | int32_t iterOpts) { |
1177 | 0 | // NOTE: literal will always be FALSE, because we don't parse escapes. |
1178 | 0 | UBool result = FALSE, literal; |
1179 | 0 | UErrorCode ec = U_ZERO_ERROR; |
1180 | 0 | iterOpts &= ~RuleCharacterIterator::PARSE_ESCAPES; |
1181 | 0 | RuleCharacterIterator::Pos pos; |
1182 | 0 | chars.getPos(pos); |
1183 | 0 | UChar32 c = chars.next(iterOpts, literal, ec); |
1184 | 0 | if (c == 0x5B /*'['*/ || c == 0x5C /*'\\'*/) { |
1185 | 0 | UChar32 d = chars.next(iterOpts & ~RuleCharacterIterator::SKIP_WHITESPACE, |
1186 | 0 | literal, ec); |
1187 | 0 | result = (c == 0x5B /*'['*/) ? (d == 0x3A /*':'*/) : |
1188 | 0 | (d == 0x4E /*'N'*/ || d == 0x70 /*'p'*/ || d == 0x50 /*'P'*/); |
1189 | 0 | } |
1190 | 0 | chars.setPos(pos); |
1191 | 0 | return result && U_SUCCESS(ec); |
1192 | 0 | } |
1193 | | |
1194 | | /** |
1195 | | * Parse the given property pattern at the given parse position. |
1196 | | */ |
1197 | | UnicodeSet& UnicodeSet::applyPropertyPattern(const UnicodeString& pattern, |
1198 | | ParsePosition& ppos, |
1199 | 0 | UErrorCode &ec) { |
1200 | 0 | int32_t pos = ppos.getIndex(); |
1201 | 0 |
|
1202 | 0 | UBool posix = FALSE; // true for [:pat:], false for \p{pat} \P{pat} \N{pat} |
1203 | 0 | UBool isName = FALSE; // true for \N{pat}, o/w false |
1204 | 0 | UBool invert = FALSE; |
1205 | 0 |
|
1206 | 0 | if (U_FAILURE(ec)) return *this; |
1207 | 0 | |
1208 | 0 | // Minimum length is 5 characters, e.g. \p{L} |
1209 | 0 | if ((pos+5) > pattern.length()) { |
1210 | 0 | FAIL(ec); |
1211 | 0 | } |
1212 | 0 |
|
1213 | 0 | // On entry, ppos should point to one of the following locations: |
1214 | 0 | // Look for an opening [:, [:^, \p, or \P |
1215 | 0 | if (isPOSIXOpen(pattern, pos)) { |
1216 | 0 | posix = TRUE; |
1217 | 0 | pos += 2; |
1218 | 0 | pos = ICU_Utility::skipWhitespace(pattern, pos); |
1219 | 0 | if (pos < pattern.length() && pattern.charAt(pos) == COMPLEMENT) { |
1220 | 0 | ++pos; |
1221 | 0 | invert = TRUE; |
1222 | 0 | } |
1223 | 0 | } else if (isPerlOpen(pattern, pos) || isNameOpen(pattern, pos)) { |
1224 | 0 | UChar c = pattern.charAt(pos+1); |
1225 | 0 | invert = (c == UPPER_P); |
1226 | 0 | isName = (c == UPPER_N); |
1227 | 0 | pos += 2; |
1228 | 0 | pos = ICU_Utility::skipWhitespace(pattern, pos); |
1229 | 0 | if (pos == pattern.length() || pattern.charAt(pos++) != OPEN_BRACE) { |
1230 | 0 | // Syntax error; "\p" or "\P" not followed by "{" |
1231 | 0 | FAIL(ec); |
1232 | 0 | } |
1233 | 0 | } else { |
1234 | 0 | // Open delimiter not seen |
1235 | 0 | FAIL(ec); |
1236 | 0 | } |
1237 | 0 |
|
1238 | 0 | // Look for the matching close delimiter, either :] or } |
1239 | 0 | int32_t close; |
1240 | 0 | if (posix) { |
1241 | 0 | close = pattern.indexOf(POSIX_CLOSE, 2, pos); |
1242 | 0 | } else { |
1243 | 0 | close = pattern.indexOf(CLOSE_BRACE, pos); |
1244 | 0 | } |
1245 | 0 | if (close < 0) { |
1246 | 0 | // Syntax error; close delimiter missing |
1247 | 0 | FAIL(ec); |
1248 | 0 | } |
1249 | 0 |
|
1250 | 0 | // Look for an '=' sign. If this is present, we will parse a |
1251 | 0 | // medium \p{gc=Cf} or long \p{GeneralCategory=Format} |
1252 | 0 | // pattern. |
1253 | 0 | int32_t equals = pattern.indexOf(EQUALS, pos); |
1254 | 0 | UnicodeString propName, valueName; |
1255 | 0 | if (equals >= 0 && equals < close && !isName) { |
1256 | 0 | // Equals seen; parse medium/long pattern |
1257 | 0 | pattern.extractBetween(pos, equals, propName); |
1258 | 0 | pattern.extractBetween(equals+1, close, valueName); |
1259 | 0 | } |
1260 | 0 | |
1261 | 0 | else { |
1262 | 0 | // Handle case where no '=' is seen, and \N{} |
1263 | 0 | pattern.extractBetween(pos, close, propName); |
1264 | 0 | |
1265 | 0 | // Handle \N{name} |
1266 | 0 | if (isName) { |
1267 | 0 | // This is a little inefficient since it means we have to |
1268 | 0 | // parse NAME_PROP back to UCHAR_NAME even though we already |
1269 | 0 | // know it's UCHAR_NAME. If we refactor the API to |
1270 | 0 | // support args of (UProperty, char*) then we can remove |
1271 | 0 | // NAME_PROP and make this a little more efficient. |
1272 | 0 | valueName = propName; |
1273 | 0 | propName = UnicodeString(NAME_PROP, NAME_PROP_LENGTH, US_INV); |
1274 | 0 | } |
1275 | 0 | } |
1276 | 0 |
|
1277 | 0 | applyPropertyAlias(propName, valueName, ec); |
1278 | 0 |
|
1279 | 0 | if (U_SUCCESS(ec)) { |
1280 | 0 | if (invert) { |
1281 | 0 | complement(); |
1282 | 0 | } |
1283 | 0 | |
1284 | 0 | // Move to the limit position after the close delimiter if the |
1285 | 0 | // parse succeeded. |
1286 | 0 | ppos.setIndex(close + (posix ? 2 : 1)); |
1287 | 0 | } |
1288 | 0 |
|
1289 | 0 | return *this; |
1290 | 0 | } |
1291 | | |
1292 | | /** |
1293 | | * Parse a property pattern. |
1294 | | * @param chars iterator over the pattern characters. Upon return |
1295 | | * it will be advanced to the first character after the parsed |
1296 | | * pattern, or the end of the iteration if all characters are |
1297 | | * parsed. |
1298 | | * @param rebuiltPat the pattern that was parsed, rebuilt or |
1299 | | * copied from the input pattern, as appropriate. |
1300 | | */ |
1301 | | void UnicodeSet::applyPropertyPattern(RuleCharacterIterator& chars, |
1302 | | UnicodeString& rebuiltPat, |
1303 | 0 | UErrorCode& ec) { |
1304 | 0 | if (U_FAILURE(ec)) return; |
1305 | 0 | UnicodeString pattern; |
1306 | 0 | chars.lookahead(pattern); |
1307 | 0 | ParsePosition pos(0); |
1308 | 0 | applyPropertyPattern(pattern, pos, ec); |
1309 | 0 | if (U_FAILURE(ec)) return; |
1310 | 0 | if (pos.getIndex() == 0) { |
1311 | 0 | // syntaxError(chars, "Invalid property pattern"); |
1312 | 0 | ec = U_MALFORMED_SET; |
1313 | 0 | return; |
1314 | 0 | } |
1315 | 0 | chars.jumpahead(pos.getIndex()); |
1316 | 0 | rebuiltPat.append(pattern, 0, pos.getIndex()); |
1317 | 0 | } |
1318 | | |
1319 | | U_NAMESPACE_END |