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 #ifndef __DICTIONARYDATA_H__
13 #define __DICTIONARYDATA_H__
15 #include "unicode/utypes.h"
17 #if !UCONFIG_NO_BREAK_ITERATION
19 #include "unicode/utext.h"
20 #include "unicode/udata.h"
22 #include "unicode/uobject.h"
23 #include "unicode/ustringtrie.h"
30 class U_COMMON_API DictionaryData
: public UMemory
{
32 static const int32_t TRIE_TYPE_BYTES
; // = 0;
33 static const int32_t TRIE_TYPE_UCHARS
; // = 1;
34 static const int32_t TRIE_TYPE_MASK
; // = 7;
35 static const int32_t TRIE_HAS_VALUES
; // = 8;
37 static const int32_t TRANSFORM_NONE
; // = 0;
38 static const int32_t TRANSFORM_TYPE_OFFSET
; // = 0x1000000;
39 static const int32_t TRANSFORM_TYPE_MASK
; // = 0x7f000000;
40 static const int32_t TRANSFORM_OFFSET_MASK
; // = 0x1fffff;
43 // Byte offsets from the start of the data, after the generic header.
44 IX_STRING_TRIE_OFFSET
,
49 // Trie type: TRIE_HAS_VALUES | TRIE_TYPE_BYTES etc.
51 // Transform specification: TRANSFORM_TYPE_OFFSET | 0xe00 etc.
61 * Wrapper class around generic dictionaries, implementing matches().
62 * getType() should return a TRIE_TYPE_??? constant from DictionaryData.
64 * All implementations of this interface must be thread-safe if they are to be used inside of the
65 * dictionary-based break iteration code.
67 class U_COMMON_API DictionaryMatcher
: public UMemory
{
69 DictionaryMatcher() {};
70 virtual ~DictionaryMatcher();
71 // this should emulate CompactTrieDictionary::matches()
72 /* @param text The text in which to look for matching words. Matching begins
73 * at the current position of the UText.
74 * @param maxLength The max length of match to consider. Units are the native indexing
76 * @param limit Capacity of output arrays, which is also the maximum number of
77 * matching words to be found.
78 * @param lengths output array, filled with the lengths of the matches, in order,
79 * from shortest to longest. Lengths are in native indexing units
80 * of the UText. May be NULL.
81 * @param cpLengths output array, filled with the lengths of the matches, in order,
82 * from shortest to longest. Lengths are the number of Unicode code points.
84 * @param values Output array, filled with the values associated with the words found.
86 * @param prefix Output parameter, the code point length of the prefix match, even if that
87 * prefix didn't lead to a complete word. Will always be >= the cpLength
88 * of the longest complete word matched. May be NULL.
89 * @return Number of matching words found.
91 virtual int32_t matches(UText
*text
, int32_t maxLength
, int32_t limit
,
92 int32_t *lengths
, int32_t *cpLengths
, int32_t *values
,
93 int32_t *prefix
) const = 0;
95 /** @return DictionaryData::TRIE_TYPE_XYZ */
96 virtual int32_t getType() const = 0;
99 // Implementation of the DictionaryMatcher interface for a UCharsTrie dictionary
100 class U_COMMON_API UCharsDictionaryMatcher
: public DictionaryMatcher
{
102 // constructs a new UCharsDictionaryMatcher.
103 // The UDataMemory * will be closed on this object's destruction.
104 UCharsDictionaryMatcher(const UChar
*c
, UDataMemory
*f
) : characters(c
), file(f
) { }
105 virtual ~UCharsDictionaryMatcher();
106 virtual int32_t matches(UText
*text
, int32_t maxLength
, int32_t limit
,
107 int32_t *lengths
, int32_t *cpLengths
, int32_t *values
,
108 int32_t *prefix
) const;
109 virtual int32_t getType() const;
111 const UChar
*characters
;
115 // Implementation of the DictionaryMatcher interface for a BytesTrie dictionary
116 class U_COMMON_API BytesDictionaryMatcher
: public DictionaryMatcher
{
118 // constructs a new BytesTrieDictionaryMatcher
119 // the transform constant should be the constant read from the file, not a masked version!
120 // the UDataMemory * fed in here will be closed on this object's destruction
121 BytesDictionaryMatcher(const char *c
, int32_t t
, UDataMemory
*f
)
122 : characters(c
), transformConstant(t
), file(f
) { }
123 virtual ~BytesDictionaryMatcher();
124 virtual int32_t matches(UText
*text
, int32_t maxLength
, int32_t limit
,
125 int32_t *lengths
, int32_t *cpLengths
, int32_t *values
,
126 int32_t *prefix
) const;
127 virtual int32_t getType() const;
129 UChar32
transform(UChar32 c
) const;
131 const char *characters
;
132 int32_t transformConstant
;
138 U_CAPI
int32_t U_EXPORT2
139 udict_swap(const UDataSwapper
*ds
, const void *inData
, int32_t length
, void *outData
, UErrorCode
*pErrorCode
);
142 * Format of dictionary .dict data files.
143 * Format version 1.0.
145 * A dictionary .dict data file contains a byte-serialized BytesTrie or
146 * a UChars-serialized UCharsTrie.
147 * Such files are used in dictionary-based break iteration (DBBI).
149 * For a BytesTrie, a transformation type is specified for
150 * transforming Unicode strings into byte sequences.
152 * A .dict file begins with a standard ICU data file header
153 * (DataHeader, see ucmndata.h and unicode/udata.h).
154 * The UDataInfo.dataVersion field is currently unused (set to 0.0.0.0).
156 * After the header, the file contains the following parts.
157 * Constants are defined in the DictionaryData class.
159 * For the data structure of BytesTrie & UCharsTrie see
160 * http://site.icu-project.org/design/struct/tries
161 * and the bytestrie.h and ucharstrie.h header files.
163 * int32_t indexes[indexesLength]; -- indexesLength=indexes[IX_STRING_TRIE_OFFSET]/4;
165 * The first four indexes are byte offsets in ascending order.
166 * Each byte offset marks the start of the next part in the data file,
167 * and the end of the previous one.
168 * When two consecutive byte offsets are the same, then the corresponding part is empty.
169 * Byte offsets are offsets from after the header,
170 * that is, from the beginning of the indexes[].
171 * Each part starts at an offset with proper alignment for its data.
172 * If necessary, the previous part may include padding bytes to achieve this alignment.
174 * trieType=indexes[IX_TRIE_TYPE] defines the trie type.
175 * transform=indexes[IX_TRANSFORM] defines the Unicode-to-bytes transformation.
176 * If the transformation type is TRANSFORM_TYPE_OFFSET,
177 * then the lower 21 bits contain the offset code point.
178 * Each code point c is mapped to byte b = (c - offset).
179 * Code points outside the range offset..(offset+0xff) cannot be mapped
180 * and do not occur in the dictionary.
182 * stringTrie; -- a serialized BytesTrie or UCharsTrie
184 * The dictionary maps strings to specific values (TRIE_HAS_VALUES bit set in trieType),
185 * or it maps all strings to 0 (TRIE_HAS_VALUES bit not set).
188 #endif /* !UCONFIG_NO_BREAK_ITERATION */
189 #endif /* __DICTIONARYDATA_H__ */