1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 *******************************************************************************
5 * Copyright (C) 2013-2015, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 * collationinfo.cpp
9 *
10 * created on: 2013aug05
11 * created by: Markus W. Scherer
12 */
13
14 #include <stdio.h>
15 #include <string.h>
16
17 #include "unicode/utypes.h"
18
19 #if !UCONFIG_NO_COLLATION
20
21 #include "collationdata.h"
22 #include "collationdatareader.h"
23 #include "collationinfo.h"
24 #include "uassert.h"
25 #include "uvectr32.h"
26
27 U_NAMESPACE_BEGIN
28
29 void
printSizes(int32_t sizeWithHeader,const int32_t indexes[])30 CollationInfo::printSizes(int32_t sizeWithHeader, const int32_t indexes[]) {
31 int32_t totalSize = indexes[CollationDataReader::IX_TOTAL_SIZE];
32 if(sizeWithHeader > totalSize) {
33 printf(" header size: %6ld\n", (long)(sizeWithHeader - totalSize));
34 }
35
36 int32_t length = indexes[CollationDataReader::IX_INDEXES_LENGTH];
37 printf(" indexes: %6ld *4 = %6ld\n", (long)length, (long)length * 4);
38
39 length = getDataLength(indexes, CollationDataReader::IX_REORDER_CODES_OFFSET);
40 if(length != 0) {
41 printf(" reorder codes: %6ld *4 = %6ld\n", (long)length / 4, (long)length);
42 }
43
44 length = getDataLength(indexes, CollationDataReader::IX_REORDER_TABLE_OFFSET);
45 if(length != 0) {
46 U_ASSERT(length >= 256);
47 printf(" reorder table: %6ld\n", (long)length);
48 }
49
50 length = getDataLength(indexes, CollationDataReader::IX_TRIE_OFFSET);
51 if(length != 0) {
52 printf(" trie size: %6ld\n", (long)length);
53 }
54
55 length = getDataLength(indexes, CollationDataReader::IX_RESERVED8_OFFSET);
56 if(length != 0) {
57 printf(" reserved (offset 8): %6ld\n", (long)length);
58 }
59
60 length = getDataLength(indexes, CollationDataReader::IX_CES_OFFSET);
61 if(length != 0) {
62 printf(" CEs: %6ld *8 = %6ld\n", (long)length / 8, (long)length);
63 }
64
65 length = getDataLength(indexes, CollationDataReader::IX_RESERVED10_OFFSET);
66 if(length != 0) {
67 printf(" reserved (offset 10): %6ld\n", (long)length);
68 }
69
70 length = getDataLength(indexes, CollationDataReader::IX_CE32S_OFFSET);
71 if(length != 0) {
72 printf(" CE32s: %6ld *4 = %6ld\n", (long)length / 4, (long)length);
73 }
74
75 length = getDataLength(indexes, CollationDataReader::IX_ROOT_ELEMENTS_OFFSET);
76 if(length != 0) {
77 printf(" rootElements: %6ld *4 = %6ld\n", (long)length / 4, (long)length);
78 }
79
80 length = getDataLength(indexes, CollationDataReader::IX_CONTEXTS_OFFSET);
81 if(length != 0) {
82 printf(" contexts: %6ld *2 = %6ld\n", (long)length / 2, (long)length);
83 }
84
85 length = getDataLength(indexes, CollationDataReader::IX_UNSAFE_BWD_OFFSET);
86 if(length != 0) {
87 printf(" unsafeBwdSet: %6ld *2 = %6ld\n", (long)length / 2, (long)length);
88 }
89
90 length = getDataLength(indexes, CollationDataReader::IX_FAST_LATIN_TABLE_OFFSET);
91 if(length != 0) {
92 printf(" fastLatin table: %6ld *2 = %6ld\n", (long)length / 2, (long)length);
93 }
94
95 length = getDataLength(indexes, CollationDataReader::IX_SCRIPTS_OFFSET);
96 if(length != 0) {
97 printf(" scripts data: %6ld *2 = %6ld\n", (long)length / 2, (long)length);
98 }
99
100 length = getDataLength(indexes, CollationDataReader::IX_COMPRESSIBLE_BYTES_OFFSET);
101 if(length != 0) {
102 U_ASSERT(length >= 256);
103 printf(" compressibleBytes: %6ld\n", (long)length);
104 }
105
106 length = getDataLength(indexes, CollationDataReader::IX_RESERVED18_OFFSET);
107 if(length != 0) {
108 printf(" reserved (offset 18): %6ld\n", (long)length);
109 }
110
111 printf(" collator binary total size: %6ld\n", (long)sizeWithHeader);
112 }
113
114 int32_t
getDataLength(const int32_t indexes[],int32_t startIndex)115 CollationInfo::getDataLength(const int32_t indexes[], int32_t startIndex) {
116 return indexes[startIndex + 1] - indexes[startIndex];
117 }
118
119 void
printReorderRanges(const CollationData & data,const int32_t * codes,int32_t length)120 CollationInfo::printReorderRanges(const CollationData &data, const int32_t *codes, int32_t length) {
121 UErrorCode errorCode = U_ZERO_ERROR;
122 UVector32 ranges(errorCode);
123 data.makeReorderRanges(codes, length, ranges, errorCode);
124 if(U_FAILURE(errorCode)) {
125 printf(" error building reorder ranges: %s\n", u_errorName(errorCode));
126 return;
127 }
128
129 int32_t start = 0;
130 for(int32_t i = 0; i < ranges.size(); ++i) {
131 int32_t pair = ranges.elementAti(i);
132 int32_t limit = (pair >> 16) & 0xffff;
133 int16_t offset = (int16_t)pair;
134 if(offset == 0) {
135 // [inclusive-start, exclusive-limit[
136 printf(" [%04x, %04x[\n", start, limit);
137 } else if(offset > 0) {
138 printf(" reorder [%04x, %04x[ by offset %02x to [%04x, %04x[\n",
139 start, limit, offset,
140 start + (offset << 8), limit + (offset << 8));
141 } else /* offset < 0 */ {
142 printf(" reorder [%04x, %04x[ by offset -%02x to [%04x, %04x[\n",
143 start, limit, -offset,
144 start + (offset << 8), limit + (offset << 8));
145 }
146 start = limit;
147 }
148 }
149
150 U_NAMESPACE_END
151
152 #endif // !UCONFIG_NO_COLLATION
153