intl/icu/source/common/utrie.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial