2 *******************************************************************************
3 * Copyright (C) 2014, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 *******************************************************************************
8 * created on: 2012may31
9 * created by: Markus W. Scherer & Maxime Serrano
12 #include "dictionarydata.h"
13 #include "unicode/ucharstrie.h"
14 #include "unicode/bytestrie.h"
15 #include "unicode/udata.h"
18 #if !UCONFIG_NO_BREAK_ITERATION
22 const int32_t DictionaryData::TRIE_TYPE_BYTES
= 0;
23 const int32_t DictionaryData::TRIE_TYPE_UCHARS
= 1;
24 const int32_t DictionaryData::TRIE_TYPE_MASK
= 7;
25 const int32_t DictionaryData::TRIE_HAS_VALUES
= 8;
27 const int32_t DictionaryData::TRANSFORM_NONE
= 0;
28 const int32_t DictionaryData::TRANSFORM_TYPE_OFFSET
= 0x1000000;
29 const int32_t DictionaryData::TRANSFORM_TYPE_MASK
= 0x7f000000;
30 const int32_t DictionaryData::TRANSFORM_OFFSET_MASK
= 0x1fffff;
32 DictionaryMatcher::~DictionaryMatcher() {
35 UCharsDictionaryMatcher::~UCharsDictionaryMatcher() {
39 int32_t UCharsDictionaryMatcher::getType() const {
40 return DictionaryData::TRIE_TYPE_UCHARS
;
43 int32_t UCharsDictionaryMatcher::matches(UText
*text
, int32_t maxLength
, int32_t limit
,
44 int32_t *lengths
, int32_t *cpLengths
, int32_t *values
,
45 int32_t *prefix
) const {
47 UCharsTrie
uct(characters
);
48 int32_t startingTextIndex
= utext_getNativeIndex(text
);
49 int32_t wordCount
= 0;
50 int32_t codePointsMatched
= 0;
52 for (UChar32 c
= utext_next32(text
); c
>= 0; c
=utext_next32(text
)) {
53 UStringTrieResult result
= (codePointsMatched
== 0) ? uct
.first(c
) : uct
.next(c
);
54 int32_t lengthMatched
= utext_getNativeIndex(text
) - startingTextIndex
;
55 codePointsMatched
+= 1;
56 if (USTRINGTRIE_HAS_VALUE(result
)) {
57 if (wordCount
< limit
) {
59 values
[wordCount
] = uct
.getValue();
61 if (lengths
!= NULL
) {
62 lengths
[wordCount
] = lengthMatched
;
64 if (cpLengths
!= NULL
) {
65 cpLengths
[wordCount
] = codePointsMatched
;
69 if (result
== USTRINGTRIE_FINAL_VALUE
) {
73 else if (result
== USTRINGTRIE_NO_MATCH
) {
76 if (lengthMatched
>= maxLength
) {
82 *prefix
= codePointsMatched
;
87 BytesDictionaryMatcher::~BytesDictionaryMatcher() {
91 UChar32
BytesDictionaryMatcher::transform(UChar32 c
) const {
92 if ((transformConstant
& DictionaryData::TRANSFORM_TYPE_MASK
) == DictionaryData::TRANSFORM_TYPE_OFFSET
) {
95 } else if (c
== 0x200C) {
98 int32_t delta
= c
- (transformConstant
& DictionaryData::TRANSFORM_OFFSET_MASK
);
99 if (delta
< 0 || 0xFD < delta
) {
102 return (UChar32
)delta
;
107 int32_t BytesDictionaryMatcher::getType() const {
108 return DictionaryData::TRIE_TYPE_BYTES
;
111 int32_t BytesDictionaryMatcher::matches(UText
*text
, int32_t maxLength
, int32_t limit
,
112 int32_t *lengths
, int32_t *cpLengths
, int32_t *values
,
113 int32_t *prefix
) const {
114 BytesTrie
bt(characters
);
115 int32_t startingTextIndex
= utext_getNativeIndex(text
);
116 int32_t wordCount
= 0;
117 int32_t codePointsMatched
= 0;
119 for (UChar32 c
= utext_next32(text
); c
>= 0; c
=utext_next32(text
)) {
120 UStringTrieResult result
= (codePointsMatched
== 0) ? bt
.first(transform(c
)) : bt
.next(transform(c
));
121 int32_t lengthMatched
= utext_getNativeIndex(text
) - startingTextIndex
;
122 codePointsMatched
+= 1;
123 if (USTRINGTRIE_HAS_VALUE(result
)) {
124 if (wordCount
< limit
) {
125 if (values
!= NULL
) {
126 values
[wordCount
] = bt
.getValue();
128 if (lengths
!= NULL
) {
129 lengths
[wordCount
] = lengthMatched
;
131 if (cpLengths
!= NULL
) {
132 cpLengths
[wordCount
] = codePointsMatched
;
136 if (result
== USTRINGTRIE_FINAL_VALUE
) {
140 else if (result
== USTRINGTRIE_NO_MATCH
) {
143 if (lengthMatched
>= maxLength
) {
148 if (prefix
!= NULL
) {
149 *prefix
= codePointsMatched
;
159 U_CAPI
int32_t U_EXPORT2
160 udict_swap(const UDataSwapper
*ds
, const void *inData
, int32_t length
,
161 void *outData
, UErrorCode
*pErrorCode
) {
162 const UDataInfo
*pInfo
;
164 const uint8_t *inBytes
;
166 const int32_t *inIndexes
;
167 int32_t indexes
[DictionaryData::IX_COUNT
];
168 int32_t i
, offset
, size
;
170 headerSize
= udata_swapDataHeader(ds
, inData
, length
, outData
, pErrorCode
);
171 if (pErrorCode
== NULL
|| U_FAILURE(*pErrorCode
)) return 0;
172 pInfo
= (const UDataInfo
*)((const char *)inData
+ 4);
173 if (!(pInfo
->dataFormat
[0] == 0x44 &&
174 pInfo
->dataFormat
[1] == 0x69 &&
175 pInfo
->dataFormat
[2] == 0x63 &&
176 pInfo
->dataFormat
[3] == 0x74 &&
177 pInfo
->formatVersion
[0] == 1)) {
178 udata_printError(ds
, "udict_swap(): data format %02x.%02x.%02x.%02x (format version %02x) is not recognized as dictionary data\n",
179 pInfo
->dataFormat
[0], pInfo
->dataFormat
[1], pInfo
->dataFormat
[2], pInfo
->dataFormat
[3], pInfo
->formatVersion
[0]);
180 *pErrorCode
= U_UNSUPPORTED_ERROR
;
184 inBytes
= (const uint8_t *)inData
+ headerSize
;
185 outBytes
= (uint8_t *)outData
+ headerSize
;
187 inIndexes
= (const int32_t *)inBytes
;
189 length
-= headerSize
;
190 if (length
< (int32_t)(sizeof(indexes
))) {
191 udata_printError(ds
, "udict_swap(): too few bytes (%d after header) for dictionary data\n", length
);
192 *pErrorCode
= U_INDEX_OUTOFBOUNDS_ERROR
;
197 for (i
= 0; i
< DictionaryData::IX_COUNT
; i
++) {
198 indexes
[i
] = udata_readInt32(ds
, inIndexes
[i
]);
201 size
= indexes
[DictionaryData::IX_TOTAL_SIZE
];
205 udata_printError(ds
, "udict_swap(): too few bytes (%d after header) for all of dictionary data\n", length
);
206 *pErrorCode
= U_INDEX_OUTOFBOUNDS_ERROR
;
210 if (inBytes
!= outBytes
) {
211 uprv_memcpy(outBytes
, inBytes
, size
);
215 ds
->swapArray32(ds
, inBytes
, sizeof(indexes
), outBytes
, pErrorCode
);
216 offset
= (int32_t)sizeof(indexes
);
217 int32_t trieType
= indexes
[DictionaryData::IX_TRIE_TYPE
] & DictionaryData::TRIE_TYPE_MASK
;
218 int32_t nextOffset
= indexes
[DictionaryData::IX_RESERVED1_OFFSET
];
220 if (trieType
== DictionaryData::TRIE_TYPE_UCHARS
) {
221 ds
->swapArray16(ds
, inBytes
+ offset
, nextOffset
- offset
, outBytes
+ offset
, pErrorCode
);
222 } else if (trieType
== DictionaryData::TRIE_TYPE_BYTES
) {
225 udata_printError(ds
, "udict_swap(): unknown trie type!\n");
226 *pErrorCode
= U_UNSUPPORTED_ERROR
;
230 // these next two sections are empty in the current format,
231 // but may be used later.
233 nextOffset
= indexes
[DictionaryData::IX_RESERVED2_OFFSET
];
235 nextOffset
= indexes
[DictionaryData::IX_TOTAL_SIZE
];
238 return headerSize
+ size
;