1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
|
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/*
*******************************************************************************
*
* Copyright (C) 2011, International Business Machines
* Corporation and others. All Rights Reserved.
*
*******************************************************************************
* file name: uniset_closure.cpp
* encoding: UTF-8
* tab size: 8 (not used)
* indentation:4
*
* created on: 2011may30
* created by: Markus W. Scherer
*
* UnicodeSet::closeOver() and related methods moved here from uniset_props.cpp
* to simplify dependencies.
* In particular, this depends on the BreakIterator, but the BreakIterator
* code also builds UnicodeSets from patterns and needs uniset_props.
*/
#include <_foundation_unicode/brkiter.h>
#include <_foundation_unicode/locid.h>
#include <_foundation_unicode/parsepos.h>
#include <_foundation_unicode/uniset.h>
#include <_foundation_unicode/utf16.h>
#include "cmemory.h"
#include "ruleiter.h"
#include "ucase.h"
#include "uprops.h"
#include "util.h"
#include "uvector.h"
U_NAMESPACE_BEGIN
// TODO memory debugging provided inside uniset.cpp
// could be made available here but probably obsolete with use of modern
// memory leak checker tools
#define _dbgct(me)
//----------------------------------------------------------------
// Constructors &c
//----------------------------------------------------------------
UnicodeSet::UnicodeSet(const UnicodeString& pattern,
uint32_t options,
const SymbolTable* symbols,
UErrorCode& status) {
applyPattern(pattern, options, symbols, status);
_dbgct(this);
}
UnicodeSet::UnicodeSet(const UnicodeString& pattern, ParsePosition& pos,
uint32_t options,
const SymbolTable* symbols,
UErrorCode& status) {
applyPattern(pattern, pos, options, symbols, status);
_dbgct(this);
}
//----------------------------------------------------------------
// Public API
//----------------------------------------------------------------
UnicodeSet& UnicodeSet::applyPattern(const UnicodeString& pattern,
uint32_t options,
const SymbolTable* symbols,
UErrorCode& status) {
ParsePosition pos(0);
applyPattern(pattern, pos, options, symbols, status);
if (U_FAILURE(status)) return *this;
int32_t i = pos.getIndex();
if (options & USET_IGNORE_SPACE) {
// Skip over trailing whitespace
ICU_Utility::skipWhitespace(pattern, i, true);
}
if (i != pattern.length()) {
status = U_ILLEGAL_ARGUMENT_ERROR;
}
return *this;
}
UnicodeSet& UnicodeSet::applyPattern(const UnicodeString& pattern,
ParsePosition& pos,
uint32_t options,
const SymbolTable* symbols,
UErrorCode& status) {
if (U_FAILURE(status)) {
return *this;
}
if (isFrozen()) {
status = U_NO_WRITE_PERMISSION;
return *this;
}
// Need to build the pattern in a temporary string because
// _applyPattern calls add() etc., which set pat to empty.
UnicodeString rebuiltPat;
RuleCharacterIterator chars(pattern, symbols, pos);
applyPattern(chars, symbols, rebuiltPat, options, &UnicodeSet::closeOver, 0, status);
if (U_FAILURE(status)) return *this;
if (chars.inVariable()) {
// syntaxError(chars, "Extra chars in variable value");
status = U_MALFORMED_SET;
return *this;
}
setPattern(rebuiltPat);
return *this;
}
// USetAdder implementation
// Does not use uset.h to reduce code dependencies
static void U_CALLCONV
_set_add(USet *set, UChar32 c) {
((UnicodeSet *)set)->add(c);
}
static void U_CALLCONV
_set_addRange(USet *set, UChar32 start, UChar32 end) {
((UnicodeSet *)set)->add(start, end);
}
static void U_CALLCONV
_set_addString(USet *set, const char16_t *str, int32_t length) {
((UnicodeSet *)set)->add(UnicodeString((UBool)(length<0), str, length));
}
//----------------------------------------------------------------
// Case folding API
//----------------------------------------------------------------
// add the result of a full case mapping to the set
// use str as a temporary string to avoid constructing one
static inline void
addCaseMapping(UnicodeSet &set, int32_t result, const char16_t *full, UnicodeString &str) {
if(result >= 0) {
if(result > UCASE_MAX_STRING_LENGTH) {
// add a single-code point case mapping
set.add(result);
} else {
// add a string case mapping from full with length result
str.setTo((UBool)false, full, result);
set.add(str);
}
}
// result < 0: the code point mapped to itself, no need to add it
// see ucase.h
}
namespace {
/** For case closure on a large set, look only at code points with relevant properties. */
const UnicodeSet &maybeOnlyCaseSensitive(const UnicodeSet &src, UnicodeSet &subset) {
// The subset must have been constructed with all code points,
// so that the retainAll() intersection effectively copies all single code points from src.
U_ASSERT(subset.contains(0, 0x10ffff));
if (src.size() < 30) {
return src;
}
// Return the intersection of the src code points with Case_Sensitive ones.
UErrorCode errorCode = U_ZERO_ERROR;
const UnicodeSet *sensitive =
CharacterProperties::getBinaryPropertySet(UCHAR_CASE_SENSITIVE, errorCode);
if (U_FAILURE(errorCode)) {
return src;
}
// Start by copying the "smaller" set.
// (We "copy" by intersecting all Unicode *code points* with the first set,
// which omits any strings.)
if (src.getRangeCount() > sensitive->getRangeCount()) {
subset.retainAll(*sensitive);
subset.retainAll(src);
} else {
subset.retainAll(src);
subset.retainAll(*sensitive);
}
return subset;
}
// Per-character scf = Simple_Case_Folding of a string.
// (Normally when we case-fold a string we use full case foldings.)
bool scfString(const UnicodeString &s, UnicodeString &scf) {
// Iterate over the raw buffer for best performance.
const char16_t *p = s.getBuffer();
int32_t length = s.length();
// Loop while not needing modification.
for (int32_t i = 0; i < length;) {
UChar32 c;
U16_NEXT(p, i, length, c); // post-increments i
UChar32 scfChar = u_foldCase(c, U_FOLD_CASE_DEFAULT);
if (scfChar != c) {
// Copy the characters before c.
scf.setTo(p, i - U16_LENGTH(c));
// Loop over the rest of the string and keep case-folding.
for (;;) {
scf.append(scfChar);
if (i == length) {
return true;
}
U16_NEXT(p, i, length, c); // post-increments i
scfChar = u_foldCase(c, U_FOLD_CASE_DEFAULT);
}
}
}
return false;
}
} // namespace
UnicodeSet& UnicodeSet::closeOver(int32_t attribute) {
if (isFrozen() || isBogus()) {
return *this;
}
switch (attribute & USET_CASE_MASK) {
case 0:
break;
case USET_CASE_INSENSITIVE:
closeOverCaseInsensitive(/* simple= */ false);
break;
case USET_ADD_CASE_MAPPINGS:
closeOverAddCaseMappings();
break;
case USET_SIMPLE_CASE_INSENSITIVE:
closeOverCaseInsensitive(/* simple= */ true);
break;
default:
// bad option (unreachable)
break;
}
return *this;
}
void UnicodeSet::closeOverCaseInsensitive(bool simple) {
// Start with input set to guarantee inclusion.
UnicodeSet foldSet(*this);
// Full case mappings closure:
// Remove strings because the strings will actually be reduced (folded);
// therefore, start with no strings and add only those needed.
// Do this before processing code points, because they may add strings.
if (!simple && foldSet.hasStrings()) {
foldSet.strings->removeAllElements();
}
USetAdder sa = {
foldSet.toUSet(),
_set_add,
_set_addRange,
_set_addString,
nullptr, // don't need remove()
nullptr // don't need removeRange()
};
UnicodeSet subset(0, 0x10ffff);
const UnicodeSet &codePoints = maybeOnlyCaseSensitive(*this, subset);
// Iterate over the ranges of single code points. Nested loop for each code point.
int32_t n = codePoints.getRangeCount();
for (int32_t i=0; i<n; ++i) {
UChar32 start = codePoints.getRangeStart(i);
UChar32 end = codePoints.getRangeEnd(i);
if (simple) {
for (UChar32 cp=start; cp<=end; ++cp) {
ucase_addSimpleCaseClosure(cp, &sa);
}
} else {
for (UChar32 cp=start; cp<=end; ++cp) {
ucase_addCaseClosure(cp, &sa);
}
}
}
if (hasStrings()) {
UnicodeString str;
for (int32_t j=0; j<strings->size(); ++j) {
const UnicodeString *pStr = (const UnicodeString *) strings->elementAt(j);
if (simple) {
if (scfString(*pStr, str)) {
foldSet.remove(*pStr).add(str);
}
} else {
str = *pStr;
str.foldCase();
if(!ucase_addStringCaseClosure(str.getBuffer(), str.length(), &sa)) {
foldSet.add(str); // does not map to code points: add the folded string itself
}
}
}
}
*this = foldSet;
}
void UnicodeSet::closeOverAddCaseMappings() {
// Start with input set to guarantee inclusion.
UnicodeSet foldSet(*this);
UnicodeSet subset(0, 0x10ffff);
const UnicodeSet &codePoints = maybeOnlyCaseSensitive(*this, subset);
// Iterate over the ranges of single code points. Nested loop for each code point.
int32_t n = codePoints.getRangeCount();
UChar32 result;
const char16_t *full;
UnicodeString str;
for (int32_t i=0; i<n; ++i) {
UChar32 start = codePoints.getRangeStart(i);
UChar32 end = codePoints.getRangeEnd(i);
// add case mappings
// (does not add long s for regular s, or Kelvin for k, for example)
for (UChar32 cp=start; cp<=end; ++cp) {
result = ucase_toFullLower(cp, nullptr, nullptr, &full, UCASE_LOC_ROOT);
addCaseMapping(foldSet, result, full, str);
result = ucase_toFullTitle(cp, nullptr, nullptr, &full, UCASE_LOC_ROOT);
addCaseMapping(foldSet, result, full, str);
result = ucase_toFullUpper(cp, nullptr, nullptr, &full, UCASE_LOC_ROOT);
addCaseMapping(foldSet, result, full, str);
result = ucase_toFullFolding(cp, &full, 0);
addCaseMapping(foldSet, result, full, str);
}
}
if (hasStrings()) {
Locale root("");
#if !UCONFIG_NO_BREAK_ITERATION
UErrorCode status = U_ZERO_ERROR;
BreakIterator *bi = BreakIterator::createWordInstance(root, status);
if (U_SUCCESS(status)) {
#endif
for (int32_t j=0; j<strings->size(); ++j) {
const UnicodeString *pStr = (const UnicodeString *) strings->elementAt(j);
(str = *pStr).toLower(root);
foldSet.add(str);
#if !UCONFIG_NO_BREAK_ITERATION
(str = *pStr).toTitle(bi, root);
foldSet.add(str);
#endif
(str = *pStr).toUpper(root);
foldSet.add(str);
(str = *pStr).foldCase();
foldSet.add(str);
}
#if !UCONFIG_NO_BREAK_ITERATION
}
delete bi;
#endif
}
*this = foldSet;
}
U_NAMESPACE_END
|