uniset_closure.cpp 12 KB

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