/* we do not expect this to be called */ static UBool
CnvExtIsValid(NewConverter *cnvData, const uint8_t *bytes, int32_t length) { // suppress compiler warnings about unused variables
(void)cnvData;
(void)bytes;
(void)length; returnfalse;
}
/* write the header and base table name for an extension-only table */
length=(int32_t)uprv_strlen(extData->ucm->baseName)+1; while(length&3) { /* add padding */
extData->ucm->baseName[length++]=0;
}
headerSize=MBCS_HEADER_V4_LENGTH*4+length;
/* fill the header */
header.version[0]=4;
header.version[1]=2;
header.flags=(uint32_t)((headerSize<<8)|MBCS_OUTPUT_EXT_ONLY);
/* write the header and the base table name */
udata_writeBlock(pData, &header, MBCS_HEADER_V4_LENGTH*4);
udata_writeBlock(pData, extData->ucm->baseName, length);
}
/* fill indexes[] - offsets/indexes are in units of the target array */
top=0;
if(VERBOSE) {
printf("size of extension data: %ld\n", (long)top);
}
/* return the number of bytes that should have been written */ return (uint32_t)(headerSize+top);
}
/* to Unicode --------------------------------------------------------------- */
/* * Remove fromUnicode fallbacks and SUB mappings which are irrelevant for * the toUnicode table. * This includes mappings with MBCS_FROM_U_EXT_FLAG which were suitable * for the base toUnicode table but not for the base fromUnicode table. * The table must be sorted. * Modifies previous data in the reverseMap.
*/ static int32_t
reduceToUMappings(UCMTable *table) {
UCMapping *mappings;
int32_t *map;
int32_t i, j, count;
int8_t flag;
/* write the Unicode result code point or string index */ if(m->uLen==1) {
u16Length=U16_LENGTH(m->u);
value=(uint32_t)(UCNV_EXT_TO_U_MIN_CODE_POINT+m->u);
} else { /* the parser enforces m->uLen<=UCNV_EXT_MAX_UCHARS */
/* get the result code point string and its 16-bit string length */
u32=UCM_GET_CODE_POINTS(table, m);
errorCode=U_ZERO_ERROR;
u_strFromUTF32(NULL, 0, &u16Length, u32, m->uLen, &errorCode); if(U_FAILURE(errorCode) && errorCode!=U_BUFFER_OVERFLOW_ERROR) { exit(errorCode);
}
/* allocate it and put its length and index into the value */
value=
(((uint32_t)u16Length+UCNV_EXT_TO_U_LENGTH_OFFSET)<<UCNV_EXT_TO_U_LENGTH_SHIFT)|
((uint32_t)utm_countItems(extData->toUUChars));
u=utm_allocN(extData->toUUChars, u16Length);
/* * Recursive toUTable generator core function. * Preconditions: * - start<limit (There is at least one mapping.) * - The mappings are sorted lexically. (Access is through the reverseMap.) * - All mappings between start and limit have input sequences that share * the same prefix of unitIndex length, and therefore all of these sequences * are at least unitIndex+1 long. * - There are only relevant mappings available through the reverseMap, * see reduceToUMappings(). * * One function invocation generates one section table. * * Steps: * 1. Count the number of unique unit values and get the low/high unit values * that occur at unitIndex. * 2. Allocate the section table with possible optimization for linear access. * 3. Write temporary version of the section table with start indexes of * subsections, each corresponding to one unit value at unitIndex. * 4. Iterate through the table once more, and depending on the subsection length: * 0: write 0 as a result value (unused byte in linear-access section table) * >0: if there is one mapping with an input unit sequence of unitIndex+1 * then defaultValue=compute the mapping result for this whole sequence * else defaultValue=0 * * recurse into the subsection
*/ static UBool
generateToUTable(CnvExtData *extData, UCMTable *table,
int32_t start, int32_t limit, int32_t unitIndex,
uint32_t defaultValue) {
UCMapping *mappings, *m;
int32_t *map;
int32_t i, j, uniqueCount, count, subStart, subLimit;
uint8_t *bytes;
int32_t low, high, prev;
uint32_t *section;
mappings=table->mappings;
map=table->reverseMap;
/* step 1: examine the input units; set low, high, uniqueCount */
m=mappings+map[start];
bytes=UCM_GET_BYTES(table, m);
low=bytes[unitIndex];
uniqueCount=1;
/* step 2: allocate the section; set count, section */
count=(high-low)+1; if(count<0x100 && (unitIndex==0 || uniqueCount>=(3*count)/4)) { /* * for the root table and for fairly full tables: * allocate for direct, linear array access * by keeping count, to write an entry for each unit value * from low to high * exception: use a compact table if count==0x100 because * that cannot be encoded in the length byte
*/
} else {
count=uniqueCount;
}
/* allocate the section: 1 entry for the header + count for the items */
section=(uint32_t *)utm_allocN(extData->toUTable, 1+count);
/* write the section header */
*section++=((uint32_t)count<<UCNV_EXT_TO_U_BYTE_SHIFT)|defaultValue;
/* step 3: write temporary section table with subsection starts */
prev=low-1; /* just before low to prevent empty subsections before low */
j=0; /* section table index */ for(i=start; i<limit; ++i) {
m=mappings+map[i];
bytes=UCM_GET_BYTES(table, m);
high=bytes[unitIndex];
if(high!=prev) { /* start of a new subsection for unit high */ if(count>uniqueCount) { /* write empty subsections for unused units in a linear table */ while(++prev<high) {
section[j++]=((uint32_t)prev<<UCNV_EXT_TO_U_BYTE_SHIFT)|(uint32_t)i;
}
} else {
prev=high;
}
/* write the entry with the subsection start */
section[j++]=((uint32_t)high<<UCNV_EXT_TO_U_BYTE_SHIFT)|(uint32_t)i;
}
} /* assert(j==count) */
/* remove the subStart temporary value */
section[j]&=~UCNV_EXT_TO_U_VALUE_MASK;
if(subStart==subLimit) { /* leave the value zero: empty subsection for unused unit in a linear table */ continue;
}
/* see if there is exactly one input unit sequence of length unitIndex+1 */
defaultValue=0;
m=mappings+map[subStart]; if(m->bLen==unitIndex+1) { /* do not include this in generateToUTable() */
++subStart;
if(subStart<subLimit && mappings[map[subStart]].bLen==unitIndex+1) { /* print error for multiple same-input-sequence mappings */
fprintf(stderr, "error: multiple mappings from same bytes\n");
ucm_printMapping(table, m, stderr);
ucm_printMapping(table, mappings+map[subStart], stderr); returnfalse;
}
if(subStart==subLimit) { /* write the result for the input sequence ending here */
section[j]|=defaultValue;
} else { /* write the index to the subsection table */
section[j]|=(uint32_t)utm_countItems(extData->toUTable);
/* * Generate the toUTable and toUUChars from the input table. * The input table must be sorted, and all precision flags must be 0..3. * This function will modify the table's reverseMap.
*/ static UBool
makeToUTable(CnvExtData *extData, UCMTable *table) {
int32_t toUCount;
/* from Unicode ------------------------------------------------------------- */
/* * preprocessing: * rebuild reverseMap with mapping indexes for mappings relevant for from Unicode * change each Unicode string to encode all but the first code point in 16-bit form * * generation: * for each unique code point * write an entry in the 3-stage trie * check that there is only one single-code point sequence * start recursion for following 16-bit input units
*/
/* * Remove toUnicode fallbacks and non-<subchar1> SUB mappings * which are irrelevant for the fromUnicode extension table. * Remove MBCS_FROM_U_EXT_FLAG bits. * Overwrite the reverseMap with an index array to the relevant mappings. * Modify the code point sequences to a generator-friendly format where * the first code points remains unchanged but the following are recoded * into 16-bit Unicode string form. * The table must be sorted. * Destroys previous data in the reverseMap.
*/ static int32_t
prepareFromUMappings(UCMTable *table) {
UCMapping *mappings, *m;
int32_t *map;
int32_t i, j, count;
int8_t flag;
if(m->f==2) { /* * no mapping, <subchar1> preferred * * no need to count in statistics because the subchars are already * counted for maxOutBytes and maxBytesPerUChar in UConverterStaticData, * and this non-mapping does not count for maxInUChars which are always * trivially at least two if counting unmappable supplementary code points
*/ return UCNV_EXT_FROM_U_SUBCHAR1;
}
bytes=UCM_GET_BYTES(table, m);
value=0; switch(m->bLen) { /* 1..3: store the bytes in the value word */ case 3:
value=((uint32_t)*bytes++)<<16; case 2:
value|=((uint32_t)*bytes++)<<8; case 1:
value|=*bytes; break; default: /* the parser enforces m->bLen<=UCNV_EXT_MAX_BYTES */ /* store the bytes in fromUBytes[] and the index in the value word */
value=(uint32_t)utm_countItems(extData->fromUBytes);
resultBytes=utm_allocN(extData->fromUBytes, m->bLen);
uprv_memcpy(resultBytes, bytes, m->bLen); break;
}
value|=(uint32_t)m->bLen<<UCNV_EXT_FROM_U_LENGTH_SHIFT; if(m->f==0) {
value|=UCNV_EXT_FROM_U_ROUNDTRIP_FLAG;
} elseif(m->f==4) {
value|=UCNV_EXT_FROM_U_GOOD_ONE_WAY_FLAG;
}
/* calculate the real UTF-16 length (see recoding in prepareFromUMappings()) */ if(m->uLen==1) {
u16Length=U16_LENGTH(m->u);
} else {
u16Length=U16_LENGTH(UCM_GET_CODE_POINTS(table, m)[0])+(m->uLen-2);
}
/* * works like generateToUTable(), except that the * output section consists of two arrays, one for input UChars and one * for result values * * also, fromUTable sections are always stored in a compact form for * access via binary search
*/ static UBool
generateFromUTable(CnvExtData *extData, UCMTable *table,
int32_t start, int32_t limit, int32_t unitIndex,
uint32_t defaultValue) {
UCMapping *mappings, *m;
int32_t *map;
int32_t i, j, uniqueCount, count, subStart, subLimit;
UChar *uchars;
UChar32 low, high, prev;
UChar *sectionUChars;
uint32_t *sectionValues;
mappings=table->mappings;
map=table->reverseMap;
/* step 1: examine the input units; set low, high, uniqueCount */
m=mappings+map[start];
uchars=(UChar *)UCM_GET_CODE_POINTS(table, m);
low=uchars[unitIndex];
uniqueCount=1;
/* step 2: allocate the section; set count, section */ /* the fromUTable always stores for access via binary search */
count=uniqueCount;
/* allocate the section: 1 entry for the header + count for the items */
sectionUChars=(UChar *)utm_allocN(extData->fromUTableUChars, 1+count);
sectionValues=(uint32_t *)utm_allocN(extData->fromUTableValues, 1+count);
/* write the section header */
*sectionUChars++=(UChar)count;
*sectionValues++=defaultValue;
/* step 3: write temporary section table with subsection starts */
prev=low-1; /* just before low to prevent empty subsections before low */
j=0; /* section table index */ for(i=start; i<limit; ++i) {
m=mappings+map[i];
uchars=(UChar *)UCM_GET_CODE_POINTS(table, m);
high=uchars[unitIndex];
if(high!=prev) { /* start of a new subsection for unit high */
prev=high;
/* write the entry with the subsection start */
sectionUChars[j]=(UChar)high;
sectionValues[j]=(uint32_t)i;
++j;
}
} /* assert(j==count) */
/* see if there is exactly one input unit sequence of length unitIndex+1 */
defaultValue=0;
m=mappings+map[subStart]; if(m->uLen==unitIndex+1) { /* do not include this in generateToUTable() */
++subStart;
if(subStart<subLimit && mappings[map[subStart]].uLen==unitIndex+1) { /* print error for multiple same-input-sequence mappings */
fprintf(stderr, "error: multiple mappings from same Unicode code points\n");
ucm_printMapping(table, m, stderr);
ucm_printMapping(table, mappings+map[subStart], stderr); returnfalse;
}
if(subStart==subLimit) { /* write the result for the input sequence ending here */
sectionValues[j]=defaultValue;
} else { /* write the index to the subsection table */
sectionValues[j]=(uint32_t)utm_countItems(extData->fromUTableValues);
/* * add entries to the fromUnicode trie, * assume to be called with code points in ascending order * and use that to build the trie in precompacted form
*/ staticvoid
addFromUTrieEntry(CnvExtData *extData, UChar32 c, uint32_t value) {
int32_t i1, i2, i3, i3b, nextOffset, min, newBlock;
if(value==0) { return;
}
/* * compute the index for each stage, * allocate a stage block if necessary, * and write the stage value
*/
i1=c>>10; if(i1>=extData->stage1Top) {
extData->stage1Top=i1+1;
}
nextOffset=(c>>4)&0x3f;
if(extData->stage1[i1]==0) { /* allocate another block in stage 2; overlap with the previous block */
newBlock=extData->stage2Top;
min=newBlock-nextOffset; /* minimum block start with overlap */ while(min<newBlock && extData->stage2[newBlock-1]==0) {
--newBlock;
}
extData->stage1[i1]=(uint16_t)newBlock;
extData->stage2Top=newBlock+MBCS_STAGE_2_BLOCK_SIZE; if(extData->stage2Top>UPRV_LENGTHOF(extData->stage2)) {
fprintf(stderr, "error: too many stage 2 entries at U+%04x\n", (int)c); exit(U_MEMORY_ALLOCATION_ERROR);
}
}
if(extData->stage2[i2]==0) { /* allocate another block in stage 3; overlap with the previous block */
newBlock=extData->stage3Top;
min=newBlock-nextOffset; /* minimum block start with overlap */ while(min<newBlock && extData->stage3[newBlock-1]==0) {
--newBlock;
}
/* round up to a multiple of stage 3 granularity >1 (similar to utrie.c) */
newBlock=(newBlock+(UCNV_EXT_STAGE_3_GRANULARITY-1))&~(UCNV_EXT_STAGE_3_GRANULARITY-1);
extData->stage2[i2]=(uint16_t)(newBlock>>UCNV_EXT_STAGE_2_LEFT_SHIFT);
extData->stage3Top=newBlock+MBCS_STAGE_3_BLOCK_SIZE; if(extData->stage3Top>UPRV_LENGTHOF(extData->stage3)) {
fprintf(stderr, "error: too many stage 3 entries at U+%04x\n", (int)c); exit(U_MEMORY_ALLOCATION_ERROR);
}
}
i3=((int32_t)extData->stage2[i2]<<UCNV_EXT_STAGE_2_LEFT_SHIFT)+nextOffset; /* * assume extData->stage3[i3]==0 because we get * code points in strictly ascending order
*/
if(value==UCNV_EXT_FROM_U_SUBCHAR1) { /* <subchar1> SUB mapping, see getFromUBytesValue() and prepareFromUMappings() */
extData->stage3[i3]=1;
/* * precompaction is not optimal for <subchar1> |2 mappings because * stage3 values for them are all the same, unlike for other mappings * which all have unique values; * use a simple compaction of reusing a whole block filled with these * mappings
*/
/* is the entire block filled with <subchar1> |2 mappings? */ if(nextOffset==MBCS_STAGE_3_BLOCK_SIZE-1) { for(min=i3-nextOffset;
min<i3 && extData->stage3[min]==1;
++min) {}
if(min==i3) { /* the entire block is filled with these mappings */ if(extData->stage3Sub1Block!=0) { /* point to the previous such block and remove this block from stage3 */
extData->stage2[i2]=extData->stage3Sub1Block;
extData->stage3Top-=MBCS_STAGE_3_BLOCK_SIZE;
uprv_memset(extData->stage3+extData->stage3Top, 0, MBCS_STAGE_3_BLOCK_SIZE*2);
} else { /* remember this block's stage2 entry */
extData->stage3Sub1Block=extData->stage2[i2];
}
}
}
} else { if((i3b=extData->stage3bTop++)>=UPRV_LENGTHOF(extData->stage3b)) {
fprintf(stderr, "error: too many stage 3b entries at U+%04x\n", (int)c); exit(U_MEMORY_ALLOCATION_ERROR);
}
/* roundtrip or fallback mapping */
extData->stage3[i3]=(uint16_t)i3b;
extData->stage3b[i3b]=value;
}
}
/* * iterate over same-initial-code point mappings, * enter the initial code point into the trie, * and start a recursion on the corresponding mappings section * with generateFromUTable()
*/
m=mappings+map[0];
codePoints=UCM_GET_CODE_POINTS(table, m);
next=codePoints[0];
subLimit=0; while(subLimit<mapLength) { /* get a new subsection of mappings starting with the same code point */
subStart=subLimit;
c=next; while(next==c && ++subLimit<mapLength) {
m=mappings+map[subLimit];
codePoints=UCM_GET_CODE_POINTS(table, m);
next=codePoints[0];
}
/* * compute the value for this code point; * if there is a mapping for this code point alone, it is at subStart * because the table is sorted lexically
*/
value=0;
m=mappings+map[subStart];
codePoints=UCM_GET_CODE_POINTS(table, m); if(m->uLen==1) { /* do not include this in generateFromUTable() */
++subStart;
if(subStart<subLimit && mappings[map[subStart]].uLen==1) { /* print error for multiple same-input-sequence mappings */
fprintf(stderr, "error: multiple mappings from same Unicode code points\n");
ucm_printMapping(table, m, stderr);
ucm_printMapping(table, mappings+map[subStart], stderr); returnfalse;
}
value=getFromUBytesValue(extData, table, m);
}
if(subStart==subLimit) { /* write the result for this one code point */
addFromUTrieEntry(extData, c, value);
} else { /* write the index to the subsection table */
addFromUTrieEntry(extData, c, (uint32_t)utm_countItems(extData->fromUTableValues));
/* recurse, starting from 16-bit-unit index 2, the first 16-bit unit after c */ if(!generateFromUTable(extData, table, subStart, subLimit, 2, value)) { returnfalse;
}
}
} returntrue;
}
/* * Generate the fromU data structures from the input table. * The input table must be sorted, and all precision flags must be 0..3. * This function will modify the table's reverseMap.
*/ static UBool
makeFromUTable(CnvExtData *extData, UCMTable *table) {
uint16_t *stage1;
int32_t i, stage1Top, fromUCount;
/* * stage 3b stores only unique values, and in * index 0: 0 for "no mapping" * index 1: "no mapping" with preference for <subchar1> rather than <subchar>
*/
extData->stage3b[1]=UCNV_EXT_FROM_U_SUBCHAR1;
extData->stage3bTop=2;
/* allocate the first entry in the fromUTable because index 0 means "no result" */
utm_alloc(extData->fromUTableUChars);
utm_alloc(extData->fromUTableValues);
/* * offset the stage 1 trie entries by stage1Top because they will * be stored in a single array
*/
stage1=extData->stage1;
stage1Top=extData->stage1Top; for(i=0; i<stage1Top; ++i) {
stage1[i]=(uint16_t)(stage1[i]+stage1Top);
}
/* * assume that the table is sorted * * call the functions in this order because * makeToUTable() modifies the original reverseMap, * makeFromUTable() writes a whole new mapping into reverseMap
*/ return
makeToUTable(extData, table) &&
makeFromUTable(extData, table);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.