michael@0: /* michael@0: ******************************************************************************* michael@0: * Copyright (C) 2013, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: ******************************************************************************* michael@0: * dictionarydata.h michael@0: * michael@0: * created on: 2012may31 michael@0: * created by: Markus W. Scherer & Maxime Serrano michael@0: */ michael@0: michael@0: #ifndef __DICTIONARYDATA_H__ michael@0: #define __DICTIONARYDATA_H__ michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: #if !UCONFIG_NO_BREAK_ITERATION michael@0: michael@0: #include "unicode/utext.h" michael@0: #include "unicode/udata.h" michael@0: #include "udataswp.h" michael@0: #include "unicode/uobject.h" michael@0: #include "unicode/ustringtrie.h" michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: class UCharsTrie; michael@0: class BytesTrie; michael@0: michael@0: class U_COMMON_API DictionaryData : public UMemory { michael@0: public: michael@0: static const int32_t TRIE_TYPE_BYTES; // = 0; michael@0: static const int32_t TRIE_TYPE_UCHARS; // = 1; michael@0: static const int32_t TRIE_TYPE_MASK; // = 7; michael@0: static const int32_t TRIE_HAS_VALUES; // = 8; michael@0: michael@0: static const int32_t TRANSFORM_NONE; // = 0; michael@0: static const int32_t TRANSFORM_TYPE_OFFSET; // = 0x1000000; michael@0: static const int32_t TRANSFORM_TYPE_MASK; // = 0x7f000000; michael@0: static const int32_t TRANSFORM_OFFSET_MASK; // = 0x1fffff; michael@0: michael@0: enum { michael@0: // Byte offsets from the start of the data, after the generic header. michael@0: IX_STRING_TRIE_OFFSET, michael@0: IX_RESERVED1_OFFSET, michael@0: IX_RESERVED2_OFFSET, michael@0: IX_TOTAL_SIZE, michael@0: michael@0: // Trie type: TRIE_HAS_VALUES | TRIE_TYPE_BYTES etc. michael@0: IX_TRIE_TYPE, michael@0: // Transform specification: TRANSFORM_TYPE_OFFSET | 0xe00 etc. michael@0: IX_TRANSFORM, michael@0: michael@0: IX_RESERVED6, michael@0: IX_RESERVED7, michael@0: IX_COUNT michael@0: }; michael@0: }; michael@0: michael@0: /** michael@0: * Wrapper class around generic dictionaries, implementing matches(). michael@0: * getType() should return a TRIE_TYPE_??? constant from DictionaryData. michael@0: * michael@0: * All implementations of this interface must be thread-safe if they are to be used inside of the michael@0: * dictionary-based break iteration code. michael@0: */ michael@0: class U_COMMON_API DictionaryMatcher : public UMemory { michael@0: public: michael@0: virtual ~DictionaryMatcher(); michael@0: // this should emulate CompactTrieDictionary::matches() michael@0: virtual int32_t matches(UText *text, int32_t maxLength, int32_t *lengths, int32_t &count, michael@0: int32_t limit, int32_t *values = NULL) const = 0; michael@0: /** @return DictionaryData::TRIE_TYPE_XYZ */ michael@0: virtual int32_t getType() const = 0; michael@0: }; michael@0: michael@0: // Implementation of the DictionaryMatcher interface for a UCharsTrie dictionary michael@0: class U_COMMON_API UCharsDictionaryMatcher : public DictionaryMatcher { michael@0: public: michael@0: // constructs a new UCharsDictionaryMatcher. michael@0: // The UDataMemory * will be closed on this object's destruction. michael@0: UCharsDictionaryMatcher(const UChar *c, UDataMemory *f) : characters(c), file(f) { } michael@0: virtual ~UCharsDictionaryMatcher(); michael@0: virtual int32_t matches(UText *text, int32_t maxLength, int32_t *lengths, int32_t &count, michael@0: int32_t limit, int32_t *values = NULL) const; michael@0: virtual int32_t getType() const; michael@0: private: michael@0: const UChar *characters; michael@0: UDataMemory *file; michael@0: }; michael@0: michael@0: // Implementation of the DictionaryMatcher interface for a BytesTrie dictionary michael@0: class U_COMMON_API BytesDictionaryMatcher : public DictionaryMatcher { michael@0: public: michael@0: // constructs a new BytesTrieDictionaryMatcher michael@0: // the transform constant should be the constant read from the file, not a masked version! michael@0: // the UDataMemory * fed in here will be closed on this object's destruction michael@0: BytesDictionaryMatcher(const char *c, int32_t t, UDataMemory *f) michael@0: : characters(c), transformConstant(t), file(f) { } michael@0: virtual ~BytesDictionaryMatcher(); michael@0: virtual int32_t matches(UText *text, int32_t maxLength, int32_t *lengths, int32_t &count, michael@0: int32_t limit, int32_t *values = NULL) const; michael@0: virtual int32_t getType() const; michael@0: private: michael@0: UChar32 transform(UChar32 c) const; michael@0: michael@0: const char *characters; michael@0: int32_t transformConstant; michael@0: UDataMemory *file; michael@0: }; michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: udict_swap(const UDataSwapper *ds, const void *inData, int32_t length, void *outData, UErrorCode *pErrorCode); michael@0: michael@0: /** michael@0: * Format of dictionary .dict data files. michael@0: * Format version 1.0. michael@0: * michael@0: * A dictionary .dict data file contains a byte-serialized BytesTrie or michael@0: * a UChars-serialized UCharsTrie. michael@0: * Such files are used in dictionary-based break iteration (DBBI). michael@0: * michael@0: * For a BytesTrie, a transformation type is specified for michael@0: * transforming Unicode strings into byte sequences. michael@0: * michael@0: * A .dict file begins with a standard ICU data file header michael@0: * (DataHeader, see ucmndata.h and unicode/udata.h). michael@0: * The UDataInfo.dataVersion field is currently unused (set to 0.0.0.0). michael@0: * michael@0: * After the header, the file contains the following parts. michael@0: * Constants are defined in the DictionaryData class. michael@0: * michael@0: * For the data structure of BytesTrie & UCharsTrie see michael@0: * http://site.icu-project.org/design/struct/tries michael@0: * and the bytestrie.h and ucharstrie.h header files. michael@0: * michael@0: * int32_t indexes[indexesLength]; -- indexesLength=indexes[IX_STRING_TRIE_OFFSET]/4; michael@0: * michael@0: * The first four indexes are byte offsets in ascending order. michael@0: * Each byte offset marks the start of the next part in the data file, michael@0: * and the end of the previous one. michael@0: * When two consecutive byte offsets are the same, then the corresponding part is empty. michael@0: * Byte offsets are offsets from after the header, michael@0: * that is, from the beginning of the indexes[]. michael@0: * Each part starts at an offset with proper alignment for its data. michael@0: * If necessary, the previous part may include padding bytes to achieve this alignment. michael@0: * michael@0: * trieType=indexes[IX_TRIE_TYPE] defines the trie type. michael@0: * transform=indexes[IX_TRANSFORM] defines the Unicode-to-bytes transformation. michael@0: * If the transformation type is TRANSFORM_TYPE_OFFSET, michael@0: * then the lower 21 bits contain the offset code point. michael@0: * Each code point c is mapped to byte b = (c - offset). michael@0: * Code points outside the range offset..(offset+0xff) cannot be mapped michael@0: * and do not occur in the dictionary. michael@0: * michael@0: * stringTrie; -- a serialized BytesTrie or UCharsTrie michael@0: * michael@0: * The dictionary maps strings to specific values (TRIE_HAS_VALUES bit set in trieType), michael@0: * or it maps all strings to 0 (TRIE_HAS_VALUES bit not set). michael@0: */ michael@0: michael@0: #endif /* !UCONFIG_NO_BREAK_ITERATION */ michael@0: #endif /* __DICTIONARYDATA_H__ */