2 *******************************************************************************
4 * Copyright (C) 1999-2008, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
10 * tab size: 8 (not used)
13 * created on: 1999dec11
14 * created by: Markus W. Scherer
16 * Store Unicode character properties efficiently for
21 #include "unicode/utypes.h"
22 #include "unicode/uchar.h"
26 #include "unicode/udata.h"
32 #define DO_DEBUG_OUT 0
34 /* Unicode character properties file format ------------------------------------
36 The file format prepared and written here contains several data
37 structures that store indexes or data.
39 Before the data contents described below, there are the headers required by
40 the udata API for loading ICU data. Especially, a UDataInfo structure
41 precedes the actual data. It contains platform properties values and the
44 The following is a description of format version 5 .
46 The format changes between version 3 and 4 because the properties related to
47 case mappings and bidi/shaping are pulled out into separate files
49 In order to reduce the need for code changes, some of the previous data
50 structures are omitted, rather than rearranging everything.
52 For details see "Changes in format version 4" below.
54 Format version 5 became necessary because the bit field for script codes
55 overflowed. Several bit fields got rearranged, and three (Script, Block,
56 Word_Break) got widened by one bit each.
60 The contents is a parsed, binary form of several Unicode character
61 database files, most prominently UnicodeData.txt.
63 Any Unicode code point from 0 to 0x10ffff can be looked up to get
64 the properties, if any, for that code point. This means that the input
65 to the lookup are 21-bit unsigned integers, with not all of the
68 It is assumed that client code keeps a uint32_t pointer
69 to the beginning of the data:
73 Formally, the file contains the following structures:
75 const int32_t indexes[16] with values i0..i15:
77 i0 indicates the length of the main trie.
78 i0..i3 all have the same value in format version 4.0;
79 the related props32[] and exceptions[] and uchars[] were used in format version 3
81 i0 propsIndex; -- 32-bit unit index to the table of 32-bit properties words
82 i1 exceptionsIndex; -- 32-bit unit index to the table of 32-bit exception words
83 i2 exceptionsTopIndex; -- 32-bit unit index to the array of UChars for special mappings
85 i3 additionalTrieIndex; -- 32-bit unit index to the additional trie for more properties
86 i4 additionalVectorsIndex; -- 32-bit unit index to the table of properties vectors
87 i5 additionalVectorsColumns; -- number of 32-bit words per properties vector
89 i6 reservedItemIndex; -- 32-bit unit index to the top of the properties vectors table
90 i7..i9 reservedIndexes; -- reserved values; 0 for now
92 i10 maxValues; -- maximum code values for vector word 0, see uprops.h (new in format version 3.1+)
93 i11 maxValues2; -- maximum code values for vector word 2, see uprops.h (new in format version 3.2)
94 i12..i15 reservedIndexes; -- reserved values; 0 for now
96 PT serialized properties trie, see utrie.h (byte size: 4*(i0-16))
98 P, E, and U are not used (empty) in format version 4
100 P const uint32_t props32[i1-i0];
101 E const uint32_t exceptions[i2-i1];
102 U const UChar uchars[2*(i3-i2)];
104 AT serialized trie for additional properties (byte size: 4*(i4-i3))
105 PV const uint32_t propsVectors[(i6-i4)/i5][i5]==uint32_t propsVectors[i6-i4];
107 Trie lookup and properties:
109 In order to condense the data for the 21-bit code space, several properties of
110 the Unicode code assignment are exploited:
111 - The code space is sparse.
112 - There are several 10k of consecutive codes with the same properties.
113 - Characters and scripts are allocated in groups of 16 code points.
114 - Inside blocks for scripts the properties are often repetitive.
115 - The 21-bit space is not fully used for Unicode.
117 The lookup of properties for a given code point is done with a trie lookup,
118 using the UTrie implementation.
119 The trie lookup result is a 16-bit properties word.
121 With a given Unicode code point
125 and 0<=c<0x110000, the lookup is done like this:
128 UTRIE_GET16(trie, c, props);
130 Each 16-bit properties word contains:
132 0.. 4 general category
134 non-digit numbers are stored with multiple types and pseudo-types
135 in order to facilitate compact encoding:
136 0 no numeric value (0)
137 1 decimal digit value (0..9)
139 3 (U_NT_NUMERIC) normal non-digit numeric value 0..0xff
140 4 (internal type UPROPS_NT_FRACTION) fraction
141 5 (internal type UPROPS_NT_LARGE) large number >0xff
144 when returning the numeric type from a public API,
145 internal types must be turned into U_NT_NUMERIC
148 encoding of fractions and large numbers see below
151 // n is the 8-bit numeric value from bits 8..15 of the trie word (shifted down)
153 num=n>>3; // num=0..31
154 den=(n&7)+2; // den=2..9
156 num=-1; // num=-1 or 1..31
158 double result=(double)num/(double)den;
161 // n is the 8-bit numeric value from bits 8..15 of the trie word (shifted down)
166 m=1; // for large powers of 10
170 } // m==10..15 are reserved
171 double result=(double)m*10^e;
173 --- Additional properties (new in format version 2.1) ---
175 The second trie for additional properties (AT) is also a UTrie with 16-bit data.
176 The data words consist of 32-bit unit indexes (not row indexes!) into the
177 table of unique properties vectors (PV).
178 Each vector contains a set of properties.
179 The width of a vector (number of uint32_t per row) may change
180 with the formatVersion, it is stored in i5.
182 Current properties: see icu/source/common/uprops.h
184 --- Changes in format version 3.1 ---
186 See i10 maxValues above, contains only UBLOCK_COUNT and USCRIPT_CODE_LIMIT.
188 --- Changes in format version 3.2 ---
190 - The tries use linear Latin-1 ranges.
191 - The additional properties bits store full properties XYZ instead
192 of partial Other_XYZ, so that changes in the derivation formulas
193 need not be tracked in runtime library code.
194 - Joining Type and Line Break are also stored completely, so that uprops.c
195 needs no runtime formulas for enumerated properties either.
196 - Store the case-sensitive flag in the main properties word.
197 - i10 also contains U_LB_COUNT and U_EA_COUNT.
198 - i11 contains maxValues2 for vector word 2.
200 --- Changes in format version 4 ---
202 The format changes between version 3 and 4 because the properties related to
203 case mappings and bidi/shaping are pulled out into separate files
205 In order to reduce the need for code changes, some of the previous data
206 structures are omitted, rather than rearranging everything.
208 (The change to format version 4 is for ICU 3.4. The last CVS revision of
209 genprops/store.c for format version 3.2 is 1.48.)
211 The main trie's data is significantly simplified:
212 - The trie's 16-bit data word is used directly instead of as an index
214 - The trie uses the default trie folding functions instead of custom ones.
215 - Numeric values are stored directly in the trie data word, with special
217 - No more exception data (the data that needed it was pulled out, or, in the
218 case of numeric values, encoded differently).
219 - No more string data (pulled out - was for case mappings).
221 Also, some of the previously used properties vector bits are reserved again.
223 The indexes[] values for the omitted structures are still filled in
224 (indicating zero-length arrays) so that the swapper code remains unchanged.
226 --- Changes in format version 5 ---
228 Rearranged bit fields in the second trie (AT) because the script code field
229 overflowed. Old code would have seen nonsensically low values for new, higher
231 Modified bit fields in icu/source/common/uprops.h
233 ----------------------------------------------------------------------------- */
235 /* UDataInfo cf. udata.h */
236 static UDataInfo dataInfo
={
245 { 0x55, 0x50, 0x72, 0x6f }, /* dataFormat="UPro" */
246 { 5, 0, UTRIE_SHIFT
, UTRIE_INDEX_SHIFT
}, /* formatVersion */
247 { 5, 1, 0, 0 } /* dataVersion */
250 static UNewTrie
*pTrie
=NULL
;
252 /* -------------------------------------------------------------------------- */
255 setUnicodeVersion(const char *v
) {
256 UVersionInfo version
;
257 u_versionFromString(version
, v
);
258 uprv_memcpy(dataInfo
.dataVersion
, version
, 4);
263 pTrie
=utrie_open(NULL
, NULL
, 40000, 0, 0, TRUE
);
265 fprintf(stderr
, "error: unable to create a UNewTrie\n");
266 exit(U_MEMORY_ALLOCATION_ERROR
);
269 initAdditionalProperties();
275 exitAdditionalProperties();
278 static uint32_t printNumericTypeValueError(Props
*p
) {
279 fprintf(stderr
, "genprops error: unable to encode numeric type & value %d %ld/%lu E%d\n",
280 (int)p
->numericType
, (long)p
->numericValue
, (unsigned long)p
->denominator
, p
->exponent
);
281 exit(U_ILLEGAL_ARGUMENT_ERROR
);
285 /* store a character's properties ------------------------------------------- */
288 makeProps(Props
*p
) {
290 int32_t type
, value
, exp
;
292 /* encode numeric type & value */
294 value
=p
->numericValue
;
300 if( type
!=U_NT_NUMERIC
||
301 value
<-1 || value
==0 || value
>UPROPS_FRACTION_MAX_NUM
||
302 den
<UPROPS_FRACTION_MIN_DEN
|| UPROPS_FRACTION_MAX_DEN
<den
||
305 return printNumericTypeValueError(p
);
307 type
=UPROPS_NT_FRACTION
;
312 den
-=UPROPS_FRACTION_DEN_OFFSET
;
313 value
=(value
<<UPROPS_FRACTION_NUM_SHIFT
)|den
;
315 /* very large value */
316 if( type
!=U_NT_NUMERIC
||
317 value
<1 || 9<value
||
318 exp
<UPROPS_LARGE_MIN_EXP
|| UPROPS_LARGE_MAX_EXP_EXTRA
<exp
320 return printNumericTypeValueError(p
);
322 type
=UPROPS_NT_LARGE
;
324 if(exp
<=UPROPS_LARGE_MAX_EXP
) {
325 /* 1..9 * 10^(2..17) */
326 exp
-=UPROPS_LARGE_EXP_OFFSET
;
328 /* 1 * 10^(18..33) */
330 return printNumericTypeValueError(p
);
333 exp
-=UPROPS_LARGE_EXP_OFFSET_EXTRA
;
335 value
=(value
<<UPROPS_LARGE_MANT_SHIFT
)|exp
;
336 } else if(value
>UPROPS_MAX_SMALL_NUMBER
) {
338 if(type
!=U_NT_NUMERIC
) {
339 return printNumericTypeValueError(p
);
341 type
=UPROPS_NT_LARGE
;
343 /* split the value into mantissa and exponent, base 10 */
344 while((value%10
)==0) {
349 return printNumericTypeValueError(p
);
352 exp
-=UPROPS_LARGE_EXP_OFFSET
;
353 value
=(value
<<UPROPS_LARGE_MANT_SHIFT
)|exp
;
355 /* unable to encode negative values, other than fractions -1/x */
356 return printNumericTypeValueError(p
);
358 /* } else normal value=0..0xff { */
361 /* encode the properties */
363 (uint32_t)p
->generalCategory
|
364 ((uint32_t)type
<<UPROPS_NUMERIC_TYPE_SHIFT
) |
365 ((uint32_t)value
<<UPROPS_NUMERIC_VALUE_SHIFT
);
369 addProps(uint32_t c
, uint32_t x
) {
370 if(!utrie_set32(pTrie
, (UChar32
)c
, x
)) {
371 fprintf(stderr
, "error: too many entries for the properties trie\n");
372 exit(U_BUFFER_OVERFLOW_ERROR
);
377 getProps(uint32_t c
) {
378 return utrie_get32(pTrie
, (UChar32
)c
, NULL
);
381 /* areas of same properties ------------------------------------------------- */
384 repeatProps(uint32_t first
, uint32_t last
, uint32_t x
) {
385 if(!utrie_setRange32(pTrie
, (UChar32
)first
, (UChar32
)(last
+1), x
, FALSE
)) {
386 fprintf(stderr
, "error: too many entries for the properties trie\n");
387 exit(U_BUFFER_OVERFLOW_ERROR
);
391 /* generate output data ----------------------------------------------------- */
394 generateData(const char *dataDir
, UBool csource
) {
395 static int32_t indexes
[UPROPS_INDEX_COUNT
]={
401 static uint8_t trieBlock
[40000];
402 static uint8_t additionalProps
[120000];
404 UNewDataMemory
*pData
;
405 UErrorCode errorCode
=U_ZERO_ERROR
;
407 int32_t trieSize
, additionalPropsSize
, offset
;
410 trieSize
=utrie_serialize(pTrie
, trieBlock
, sizeof(trieBlock
), NULL
, TRUE
, &errorCode
);
411 if(U_FAILURE(errorCode
)) {
412 fprintf(stderr
, "error: utrie_serialize failed: %s (length %ld)\n", u_errorName(errorCode
), (long)trieSize
);
416 offset
=sizeof(indexes
)/4; /* uint32_t offset to the properties trie */
418 /* round up trie size to 4-alignment */
419 trieSize
=(trieSize
+3)&~3;
421 indexes
[UPROPS_PROPS32_INDEX
]= /* set indexes to the same offsets for empty */
422 indexes
[UPROPS_EXCEPTIONS_INDEX
]= /* structures from the old format version 3 */
423 indexes
[UPROPS_EXCEPTIONS_TOP_INDEX
]= /* so that less runtime code has to be changed */
424 indexes
[UPROPS_ADDITIONAL_TRIE_INDEX
]=offset
;
427 printf("trie size in bytes: %5u\n", (int)trieSize
);
431 /* write .c file for hardcoded data */
435 utrie_unserialize(&trie
, trieBlock
, trieSize
, &errorCode
);
436 if(U_FAILURE(errorCode
)) {
439 "genprops error: failed to utrie_unserialize(uprops.icu main trie) - %s\n",
440 u_errorName(errorCode
));
444 f
=usrc_create(dataDir
, "uchar_props_data.c");
447 "static const UVersionInfo formatVersion={",
448 dataInfo
.formatVersion
, 8, 4,
451 "static const UVersionInfo dataVersion={",
452 dataInfo
.dataVersion
, 8, 4,
454 usrc_writeUTrieArrays(f
,
455 "static const uint16_t propsTrie_index[%ld]={\n", NULL
,
458 usrc_writeUTrieStruct(f
,
459 "static const UTrie propsTrie={\n",
460 &trie
, "propsTrie_index", NULL
, NULL
,
463 additionalPropsSize
=writeAdditionalData(f
, additionalProps
, sizeof(additionalProps
), indexes
);
464 size
=4*offset
+additionalPropsSize
; /* total size of data */
467 "static const int32_t indexes[UPROPS_INDEX_COUNT]={",
468 indexes
, 32, UPROPS_INDEX_COUNT
,
474 pData
=udata_create(dataDir
, DATA_TYPE
, DATA_NAME
, &dataInfo
,
475 haveCopyright
? U_COPYRIGHT_STRING
: NULL
, &errorCode
);
476 if(U_FAILURE(errorCode
)) {
477 fprintf(stderr
, "genprops: unable to create data memory, %s\n", u_errorName(errorCode
));
481 additionalPropsSize
=writeAdditionalData(NULL
, additionalProps
, sizeof(additionalProps
), indexes
);
482 size
=4*offset
+additionalPropsSize
; /* total size of data */
484 udata_writeBlock(pData
, indexes
, sizeof(indexes
));
485 udata_writeBlock(pData
, trieBlock
, trieSize
);
486 udata_writeBlock(pData
, additionalProps
, additionalPropsSize
);
489 dataLength
=udata_finish(pData
, &errorCode
);
490 if(U_FAILURE(errorCode
)) {
491 fprintf(stderr
, "genprops: error %d writing the output file\n", errorCode
);
495 if(dataLength
!=(long)size
) {
496 fprintf(stderr
, "genprops: data length %ld != calculated size %lu\n",
497 dataLength
, (unsigned long)size
);
498 exit(U_INTERNAL_PROGRAM_ERROR
);
503 printf("data size: %6lu\n", (unsigned long)size
);
508 * Hey, Emacs, please set the following:
511 * indent-tabs-mode: nil