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
|
/*
This file is part of the KDE libraries
SPDX-FileCopyrightText: 2008 Jakub Stachowski <qbast@go2.pl>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#ifndef BUFFERFRAGMENT_H
#define BUFFERFRAGMENT_H
#include "kconfigini_p.h"
#define bf_isspace(str) ((str == ' ') || (str == '\t') || (str == '\r'))
// This class provides wrapper around fragment of existing buffer (array of bytes).
// If underlying buffer gets deleted, all BufferFragment objects referencing it become invalid.
// Use toByteArray() to make deep copy of the buffer fragment.
//
// API is designed to subset of QByteArray methods with some changes:
// - trim() is like QByteArray.trimmed(), but it modifies current object
// - truncateLeft() provides way to cut off beginning of the buffer
// - split() works more like strtok_r than QByteArray.split()
// - truncateLeft() and mid() require position argument to be valid
class KConfigIniBackend::BufferFragment
{
public:
BufferFragment() : d(nullptr), len(0)
{
}
BufferFragment(char *buf, int size) : d(buf), len(size)
{
}
int length() const
{
return len;
}
char at(unsigned int i) const
{
Q_ASSERT(i < len);
return d[i];
}
void clear()
{
len = 0;
}
const char *constData() const
{
return d;
}
char *data() const
{
return d;
}
void trim()
{
while (bf_isspace(*d) && len > 0) {
d++;
len--;
}
while (len > 0 && bf_isspace(d[len - 1])) {
len--;
}
}
// similar to strtok_r . On first call variable pointed by start should be set to 0.
// Each call will update *start to new starting position.
BufferFragment split(char c, unsigned int *start)
{
while (*start < len) {
int end = indexOf(c, *start);
if (end == -1) {
end = len;
}
BufferFragment line(d + (*start), end - (*start));
*start = end + 1;
return line;
}
return BufferFragment();
}
bool isEmpty() const
{
return !len;
}
BufferFragment left(unsigned int size) const
{
return BufferFragment(d, qMin(size, len));
}
void truncateLeft(unsigned int size)
{
Q_ASSERT(size <= len);
d += size;
len -= size;
}
void truncate(unsigned int pos)
{
if (pos < len) {
len = pos;
}
}
bool isNull() const
{
return !d;
}
BufferFragment mid(unsigned int pos, int length = -1) const
{
Q_ASSERT(pos < len);
int size = length;
if (length == -1 || (pos + length) > len) {
size = len - pos;
}
return BufferFragment(d + pos, size);
}
bool operator==(const QByteArray &other) const
{
return (other.size() == (int)len && memcmp(d, other.constData(), len) == 0);
}
bool operator!=(const QByteArray &other) const
{
return (other.size() != (int)len || memcmp(d, other.constData(), len) != 0);
}
bool operator==(const BufferFragment other) const
{
return other.len == len && !memcmp(d, other.d, len);
}
int indexOf(char c, unsigned int from = 0) const
{
const char *cursor = d + from - 1;
const char *end = d + len;
while (++cursor < end)
if (*cursor == c) {
return cursor - d;
}
return -1;
}
int lastIndexOf(char c) const
{
int from = len - 1;
while (from >= 0)
if (d[from] == c) {
return from;
} else {
from--;
}
return -1;
}
QByteArray toByteArray() const
{
return QByteArray(d, len);
}
// this is faster than toByteArray, but returned QByteArray becomes invalid
// when buffer for this BufferFragment disappears
QByteArray toVolatileByteArray() const
{
return QByteArray::fromRawData(d, len);
}
private:
char *d;
unsigned int len;
};
uint qHash(const KConfigIniBackend::BufferFragment fragment)
{
const uchar *p = reinterpret_cast<const uchar*>(fragment.constData());
const int len = fragment.length();
// This algorithm is copied from qhash.cpp (Qt5 version).
// Sadly this code is not accessible from the outside without going through abstraction
// layers. Even QByteArray::fromRawData would do an allocation internally...
uint h = 0;
for (int i = 0; i < len; ++i) {
h = 31 * h + p[i];
}
return h;
}
#endif
|