1 // Copyright (C) 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:   US-ASCII
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 // initial storage. Must be >= 0
35 // *** same as in uniset.cpp ! ***
36 #define START_EXTRA 16
37 
38 U_NAMESPACE_BEGIN
39 
40 // TODO memory debugging provided inside uniset.cpp
41 // could be made available here but probably obsolete with use of modern
42 // memory leak checker tools
43 #define _dbgct(me)
44 
45 //----------------------------------------------------------------
46 // Constructors &c
47 //----------------------------------------------------------------
48 
UnicodeSet(const UnicodeString & pattern,uint32_t options,const SymbolTable * symbols,UErrorCode & status)49 UnicodeSet::UnicodeSet(const UnicodeString& pattern,
50                        uint32_t options,
51                        const SymbolTable* symbols,
52                        UErrorCode& status) :
53     len(0), capacity(START_EXTRA), list(0), bmpSet(0), buffer(0),
54     bufferCapacity(0), patLen(0), pat(NULL), strings(NULL), stringSpan(NULL),
55     fFlags(0)
56 {
57     if(U_SUCCESS(status)){
58         list = (UChar32*) uprv_malloc(sizeof(UChar32) * capacity);
59         /* test for NULL */
60         if(list == NULL) {
61             status = U_MEMORY_ALLOCATION_ERROR;
62         }else{
63             allocateStrings(status);
64             applyPattern(pattern, options, symbols, status);
65         }
66     }
67     _dbgct(this);
68 }
69 
UnicodeSet(const UnicodeString & pattern,ParsePosition & pos,uint32_t options,const SymbolTable * symbols,UErrorCode & status)70 UnicodeSet::UnicodeSet(const UnicodeString& pattern, ParsePosition& pos,
71                        uint32_t options,
72                        const SymbolTable* symbols,
73                        UErrorCode& status) :
74     len(0), capacity(START_EXTRA), list(0), bmpSet(0), buffer(0),
75     bufferCapacity(0), patLen(0), pat(NULL), strings(NULL), stringSpan(NULL),
76     fFlags(0)
77 {
78     if(U_SUCCESS(status)){
79         list = (UChar32*) uprv_malloc(sizeof(UChar32) * capacity);
80         /* test for NULL */
81         if(list == NULL) {
82             status = U_MEMORY_ALLOCATION_ERROR;
83         }else{
84             allocateStrings(status);
85             applyPattern(pattern, pos, options, symbols, status);
86         }
87     }
88     _dbgct(this);
89 }
90 
91 //----------------------------------------------------------------
92 // Public API
93 //----------------------------------------------------------------
94 
applyPattern(const UnicodeString & pattern,uint32_t options,const SymbolTable * symbols,UErrorCode & status)95 UnicodeSet& UnicodeSet::applyPattern(const UnicodeString& pattern,
96                                      uint32_t options,
97                                      const SymbolTable* symbols,
98                                      UErrorCode& status) {
99     ParsePosition pos(0);
100     applyPattern(pattern, pos, options, symbols, status);
101     if (U_FAILURE(status)) return *this;
102 
103     int32_t i = pos.getIndex();
104 
105     if (options & USET_IGNORE_SPACE) {
106         // Skip over trailing whitespace
107         ICU_Utility::skipWhitespace(pattern, i, TRUE);
108     }
109 
110     if (i != pattern.length()) {
111         status = U_ILLEGAL_ARGUMENT_ERROR;
112     }
113     return *this;
114 }
115 
applyPattern(const UnicodeString & pattern,ParsePosition & pos,uint32_t options,const SymbolTable * symbols,UErrorCode & status)116 UnicodeSet& UnicodeSet::applyPattern(const UnicodeString& pattern,
117                               ParsePosition& pos,
118                               uint32_t options,
119                               const SymbolTable* symbols,
120                               UErrorCode& status) {
121     if (U_FAILURE(status)) {
122         return *this;
123     }
124     if (isFrozen()) {
125         status = U_NO_WRITE_PERMISSION;
126         return *this;
127     }
128     // Need to build the pattern in a temporary string because
129     // _applyPattern calls add() etc., which set pat to empty.
130     UnicodeString rebuiltPat;
131     RuleCharacterIterator chars(pattern, symbols, pos);
132     applyPattern(chars, symbols, rebuiltPat, options, &UnicodeSet::closeOver, status);
133     if (U_FAILURE(status)) return *this;
134     if (chars.inVariable()) {
135         // syntaxError(chars, "Extra chars in variable value");
136         status = U_MALFORMED_SET;
137         return *this;
138     }
139     setPattern(rebuiltPat);
140     return *this;
141 }
142 
143 // USetAdder implementation
144 // Does not use uset.h to reduce code dependencies
145 static void U_CALLCONV
_set_add(USet * set,UChar32 c)146 _set_add(USet *set, UChar32 c) {
147     ((UnicodeSet *)set)->add(c);
148 }
149 
150 static void U_CALLCONV
_set_addRange(USet * set,UChar32 start,UChar32 end)151 _set_addRange(USet *set, UChar32 start, UChar32 end) {
152     ((UnicodeSet *)set)->add(start, end);
153 }
154 
155 static void U_CALLCONV
_set_addString(USet * set,const UChar * str,int32_t length)156 _set_addString(USet *set, const UChar *str, int32_t length) {
157     ((UnicodeSet *)set)->add(UnicodeString((UBool)(length<0), str, length));
158 }
159 
160 //----------------------------------------------------------------
161 // Case folding API
162 //----------------------------------------------------------------
163 
164 // add the result of a full case mapping to the set
165 // use str as a temporary string to avoid constructing one
166 static inline void
addCaseMapping(UnicodeSet & set,int32_t result,const UChar * full,UnicodeString & str)167 addCaseMapping(UnicodeSet &set, int32_t result, const UChar *full, UnicodeString &str) {
168     if(result >= 0) {
169         if(result > UCASE_MAX_STRING_LENGTH) {
170             // add a single-code point case mapping
171             set.add(result);
172         } else {
173             // add a string case mapping from full with length result
174             str.setTo((UBool)FALSE, full, result);
175             set.add(str);
176         }
177     }
178     // result < 0: the code point mapped to itself, no need to add it
179     // see ucase.h
180 }
181 
closeOver(int32_t attribute)182 UnicodeSet& UnicodeSet::closeOver(int32_t attribute) {
183     if (isFrozen() || isBogus()) {
184         return *this;
185     }
186     if (attribute & (USET_CASE_INSENSITIVE | USET_ADD_CASE_MAPPINGS)) {
187         const UCaseProps *csp = ucase_getSingleton();
188         {
189             UnicodeSet foldSet(*this);
190             UnicodeString str;
191             USetAdder sa = {
192                 foldSet.toUSet(),
193                 _set_add,
194                 _set_addRange,
195                 _set_addString,
196                 NULL, // don't need remove()
197                 NULL // don't need removeRange()
198             };
199 
200             // start with input set to guarantee inclusion
201             // USET_CASE: remove strings because the strings will actually be reduced (folded);
202             //            therefore, start with no strings and add only those needed
203             if (attribute & USET_CASE_INSENSITIVE) {
204                 foldSet.strings->removeAllElements();
205             }
206 
207             int32_t n = getRangeCount();
208             UChar32 result;
209             const UChar *full;
210             int32_t locCache = 0;
211 
212             for (int32_t i=0; i<n; ++i) {
213                 UChar32 start = getRangeStart(i);
214                 UChar32 end   = getRangeEnd(i);
215 
216                 if (attribute & USET_CASE_INSENSITIVE) {
217                     // full case closure
218                     for (UChar32 cp=start; cp<=end; ++cp) {
219                         ucase_addCaseClosure(csp, cp, &sa);
220                     }
221                 } else {
222                     // add case mappings
223                     // (does not add long s for regular s, or Kelvin for k, for example)
224                     for (UChar32 cp=start; cp<=end; ++cp) {
225                         result = ucase_toFullLower(csp, cp, NULL, NULL, &full, "", &locCache);
226                         addCaseMapping(foldSet, result, full, str);
227 
228                         result = ucase_toFullTitle(csp, cp, NULL, NULL, &full, "", &locCache);
229                         addCaseMapping(foldSet, result, full, str);
230 
231                         result = ucase_toFullUpper(csp, cp, NULL, NULL, &full, "", &locCache);
232                         addCaseMapping(foldSet, result, full, str);
233 
234                         result = ucase_toFullFolding(csp, cp, &full, 0);
235                         addCaseMapping(foldSet, result, full, str);
236                     }
237                 }
238             }
239             if (strings != NULL && strings->size() > 0) {
240                 if (attribute & USET_CASE_INSENSITIVE) {
241                     for (int32_t j=0; j<strings->size(); ++j) {
242                         str = *(const UnicodeString *) strings->elementAt(j);
243                         str.foldCase();
244                         if(!ucase_addStringCaseClosure(csp, str.getBuffer(), str.length(), &sa)) {
245                             foldSet.add(str); // does not map to code points: add the folded string itself
246                         }
247                     }
248                 } else {
249                     Locale root("");
250 #if !UCONFIG_NO_BREAK_ITERATION
251                     UErrorCode status = U_ZERO_ERROR;
252                     BreakIterator *bi = BreakIterator::createWordInstance(root, status);
253                     if (U_SUCCESS(status)) {
254 #endif
255                         const UnicodeString *pStr;
256 
257                         for (int32_t j=0; j<strings->size(); ++j) {
258                             pStr = (const UnicodeString *) strings->elementAt(j);
259                             (str = *pStr).toLower(root);
260                             foldSet.add(str);
261 #if !UCONFIG_NO_BREAK_ITERATION
262                             (str = *pStr).toTitle(bi, root);
263                             foldSet.add(str);
264 #endif
265                             (str = *pStr).toUpper(root);
266                             foldSet.add(str);
267                             (str = *pStr).foldCase();
268                             foldSet.add(str);
269                         }
270 #if !UCONFIG_NO_BREAK_ITERATION
271                     }
272                     delete bi;
273 #endif
274                 }
275             }
276             *this = foldSet;
277         }
278     }
279     return *this;
280 }
281 
282 U_NAMESPACE_END
283