summaryrefslogtreecommitdiff
path: root/src/libs/sqlite/utf8string.h
blob: 6e8e469595f1fca32ef58f34a43aa7c8aedb1f42 (plain)
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#pragma once

#include "sqliteglobal.h"

#include <QByteArray>
#include <QDataStream>
#include <QHashFunctions>
#include <QMetaType>
#include <QString>

#include <cstring>
#include <iosfwd>

class Utf8StringVector;
class Utf8String;

class Utf8String
{
    friend class Utf8StringVector;

public:
    Utf8String() = default;

    explicit Utf8String(const char *utf8Text)
        : byteArray(utf8Text, utf8Text ? static_cast<int>(std::strlen(utf8Text)) : -1)
    {}

    explicit Utf8String(const char *utf8Text, int size)
        : byteArray(utf8Text, size)
    {}

    Utf8String(const QString &text)
        : byteArray(text.toUtf8())
    {
    }

    const char *constData() const
    {
        return byteArray.constData();
    }

    int byteSize() const
    {
        return byteArray.size();
    }

    static Utf8String fromUtf8(const char *utf8Text)
    {
        return Utf8String(utf8Text, -1);
    }

    static Utf8String fromByteArray(const QByteArray &utf8ByteArray)
    {
        return Utf8String(utf8ByteArray);
    }
    const QByteArray &toByteArray() const
    {
        return byteArray;
    }

    static Utf8String fromString(const QString &text)
    {
        return Utf8String::fromByteArray(text.toUtf8());
    }

    QString toString() const
    {
        return QString::fromUtf8(byteArray, byteArray.size());
    }

    Utf8String mid(int position, int length = -1) const
    {
        return Utf8String(byteArray.mid(position, length));
    }

    void replace(const Utf8String &before, const Utf8String &after)
    {
        byteArray.replace(before.byteArray, after.byteArray);
    }

    void replace(int position, int length, const Utf8String &after)
    {
        byteArray.replace(position, length, after.byteArray);
    }

    SQLITE_EXPORT Utf8StringVector split(char separator) const;

    void clear()
    {
        byteArray.clear();
    }

    void append(const Utf8String &textToAppend)
    {
        byteArray.append(textToAppend.byteArray);
    }

    void chop(int n)
    {
        byteArray.chop(n);
    }

    int indexOf(const Utf8String &text) const
    {
        return byteArray.indexOf(text.byteArray);
    }

    int indexOf(const char *text) const
    {
        return byteArray.indexOf(text);
    }

    int lastIndexOf(const Utf8String &text) const
    {
        return byteArray.lastIndexOf(text.byteArray);
    }

    int lastIndexOf(const char *text) const
    {
        return byteArray.lastIndexOf(text);
    }

    int indexOf(char character) const
    {
        return byteArray.indexOf(character);
    }

    bool contains(const Utf8String &text) const
    {
        return byteArray.contains(text.byteArray);
    }

    bool contains(const char *text) const
    {
        return byteArray.contains(text);
    }

    bool contains(char character) const
    {
        return byteArray.contains(character);
    }

    bool startsWith(const Utf8String &text) const
    {
        return byteArray.startsWith(text.byteArray);
    }

    bool startsWith(const char *text) const
    {
        return byteArray.startsWith(text);
    }

    bool startsWith(char character) const
    {
        return byteArray.startsWith(character);
    }

    bool endsWith(const Utf8String &text) const
    {
        return byteArray.endsWith(text.byteArray);
    }

    bool endsWith(const char *text) const
    {
        return byteArray.endsWith(text);
    }

    bool endsWith(char character) const
    {
        return byteArray.endsWith(character);
    }

    bool isNull() const
    {
        return byteArray.isNull();
    }

    bool isEmpty() const
    {
        return byteArray.isEmpty();
    }

    bool hasContent() const
    {
        return !isEmpty();
    }

    void reserve(int reserveSize)
    {
        byteArray.reserve(reserveSize);
    }

    template<typename T>
    static Utf8String number(T number, int base = 10)
    {
        return Utf8String::fromByteArray(QByteArray::number(number, base));
    }

    const Utf8String &operator+=(const Utf8String &text)
    {
        byteArray += text.byteArray;

        return *this;
    }

    static void registerType()
    {
        qRegisterMetaType<Utf8String>("Utf8String");
    }

    operator QString() const
    {
        return toString();
    }

    operator const QByteArray &() const
    {
        return byteArray;
    }

    friend const Utf8String operator+(const Utf8String &first, const Utf8String &second)
    {
        return Utf8String(first.byteArray + second.byteArray);
    }

    friend bool operator!=(const Utf8String &first, const Utf8String &second)
    {
        return first.byteArray != second.byteArray;
    }

    friend bool operator==(const Utf8String &first, const Utf8String &second)
    {
        return first.byteArray == second.byteArray;
    }

    friend bool operator==(const Utf8String &first, const char *second)
    {
        return first.byteArray == second;
    }

    friend bool operator==(const char *first, const Utf8String &second)
    {
        return second == first;
    }

    friend bool operator==(const Utf8String &first, const QString &second)
    {
        return first.byteArray == second.toUtf8();
    }

    friend bool operator<(const Utf8String &first, const Utf8String &second)
    {
        if (first.byteSize() == second.byteSize())
            return first.byteArray < second.byteArray;

        return first.byteSize() < second.byteSize();
    }

    friend  QDataStream &operator<<(QDataStream &datastream, const Utf8String &text)
    {
        datastream << text.byteArray;

        return datastream;
    }

    friend QDataStream &operator>>(QDataStream &datastream, Utf8String &text)
    {
        datastream >> text.byteArray;

        return datastream;
    }

    friend uint qHash(const Utf8String &utf8String)
    {
        return qHash(utf8String.byteArray);
    }

protected:
    explicit Utf8String(const QByteArray &utf8ByteArray)
        : byteArray(utf8ByteArray)
    {
    }

private:
    QByteArray byteArray;
};

SQLITE_EXPORT QDebug operator<<(QDebug debug, const Utf8String &text);
SQLITE_EXPORT std::ostream& operator<<(std::ostream &os, const Utf8String &utf8String);

#define Utf8StringLiteral(str) Utf8String::fromByteArray(QByteArrayLiteral(str))