File indexing completed on 2018-12-11 17:32:05

0001 /*
0002     This file is part of Konsole, an X terminal.
0003 
0004     Copyright 2007-2008 by Robert Knight <robertknight@gmail.com>
0005 
0006     This program is free software; you can redistribute it and/or modify
0007     it under the terms of the GNU General Public License as published by
0008     the Free Software Foundation; either version 2 of the License, or
0009     (at your option) any later version.
0010 
0011     This program is distributed in the hope that it will be useful,
0012     but WITHOUT ANY WARRANTY; without even the implied warranty of
0013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014     GNU General Public License for more details.
0015 
0016     You should have received a copy of the GNU General Public License
0017     along with this program; if not, write to the Free Software
0018     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0019     02110-1301  USA.
0020 */
0021 
0022 #ifndef EXTENDEDCHARTABLE_H
0023 #define EXTENDEDCHARTABLE_H
0024 
0025 // Qt
0026 #include <QHash>
0027 
0028 namespace Konsole {
0029 /**
0030  * A table which stores sequences of unicode characters, referenced
0031  * by hash keys.  The hash key itself is the same size as a unicode
0032  * character ( uint ) so that it can occupy the same space in
0033  * a structure.
0034  */
0035 class ExtendedCharTable
0036 {
0037 public:
0038     /** Constructs a new character table. */
0039     ExtendedCharTable();
0040     ~ExtendedCharTable();
0041 
0042     /**
0043      * Adds a sequences of unicode characters to the table and returns
0044      * a hash code which can be used later to look up the sequence
0045      * using lookupExtendedChar()
0046      *
0047      * If the same sequence already exists in the table, the hash
0048      * of the existing sequence will be returned.
0049      *
0050      * @param unicodePoints An array of unicode character points
0051      * @param length Length of @p unicodePoints
0052      */
0053     uint createExtendedChar(const uint *unicodePoints, ushort length);
0054     /**
0055      * Looks up and returns a pointer to a sequence of unicode characters
0056      * which was added to the table using createExtendedChar().
0057      *
0058      * @param hash The hash key returned by createExtendedChar()
0059      * @param length This variable is set to the length of the
0060      * character sequence.
0061      *
0062      * @return A unicode character sequence of size @p length.
0063      */
0064     uint *lookupExtendedChar(uint hash, ushort &length) const;
0065 
0066     /** The global ExtendedCharTable instance. */
0067     static ExtendedCharTable instance;
0068 private:
0069     // calculates the hash key of a sequence of unicode points of size 'length'
0070     uint extendedCharHash(const uint *unicodePoints, ushort length) const;
0071     // tests whether the entry in the table specified by 'hash' matches the
0072     // character sequence 'unicodePoints' of size 'length'
0073     bool extendedCharMatch(uint hash, const uint *unicodePoints, ushort length) const;
0074     // internal, maps hash keys to character sequence buffers.  The first uint
0075     // in each value is the length of the buffer, followed by the uints in the buffer
0076     // themselves.
0077     QHash<uint, uint *> _extendedCharTable;
0078 };
0079 }
0080 #endif  // end of EXTENDEDCHARTABLE_H