utrie.cpp 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234
  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) 2001-2012, International Business Machines
  7. * Corporation and others. All Rights Reserved.
  8. *
  9. ******************************************************************************
  10. * file name: utrie.cpp
  11. * encoding: UTF-8
  12. * tab size: 8 (not used)
  13. * indentation:4
  14. *
  15. * created on: 2001oct20
  16. * created by: Markus W. Scherer
  17. *
  18. * This is a common implementation of a "folded" trie.
  19. * It is a kind of compressed, serializable table of 16- or 32-bit values associated with
  20. * Unicode code points (0..0x10ffff).
  21. */
  22. #ifdef UTRIE_DEBUG
  23. # include <stdio.h>
  24. #endif
  25. #include "unicode/utypes.h"
  26. #include "cmemory.h"
  27. #include "utrie.h"
  28. /* miscellaneous ------------------------------------------------------------ */
  29. #undef ABS
  30. #define ABS(x) ((x)>=0 ? (x) : -(x))
  31. static inline UBool
  32. equal_uint32(const uint32_t *s, const uint32_t *t, int32_t length) {
  33. while(length>0 && *s==*t) {
  34. ++s;
  35. ++t;
  36. --length;
  37. }
  38. return (UBool)(length==0);
  39. }
  40. /* Building a trie ----------------------------------------------------------*/
  41. U_CAPI UNewTrie * U_EXPORT2
  42. utrie_open(UNewTrie *fillIn,
  43. uint32_t *aliasData, int32_t maxDataLength,
  44. uint32_t initialValue, uint32_t leadUnitValue,
  45. UBool latin1Linear) {
  46. UNewTrie *trie;
  47. int32_t i, j;
  48. if( maxDataLength<UTRIE_DATA_BLOCK_LENGTH ||
  49. (latin1Linear && maxDataLength<1024)
  50. ) {
  51. return nullptr;
  52. }
  53. if(fillIn!=nullptr) {
  54. trie=fillIn;
  55. } else {
  56. trie=(UNewTrie *)uprv_malloc(sizeof(UNewTrie));
  57. if(trie==nullptr) {
  58. return nullptr;
  59. }
  60. }
  61. uprv_memset(trie, 0, sizeof(UNewTrie));
  62. trie->isAllocated= (UBool)(fillIn==nullptr);
  63. if(aliasData!=nullptr) {
  64. trie->data=aliasData;
  65. trie->isDataAllocated=false;
  66. } else {
  67. trie->data=(uint32_t *)uprv_malloc(maxDataLength*4);
  68. if(trie->data==nullptr) {
  69. uprv_free(trie);
  70. return nullptr;
  71. }
  72. trie->isDataAllocated=true;
  73. }
  74. /* preallocate and reset the first data block (block index 0) */
  75. j=UTRIE_DATA_BLOCK_LENGTH;
  76. if(latin1Linear) {
  77. /* preallocate and reset the first block (number 0) and Latin-1 (U+0000..U+00ff) after that */
  78. /* made sure above that maxDataLength>=1024 */
  79. /* set indexes to point to consecutive data blocks */
  80. i=0;
  81. do {
  82. /* do this at least for trie->index[0] even if that block is only partly used for Latin-1 */
  83. trie->index[i++]=j;
  84. j+=UTRIE_DATA_BLOCK_LENGTH;
  85. } while(i<(256>>UTRIE_SHIFT));
  86. }
  87. /* reset the initially allocated blocks to the initial value */
  88. trie->dataLength=j;
  89. while(j>0) {
  90. trie->data[--j]=initialValue;
  91. }
  92. trie->leadUnitValue=leadUnitValue;
  93. trie->indexLength=UTRIE_MAX_INDEX_LENGTH;
  94. trie->dataCapacity=maxDataLength;
  95. trie->isLatin1Linear=latin1Linear;
  96. trie->isCompacted=false;
  97. return trie;
  98. }
  99. U_CAPI UNewTrie * U_EXPORT2
  100. utrie_clone(UNewTrie *fillIn, const UNewTrie *other, uint32_t *aliasData, int32_t aliasDataCapacity) {
  101. UNewTrie *trie;
  102. UBool isDataAllocated;
  103. /* do not clone if other is not valid or already compacted */
  104. if(other==nullptr || other->data==nullptr || other->isCompacted) {
  105. return nullptr;
  106. }
  107. /* clone data */
  108. if(aliasData!=nullptr && aliasDataCapacity>=other->dataCapacity) {
  109. isDataAllocated=false;
  110. } else {
  111. aliasDataCapacity=other->dataCapacity;
  112. aliasData=(uint32_t *)uprv_malloc(other->dataCapacity*4);
  113. if(aliasData==nullptr) {
  114. return nullptr;
  115. }
  116. isDataAllocated=true;
  117. }
  118. trie=utrie_open(fillIn, aliasData, aliasDataCapacity,
  119. other->data[0], other->leadUnitValue,
  120. other->isLatin1Linear);
  121. if(trie==nullptr) {
  122. uprv_free(aliasData);
  123. } else {
  124. uprv_memcpy(trie->index, other->index, sizeof(trie->index));
  125. uprv_memcpy(trie->data, other->data, (size_t)other->dataLength*4);
  126. trie->dataLength=other->dataLength;
  127. trie->isDataAllocated=isDataAllocated;
  128. }
  129. return trie;
  130. }
  131. U_CAPI void U_EXPORT2
  132. utrie_close(UNewTrie *trie) {
  133. if(trie!=nullptr) {
  134. if(trie->isDataAllocated) {
  135. uprv_free(trie->data);
  136. trie->data=nullptr;
  137. }
  138. if(trie->isAllocated) {
  139. uprv_free(trie);
  140. }
  141. }
  142. }
  143. U_CAPI uint32_t * U_EXPORT2
  144. utrie_getData(UNewTrie *trie, int32_t *pLength) {
  145. if(trie==nullptr || pLength==nullptr) {
  146. return nullptr;
  147. }
  148. *pLength=trie->dataLength;
  149. return trie->data;
  150. }
  151. static int32_t
  152. utrie_allocDataBlock(UNewTrie *trie) {
  153. int32_t newBlock, newTop;
  154. newBlock=trie->dataLength;
  155. newTop=newBlock+UTRIE_DATA_BLOCK_LENGTH;
  156. if(newTop>trie->dataCapacity) {
  157. /* out of memory in the data array */
  158. return -1;
  159. }
  160. trie->dataLength=newTop;
  161. return newBlock;
  162. }
  163. /**
  164. * No error checking for illegal arguments.
  165. *
  166. * @return -1 if no new data block available (out of memory in data array)
  167. * @internal
  168. */
  169. static int32_t
  170. utrie_getDataBlock(UNewTrie *trie, UChar32 c) {
  171. int32_t indexValue, newBlock;
  172. c>>=UTRIE_SHIFT;
  173. indexValue=trie->index[c];
  174. if(indexValue>0) {
  175. return indexValue;
  176. }
  177. /* allocate a new data block */
  178. newBlock=utrie_allocDataBlock(trie);
  179. if(newBlock<0) {
  180. /* out of memory in the data array */
  181. return -1;
  182. }
  183. trie->index[c]=newBlock;
  184. /* copy-on-write for a block from a setRange() */
  185. uprv_memcpy(trie->data+newBlock, trie->data-indexValue, 4*UTRIE_DATA_BLOCK_LENGTH);
  186. return newBlock;
  187. }
  188. /**
  189. * @return true if the value was successfully set
  190. */
  191. U_CAPI UBool U_EXPORT2
  192. utrie_set32(UNewTrie *trie, UChar32 c, uint32_t value) {
  193. int32_t block;
  194. /* valid, uncompacted trie and valid c? */
  195. if(trie==nullptr || trie->isCompacted || (uint32_t)c>0x10ffff) {
  196. return false;
  197. }
  198. block=utrie_getDataBlock(trie, c);
  199. if(block<0) {
  200. return false;
  201. }
  202. trie->data[block+(c&UTRIE_MASK)]=value;
  203. return true;
  204. }
  205. U_CAPI uint32_t U_EXPORT2
  206. utrie_get32(UNewTrie *trie, UChar32 c, UBool *pInBlockZero) {
  207. int32_t block;
  208. /* valid, uncompacted trie and valid c? */
  209. if(trie==nullptr || trie->isCompacted || (uint32_t)c>0x10ffff) {
  210. if(pInBlockZero!=nullptr) {
  211. *pInBlockZero=true;
  212. }
  213. return 0;
  214. }
  215. block=trie->index[c>>UTRIE_SHIFT];
  216. if(pInBlockZero!=nullptr) {
  217. *pInBlockZero= (UBool)(block==0);
  218. }
  219. return trie->data[ABS(block)+(c&UTRIE_MASK)];
  220. }
  221. /**
  222. * @internal
  223. */
  224. static void
  225. utrie_fillBlock(uint32_t *block, UChar32 start, UChar32 limit,
  226. uint32_t value, uint32_t initialValue, UBool overwrite) {
  227. uint32_t *pLimit;
  228. pLimit=block+limit;
  229. block+=start;
  230. if(overwrite) {
  231. while(block<pLimit) {
  232. *block++=value;
  233. }
  234. } else {
  235. while(block<pLimit) {
  236. if(*block==initialValue) {
  237. *block=value;
  238. }
  239. ++block;
  240. }
  241. }
  242. }
  243. U_CAPI UBool U_EXPORT2
  244. utrie_setRange32(UNewTrie *trie, UChar32 start, UChar32 limit, uint32_t value, UBool overwrite) {
  245. /*
  246. * repeat value in [start..limit[
  247. * mark index values for repeat-data blocks by setting bit 31 of the index values
  248. * fill around existing values if any, if(overwrite)
  249. */
  250. uint32_t initialValue;
  251. int32_t block, rest, repeatBlock;
  252. /* valid, uncompacted trie and valid indexes? */
  253. if( trie==nullptr || trie->isCompacted ||
  254. (uint32_t)start>0x10ffff || (uint32_t)limit>0x110000 || start>limit
  255. ) {
  256. return false;
  257. }
  258. if(start==limit) {
  259. return true; /* nothing to do */
  260. }
  261. initialValue=trie->data[0];
  262. if(start&UTRIE_MASK) {
  263. UChar32 nextStart;
  264. /* set partial block at [start..following block boundary[ */
  265. block=utrie_getDataBlock(trie, start);
  266. if(block<0) {
  267. return false;
  268. }
  269. nextStart=(start+UTRIE_DATA_BLOCK_LENGTH)&~UTRIE_MASK;
  270. if(nextStart<=limit) {
  271. utrie_fillBlock(trie->data+block, start&UTRIE_MASK, UTRIE_DATA_BLOCK_LENGTH,
  272. value, initialValue, overwrite);
  273. start=nextStart;
  274. } else {
  275. utrie_fillBlock(trie->data+block, start&UTRIE_MASK, limit&UTRIE_MASK,
  276. value, initialValue, overwrite);
  277. return true;
  278. }
  279. }
  280. /* number of positions in the last, partial block */
  281. rest=limit&UTRIE_MASK;
  282. /* round down limit to a block boundary */
  283. limit&=~UTRIE_MASK;
  284. /* iterate over all-value blocks */
  285. if(value==initialValue) {
  286. repeatBlock=0;
  287. } else {
  288. repeatBlock=-1;
  289. }
  290. while(start<limit) {
  291. /* get index value */
  292. block=trie->index[start>>UTRIE_SHIFT];
  293. if(block>0) {
  294. /* already allocated, fill in value */
  295. utrie_fillBlock(trie->data+block, 0, UTRIE_DATA_BLOCK_LENGTH, value, initialValue, overwrite);
  296. } else if(trie->data[-block]!=value && (block==0 || overwrite)) {
  297. /* set the repeatBlock instead of the current block 0 or range block */
  298. if(repeatBlock>=0) {
  299. trie->index[start>>UTRIE_SHIFT]=-repeatBlock;
  300. } else {
  301. /* create and set and fill the repeatBlock */
  302. repeatBlock=utrie_getDataBlock(trie, start);
  303. if(repeatBlock<0) {
  304. return false;
  305. }
  306. /* set the negative block number to indicate that it is a repeat block */
  307. trie->index[start>>UTRIE_SHIFT]=-repeatBlock;
  308. utrie_fillBlock(trie->data+repeatBlock, 0, UTRIE_DATA_BLOCK_LENGTH, value, initialValue, true);
  309. }
  310. }
  311. start+=UTRIE_DATA_BLOCK_LENGTH;
  312. }
  313. if(rest>0) {
  314. /* set partial block at [last block boundary..limit[ */
  315. block=utrie_getDataBlock(trie, start);
  316. if(block<0) {
  317. return false;
  318. }
  319. utrie_fillBlock(trie->data+block, 0, rest, value, initialValue, overwrite);
  320. }
  321. return true;
  322. }
  323. static int32_t
  324. _findSameIndexBlock(const int32_t *idx, int32_t indexLength,
  325. int32_t otherBlock) {
  326. int32_t block, i;
  327. for(block=UTRIE_BMP_INDEX_LENGTH; block<indexLength; block+=UTRIE_SURROGATE_BLOCK_COUNT) {
  328. for(i=0; i<UTRIE_SURROGATE_BLOCK_COUNT; ++i) {
  329. if(idx[block+i]!=idx[otherBlock+i]) {
  330. break;
  331. }
  332. }
  333. if(i==UTRIE_SURROGATE_BLOCK_COUNT) {
  334. return block;
  335. }
  336. }
  337. return indexLength;
  338. }
  339. /*
  340. * Fold the normalization data for supplementary code points into
  341. * a compact area on top of the BMP-part of the trie index,
  342. * with the lead surrogates indexing this compact area.
  343. *
  344. * Duplicate the index values for lead surrogates:
  345. * From inside the BMP area, where some may be overridden with folded values,
  346. * to just after the BMP area, where they can be retrieved for
  347. * code point lookups.
  348. */
  349. static void
  350. utrie_fold(UNewTrie *trie, UNewTrieGetFoldedValue *getFoldedValue, UErrorCode *pErrorCode) {
  351. int32_t leadIndexes[UTRIE_SURROGATE_BLOCK_COUNT];
  352. int32_t *idx;
  353. uint32_t value;
  354. UChar32 c;
  355. int32_t indexLength, block;
  356. #ifdef UTRIE_DEBUG
  357. int countLeadCUWithData=0;
  358. #endif
  359. idx=trie->index;
  360. /* copy the lead surrogate indexes into a temporary array */
  361. uprv_memcpy(leadIndexes, idx+(0xd800>>UTRIE_SHIFT), 4*UTRIE_SURROGATE_BLOCK_COUNT);
  362. /*
  363. * set all values for lead surrogate code *units* to leadUnitValue
  364. * so that, by default, runtime lookups will find no data for associated
  365. * supplementary code points, unless there is data for such code points
  366. * which will result in a non-zero folding value below that is set for
  367. * the respective lead units
  368. *
  369. * the above saved the indexes for surrogate code *points*
  370. * fill the indexes with simplified code from utrie_setRange32()
  371. */
  372. if(trie->leadUnitValue==trie->data[0]) {
  373. block=0; /* leadUnitValue==initialValue, use all-initial-value block */
  374. } else {
  375. /* create and fill the repeatBlock */
  376. block=utrie_allocDataBlock(trie);
  377. if(block<0) {
  378. /* data table overflow */
  379. *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
  380. return;
  381. }
  382. utrie_fillBlock(trie->data+block, 0, UTRIE_DATA_BLOCK_LENGTH, trie->leadUnitValue, trie->data[0], true);
  383. block=-block; /* negative block number to indicate that it is a repeat block */
  384. }
  385. for(c=(0xd800>>UTRIE_SHIFT); c<(0xdc00>>UTRIE_SHIFT); ++c) {
  386. trie->index[c]=block;
  387. }
  388. /*
  389. * Fold significant index values into the area just after the BMP indexes.
  390. * In case the first lead surrogate has significant data,
  391. * its index block must be used first (in which case the folding is a no-op).
  392. * Later all folded index blocks are moved up one to insert the copied
  393. * lead surrogate indexes.
  394. */
  395. indexLength=UTRIE_BMP_INDEX_LENGTH;
  396. /* search for any index (stage 1) entries for supplementary code points */
  397. for(c=0x10000; c<0x110000;) {
  398. if(idx[c>>UTRIE_SHIFT]!=0) {
  399. /* there is data, treat the full block for a lead surrogate */
  400. c&=~0x3ff;
  401. #ifdef UTRIE_DEBUG
  402. ++countLeadCUWithData;
  403. /* printf("supplementary data for lead surrogate U+%04lx\n", (long)(0xd7c0+(c>>10))); */
  404. #endif
  405. /* is there an identical index block? */
  406. block=_findSameIndexBlock(idx, indexLength, c>>UTRIE_SHIFT);
  407. /*
  408. * get a folded value for [c..c+0x400[ and,
  409. * if different from the value for the lead surrogate code point,
  410. * set it for the lead surrogate code unit
  411. */
  412. value=getFoldedValue(trie, c, block+UTRIE_SURROGATE_BLOCK_COUNT);
  413. if(value!=utrie_get32(trie, U16_LEAD(c), nullptr)) {
  414. if(!utrie_set32(trie, U16_LEAD(c), value)) {
  415. /* data table overflow */
  416. *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
  417. return;
  418. }
  419. /* if we did not find an identical index block... */
  420. if(block==indexLength) {
  421. /* move the actual index (stage 1) entries from the supplementary position to the new one */
  422. uprv_memmove(idx+indexLength,
  423. idx+(c>>UTRIE_SHIFT),
  424. 4*UTRIE_SURROGATE_BLOCK_COUNT);
  425. indexLength+=UTRIE_SURROGATE_BLOCK_COUNT;
  426. }
  427. }
  428. c+=0x400;
  429. } else {
  430. c+=UTRIE_DATA_BLOCK_LENGTH;
  431. }
  432. }
  433. #ifdef UTRIE_DEBUG
  434. if(countLeadCUWithData>0) {
  435. printf("supplementary data for %d lead surrogates\n", countLeadCUWithData);
  436. }
  437. #endif
  438. /*
  439. * index array overflow?
  440. * This is to guarantee that a folding offset is of the form
  441. * UTRIE_BMP_INDEX_LENGTH+n*UTRIE_SURROGATE_BLOCK_COUNT with n=0..1023.
  442. * If the index is too large, then n>=1024 and more than 10 bits are necessary.
  443. *
  444. * In fact, it can only ever become n==1024 with completely unfoldable data and
  445. * the additional block of duplicated values for lead surrogates.
  446. */
  447. if(indexLength>=UTRIE_MAX_INDEX_LENGTH) {
  448. *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
  449. return;
  450. }
  451. /*
  452. * make space for the lead surrogate index block and
  453. * insert it between the BMP indexes and the folded ones
  454. */
  455. uprv_memmove(idx+UTRIE_BMP_INDEX_LENGTH+UTRIE_SURROGATE_BLOCK_COUNT,
  456. idx+UTRIE_BMP_INDEX_LENGTH,
  457. 4*(indexLength-UTRIE_BMP_INDEX_LENGTH));
  458. uprv_memcpy(idx+UTRIE_BMP_INDEX_LENGTH,
  459. leadIndexes,
  460. 4*UTRIE_SURROGATE_BLOCK_COUNT);
  461. indexLength+=UTRIE_SURROGATE_BLOCK_COUNT;
  462. #ifdef UTRIE_DEBUG
  463. printf("trie index count: BMP %ld all Unicode %ld folded %ld\n",
  464. UTRIE_BMP_INDEX_LENGTH, (long)UTRIE_MAX_INDEX_LENGTH, indexLength);
  465. #endif
  466. trie->indexLength=indexLength;
  467. }
  468. /*
  469. * Set a value in the trie index map to indicate which data block
  470. * is referenced and which one is not.
  471. * utrie_compact() will remove data blocks that are not used at all.
  472. * Set
  473. * - 0 if it is used
  474. * - -1 if it is not used
  475. */
  476. static void
  477. _findUnusedBlocks(UNewTrie *trie) {
  478. int32_t i;
  479. /* fill the entire map with "not used" */
  480. uprv_memset(trie->map, 0xff, (UTRIE_MAX_BUILD_TIME_DATA_LENGTH>>UTRIE_SHIFT)*4);
  481. /* mark each block that _is_ used with 0 */
  482. for(i=0; i<trie->indexLength; ++i) {
  483. trie->map[ABS(trie->index[i])>>UTRIE_SHIFT]=0;
  484. }
  485. /* never move the all-initial-value block 0 */
  486. trie->map[0]=0;
  487. }
  488. static int32_t
  489. _findSameDataBlock(const uint32_t *data, int32_t dataLength,
  490. int32_t otherBlock, int32_t step) {
  491. int32_t block;
  492. /* ensure that we do not even partially get past dataLength */
  493. dataLength-=UTRIE_DATA_BLOCK_LENGTH;
  494. for(block=0; block<=dataLength; block+=step) {
  495. if(equal_uint32(data+block, data+otherBlock, UTRIE_DATA_BLOCK_LENGTH)) {
  496. return block;
  497. }
  498. }
  499. return -1;
  500. }
  501. /*
  502. * Compact a folded build-time trie.
  503. *
  504. * The compaction
  505. * - removes blocks that are identical with earlier ones
  506. * - overlaps adjacent blocks as much as possible (if overlap==true)
  507. * - moves blocks in steps of the data granularity
  508. * - moves and overlaps blocks that overlap with multiple values in the overlap region
  509. *
  510. * It does not
  511. * - try to move and overlap blocks that are not already adjacent
  512. */
  513. static void
  514. utrie_compact(UNewTrie *trie, UBool overlap, UErrorCode *pErrorCode) {
  515. int32_t i, start, newStart, overlapStart;
  516. if(pErrorCode==nullptr || U_FAILURE(*pErrorCode)) {
  517. return;
  518. }
  519. /* valid, uncompacted trie? */
  520. if(trie==nullptr) {
  521. *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
  522. return;
  523. }
  524. if(trie->isCompacted) {
  525. return; /* nothing left to do */
  526. }
  527. /* compaction */
  528. /* initialize the index map with "block is used/unused" flags */
  529. _findUnusedBlocks(trie);
  530. /* if Latin-1 is preallocated and linear, then do not compact Latin-1 data */
  531. if(trie->isLatin1Linear && UTRIE_SHIFT<=8) {
  532. overlapStart=UTRIE_DATA_BLOCK_LENGTH+256;
  533. } else {
  534. overlapStart=UTRIE_DATA_BLOCK_LENGTH;
  535. }
  536. newStart=UTRIE_DATA_BLOCK_LENGTH;
  537. for(start=newStart; start<trie->dataLength;) {
  538. /*
  539. * start: index of first entry of current block
  540. * newStart: index where the current block is to be moved
  541. * (right after current end of already-compacted data)
  542. */
  543. /* skip blocks that are not used */
  544. if(trie->map[start>>UTRIE_SHIFT]<0) {
  545. /* advance start to the next block */
  546. start+=UTRIE_DATA_BLOCK_LENGTH;
  547. /* leave newStart with the previous block! */
  548. continue;
  549. }
  550. /* search for an identical block */
  551. if( start>=overlapStart &&
  552. (i=_findSameDataBlock(trie->data, newStart, start,
  553. overlap ? UTRIE_DATA_GRANULARITY : UTRIE_DATA_BLOCK_LENGTH))
  554. >=0
  555. ) {
  556. /* found an identical block, set the other block's index value for the current block */
  557. trie->map[start>>UTRIE_SHIFT]=i;
  558. /* advance start to the next block */
  559. start+=UTRIE_DATA_BLOCK_LENGTH;
  560. /* leave newStart with the previous block! */
  561. continue;
  562. }
  563. /* see if the beginning of this block can be overlapped with the end of the previous block */
  564. if(overlap && start>=overlapStart) {
  565. /* look for maximum overlap (modulo granularity) with the previous, adjacent block */
  566. for(i=UTRIE_DATA_BLOCK_LENGTH-UTRIE_DATA_GRANULARITY;
  567. i>0 && !equal_uint32(trie->data+(newStart-i), trie->data+start, i);
  568. i-=UTRIE_DATA_GRANULARITY) {}
  569. } else {
  570. i=0;
  571. }
  572. if(i>0) {
  573. /* some overlap */
  574. trie->map[start>>UTRIE_SHIFT]=newStart-i;
  575. /* move the non-overlapping indexes to their new positions */
  576. start+=i;
  577. for(i=UTRIE_DATA_BLOCK_LENGTH-i; i>0; --i) {
  578. trie->data[newStart++]=trie->data[start++];
  579. }
  580. } else if(newStart<start) {
  581. /* no overlap, just move the indexes to their new positions */
  582. trie->map[start>>UTRIE_SHIFT]=newStart;
  583. for(i=UTRIE_DATA_BLOCK_LENGTH; i>0; --i) {
  584. trie->data[newStart++]=trie->data[start++];
  585. }
  586. } else /* no overlap && newStart==start */ {
  587. trie->map[start>>UTRIE_SHIFT]=start;
  588. newStart+=UTRIE_DATA_BLOCK_LENGTH;
  589. start=newStart;
  590. }
  591. }
  592. /* now adjust the index (stage 1) table */
  593. for(i=0; i<trie->indexLength; ++i) {
  594. trie->index[i]=trie->map[ABS(trie->index[i])>>UTRIE_SHIFT];
  595. }
  596. #ifdef UTRIE_DEBUG
  597. /* we saved some space */
  598. printf("compacting trie: count of 32-bit words %lu->%lu\n",
  599. (long)trie->dataLength, (long)newStart);
  600. #endif
  601. trie->dataLength=newStart;
  602. }
  603. /* serialization ------------------------------------------------------------ */
  604. /*
  605. * Default function for the folding value:
  606. * Just store the offset (16 bits) if there is any non-initial-value entry.
  607. *
  608. * The offset parameter is never 0.
  609. * Returning the offset itself is safe for UTRIE_SHIFT>=5 because
  610. * for UTRIE_SHIFT==5 the maximum index length is UTRIE_MAX_INDEX_LENGTH==0x8800
  611. * which fits into 16-bit trie values;
  612. * for higher UTRIE_SHIFT, UTRIE_MAX_INDEX_LENGTH decreases.
  613. *
  614. * Theoretically, it would be safer for all possible UTRIE_SHIFT including
  615. * those of 4 and lower to return offset>>UTRIE_SURROGATE_BLOCK_BITS
  616. * which would always result in a value of 0x40..0x43f
  617. * (start/end 1k blocks of supplementary Unicode code points).
  618. * However, this would be uglier, and would not work for some existing
  619. * binary data file formats.
  620. *
  621. * Also, we do not plan to change UTRIE_SHIFT because it would change binary
  622. * data file formats, and we would probably not make it smaller because of
  623. * the then even larger BMP index length even for empty tries.
  624. */
  625. static uint32_t U_CALLCONV
  626. defaultGetFoldedValue(UNewTrie *trie, UChar32 start, int32_t offset) {
  627. uint32_t value, initialValue;
  628. UChar32 limit;
  629. UBool inBlockZero;
  630. initialValue=trie->data[0];
  631. limit=start+0x400;
  632. while(start<limit) {
  633. value=utrie_get32(trie, start, &inBlockZero);
  634. if(inBlockZero) {
  635. start+=UTRIE_DATA_BLOCK_LENGTH;
  636. } else if(value!=initialValue) {
  637. return (uint32_t)offset;
  638. } else {
  639. ++start;
  640. }
  641. }
  642. return 0;
  643. }
  644. U_CAPI int32_t U_EXPORT2
  645. utrie_serialize(UNewTrie *trie, void *dt, int32_t capacity,
  646. UNewTrieGetFoldedValue *getFoldedValue,
  647. UBool reduceTo16Bits,
  648. UErrorCode *pErrorCode) {
  649. UTrieHeader *header;
  650. uint32_t *p;
  651. uint16_t *dest16;
  652. int32_t i, length;
  653. uint8_t* data = nullptr;
  654. /* argument check */
  655. if(pErrorCode==nullptr || U_FAILURE(*pErrorCode)) {
  656. return 0;
  657. }
  658. if(trie==nullptr || capacity<0 || (capacity>0 && dt==nullptr)) {
  659. *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
  660. return 0;
  661. }
  662. if(getFoldedValue==nullptr) {
  663. getFoldedValue=defaultGetFoldedValue;
  664. }
  665. data = (uint8_t*)dt;
  666. /* fold and compact if necessary, also checks that indexLength is within limits */
  667. if(!trie->isCompacted) {
  668. /* compact once without overlap to improve folding */
  669. utrie_compact(trie, false, pErrorCode);
  670. /* fold the supplementary part of the index array */
  671. utrie_fold(trie, getFoldedValue, pErrorCode);
  672. /* compact again with overlap for minimum data array length */
  673. utrie_compact(trie, true, pErrorCode);
  674. trie->isCompacted=true;
  675. if(U_FAILURE(*pErrorCode)) {
  676. return 0;
  677. }
  678. }
  679. /* is dataLength within limits? */
  680. if( (reduceTo16Bits ? (trie->dataLength+trie->indexLength) : trie->dataLength) >= UTRIE_MAX_DATA_LENGTH) {
  681. *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
  682. }
  683. length=sizeof(UTrieHeader)+2*trie->indexLength;
  684. if(reduceTo16Bits) {
  685. length+=2*trie->dataLength;
  686. } else {
  687. length+=4*trie->dataLength;
  688. }
  689. if(length>capacity) {
  690. return length; /* preflighting */
  691. }
  692. #ifdef UTRIE_DEBUG
  693. printf("**UTrieLengths(serialize)** index:%6ld data:%6ld serialized:%6ld\n",
  694. (long)trie->indexLength, (long)trie->dataLength, (long)length);
  695. #endif
  696. /* set the header fields */
  697. header=(UTrieHeader *)data;
  698. data+=sizeof(UTrieHeader);
  699. header->signature=0x54726965; /* "Trie" */
  700. header->options=UTRIE_SHIFT | (UTRIE_INDEX_SHIFT<<UTRIE_OPTIONS_INDEX_SHIFT);
  701. if(!reduceTo16Bits) {
  702. header->options|=UTRIE_OPTIONS_DATA_IS_32_BIT;
  703. }
  704. if(trie->isLatin1Linear) {
  705. header->options|=UTRIE_OPTIONS_LATIN1_IS_LINEAR;
  706. }
  707. header->indexLength=trie->indexLength;
  708. header->dataLength=trie->dataLength;
  709. /* write the index (stage 1) array and the 16/32-bit data (stage 2) array */
  710. if(reduceTo16Bits) {
  711. /* write 16-bit index values shifted right by UTRIE_INDEX_SHIFT, after adding indexLength */
  712. p=(uint32_t *)trie->index;
  713. dest16=(uint16_t *)data;
  714. for(i=trie->indexLength; i>0; --i) {
  715. *dest16++=(uint16_t)((*p++ + trie->indexLength)>>UTRIE_INDEX_SHIFT);
  716. }
  717. /* write 16-bit data values */
  718. p=trie->data;
  719. for(i=trie->dataLength; i>0; --i) {
  720. *dest16++=(uint16_t)*p++;
  721. }
  722. } else {
  723. /* write 16-bit index values shifted right by UTRIE_INDEX_SHIFT */
  724. p=(uint32_t *)trie->index;
  725. dest16=(uint16_t *)data;
  726. for(i=trie->indexLength; i>0; --i) {
  727. *dest16++=(uint16_t)(*p++ >> UTRIE_INDEX_SHIFT);
  728. }
  729. /* write 32-bit data values */
  730. uprv_memcpy(dest16, trie->data, 4*(size_t)trie->dataLength);
  731. }
  732. return length;
  733. }
  734. /* inverse to defaultGetFoldedValue() */
  735. U_CAPI int32_t U_EXPORT2
  736. utrie_defaultGetFoldingOffset(uint32_t data) {
  737. return (int32_t)data;
  738. }
  739. U_CAPI int32_t U_EXPORT2
  740. utrie_unserialize(UTrie *trie, const void *data, int32_t length, UErrorCode *pErrorCode) {
  741. const UTrieHeader *header;
  742. const uint16_t *p16;
  743. uint32_t options;
  744. if(pErrorCode==nullptr || U_FAILURE(*pErrorCode)) {
  745. return -1;
  746. }
  747. /* enough data for a trie header? */
  748. if(length<(int32_t)sizeof(UTrieHeader)) {
  749. *pErrorCode=U_INVALID_FORMAT_ERROR;
  750. return -1;
  751. }
  752. /* check the signature */
  753. header=(const UTrieHeader *)data;
  754. if(header->signature!=0x54726965) {
  755. *pErrorCode=U_INVALID_FORMAT_ERROR;
  756. return -1;
  757. }
  758. /* get the options and check the shift values */
  759. options=header->options;
  760. if( (options&UTRIE_OPTIONS_SHIFT_MASK)!=UTRIE_SHIFT ||
  761. ((options>>UTRIE_OPTIONS_INDEX_SHIFT)&UTRIE_OPTIONS_SHIFT_MASK)!=UTRIE_INDEX_SHIFT
  762. ) {
  763. *pErrorCode=U_INVALID_FORMAT_ERROR;
  764. return -1;
  765. }
  766. trie->isLatin1Linear= (UBool)((options&UTRIE_OPTIONS_LATIN1_IS_LINEAR)!=0);
  767. /* get the length values */
  768. trie->indexLength=header->indexLength;
  769. trie->dataLength=header->dataLength;
  770. length-=(int32_t)sizeof(UTrieHeader);
  771. /* enough data for the index? */
  772. if(length<2*trie->indexLength) {
  773. *pErrorCode=U_INVALID_FORMAT_ERROR;
  774. return -1;
  775. }
  776. p16=(const uint16_t *)(header+1);
  777. trie->index=p16;
  778. p16+=trie->indexLength;
  779. length-=2*trie->indexLength;
  780. /* get the data */
  781. if(options&UTRIE_OPTIONS_DATA_IS_32_BIT) {
  782. if(length<4*trie->dataLength) {
  783. *pErrorCode=U_INVALID_FORMAT_ERROR;
  784. return -1;
  785. }
  786. trie->data32=(const uint32_t *)p16;
  787. trie->initialValue=trie->data32[0];
  788. length=(int32_t)sizeof(UTrieHeader)+2*trie->indexLength+4*trie->dataLength;
  789. } else {
  790. if(length<2*trie->dataLength) {
  791. *pErrorCode=U_INVALID_FORMAT_ERROR;
  792. return -1;
  793. }
  794. /* the "data16" data is used via the index pointer */
  795. trie->data32=nullptr;
  796. trie->initialValue=trie->index[trie->indexLength];
  797. length=(int32_t)sizeof(UTrieHeader)+2*trie->indexLength+2*trie->dataLength;
  798. }
  799. trie->getFoldingOffset=utrie_defaultGetFoldingOffset;
  800. return length;
  801. }
  802. U_CAPI int32_t U_EXPORT2
  803. utrie_unserializeDummy(UTrie *trie,
  804. void *data, int32_t length,
  805. uint32_t initialValue, uint32_t leadUnitValue,
  806. UBool make16BitTrie,
  807. UErrorCode *pErrorCode) {
  808. uint16_t *p16;
  809. int32_t actualLength, latin1Length, i, limit;
  810. uint16_t block;
  811. if(pErrorCode==nullptr || U_FAILURE(*pErrorCode)) {
  812. return -1;
  813. }
  814. /* calculate the actual size of the dummy trie data */
  815. /* max(Latin-1, block 0) */
  816. latin1Length= 256; /*UTRIE_SHIFT<=8 ? 256 : UTRIE_DATA_BLOCK_LENGTH;*/
  817. trie->indexLength=UTRIE_BMP_INDEX_LENGTH+UTRIE_SURROGATE_BLOCK_COUNT;
  818. trie->dataLength=latin1Length;
  819. if(leadUnitValue!=initialValue) {
  820. trie->dataLength+=UTRIE_DATA_BLOCK_LENGTH;
  821. }
  822. actualLength=trie->indexLength*2;
  823. if(make16BitTrie) {
  824. actualLength+=trie->dataLength*2;
  825. } else {
  826. actualLength+=trie->dataLength*4;
  827. }
  828. /* enough space for the dummy trie? */
  829. if(length<actualLength) {
  830. *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
  831. return actualLength;
  832. }
  833. trie->isLatin1Linear=true;
  834. trie->initialValue=initialValue;
  835. /* fill the index and data arrays */
  836. p16=(uint16_t *)data;
  837. trie->index=p16;
  838. if(make16BitTrie) {
  839. /* indexes to block 0 */
  840. block=(uint16_t)(trie->indexLength>>UTRIE_INDEX_SHIFT);
  841. limit=trie->indexLength;
  842. for(i=0; i<limit; ++i) {
  843. p16[i]=block;
  844. }
  845. if(leadUnitValue!=initialValue) {
  846. /* indexes for lead surrogate code units to the block after Latin-1 */
  847. block+=(uint16_t)(latin1Length>>UTRIE_INDEX_SHIFT);
  848. i=0xd800>>UTRIE_SHIFT;
  849. limit=0xdc00>>UTRIE_SHIFT;
  850. for(; i<limit; ++i) {
  851. p16[i]=block;
  852. }
  853. }
  854. trie->data32=nullptr;
  855. /* Latin-1 data */
  856. p16+=trie->indexLength;
  857. for(i=0; i<latin1Length; ++i) {
  858. p16[i]=(uint16_t)initialValue;
  859. }
  860. /* data for lead surrogate code units */
  861. if(leadUnitValue!=initialValue) {
  862. limit=latin1Length+UTRIE_DATA_BLOCK_LENGTH;
  863. for(/* i=latin1Length */; i<limit; ++i) {
  864. p16[i]=(uint16_t)leadUnitValue;
  865. }
  866. }
  867. } else {
  868. uint32_t *p32;
  869. /* indexes to block 0 */
  870. uprv_memset(p16, 0, trie->indexLength*2);
  871. if(leadUnitValue!=initialValue) {
  872. /* indexes for lead surrogate code units to the block after Latin-1 */
  873. block=(uint16_t)(latin1Length>>UTRIE_INDEX_SHIFT);
  874. i=0xd800>>UTRIE_SHIFT;
  875. limit=0xdc00>>UTRIE_SHIFT;
  876. for(; i<limit; ++i) {
  877. p16[i]=block;
  878. }
  879. }
  880. trie->data32=p32=(uint32_t *)(p16+trie->indexLength);
  881. /* Latin-1 data */
  882. for(i=0; i<latin1Length; ++i) {
  883. p32[i]=initialValue;
  884. }
  885. /* data for lead surrogate code units */
  886. if(leadUnitValue!=initialValue) {
  887. limit=latin1Length+UTRIE_DATA_BLOCK_LENGTH;
  888. for(/* i=latin1Length */; i<limit; ++i) {
  889. p32[i]=leadUnitValue;
  890. }
  891. }
  892. }
  893. trie->getFoldingOffset=utrie_defaultGetFoldingOffset;
  894. return actualLength;
  895. }
  896. /* enumeration -------------------------------------------------------------- */
  897. /* default UTrieEnumValue() returns the input value itself */
  898. static uint32_t U_CALLCONV
  899. enumSameValue(const void * /*context*/, uint32_t value) {
  900. return value;
  901. }
  902. /**
  903. * Enumerate all ranges of code points with the same relevant values.
  904. * The values are transformed from the raw trie entries by the enumValue function.
  905. */
  906. U_CAPI void U_EXPORT2
  907. utrie_enum(const UTrie *trie,
  908. UTrieEnumValue *enumValue, UTrieEnumRange *enumRange, const void *context) {
  909. const uint32_t *data32;
  910. const uint16_t *idx;
  911. uint32_t value, prevValue, initialValue;
  912. UChar32 c, prev;
  913. int32_t l, i, j, block, prevBlock, nullBlock, offset;
  914. /* check arguments */
  915. if(trie==nullptr || trie->index==nullptr || enumRange==nullptr) {
  916. return;
  917. }
  918. if(enumValue==nullptr) {
  919. enumValue=enumSameValue;
  920. }
  921. idx=trie->index;
  922. data32=trie->data32;
  923. /* get the enumeration value that corresponds to an initial-value trie data entry */
  924. initialValue=enumValue(context, trie->initialValue);
  925. if(data32==nullptr) {
  926. nullBlock=trie->indexLength;
  927. } else {
  928. nullBlock=0;
  929. }
  930. /* set variables for previous range */
  931. prevBlock=nullBlock;
  932. prev=0;
  933. prevValue=initialValue;
  934. /* enumerate BMP - the main loop enumerates data blocks */
  935. for(i=0, c=0; c<=0xffff; ++i) {
  936. if(c==0xd800) {
  937. /* skip lead surrogate code _units_, go to lead surr. code _points_ */
  938. i=UTRIE_BMP_INDEX_LENGTH;
  939. } else if(c==0xdc00) {
  940. /* go back to regular BMP code points */
  941. i=c>>UTRIE_SHIFT;
  942. }
  943. block=idx[i]<<UTRIE_INDEX_SHIFT;
  944. if(block==prevBlock) {
  945. /* the block is the same as the previous one, and filled with value */
  946. c+=UTRIE_DATA_BLOCK_LENGTH;
  947. } else if(block==nullBlock) {
  948. /* this is the all-initial-value block */
  949. if(prevValue!=initialValue) {
  950. if(prev<c) {
  951. if(!enumRange(context, prev, c, prevValue)) {
  952. return;
  953. }
  954. }
  955. prevBlock=nullBlock;
  956. prev=c;
  957. prevValue=initialValue;
  958. }
  959. c+=UTRIE_DATA_BLOCK_LENGTH;
  960. } else {
  961. prevBlock=block;
  962. for(j=0; j<UTRIE_DATA_BLOCK_LENGTH; ++j) {
  963. value=enumValue(context, data32!=nullptr ? data32[block+j] : idx[block+j]);
  964. if(value!=prevValue) {
  965. if(prev<c) {
  966. if(!enumRange(context, prev, c, prevValue)) {
  967. return;
  968. }
  969. }
  970. if(j>0) {
  971. /* the block is not filled with all the same value */
  972. prevBlock=-1;
  973. }
  974. prev=c;
  975. prevValue=value;
  976. }
  977. ++c;
  978. }
  979. }
  980. }
  981. /* enumerate supplementary code points */
  982. for(l=0xd800; l<0xdc00;) {
  983. /* lead surrogate access */
  984. offset=idx[l>>UTRIE_SHIFT]<<UTRIE_INDEX_SHIFT;
  985. if(offset==nullBlock) {
  986. /* no entries for a whole block of lead surrogates */
  987. if(prevValue!=initialValue) {
  988. if(prev<c) {
  989. if(!enumRange(context, prev, c, prevValue)) {
  990. return;
  991. }
  992. }
  993. prevBlock=nullBlock;
  994. prev=c;
  995. prevValue=initialValue;
  996. }
  997. l+=UTRIE_DATA_BLOCK_LENGTH;
  998. c+=UTRIE_DATA_BLOCK_LENGTH<<10;
  999. continue;
  1000. }
  1001. value= data32!=nullptr ? data32[offset+(l&UTRIE_MASK)] : idx[offset+(l&UTRIE_MASK)];
  1002. /* enumerate trail surrogates for this lead surrogate */
  1003. offset=trie->getFoldingOffset(value);
  1004. if(offset<=0) {
  1005. /* no data for this lead surrogate */
  1006. if(prevValue!=initialValue) {
  1007. if(prev<c) {
  1008. if(!enumRange(context, prev, c, prevValue)) {
  1009. return;
  1010. }
  1011. }
  1012. prevBlock=nullBlock;
  1013. prev=c;
  1014. prevValue=initialValue;
  1015. }
  1016. /* nothing else to do for the supplementary code points for this lead surrogate */
  1017. c+=0x400;
  1018. } else {
  1019. /* enumerate code points for this lead surrogate */
  1020. i=offset;
  1021. offset+=UTRIE_SURROGATE_BLOCK_COUNT;
  1022. do {
  1023. /* copy of most of the body of the BMP loop */
  1024. block=idx[i]<<UTRIE_INDEX_SHIFT;
  1025. if(block==prevBlock) {
  1026. /* the block is the same as the previous one, and filled with value */
  1027. c+=UTRIE_DATA_BLOCK_LENGTH;
  1028. } else if(block==nullBlock) {
  1029. /* this is the all-initial-value block */
  1030. if(prevValue!=initialValue) {
  1031. if(prev<c) {
  1032. if(!enumRange(context, prev, c, prevValue)) {
  1033. return;
  1034. }
  1035. }
  1036. prevBlock=nullBlock;
  1037. prev=c;
  1038. prevValue=initialValue;
  1039. }
  1040. c+=UTRIE_DATA_BLOCK_LENGTH;
  1041. } else {
  1042. prevBlock=block;
  1043. for(j=0; j<UTRIE_DATA_BLOCK_LENGTH; ++j) {
  1044. value=enumValue(context, data32!=nullptr ? data32[block+j] : idx[block+j]);
  1045. if(value!=prevValue) {
  1046. if(prev<c) {
  1047. if(!enumRange(context, prev, c, prevValue)) {
  1048. return;
  1049. }
  1050. }
  1051. if(j>0) {
  1052. /* the block is not filled with all the same value */
  1053. prevBlock=-1;
  1054. }
  1055. prev=c;
  1056. prevValue=value;
  1057. }
  1058. ++c;
  1059. }
  1060. }
  1061. } while(++i<offset);
  1062. }
  1063. ++l;
  1064. }
  1065. /* deliver last range */
  1066. enumRange(context, prev, c, prevValue);
  1067. }