utrie2_builder.cpp 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481
  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-2014, International Business Machines
  7. * Corporation and others. All Rights Reserved.
  8. *
  9. ******************************************************************************
  10. * file name: utrie2_builder.cpp
  11. * encoding: UTF-8
  12. * tab size: 8 (not used)
  13. * indentation:4
  14. *
  15. * created on: 2008sep26 (split off from utrie2.c)
  16. * created by: Markus W. Scherer
  17. *
  18. * This is a common implementation of a Unicode 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. * This is the second common version of a Unicode trie (hence the name UTrie2).
  22. * See utrie2.h for a comparison.
  23. *
  24. * This file contains only the builder code.
  25. * See utrie2.c for the runtime and enumeration code.
  26. */
  27. // #define UTRIE2_DEBUG
  28. #ifdef UTRIE2_DEBUG
  29. # include <stdio.h>
  30. #endif
  31. // #define UCPTRIE_DEBUG
  32. #include "unicode/utypes.h"
  33. #ifdef UCPTRIE_DEBUG
  34. #include "unicode/ucptrie.h"
  35. #include "unicode/umutablecptrie.h"
  36. #include "ucptrie_impl.h"
  37. #endif
  38. #include "cmemory.h"
  39. #include "utrie2.h"
  40. #include "utrie2_impl.h"
  41. #include "utrie.h" // for utrie2_fromUTrie()
  42. /* Implementation notes ----------------------------------------------------- */
  43. /*
  44. * The UTRIE2_SHIFT_1, UTRIE2_SHIFT_2, UTRIE2_INDEX_SHIFT and other values
  45. * have been chosen to minimize trie sizes overall.
  46. * Most of the code is flexible enough to work with a range of values,
  47. * within certain limits.
  48. *
  49. * Exception: Support for separate values for lead surrogate code _units_
  50. * vs. code _points_ was added after the constants were fixed,
  51. * and has not been tested nor particularly designed for different constant values.
  52. * (Especially the utrie2_enum() code that jumps to the special LSCP index-2
  53. * part and back.)
  54. *
  55. * Requires UTRIE2_SHIFT_2<=6. Otherwise 0xc0 which is the top of the ASCII-linear data
  56. * including the bad-UTF-8-data block is not a multiple of UTRIE2_DATA_BLOCK_LENGTH
  57. * and map[block>>UTRIE2_SHIFT_2] (used in reference counting and compaction
  58. * remapping) stops working.
  59. *
  60. * Requires UTRIE2_SHIFT_1>=10 because utrie2_enumForLeadSurrogate()
  61. * assumes that a single index-2 block is used for 0x400 code points
  62. * corresponding to one lead surrogate.
  63. *
  64. * Requires UTRIE2_SHIFT_1<=16. Otherwise one single index-2 block contains
  65. * more than one Unicode plane, and the split of the index-2 table into a BMP
  66. * part and a supplementary part, with a gap in between, would not work.
  67. *
  68. * Requires UTRIE2_INDEX_SHIFT>=1 not because of the code but because
  69. * there is data with more than 64k distinct values,
  70. * for example for Unihan collation with a separate collation weight per
  71. * Han character.
  72. */
  73. /* Building a trie ----------------------------------------------------------*/
  74. enum {
  75. /** The null index-2 block, following the gap in the index-2 table. */
  76. UNEWTRIE2_INDEX_2_NULL_OFFSET=UNEWTRIE2_INDEX_GAP_OFFSET+UNEWTRIE2_INDEX_GAP_LENGTH,
  77. /** The start of allocated index-2 blocks. */
  78. UNEWTRIE2_INDEX_2_START_OFFSET=UNEWTRIE2_INDEX_2_NULL_OFFSET+UTRIE2_INDEX_2_BLOCK_LENGTH,
  79. /**
  80. * The null data block.
  81. * Length 64=0x40 even if UTRIE2_DATA_BLOCK_LENGTH is smaller,
  82. * to work with 6-bit trail bytes from 2-byte UTF-8.
  83. */
  84. UNEWTRIE2_DATA_NULL_OFFSET=UTRIE2_DATA_START_OFFSET,
  85. /** The start of allocated data blocks. */
  86. UNEWTRIE2_DATA_START_OFFSET=UNEWTRIE2_DATA_NULL_OFFSET+0x40,
  87. /**
  88. * The start of data blocks for U+0800 and above.
  89. * Below, compaction uses a block length of 64 for 2-byte UTF-8.
  90. * From here on, compaction uses UTRIE2_DATA_BLOCK_LENGTH.
  91. * Data values for 0x780 code points beyond ASCII.
  92. */
  93. UNEWTRIE2_DATA_0800_OFFSET=UNEWTRIE2_DATA_START_OFFSET+0x780
  94. };
  95. /* Start with allocation of 16k data entries. */
  96. #define UNEWTRIE2_INITIAL_DATA_LENGTH ((int32_t)1<<14)
  97. /* Grow about 8x each time. */
  98. #define UNEWTRIE2_MEDIUM_DATA_LENGTH ((int32_t)1<<17)
  99. static int32_t
  100. allocIndex2Block(UNewTrie2 *trie);
  101. U_CAPI UTrie2 * U_EXPORT2
  102. utrie2_open(uint32_t initialValue, uint32_t errorValue, UErrorCode *pErrorCode) {
  103. UTrie2 *trie;
  104. UNewTrie2 *newTrie;
  105. uint32_t *data;
  106. int32_t i, j;
  107. if(U_FAILURE(*pErrorCode)) {
  108. return nullptr;
  109. }
  110. trie=(UTrie2 *)uprv_malloc(sizeof(UTrie2));
  111. newTrie=(UNewTrie2 *)uprv_malloc(sizeof(UNewTrie2));
  112. data=(uint32_t *)uprv_malloc(UNEWTRIE2_INITIAL_DATA_LENGTH*4);
  113. if(trie==nullptr || newTrie==nullptr || data==nullptr) {
  114. uprv_free(trie);
  115. uprv_free(newTrie);
  116. uprv_free(data);
  117. *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
  118. return nullptr;
  119. }
  120. uprv_memset(trie, 0, sizeof(UTrie2));
  121. trie->initialValue=initialValue;
  122. trie->errorValue=errorValue;
  123. trie->highStart=0x110000;
  124. trie->newTrie=newTrie;
  125. #ifdef UTRIE2_DEBUG
  126. trie->name="open";
  127. #endif
  128. newTrie->data=data;
  129. #ifdef UCPTRIE_DEBUG
  130. newTrie->t3=umutablecptrie_open(initialValue, errorValue, pErrorCode);
  131. #endif
  132. newTrie->dataCapacity=UNEWTRIE2_INITIAL_DATA_LENGTH;
  133. newTrie->initialValue=initialValue;
  134. newTrie->errorValue=errorValue;
  135. newTrie->highStart=0x110000;
  136. newTrie->firstFreeBlock=0; /* no free block in the list */
  137. newTrie->isCompacted=false;
  138. /*
  139. * preallocate and reset
  140. * - ASCII
  141. * - the bad-UTF-8-data block
  142. * - the null data block
  143. */
  144. for(i=0; i<0x80; ++i) {
  145. newTrie->data[i]=initialValue;
  146. }
  147. for(; i<0xc0; ++i) {
  148. newTrie->data[i]=errorValue;
  149. }
  150. for(i=UNEWTRIE2_DATA_NULL_OFFSET; i<UNEWTRIE2_DATA_START_OFFSET; ++i) {
  151. newTrie->data[i]=initialValue;
  152. }
  153. newTrie->dataNullOffset=UNEWTRIE2_DATA_NULL_OFFSET;
  154. newTrie->dataLength=UNEWTRIE2_DATA_START_OFFSET;
  155. /* set the index-2 indexes for the 2=0x80>>UTRIE2_SHIFT_2 ASCII data blocks */
  156. for(i=0, j=0; j<0x80; ++i, j+=UTRIE2_DATA_BLOCK_LENGTH) {
  157. newTrie->index2[i]=j;
  158. newTrie->map[i]=1;
  159. }
  160. /* reference counts for the bad-UTF-8-data block */
  161. for(; j<0xc0; ++i, j+=UTRIE2_DATA_BLOCK_LENGTH) {
  162. newTrie->map[i]=0;
  163. }
  164. /*
  165. * Reference counts for the null data block: all blocks except for the ASCII blocks.
  166. * Plus 1 so that we don't drop this block during compaction.
  167. * Plus as many as needed for lead surrogate code points.
  168. */
  169. /* i==newTrie->dataNullOffset */
  170. newTrie->map[i++]=
  171. (0x110000>>UTRIE2_SHIFT_2)-
  172. (0x80>>UTRIE2_SHIFT_2)+
  173. 1+
  174. UTRIE2_LSCP_INDEX_2_LENGTH;
  175. j+=UTRIE2_DATA_BLOCK_LENGTH;
  176. for(; j<UNEWTRIE2_DATA_START_OFFSET; ++i, j+=UTRIE2_DATA_BLOCK_LENGTH) {
  177. newTrie->map[i]=0;
  178. }
  179. /*
  180. * set the remaining indexes in the BMP index-2 block
  181. * to the null data block
  182. */
  183. for(i=0x80>>UTRIE2_SHIFT_2; i<UTRIE2_INDEX_2_BMP_LENGTH; ++i) {
  184. newTrie->index2[i]=UNEWTRIE2_DATA_NULL_OFFSET;
  185. }
  186. /*
  187. * Fill the index gap with impossible values so that compaction
  188. * does not overlap other index-2 blocks with the gap.
  189. */
  190. for(i=0; i<UNEWTRIE2_INDEX_GAP_LENGTH; ++i) {
  191. newTrie->index2[UNEWTRIE2_INDEX_GAP_OFFSET+i]=-1;
  192. }
  193. /* set the indexes in the null index-2 block */
  194. for(i=0; i<UTRIE2_INDEX_2_BLOCK_LENGTH; ++i) {
  195. newTrie->index2[UNEWTRIE2_INDEX_2_NULL_OFFSET+i]=UNEWTRIE2_DATA_NULL_OFFSET;
  196. }
  197. newTrie->index2NullOffset=UNEWTRIE2_INDEX_2_NULL_OFFSET;
  198. newTrie->index2Length=UNEWTRIE2_INDEX_2_START_OFFSET;
  199. /* set the index-1 indexes for the linear index-2 block */
  200. for(i=0, j=0;
  201. i<UTRIE2_OMITTED_BMP_INDEX_1_LENGTH;
  202. ++i, j+=UTRIE2_INDEX_2_BLOCK_LENGTH
  203. ) {
  204. newTrie->index1[i]=j;
  205. }
  206. /* set the remaining index-1 indexes to the null index-2 block */
  207. for(; i<UNEWTRIE2_INDEX_1_LENGTH; ++i) {
  208. newTrie->index1[i]=UNEWTRIE2_INDEX_2_NULL_OFFSET;
  209. }
  210. /*
  211. * Preallocate and reset data for U+0080..U+07ff,
  212. * for 2-byte UTF-8 which will be compacted in 64-blocks
  213. * even if UTRIE2_DATA_BLOCK_LENGTH is smaller.
  214. */
  215. for(i=0x80; i<0x800; i+=UTRIE2_DATA_BLOCK_LENGTH) {
  216. utrie2_set32(trie, i, initialValue, pErrorCode);
  217. }
  218. return trie;
  219. }
  220. static UNewTrie2 *
  221. cloneBuilder(const UNewTrie2 *other) {
  222. UNewTrie2 *trie;
  223. trie = static_cast<UNewTrie2*>(uprv_malloc(sizeof(UNewTrie2)));
  224. if(trie==nullptr) {
  225. return nullptr;
  226. }
  227. trie->data = static_cast<uint32_t*>(uprv_malloc(other->dataCapacity * 4));
  228. if(trie->data==nullptr) {
  229. uprv_free(trie);
  230. return nullptr;
  231. }
  232. #ifdef UCPTRIE_DEBUG
  233. if(other->t3==nullptr) {
  234. trie->t3=nullptr;
  235. } else {
  236. UErrorCode errorCode=U_ZERO_ERROR;
  237. trie->t3=umutablecptrie_clone(other->t3, &errorCode);
  238. }
  239. #endif
  240. trie->dataCapacity=other->dataCapacity;
  241. /* clone data */
  242. uprv_memcpy(trie->index1, other->index1, sizeof(trie->index1));
  243. uprv_memcpy(trie->index2, other->index2, (size_t)other->index2Length*4);
  244. trie->index2NullOffset=other->index2NullOffset;
  245. trie->index2Length=other->index2Length;
  246. uprv_memcpy(trie->data, other->data, (size_t)other->dataLength*4);
  247. trie->dataNullOffset=other->dataNullOffset;
  248. trie->dataLength=other->dataLength;
  249. /* reference counters */
  250. if(other->isCompacted) {
  251. trie->firstFreeBlock=0;
  252. } else {
  253. uprv_memcpy(trie->map, other->map, ((size_t)other->dataLength>>UTRIE2_SHIFT_2)*4);
  254. trie->firstFreeBlock=other->firstFreeBlock;
  255. }
  256. trie->initialValue=other->initialValue;
  257. trie->errorValue=other->errorValue;
  258. trie->highStart=other->highStart;
  259. trie->isCompacted=other->isCompacted;
  260. return trie;
  261. }
  262. U_CAPI UTrie2 * U_EXPORT2
  263. utrie2_clone(const UTrie2 *other, UErrorCode *pErrorCode) {
  264. UTrie2 *trie;
  265. if(U_FAILURE(*pErrorCode)) {
  266. return nullptr;
  267. }
  268. if(other==nullptr || (other->memory==nullptr && other->newTrie==nullptr)) {
  269. *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
  270. return nullptr;
  271. }
  272. trie=(UTrie2 *)uprv_malloc(sizeof(UTrie2));
  273. if(trie==nullptr) {
  274. *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
  275. return nullptr;
  276. }
  277. uprv_memcpy(trie, other, sizeof(UTrie2));
  278. if(other->memory!=nullptr) {
  279. trie->memory=uprv_malloc(other->length);
  280. if(trie->memory!=nullptr) {
  281. trie->isMemoryOwned=true;
  282. uprv_memcpy(trie->memory, other->memory, other->length);
  283. /* make the clone's pointers point to its own memory */
  284. trie->index=(uint16_t *)trie->memory+(other->index-(uint16_t *)other->memory);
  285. if(other->data16!=nullptr) {
  286. trie->data16=(uint16_t *)trie->memory+(other->data16-(uint16_t *)other->memory);
  287. }
  288. if(other->data32!=nullptr) {
  289. trie->data32=(uint32_t *)trie->memory+(other->data32-(uint32_t *)other->memory);
  290. }
  291. }
  292. } else /* other->newTrie!=nullptr */ {
  293. trie->newTrie=cloneBuilder(other->newTrie);
  294. }
  295. if(trie->memory==nullptr && trie->newTrie==nullptr) {
  296. *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
  297. uprv_free(trie);
  298. trie=nullptr;
  299. }
  300. return trie;
  301. }
  302. typedef struct NewTrieAndStatus {
  303. UTrie2 *trie;
  304. UErrorCode errorCode;
  305. UBool exclusiveLimit; /* rather than inclusive range end */
  306. } NewTrieAndStatus;
  307. static UBool U_CALLCONV
  308. copyEnumRange(const void *context, UChar32 start, UChar32 end, uint32_t value) {
  309. NewTrieAndStatus *nt=(NewTrieAndStatus *)context;
  310. if(value!=nt->trie->initialValue) {
  311. if(nt->exclusiveLimit) {
  312. --end;
  313. }
  314. if(start==end) {
  315. utrie2_set32(nt->trie, start, value, &nt->errorCode);
  316. } else {
  317. utrie2_setRange32(nt->trie, start, end, value, true, &nt->errorCode);
  318. }
  319. return U_SUCCESS(nt->errorCode);
  320. } else {
  321. return true;
  322. }
  323. }
  324. #ifdef UTRIE2_DEBUG
  325. static long countInitial(const UTrie2 *trie) {
  326. uint32_t initialValue=trie->initialValue;
  327. int32_t length=trie->dataLength;
  328. long count=0;
  329. if(trie->data16!=nullptr) {
  330. for(int32_t i=0; i<length; ++i) {
  331. if(trie->data16[i]==initialValue) { ++count; }
  332. }
  333. } else {
  334. for(int32_t i=0; i<length; ++i) {
  335. if(trie->data32[i]==initialValue) { ++count; }
  336. }
  337. }
  338. return count;
  339. }
  340. static void
  341. utrie_printLengths(const UTrie *trie) {
  342. long indexLength=trie->indexLength;
  343. long dataLength=(long)trie->dataLength;
  344. long totalLength=(long)sizeof(UTrieHeader)+indexLength*2+dataLength*(trie->data32!=nullptr ? 4 : 2);
  345. printf("**UTrieLengths** index:%6ld data:%6ld serialized:%6ld\n",
  346. indexLength, dataLength, totalLength);
  347. }
  348. static void
  349. utrie2_printLengths(const UTrie2 *trie, const char *which) {
  350. long indexLength=trie->indexLength;
  351. long dataLength=(long)trie->dataLength;
  352. long totalLength=(long)sizeof(UTrie2Header)+indexLength*2+dataLength*(trie->data32!=nullptr ? 4 : 2);
  353. printf("**UTrie2Lengths(%s %s)** index:%6ld data:%6ld countInitial:%6ld serialized:%6ld\n",
  354. which, trie->name, indexLength, dataLength, countInitial(trie), totalLength);
  355. }
  356. #endif
  357. U_CAPI UTrie2 * U_EXPORT2
  358. utrie2_cloneAsThawed(const UTrie2 *other, UErrorCode *pErrorCode) {
  359. NewTrieAndStatus context;
  360. char16_t lead;
  361. if(U_FAILURE(*pErrorCode)) {
  362. return nullptr;
  363. }
  364. if(other==nullptr || (other->memory==nullptr && other->newTrie==nullptr)) {
  365. *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
  366. return nullptr;
  367. }
  368. if(other->newTrie!=nullptr && !other->newTrie->isCompacted) {
  369. return utrie2_clone(other, pErrorCode); /* clone an unfrozen trie */
  370. }
  371. /* Clone the frozen trie by enumerating it and building a new one. */
  372. context.trie=utrie2_open(other->initialValue, other->errorValue, pErrorCode);
  373. if(U_FAILURE(*pErrorCode)) {
  374. return nullptr;
  375. }
  376. context.exclusiveLimit=false;
  377. context.errorCode=*pErrorCode;
  378. utrie2_enum(other, nullptr, copyEnumRange, &context);
  379. *pErrorCode=context.errorCode;
  380. for(lead=0xd800; lead<0xdc00; ++lead) {
  381. uint32_t value;
  382. if(other->data32==nullptr) {
  383. value=UTRIE2_GET16_FROM_U16_SINGLE_LEAD(other, lead);
  384. } else {
  385. value=UTRIE2_GET32_FROM_U16_SINGLE_LEAD(other, lead);
  386. }
  387. if(value!=other->initialValue) {
  388. utrie2_set32ForLeadSurrogateCodeUnit(context.trie, lead, value, pErrorCode);
  389. }
  390. }
  391. if(U_FAILURE(*pErrorCode)) {
  392. utrie2_close(context.trie);
  393. context.trie=nullptr;
  394. }
  395. return context.trie;
  396. }
  397. /* Almost the same as utrie2_cloneAsThawed() but copies a UTrie and freezes the clone. */
  398. U_CAPI UTrie2 * U_EXPORT2
  399. utrie2_fromUTrie(const UTrie *trie1, uint32_t errorValue, UErrorCode *pErrorCode) {
  400. NewTrieAndStatus context;
  401. char16_t lead;
  402. if(U_FAILURE(*pErrorCode)) {
  403. return nullptr;
  404. }
  405. if(trie1==nullptr) {
  406. *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
  407. return nullptr;
  408. }
  409. context.trie=utrie2_open(trie1->initialValue, errorValue, pErrorCode);
  410. if(U_FAILURE(*pErrorCode)) {
  411. return nullptr;
  412. }
  413. context.exclusiveLimit=true;
  414. context.errorCode=*pErrorCode;
  415. utrie_enum(trie1, nullptr, copyEnumRange, &context);
  416. *pErrorCode=context.errorCode;
  417. for(lead=0xd800; lead<0xdc00; ++lead) {
  418. uint32_t value;
  419. if(trie1->data32==nullptr) {
  420. value=UTRIE_GET16_FROM_LEAD(trie1, lead);
  421. } else {
  422. value=UTRIE_GET32_FROM_LEAD(trie1, lead);
  423. }
  424. if(value!=trie1->initialValue) {
  425. utrie2_set32ForLeadSurrogateCodeUnit(context.trie, lead, value, pErrorCode);
  426. }
  427. }
  428. if(U_SUCCESS(*pErrorCode)) {
  429. utrie2_freeze(context.trie,
  430. trie1->data32!=nullptr ? UTRIE2_32_VALUE_BITS : UTRIE2_16_VALUE_BITS,
  431. pErrorCode);
  432. }
  433. #ifdef UTRIE2_DEBUG
  434. if(U_SUCCESS(*pErrorCode)) {
  435. utrie_printLengths(trie1);
  436. utrie2_printLengths(context.trie, "fromUTrie");
  437. }
  438. #endif
  439. if(U_FAILURE(*pErrorCode)) {
  440. utrie2_close(context.trie);
  441. context.trie=nullptr;
  442. }
  443. return context.trie;
  444. }
  445. static inline UBool
  446. isInNullBlock(UNewTrie2 *trie, UChar32 c, UBool forLSCP) {
  447. int32_t i2, block;
  448. if(U_IS_LEAD(c) && forLSCP) {
  449. i2=(UTRIE2_LSCP_INDEX_2_OFFSET-(0xd800>>UTRIE2_SHIFT_2))+
  450. (c>>UTRIE2_SHIFT_2);
  451. } else {
  452. i2=trie->index1[c>>UTRIE2_SHIFT_1]+
  453. ((c>>UTRIE2_SHIFT_2)&UTRIE2_INDEX_2_MASK);
  454. }
  455. block=trie->index2[i2];
  456. return block == trie->dataNullOffset;
  457. }
  458. static int32_t
  459. allocIndex2Block(UNewTrie2 *trie) {
  460. int32_t newBlock, newTop;
  461. newBlock=trie->index2Length;
  462. newTop=newBlock+UTRIE2_INDEX_2_BLOCK_LENGTH;
  463. if(newTop>UPRV_LENGTHOF(trie->index2)) {
  464. /*
  465. * Should never occur.
  466. * Either UTRIE2_MAX_BUILD_TIME_INDEX_LENGTH is incorrect,
  467. * or the code writes more values than should be possible.
  468. */
  469. return -1;
  470. }
  471. trie->index2Length=newTop;
  472. uprv_memcpy(trie->index2+newBlock, trie->index2+trie->index2NullOffset, UTRIE2_INDEX_2_BLOCK_LENGTH*4);
  473. return newBlock;
  474. }
  475. static int32_t
  476. getIndex2Block(UNewTrie2 *trie, UChar32 c, UBool forLSCP) {
  477. int32_t i1, i2;
  478. if(U_IS_LEAD(c) && forLSCP) {
  479. return UTRIE2_LSCP_INDEX_2_OFFSET;
  480. }
  481. i1=c>>UTRIE2_SHIFT_1;
  482. i2=trie->index1[i1];
  483. if(i2==trie->index2NullOffset) {
  484. i2=allocIndex2Block(trie);
  485. if(i2<0) {
  486. return -1; /* program error */
  487. }
  488. trie->index1[i1]=i2;
  489. }
  490. return i2;
  491. }
  492. static int32_t
  493. allocDataBlock(UNewTrie2 *trie, int32_t copyBlock) {
  494. int32_t newBlock, newTop;
  495. if(trie->firstFreeBlock!=0) {
  496. /* get the first free block */
  497. newBlock=trie->firstFreeBlock;
  498. trie->firstFreeBlock=-trie->map[newBlock>>UTRIE2_SHIFT_2];
  499. } else {
  500. /* get a new block from the high end */
  501. newBlock=trie->dataLength;
  502. newTop=newBlock+UTRIE2_DATA_BLOCK_LENGTH;
  503. if(newTop>trie->dataCapacity) {
  504. /* out of memory in the data array */
  505. int32_t capacity;
  506. uint32_t *data;
  507. if(trie->dataCapacity<UNEWTRIE2_MEDIUM_DATA_LENGTH) {
  508. capacity=UNEWTRIE2_MEDIUM_DATA_LENGTH;
  509. } else if(trie->dataCapacity<UNEWTRIE2_MAX_DATA_LENGTH) {
  510. capacity=UNEWTRIE2_MAX_DATA_LENGTH;
  511. } else {
  512. /*
  513. * Should never occur.
  514. * Either UNEWTRIE2_MAX_DATA_LENGTH is incorrect,
  515. * or the code writes more values than should be possible.
  516. */
  517. return -1;
  518. }
  519. data = static_cast<uint32_t*>(uprv_malloc(capacity * 4));
  520. if(data==nullptr) {
  521. return -1;
  522. }
  523. uprv_memcpy(data, trie->data, (size_t)trie->dataLength*4);
  524. uprv_free(trie->data);
  525. trie->data=data;
  526. trie->dataCapacity=capacity;
  527. }
  528. trie->dataLength=newTop;
  529. }
  530. uprv_memcpy(trie->data+newBlock, trie->data+copyBlock, UTRIE2_DATA_BLOCK_LENGTH*4);
  531. trie->map[newBlock>>UTRIE2_SHIFT_2]=0;
  532. return newBlock;
  533. }
  534. /* call when the block's reference counter reaches 0 */
  535. static void
  536. releaseDataBlock(UNewTrie2 *trie, int32_t block) {
  537. /* put this block at the front of the free-block chain */
  538. trie->map[block>>UTRIE2_SHIFT_2]=-trie->firstFreeBlock;
  539. trie->firstFreeBlock=block;
  540. }
  541. static inline UBool
  542. isWritableBlock(UNewTrie2 *trie, int32_t block) {
  543. return block != trie->dataNullOffset && 1 == trie->map[block >> UTRIE2_SHIFT_2];
  544. }
  545. static inline void
  546. setIndex2Entry(UNewTrie2 *trie, int32_t i2, int32_t block) {
  547. int32_t oldBlock;
  548. ++trie->map[block>>UTRIE2_SHIFT_2]; /* increment first, in case block==oldBlock! */
  549. oldBlock=trie->index2[i2];
  550. if(0 == --trie->map[oldBlock>>UTRIE2_SHIFT_2]) {
  551. releaseDataBlock(trie, oldBlock);
  552. }
  553. trie->index2[i2]=block;
  554. }
  555. /**
  556. * No error checking for illegal arguments.
  557. *
  558. * @return -1 if no new data block available (out of memory in data array)
  559. * @internal
  560. */
  561. static int32_t
  562. getDataBlock(UNewTrie2 *trie, UChar32 c, UBool forLSCP) {
  563. int32_t i2, oldBlock, newBlock;
  564. i2=getIndex2Block(trie, c, forLSCP);
  565. if(i2<0) {
  566. return -1; /* program error */
  567. }
  568. i2+=(c>>UTRIE2_SHIFT_2)&UTRIE2_INDEX_2_MASK;
  569. oldBlock=trie->index2[i2];
  570. if(isWritableBlock(trie, oldBlock)) {
  571. return oldBlock;
  572. }
  573. /* allocate a new data block */
  574. newBlock=allocDataBlock(trie, oldBlock);
  575. if(newBlock<0) {
  576. /* out of memory in the data array */
  577. return -1;
  578. }
  579. setIndex2Entry(trie, i2, newBlock);
  580. return newBlock;
  581. }
  582. /**
  583. * @return true if the value was successfully set
  584. */
  585. static void
  586. set32(UNewTrie2 *trie,
  587. UChar32 c, UBool forLSCP, uint32_t value,
  588. UErrorCode *pErrorCode) {
  589. int32_t block;
  590. if(trie==nullptr || trie->isCompacted) {
  591. *pErrorCode=U_NO_WRITE_PERMISSION;
  592. return;
  593. }
  594. #ifdef UCPTRIE_DEBUG
  595. umutablecptrie_set(trie->t3, c, value, pErrorCode);
  596. #endif
  597. block=getDataBlock(trie, c, forLSCP);
  598. if(block<0) {
  599. *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
  600. return;
  601. }
  602. trie->data[block+(c&UTRIE2_DATA_MASK)]=value;
  603. }
  604. U_CAPI void U_EXPORT2
  605. utrie2_set32(UTrie2 *trie, UChar32 c, uint32_t value, UErrorCode *pErrorCode) {
  606. if(U_FAILURE(*pErrorCode)) {
  607. return;
  608. }
  609. if((uint32_t)c>0x10ffff) {
  610. *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
  611. return;
  612. }
  613. set32(trie->newTrie, c, true, value, pErrorCode);
  614. }
  615. U_CAPI void U_EXPORT2
  616. utrie2_set32ForLeadSurrogateCodeUnit(UTrie2 *trie,
  617. UChar32 c, uint32_t value,
  618. UErrorCode *pErrorCode) {
  619. if(U_FAILURE(*pErrorCode)) {
  620. return;
  621. }
  622. if(!U_IS_LEAD(c)) {
  623. *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
  624. return;
  625. }
  626. set32(trie->newTrie, c, false, value, pErrorCode);
  627. }
  628. static void
  629. writeBlock(uint32_t *block, uint32_t value) {
  630. uint32_t *limit=block+UTRIE2_DATA_BLOCK_LENGTH;
  631. while(block<limit) {
  632. *block++=value;
  633. }
  634. }
  635. /**
  636. * initialValue is ignored if overwrite=true
  637. * @internal
  638. */
  639. static void
  640. fillBlock(uint32_t *block, UChar32 start, UChar32 limit,
  641. uint32_t value, uint32_t initialValue, UBool overwrite) {
  642. uint32_t *pLimit;
  643. pLimit=block+limit;
  644. block+=start;
  645. if(overwrite) {
  646. while(block<pLimit) {
  647. *block++=value;
  648. }
  649. } else {
  650. while(block<pLimit) {
  651. if(*block==initialValue) {
  652. *block=value;
  653. }
  654. ++block;
  655. }
  656. }
  657. }
  658. U_CAPI void U_EXPORT2
  659. utrie2_setRange32(UTrie2 *trie,
  660. UChar32 start, UChar32 end,
  661. uint32_t value, UBool overwrite,
  662. UErrorCode *pErrorCode) {
  663. /*
  664. * repeat value in [start..end]
  665. * mark index values for repeat-data blocks by setting bit 31 of the index values
  666. * fill around existing values if any, if(overwrite)
  667. */
  668. UNewTrie2 *newTrie;
  669. int32_t block, rest, repeatBlock;
  670. UChar32 limit;
  671. if(U_FAILURE(*pErrorCode)) {
  672. return;
  673. }
  674. if((uint32_t)start>0x10ffff || (uint32_t)end>0x10ffff || start>end) {
  675. *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
  676. return;
  677. }
  678. newTrie=trie->newTrie;
  679. if(newTrie==nullptr || newTrie->isCompacted) {
  680. *pErrorCode=U_NO_WRITE_PERMISSION;
  681. return;
  682. }
  683. #ifdef UCPTRIE_DEBUG
  684. umutablecptrie_setRange(newTrie->t3, start, end, value, pErrorCode);
  685. #endif
  686. if(!overwrite && value==newTrie->initialValue) {
  687. return; /* nothing to do */
  688. }
  689. limit=end+1;
  690. if(start&UTRIE2_DATA_MASK) {
  691. UChar32 nextStart;
  692. /* set partial block at [start..following block boundary[ */
  693. block=getDataBlock(newTrie, start, true);
  694. if(block<0) {
  695. *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
  696. return;
  697. }
  698. nextStart=(start+UTRIE2_DATA_MASK)&~UTRIE2_DATA_MASK;
  699. if(nextStart<=limit) {
  700. fillBlock(newTrie->data+block, start&UTRIE2_DATA_MASK, UTRIE2_DATA_BLOCK_LENGTH,
  701. value, newTrie->initialValue, overwrite);
  702. start=nextStart;
  703. } else {
  704. fillBlock(newTrie->data+block, start&UTRIE2_DATA_MASK, limit&UTRIE2_DATA_MASK,
  705. value, newTrie->initialValue, overwrite);
  706. return;
  707. }
  708. }
  709. /* number of positions in the last, partial block */
  710. rest=limit&UTRIE2_DATA_MASK;
  711. /* round down limit to a block boundary */
  712. limit&=~UTRIE2_DATA_MASK;
  713. /* iterate over all-value blocks */
  714. if(value==newTrie->initialValue) {
  715. repeatBlock=newTrie->dataNullOffset;
  716. } else {
  717. repeatBlock=-1;
  718. }
  719. while(start<limit) {
  720. int32_t i2;
  721. UBool setRepeatBlock=false;
  722. if(value==newTrie->initialValue && isInNullBlock(newTrie, start, true)) {
  723. start+=UTRIE2_DATA_BLOCK_LENGTH; /* nothing to do */
  724. continue;
  725. }
  726. /* get index value */
  727. i2=getIndex2Block(newTrie, start, true);
  728. if(i2<0) {
  729. *pErrorCode=U_INTERNAL_PROGRAM_ERROR;
  730. return;
  731. }
  732. i2+=(start>>UTRIE2_SHIFT_2)&UTRIE2_INDEX_2_MASK;
  733. block=newTrie->index2[i2];
  734. if(isWritableBlock(newTrie, block)) {
  735. /* already allocated */
  736. if(overwrite && block>=UNEWTRIE2_DATA_0800_OFFSET) {
  737. /*
  738. * We overwrite all values, and it's not a
  739. * protected (ASCII-linear or 2-byte UTF-8) block:
  740. * replace with the repeatBlock.
  741. */
  742. setRepeatBlock=true;
  743. } else {
  744. /* !overwrite, or protected block: just write the values into this block */
  745. fillBlock(newTrie->data+block,
  746. 0, UTRIE2_DATA_BLOCK_LENGTH,
  747. value, newTrie->initialValue, overwrite);
  748. }
  749. } else if(newTrie->data[block]!=value && (overwrite || block==newTrie->dataNullOffset)) {
  750. /*
  751. * Set the repeatBlock instead of the null block or previous repeat block:
  752. *
  753. * If !isWritableBlock() then all entries in the block have the same value
  754. * because it's the null block or a range block (the repeatBlock from a previous
  755. * call to utrie2_setRange32()).
  756. * No other blocks are used multiple times before compacting.
  757. *
  758. * The null block is the only non-writable block with the initialValue because
  759. * of the repeatBlock initialization above. (If value==initialValue, then
  760. * the repeatBlock will be the null data block.)
  761. *
  762. * We set our repeatBlock if the desired value differs from the block's value,
  763. * and if we overwrite any data or if the data is all initial values
  764. * (which is the same as the block being the null block, see above).
  765. */
  766. setRepeatBlock=true;
  767. }
  768. if(setRepeatBlock) {
  769. if(repeatBlock>=0) {
  770. setIndex2Entry(newTrie, i2, repeatBlock);
  771. } else {
  772. /* create and set and fill the repeatBlock */
  773. repeatBlock=getDataBlock(newTrie, start, true);
  774. if(repeatBlock<0) {
  775. *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
  776. return;
  777. }
  778. writeBlock(newTrie->data+repeatBlock, value);
  779. }
  780. }
  781. start+=UTRIE2_DATA_BLOCK_LENGTH;
  782. }
  783. if(rest>0) {
  784. /* set partial block at [last block boundary..limit[ */
  785. block=getDataBlock(newTrie, start, true);
  786. if(block<0) {
  787. *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
  788. return;
  789. }
  790. fillBlock(newTrie->data+block, 0, rest, value, newTrie->initialValue, overwrite);
  791. }
  792. }
  793. /* compaction --------------------------------------------------------------- */
  794. static inline UBool
  795. equal_int32(const int32_t *s, const int32_t *t, int32_t length) {
  796. while(length>0 && *s==*t) {
  797. ++s;
  798. ++t;
  799. --length;
  800. }
  801. return length == 0;
  802. }
  803. static inline UBool
  804. equal_uint32(const uint32_t *s, const uint32_t *t, int32_t length) {
  805. while(length>0 && *s==*t) {
  806. ++s;
  807. ++t;
  808. --length;
  809. }
  810. return length == 0;
  811. }
  812. static int32_t
  813. findSameIndex2Block(const int32_t *idx, int32_t index2Length, int32_t otherBlock) {
  814. int32_t block;
  815. /* ensure that we do not even partially get past index2Length */
  816. index2Length-=UTRIE2_INDEX_2_BLOCK_LENGTH;
  817. for(block=0; block<=index2Length; ++block) {
  818. if(equal_int32(idx+block, idx+otherBlock, UTRIE2_INDEX_2_BLOCK_LENGTH)) {
  819. return block;
  820. }
  821. }
  822. return -1;
  823. }
  824. static int32_t
  825. findSameDataBlock(const uint32_t *data, int32_t dataLength, int32_t otherBlock, int32_t blockLength) {
  826. int32_t block;
  827. /* ensure that we do not even partially get past dataLength */
  828. dataLength-=blockLength;
  829. for(block=0; block<=dataLength; block+=UTRIE2_DATA_GRANULARITY) {
  830. if(equal_uint32(data+block, data+otherBlock, blockLength)) {
  831. return block;
  832. }
  833. }
  834. return -1;
  835. }
  836. /*
  837. * Find the start of the last range in the trie by enumerating backward.
  838. * Indexes for supplementary code points higher than this will be omitted.
  839. */
  840. static UChar32
  841. findHighStart(UNewTrie2 *trie, uint32_t highValue) {
  842. const uint32_t *data32;
  843. uint32_t value, initialValue;
  844. UChar32 c, prev;
  845. int32_t i1, i2, j, i2Block, prevI2Block, index2NullOffset, block, prevBlock, nullBlock;
  846. data32=trie->data;
  847. initialValue=trie->initialValue;
  848. index2NullOffset=trie->index2NullOffset;
  849. nullBlock=trie->dataNullOffset;
  850. /* set variables for previous range */
  851. if(highValue==initialValue) {
  852. prevI2Block=index2NullOffset;
  853. prevBlock=nullBlock;
  854. } else {
  855. prevI2Block=-1;
  856. prevBlock=-1;
  857. }
  858. prev=0x110000;
  859. /* enumerate index-2 blocks */
  860. i1=UNEWTRIE2_INDEX_1_LENGTH;
  861. c=prev;
  862. while(c>0) {
  863. i2Block=trie->index1[--i1];
  864. if(i2Block==prevI2Block) {
  865. /* the index-2 block is the same as the previous one, and filled with highValue */
  866. c-=UTRIE2_CP_PER_INDEX_1_ENTRY;
  867. continue;
  868. }
  869. prevI2Block=i2Block;
  870. if(i2Block==index2NullOffset) {
  871. /* this is the null index-2 block */
  872. if(highValue!=initialValue) {
  873. return c;
  874. }
  875. c-=UTRIE2_CP_PER_INDEX_1_ENTRY;
  876. } else {
  877. /* enumerate data blocks for one index-2 block */
  878. for(i2=UTRIE2_INDEX_2_BLOCK_LENGTH; i2>0;) {
  879. block=trie->index2[i2Block+ --i2];
  880. if(block==prevBlock) {
  881. /* the block is the same as the previous one, and filled with highValue */
  882. c-=UTRIE2_DATA_BLOCK_LENGTH;
  883. continue;
  884. }
  885. prevBlock=block;
  886. if(block==nullBlock) {
  887. /* this is the null data block */
  888. if(highValue!=initialValue) {
  889. return c;
  890. }
  891. c-=UTRIE2_DATA_BLOCK_LENGTH;
  892. } else {
  893. for(j=UTRIE2_DATA_BLOCK_LENGTH; j>0;) {
  894. value=data32[block+ --j];
  895. if(value!=highValue) {
  896. return c;
  897. }
  898. --c;
  899. }
  900. }
  901. }
  902. }
  903. }
  904. /* deliver last range */
  905. return 0;
  906. }
  907. /*
  908. * Compact a build-time trie.
  909. *
  910. * The compaction
  911. * - removes blocks that are identical with earlier ones
  912. * - overlaps adjacent blocks as much as possible (if overlap==true)
  913. * - moves blocks in steps of the data granularity
  914. * - moves and overlaps blocks that overlap with multiple values in the overlap region
  915. *
  916. * It does not
  917. * - try to move and overlap blocks that are not already adjacent
  918. */
  919. static void
  920. compactData(UNewTrie2 *trie) {
  921. #ifdef UTRIE2_DEBUG
  922. int32_t countSame=0, sumOverlaps=0;
  923. #endif
  924. int32_t start, newStart, movedStart;
  925. int32_t blockLength, overlap;
  926. int32_t i, mapIndex, blockCount;
  927. /* do not compact linear-ASCII data */
  928. newStart=UTRIE2_DATA_START_OFFSET;
  929. for(start=0, i=0; start<newStart; start+=UTRIE2_DATA_BLOCK_LENGTH, ++i) {
  930. trie->map[i]=start;
  931. }
  932. /*
  933. * Start with a block length of 64 for 2-byte UTF-8,
  934. * then switch to UTRIE2_DATA_BLOCK_LENGTH.
  935. */
  936. blockLength=64;
  937. blockCount=blockLength>>UTRIE2_SHIFT_2;
  938. for(start=newStart; start<trie->dataLength;) {
  939. /*
  940. * start: index of first entry of current block
  941. * newStart: index where the current block is to be moved
  942. * (right after current end of already-compacted data)
  943. */
  944. if(start==UNEWTRIE2_DATA_0800_OFFSET) {
  945. blockLength=UTRIE2_DATA_BLOCK_LENGTH;
  946. blockCount=1;
  947. }
  948. /* skip blocks that are not used */
  949. if(trie->map[start>>UTRIE2_SHIFT_2]<=0) {
  950. /* advance start to the next block */
  951. start+=blockLength;
  952. /* leave newStart with the previous block! */
  953. continue;
  954. }
  955. /* search for an identical block */
  956. if( (movedStart=findSameDataBlock(trie->data, newStart, start, blockLength))
  957. >=0
  958. ) {
  959. #ifdef UTRIE2_DEBUG
  960. ++countSame;
  961. #endif
  962. /* found an identical block, set the other block's index value for the current block */
  963. for(i=blockCount, mapIndex=start>>UTRIE2_SHIFT_2; i>0; --i) {
  964. trie->map[mapIndex++]=movedStart;
  965. movedStart+=UTRIE2_DATA_BLOCK_LENGTH;
  966. }
  967. /* advance start to the next block */
  968. start+=blockLength;
  969. /* leave newStart with the previous block! */
  970. continue;
  971. }
  972. /* see if the beginning of this block can be overlapped with the end of the previous block */
  973. /* look for maximum overlap (modulo granularity) with the previous, adjacent block */
  974. for(overlap=blockLength-UTRIE2_DATA_GRANULARITY;
  975. overlap>0 && !equal_uint32(trie->data+(newStart-overlap), trie->data+start, overlap);
  976. overlap-=UTRIE2_DATA_GRANULARITY) {}
  977. #ifdef UTRIE2_DEBUG
  978. sumOverlaps+=overlap;
  979. #endif
  980. if(overlap>0 || newStart<start) {
  981. /* some overlap, or just move the whole block */
  982. movedStart=newStart-overlap;
  983. for(i=blockCount, mapIndex=start>>UTRIE2_SHIFT_2; i>0; --i) {
  984. trie->map[mapIndex++]=movedStart;
  985. movedStart+=UTRIE2_DATA_BLOCK_LENGTH;
  986. }
  987. /* move the non-overlapping indexes to their new positions */
  988. start+=overlap;
  989. for(i=blockLength-overlap; i>0; --i) {
  990. trie->data[newStart++]=trie->data[start++];
  991. }
  992. } else /* no overlap && newStart==start */ {
  993. for(i=blockCount, mapIndex=start>>UTRIE2_SHIFT_2; i>0; --i) {
  994. trie->map[mapIndex++]=start;
  995. start+=UTRIE2_DATA_BLOCK_LENGTH;
  996. }
  997. newStart=start;
  998. }
  999. }
  1000. /* now adjust the index-2 table */
  1001. for(i=0; i<trie->index2Length; ++i) {
  1002. if(i==UNEWTRIE2_INDEX_GAP_OFFSET) {
  1003. /* Gap indexes are invalid (-1). Skip over the gap. */
  1004. i+=UNEWTRIE2_INDEX_GAP_LENGTH;
  1005. }
  1006. trie->index2[i]=trie->map[trie->index2[i]>>UTRIE2_SHIFT_2];
  1007. }
  1008. trie->dataNullOffset=trie->map[trie->dataNullOffset>>UTRIE2_SHIFT_2];
  1009. /* ensure dataLength alignment */
  1010. while((newStart&(UTRIE2_DATA_GRANULARITY-1))!=0) {
  1011. trie->data[newStart++]=trie->initialValue;
  1012. }
  1013. #ifdef UTRIE2_DEBUG
  1014. /* we saved some space */
  1015. printf("compacting UTrie2: count of 32-bit data words %lu->%lu countSame=%ld sumOverlaps=%ld\n",
  1016. (long)trie->dataLength, (long)newStart, (long)countSame, (long)sumOverlaps);
  1017. #endif
  1018. trie->dataLength=newStart;
  1019. }
  1020. static void
  1021. compactIndex2(UNewTrie2 *trie) {
  1022. int32_t i, start, newStart, movedStart, overlap;
  1023. /* do not compact linear-BMP index-2 blocks */
  1024. newStart=UTRIE2_INDEX_2_BMP_LENGTH;
  1025. for(start=0, i=0; start<newStart; start+=UTRIE2_INDEX_2_BLOCK_LENGTH, ++i) {
  1026. trie->map[i]=start;
  1027. }
  1028. /* Reduce the index table gap to what will be needed at runtime. */
  1029. newStart+=UTRIE2_UTF8_2B_INDEX_2_LENGTH+((trie->highStart-0x10000)>>UTRIE2_SHIFT_1);
  1030. for(start=UNEWTRIE2_INDEX_2_NULL_OFFSET; start<trie->index2Length;) {
  1031. /*
  1032. * start: index of first entry of current block
  1033. * newStart: index where the current block is to be moved
  1034. * (right after current end of already-compacted data)
  1035. */
  1036. /* search for an identical block */
  1037. if( (movedStart=findSameIndex2Block(trie->index2, newStart, start))
  1038. >=0
  1039. ) {
  1040. /* found an identical block, set the other block's index value for the current block */
  1041. trie->map[start>>UTRIE2_SHIFT_1_2]=movedStart;
  1042. /* advance start to the next block */
  1043. start+=UTRIE2_INDEX_2_BLOCK_LENGTH;
  1044. /* leave newStart with the previous block! */
  1045. continue;
  1046. }
  1047. /* see if the beginning of this block can be overlapped with the end of the previous block */
  1048. /* look for maximum overlap with the previous, adjacent block */
  1049. for(overlap=UTRIE2_INDEX_2_BLOCK_LENGTH-1;
  1050. overlap>0 && !equal_int32(trie->index2+(newStart-overlap), trie->index2+start, overlap);
  1051. --overlap) {}
  1052. if(overlap>0 || newStart<start) {
  1053. /* some overlap, or just move the whole block */
  1054. trie->map[start>>UTRIE2_SHIFT_1_2]=newStart-overlap;
  1055. /* move the non-overlapping indexes to their new positions */
  1056. start+=overlap;
  1057. for(i=UTRIE2_INDEX_2_BLOCK_LENGTH-overlap; i>0; --i) {
  1058. trie->index2[newStart++]=trie->index2[start++];
  1059. }
  1060. } else /* no overlap && newStart==start */ {
  1061. trie->map[start>>UTRIE2_SHIFT_1_2]=start;
  1062. start+=UTRIE2_INDEX_2_BLOCK_LENGTH;
  1063. newStart=start;
  1064. }
  1065. }
  1066. /* now adjust the index-1 table */
  1067. for(i=0; i<UNEWTRIE2_INDEX_1_LENGTH; ++i) {
  1068. trie->index1[i]=trie->map[trie->index1[i]>>UTRIE2_SHIFT_1_2];
  1069. }
  1070. trie->index2NullOffset=trie->map[trie->index2NullOffset>>UTRIE2_SHIFT_1_2];
  1071. /*
  1072. * Ensure data table alignment:
  1073. * Needs to be granularity-aligned for 16-bit trie
  1074. * (so that dataMove will be down-shiftable),
  1075. * and 2-aligned for uint32_t data.
  1076. */
  1077. while((newStart&((UTRIE2_DATA_GRANULARITY-1)|1))!=0) {
  1078. /* Arbitrary value: 0x3fffc not possible for real data. */
  1079. trie->index2[newStart++] = static_cast<int32_t>(0xffff) << UTRIE2_INDEX_SHIFT;
  1080. }
  1081. #ifdef UTRIE2_DEBUG
  1082. /* we saved some space */
  1083. printf("compacting UTrie2: count of 16-bit index words %lu->%lu\n",
  1084. (long)trie->index2Length, (long)newStart);
  1085. #endif
  1086. trie->index2Length=newStart;
  1087. }
  1088. static void
  1089. compactTrie(UTrie2 *trie, UErrorCode *pErrorCode) {
  1090. UNewTrie2 *newTrie;
  1091. UChar32 highStart, suppHighStart;
  1092. uint32_t highValue;
  1093. newTrie=trie->newTrie;
  1094. /* find highStart and round it up */
  1095. highValue=utrie2_get32(trie, 0x10ffff);
  1096. highStart=findHighStart(newTrie, highValue);
  1097. highStart=(highStart+(UTRIE2_CP_PER_INDEX_1_ENTRY-1))&~(UTRIE2_CP_PER_INDEX_1_ENTRY-1);
  1098. if(highStart==0x110000) {
  1099. highValue=trie->errorValue;
  1100. }
  1101. /*
  1102. * Set trie->highStart only after utrie2_get32(trie, highStart).
  1103. * Otherwise utrie2_get32(trie, highStart) would try to read the highValue.
  1104. */
  1105. trie->highStart=newTrie->highStart=highStart;
  1106. #ifdef UTRIE2_DEBUG
  1107. printf("UTrie2: highStart U+%06lx highValue 0x%lx initialValue 0x%lx\n",
  1108. (long)highStart, (long)highValue, (long)trie->initialValue);
  1109. #endif
  1110. if(highStart<0x110000) {
  1111. /* Blank out [highStart..10ffff] to release associated data blocks. */
  1112. suppHighStart= highStart<=0x10000 ? 0x10000 : highStart;
  1113. utrie2_setRange32(trie, suppHighStart, 0x10ffff, trie->initialValue, true, pErrorCode);
  1114. if(U_FAILURE(*pErrorCode)) {
  1115. return;
  1116. }
  1117. }
  1118. compactData(newTrie);
  1119. if(highStart>0x10000) {
  1120. compactIndex2(newTrie);
  1121. #ifdef UTRIE2_DEBUG
  1122. } else {
  1123. printf("UTrie2: highStart U+%04lx count of 16-bit index words %lu->%lu\n",
  1124. (long)highStart, (long)trie->newTrie->index2Length, (long)UTRIE2_INDEX_1_OFFSET);
  1125. #endif
  1126. }
  1127. /*
  1128. * Store the highValue in the data array and round up the dataLength.
  1129. * Must be done after compactData() because that assumes that dataLength
  1130. * is a multiple of UTRIE2_DATA_BLOCK_LENGTH.
  1131. */
  1132. newTrie->data[newTrie->dataLength++]=highValue;
  1133. while((newTrie->dataLength&(UTRIE2_DATA_GRANULARITY-1))!=0) {
  1134. newTrie->data[newTrie->dataLength++]=trie->initialValue;
  1135. }
  1136. newTrie->isCompacted=true;
  1137. }
  1138. /* serialization ------------------------------------------------------------ */
  1139. /**
  1140. * Maximum length of the runtime index array.
  1141. * Limited by its own 16-bit index values, and by uint16_t UTrie2Header.indexLength.
  1142. * (The actual maximum length is lower,
  1143. * (0x110000>>UTRIE2_SHIFT_2)+UTRIE2_UTF8_2B_INDEX_2_LENGTH+UTRIE2_MAX_INDEX_1_LENGTH.)
  1144. */
  1145. #define UTRIE2_MAX_INDEX_LENGTH 0xffff
  1146. /**
  1147. * Maximum length of the runtime data array.
  1148. * Limited by 16-bit index values that are left-shifted by UTRIE2_INDEX_SHIFT,
  1149. * and by uint16_t UTrie2Header.shiftedDataLength.
  1150. */
  1151. #define UTRIE2_MAX_DATA_LENGTH (0xffff<<UTRIE2_INDEX_SHIFT)
  1152. /* Compact and internally serialize the trie. */
  1153. U_CAPI void U_EXPORT2
  1154. utrie2_freeze(UTrie2 *trie, UTrie2ValueBits valueBits, UErrorCode *pErrorCode) {
  1155. UNewTrie2 *newTrie;
  1156. UTrie2Header *header;
  1157. uint32_t *p;
  1158. uint16_t *dest16;
  1159. int32_t i, length;
  1160. int32_t allIndexesLength;
  1161. int32_t dataMove; /* >0 if the data is moved to the end of the index array */
  1162. UChar32 highStart;
  1163. /* argument check */
  1164. if(U_FAILURE(*pErrorCode)) {
  1165. return;
  1166. }
  1167. if( trie==nullptr ||
  1168. valueBits<0 || UTRIE2_COUNT_VALUE_BITS<=valueBits
  1169. ) {
  1170. *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
  1171. return;
  1172. }
  1173. newTrie=trie->newTrie;
  1174. if(newTrie==nullptr) {
  1175. /* already frozen */
  1176. UTrie2ValueBits frozenValueBits=
  1177. trie->data16!=nullptr ? UTRIE2_16_VALUE_BITS : UTRIE2_32_VALUE_BITS;
  1178. if(valueBits!=frozenValueBits) {
  1179. *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
  1180. }
  1181. return;
  1182. }
  1183. /* compact if necessary */
  1184. if(!newTrie->isCompacted) {
  1185. compactTrie(trie, pErrorCode);
  1186. if(U_FAILURE(*pErrorCode)) {
  1187. return;
  1188. }
  1189. }
  1190. highStart=trie->highStart;
  1191. if(highStart<=0x10000) {
  1192. allIndexesLength=UTRIE2_INDEX_1_OFFSET;
  1193. } else {
  1194. allIndexesLength=newTrie->index2Length;
  1195. }
  1196. if(valueBits==UTRIE2_16_VALUE_BITS) {
  1197. dataMove=allIndexesLength;
  1198. } else {
  1199. dataMove=0;
  1200. }
  1201. /* are indexLength and dataLength within limits? */
  1202. if( /* for unshifted indexLength */
  1203. allIndexesLength>UTRIE2_MAX_INDEX_LENGTH ||
  1204. /* for unshifted dataNullOffset */
  1205. (dataMove+newTrie->dataNullOffset)>0xffff ||
  1206. /* for unshifted 2-byte UTF-8 index-2 values */
  1207. (dataMove+UNEWTRIE2_DATA_0800_OFFSET)>0xffff ||
  1208. /* for shiftedDataLength */
  1209. (dataMove+newTrie->dataLength)>UTRIE2_MAX_DATA_LENGTH
  1210. ) {
  1211. *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
  1212. return;
  1213. }
  1214. /* calculate the total serialized length */
  1215. length=sizeof(UTrie2Header)+allIndexesLength*2;
  1216. if(valueBits==UTRIE2_16_VALUE_BITS) {
  1217. length+=newTrie->dataLength*2;
  1218. } else {
  1219. length+=newTrie->dataLength*4;
  1220. }
  1221. trie->memory=uprv_malloc(length);
  1222. if(trie->memory==nullptr) {
  1223. *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
  1224. return;
  1225. }
  1226. trie->length=length;
  1227. trie->isMemoryOwned=true;
  1228. trie->indexLength=allIndexesLength;
  1229. trie->dataLength=newTrie->dataLength;
  1230. if(highStart<=0x10000) {
  1231. trie->index2NullOffset=0xffff;
  1232. } else {
  1233. trie->index2NullOffset=static_cast<uint16_t>(UTRIE2_INDEX_2_OFFSET+newTrie->index2NullOffset);
  1234. }
  1235. trie->dataNullOffset=(uint16_t)(dataMove+newTrie->dataNullOffset);
  1236. trie->highValueIndex=dataMove+trie->dataLength-UTRIE2_DATA_GRANULARITY;
  1237. /* set the header fields */
  1238. header=(UTrie2Header *)trie->memory;
  1239. header->signature=UTRIE2_SIG; /* "Tri2" */
  1240. header->options=(uint16_t)valueBits;
  1241. header->indexLength=(uint16_t)trie->indexLength;
  1242. header->shiftedDataLength=(uint16_t)(trie->dataLength>>UTRIE2_INDEX_SHIFT);
  1243. header->index2NullOffset=trie->index2NullOffset;
  1244. header->dataNullOffset=trie->dataNullOffset;
  1245. header->shiftedHighStart=(uint16_t)(highStart>>UTRIE2_SHIFT_1);
  1246. /* fill the index and data arrays */
  1247. dest16=(uint16_t *)(header+1);
  1248. trie->index=dest16;
  1249. /* write the index-2 array values shifted right by UTRIE2_INDEX_SHIFT, after adding dataMove */
  1250. p=(uint32_t *)newTrie->index2;
  1251. for(i=UTRIE2_INDEX_2_BMP_LENGTH; i>0; --i) {
  1252. *dest16++=(uint16_t)((dataMove + *p++)>>UTRIE2_INDEX_SHIFT);
  1253. }
  1254. /* write UTF-8 2-byte index-2 values, not right-shifted */
  1255. for(i=0; i<(0xc2-0xc0); ++i) { /* C0..C1 */
  1256. *dest16++=(uint16_t)(dataMove+UTRIE2_BAD_UTF8_DATA_OFFSET);
  1257. }
  1258. for(; i<(0xe0-0xc0); ++i) { /* C2..DF */
  1259. *dest16++=(uint16_t)(dataMove+newTrie->index2[i<<(6-UTRIE2_SHIFT_2)]);
  1260. }
  1261. if(highStart>0x10000) {
  1262. int32_t index1Length=(highStart-0x10000)>>UTRIE2_SHIFT_1;
  1263. int32_t index2Offset=UTRIE2_INDEX_2_BMP_LENGTH+UTRIE2_UTF8_2B_INDEX_2_LENGTH+index1Length;
  1264. /* write 16-bit index-1 values for supplementary code points */
  1265. p=(uint32_t *)newTrie->index1+UTRIE2_OMITTED_BMP_INDEX_1_LENGTH;
  1266. for(i=index1Length; i>0; --i) {
  1267. *dest16++=(uint16_t)(UTRIE2_INDEX_2_OFFSET + *p++);
  1268. }
  1269. /*
  1270. * write the index-2 array values for supplementary code points,
  1271. * shifted right by UTRIE2_INDEX_SHIFT, after adding dataMove
  1272. */
  1273. p=(uint32_t *)newTrie->index2+index2Offset;
  1274. for(i=newTrie->index2Length-index2Offset; i>0; --i) {
  1275. *dest16++=(uint16_t)((dataMove + *p++)>>UTRIE2_INDEX_SHIFT);
  1276. }
  1277. }
  1278. /* write the 16/32-bit data array */
  1279. switch(valueBits) {
  1280. case UTRIE2_16_VALUE_BITS:
  1281. /* write 16-bit data values */
  1282. trie->data16=dest16;
  1283. trie->data32=nullptr;
  1284. p=newTrie->data;
  1285. for(i=newTrie->dataLength; i>0; --i) {
  1286. *dest16++=(uint16_t)*p++;
  1287. }
  1288. break;
  1289. case UTRIE2_32_VALUE_BITS:
  1290. /* write 32-bit data values */
  1291. trie->data16=nullptr;
  1292. trie->data32=(uint32_t *)dest16;
  1293. uprv_memcpy(dest16, newTrie->data, (size_t)newTrie->dataLength*4);
  1294. break;
  1295. default:
  1296. *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
  1297. return;
  1298. }
  1299. #ifdef UTRIE2_DEBUG
  1300. utrie2_printLengths(trie, "");
  1301. #endif
  1302. #ifdef UCPTRIE_DEBUG
  1303. umutablecptrie_setName(newTrie->t3, trie->name);
  1304. ucptrie_close(
  1305. umutablecptrie_buildImmutable(
  1306. newTrie->t3, UCPTRIE_TYPE_FAST, (UCPTrieValueWidth)valueBits, pErrorCode));
  1307. #endif
  1308. /* Delete the UNewTrie2. */
  1309. uprv_free(newTrie->data);
  1310. uprv_free(newTrie);
  1311. trie->newTrie=nullptr;
  1312. }