File: indexedstring.h

package info (click to toggle)
kdevelop 4%3A22.12.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 70,096 kB
  • sloc: cpp: 284,635; javascript: 3,558; python: 3,422; sh: 1,319; ansic: 685; xml: 331; php: 95; lisp: 66; makefile: 39; sed: 12
file content (258 lines) | stat: -rw-r--r-- 7,922 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
    SPDX-FileCopyrightText: 2008 David Nolden <david.nolden.kdevelop@art-master.de>

    SPDX-License-Identifier: GPL-2.0-or-later
*/

#ifndef KDEVPLATFORM_INDEXED_STRING_H
#define KDEVPLATFORM_INDEXED_STRING_H

//krazy:excludeall=dpointer,inline

#include <QMetaType>
#include <QStringView>
#include <QUrl>

#include "referencecounting.h"

#include "serializationexport.h"

namespace KDevelop {
/**
 * This string does "disk reference-counting", which means that reference-counts are maintainted,
 * but only when the string is in a disk-stored location. The file referencecounting.h is used
 * to manage this condition.
 *
 * Whenever reference-counting is enabled for a range that contains the IndexedString, it will
 * manipulate the reference-counts.
 *
 * The duchain storage mechanisms automatically are about correctly managing that condition,
 * so you don't need to care, and can just use this class in every duchain data type without
 * restrictions.
 *
 * @warning Do not use IndexedString after QCoreApplication::aboutToQuit() has been emitted,
 * items that are not disk-referenced will be invalid at that point.
 *
 * @note Empty strings have an index of zero.
 *
 * @note Strings of length one are not put into the repository, but are encoded directly within
 * the index: They are encoded like @c 0xffff00bb where @c bb is the byte of the character.
 *
 * @note Move constructor and move assignment operator are deliberately not implemented for
 * IndexedString. The move operations are tricky to implement correctly and more efficiently
 * in practice than the copy operations, seeing that more than 99% of arguments of the copy/move
 * operations are not disk-reference-counted. Moreover, according to test runs at the time of
 * this writing, the copied- or moved-from IndexedString is never disk-reference-counted in
 * practice, so the moved-from string's reference count cannot be stolen. IndexedString's copy
 * constructor and copy assignment operator are noexcept to allow noexcept move operations in
 * classes that contain IndexedString as a data member.
 */
class KDEVPLATFORMSERIALIZATION_EXPORT IndexedString
{
public:
    IndexedString() = default;
    /**
     * @param str must be a utf8 encoded string, does not need to be 0-terminated.
     * @param length must be its length in bytes.
     * @param hash must be a hash as constructed with the here defined hash functions.
     *             If it is zero, it will be computed.
     */
    explicit IndexedString(const char* str, unsigned short length, unsigned int hash = 0);

    /**
     * Needs a zero terminated string. When the information is already available,
     * try using the other constructor.
     *
     * WARNING There is a UTF8-related issue when attempting to retrieve the string
     * using str from an IndexedString built from this constructor
     */
    explicit IndexedString(const char* str);

    explicit IndexedString(char c);

    explicit IndexedString(bool) = delete;

    /**
     * When the information is already available, try using the other constructor.
     *
     * @note This is expensive.
     */
    explicit IndexedString(QStringView str);
    explicit IndexedString(const QString& str)
        : IndexedString(QStringView{str})
    {
    }

    /**
     * When the information is already available, try using the other constructor.
     *
     * @note This is expensive.
     */
    explicit IndexedString(const QByteArray& str);

    /**
     * Returns a not reference-counted IndexedString that represents the given index.
     *
     * @warning It is dangerous dealing with indices directly, because it may break
     *          the reference counting logic. Never store pure indices to disk.
     */
    static IndexedString fromIndex(unsigned int index)
    {
        IndexedString ret;
        ret.m_index = index;
        return ret;
    }

    /**
     * @warning This is relatively expensive: needs a mutex lock, hash lookups, and eventual loading,
     *       so avoid it when possible.
     */
    static int lengthFromIndex(unsigned int index);

    IndexedString(const IndexedString&) noexcept;

    ~IndexedString();

    /**
     * Creates an indexed string from a QUrl, this is expensive.
     */
    explicit IndexedString(const QUrl& url);

    /**
     * Re-construct a QUrl from this indexed string, the result can be used with the
     * QUrl-using constructor.
     *
     * @note This is expensive.
     */
    QUrl toUrl() const;

    inline unsigned int hash() const
    {
        return m_index;
    }

    /**
     * The string is uniquely identified by this index. You can use it for comparison.
     *
     * @warning It is dangerous dealing with indices directly, because it may break the
     *          reference counting logic. never store pure indices to disk
     */
    inline unsigned int index() const
    {
        return m_index;
    }

    bool isEmpty() const
    {
        return m_index == 0;
    }

    /**
     * @note This is relatively expensive: needs a mutex lock, hash lookups, and eventual loading,
     * so avoid it when possible.
     */
    int length() const;

    /**
     * Returns the underlying c string, in utf-8 encoding.
     *
     * @warning The string is not 0-terminated, consider length()!
     */
    const char* c_str() const;

    /**
     * Convenience function, avoid using it, it's relatively expensive
     */
    QString str() const;

    /**
     * Convenience function, avoid using it, it's relatively expensive (less expensive then str() though)
     */
    QByteArray byteArray() const;

    IndexedString& operator=(const IndexedString&) noexcept;

    friend KDEVPLATFORMSERIALIZATION_EXPORT void swap(IndexedString&, IndexedString&) noexcept;

    /**
     * Fast index-based comparison
     */
    bool operator ==(const IndexedString& rhs) const
    {
        return m_index == rhs.m_index;
    }

    /**
     * Fast index-based comparison
     */
    bool operator !=(const IndexedString& rhs) const
    {
        return m_index != rhs.m_index;
    }

    /**
     * Does not compare alphabetically, uses the index for ordering.
     */
    bool operator <(const IndexedString& rhs) const
    {
        return m_index < rhs.m_index;
    }

    /**
     * Use this to construct a hash-value on-the-fly
     *
     * To read it, just use the hash member, and when a new string is started, call @c clear().
     *
     * This needs very fast performance(per character operation), so it must stay inlined.
     */
    struct RunningHash
    {
        enum {
            HashInitialValue = 5381
        };

        RunningHash()
        {
        }
        inline void append(const char c)
        {
            hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
        }
        inline void clear()
        {
            hash = HashInitialValue;
        }

        /// We initialize the hash with zero, because we want empty strings to create a zero hash(invalid)
        unsigned int hash = HashInitialValue;
    };

    static unsigned int hashString(const char* str, unsigned short length);

    /**
     * Optimized function that only computes the index of a string
     * removes the overhead of the IndexedString ref counting
     */
    static uint indexForString(const char* str, unsigned short length, uint hash = 0);
    static uint indexForString(const QString& str, uint hash = 0);

private:
    uint m_index = 0;
};

// the following function would need to be exported in case you'd remove the inline keyword.
inline uint qHash(const KDevelop::IndexedString& str)
{
    return str.index();
}
}

/**
 * qDebug() stream operator.  Writes the string to the debug output.
 */
KDEVPLATFORMSERIALIZATION_EXPORT QDebug operator<<(QDebug s, const KDevelop::IndexedString& string);

Q_DECLARE_METATYPE(KDevelop::IndexedString)
Q_DECLARE_TYPEINFO(KDevelop::IndexedString, Q_MOVABLE_TYPE);

#endif