Coverage Report

Created: 2018-09-25 14:53

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