File indexing completed on 2024-04-28 05:50:37

0001 /*
0002     SPDX-FileCopyrightText: 2007-2008 Robert Knight <robertknight@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #ifndef EXTENDEDCHARTABLE_H
0008 #define EXTENDEDCHARTABLE_H
0009 
0010 // Qt
0011 #include <QHash>
0012 #include <QSet>
0013 
0014 #include <functional>
0015 
0016 namespace Konsole
0017 {
0018 /**
0019  * A table which stores sequences of unicode characters, referenced
0020  * by hash keys.  The hash key itself is the same size as a unicode
0021  * character ( char32_t ) so that it can occupy the same space in
0022  * a structure.
0023  */
0024 class ExtendedCharTable
0025 {
0026 public:
0027     typedef std::function<QSet<uint>()> pExtendedChars;
0028 
0029     /** Constructs a new character table. */
0030     ExtendedCharTable();
0031     ~ExtendedCharTable();
0032 
0033     /**
0034      * Adds a sequences of unicode characters to the table and returns
0035      * a hash code which can be used later to look up the sequence
0036      * using lookupExtendedChar()
0037      *
0038      * If the same sequence already exists in the table, the hash
0039      * of the existing sequence will be returned.
0040      *
0041      * @param unicodePoints An array of unicode character points
0042      * @param length Length of @p unicodePoints
0043      */
0044     char32_t createExtendedChar(const char32_t *unicodePoints, ushort length, const pExtendedChars extendedChars);
0045     /**
0046      * Looks up and returns a pointer to a sequence of unicode characters
0047      * which was added to the table using createExtendedChar().
0048      *
0049      * @param hash The hash key returned by createExtendedChar()
0050      * @param length This variable is set to the length of the
0051      * character sequence.
0052      *
0053      * @return A unicode character sequence of size @p length.
0054      */
0055     char32_t *lookupExtendedChar(uint hash, ushort &length) const;
0056 
0057     /** The global ExtendedCharTable instance. */
0058     static ExtendedCharTable instance;
0059 
0060 private:
0061     // calculates the hash key of a sequence of unicode points of size 'length'
0062     uint extendedCharHash(const char32_t *unicodePoints, ushort length) const;
0063     // tests whether the entry in the table specified by 'hash' matches the
0064     // character sequence 'unicodePoints' of size 'length'
0065     bool extendedCharMatch(uint hash, const char32_t *unicodePoints, ushort length) const;
0066     // internal, maps hash keys to character sequence buffers.  The first uint
0067     // in each value is the length of the buffer, followed by the uints in the buffer
0068     // themselves.
0069     QHash<uint, char32_t *> _extendedCharTable;
0070 };
0071 
0072 }
0073 
0074 #endif // end of EXTENDEDCHARTABLE_H