2 *******************************************************************************
4 * Copyright (C) 1999-2006, 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 4 .
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.
56 The contents is a parsed, binary form of several Unicode character
57 database files, most prominently UnicodeData.txt.
59 Any Unicode code point from 0 to 0x10ffff can be looked up to get
60 the properties, if any, for that code point. This means that the input
61 to the lookup are 21-bit unsigned integers, with not all of the
64 It is assumed that client code keeps a uint32_t pointer
65 to the beginning of the data:
69 Formally, the file contains the following structures:
71 const int32_t indexes[16] with values i0..i15:
73 i0 indicates the length of the main trie.
74 i0..i3 all have the same value in format version 4.0;
75 the related props32[] and exceptions[] and uchars[] were used in format version 3
77 i0 propsIndex; -- 32-bit unit index to the table of 32-bit properties words
78 i1 exceptionsIndex; -- 32-bit unit index to the table of 32-bit exception words
79 i2 exceptionsTopIndex; -- 32-bit unit index to the array of UChars for special mappings
81 i3 additionalTrieIndex; -- 32-bit unit index to the additional trie for more properties
82 i4 additionalVectorsIndex; -- 32-bit unit index to the table of properties vectors
83 i5 additionalVectorsColumns; -- number of 32-bit words per properties vector
85 i6 reservedItemIndex; -- 32-bit unit index to the top of the properties vectors table
86 i7..i9 reservedIndexes; -- reserved values; 0 for now
88 i10 maxValues; -- maximum code values for vector word 0, see uprops.h (new in format version 3.1+)
89 i11 maxValues2; -- maximum code values for vector word 2, see uprops.h (new in format version 3.2)
90 i12..i15 reservedIndexes; -- reserved values; 0 for now
92 PT serialized properties trie, see utrie.h (byte size: 4*(i0-16))
94 P, E, and U are not used (empty) in format version 4
96 P const uint32_t props32[i1-i0];
97 E const uint32_t exceptions[i2-i1];
98 U const UChar uchars[2*(i3-i2)];
100 AT serialized trie for additional properties (byte size: 4*(i4-i3))
101 PV const uint32_t propsVectors[(i6-i4)/i5][i5]==uint32_t propsVectors[i6-i4];
103 Trie lookup and properties:
105 In order to condense the data for the 21-bit code space, several properties of
106 the Unicode code assignment are exploited:
107 - The code space is sparse.
108 - There are several 10k of consecutive codes with the same properties.
109 - Characters and scripts are allocated in groups of 16 code points.
110 - Inside blocks for scripts the properties are often repetitive.
111 - The 21-bit space is not fully used for Unicode.
113 The lookup of properties for a given code point is done with a trie lookup,
114 using the UTrie implementation.
115 The trie lookup result is a 16-bit properties word.
117 With a given Unicode code point
121 and 0<=c<0x110000, the lookup is done like this:
124 UTRIE_GET16(trie, c, props);
126 Each 16-bit properties word contains:
128 0.. 4 general category
130 non-digit numbers are stored with multiple types and pseudo-types
131 in order to facilitate compact encoding:
132 0 no numeric value (0)
133 1 decimal digit value (0..9)
135 3 (U_NT_NUMERIC) normal non-digit numeric value 0..0xff
136 4 (internal type UPROPS_NT_FRACTION) fraction
137 5 (internal type UPROPS_NT_LARGE) large number >0xff
140 when returning the numeric type from a public API,
141 internal types must be turned into U_NT_NUMERIC
144 encoding of fractions and large numbers see below
147 // n is the 8-bit numeric value from bits 8..15 of the trie word (shifted down)
149 num=n>>3; // num=0..31
150 den=(n&7)+2; // den=2..9
152 num=-1; // num=-1 or 1..31
154 double result=(double)num/(double)den;
157 // n is the 8-bit numeric value from bits 8..15 of the trie word (shifted down)
162 m=1; // for large powers of 10
166 } // m==10..15 are reserved
167 double result=(double)m*10^e;
169 --- Additional properties (new in format version 2.1) ---
171 The second trie for additional properties (AT) is also a UTrie with 16-bit data.
172 The data words consist of 32-bit unit indexes (not row indexes!) into the
173 table of unique properties vectors (PV).
174 Each vector contains a set of properties.
175 The width of a vector (number of uint32_t per row) may change
176 with the formatVersion, it is stored in i5.
178 Current properties: see icu/source/common/uprops.h
180 --- Changes in format version 3.1 ---
182 See i10 maxValues above, contains only UBLOCK_COUNT and USCRIPT_CODE_LIMIT.
184 --- Changes in format version 3.2 ---
186 - The tries use linear Latin-1 ranges.
187 - The additional properties bits store full properties XYZ instead
188 of partial Other_XYZ, so that changes in the derivation formulas
189 need not be tracked in runtime library code.
190 - Joining Type and Line Break are also stored completely, so that uprops.c
191 needs no runtime formulas for enumerated properties either.
192 - Store the case-sensitive flag in the main properties word.
193 - i10 also contains U_LB_COUNT and U_EA_COUNT.
194 - i11 contains maxValues2 for vector word 2.
196 --- Changes in format version 4 ---
198 The format changes between version 3 and 4 because the properties related to
199 case mappings and bidi/shaping are pulled out into separate files
201 In order to reduce the need for code changes, some of the previous data
202 structures are omitted, rather than rearranging everything.
204 (The change to format version 4 is for ICU 3.4. The last CVS revision of
205 genprops/store.c for format version 3.2 is 1.48.)
207 The main trie's data is significantly simplified:
208 - The trie's 16-bit data word is used directly instead of as an index
210 - The trie uses the default trie folding functions instead of custom ones.
211 - Numeric values are stored directly in the trie data word, with special
213 - No more exception data (the data that needed it was pulled out, or, in the
214 case of numeric values, encoded differently).
215 - No more string data (pulled out - was for case mappings).
217 Also, some of the previously used properties vector bits are reserved again.
219 The indexes[] values for the omitted structures are still filled in
220 (indicating zero-length arrays) so that the swapper code remains unchanged.
222 ----------------------------------------------------------------------------- */
224 /* UDataInfo cf. udata.h */
225 static UDataInfo dataInfo
={
234 { 0x55, 0x50, 0x72, 0x6f }, /* dataFormat="UPro" */
235 { 4, 0, UTRIE_SHIFT
, UTRIE_INDEX_SHIFT
}, /* formatVersion */
236 { 4, 0, 1, 0 } /* dataVersion */
239 static UNewTrie
*pTrie
=NULL
;
241 /* -------------------------------------------------------------------------- */
244 setUnicodeVersion(const char *v
) {
245 UVersionInfo version
;
246 u_versionFromString(version
, v
);
247 uprv_memcpy(dataInfo
.dataVersion
, version
, 4);
252 pTrie
=utrie_open(NULL
, NULL
, 40000, 0, 0, TRUE
);
254 fprintf(stderr
, "error: unable to create a UNewTrie\n");
255 exit(U_MEMORY_ALLOCATION_ERROR
);
258 initAdditionalProperties();
264 exitAdditionalProperties();
267 static uint32_t printNumericTypeValueError(Props
*p
) {
268 fprintf(stderr
, "genprops error: unable to encode numeric type & value %d %ld/%lu E%d\n",
269 (int)p
->numericType
, (long)p
->numericValue
, (unsigned long)p
->denominator
, p
->exponent
);
270 exit(U_ILLEGAL_ARGUMENT_ERROR
);
274 /* store a character's properties ------------------------------------------- */
277 makeProps(Props
*p
) {
279 int32_t type
, value
, exp
;
281 /* encode numeric type & value */
283 value
=p
->numericValue
;
289 if( type
!=U_NT_NUMERIC
||
290 value
<-1 || value
==0 || value
>UPROPS_FRACTION_MAX_NUM
||
291 den
<UPROPS_FRACTION_MIN_DEN
|| UPROPS_FRACTION_MAX_DEN
<den
||
294 return printNumericTypeValueError(p
);
296 type
=UPROPS_NT_FRACTION
;
301 den
-=UPROPS_FRACTION_DEN_OFFSET
;
302 value
=(value
<<UPROPS_FRACTION_NUM_SHIFT
)|den
;
304 /* very large value */
305 if( type
!=U_NT_NUMERIC
||
306 value
<1 || 9<value
||
307 exp
<UPROPS_LARGE_MIN_EXP
|| UPROPS_LARGE_MAX_EXP_EXTRA
<exp
309 return printNumericTypeValueError(p
);
311 type
=UPROPS_NT_LARGE
;
313 if(exp
<=UPROPS_LARGE_MAX_EXP
) {
314 /* 1..9 * 10^(2..17) */
315 exp
-=UPROPS_LARGE_EXP_OFFSET
;
317 /* 1 * 10^(18..33) */
319 return printNumericTypeValueError(p
);
322 exp
-=UPROPS_LARGE_EXP_OFFSET_EXTRA
;
324 value
=(value
<<UPROPS_LARGE_MANT_SHIFT
)|exp
;
325 } else if(value
>UPROPS_MAX_SMALL_NUMBER
) {
327 if(type
!=U_NT_NUMERIC
) {
328 return printNumericTypeValueError(p
);
330 type
=UPROPS_NT_LARGE
;
332 /* split the value into mantissa and exponent, base 10 */
333 while((value%10
)==0) {
338 return printNumericTypeValueError(p
);
341 exp
-=UPROPS_LARGE_EXP_OFFSET
;
342 value
=(value
<<UPROPS_LARGE_MANT_SHIFT
)|exp
;
344 /* unable to encode negative values, other than fractions -1/x */
345 return printNumericTypeValueError(p
);
347 /* } else normal value=0..0xff { */
350 /* encode the properties */
352 (uint32_t)p
->generalCategory
|
353 ((uint32_t)type
<<UPROPS_NUMERIC_TYPE_SHIFT
) |
354 ((uint32_t)value
<<UPROPS_NUMERIC_VALUE_SHIFT
);
358 addProps(uint32_t c
, uint32_t x
) {
359 if(!utrie_set32(pTrie
, (UChar32
)c
, x
)) {
360 fprintf(stderr
, "error: too many entries for the properties trie\n");
361 exit(U_BUFFER_OVERFLOW_ERROR
);
366 getProps(uint32_t c
) {
367 return utrie_get32(pTrie
, (UChar32
)c
, NULL
);
370 /* areas of same properties ------------------------------------------------- */
373 repeatProps(uint32_t first
, uint32_t last
, uint32_t x
) {
374 if(!utrie_setRange32(pTrie
, (UChar32
)first
, (UChar32
)(last
+1), x
, FALSE
)) {
375 fprintf(stderr
, "error: too many entries for the properties trie\n");
376 exit(U_BUFFER_OVERFLOW_ERROR
);
380 /* generate output data ----------------------------------------------------- */
383 generateData(const char *dataDir
, UBool csource
) {
384 static int32_t indexes
[UPROPS_INDEX_COUNT
]={
390 static uint8_t trieBlock
[40000];
391 static uint8_t additionalProps
[120000];
393 UNewDataMemory
*pData
;
394 UErrorCode errorCode
=U_ZERO_ERROR
;
396 int32_t trieSize
, additionalPropsSize
, offset
;
399 trieSize
=utrie_serialize(pTrie
, trieBlock
, sizeof(trieBlock
), NULL
, TRUE
, &errorCode
);
400 if(U_FAILURE(errorCode
)) {
401 fprintf(stderr
, "error: utrie_serialize failed: %s (length %ld)\n", u_errorName(errorCode
), (long)trieSize
);
405 offset
=sizeof(indexes
)/4; /* uint32_t offset to the properties trie */
407 /* round up trie size to 4-alignment */
408 trieSize
=(trieSize
+3)&~3;
410 indexes
[UPROPS_PROPS32_INDEX
]= /* set indexes to the same offsets for empty */
411 indexes
[UPROPS_EXCEPTIONS_INDEX
]= /* structures from the old format version 3 */
412 indexes
[UPROPS_EXCEPTIONS_TOP_INDEX
]= /* so that less runtime code has to be changed */
413 indexes
[UPROPS_ADDITIONAL_TRIE_INDEX
]=offset
;
416 printf("trie size in bytes: %5u\n", (int)trieSize
);
420 /* write .c file for hardcoded data */
424 utrie_unserialize(&trie
, trieBlock
, trieSize
, &errorCode
);
425 if(U_FAILURE(errorCode
)) {
428 "genprops error: failed to utrie_unserialize(uprops.icu main trie) - %s\n",
429 u_errorName(errorCode
));
433 f
=usrc_create(dataDir
, "uchar_props_data.c");
436 "static const UVersionInfo formatVersion={",
437 dataInfo
.formatVersion
, 8, 4,
440 "static const UVersionInfo dataVersion={",
441 dataInfo
.dataVersion
, 8, 4,
443 usrc_writeUTrieArrays(f
,
444 "static const uint16_t propsTrie_index[%ld]={\n", NULL
,
447 usrc_writeUTrieStruct(f
,
448 "static const UTrie propsTrie={\n",
449 &trie
, "propsTrie_index", NULL
, NULL
,
452 additionalPropsSize
=writeAdditionalData(f
, additionalProps
, sizeof(additionalProps
), indexes
);
453 size
=4*offset
+additionalPropsSize
; /* total size of data */
456 "static const int32_t indexes[UPROPS_INDEX_COUNT]={",
457 indexes
, 32, UPROPS_INDEX_COUNT
,
463 pData
=udata_create(dataDir
, DATA_TYPE
, DATA_NAME
, &dataInfo
,
464 haveCopyright
? U_COPYRIGHT_STRING
: NULL
, &errorCode
);
465 if(U_FAILURE(errorCode
)) {
466 fprintf(stderr
, "genprops: unable to create data memory, %s\n", u_errorName(errorCode
));
470 additionalPropsSize
=writeAdditionalData(NULL
, additionalProps
, sizeof(additionalProps
), indexes
);
471 size
=4*offset
+additionalPropsSize
; /* total size of data */
473 udata_writeBlock(pData
, indexes
, sizeof(indexes
));
474 udata_writeBlock(pData
, trieBlock
, trieSize
);
475 udata_writeBlock(pData
, additionalProps
, additionalPropsSize
);
478 dataLength
=udata_finish(pData
, &errorCode
);
479 if(U_FAILURE(errorCode
)) {
480 fprintf(stderr
, "genprops: error %d writing the output file\n", errorCode
);
484 if(dataLength
!=(long)size
) {
485 fprintf(stderr
, "genprops: data length %ld != calculated size %lu\n",
486 dataLength
, (unsigned long)size
);
487 exit(U_INTERNAL_PROGRAM_ERROR
);
492 printf("data size: %6lu\n", (unsigned long)size
);
497 * Hey, Emacs, please set the following:
500 * indent-tabs-mode: nil