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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
|
// slang-json-value.cpp
#include "slang-json-value.h"
#include "../core/slang-string-escape-util.h"
#include "../core/slang-string-util.h"
namespace Slang {
/* static */const JSONValue::Kind JSONValue::g_typeToKind[] =
{
JSONValue::Kind::Invalid, // Invalid
JSONValue::Kind::Bool, // True,
JSONValue::Kind::Bool, // False
JSONValue::Kind::Null, // Null,
JSONValue::Kind::String, // StringLexeme,
JSONValue::Kind::Integer, // IntegerLexeme,
JSONValue::Kind::Float, // FloatLexeme,
JSONValue::Kind::Integer, // IntegerValue,
JSONValue::Kind::Float, // FloatValue,
JSONValue::Kind::String, // StringValue,
JSONValue::Kind::Array, // Array,
JSONValue::Kind::Object, // Object,
};
static JSONKeyValue _makeInvalidKeyValue()
{
JSONKeyValue keyValue;
keyValue.key = JSONKey(0);
keyValue.value.type = JSONValue::Type::Invalid;
return keyValue;
}
/* static */JSONKeyValue g_invalid = _makeInvalidKeyValue();
/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
JSONValue
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
bool JSONValue::asBool() const
{
switch (type)
{
case JSONValue::Type::True: return true;
case JSONValue::Type::False:
case JSONValue::Type::Null:
{
return false;
}
case JSONValue::Type::IntegerValue: return intValue != 0;
case JSONValue::Type::FloatValue: return floatValue != 0;
default: break;
}
if (isLexeme(type))
{
SLANG_ASSERT(!"Lexeme values can only be accessed through container");
}
else
{
SLANG_ASSERT(!"Not bool convertable");
}
return false;
}
int64_t JSONValue::asInteger() const
{
switch (type)
{
case JSONValue::Type::True: return 1;
case JSONValue::Type::False:
case JSONValue::Type::Null:
{
return 0;
}
case JSONValue::Type::IntegerValue: return intValue;
case JSONValue::Type::FloatValue: return int64_t(floatValue);
break;
}
if (isLexeme(type))
{
SLANG_ASSERT(!"Lexeme values can only be accessed through container");
}
else
{
SLANG_ASSERT(!"Not int convertable");
}
return 0;
}
double JSONValue::asFloat() const
{
switch (type)
{
case JSONValue::Type::True: return 1.0;
case JSONValue::Type::False:
case JSONValue::Type::Null:
{
return 0.0;
}
case JSONValue::Type::IntegerValue: return double(intValue);
case JSONValue::Type::FloatValue: return floatValue;
default: break;
}
if (isLexeme(type))
{
SLANG_ASSERT(!"Lexeme values can only be accessed through container");
}
else
{
SLANG_ASSERT(!"Not float convertable");
}
return 0;
}
/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
JSONContainer
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
JSONContainer::JSONContainer(SourceManager* sourceManager):
m_slicePool(StringSlicePool::Style::Default),
m_sourceManager(sourceManager)
{
// Index 0 is the empty array or object
_addRange(Range::Type::None, 0, 0);
}
/* static */bool JSONContainer::areKeysUnique(const JSONKeyValue* keyValues, Index keyValueCount)
{
for (Index i = 1; i < keyValueCount; ++i)
{
const JSONKey key = keyValues[i].key;
for (Int j = 0; j < i - 1; j++)
{
if (keyValues[j].key == key)
{
return false;
}
}
}
return true;
}
Index JSONContainer::_addRange(Range::Type type, Index startIndex, Index count)
{
if (m_freeRangeIndices.getCount() > 0)
{
const Index rangeIndex = m_freeRangeIndices.getLast();
m_freeRangeIndices.removeLast();
auto& range = m_ranges[rangeIndex];
range.type = type;
range.startIndex = startIndex;
range.count = count;
range.capacity = count;
return rangeIndex;
}
else
{
Range range;
range.type = type;
range.startIndex = startIndex;
range.count = count;
range.capacity = count;
m_ranges.add(range);
return m_ranges.getCount() - 1;
}
}
JSONValue JSONContainer::createArray(const JSONValue* values, Index valuesCount, SourceLoc loc)
{
if (valuesCount <= 0)
{
return JSONValue::makeEmptyArray(loc);
}
JSONValue value;
value.type = JSONValue::Type::Array;
value.loc = loc;
value.rangeIndex = _addRange(Range::Type::Array, m_objectValues.getCount(), valuesCount);
m_arrayValues.addRange(values, valuesCount);
return value;
}
JSONValue JSONContainer::createObject(const JSONKeyValue* keyValues, Index keyValueCount, SourceLoc loc)
{
if (keyValueCount <= 0)
{
return JSONValue::makeEmptyObject(loc);
}
JSONValue value;
value.type = JSONValue::Type::Object;
value.loc = loc;
value.rangeIndex = _addRange(Range::Type::Object, m_objectValues.getCount(), keyValueCount);
m_objectValues.addRange(keyValues, keyValueCount);
return value;
}
JSONValue JSONContainer::createString(const UnownedStringSlice& slice, SourceLoc loc)
{
JSONValue value;
value.type = JSONValue::Type::StringValue;
value.loc = loc;
value.stringKey = getKey(slice);
return value;
}
JSONKey JSONContainer::getKey(const UnownedStringSlice& slice)
{
return JSONKey(m_slicePool.add(slice));
}
ConstArrayView<JSONValue> JSONContainer::getArray(const JSONValue& in) const
{
SLANG_ASSERT(in.type == JSONValue::Type::Array);
if (in.type != JSONValue::Type::Array || in.rangeIndex == 0)
{
return ConstArrayView<JSONValue>((const JSONValue*)nullptr, 0);
}
const Range& range = m_ranges[in.rangeIndex];
return ConstArrayView<JSONValue>(m_arrayValues.getBuffer() + range.startIndex, range.count);
}
ConstArrayView<JSONKeyValue> JSONContainer::getObject(const JSONValue& in) const
{
SLANG_ASSERT(in.type == JSONValue::Type::Array);
if (in.type != JSONValue::Type::Array || in.rangeIndex == 0)
{
return ConstArrayView<JSONKeyValue>((const JSONKeyValue*)nullptr, 0);
}
const Range& range = m_ranges[in.rangeIndex];
return ConstArrayView<JSONKeyValue>(m_objectValues.getBuffer() + range.startIndex, range.count);
}
ArrayView<JSONValue> JSONContainer::getArray(const JSONValue& in)
{
SLANG_ASSERT(in.type == JSONValue::Type::Array);
if (in.type != JSONValue::Type::Array || in.rangeIndex == 0)
{
return ArrayView<JSONValue>((JSONValue*)nullptr, 0);
}
const Range& range = m_ranges[in.rangeIndex];
return ArrayView<JSONValue>(m_arrayValues.getBuffer() + range.startIndex, range.count);
}
ArrayView<JSONKeyValue> JSONContainer::getObject(const JSONValue& in)
{
SLANG_ASSERT(in.type == JSONValue::Type::Object);
if (in.type != JSONValue::Type::Object || in.rangeIndex == 0)
{
return ArrayView<JSONKeyValue>((JSONKeyValue*)nullptr, 0);
}
const Range& range = m_ranges[in.rangeIndex];
return ArrayView<JSONKeyValue>(m_objectValues.getBuffer() + range.startIndex, range.count);
}
UnownedStringSlice JSONContainer::getLexeme(const JSONValue& in)
{
SLANG_ASSERT(JSONValue::isLexeme(in.type));
if (!JSONValue::isLexeme(in.type))
{
return UnownedStringSlice();
}
if (!(m_currentView && m_currentView->getRange().contains(in.loc)))
{
m_currentView = m_sourceManager->findSourceView(in.loc);
if (!m_currentView)
{
return UnownedStringSlice();
}
}
const auto offset = m_currentView->getRange().getOffset(in.loc);
SourceFile* sourceFile = m_currentView->getSourceFile();
return UnownedStringSlice(sourceFile->getContent().begin() + offset, in.length);
}
UnownedStringSlice JSONContainer::getString(const JSONValue& in)
{
switch (in.type)
{
case JSONValue::Type::StringValue: return getStringFromKey(in.stringKey);
case JSONValue::Type::StringLexeme:
{
StringEscapeHandler* handler = StringEscapeUtil::getHandler(StringEscapeUtil::Style::JSON);
UnownedStringSlice lexeme = getLexeme(in);
UnownedStringSlice unquoted = StringEscapeUtil::unquote(handler, lexeme);
if (handler->isUnescapingNeeeded(unquoted))
{
m_buf.Clear();
handler->appendUnescaped(unquoted, m_buf);
return m_buf.getUnownedSlice();
}
else
{
return unquoted;
}
}
}
SLANG_ASSERT(!"Not a string type");
return UnownedStringSlice();
}
JSONKey JSONContainer::getStringKey(const JSONValue& in)
{
return (in.type == JSONValue::Type::StringValue) ? in.stringKey : getKey(getString(in));
}
bool JSONContainer::asBool(const JSONValue& value)
{
switch (value.type)
{
case JSONValue::Type::IntegerLexeme: return asInteger(value) != 0;
case JSONValue::Type::FloatLexeme: return asFloat(value) != 0.0;
default: return value.asBool();
}
}
int64_t JSONContainer::asInteger(const JSONValue& value)
{
switch (value.type)
{
case JSONValue::Type::IntegerLexeme:
{
UnownedStringSlice slice = getLexeme(value);
int64_t intValue;
if (SLANG_SUCCEEDED(StringUtil::parseInt64(slice, intValue)) && slice.getLength() == 0)
{
return intValue;
}
SLANG_ASSERT(!"Couldn't convert int");
return 0;
}
case JSONValue::Type::FloatLexeme: return int64_t(asFloat(value));
default: return value.asInteger();
}
}
double JSONContainer::asFloat(const JSONValue& value)
{
switch (value.type)
{
case JSONValue::Type::IntegerLexeme: return double(asInteger(value));
case JSONValue::Type::FloatLexeme:
{
UnownedStringSlice slice = getLexeme(value);
double floatValue;
if (SLANG_SUCCEEDED(StringUtil::parseDouble(slice, floatValue)) && slice.getLength() == 0)
{
return floatValue;
}
SLANG_ASSERT(!"Couldn't convert double");
return 0.0;
}
default: return value.asFloat();
}
}
JSONValue& JSONContainer::getAt(const JSONValue& array, Index index)
{
SLANG_ASSERT(array.type == JSONValue::Type::Array);
const Range& range = m_ranges[array.rangeIndex];
SLANG_ASSERT(index >= 0 && index < range.count);
return m_arrayValues[range.startIndex + index];
}
void JSONContainer::addToArray(JSONValue& array, const JSONValue& value)
{
SLANG_ASSERT(array.type == JSONValue::Type::Array);
if (array.type == JSONValue::Type::Array)
{
// If it's empty
if (array.rangeIndex == 0)
{
// We can just add to the end
array.rangeIndex = _addRange(Range::Type::Array, m_arrayValues.getCount(), 1);
m_arrayValues.add(value);
}
else
{
_add(m_ranges[array.rangeIndex], m_arrayValues, value);
}
}
}
Index JSONContainer::findKeyGlobalIndex(const JSONValue& obj, JSONKey key)
{
SLANG_ASSERT(obj.type == JSONValue::Type::Object);
if (obj.type != JSONValue::Type::Object)
{
return -1;
}
auto buf = m_objectValues.getBuffer();
const Range& range = m_ranges[obj.rangeIndex];
for (Index i = range.startIndex; i < range.startIndex + range.count; ++i)
{
if (buf[i].key == key)
{
return i;
}
}
return -1;
}
Index JSONContainer::findKeyGlobalIndex(const JSONValue& obj, const UnownedStringSlice& slice)
{
Index keyIndex = m_slicePool.findIndex(slice);
if (keyIndex < 0)
{
return -1;
}
return findKeyGlobalIndex(obj, JSONKey(keyIndex));
}
void JSONContainer::_removeKey(JSONValue& obj, Index globalIndex)
{
Range& range = m_ranges[obj.rangeIndex];
const auto localIndex = globalIndex + range.startIndex;
if (localIndex < range.count - 1)
{
auto localBuf = m_objectValues.getBuffer() + range.startIndex;
::memmove(localBuf + localIndex, localBuf + localIndex + 1, sizeof(*localBuf) * (range.count - (localIndex + 1)));
}
--range.count;
}
bool JSONContainer::removeKey(JSONValue& obj, JSONKey key)
{
const Index globalIndex = findKeyGlobalIndex(obj, key);
if (globalIndex >= 0)
{
_removeKey(obj, globalIndex);
return true;
}
return false;
}
bool JSONContainer::removeKey(JSONValue& obj, const UnownedStringSlice& slice)
{
const Index globalIndex = findKeyGlobalIndex(obj, slice);
if (globalIndex >= 0)
{
_removeKey(obj, globalIndex);
return true;
}
return false;
}
template <typename T>
/* static */void JSONContainer::_add(Range& ioRange, List<T>& ioList, const T& value)
{
// If we have capacity, we can add to the end
if (ioRange.count < ioRange.capacity)
{
ioList[ioRange.startIndex + ioRange.count++] = value;
return;
}
// If we are at the end, we can just add
if (ioRange.startIndex + ioRange.capacity == ioList.getCount())
{
ioList.add(value);
ioRange.capacity++;
ioRange.count++;
return;
}
// Okay we have no choice but to make new space at the end
// So there's no place to add. We want to move to the end with an extra space.
const Index newStartIndex = ioList.getCount();
ioList.growToCount(newStartIndex + ioRange.count + 1);
auto buffer = ioList.getBuffer();
::memmove(buffer + newStartIndex, buffer + ioRange.startIndex, sizeof(*buffer) * ioRange.count);
buffer[newStartIndex + ioRange.count] = value;
ioRange.startIndex = newStartIndex;
ioRange.count++;
ioRange.capacity++;
}
void JSONContainer::setKeyValue(JSONValue& obj, JSONKey key, const JSONValue& value, SourceLoc loc)
{
SLANG_ASSERT(obj.type == JSONValue::Type::Object);
if (obj.type != JSONValue::Type::Object)
{
return;
}
const JSONKeyValue keyValue{key, loc, value};
if (obj.rangeIndex == 0)
{
// We need a new range and add to the end
obj.rangeIndex = _addRange(Range::Type::Object, m_objectValues.getCount(), 1);
m_objectValues.add(keyValue);
return;
}
const Index globalIndex = findKeyGlobalIndex(obj, key);
if (globalIndex >= 0)
{
auto& dst = m_objectValues[globalIndex];
SLANG_ASSERT(dst.key == key);
dst = keyValue;
return;
}
Range& range = m_ranges[obj.rangeIndex];
_add(range, m_objectValues, keyValue);
}
void JSONContainer::_destroyRange(Index rangeIndex)
{
auto& range = m_ranges[rangeIndex];
// If the range is at the end, shrink it
switch (range.type)
{
case Range::Type::Array:
{
if (range.startIndex + range.capacity == m_arrayValues.getCount())
{
m_arrayValues.setCount(range.startIndex);
}
break;
}
case Range::Type::Object:
{
if (range.startIndex + range.capacity == m_objectValues.getCount())
{
m_objectValues.setCount(range.startIndex);
}
break;
}
default: break;
}
range.type = Range::Type::Destroyed;
m_freeRangeIndices.add(rangeIndex);
}
void JSONContainer::destroy(JSONValue& value)
{
if (value.needsDestroy())
{
_destroyRange(value.rangeIndex);
}
value.type = JSONValue::Type::Invalid;
}
void JSONContainer::destroyRecursively(JSONValue& inValue)
{
if (!(inValue.needsDestroy() && m_ranges[inValue.rangeIndex].isActive()))
{
inValue.type = JSONValue::Type::Invalid;
return;
}
inValue.type = JSONValue::Type::Invalid;
List<Range> activeRanges;
activeRanges.add(m_ranges[inValue.rangeIndex]);
_destroyRange(inValue.rangeIndex);
while (activeRanges.getCount())
{
const Range range = activeRanges.getLast();
activeRanges.removeLast();
auto type = range.type;
const Index count = range.count;
if (type == Range::Type::Array)
{
auto* buf = m_arrayValues.getBuffer() + range.startIndex;
for (Index i = 0; i < count; ++i)
{
auto& value = buf[i];
// If we have an active range, add to work list, and destroy
if (value.needsDestroy() && m_ranges[value.rangeIndex].isActive())
{
activeRanges.add(m_ranges[value.rangeIndex]);
_destroyRange(value.rangeIndex);
}
value.type = JSONValue::Type::Invalid;
}
}
else
{
SLANG_ASSERT(type == Range::Type::Object);
auto* buf = m_objectValues.getBuffer() + range.startIndex;
for (Index i = 0; i < count; ++i)
{
auto& keyValue = buf[i];
auto& value = keyValue.value;
// We want to mark that it's in the list so that if we have a badly formed tree we don't read
if (value.needsDestroy() && m_ranges[value.rangeIndex].isActive())
{
activeRanges.add(m_ranges[value.rangeIndex]);
_destroyRange(value.rangeIndex);
}
value.type = JSONValue::Type::Invalid;
}
}
}
}
bool JSONContainer::areEqual(const JSONValue* a, const JSONValue* b, Index count)
{
for (Index i = 0; i < count; ++i)
{
if (!areEqual(a[i], b[i]))
{
return false;
}
}
return true;
}
/* static */bool JSONContainer::_sameKeyOrder(const JSONKeyValue* a, const JSONKeyValue* b, Index count)
{
for (Index i = 0; i < count; ++i)
{
if (a[i].key != b[i].key)
{
return false;
}
}
return true;
}
bool JSONContainer::_areEqualOrderedKeys(const JSONKeyValue* a, const JSONKeyValue* b, Index count)
{
for (Index i = 0; i < count; ++i)
{
const auto& curA = a[i];
const auto& curB = b[i];
if (curA.key != curB.key ||
!areEqual(curA.value, curB.value))
{
return false;
}
}
return true;
}
bool JSONContainer::_areEqualValues(const JSONKeyValue* a, const JSONKeyValue* b, Index count)
{
for (Index i = 0; i < count; ++i)
{
if (!areEqual(a[i].value, b[i].value))
{
return false;
}
}
return true;
}
bool JSONContainer::areEqual(const JSONKeyValue* a, const JSONKeyValue* b, Index count)
{
if (count == 0)
{
return true;
}
if (count == 1)
{
return _areEqualOrderedKeys(a, b, count);
}
else if (_sameKeyOrder(a, b, count))
{
return _areEqualValues(a, b, count);
}
else
{
// We need to compare with keys in the same order
List<JSONKeyValue> sortedAs;
sortedAs.addRange(a, count);
List<JSONKeyValue> sortedBs;
sortedBs.addRange(b, count);
sortedAs.sort([](const JSONKeyValue&a, const JSONKeyValue& b) -> bool { return a.key < b.key; });
sortedBs.sort([](const JSONKeyValue&a, const JSONKeyValue& b) -> bool { return a.key < b.key; });
return _areEqualOrderedKeys(sortedAs.getBuffer(), sortedBs.getBuffer(), count);
}
}
bool JSONContainer::areEqual(const JSONValue& a, const JSONValue& b)
{
if (&a == &b)
{
return true;
}
if (a.type == b.type)
{
switch (a.type)
{
default:
// Invalid are never equal
case JSONValue::Type::Invalid: return false;
case JSONValue::Type::True:
case JSONValue::Type::False:
case JSONValue::Type::Null:
{
return true;
}
case JSONValue::Type::IntegerLexeme:return asInteger(a) == asInteger(b);
case JSONValue::Type::FloatLexeme: return asFloat(a) == asFloat(b);
case JSONValue::Type::StringLexeme:
{
// If the lexemes are equal they are equal
UnownedStringSlice lexemeA = getLexeme(a);
UnownedStringSlice lexemeB = getLexeme(b);
// Else we want to decode the string to be sure if they are equal.
return lexemeA == lexemeB || getStringKey(a) == getStringKey(b);
}
case JSONValue::Type::IntegerValue: return a.intValue == b.intValue;
case JSONValue::Type::FloatValue: return a.floatValue == b.floatValue;
case JSONValue::Type::StringValue: return a.stringKey == b.stringKey;
case JSONValue::Type::Array:
{
if (a.rangeIndex == b.rangeIndex)
{
return true;
}
auto arrayA = getArray(a);
auto arrayB = getArray(b);
const Index count = arrayA.getCount();
return (count == arrayB.getCount()) && areEqual(arrayA.getBuffer(), arrayB.getBuffer(), count);
}
case JSONValue::Type::Object:
{
if (a.rangeIndex == b.rangeIndex)
{
return true;
}
const auto aValues = getObject(a);
const auto bValues = getObject(b);
const Index count = aValues.getCount();
return (count == bValues.getCount()) && areEqual(aValues.getBuffer(), bValues.getBuffer(), count);
}
}
}
// If they are the same kind, and float/int/string we can convert to compare
const JSONValue::Kind kind = a.getKind();
if (kind == b.getKind())
{
switch (kind)
{
case JSONValue::Kind::String: return getStringKey(a) == getStringKey(b);
case JSONValue::Kind::Integer: return asInteger(a) == asInteger(b);
case JSONValue::Kind::Float: return asFloat(a) == asFloat(b);
default: break;
}
}
return false;
}
} // namespace Slang
|