12e5b6d6dSopenharmony_ci// © 2016 and later: Unicode, Inc. and others.
22e5b6d6dSopenharmony_ci// License & terms of use: http://www.unicode.org/copyright.html
32e5b6d6dSopenharmony_ci/*
42e5b6d6dSopenharmony_ci*******************************************************************************
52e5b6d6dSopenharmony_ci* Copyright (C) 2014, International Business Machines
62e5b6d6dSopenharmony_ci* Corporation and others.  All Rights Reserved.
72e5b6d6dSopenharmony_ci*******************************************************************************
82e5b6d6dSopenharmony_ci* dictionarydata.h
92e5b6d6dSopenharmony_ci*
102e5b6d6dSopenharmony_ci* created on: 2012may31
112e5b6d6dSopenharmony_ci* created by: Markus W. Scherer & Maxime Serrano
122e5b6d6dSopenharmony_ci*/
132e5b6d6dSopenharmony_ci
142e5b6d6dSopenharmony_ci#ifndef __DICTIONARYDATA_H__
152e5b6d6dSopenharmony_ci#define __DICTIONARYDATA_H__
162e5b6d6dSopenharmony_ci
172e5b6d6dSopenharmony_ci#include "unicode/utypes.h"
182e5b6d6dSopenharmony_ci
192e5b6d6dSopenharmony_ci#if !UCONFIG_NO_BREAK_ITERATION
202e5b6d6dSopenharmony_ci
212e5b6d6dSopenharmony_ci#include "unicode/utext.h"
222e5b6d6dSopenharmony_ci#include "unicode/udata.h"
232e5b6d6dSopenharmony_ci#include "udataswp.h"
242e5b6d6dSopenharmony_ci#include "unicode/uobject.h"
252e5b6d6dSopenharmony_ci#include "unicode/ustringtrie.h"
262e5b6d6dSopenharmony_ci
272e5b6d6dSopenharmony_ciU_NAMESPACE_BEGIN
282e5b6d6dSopenharmony_ci
292e5b6d6dSopenharmony_ciclass UCharsTrie;
302e5b6d6dSopenharmony_ciclass BytesTrie;
312e5b6d6dSopenharmony_ci
322e5b6d6dSopenharmony_ciclass U_COMMON_API DictionaryData : public UMemory {
332e5b6d6dSopenharmony_cipublic:
342e5b6d6dSopenharmony_ci    static const int32_t TRIE_TYPE_BYTES; // = 0;
352e5b6d6dSopenharmony_ci    static const int32_t TRIE_TYPE_UCHARS; // = 1;
362e5b6d6dSopenharmony_ci    static const int32_t TRIE_TYPE_MASK; // = 7;
372e5b6d6dSopenharmony_ci    static const int32_t TRIE_HAS_VALUES; // = 8;
382e5b6d6dSopenharmony_ci
392e5b6d6dSopenharmony_ci    static const int32_t TRANSFORM_NONE; // = 0;
402e5b6d6dSopenharmony_ci    static const int32_t TRANSFORM_TYPE_OFFSET; // = 0x1000000;
412e5b6d6dSopenharmony_ci    static const int32_t TRANSFORM_TYPE_MASK; // = 0x7f000000;
422e5b6d6dSopenharmony_ci    static const int32_t TRANSFORM_OFFSET_MASK; // = 0x1fffff;
432e5b6d6dSopenharmony_ci
442e5b6d6dSopenharmony_ci    enum {
452e5b6d6dSopenharmony_ci        // Byte offsets from the start of the data, after the generic header.
462e5b6d6dSopenharmony_ci        IX_STRING_TRIE_OFFSET,
472e5b6d6dSopenharmony_ci        IX_RESERVED1_OFFSET,
482e5b6d6dSopenharmony_ci        IX_RESERVED2_OFFSET,
492e5b6d6dSopenharmony_ci        IX_TOTAL_SIZE,
502e5b6d6dSopenharmony_ci
512e5b6d6dSopenharmony_ci        // Trie type: TRIE_HAS_VALUES | TRIE_TYPE_BYTES etc.
522e5b6d6dSopenharmony_ci        IX_TRIE_TYPE,
532e5b6d6dSopenharmony_ci        // Transform specification: TRANSFORM_TYPE_OFFSET | 0xe00 etc.
542e5b6d6dSopenharmony_ci        IX_TRANSFORM,
552e5b6d6dSopenharmony_ci
562e5b6d6dSopenharmony_ci        IX_RESERVED6,
572e5b6d6dSopenharmony_ci        IX_RESERVED7,
582e5b6d6dSopenharmony_ci        IX_COUNT
592e5b6d6dSopenharmony_ci    };
602e5b6d6dSopenharmony_ci};
612e5b6d6dSopenharmony_ci
622e5b6d6dSopenharmony_ci/**
632e5b6d6dSopenharmony_ci * Wrapper class around generic dictionaries, implementing matches().
642e5b6d6dSopenharmony_ci * getType() should return a TRIE_TYPE_??? constant from DictionaryData.
652e5b6d6dSopenharmony_ci *
662e5b6d6dSopenharmony_ci * All implementations of this interface must be thread-safe if they are to be used inside of the
672e5b6d6dSopenharmony_ci * dictionary-based break iteration code.
682e5b6d6dSopenharmony_ci */
692e5b6d6dSopenharmony_ciclass U_COMMON_API DictionaryMatcher : public UMemory {
702e5b6d6dSopenharmony_cipublic:
712e5b6d6dSopenharmony_ci    DictionaryMatcher() {}
722e5b6d6dSopenharmony_ci    virtual ~DictionaryMatcher();
732e5b6d6dSopenharmony_ci    // this should emulate CompactTrieDictionary::matches()
742e5b6d6dSopenharmony_ci    /*  @param text      The text in which to look for matching words. Matching begins
752e5b6d6dSopenharmony_ci     *                   at the current position of the UText.
762e5b6d6dSopenharmony_ci     *  @param maxLength The max length of match to consider. Units are the native indexing
772e5b6d6dSopenharmony_ci     *                   units of the UText.
782e5b6d6dSopenharmony_ci     *  @param limit     Capacity of output arrays, which is also the maximum number of
792e5b6d6dSopenharmony_ci     *                   matching words to be found.
802e5b6d6dSopenharmony_ci     *  @param lengths   output array, filled with the lengths of the matches, in order,
812e5b6d6dSopenharmony_ci     *                   from shortest to longest. Lengths are in native indexing units
822e5b6d6dSopenharmony_ci     *                   of the UText. May be NULL.
832e5b6d6dSopenharmony_ci     *  @param cpLengths output array, filled with the lengths of the matches, in order,
842e5b6d6dSopenharmony_ci     *                   from shortest to longest. Lengths are the number of Unicode code points.
852e5b6d6dSopenharmony_ci     *                   May be NULL.
862e5b6d6dSopenharmony_ci     *  @param values    Output array, filled with the values associated with the words found.
872e5b6d6dSopenharmony_ci     *                   May be NULL.
882e5b6d6dSopenharmony_ci     *  @param prefix    Output parameter, the code point length of the prefix match, even if that
892e5b6d6dSopenharmony_ci     *                   prefix didn't lead to a complete word. Will always be >= the cpLength
902e5b6d6dSopenharmony_ci     *                   of the longest complete word matched. May be NULL.
912e5b6d6dSopenharmony_ci     *  @return          Number of matching words found.
922e5b6d6dSopenharmony_ci     */
932e5b6d6dSopenharmony_ci    virtual int32_t matches(UText *text, int32_t maxLength, int32_t limit,
942e5b6d6dSopenharmony_ci                            int32_t *lengths, int32_t *cpLengths, int32_t *values,
952e5b6d6dSopenharmony_ci                            int32_t *prefix) const = 0;
962e5b6d6dSopenharmony_ci
972e5b6d6dSopenharmony_ci    /** @return DictionaryData::TRIE_TYPE_XYZ */
982e5b6d6dSopenharmony_ci    virtual int32_t getType() const = 0;
992e5b6d6dSopenharmony_ci};
1002e5b6d6dSopenharmony_ci
1012e5b6d6dSopenharmony_ci// Implementation of the DictionaryMatcher interface for a UCharsTrie dictionary
1022e5b6d6dSopenharmony_ciclass U_COMMON_API UCharsDictionaryMatcher : public DictionaryMatcher {
1032e5b6d6dSopenharmony_cipublic:
1042e5b6d6dSopenharmony_ci    // constructs a new UCharsDictionaryMatcher.
1052e5b6d6dSopenharmony_ci    // The UDataMemory * will be closed on this object's destruction.
1062e5b6d6dSopenharmony_ci    UCharsDictionaryMatcher(const UChar *c, UDataMemory *f) : characters(c), file(f) { }
1072e5b6d6dSopenharmony_ci    virtual ~UCharsDictionaryMatcher();
1082e5b6d6dSopenharmony_ci    virtual int32_t matches(UText *text, int32_t maxLength, int32_t limit,
1092e5b6d6dSopenharmony_ci                            int32_t *lengths, int32_t *cpLengths, int32_t *values,
1102e5b6d6dSopenharmony_ci                            int32_t *prefix) const override;
1112e5b6d6dSopenharmony_ci    virtual int32_t getType() const override;
1122e5b6d6dSopenharmony_ciprivate:
1132e5b6d6dSopenharmony_ci    const UChar *characters;
1142e5b6d6dSopenharmony_ci    UDataMemory *file;
1152e5b6d6dSopenharmony_ci};
1162e5b6d6dSopenharmony_ci
1172e5b6d6dSopenharmony_ci// Implementation of the DictionaryMatcher interface for a BytesTrie dictionary
1182e5b6d6dSopenharmony_ciclass U_COMMON_API BytesDictionaryMatcher : public DictionaryMatcher {
1192e5b6d6dSopenharmony_cipublic:
1202e5b6d6dSopenharmony_ci    // constructs a new BytesTrieDictionaryMatcher
1212e5b6d6dSopenharmony_ci    // the transform constant should be the constant read from the file, not a masked version!
1222e5b6d6dSopenharmony_ci    // the UDataMemory * fed in here will be closed on this object's destruction
1232e5b6d6dSopenharmony_ci    BytesDictionaryMatcher(const char *c, int32_t t, UDataMemory *f)
1242e5b6d6dSopenharmony_ci            : characters(c), transformConstant(t), file(f) { }
1252e5b6d6dSopenharmony_ci    virtual ~BytesDictionaryMatcher();
1262e5b6d6dSopenharmony_ci    virtual int32_t matches(UText *text, int32_t maxLength, int32_t limit,
1272e5b6d6dSopenharmony_ci                            int32_t *lengths, int32_t *cpLengths, int32_t *values,
1282e5b6d6dSopenharmony_ci                            int32_t *prefix) const override;
1292e5b6d6dSopenharmony_ci    virtual int32_t getType() const override;
1302e5b6d6dSopenharmony_ciprivate:
1312e5b6d6dSopenharmony_ci    UChar32 transform(UChar32 c) const;
1322e5b6d6dSopenharmony_ci
1332e5b6d6dSopenharmony_ci    const char *characters;
1342e5b6d6dSopenharmony_ci    int32_t transformConstant;
1352e5b6d6dSopenharmony_ci    UDataMemory *file;
1362e5b6d6dSopenharmony_ci};
1372e5b6d6dSopenharmony_ci
1382e5b6d6dSopenharmony_ciU_NAMESPACE_END
1392e5b6d6dSopenharmony_ci
1402e5b6d6dSopenharmony_ciU_CAPI int32_t U_EXPORT2
1412e5b6d6dSopenharmony_ciudict_swap(const UDataSwapper *ds, const void *inData, int32_t length, void *outData, UErrorCode *pErrorCode);
1422e5b6d6dSopenharmony_ci
1432e5b6d6dSopenharmony_ci/**
1442e5b6d6dSopenharmony_ci * Format of dictionary .dict data files.
1452e5b6d6dSopenharmony_ci * Format version 1.0.
1462e5b6d6dSopenharmony_ci *
1472e5b6d6dSopenharmony_ci * A dictionary .dict data file contains a byte-serialized BytesTrie or
1482e5b6d6dSopenharmony_ci * a UChars-serialized UCharsTrie.
1492e5b6d6dSopenharmony_ci * Such files are used in dictionary-based break iteration (DBBI).
1502e5b6d6dSopenharmony_ci *
1512e5b6d6dSopenharmony_ci * For a BytesTrie, a transformation type is specified for
1522e5b6d6dSopenharmony_ci * transforming Unicode strings into byte sequences.
1532e5b6d6dSopenharmony_ci *
1542e5b6d6dSopenharmony_ci * A .dict file begins with a standard ICU data file header
1552e5b6d6dSopenharmony_ci * (DataHeader, see ucmndata.h and unicode/udata.h).
1562e5b6d6dSopenharmony_ci * The UDataInfo.dataVersion field is currently unused (set to 0.0.0.0).
1572e5b6d6dSopenharmony_ci *
1582e5b6d6dSopenharmony_ci * After the header, the file contains the following parts.
1592e5b6d6dSopenharmony_ci * Constants are defined in the DictionaryData class.
1602e5b6d6dSopenharmony_ci *
1612e5b6d6dSopenharmony_ci * For the data structure of BytesTrie & UCharsTrie see
1622e5b6d6dSopenharmony_ci * https://icu.unicode.org/design/struct/tries
1632e5b6d6dSopenharmony_ci * and the bytestrie.h and ucharstrie.h header files.
1642e5b6d6dSopenharmony_ci *
1652e5b6d6dSopenharmony_ci * int32_t indexes[indexesLength]; -- indexesLength=indexes[IX_STRING_TRIE_OFFSET]/4;
1662e5b6d6dSopenharmony_ci *
1672e5b6d6dSopenharmony_ci *      The first four indexes are byte offsets in ascending order.
1682e5b6d6dSopenharmony_ci *      Each byte offset marks the start of the next part in the data file,
1692e5b6d6dSopenharmony_ci *      and the end of the previous one.
1702e5b6d6dSopenharmony_ci *      When two consecutive byte offsets are the same, then the corresponding part is empty.
1712e5b6d6dSopenharmony_ci *      Byte offsets are offsets from after the header,
1722e5b6d6dSopenharmony_ci *      that is, from the beginning of the indexes[].
1732e5b6d6dSopenharmony_ci *      Each part starts at an offset with proper alignment for its data.
1742e5b6d6dSopenharmony_ci *      If necessary, the previous part may include padding bytes to achieve this alignment.
1752e5b6d6dSopenharmony_ci *
1762e5b6d6dSopenharmony_ci *      trieType=indexes[IX_TRIE_TYPE] defines the trie type.
1772e5b6d6dSopenharmony_ci *      transform=indexes[IX_TRANSFORM] defines the Unicode-to-bytes transformation.
1782e5b6d6dSopenharmony_ci *          If the transformation type is TRANSFORM_TYPE_OFFSET,
1792e5b6d6dSopenharmony_ci *          then the lower 21 bits contain the offset code point.
1802e5b6d6dSopenharmony_ci *          Each code point c is mapped to byte b = (c - offset).
1812e5b6d6dSopenharmony_ci *          Code points outside the range offset..(offset+0xff) cannot be mapped
1822e5b6d6dSopenharmony_ci *          and do not occur in the dictionary.
1832e5b6d6dSopenharmony_ci *
1842e5b6d6dSopenharmony_ci * stringTrie; -- a serialized BytesTrie or UCharsTrie
1852e5b6d6dSopenharmony_ci *
1862e5b6d6dSopenharmony_ci *      The dictionary maps strings to specific values (TRIE_HAS_VALUES bit set in trieType),
1872e5b6d6dSopenharmony_ci *      or it maps all strings to 0 (TRIE_HAS_VALUES bit not set).
1882e5b6d6dSopenharmony_ci */
1892e5b6d6dSopenharmony_ci
1902e5b6d6dSopenharmony_ci#endif  /* !UCONFIG_NO_BREAK_ITERATION */
1912e5b6d6dSopenharmony_ci#endif  /* __DICTIONARYDATA_H__ */
192