/src/icu/source/common/uniset_closure.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) 2011, International Business Machines  | 
7  |  | *   Corporation and others.  All Rights Reserved.  | 
8  |  | *  | 
9  |  | *******************************************************************************  | 
10  |  | *   file name:  uniset_closure.cpp  | 
11  |  | *   encoding:   UTF-8  | 
12  |  | *   tab size:   8 (not used)  | 
13  |  | *   indentation:4  | 
14  |  | *  | 
15  |  | *   created on: 2011may30  | 
16  |  | *   created by: Markus W. Scherer  | 
17  |  | *  | 
18  |  | *   UnicodeSet::closeOver() and related methods moved here from uniset_props.cpp  | 
19  |  | *   to simplify dependencies.  | 
20  |  | *   In particular, this depends on the BreakIterator, but the BreakIterator  | 
21  |  | *   code also builds UnicodeSets from patterns and needs uniset_props.  | 
22  |  | */  | 
23  |  |  | 
24  |  | #include "unicode/brkiter.h"  | 
25  |  | #include "unicode/locid.h"  | 
26  |  | #include "unicode/parsepos.h"  | 
27  |  | #include "unicode/uniset.h"  | 
28  |  | #include "cmemory.h"  | 
29  |  | #include "ruleiter.h"  | 
30  |  | #include "ucase.h"  | 
31  |  | #include "util.h"  | 
32  |  | #include "uvector.h"  | 
33  |  |  | 
34  |  | U_NAMESPACE_BEGIN  | 
35  |  |  | 
36  |  | // TODO memory debugging provided inside uniset.cpp  | 
37  |  | // could be made available here but probably obsolete with use of modern  | 
38  |  | // memory leak checker tools  | 
39  |  | #define _dbgct(me)  | 
40  |  |  | 
41  |  | //----------------------------------------------------------------  | 
42  |  | // Constructors &c  | 
43  |  | //----------------------------------------------------------------  | 
44  |  |  | 
45  |  | UnicodeSet::UnicodeSet(const UnicodeString& pattern,  | 
46  |  |                        uint32_t options,  | 
47  |  |                        const SymbolTable* symbols,  | 
48  | 0  |                        UErrorCode& status) { | 
49  | 0  |     applyPattern(pattern, options, symbols, status);  | 
50  | 0  |     _dbgct(this);  | 
51  | 0  | }  | 
52  |  |  | 
53  |  | UnicodeSet::UnicodeSet(const UnicodeString& pattern, ParsePosition& pos,  | 
54  |  |                        uint32_t options,  | 
55  |  |                        const SymbolTable* symbols,  | 
56  | 0  |                        UErrorCode& status) { | 
57  | 0  |     applyPattern(pattern, pos, options, symbols, status);  | 
58  | 0  |     _dbgct(this);  | 
59  | 0  | }  | 
60  |  |  | 
61  |  | //----------------------------------------------------------------  | 
62  |  | // Public API  | 
63  |  | //----------------------------------------------------------------  | 
64  |  |  | 
65  |  | UnicodeSet& UnicodeSet::applyPattern(const UnicodeString& pattern,  | 
66  |  |                                      uint32_t options,  | 
67  |  |                                      const SymbolTable* symbols,  | 
68  | 0  |                                      UErrorCode& status) { | 
69  | 0  |     ParsePosition pos(0);  | 
70  | 0  |     applyPattern(pattern, pos, options, symbols, status);  | 
71  | 0  |     if (U_FAILURE(status)) return *this;  | 
72  |  |  | 
73  | 0  |     int32_t i = pos.getIndex();  | 
74  |  | 
  | 
75  | 0  |     if (options & USET_IGNORE_SPACE) { | 
76  |  |         // Skip over trailing whitespace  | 
77  | 0  |         ICU_Utility::skipWhitespace(pattern, i, TRUE);  | 
78  | 0  |     }  | 
79  |  | 
  | 
80  | 0  |     if (i != pattern.length()) { | 
81  | 0  |         status = U_ILLEGAL_ARGUMENT_ERROR;  | 
82  | 0  |     }  | 
83  | 0  |     return *this;  | 
84  | 0  | }  | 
85  |  |  | 
86  |  | UnicodeSet& UnicodeSet::applyPattern(const UnicodeString& pattern,  | 
87  |  |                               ParsePosition& pos,  | 
88  |  |                               uint32_t options,  | 
89  |  |                               const SymbolTable* symbols,  | 
90  | 0  |                               UErrorCode& status) { | 
91  | 0  |     if (U_FAILURE(status)) { | 
92  | 0  |         return *this;  | 
93  | 0  |     }  | 
94  | 0  |     if (isFrozen()) { | 
95  | 0  |         status = U_NO_WRITE_PERMISSION;  | 
96  | 0  |         return *this;  | 
97  | 0  |     }  | 
98  |  |     // Need to build the pattern in a temporary string because  | 
99  |  |     // _applyPattern calls add() etc., which set pat to empty.  | 
100  | 0  |     UnicodeString rebuiltPat;  | 
101  | 0  |     RuleCharacterIterator chars(pattern, symbols, pos);  | 
102  | 0  |     applyPattern(chars, symbols, rebuiltPat, options, &UnicodeSet::closeOver, 0, status);  | 
103  | 0  |     if (U_FAILURE(status)) return *this;  | 
104  | 0  |     if (chars.inVariable()) { | 
105  |  |         // syntaxError(chars, "Extra chars in variable value");  | 
106  | 0  |         status = U_MALFORMED_SET;  | 
107  | 0  |         return *this;  | 
108  | 0  |     }  | 
109  | 0  |     setPattern(rebuiltPat);  | 
110  | 0  |     return *this;  | 
111  | 0  | }  | 
112  |  |  | 
113  |  | // USetAdder implementation  | 
114  |  | // Does not use uset.h to reduce code dependencies  | 
115  |  | static void U_CALLCONV  | 
116  | 0  | _set_add(USet *set, UChar32 c) { | 
117  | 0  |     ((UnicodeSet *)set)->add(c);  | 
118  | 0  | }  | 
119  |  |  | 
120  |  | static void U_CALLCONV  | 
121  | 0  | _set_addRange(USet *set, UChar32 start, UChar32 end) { | 
122  | 0  |     ((UnicodeSet *)set)->add(start, end);  | 
123  | 0  | }  | 
124  |  |  | 
125  |  | static void U_CALLCONV  | 
126  | 0  | _set_addString(USet *set, const UChar *str, int32_t length) { | 
127  | 0  |     ((UnicodeSet *)set)->add(UnicodeString((UBool)(length<0), str, length));  | 
128  | 0  | }  | 
129  |  |  | 
130  |  | //----------------------------------------------------------------  | 
131  |  | // Case folding API  | 
132  |  | //----------------------------------------------------------------  | 
133  |  |  | 
134  |  | // add the result of a full case mapping to the set  | 
135  |  | // use str as a temporary string to avoid constructing one  | 
136  |  | static inline void  | 
137  | 0  | addCaseMapping(UnicodeSet &set, int32_t result, const UChar *full, UnicodeString &str) { | 
138  | 0  |     if(result >= 0) { | 
139  | 0  |         if(result > UCASE_MAX_STRING_LENGTH) { | 
140  |  |             // add a single-code point case mapping  | 
141  | 0  |             set.add(result);  | 
142  | 0  |         } else { | 
143  |  |             // add a string case mapping from full with length result  | 
144  | 0  |             str.setTo((UBool)FALSE, full, result);  | 
145  | 0  |             set.add(str);  | 
146  | 0  |         }  | 
147  | 0  |     }  | 
148  |  |     // result < 0: the code point mapped to itself, no need to add it  | 
149  |  |     // see ucase.h  | 
150  | 0  | }  | 
151  |  |  | 
152  | 0  | UnicodeSet& UnicodeSet::closeOver(int32_t attribute) { | 
153  | 0  |     if (isFrozen() || isBogus()) { | 
154  | 0  |         return *this;  | 
155  | 0  |     }  | 
156  | 0  |     if (attribute & (USET_CASE_INSENSITIVE | USET_ADD_CASE_MAPPINGS)) { | 
157  | 0  |         { | 
158  | 0  |             UnicodeSet foldSet(*this);  | 
159  | 0  |             UnicodeString str;  | 
160  | 0  |             USetAdder sa = { | 
161  | 0  |                 foldSet.toUSet(),  | 
162  | 0  |                 _set_add,  | 
163  | 0  |                 _set_addRange,  | 
164  | 0  |                 _set_addString,  | 
165  | 0  |                 NULL, // don't need remove()  | 
166  |  |                 NULL // don't need removeRange()  | 
167  | 0  |             };  | 
168  |  |  | 
169  |  |             // start with input set to guarantee inclusion  | 
170  |  |             // USET_CASE: remove strings because the strings will actually be reduced (folded);  | 
171  |  |             //            therefore, start with no strings and add only those needed  | 
172  | 0  |             if ((attribute & USET_CASE_INSENSITIVE) && foldSet.hasStrings()) { | 
173  | 0  |                 foldSet.strings->removeAllElements();  | 
174  | 0  |             }  | 
175  |  | 
  | 
176  | 0  |             int32_t n = getRangeCount();  | 
177  | 0  |             UChar32 result;  | 
178  | 0  |             const UChar *full;  | 
179  |  | 
  | 
180  | 0  |             for (int32_t i=0; i<n; ++i) { | 
181  | 0  |                 UChar32 start = getRangeStart(i);  | 
182  | 0  |                 UChar32 end   = getRangeEnd(i);  | 
183  |  | 
  | 
184  | 0  |                 if (attribute & USET_CASE_INSENSITIVE) { | 
185  |  |                     // full case closure  | 
186  | 0  |                     for (UChar32 cp=start; cp<=end; ++cp) { | 
187  | 0  |                         ucase_addCaseClosure(cp, &sa);  | 
188  | 0  |                     }  | 
189  | 0  |                 } else { | 
190  |  |                     // add case mappings  | 
191  |  |                     // (does not add long s for regular s, or Kelvin for k, for example)  | 
192  | 0  |                     for (UChar32 cp=start; cp<=end; ++cp) { | 
193  | 0  |                         result = ucase_toFullLower(cp, NULL, NULL, &full, UCASE_LOC_ROOT);  | 
194  | 0  |                         addCaseMapping(foldSet, result, full, str);  | 
195  |  | 
  | 
196  | 0  |                         result = ucase_toFullTitle(cp, NULL, NULL, &full, UCASE_LOC_ROOT);  | 
197  | 0  |                         addCaseMapping(foldSet, result, full, str);  | 
198  |  | 
  | 
199  | 0  |                         result = ucase_toFullUpper(cp, NULL, NULL, &full, UCASE_LOC_ROOT);  | 
200  | 0  |                         addCaseMapping(foldSet, result, full, str);  | 
201  |  | 
  | 
202  | 0  |                         result = ucase_toFullFolding(cp, &full, 0);  | 
203  | 0  |                         addCaseMapping(foldSet, result, full, str);  | 
204  | 0  |                     }  | 
205  | 0  |                 }  | 
206  | 0  |             }  | 
207  | 0  |             if (hasStrings()) { | 
208  | 0  |                 if (attribute & USET_CASE_INSENSITIVE) { | 
209  | 0  |                     for (int32_t j=0; j<strings->size(); ++j) { | 
210  | 0  |                         str = *(const UnicodeString *) strings->elementAt(j);  | 
211  | 0  |                         str.foldCase();  | 
212  | 0  |                         if(!ucase_addStringCaseClosure(str.getBuffer(), str.length(), &sa)) { | 
213  | 0  |                             foldSet.add(str); // does not map to code points: add the folded string itself  | 
214  | 0  |                         }  | 
215  | 0  |                     }  | 
216  | 0  |                 } else { | 
217  | 0  |                     Locale root(""); | 
218  | 0  | #if !UCONFIG_NO_BREAK_ITERATION  | 
219  | 0  |                     UErrorCode status = U_ZERO_ERROR;  | 
220  | 0  |                     BreakIterator *bi = BreakIterator::createWordInstance(root, status);  | 
221  | 0  |                     if (U_SUCCESS(status)) { | 
222  | 0  | #endif  | 
223  | 0  |                         const UnicodeString *pStr;  | 
224  |  | 
  | 
225  | 0  |                         for (int32_t j=0; j<strings->size(); ++j) { | 
226  | 0  |                             pStr = (const UnicodeString *) strings->elementAt(j);  | 
227  | 0  |                             (str = *pStr).toLower(root);  | 
228  | 0  |                             foldSet.add(str);  | 
229  | 0  | #if !UCONFIG_NO_BREAK_ITERATION  | 
230  | 0  |                             (str = *pStr).toTitle(bi, root);  | 
231  | 0  |                             foldSet.add(str);  | 
232  | 0  | #endif  | 
233  | 0  |                             (str = *pStr).toUpper(root);  | 
234  | 0  |                             foldSet.add(str);  | 
235  | 0  |                             (str = *pStr).foldCase();  | 
236  | 0  |                             foldSet.add(str);  | 
237  | 0  |                         }  | 
238  | 0  | #if !UCONFIG_NO_BREAK_ITERATION  | 
239  | 0  |                     }  | 
240  | 0  |                     delete bi;  | 
241  | 0  | #endif  | 
242  | 0  |                 }  | 
243  | 0  |             }  | 
244  | 0  |             *this = foldSet;  | 
245  | 0  |         }  | 
246  | 0  |     }  | 
247  | 0  |     return *this;  | 
248  | 0  | }  | 
249  |  |  | 
250  |  | U_NAMESPACE_END  |