1 /*
2     SPDX-FileCopyrightText: 2007-2008 Robert Knight <robertknight@gmail.com>
3 
4     SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #ifndef EXTENDEDCHARTABLE_H
8 #define EXTENDEDCHARTABLE_H
9 
10 // Qt
11 #include <QHash>
12 #include <QSet>
13 
14 #include <functional>
15 
16 namespace Konsole
17 {
18 /**
19  * A table which stores sequences of unicode characters, referenced
20  * by hash keys.  The hash key itself is the same size as a unicode
21  * character ( uint ) so that it can occupy the same space in
22  * a structure.
23  */
24 class ExtendedCharTable
25 {
26 public:
27     typedef std::function<QSet<uint>()> pExtendedChars;
28 
29     /** Constructs a new character table. */
30     ExtendedCharTable();
31     ~ExtendedCharTable();
32 
33     /**
34      * Adds a sequences of unicode characters to the table and returns
35      * a hash code which can be used later to look up the sequence
36      * using lookupExtendedChar()
37      *
38      * If the same sequence already exists in the table, the hash
39      * of the existing sequence will be returned.
40      *
41      * @param unicodePoints An array of unicode character points
42      * @param length Length of @p unicodePoints
43      */
44     uint createExtendedChar(const uint *unicodePoints, ushort length, const pExtendedChars extendedChars);
45     /**
46      * Looks up and returns a pointer to a sequence of unicode characters
47      * which was added to the table using createExtendedChar().
48      *
49      * @param hash The hash key returned by createExtendedChar()
50      * @param length This variable is set to the length of the
51      * character sequence.
52      *
53      * @return A unicode character sequence of size @p length.
54      */
55     uint *lookupExtendedChar(uint hash, ushort &length) const;
56 
57     /** The global ExtendedCharTable instance. */
58     static ExtendedCharTable instance;
59 
60 private:
61     // calculates the hash key of a sequence of unicode points of size 'length'
62     uint extendedCharHash(const uint *unicodePoints, ushort length) const;
63     // tests whether the entry in the table specified by 'hash' matches the
64     // character sequence 'unicodePoints' of size 'length'
65     bool extendedCharMatch(uint hash, const uint *unicodePoints, ushort length) const;
66     // internal, maps hash keys to character sequence buffers.  The first uint
67     // in each value is the length of the buffer, followed by the uints in the buffer
68     // themselves.
69     QHash<uint, uint *> _extendedCharTable;
70 };
71 
72 }
73 
74 #endif // end of EXTENDEDCHARTABLE_H
75