1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 ******************************************************************************
6 * Copyright (C) 2001-2012, International Business Machines
7 * Corporation and others. All Rights Reserved.
9 ******************************************************************************
10 * file name: utrie.cpp
12 * tab size: 8 (not used)
15 * created on: 2001oct20
16 * created by: Markus W. Scherer
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).
27 #include "unicode/utypes.h"
31 /* miscellaneous ------------------------------------------------------------ */
34 #define ABS(x) ((x)>=0 ? (x) : -(x))
37 equal_uint32(const uint32_t *s, const uint32_t *t, int32_t length) {
38 while(length>0 && *s==*t) {
43 return (UBool)(length==0);
46 /* Building a trie ----------------------------------------------------------*/
48 U_CAPI UNewTrie * U_EXPORT2
49 utrie_open(UNewTrie *fillIn,
50 uint32_t *aliasData, int32_t maxDataLength,
51 uint32_t initialValue, uint32_t leadUnitValue,
56 if( maxDataLength<UTRIE_DATA_BLOCK_LENGTH ||
57 (latin1Linear && maxDataLength<1024)
65 trie=(UNewTrie *)uprv_malloc(sizeof(UNewTrie));
70 uprv_memset(trie, 0, sizeof(UNewTrie));
71 trie->isAllocated= (UBool)(fillIn==NULL);
75 trie->isDataAllocated=FALSE;
77 trie->data=(uint32_t *)uprv_malloc(maxDataLength*4);
78 if(trie->data==NULL) {
82 trie->isDataAllocated=TRUE;
85 /* preallocate and reset the first data block (block index 0) */
86 j=UTRIE_DATA_BLOCK_LENGTH;
89 /* preallocate and reset the first block (number 0) and Latin-1 (U+0000..U+00ff) after that */
90 /* made sure above that maxDataLength>=1024 */
92 /* set indexes to point to consecutive data blocks */
95 /* do this at least for trie->index[0] even if that block is only partly used for Latin-1 */
97 j+=UTRIE_DATA_BLOCK_LENGTH;
98 } while(i<(256>>UTRIE_SHIFT));
101 /* reset the initially allocated blocks to the initial value */
104 trie->data[--j]=initialValue;
107 trie->leadUnitValue=leadUnitValue;
108 trie->indexLength=UTRIE_MAX_INDEX_LENGTH;
109 trie->dataCapacity=maxDataLength;
110 trie->isLatin1Linear=latin1Linear;
111 trie->isCompacted=FALSE;
115 U_CAPI UNewTrie * U_EXPORT2
116 utrie_clone(UNewTrie *fillIn, const UNewTrie *other, uint32_t *aliasData, int32_t aliasDataCapacity) {
118 UBool isDataAllocated;
120 /* do not clone if other is not valid or already compacted */
121 if(other==NULL || other->data==NULL || other->isCompacted) {
126 if(aliasData!=NULL && aliasDataCapacity>=other->dataCapacity) {
127 isDataAllocated=FALSE;
129 aliasDataCapacity=other->dataCapacity;
130 aliasData=(uint32_t *)uprv_malloc(other->dataCapacity*4);
131 if(aliasData==NULL) {
134 isDataAllocated=TRUE;
137 trie=utrie_open(fillIn, aliasData, aliasDataCapacity,
138 other->data[0], other->leadUnitValue,
139 other->isLatin1Linear);
141 uprv_free(aliasData);
143 uprv_memcpy(trie->index, other->index, sizeof(trie->index));
144 uprv_memcpy(trie->data, other->data, (size_t)other->dataLength*4);
145 trie->dataLength=other->dataLength;
146 trie->isDataAllocated=isDataAllocated;
152 U_CAPI void U_EXPORT2
153 utrie_close(UNewTrie *trie) {
155 if(trie->isDataAllocated) {
156 uprv_free(trie->data);
159 if(trie->isAllocated) {
165 U_CAPI uint32_t * U_EXPORT2
166 utrie_getData(UNewTrie *trie, int32_t *pLength) {
167 if(trie==NULL || pLength==NULL) {
171 *pLength=trie->dataLength;
176 utrie_allocDataBlock(UNewTrie *trie) {
177 int32_t newBlock, newTop;
179 newBlock=trie->dataLength;
180 newTop=newBlock+UTRIE_DATA_BLOCK_LENGTH;
181 if(newTop>trie->dataCapacity) {
182 /* out of memory in the data array */
185 trie->dataLength=newTop;
190 * No error checking for illegal arguments.
192 * @return -1 if no new data block available (out of memory in data array)
196 utrie_getDataBlock(UNewTrie *trie, UChar32 c) {
197 int32_t indexValue, newBlock;
200 indexValue=trie->index[c];
205 /* allocate a new data block */
206 newBlock=utrie_allocDataBlock(trie);
208 /* out of memory in the data array */
211 trie->index[c]=newBlock;
213 /* copy-on-write for a block from a setRange() */
214 uprv_memcpy(trie->data+newBlock, trie->data-indexValue, 4*UTRIE_DATA_BLOCK_LENGTH);
219 * @return TRUE if the value was successfully set
221 U_CAPI UBool U_EXPORT2
222 utrie_set32(UNewTrie *trie, UChar32 c, uint32_t value) {
225 /* valid, uncompacted trie and valid c? */
226 if(trie==NULL || trie->isCompacted || (uint32_t)c>0x10ffff) {
230 block=utrie_getDataBlock(trie, c);
235 trie->data[block+(c&UTRIE_MASK)]=value;
239 U_CAPI uint32_t U_EXPORT2
240 utrie_get32(UNewTrie *trie, UChar32 c, UBool *pInBlockZero) {
243 /* valid, uncompacted trie and valid c? */
244 if(trie==NULL || trie->isCompacted || (uint32_t)c>0x10ffff) {
245 if(pInBlockZero!=NULL) {
251 block=trie->index[c>>UTRIE_SHIFT];
252 if(pInBlockZero!=NULL) {
253 *pInBlockZero= (UBool)(block==0);
256 return trie->data[ABS(block)+(c&UTRIE_MASK)];
263 utrie_fillBlock(uint32_t *block, UChar32 start, UChar32 limit,
264 uint32_t value, uint32_t initialValue, UBool overwrite) {
270 while(block<pLimit) {
274 while(block<pLimit) {
275 if(*block==initialValue) {
283 U_CAPI UBool U_EXPORT2
284 utrie_setRange32(UNewTrie *trie, UChar32 start, UChar32 limit, uint32_t value, UBool overwrite) {
286 * repeat value in [start..limit[
287 * mark index values for repeat-data blocks by setting bit 31 of the index values
288 * fill around existing values if any, if(overwrite)
290 uint32_t initialValue;
291 int32_t block, rest, repeatBlock;
293 /* valid, uncompacted trie and valid indexes? */
294 if( trie==NULL || trie->isCompacted ||
295 (uint32_t)start>0x10ffff || (uint32_t)limit>0x110000 || start>limit
300 return TRUE; /* nothing to do */
303 initialValue=trie->data[0];
304 if(start&UTRIE_MASK) {
307 /* set partial block at [start..following block boundary[ */
308 block=utrie_getDataBlock(trie, start);
313 nextStart=(start+UTRIE_DATA_BLOCK_LENGTH)&~UTRIE_MASK;
314 if(nextStart<=limit) {
315 utrie_fillBlock(trie->data+block, start&UTRIE_MASK, UTRIE_DATA_BLOCK_LENGTH,
316 value, initialValue, overwrite);
319 utrie_fillBlock(trie->data+block, start&UTRIE_MASK, limit&UTRIE_MASK,
320 value, initialValue, overwrite);
325 /* number of positions in the last, partial block */
326 rest=limit&UTRIE_MASK;
328 /* round down limit to a block boundary */
331 /* iterate over all-value blocks */
332 if(value==initialValue) {
338 /* get index value */
339 block=trie->index[start>>UTRIE_SHIFT];
341 /* already allocated, fill in value */
342 utrie_fillBlock(trie->data+block, 0, UTRIE_DATA_BLOCK_LENGTH, value, initialValue, overwrite);
343 } else if(trie->data[-block]!=value && (block==0 || overwrite)) {
344 /* set the repeatBlock instead of the current block 0 or range block */
346 trie->index[start>>UTRIE_SHIFT]=-repeatBlock;
348 /* create and set and fill the repeatBlock */
349 repeatBlock=utrie_getDataBlock(trie, start);
354 /* set the negative block number to indicate that it is a repeat block */
355 trie->index[start>>UTRIE_SHIFT]=-repeatBlock;
356 utrie_fillBlock(trie->data+repeatBlock, 0, UTRIE_DATA_BLOCK_LENGTH, value, initialValue, TRUE);
360 start+=UTRIE_DATA_BLOCK_LENGTH;
364 /* set partial block at [last block boundary..limit[ */
365 block=utrie_getDataBlock(trie, start);
370 utrie_fillBlock(trie->data+block, 0, rest, value, initialValue, overwrite);
377 _findSameIndexBlock(const int32_t *idx, int32_t indexLength,
378 int32_t otherBlock) {
381 for(block=UTRIE_BMP_INDEX_LENGTH; block<indexLength; block+=UTRIE_SURROGATE_BLOCK_COUNT) {
382 for(i=0; i<UTRIE_SURROGATE_BLOCK_COUNT; ++i) {
383 if(idx[block+i]!=idx[otherBlock+i]) {
387 if(i==UTRIE_SURROGATE_BLOCK_COUNT) {
395 * Fold the normalization data for supplementary code points into
396 * a compact area on top of the BMP-part of the trie index,
397 * with the lead surrogates indexing this compact area.
399 * Duplicate the index values for lead surrogates:
400 * From inside the BMP area, where some may be overridden with folded values,
401 * to just after the BMP area, where they can be retrieved for
402 * code point lookups.
405 utrie_fold(UNewTrie *trie, UNewTrieGetFoldedValue *getFoldedValue, UErrorCode *pErrorCode) {
406 int32_t leadIndexes[UTRIE_SURROGATE_BLOCK_COUNT];
410 int32_t indexLength, block;
412 int countLeadCUWithData=0;
417 /* copy the lead surrogate indexes into a temporary array */
418 uprv_memcpy(leadIndexes, idx+(0xd800>>UTRIE_SHIFT), 4*UTRIE_SURROGATE_BLOCK_COUNT);
421 * set all values for lead surrogate code *units* to leadUnitValue
422 * so that, by default, runtime lookups will find no data for associated
423 * supplementary code points, unless there is data for such code points
424 * which will result in a non-zero folding value below that is set for
425 * the respective lead units
427 * the above saved the indexes for surrogate code *points*
428 * fill the indexes with simplified code from utrie_setRange32()
430 if(trie->leadUnitValue==trie->data[0]) {
431 block=0; /* leadUnitValue==initialValue, use all-initial-value block */
433 /* create and fill the repeatBlock */
434 block=utrie_allocDataBlock(trie);
436 /* data table overflow */
437 *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
440 utrie_fillBlock(trie->data+block, 0, UTRIE_DATA_BLOCK_LENGTH, trie->leadUnitValue, trie->data[0], TRUE);
441 block=-block; /* negative block number to indicate that it is a repeat block */
443 for(c=(0xd800>>UTRIE_SHIFT); c<(0xdc00>>UTRIE_SHIFT); ++c) {
444 trie->index[c]=block;
448 * Fold significant index values into the area just after the BMP indexes.
449 * In case the first lead surrogate has significant data,
450 * its index block must be used first (in which case the folding is a no-op).
451 * Later all folded index blocks are moved up one to insert the copied
452 * lead surrogate indexes.
454 indexLength=UTRIE_BMP_INDEX_LENGTH;
456 /* search for any index (stage 1) entries for supplementary code points */
457 for(c=0x10000; c<0x110000;) {
458 if(idx[c>>UTRIE_SHIFT]!=0) {
459 /* there is data, treat the full block for a lead surrogate */
463 ++countLeadCUWithData;
464 /* printf("supplementary data for lead surrogate U+%04lx\n", (long)(0xd7c0+(c>>10))); */
467 /* is there an identical index block? */
468 block=_findSameIndexBlock(idx, indexLength, c>>UTRIE_SHIFT);
471 * get a folded value for [c..c+0x400[ and,
472 * if different from the value for the lead surrogate code point,
473 * set it for the lead surrogate code unit
475 value=getFoldedValue(trie, c, block+UTRIE_SURROGATE_BLOCK_COUNT);
476 if(value!=utrie_get32(trie, U16_LEAD(c), NULL)) {
477 if(!utrie_set32(trie, U16_LEAD(c), value)) {
478 /* data table overflow */
479 *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
483 /* if we did not find an identical index block... */
484 if(block==indexLength) {
485 /* move the actual index (stage 1) entries from the supplementary position to the new one */
486 uprv_memmove(idx+indexLength,
487 idx+(c>>UTRIE_SHIFT),
488 4*UTRIE_SURROGATE_BLOCK_COUNT);
489 indexLength+=UTRIE_SURROGATE_BLOCK_COUNT;
494 c+=UTRIE_DATA_BLOCK_LENGTH;
498 if(countLeadCUWithData>0) {
499 printf("supplementary data for %d lead surrogates\n", countLeadCUWithData);
504 * index array overflow?
505 * This is to guarantee that a folding offset is of the form
506 * UTRIE_BMP_INDEX_LENGTH+n*UTRIE_SURROGATE_BLOCK_COUNT with n=0..1023.
507 * If the index is too large, then n>=1024 and more than 10 bits are necessary.
509 * In fact, it can only ever become n==1024 with completely unfoldable data and
510 * the additional block of duplicated values for lead surrogates.
512 if(indexLength>=UTRIE_MAX_INDEX_LENGTH) {
513 *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
518 * make space for the lead surrogate index block and
519 * insert it between the BMP indexes and the folded ones
521 uprv_memmove(idx+UTRIE_BMP_INDEX_LENGTH+UTRIE_SURROGATE_BLOCK_COUNT,
522 idx+UTRIE_BMP_INDEX_LENGTH,
523 4*(indexLength-UTRIE_BMP_INDEX_LENGTH));
524 uprv_memcpy(idx+UTRIE_BMP_INDEX_LENGTH,
526 4*UTRIE_SURROGATE_BLOCK_COUNT);
527 indexLength+=UTRIE_SURROGATE_BLOCK_COUNT;
530 printf("trie index count: BMP %ld all Unicode %ld folded %ld\n",
531 UTRIE_BMP_INDEX_LENGTH, (long)UTRIE_MAX_INDEX_LENGTH, indexLength);
534 trie->indexLength=indexLength;
538 * Set a value in the trie index map to indicate which data block
539 * is referenced and which one is not.
540 * utrie_compact() will remove data blocks that are not used at all.
543 * - -1 if it is not used
546 _findUnusedBlocks(UNewTrie *trie) {
549 /* fill the entire map with "not used" */
550 uprv_memset(trie->map, 0xff, (UTRIE_MAX_BUILD_TIME_DATA_LENGTH>>UTRIE_SHIFT)*4);
552 /* mark each block that _is_ used with 0 */
553 for(i=0; i<trie->indexLength; ++i) {
554 trie->map[ABS(trie->index[i])>>UTRIE_SHIFT]=0;
557 /* never move the all-initial-value block 0 */
562 _findSameDataBlock(const uint32_t *data, int32_t dataLength,
563 int32_t otherBlock, int32_t step) {
566 /* ensure that we do not even partially get past dataLength */
567 dataLength-=UTRIE_DATA_BLOCK_LENGTH;
569 for(block=0; block<=dataLength; block+=step) {
570 if(equal_uint32(data+block, data+otherBlock, UTRIE_DATA_BLOCK_LENGTH)) {
578 * Compact a folded build-time trie.
581 * - removes blocks that are identical with earlier ones
582 * - overlaps adjacent blocks as much as possible (if overlap==TRUE)
583 * - moves blocks in steps of the data granularity
584 * - moves and overlaps blocks that overlap with multiple values in the overlap region
587 * - try to move and overlap blocks that are not already adjacent
590 utrie_compact(UNewTrie *trie, UBool overlap, UErrorCode *pErrorCode) {
591 int32_t i, start, newStart, overlapStart;
593 if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
597 /* valid, uncompacted trie? */
599 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
602 if(trie->isCompacted) {
603 return; /* nothing left to do */
608 /* initialize the index map with "block is used/unused" flags */
609 _findUnusedBlocks(trie);
611 /* if Latin-1 is preallocated and linear, then do not compact Latin-1 data */
612 if(trie->isLatin1Linear && UTRIE_SHIFT<=8) {
613 overlapStart=UTRIE_DATA_BLOCK_LENGTH+256;
615 overlapStart=UTRIE_DATA_BLOCK_LENGTH;
618 newStart=UTRIE_DATA_BLOCK_LENGTH;
619 for(start=newStart; start<trie->dataLength;) {
621 * start: index of first entry of current block
622 * newStart: index where the current block is to be moved
623 * (right after current end of already-compacted data)
626 /* skip blocks that are not used */
627 if(trie->map[start>>UTRIE_SHIFT]<0) {
628 /* advance start to the next block */
629 start+=UTRIE_DATA_BLOCK_LENGTH;
631 /* leave newStart with the previous block! */
635 /* search for an identical block */
636 if( start>=overlapStart &&
637 (i=_findSameDataBlock(trie->data, newStart, start,
638 overlap ? UTRIE_DATA_GRANULARITY : UTRIE_DATA_BLOCK_LENGTH))
641 /* found an identical block, set the other block's index value for the current block */
642 trie->map[start>>UTRIE_SHIFT]=i;
644 /* advance start to the next block */
645 start+=UTRIE_DATA_BLOCK_LENGTH;
647 /* leave newStart with the previous block! */
651 /* see if the beginning of this block can be overlapped with the end of the previous block */
652 if(overlap && start>=overlapStart) {
653 /* look for maximum overlap (modulo granularity) with the previous, adjacent block */
654 for(i=UTRIE_DATA_BLOCK_LENGTH-UTRIE_DATA_GRANULARITY;
655 i>0 && !equal_uint32(trie->data+(newStart-i), trie->data+start, i);
656 i-=UTRIE_DATA_GRANULARITY) {}
663 trie->map[start>>UTRIE_SHIFT]=newStart-i;
665 /* move the non-overlapping indexes to their new positions */
667 for(i=UTRIE_DATA_BLOCK_LENGTH-i; i>0; --i) {
668 trie->data[newStart++]=trie->data[start++];
670 } else if(newStart<start) {
671 /* no overlap, just move the indexes to their new positions */
672 trie->map[start>>UTRIE_SHIFT]=newStart;
673 for(i=UTRIE_DATA_BLOCK_LENGTH; i>0; --i) {
674 trie->data[newStart++]=trie->data[start++];
676 } else /* no overlap && newStart==start */ {
677 trie->map[start>>UTRIE_SHIFT]=start;
678 newStart+=UTRIE_DATA_BLOCK_LENGTH;
683 /* now adjust the index (stage 1) table */
684 for(i=0; i<trie->indexLength; ++i) {
685 trie->index[i]=trie->map[ABS(trie->index[i])>>UTRIE_SHIFT];
689 /* we saved some space */
690 printf("compacting trie: count of 32-bit words %lu->%lu\n",
691 (long)trie->dataLength, (long)newStart);
694 trie->dataLength=newStart;
697 /* serialization ------------------------------------------------------------ */
700 * Default function for the folding value:
701 * Just store the offset (16 bits) if there is any non-initial-value entry.
703 * The offset parameter is never 0.
704 * Returning the offset itself is safe for UTRIE_SHIFT>=5 because
705 * for UTRIE_SHIFT==5 the maximum index length is UTRIE_MAX_INDEX_LENGTH==0x8800
706 * which fits into 16-bit trie values;
707 * for higher UTRIE_SHIFT, UTRIE_MAX_INDEX_LENGTH decreases.
709 * Theoretically, it would be safer for all possible UTRIE_SHIFT including
710 * those of 4 and lower to return offset>>UTRIE_SURROGATE_BLOCK_BITS
711 * which would always result in a value of 0x40..0x43f
712 * (start/end 1k blocks of supplementary Unicode code points).
713 * However, this would be uglier, and would not work for some existing
714 * binary data file formats.
716 * Also, we do not plan to change UTRIE_SHIFT because it would change binary
717 * data file formats, and we would probably not make it smaller because of
718 * the then even larger BMP index length even for empty tries.
720 static uint32_t U_CALLCONV
721 defaultGetFoldedValue(UNewTrie *trie, UChar32 start, int32_t offset) {
722 uint32_t value, initialValue;
726 initialValue=trie->data[0];
729 value=utrie_get32(trie, start, &inBlockZero);
731 start+=UTRIE_DATA_BLOCK_LENGTH;
732 } else if(value!=initialValue) {
733 return (uint32_t)offset;
741 U_CAPI int32_t U_EXPORT2
742 utrie_serialize(UNewTrie *trie, void *dt, int32_t capacity,
743 UNewTrieGetFoldedValue *getFoldedValue,
744 UBool reduceTo16Bits,
745 UErrorCode *pErrorCode) {
750 uint8_t* data = NULL;
753 if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
757 if(trie==NULL || capacity<0 || (capacity>0 && dt==NULL)) {
758 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
761 if(getFoldedValue==NULL) {
762 getFoldedValue=defaultGetFoldedValue;
766 /* fold and compact if necessary, also checks that indexLength is within limits */
767 if(!trie->isCompacted) {
768 /* compact once without overlap to improve folding */
769 utrie_compact(trie, FALSE, pErrorCode);
771 /* fold the supplementary part of the index array */
772 utrie_fold(trie, getFoldedValue, pErrorCode);
774 /* compact again with overlap for minimum data array length */
775 utrie_compact(trie, TRUE, pErrorCode);
777 trie->isCompacted=TRUE;
778 if(U_FAILURE(*pErrorCode)) {
783 /* is dataLength within limits? */
784 if( (reduceTo16Bits ? (trie->dataLength+trie->indexLength) : trie->dataLength) >= UTRIE_MAX_DATA_LENGTH) {
785 *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
788 length=sizeof(UTrieHeader)+2*trie->indexLength;
790 length+=2*trie->dataLength;
792 length+=4*trie->dataLength;
795 if(length>capacity) {
796 return length; /* preflighting */
800 printf("**UTrieLengths(serialize)** index:%6ld data:%6ld serialized:%6ld\n",
801 (long)trie->indexLength, (long)trie->dataLength, (long)length);
804 /* set the header fields */
805 header=(UTrieHeader *)data;
806 data+=sizeof(UTrieHeader);
808 header->signature=0x54726965; /* "Trie" */
809 header->options=UTRIE_SHIFT | (UTRIE_INDEX_SHIFT<<UTRIE_OPTIONS_INDEX_SHIFT);
811 if(!reduceTo16Bits) {
812 header->options|=UTRIE_OPTIONS_DATA_IS_32_BIT;
814 if(trie->isLatin1Linear) {
815 header->options|=UTRIE_OPTIONS_LATIN1_IS_LINEAR;
818 header->indexLength=trie->indexLength;
819 header->dataLength=trie->dataLength;
821 /* write the index (stage 1) array and the 16/32-bit data (stage 2) array */
823 /* write 16-bit index values shifted right by UTRIE_INDEX_SHIFT, after adding indexLength */
824 p=(uint32_t *)trie->index;
825 dest16=(uint16_t *)data;
826 for(i=trie->indexLength; i>0; --i) {
827 *dest16++=(uint16_t)((*p++ + trie->indexLength)>>UTRIE_INDEX_SHIFT);
830 /* write 16-bit data values */
832 for(i=trie->dataLength; i>0; --i) {
833 *dest16++=(uint16_t)*p++;
836 /* write 16-bit index values shifted right by UTRIE_INDEX_SHIFT */
837 p=(uint32_t *)trie->index;
838 dest16=(uint16_t *)data;
839 for(i=trie->indexLength; i>0; --i) {
840 *dest16++=(uint16_t)(*p++ >> UTRIE_INDEX_SHIFT);
843 /* write 32-bit data values */
844 uprv_memcpy(dest16, trie->data, 4*(size_t)trie->dataLength);
850 /* inverse to defaultGetFoldedValue() */
851 U_CAPI int32_t U_EXPORT2
852 utrie_defaultGetFoldingOffset(uint32_t data) {
853 return (int32_t)data;
856 U_CAPI int32_t U_EXPORT2
857 utrie_unserialize(UTrie *trie, const void *data, int32_t length, UErrorCode *pErrorCode) {
858 const UTrieHeader *header;
862 if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
866 /* enough data for a trie header? */
867 if(length<(int32_t)sizeof(UTrieHeader)) {
868 *pErrorCode=U_INVALID_FORMAT_ERROR;
872 /* check the signature */
873 header=(const UTrieHeader *)data;
874 if(header->signature!=0x54726965) {
875 *pErrorCode=U_INVALID_FORMAT_ERROR;
879 /* get the options and check the shift values */
880 options=header->options;
881 if( (options&UTRIE_OPTIONS_SHIFT_MASK)!=UTRIE_SHIFT ||
882 ((options>>UTRIE_OPTIONS_INDEX_SHIFT)&UTRIE_OPTIONS_SHIFT_MASK)!=UTRIE_INDEX_SHIFT
884 *pErrorCode=U_INVALID_FORMAT_ERROR;
887 trie->isLatin1Linear= (UBool)((options&UTRIE_OPTIONS_LATIN1_IS_LINEAR)!=0);
889 /* get the length values */
890 trie->indexLength=header->indexLength;
891 trie->dataLength=header->dataLength;
893 length-=(int32_t)sizeof(UTrieHeader);
895 /* enough data for the index? */
896 if(length<2*trie->indexLength) {
897 *pErrorCode=U_INVALID_FORMAT_ERROR;
900 p16=(const uint16_t *)(header+1);
902 p16+=trie->indexLength;
903 length-=2*trie->indexLength;
906 if(options&UTRIE_OPTIONS_DATA_IS_32_BIT) {
907 if(length<4*trie->dataLength) {
908 *pErrorCode=U_INVALID_FORMAT_ERROR;
911 trie->data32=(const uint32_t *)p16;
912 trie->initialValue=trie->data32[0];
913 length=(int32_t)sizeof(UTrieHeader)+2*trie->indexLength+4*trie->dataLength;
915 if(length<2*trie->dataLength) {
916 *pErrorCode=U_INVALID_FORMAT_ERROR;
920 /* the "data16" data is used via the index pointer */
922 trie->initialValue=trie->index[trie->indexLength];
923 length=(int32_t)sizeof(UTrieHeader)+2*trie->indexLength+2*trie->dataLength;
926 trie->getFoldingOffset=utrie_defaultGetFoldingOffset;
931 U_CAPI int32_t U_EXPORT2
932 utrie_unserializeDummy(UTrie *trie,
933 void *data, int32_t length,
934 uint32_t initialValue, uint32_t leadUnitValue,
936 UErrorCode *pErrorCode) {
938 int32_t actualLength, latin1Length, i, limit;
941 if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
945 /* calculate the actual size of the dummy trie data */
947 /* max(Latin-1, block 0) */
948 latin1Length= 256; /*UTRIE_SHIFT<=8 ? 256 : UTRIE_DATA_BLOCK_LENGTH;*/
950 trie->indexLength=UTRIE_BMP_INDEX_LENGTH+UTRIE_SURROGATE_BLOCK_COUNT;
951 trie->dataLength=latin1Length;
952 if(leadUnitValue!=initialValue) {
953 trie->dataLength+=UTRIE_DATA_BLOCK_LENGTH;
956 actualLength=trie->indexLength*2;
958 actualLength+=trie->dataLength*2;
960 actualLength+=trie->dataLength*4;
963 /* enough space for the dummy trie? */
964 if(length<actualLength) {
965 *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
969 trie->isLatin1Linear=TRUE;
970 trie->initialValue=initialValue;
972 /* fill the index and data arrays */
973 p16=(uint16_t *)data;
977 /* indexes to block 0 */
978 block=(uint16_t)(trie->indexLength>>UTRIE_INDEX_SHIFT);
979 limit=trie->indexLength;
980 for(i=0; i<limit; ++i) {
984 if(leadUnitValue!=initialValue) {
985 /* indexes for lead surrogate code units to the block after Latin-1 */
986 block+=(uint16_t)(latin1Length>>UTRIE_INDEX_SHIFT);
987 i=0xd800>>UTRIE_SHIFT;
988 limit=0xdc00>>UTRIE_SHIFT;
989 for(; i<limit; ++i) {
997 p16+=trie->indexLength;
998 for(i=0; i<latin1Length; ++i) {
999 p16[i]=(uint16_t)initialValue;
1002 /* data for lead surrogate code units */
1003 if(leadUnitValue!=initialValue) {
1004 limit=latin1Length+UTRIE_DATA_BLOCK_LENGTH;
1005 for(/* i=latin1Length */; i<limit; ++i) {
1006 p16[i]=(uint16_t)leadUnitValue;
1012 /* indexes to block 0 */
1013 uprv_memset(p16, 0, trie->indexLength*2);
1015 if(leadUnitValue!=initialValue) {
1016 /* indexes for lead surrogate code units to the block after Latin-1 */
1017 block=(uint16_t)(latin1Length>>UTRIE_INDEX_SHIFT);
1018 i=0xd800>>UTRIE_SHIFT;
1019 limit=0xdc00>>UTRIE_SHIFT;
1020 for(; i<limit; ++i) {
1025 trie->data32=p32=(uint32_t *)(p16+trie->indexLength);
1028 for(i=0; i<latin1Length; ++i) {
1029 p32[i]=initialValue;
1032 /* data for lead surrogate code units */
1033 if(leadUnitValue!=initialValue) {
1034 limit=latin1Length+UTRIE_DATA_BLOCK_LENGTH;
1035 for(/* i=latin1Length */; i<limit; ++i) {
1036 p32[i]=leadUnitValue;
1041 trie->getFoldingOffset=utrie_defaultGetFoldingOffset;
1043 return actualLength;
1046 /* enumeration -------------------------------------------------------------- */
1048 /* default UTrieEnumValue() returns the input value itself */
1049 static uint32_t U_CALLCONV
1050 enumSameValue(const void * /*context*/, uint32_t value) {
1055 * Enumerate all ranges of code points with the same relevant values.
1056 * The values are transformed from the raw trie entries by the enumValue function.
1058 U_CAPI void U_EXPORT2
1059 utrie_enum(const UTrie *trie,
1060 UTrieEnumValue *enumValue, UTrieEnumRange *enumRange, const void *context) {
1061 const uint32_t *data32;
1062 const uint16_t *idx;
1064 uint32_t value, prevValue, initialValue;
1066 int32_t l, i, j, block, prevBlock, nullBlock, offset;
1068 /* check arguments */
1069 if(trie==NULL || trie->index==NULL || enumRange==NULL) {
1072 if(enumValue==NULL) {
1073 enumValue=enumSameValue;
1077 data32=trie->data32;
1079 /* get the enumeration value that corresponds to an initial-value trie data entry */
1080 initialValue=enumValue(context, trie->initialValue);
1083 nullBlock=trie->indexLength;
1088 /* set variables for previous range */
1089 prevBlock=nullBlock;
1091 prevValue=initialValue;
1093 /* enumerate BMP - the main loop enumerates data blocks */
1094 for(i=0, c=0; c<=0xffff; ++i) {
1096 /* skip lead surrogate code _units_, go to lead surr. code _points_ */
1097 i=UTRIE_BMP_INDEX_LENGTH;
1098 } else if(c==0xdc00) {
1099 /* go back to regular BMP code points */
1103 block=idx[i]<<UTRIE_INDEX_SHIFT;
1104 if(block==prevBlock) {
1105 /* the block is the same as the previous one, and filled with value */
1106 c+=UTRIE_DATA_BLOCK_LENGTH;
1107 } else if(block==nullBlock) {
1108 /* this is the all-initial-value block */
1109 if(prevValue!=initialValue) {
1111 if(!enumRange(context, prev, c, prevValue)) {
1115 prevBlock=nullBlock;
1117 prevValue=initialValue;
1119 c+=UTRIE_DATA_BLOCK_LENGTH;
1122 for(j=0; j<UTRIE_DATA_BLOCK_LENGTH; ++j) {
1123 value=enumValue(context, data32!=NULL ? data32[block+j] : idx[block+j]);
1124 if(value!=prevValue) {
1126 if(!enumRange(context, prev, c, prevValue)) {
1131 /* the block is not filled with all the same value */
1142 /* enumerate supplementary code points */
1143 for(l=0xd800; l<0xdc00;) {
1144 /* lead surrogate access */
1145 offset=idx[l>>UTRIE_SHIFT]<<UTRIE_INDEX_SHIFT;
1146 if(offset==nullBlock) {
1147 /* no entries for a whole block of lead surrogates */
1148 if(prevValue!=initialValue) {
1150 if(!enumRange(context, prev, c, prevValue)) {
1154 prevBlock=nullBlock;
1156 prevValue=initialValue;
1159 l+=UTRIE_DATA_BLOCK_LENGTH;
1160 c+=UTRIE_DATA_BLOCK_LENGTH<<10;
1164 value= data32!=NULL ? data32[offset+(l&UTRIE_MASK)] : idx[offset+(l&UTRIE_MASK)];
1166 /* enumerate trail surrogates for this lead surrogate */
1167 offset=trie->getFoldingOffset(value);
1169 /* no data for this lead surrogate */
1170 if(prevValue!=initialValue) {
1172 if(!enumRange(context, prev, c, prevValue)) {
1176 prevBlock=nullBlock;
1178 prevValue=initialValue;
1181 /* nothing else to do for the supplementary code points for this lead surrogate */
1184 /* enumerate code points for this lead surrogate */
1186 offset+=UTRIE_SURROGATE_BLOCK_COUNT;
1188 /* copy of most of the body of the BMP loop */
1189 block=idx[i]<<UTRIE_INDEX_SHIFT;
1190 if(block==prevBlock) {
1191 /* the block is the same as the previous one, and filled with value */
1192 c+=UTRIE_DATA_BLOCK_LENGTH;
1193 } else if(block==nullBlock) {
1194 /* this is the all-initial-value block */
1195 if(prevValue!=initialValue) {
1197 if(!enumRange(context, prev, c, prevValue)) {
1201 prevBlock=nullBlock;
1203 prevValue=initialValue;
1205 c+=UTRIE_DATA_BLOCK_LENGTH;
1208 for(j=0; j<UTRIE_DATA_BLOCK_LENGTH; ++j) {
1209 value=enumValue(context, data32!=NULL ? data32[block+j] : idx[block+j]);
1210 if(value!=prevValue) {
1212 if(!enumRange(context, prev, c, prevValue)) {
1217 /* the block is not filled with all the same value */
1226 } while(++i<offset);
1232 /* deliver last range */
1233 enumRange(context, prev, c, prevValue);