json/qjson.cpp

Switch to Source codePreprocessed file
LineSource CodeCoverage
1 -
2 -
3 -
4namespace QJsonPrivate -
5{ -
6 -
7 -
8 -
9 -
10 -
11 -
12 -
13static const Base emptyArray = { { (sizeof(Base)) }, { 0 }, { 0 } }; -
14static const Base emptyObject = { { (sizeof(Base)) }, { 0 }, { 0 } }; -
15 -
16 -
17void Data::compact() -
18{ -
19 qt_noop(); -
20 -
21 if (!compactionCounter)
partially evaluated: !compactionCounter
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:8
0-8
22 return;
never executed: return;
0
23 -
24 Base *base = header->root(); -
25 int reserve = 0; -
26 if (base->is_object) {
evaluated: base->is_object
TRUEFALSE
yes
Evaluation Count:5
yes
Evaluation Count:3
3-5
27 Object *o = static_cast<Object *>(base); -
28 for (int i = 0; i < (int)o->length; ++i)
evaluated: i < (int)o->length
TRUEFALSE
yes
Evaluation Count:7
yes
Evaluation Count:5
5-7
29 reserve += o->entryAt(i)->usedStorage(o);
executed: reserve += o->entryAt(i)->usedStorage(o);
Execution Count:7
7
30 } else {
executed: }
Execution Count:5
5
31 Array *a = static_cast<Array *>(base); -
32 for (int i = 0; i < (int)a->length; ++i)
evaluated: i < (int)a->length
TRUEFALSE
yes
Evaluation Count:3
yes
Evaluation Count:3
3
33 reserve += (*a)[i].usedStorage(a);
executed: reserve += (*a)[i].usedStorage(a);
Execution Count:3
3
34 }
executed: }
Execution Count:3
3
35 -
36 int size = sizeof(Base) + reserve + base->length*sizeof(offset); -
37 int alloc = sizeof(Header) + size; -
38 Header *h = (Header *) malloc(alloc); -
39 h->tag = QJsonDocument::BinaryFormatTag; -
40 h->version = 1; -
41 Base *b = h->root(); -
42 b->size = size; -
43 b->is_object = header->root()->is_object; -
44 b->length = base->length; -
45 b->tableOffset = reserve + sizeof(Array); -
46 -
47 int offset = sizeof(Base); -
48 if (b->is_object) {
evaluated: b->is_object
TRUEFALSE
yes
Evaluation Count:5
yes
Evaluation Count:3
3-5
49 Object *o = static_cast<Object *>(base); -
50 Object *no = static_cast<Object *>(b); -
51 -
52 for (int i = 0; i < (int)o->length; ++i) {
evaluated: i < (int)o->length
TRUEFALSE
yes
Evaluation Count:7
yes
Evaluation Count:5
5-7
53 no->table()[i] = offset; -
54 -
55 const Entry *e = o->entryAt(i); -
56 Entry *ne = no->entryAt(i); -
57 int s = e->size(); -
58 memcpy(ne, e, s); -
59 offset += s; -
60 int dataSize = e->value.usedStorage(o); -
61 if (dataSize) {
partially evaluated: dataSize
TRUEFALSE
yes
Evaluation Count:7
no
Evaluation Count:0
0-7
62 memcpy((char *)no + offset, e->value.data(o), dataSize); -
63 ne->value.value = offset; -
64 offset += dataSize; -
65 }
executed: }
Execution Count:7
7
66 }
executed: }
Execution Count:7
7
67 } else {
executed: }
Execution Count:5
5
68 Array *a = static_cast<Array *>(base); -
69 Array *na = static_cast<Array *>(b); -
70 -
71 for (int i = 0; i < (int)a->length; ++i) {
evaluated: i < (int)a->length
TRUEFALSE
yes
Evaluation Count:3
yes
Evaluation Count:3
3
72 const Value &v = (*a)[i]; -
73 Value &nv = (*na)[i]; -
74 nv = v; -
75 int dataSize = v.usedStorage(a); -
76 if (dataSize) {
partially evaluated: dataSize
TRUEFALSE
yes
Evaluation Count:3
no
Evaluation Count:0
0-3
77 memcpy((char *)na + offset, v.data(a), dataSize); -
78 nv.value = offset; -
79 offset += dataSize; -
80 }
executed: }
Execution Count:3
3
81 }
executed: }
Execution Count:3
3
82 }
executed: }
Execution Count:3
3
83 qt_noop(); -
84 -
85 free(header); -
86 header = h; -
87 this->alloc = alloc; -
88 compactionCounter = 0; -
89}
executed: }
Execution Count:8
8
90 -
91bool Data::valid() const -
92{ -
93 if (header->tag != QJsonDocument::BinaryFormatTag || header->version != 1u)
partially evaluated: header->tag != QJsonDocument::BinaryFormatTag
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:1857
partially evaluated: header->version != 1u
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:1857
0-1857
94 return false;
never executed: return false;
0
95 -
96 bool res = false; -
97 if (header->root()->is_object)
evaluated: header->root()->is_object
TRUEFALSE
yes
Evaluation Count:867
yes
Evaluation Count:990
867-990
98 res = static_cast<Object *>(header->root())->isValid();
executed: res = static_cast<Object *>(header->root())->isValid();
Execution Count:867
867
99 else -
100 res = static_cast<Array *>(header->root())->isValid();
executed: res = static_cast<Array *>(header->root())->isValid();
Execution Count:990
990
101 -
102 return res;
executed: return res;
Execution Count:1857
1857
103} -
104 -
105 -
106int Base::reserveSpace(uint dataSize, int posInTable, uint numItems, bool replace) -
107{ -
108 qt_noop(); -
109 -
110 offset off = tableOffset; -
111 -
112 if (replace) {
evaluated: replace
TRUEFALSE
yes
Evaluation Count:97
yes
Evaluation Count:249
97-249
113 memmove((char *)(table()) + dataSize, table(), length*sizeof(offset)); -
114 } else {
executed: }
Execution Count:97
97
115 memmove((char *)(table() + posInTable + numItems) + dataSize, table() + posInTable, (length - posInTable)*sizeof(offset)); -
116 memmove((char *)(table()) + dataSize, table(), posInTable*sizeof(offset)); -
117 }
executed: }
Execution Count:249
249
118 tableOffset += dataSize; -
119 for (int i = 0; i < (int)numItems; ++i)
evaluated: i < (int)numItems
TRUEFALSE
yes
Evaluation Count:346
yes
Evaluation Count:346
346
120 table()[posInTable + i] = off;
executed: table()[posInTable + i] = off;
Execution Count:346
346
121 size += dataSize; -
122 if (!replace) {
evaluated: !replace
TRUEFALSE
yes
Evaluation Count:249
yes
Evaluation Count:97
97-249
123 length += numItems; -
124 size += numItems * sizeof(offset); -
125 }
executed: }
Execution Count:249
249
126 return off;
executed: return off;
Execution Count:346
346
127} -
128 -
129void Base::removeItems(int pos, int numItems) -
130{ -
131 qt_noop(); -
132 if (pos + numItems < (int)length)
evaluated: pos + numItems < (int)length
TRUEFALSE
yes
Evaluation Count:30
yes
Evaluation Count:43
30-43
133 memmove(table() + pos, table() + pos + numItems, (length - pos - numItems)*sizeof(offset));
executed: memmove(table() + pos, table() + pos + numItems, (length - pos - numItems)*sizeof(offset));
Execution Count:30
30
134 length -= numItems; -
135}
executed: }
Execution Count:73
73
136 -
137int Object::indexOf(const QString &key, bool *exists) -
138{ -
139 int min = 0; -
140 int n = length; -
141 while (n > 0) {
evaluated: n > 0
TRUEFALSE
yes
Evaluation Count:60569
yes
Evaluation Count:28818
28818-60569
142 int half = n >> 1; -
143 int middle = min + half; -
144 if (*entryAt(middle) >= key) {
evaluated: *entryAt(middle) >= key
TRUEFALSE
yes
Evaluation Count:43560
yes
Evaluation Count:17009
17009-43560
145 n = half; -
146 } else {
executed: }
Execution Count:43560
43560
147 min = middle + 1; -
148 n -= half + 1; -
149 }
executed: }
Execution Count:17009
17009
150 } -
151 if (min < (int)length && *entryAt(min) == key) {
evaluated: min < (int)length
TRUEFALSE
yes
Evaluation Count:28675
yes
Evaluation Count:143
evaluated: *entryAt(min) == key
TRUEFALSE
yes
Evaluation Count:28505
yes
Evaluation Count:170
143-28675
152 *exists = true; -
153 return min;
executed: return min;
Execution Count:28505
28505
154 } -
155 *exists = false; -
156 return min;
executed: return min;
Execution Count:313
313
157} -
158 -
159bool Object::isValid() const -
160{ -
161 if (tableOffset + length*sizeof(offset) > size)
evaluated: tableOffset + length*sizeof(offset) > size
TRUEFALSE
yes
Evaluation Count:65
yes
Evaluation Count:322817
65-322817
162 return false;
executed: return false;
Execution Count:65
65
163 -
164 for (uint i = 0; i < length; ++i) {
evaluated: i < length
TRUEFALSE
yes
Evaluation Count:992716
yes
Evaluation Count:322327
322327-992716
165 offset entryOffset = table()[i]; -
166 if (entryOffset + sizeof(Entry) >= tableOffset)
evaluated: entryOffset + sizeof(Entry) >= tableOffset
TRUEFALSE
yes
Evaluation Count:51
yes
Evaluation Count:992665
51-992665
167 return false;
executed: return false;
Execution Count:51
51
168 Entry *e = entryAt(i); -
169 int s = e->size(); -
170 if (table()[i] + s > tableOffset)
evaluated: table()[i] + s > tableOffset
TRUEFALSE
yes
Evaluation Count:75
yes
Evaluation Count:992590
75-992590
171 return false;
executed: return false;
Execution Count:75
75
172 if (!e->value.isValid(this))
evaluated: !e->value.isValid(this)
TRUEFALSE
yes
Evaluation Count:364
yes
Evaluation Count:992226
364-992226
173 return false;
executed: return false;
Execution Count:364
364
174 }
executed: }
Execution Count:992226
992226
175 return true;
executed: return true;
Execution Count:322327
322327
176} -
177 -
178 -
179 -
180bool Array::isValid() const -
181{ -
182 if (tableOffset + length*sizeof(offset) > size)
evaluated: tableOffset + length*sizeof(offset) > size
TRUEFALSE
yes
Evaluation Count:55
yes
Evaluation Count:38617
55-38617
183 return false;
executed: return false;
Execution Count:55
55
184 -
185 for (uint i = 0; i < length; ++i) {
evaluated: i < length
TRUEFALSE
yes
Evaluation Count:182122
yes
Evaluation Count:38289
38289-182122
186 if (!at(i).isValid(this))
evaluated: !at(i).isValid(this)
TRUEFALSE
yes
Evaluation Count:328
yes
Evaluation Count:181794
328-181794
187 return false;
executed: return false;
Execution Count:328
328
188 }
executed: }
Execution Count:181794
181794
189 return true;
executed: return true;
Execution Count:38289
38289
190} -
191 -
192 -
193bool Entry::operator ==(const QString &key) const -
194{ -
195 if (value.latinKey)
evaluated: value.latinKey
TRUEFALSE
yes
Evaluation Count:28672
yes
Evaluation Count:3
3-28672
196 return (shallowLatin1Key() == key);
executed: return (shallowLatin1Key() == key);
Execution Count:28672
28672
197 else -
198 return (shallowKey() == key);
executed: return (shallowKey() == key);
Execution Count:3
3
199} -
200 -
201bool Entry::operator ==(const Entry &other) const -
202{ -
203 if (value.latinKey) {
partially evaluated: value.latinKey
TRUEFALSE
yes
Evaluation Count:2442
no
Evaluation Count:0
0-2442
204 if (other.value.latinKey)
evaluated: other.value.latinKey
TRUEFALSE
yes
Evaluation Count:2438
yes
Evaluation Count:4
4-2438
205 return shallowLatin1Key() == other.shallowLatin1Key();
executed: return shallowLatin1Key() == other.shallowLatin1Key();
Execution Count:2438
2438
206 return shallowLatin1Key() == other.shallowKey();
executed: return shallowLatin1Key() == other.shallowKey();
Execution Count:4
4
207 } else if (other.value.latinKey) {
never evaluated: other.value.latinKey
0
208 return shallowKey() == other.shallowLatin1Key();
never executed: return shallowKey() == other.shallowLatin1Key();
0
209 } -
210 return shallowKey() == other.shallowKey();
never executed: return shallowKey() == other.shallowKey();
0
211} -
212 -
213bool Entry::operator >=(const Entry &other) const -
214{ -
215 if (value.latinKey) {
partially evaluated: value.latinKey
TRUEFALSE
yes
Evaluation Count:7317
no
Evaluation Count:0
0-7317
216 if (other.value.latinKey)
evaluated: other.value.latinKey
TRUEFALSE
yes
Evaluation Count:7297
yes
Evaluation Count:20
20-7297
217 return shallowLatin1Key() >= other.shallowLatin1Key();
executed: return shallowLatin1Key() >= other.shallowLatin1Key();
Execution Count:7297
7297
218 return shallowLatin1Key() >= other.shallowKey();
executed: return shallowLatin1Key() >= other.shallowKey();
Execution Count:20
20
219 } else if (other.value.latinKey) {
never evaluated: other.value.latinKey
0
220 return shallowKey() >= other.shallowLatin1Key();
never executed: return shallowKey() >= other.shallowLatin1Key();
0
221 } -
222 return shallowKey() >= other.shallowKey();
never executed: return shallowKey() >= other.shallowKey();
0
223} -
224 -
225 -
226int Value::usedStorage(const Base *b) const -
227{ -
228 int s = 0; -
229 switch (type) { -
230 case QJsonValue::Double: -
231 if (latinOrIntValue)
partially evaluated: latinOrIntValue
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:69828
0-69828
232 break;
never executed: break;
0
233 s = sizeof(double); -
234 break;
executed: break;
Execution Count:69828
69828
235 case QJsonValue::String: { -
236 char *d = data(b); -
237 if (latinOrIntValue)
evaluated: latinOrIntValue
TRUEFALSE
yes
Evaluation Count:353880
yes
Evaluation Count:804
804-353880
238 s = sizeof(ushort) + qFromLittleEndian(*(ushort *)d);
executed: s = sizeof(ushort) + qFromLittleEndian(*(ushort *)d);
Execution Count:353880
353880
239 else -
240 s = sizeof(int) + sizeof(ushort) * qFromLittleEndian(*(int *)d);
executed: s = sizeof(int) + sizeof(ushort) * qFromLittleEndian(*(int *)d);
Execution Count:804
804
241 break;
executed: break;
Execution Count:354684
354684
242 } -
243 case QJsonValue::Array: -
244 case QJsonValue::Object: -
245 s = base(b)->size; -
246 break;
executed: break;
Execution Count:359743
359743
247 case QJsonValue::Null: -
248 case QJsonValue::Bool: -
249 default: -
250 break;
never executed: break;
0
251 } -
252 return alignedSize(s);
executed: return alignedSize(s);
Execution Count:784255
784255
253} -
254 -
255bool Value::isValid(const Base *b) const -
256{ -
257 int offset = 0; -
258 switch (type) { -
259 case QJsonValue::Double: -
260 if (latinOrIntValue)
evaluated: latinOrIntValue
TRUEFALSE
yes
Evaluation Count:384942
yes
Evaluation Count:69843
69843-384942
261 break;
executed: break;
Execution Count:384942
384942
262 -
263 case QJsonValue::String:
code before this statement executed: case QJsonValue::String:
Execution Count:69843
69843
264 case QJsonValue::Array: -
265 case QJsonValue::Object: -
266 offset = value; -
267 break;
executed: break;
Execution Count:784361
784361
268 case QJsonValue::Null: -
269 case QJsonValue::Bool: -
270 default: -
271 break;
executed: break;
Execution Count:5409
5409
272 } -
273 -
274 if (!offset)
evaluated: !offset
TRUEFALSE
yes
Evaluation Count:390351
yes
Evaluation Count:784361
390351-784361
275 return true;
executed: return true;
Execution Count:390351
390351
276 if (offset + sizeof(uint) > b->tableOffset)
evaluated: offset + sizeof(uint) > b->tableOffset
TRUEFALSE
yes
Evaluation Count:126
yes
Evaluation Count:784235
126-784235
277 return false;
executed: return false;
Execution Count:126
126
278 -
279 int s = usedStorage(b); -
280 if (!s)
evaluated: !s
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:784234
1-784234
281 return true;
executed: return true;
Execution Count:1
1
282 if (s < 0 || offset + s > (int)b->tableOffset)
evaluated: s < 0
TRUEFALSE
yes
Evaluation Count:14
yes
Evaluation Count:784220
evaluated: offset + s > (int)b->tableOffset
TRUEFALSE
yes
Evaluation Count:69
yes
Evaluation Count:784151
14-784220
283 return false;
executed: return false;
Execution Count:83
83
284 if (type == QJsonValue::Array)
evaluated: type == QJsonValue::Array
TRUEFALSE
yes
Evaluation Count:37682
yes
Evaluation Count:746469
37682-746469
285 return static_cast<Array *>(base(b))->isValid();
executed: return static_cast<Array *>(base(b))->isValid();
Execution Count:37682
37682
286 if (type == QJsonValue::Object)
evaluated: type == QJsonValue::Object
TRUEFALSE
yes
Evaluation Count:322015
yes
Evaluation Count:424454
322015-424454
287 return static_cast<Object *>(base(b))->isValid();
executed: return static_cast<Object *>(base(b))->isValid();
Execution Count:322015
322015
288 return true;
executed: return true;
Execution Count:424454
424454
289} -
290 -
291 -
292 -
293 -
294int Value::requiredStorage(const QJsonValue &v, bool *compressed) -
295{ -
296 *compressed = false; -
297 switch (v.t) { -
298 case QJsonValue::Double: -
299 if (QJsonPrivate::compressedNumber(v.dbl) != 2147483647) {
evaluated: QJsonPrivate::compressedNumber(v.dbl) != 2147483647
TRUEFALSE
yes
Evaluation Count:81
yes
Evaluation Count:34
34-81
300 *compressed = true; -
301 return 0;
executed: return 0;
Execution Count:81
81
302 } -
303 return sizeof(double);
executed: return sizeof(double);
Execution Count:34
34
304 case QJsonValue::String: { -
305 QString s = v.toString(); -
306 *compressed = QJsonPrivate::useCompressed(s); -
307 return QJsonPrivate::qStringSize(s, *compressed);
executed: return QJsonPrivate::qStringSize(s, *compressed);
Execution Count:153
153
308 } -
309 case QJsonValue::Array: -
310 case QJsonValue::Object: -
311 return v.base ? v.base->size : sizeof(QJsonPrivate::Base);
executed: return v.base ? v.base->size : sizeof(QJsonPrivate::Base);
Execution Count:22
22
312 case QJsonValue::Undefined: -
313 case QJsonValue::Null: -
314 case QJsonValue::Bool: -
315 break;
executed: break;
Execution Count:56
56
316 } -
317 return 0;
executed: return 0;
Execution Count:56
56
318} -
319 -
320 -
321 -
322 -
323uint Value::valueToStore(const QJsonValue &v, uint offset) -
324{ -
325 switch (v.t) { -
326 case QJsonValue::Undefined: -
327 case QJsonValue::Null: -
328 break;
executed: break;
Execution Count:37
37
329 case QJsonValue::Bool: -
330 return v.b;
executed: return v.b;
Execution Count:19
19
331 case QJsonValue::Double: { -
332 int c = QJsonPrivate::compressedNumber(v.dbl); -
333 if (c != 2147483647)
evaluated: c != 2147483647
TRUEFALSE
yes
Evaluation Count:81
yes
Evaluation Count:34
34-81
334 return c;
executed: return c;
Execution Count:81
81
335 } -
336 -
337 case QJsonValue::String: -
338 case QJsonValue::Array: -
339 case QJsonValue::Object: -
340 return offset;
executed: return offset;
Execution Count:209
209
341 } -
342 return 0;
executed: return 0;
Execution Count:37
37
343} -
344 -
345 -
346 -
347 -
348void Value::copyData(const QJsonValue &v, char *dest, bool compressed) -
349{ -
350 switch (v.t) { -
351 case QJsonValue::Double: -
352 if (!compressed) {
partially evaluated: !compressed
TRUEFALSE
yes
Evaluation Count:34
no
Evaluation Count:0
0-34
353 qToLittleEndian(v.ui, (uchar *)dest); -
354 }
executed: }
Execution Count:34
34
355 break;
executed: break;
Execution Count:34
34
356 case QJsonValue::String: { -
357 QString str = v.toString(); -
358 QJsonPrivate::copyString(dest, str, compressed); -
359 break;
executed: break;
Execution Count:153
153
360 } -
361 case QJsonValue::Array: -
362 case QJsonValue::Object: { -
363 const QJsonPrivate::Base *b = v.base; -
364 if (!b)
evaluated: !b
TRUEFALSE
yes
Evaluation Count:5
yes
Evaluation Count:17
5-17
365 b = (v.t == QJsonValue::Array ? &emptyArray : &emptyObject);
evaluated: v.t == QJsonValue::Array
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:4
executed: b = (v.t == QJsonValue::Array ? &emptyArray : &emptyObject);
Execution Count:5
1-5
366 memcpy(dest, b, b->size); -
367 break;
executed: break;
Execution Count:22
22
368 } -
369 default: -
370 break;
never executed: break;
0
371 } -
372}
executed: }
Execution Count:209
209
373 -
374} -
375 -
376 -
377 -
Switch to Source codePreprocessed file

Generated by Squish Coco Non-Commercial