caniter.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. *****************************************************************************
  5. * Copyright (C) 1996-2015, International Business Machines Corporation and
  6. * others. All Rights Reserved.
  7. *****************************************************************************
  8. */
  9. #include "unicode/utypes.h"
  10. #if !UCONFIG_NO_NORMALIZATION
  11. #include "unicode/caniter.h"
  12. #include "unicode/normalizer2.h"
  13. #include "unicode/uchar.h"
  14. #include "unicode/uniset.h"
  15. #include "unicode/usetiter.h"
  16. #include "unicode/ustring.h"
  17. #include "unicode/utf16.h"
  18. #include "cmemory.h"
  19. #include "hash.h"
  20. #include "normalizer2impl.h"
  21. /**
  22. * This class allows one to iterate through all the strings that are canonically equivalent to a given
  23. * string. For example, here are some sample results:
  24. Results for: {LATIN CAPITAL LETTER A WITH RING ABOVE}{LATIN SMALL LETTER D}{COMBINING DOT ABOVE}{COMBINING CEDILLA}
  25. 1: \u0041\u030A\u0064\u0307\u0327
  26. = {LATIN CAPITAL LETTER A}{COMBINING RING ABOVE}{LATIN SMALL LETTER D}{COMBINING DOT ABOVE}{COMBINING CEDILLA}
  27. 2: \u0041\u030A\u0064\u0327\u0307
  28. = {LATIN CAPITAL LETTER A}{COMBINING RING ABOVE}{LATIN SMALL LETTER D}{COMBINING CEDILLA}{COMBINING DOT ABOVE}
  29. 3: \u0041\u030A\u1E0B\u0327
  30. = {LATIN CAPITAL LETTER A}{COMBINING RING ABOVE}{LATIN SMALL LETTER D WITH DOT ABOVE}{COMBINING CEDILLA}
  31. 4: \u0041\u030A\u1E11\u0307
  32. = {LATIN CAPITAL LETTER A}{COMBINING RING ABOVE}{LATIN SMALL LETTER D WITH CEDILLA}{COMBINING DOT ABOVE}
  33. 5: \u00C5\u0064\u0307\u0327
  34. = {LATIN CAPITAL LETTER A WITH RING ABOVE}{LATIN SMALL LETTER D}{COMBINING DOT ABOVE}{COMBINING CEDILLA}
  35. 6: \u00C5\u0064\u0327\u0307
  36. = {LATIN CAPITAL LETTER A WITH RING ABOVE}{LATIN SMALL LETTER D}{COMBINING CEDILLA}{COMBINING DOT ABOVE}
  37. 7: \u00C5\u1E0B\u0327
  38. = {LATIN CAPITAL LETTER A WITH RING ABOVE}{LATIN SMALL LETTER D WITH DOT ABOVE}{COMBINING CEDILLA}
  39. 8: \u00C5\u1E11\u0307
  40. = {LATIN CAPITAL LETTER A WITH RING ABOVE}{LATIN SMALL LETTER D WITH CEDILLA}{COMBINING DOT ABOVE}
  41. 9: \u212B\u0064\u0307\u0327
  42. = {ANGSTROM SIGN}{LATIN SMALL LETTER D}{COMBINING DOT ABOVE}{COMBINING CEDILLA}
  43. 10: \u212B\u0064\u0327\u0307
  44. = {ANGSTROM SIGN}{LATIN SMALL LETTER D}{COMBINING CEDILLA}{COMBINING DOT ABOVE}
  45. 11: \u212B\u1E0B\u0327
  46. = {ANGSTROM SIGN}{LATIN SMALL LETTER D WITH DOT ABOVE}{COMBINING CEDILLA}
  47. 12: \u212B\u1E11\u0307
  48. = {ANGSTROM SIGN}{LATIN SMALL LETTER D WITH CEDILLA}{COMBINING DOT ABOVE}
  49. *<br>Note: the code is intended for use with small strings, and is not suitable for larger ones,
  50. * since it has not been optimized for that situation.
  51. *@author M. Davis
  52. *@draft
  53. */
  54. // public
  55. U_NAMESPACE_BEGIN
  56. // TODO: add boilerplate methods.
  57. UOBJECT_DEFINE_RTTI_IMPLEMENTATION(CanonicalIterator)
  58. /**
  59. *@param source string to get results for
  60. */
  61. CanonicalIterator::CanonicalIterator(const UnicodeString &sourceStr, UErrorCode &status) :
  62. pieces(nullptr),
  63. pieces_length(0),
  64. pieces_lengths(nullptr),
  65. current(nullptr),
  66. current_length(0),
  67. nfd(Normalizer2::getNFDInstance(status)),
  68. nfcImpl(Normalizer2Factory::getNFCImpl(status))
  69. {
  70. if(U_SUCCESS(status) && nfcImpl->ensureCanonIterData(status)) {
  71. setSource(sourceStr, status);
  72. }
  73. }
  74. CanonicalIterator::~CanonicalIterator() {
  75. cleanPieces();
  76. }
  77. void CanonicalIterator::cleanPieces() {
  78. int32_t i = 0;
  79. if(pieces != nullptr) {
  80. for(i = 0; i < pieces_length; i++) {
  81. if(pieces[i] != nullptr) {
  82. delete[] pieces[i];
  83. }
  84. }
  85. uprv_free(pieces);
  86. pieces = nullptr;
  87. pieces_length = 0;
  88. }
  89. if(pieces_lengths != nullptr) {
  90. uprv_free(pieces_lengths);
  91. pieces_lengths = nullptr;
  92. }
  93. if(current != nullptr) {
  94. uprv_free(current);
  95. current = nullptr;
  96. current_length = 0;
  97. }
  98. }
  99. /**
  100. *@return gets the source: NOTE: it is the NFD form of source
  101. */
  102. UnicodeString CanonicalIterator::getSource() {
  103. return source;
  104. }
  105. /**
  106. * Resets the iterator so that one can start again from the beginning.
  107. */
  108. void CanonicalIterator::reset() {
  109. done = false;
  110. for (int i = 0; i < current_length; ++i) {
  111. current[i] = 0;
  112. }
  113. }
  114. /**
  115. *@return the next string that is canonically equivalent. The value null is returned when
  116. * the iteration is done.
  117. */
  118. UnicodeString CanonicalIterator::next() {
  119. int32_t i = 0;
  120. if (done) {
  121. buffer.setToBogus();
  122. return buffer;
  123. }
  124. // delete old contents
  125. buffer.remove();
  126. // construct return value
  127. for (i = 0; i < pieces_length; ++i) {
  128. buffer.append(pieces[i][current[i]]);
  129. }
  130. //String result = buffer.toString(); // not needed
  131. // find next value for next time
  132. for (i = current_length - 1; ; --i) {
  133. if (i < 0) {
  134. done = true;
  135. break;
  136. }
  137. current[i]++;
  138. if (current[i] < pieces_lengths[i]) break; // got sequence
  139. current[i] = 0;
  140. }
  141. return buffer;
  142. }
  143. /**
  144. *@param set the source string to iterate against. This allows the same iterator to be used
  145. * while changing the source string, saving object creation.
  146. */
  147. void CanonicalIterator::setSource(const UnicodeString &newSource, UErrorCode &status) {
  148. int32_t list_length = 0;
  149. UChar32 cp = 0;
  150. int32_t start = 0;
  151. int32_t i = 0;
  152. UnicodeString *list = nullptr;
  153. nfd->normalize(newSource, source, status);
  154. if(U_FAILURE(status)) {
  155. return;
  156. }
  157. done = false;
  158. cleanPieces();
  159. // catch degenerate case
  160. if (newSource.length() == 0) {
  161. pieces = static_cast<UnicodeString**>(uprv_malloc(sizeof(UnicodeString*)));
  162. pieces_lengths = static_cast<int32_t*>(uprv_malloc(1 * sizeof(int32_t)));
  163. pieces_length = 1;
  164. current = static_cast<int32_t*>(uprv_malloc(1 * sizeof(int32_t)));
  165. current_length = 1;
  166. if (pieces == nullptr || pieces_lengths == nullptr || current == nullptr) {
  167. status = U_MEMORY_ALLOCATION_ERROR;
  168. goto CleanPartialInitialization;
  169. }
  170. current[0] = 0;
  171. pieces[0] = new UnicodeString[1];
  172. pieces_lengths[0] = 1;
  173. if (pieces[0] == nullptr) {
  174. status = U_MEMORY_ALLOCATION_ERROR;
  175. goto CleanPartialInitialization;
  176. }
  177. return;
  178. }
  179. list = new UnicodeString[source.length()];
  180. if (list == nullptr) {
  181. status = U_MEMORY_ALLOCATION_ERROR;
  182. goto CleanPartialInitialization;
  183. }
  184. // i should initially be the number of code units at the
  185. // start of the string
  186. i = U16_LENGTH(source.char32At(0));
  187. // int32_t i = 1;
  188. // find the segments
  189. // This code iterates through the source string and
  190. // extracts segments that end up on a codepoint that
  191. // doesn't start any decompositions. (Analysis is done
  192. // on the NFD form - see above).
  193. for (; i < source.length(); i += U16_LENGTH(cp)) {
  194. cp = source.char32At(i);
  195. if (nfcImpl->isCanonSegmentStarter(cp)) {
  196. source.extract(start, i-start, list[list_length++]); // add up to i
  197. start = i;
  198. }
  199. }
  200. source.extract(start, i-start, list[list_length++]); // add last one
  201. // allocate the arrays, and find the strings that are CE to each segment
  202. pieces = static_cast<UnicodeString**>(uprv_malloc(list_length * sizeof(UnicodeString*)));
  203. pieces_length = list_length;
  204. pieces_lengths = static_cast<int32_t*>(uprv_malloc(list_length * sizeof(int32_t)));
  205. current = static_cast<int32_t*>(uprv_malloc(list_length * sizeof(int32_t)));
  206. current_length = list_length;
  207. if (pieces == nullptr || pieces_lengths == nullptr || current == nullptr) {
  208. status = U_MEMORY_ALLOCATION_ERROR;
  209. goto CleanPartialInitialization;
  210. }
  211. for (i = 0; i < current_length; i++) {
  212. current[i] = 0;
  213. }
  214. // for each segment, get all the combinations that can produce
  215. // it after NFD normalization
  216. for (i = 0; i < pieces_length; ++i) {
  217. //if (PROGRESS) printf("SEGMENT\n");
  218. pieces[i] = getEquivalents(list[i], pieces_lengths[i], status);
  219. }
  220. delete[] list;
  221. return;
  222. // Common section to cleanup all local variables and reset object variables.
  223. CleanPartialInitialization:
  224. delete[] list;
  225. cleanPieces();
  226. }
  227. /**
  228. * Dumb recursive implementation of permutation.
  229. * TODO: optimize
  230. * @param source the string to find permutations for
  231. * @return the results in a set.
  232. */
  233. void U_EXPORT2 CanonicalIterator::permute(UnicodeString &source, UBool skipZeros, Hashtable *result, UErrorCode &status, int32_t depth) {
  234. if(U_FAILURE(status)) {
  235. return;
  236. }
  237. // To avoid infinity loop caused by permute, we limit the depth of recursive
  238. // call to permute and return U_UNSUPPORTED_ERROR.
  239. // We know in some unit test we need at least 4. Set to 8 just in case some
  240. // unforseen use cases.
  241. constexpr int32_t kPermuteDepthLimit = 8;
  242. if (depth > kPermuteDepthLimit) {
  243. status = U_UNSUPPORTED_ERROR;
  244. return;
  245. }
  246. //if (PROGRESS) printf("Permute: %s\n", UToS(Tr(source)));
  247. int32_t i = 0;
  248. // optimization:
  249. // if zero or one character, just return a set with it
  250. // we check for length < 2 to keep from counting code points all the time
  251. if (source.length() <= 2 && source.countChar32() <= 1) {
  252. UnicodeString *toPut = new UnicodeString(source);
  253. /* test for nullptr */
  254. if (toPut == nullptr) {
  255. status = U_MEMORY_ALLOCATION_ERROR;
  256. return;
  257. }
  258. result->put(source, toPut, status);
  259. return;
  260. }
  261. // otherwise iterate through the string, and recursively permute all the other characters
  262. UChar32 cp;
  263. Hashtable subpermute(status);
  264. if(U_FAILURE(status)) {
  265. return;
  266. }
  267. subpermute.setValueDeleter(uprv_deleteUObject);
  268. for (i = 0; i < source.length(); i += U16_LENGTH(cp)) {
  269. cp = source.char32At(i);
  270. const UHashElement *ne = nullptr;
  271. int32_t el = UHASH_FIRST;
  272. UnicodeString subPermuteString = source;
  273. // optimization:
  274. // if the character is canonical combining class zero,
  275. // don't permute it
  276. if (skipZeros && i != 0 && u_getCombiningClass(cp) == 0) {
  277. //System.out.println("Skipping " + Utility.hex(UTF16.valueOf(source, i)));
  278. continue;
  279. }
  280. subpermute.removeAll();
  281. // see what the permutations of the characters before and after this one are
  282. //Hashtable *subpermute = permute(source.substring(0,i) + source.substring(i + UTF16.getCharCount(cp)));
  283. permute(subPermuteString.remove(i, U16_LENGTH(cp)), skipZeros, &subpermute, status, depth+1);
  284. /* Test for buffer overflows */
  285. if(U_FAILURE(status)) {
  286. return;
  287. }
  288. // The upper remove is destructive. The question is do we have to make a copy, or we don't care about the contents
  289. // of source at this point.
  290. // prefix this character to all of them
  291. ne = subpermute.nextElement(el);
  292. while (ne != nullptr) {
  293. UnicodeString* permRes = static_cast<UnicodeString*>(ne->value.pointer);
  294. UnicodeString *chStr = new UnicodeString(cp);
  295. //test for nullptr
  296. if (chStr == nullptr) {
  297. status = U_MEMORY_ALLOCATION_ERROR;
  298. return;
  299. }
  300. chStr->append(*permRes); //*((UnicodeString *)(ne->value.pointer));
  301. //if (PROGRESS) printf(" Piece: %s\n", UToS(*chStr));
  302. result->put(*chStr, chStr, status);
  303. ne = subpermute.nextElement(el);
  304. }
  305. }
  306. //return result;
  307. }
  308. // privates
  309. // we have a segment, in NFD. Find all the strings that are canonically equivalent to it.
  310. UnicodeString* CanonicalIterator::getEquivalents(const UnicodeString &segment, int32_t &result_len, UErrorCode &status) {
  311. Hashtable result(status);
  312. Hashtable permutations(status);
  313. Hashtable basic(status);
  314. if (U_FAILURE(status)) {
  315. return nullptr;
  316. }
  317. result.setValueDeleter(uprv_deleteUObject);
  318. permutations.setValueDeleter(uprv_deleteUObject);
  319. basic.setValueDeleter(uprv_deleteUObject);
  320. char16_t USeg[256];
  321. int32_t segLen = segment.extract(USeg, 256, status);
  322. getEquivalents2(&basic, USeg, segLen, status);
  323. if (U_FAILURE(status)) {
  324. return nullptr;
  325. }
  326. // now get all the permutations
  327. // add only the ones that are canonically equivalent
  328. // TODO: optimize by not permuting any class zero.
  329. const UHashElement *ne = nullptr;
  330. int32_t el = UHASH_FIRST;
  331. //Iterator it = basic.iterator();
  332. ne = basic.nextElement(el);
  333. //while (it.hasNext())
  334. while (ne != nullptr) {
  335. //String item = (String) it.next();
  336. UnicodeString item = *static_cast<UnicodeString*>(ne->value.pointer);
  337. permutations.removeAll();
  338. permute(item, CANITER_SKIP_ZEROES, &permutations, status);
  339. const UHashElement *ne2 = nullptr;
  340. int32_t el2 = UHASH_FIRST;
  341. //Iterator it2 = permutations.iterator();
  342. ne2 = permutations.nextElement(el2);
  343. //while (it2.hasNext())
  344. while (ne2 != nullptr) {
  345. //String possible = (String) it2.next();
  346. //UnicodeString *possible = new UnicodeString(*((UnicodeString *)(ne2->value.pointer)));
  347. UnicodeString possible(*static_cast<UnicodeString*>(ne2->value.pointer));
  348. UnicodeString attempt;
  349. nfd->normalize(possible, attempt, status);
  350. // TODO: check if operator == is semanticaly the same as attempt.equals(segment)
  351. if (attempt==segment) {
  352. //if (PROGRESS) printf("Adding Permutation: %s\n", UToS(Tr(*possible)));
  353. // TODO: use the hashtable just to catch duplicates - store strings directly (somehow).
  354. result.put(possible, new UnicodeString(possible), status); //add(possible);
  355. } else {
  356. //if (PROGRESS) printf("-Skipping Permutation: %s\n", UToS(Tr(*possible)));
  357. }
  358. ne2 = permutations.nextElement(el2);
  359. }
  360. ne = basic.nextElement(el);
  361. }
  362. /* Test for buffer overflows */
  363. if(U_FAILURE(status)) {
  364. return nullptr;
  365. }
  366. // convert into a String[] to clean up storage
  367. //String[] finalResult = new String[result.size()];
  368. UnicodeString *finalResult = nullptr;
  369. int32_t resultCount;
  370. if((resultCount = result.count()) != 0) {
  371. finalResult = new UnicodeString[resultCount];
  372. if (finalResult == nullptr) {
  373. status = U_MEMORY_ALLOCATION_ERROR;
  374. return nullptr;
  375. }
  376. }
  377. else {
  378. status = U_ILLEGAL_ARGUMENT_ERROR;
  379. return nullptr;
  380. }
  381. //result.toArray(finalResult);
  382. result_len = 0;
  383. el = UHASH_FIRST;
  384. ne = result.nextElement(el);
  385. while(ne != nullptr) {
  386. finalResult[result_len++] = *static_cast<UnicodeString*>(ne->value.pointer);
  387. ne = result.nextElement(el);
  388. }
  389. return finalResult;
  390. }
  391. Hashtable *CanonicalIterator::getEquivalents2(Hashtable *fillinResult, const char16_t *segment, int32_t segLen, UErrorCode &status) {
  392. if (U_FAILURE(status)) {
  393. return nullptr;
  394. }
  395. //if (PROGRESS) printf("Adding: %s\n", UToS(Tr(segment)));
  396. UnicodeString toPut(segment, segLen);
  397. fillinResult->put(toPut, new UnicodeString(toPut), status);
  398. UnicodeSet starts;
  399. // cycle through all the characters
  400. UChar32 cp;
  401. for (int32_t i = 0; i < segLen; i += U16_LENGTH(cp)) {
  402. // see if any character is at the start of some decomposition
  403. U16_GET(segment, 0, i, segLen, cp);
  404. if (!nfcImpl->getCanonStartSet(cp, starts)) {
  405. continue;
  406. }
  407. // if so, see which decompositions match
  408. UnicodeSetIterator iter(starts);
  409. while (iter.next()) {
  410. UChar32 cp2 = iter.getCodepoint();
  411. Hashtable remainder(status);
  412. remainder.setValueDeleter(uprv_deleteUObject);
  413. if (extract(&remainder, cp2, segment, segLen, i, status) == nullptr) {
  414. if (U_FAILURE(status)) {
  415. return nullptr;
  416. }
  417. continue;
  418. }
  419. // there were some matches, so add all the possibilities to the set.
  420. UnicodeString prefix(segment, i);
  421. prefix += cp2;
  422. int32_t el = UHASH_FIRST;
  423. const UHashElement *ne = remainder.nextElement(el);
  424. while (ne != nullptr) {
  425. UnicodeString item = *static_cast<UnicodeString*>(ne->value.pointer);
  426. UnicodeString *toAdd = new UnicodeString(prefix);
  427. /* test for nullptr */
  428. if (toAdd == nullptr) {
  429. status = U_MEMORY_ALLOCATION_ERROR;
  430. return nullptr;
  431. }
  432. *toAdd += item;
  433. fillinResult->put(*toAdd, toAdd, status);
  434. //if (PROGRESS) printf("Adding: %s\n", UToS(Tr(*toAdd)));
  435. ne = remainder.nextElement(el);
  436. }
  437. // ICU-22642 Guards against strings that have so many permutations
  438. // that they would otherwise hang the function.
  439. constexpr int32_t kResultLimit = 4096;
  440. if (fillinResult->count() > kResultLimit) {
  441. status = U_UNSUPPORTED_ERROR;
  442. return nullptr;
  443. }
  444. }
  445. }
  446. /* Test for buffer overflows */
  447. if(U_FAILURE(status)) {
  448. return nullptr;
  449. }
  450. return fillinResult;
  451. }
  452. /**
  453. * See if the decomposition of cp2 is at segment starting at segmentPos
  454. * (with canonical rearrangement!)
  455. * If so, take the remainder, and return the equivalents
  456. */
  457. Hashtable *CanonicalIterator::extract(Hashtable *fillinResult, UChar32 comp, const char16_t *segment, int32_t segLen, int32_t segmentPos, UErrorCode &status) {
  458. //Hashtable *CanonicalIterator::extract(UChar32 comp, const UnicodeString &segment, int32_t segLen, int32_t segmentPos, UErrorCode &status) {
  459. //if (PROGRESS) printf(" extract: %s, ", UToS(Tr(UnicodeString(comp))));
  460. //if (PROGRESS) printf("%s, %i\n", UToS(Tr(segment)), segmentPos);
  461. if (U_FAILURE(status)) {
  462. return nullptr;
  463. }
  464. UnicodeString temp(comp);
  465. int32_t inputLen=temp.length();
  466. UnicodeString decompString;
  467. nfd->normalize(temp, decompString, status);
  468. if (U_FAILURE(status)) {
  469. return nullptr;
  470. }
  471. if (decompString.isBogus()) {
  472. status = U_MEMORY_ALLOCATION_ERROR;
  473. return nullptr;
  474. }
  475. const char16_t *decomp=decompString.getBuffer();
  476. int32_t decompLen=decompString.length();
  477. // See if it matches the start of segment (at segmentPos)
  478. UBool ok = false;
  479. UChar32 cp;
  480. int32_t decompPos = 0;
  481. UChar32 decompCp;
  482. U16_NEXT(decomp, decompPos, decompLen, decompCp);
  483. int32_t i = segmentPos;
  484. while(i < segLen) {
  485. U16_NEXT(segment, i, segLen, cp);
  486. if (cp == decompCp) { // if equal, eat another cp from decomp
  487. //if (PROGRESS) printf(" matches: %s\n", UToS(Tr(UnicodeString(cp))));
  488. if (decompPos == decompLen) { // done, have all decomp characters!
  489. temp.append(segment+i, segLen-i);
  490. ok = true;
  491. break;
  492. }
  493. U16_NEXT(decomp, decompPos, decompLen, decompCp);
  494. } else {
  495. //if (PROGRESS) printf(" buffer: %s\n", UToS(Tr(UnicodeString(cp))));
  496. // brute force approach
  497. temp.append(cp);
  498. /* TODO: optimize
  499. // since we know that the classes are monotonically increasing, after zero
  500. // e.g. 0 5 7 9 0 3
  501. // we can do an optimization
  502. // there are only a few cases that work: zero, less, same, greater
  503. // if both classes are the same, we fail
  504. // if the decomp class < the segment class, we fail
  505. segClass = getClass(cp);
  506. if (decompClass <= segClass) return null;
  507. */
  508. }
  509. }
  510. if (!ok)
  511. return nullptr; // we failed, characters left over
  512. //if (PROGRESS) printf("Matches\n");
  513. if (inputLen == temp.length()) {
  514. fillinResult->put(UnicodeString(), new UnicodeString(), status);
  515. return fillinResult; // succeed, but no remainder
  516. }
  517. // brute force approach
  518. // check to make sure result is canonically equivalent
  519. UnicodeString trial;
  520. nfd->normalize(temp, trial, status);
  521. if(U_FAILURE(status) || trial.compare(segment+segmentPos, segLen - segmentPos) != 0) {
  522. return nullptr;
  523. }
  524. return getEquivalents2(fillinResult, temp.getBuffer()+inputLen, temp.length()-inputLen, status);
  525. }
  526. U_NAMESPACE_END
  527. #endif /* #if !UCONFIG_NO_NORMALIZATION */