-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalue.cpp
719 lines (611 loc) · 24.7 KB
/
value.cpp
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
#include <rift/value.hpp>
#include <rift/util.hpp>
#include <fmt/format.h>
#include <algorithm>
namespace rift {
#ifdef RIFT_INCLUDE_MATJSON
Value::Value(matjson::Value value) noexcept {
switch (value.type()) {
default: case matjson::Type::Null: {
m_type = Type::Null;
m_data = std::monostate();
} break;
case matjson::Type::String: {
m_type = Type::String;
m_data = value.asString().unwrap();
} break;
case matjson::Type::Bool: {
m_type = Type::Boolean;
m_data = value.asBool().unwrap();
} break;
case matjson::Type::Number: {
if (value.isExactlyDouble()) {
m_type = Type::Float;
m_data = value.asDouble().unwrap();
} else {
m_type = Type::Integer;
m_data = value.asInt().unwrap();
}
} break;
case matjson::Type::Array: {
m_type = Type::Array;
auto array = value.asArray().unwrap();
Array result;
result.reserve(array.size());
for (auto const& element : array) {
result.emplace_back(element);
}
m_data = std::move(result);
} break;
case matjson::Type::Object: {
m_type = Type::Object;
Object result;
for (auto const& [key, element] : value) {
result[key] = element;
}
m_data = std::move(result);
} break;
}
}
matjson::Value Value::toJson() const noexcept {
switch (m_type) {
case Type::String:
return std::get<std::string>(m_data);
case Type::Integer:
return std::get<int64_t>(m_data);
case Type::Float:
return std::get<double>(m_data);
case Type::Boolean:
return std::get<bool>(m_data);
case Type::Array: {
auto const& array = getArray();
matjson::Value result = matjson::Value::array();
for (auto const& element : array) {
result.push(element.toJson());
}
return result;
}
case Type::Object: {
auto const& object = getObject();
matjson::Value result;
for (auto const& [key, element] : object) {
result[key] = element.toJson();
}
return result;
}
default:
return matjson::Value(std::nullptr_t{});
}
}
#endif
std::string Value::toString() const noexcept {
switch (m_type) {
case Type::String:
return std::get<std::string>(m_data);
case Type::Integer:
return std::to_string(std::get<int64_t>(m_data));
case Type::Float:
return fmt::format("{:.2f}", std::get<double>(m_data));
case Type::Boolean:
return std::get<bool>(m_data) ? "true" : "false";
case Type::Array: {
std::string result = "[";
auto const& array = getArray();
for (size_t i = 0; i < array.size(); ++i) {
result += array[i].toString();
if (i + 1 < array.size()) result += ", ";
}
return result + "]";
}
case Type::Object: {
// print all key-value pairs in the object on separate lines
std::string result = "{";
for (auto const& [key, value] : getObject()) {
result += fmt::format("{}: {}, ", key, value.toString());
}
if (!result.empty()) result.resize(result.size() - 2);
return result + "}";
}
default:
return "null";
}
}
int64_t Value::toInteger() const noexcept {
switch (m_type) {
case Type::String:
return util::readNumber<int64_t>(std::get<std::string>(m_data)).unwrapOrDefault();
case Type::Integer:
return std::get<int64_t>(m_data);
case Type::Float:
return static_cast<int64_t>(std::get<double>(m_data));
case Type::Boolean:
return std::get<bool>(m_data) ? 1 : 0;
case Type::Array:
return static_cast<int64_t>(std::get<Array>(m_data).size());
case Type::Object:
return static_cast<int64_t>(std::get<Object>(m_data).size());
default:
return 0;
}
}
double Value::toFloat() const noexcept {
switch (m_type) {
case Type::String:
return util::readNumber<double>(std::get<std::string>(m_data)).unwrapOr(std::numeric_limits<double>::quiet_NaN());
case Type::Integer:
return static_cast<double>(std::get<int64_t>(m_data));
case Type::Float:
return std::get<double>(m_data);
case Type::Boolean:
return std::get<bool>(m_data) ? 1.0 : 0.0;
case Type::Array:
return static_cast<double>(std::get<Array>(m_data).size());
case Type::Object:
return static_cast<double>(std::get<Object>(m_data).size());
default:
return 0.0;
}
}
bool Value::toBoolean() const noexcept {
switch (m_type) {
case Type::String:
return !std::get<std::string>(m_data).empty();
case Type::Integer:
return std::get<int64_t>(m_data) != 0;
case Type::Float:
return std::get<double>(m_data) != 0.0;
case Type::Boolean:
return std::get<bool>(m_data);
case Type::Array:
return !std::get<Array>(m_data).empty();
case Type::Object:
return !std::get<Object>(m_data).empty();
default:
return false;
}
}
Value::Result Value::operator+(const Value& other) const noexcept {
// if either value is null, return null
if (isNull() || other.isNull()) {
return geode::Ok(Value());
}
// if either value is an object, return an error
if (isObject() || other.isObject()) {
return geode::Err("Cannot perform object addition");
}
// if either value is an array, concatenate them
if (isArray() || other.isArray()) {
// if both values are arrays, concatenate them
if (isArray() && other.isArray()) {
auto result = getArray();
result.insert(result.end(), other.getArray().begin(), other.getArray().end());
return geode::Ok(Value(result));
}
// if one value is an array, append the other value
auto const& array = isArray() ? getArray() : other.getArray();
auto result = array;
result.push_back(isArray() ? other : *this);
return geode::Ok(Value(result));
}
// if either value is a string, concatenate them
if (isString() || other.isString()) {
return geode::Ok(Value(toString() + other.toString()));
}
// if either value is a float, convert both to floats and add
if (isFloat() || other.isFloat()) {
return geode::Ok(Value(toFloat() + other.toFloat()));
}
// otherwise, convert both to integers and add
return geode::Ok(Value(toInteger() + other.toInteger()));
}
Value::Result Value::operator-(const Value& other) const noexcept {
// if either value is null, return null
if (isNull() || other.isNull()) {
return geode::Ok(Value());
}
// if either value is an object, return an error
if (isObject() || other.isObject()) {
return geode::Err("Cannot perform object subtraction");
}
// if either value is an array, remove all occurrences of the other value
if (isArray() || other.isArray()) {
// make sure only one of the values is an array
if (isArray() && other.isArray()) {
return geode::Err("Cannot subtract two arrays");
}
auto const& array = isArray() ? getArray() : other.getArray();
auto const& value = isArray() ? other : *this;
Array result;
result.reserve(array.size());
for (auto const& item : array) {
if (item != value) {
result.push_back(item);
}
}
return geode::Ok(Value(result));
}
// if either value is a string, remove all occurrences of the other value
if (isString() || other.isString()) {
// if both values are strings, remove all occurrences of the other string
if (isString() && other.isString()) {
auto result = getString();
auto const& str = other.getString();
// if the value is empty, return the string
if (str.empty()) {
return geode::Ok(Value(result));
}
size_t pos = 0;
while ((pos = result.find(str, pos)) != std::string::npos) {
result.erase(pos, str.size());
}
return geode::Ok(Value(result));
}
// if first value is not a string, return error
if (!isString()) {
return geode::Err("Cannot subtract a string from a non-string");
}
// subtract the other value from the string
auto str = getString();
auto const& value = other.toString();
if (value.empty()) {
return geode::Ok(Value(str));
}
size_t pos = 0;
while ((pos = str.find(value, pos)) != std::string::npos) {
str.erase(pos, value.size());
}
return geode::Ok(Value(str));
}
// if either value is a float, convert both to floats and subtract
if (isFloat() || other.isFloat()) {
return geode::Ok(Value(toFloat() - other.toFloat()));
}
// otherwise, convert both to integers and subtract
return geode::Ok(Value(toInteger() - other.toInteger()));
}
Value::Result Value::operator*(const Value& other) const noexcept {
// if either value is null, return null
if (isNull() || other.isNull()) {
return geode::Ok(Value());
}
// if either value is an object, return an error
if (isObject() || other.isObject()) {
return geode::Err("Cannot perform object multiplication");
}
// if either value is an array, repeat it n times, where n is the other value
if (isArray() || other.isArray()) {
// make sure only one of the values is an array
if (isArray() && other.isArray()) {
return geode::Err("Cannot multiply two arrays");
}
auto const& array = isArray() ? getArray() : other.getArray();
auto const& num = isArray() ? other.toInteger() : toInteger();
Array result;
result.reserve(array.size() * num);
for (int64_t i = 0; i < num; ++i) {
result.insert(result.end(), array.begin(), array.end());
}
return geode::Ok(Value(result));
}
// if either value is a string, repeat it n times, where n is the other value
if (isString() || other.isString()) {
// make sure only one of the values is a string
if (isString() && other.isString()) {
return geode::Err("Cannot multiply two strings");
}
auto const& str = isString() ? getString() : other.getString();
auto num = isString() ? other.toInteger() : toInteger();
if (num <= 0) {
return geode::Ok(Value(""));
}
std::string result;
result.reserve(str.size() * num);
for (int64_t i = 0; i < num; ++i) {
result += str;
}
return geode::Ok(Value(result));
}
// if either value is a float, convert both to floats and multiply
if (isFloat() || other.isFloat()) {
return geode::Ok(Value(toFloat() * other.toFloat()));
}
// otherwise, convert both to integers and multiply
return geode::Ok(Value(toInteger() * other.toInteger()));
}
Value::Result Value::operator/(const Value& other) const noexcept {
// if either value is null, return null
if (isNull() || other.isNull()) {
return geode::Ok(Value());
}
// if either value is an object, return an error
if (isObject() || other.isObject()) {
return geode::Err("Cannot perform object division");
}
// if either value is an array, return an error
if (isArray() || other.isArray()) {
return geode::Err("Cannot perform array division");
}
// if either value is a string, return an error
if (isString() || other.isString()) {
return geode::Err("Cannot perform string division");
}
// if either value is a float, convert both to floats and divide
if (isFloat() || other.isFloat()) {
// if the divisor is zero, return Infinity or -Infinity
if (other.toFloat() == 0.0) {
return geode::Ok(Value(toFloat() < 0.0 ? -std::numeric_limits<double>::infinity() : std::numeric_limits<double>::infinity()));
}
return geode::Ok(Value(toFloat() / other.toFloat()));
}
// otherwise, convert both to integers and divide
// if the divisor is zero, return Infinity or -Infinity
if (other.toInteger() == 0) {
return geode::Ok(Value(toInteger() < 0 ? -std::numeric_limits<double>::infinity() : std::numeric_limits<double>::infinity()));
}
return geode::Ok(Value(toInteger() / other.toInteger()));
}
Value::Result Value::operator%(const Value& other) const noexcept {
// if either value is null, return null
if (isNull() || other.isNull()) {
return geode::Ok(Value());
}
// if either value is an object, return an error
if (isObject() || other.isObject()) {
return geode::Err("Cannot perform object modulo");
}
// if either value is an array, return an error
if (isArray() || other.isArray()) {
return geode::Err("Cannot perform array modulo");
}
// if either value is a string, get a substring
if (isString() || other.isString()) {
auto const& str = isString() ? getString() : other.getString();
auto const& num = isString() ? other.toInteger() : toInteger();
auto start = num < 0 ? str.size() + num : num;
auto count = num < 0 ? -num : str.size() - num;
start = std::clamp<size_t>(start, 0, str.size() - 1);
count = std::clamp<size_t>(count, 0, str.size() - start);
return geode::Ok(Value(str.substr(start, count)));
}
// if either value is a float, convert both to floats and get the remainder
if (isFloat() || other.isFloat()) {
// if the divisor is zero, return NaN
if (other.toFloat() == 0.0) {
return geode::Ok(Value(std::numeric_limits<double>::quiet_NaN()));
}
return geode::Ok(Value(std::fmod(toFloat(), other.toFloat())));
}
// otherwise, convert both to integers and get the remainder
// if the divisor is zero, return NaN
if (other.toInteger() == 0) {
return geode::Ok(Value(std::numeric_limits<double>::quiet_NaN()));
}
return geode::Ok(Value(toInteger() % other.toInteger()));
}
Value::Result Value::operator^(const Value& other) const noexcept {
// if either value is null, return null
if (isNull() || other.isNull()) {
return geode::Ok(Value());
}
// if either value is an object, return an error
if (isObject() || other.isObject()) {
return geode::Err("Cannot perform object exponentiation");
}
// if either value is an array, return an error
if (isArray() || other.isArray()) {
return geode::Err("Cannot perform array exponentiation");
}
// if either value is a string, return an error
if (isString() || other.isString()) {
return geode::Err("Cannot perform string exponentiation");
}
// if either value is a float, convert both to floats and raise to the power
if (isFloat() || other.isFloat()) {
return geode::Ok(Value(std::pow(toFloat(), other.toFloat())));
}
// otherwise, convert both to integers and raise to the power
return geode::Ok(Value(std::pow(toInteger(), other.toInteger())));
}
Value Value::operator==(const Value& other) const noexcept {
// if both values are null, return true
if (isNull() && other.isNull()) {
return true;
}
// if only one value is null, return false
if (isNull() || other.isNull()) {
return false;
}
// if both values are objects, compare them
if (isObject() && other.isObject()) {
// fast fail if the objects have different sizes
if (getObject().size() != other.getObject().size()) {
return false;
}
// compare each key-value pair in the objects
for (auto const& [key, value] : getObject()) {
auto it = other.getObject().find(key);
if (it == other.getObject().end() || it->second != value) {
return false;
}
}
return true;
}
// if both values are arrays, compare them
if (isArray() && other.isArray()) {
auto const& array = getArray();
auto const& otherArray = other.getArray();
// fast fail if the arrays have different sizes
if (array.size() != otherArray.size()) {
return false;
}
// compare each element in the arrays
for (size_t i = 0; i < array.size(); ++i) {
if (array[i] != otherArray[i]) {
return false;
}
}
return true;
}
// if both values are strings, compare them
if (isString() && other.isString()) {
return getString() == other.getString();
}
// if either value is a float, convert both to floats and compare
if (isFloat() || other.isFloat()) {
return toFloat() == other.toFloat();
}
// otherwise, convert both to integers and compare
return toInteger() == other.toInteger();
}
Value Value::operator!=(const Value& other) const noexcept {
// if both values are null, return false
if (isNull() && other.isNull()) {
return false;
}
// if only one value is null, return true
if (isNull() || other.isNull()) {
return true;
}
// if both values are objects, compare them
if (isObject() && other.isObject()) {
// fast fail if the objects have different sizes
if (getObject().size() != other.getObject().size()) {
return true;
}
// compare each key-value pair in the objects
for (auto const& [key, value] : getObject()) {
auto it = other.getObject().find(key);
if (it == other.getObject().end() || it->second != value) {
return true;
}
}
return false;
}
// if both values are arrays, compare them
if (isArray() && other.isArray()) {
auto const& array = getArray();
auto const& otherArray = other.getArray();
// fast fail if the arrays have different sizes
if (array.size() != otherArray.size()) {
return true;
}
// compare each element in the arrays
for (size_t i = 0; i < array.size(); ++i) {
if (array[i] != otherArray[i]) {
return true;
}
}
return false;
}
// if both values are strings, compare them
if (isString() && other.isString()) {
return getString() != other.getString();
}
// if either value is a float, convert both to floats and compare
if (isFloat() || other.isFloat()) {
return toFloat() != other.toFloat();
}
// otherwise, convert both to integers and compare
return toInteger() != other.toInteger();
}
Value Value::operator<(const Value& other) const noexcept {
if (isFloat() || other.isFloat()) {
return toFloat() < other.toFloat();
}
return toInteger() < other.toInteger();
}
Value Value::operator>(const Value& other) const noexcept {
if (isFloat() || other.isFloat()) {
return toFloat() > other.toFloat();
}
return toInteger() > other.toInteger();
}
Value Value::operator<=(const Value& other) const noexcept {
if (isFloat() || other.isFloat()) {
return toFloat() <= other.toFloat();
}
return toInteger() <= other.toInteger();
}
Value Value::operator>=(const Value& other) const noexcept {
if (isFloat() || other.isFloat()) {
return toFloat() >= other.toFloat();
}
return toInteger() >= other.toInteger();
}
Value Value::operator&&(const Value& other) const noexcept {
return toBoolean() && other.toBoolean();
}
Value Value::operator||(const Value& other) const noexcept {
return toBoolean() || other.toBoolean();
}
Value Value::operator!() const noexcept {
return !toBoolean();
}
Value Value::operator-() const noexcept {
if (isFloat()) {
return -std::get<double>(m_data);
}
return -toInteger();
}
Value Value::at(const Value& key) const noexcept {
switch (m_type) {
case Type::Array: {
auto const& array = getArray();
auto index = key.toInteger();
if (index < 0 || index >= static_cast<int64_t>(array.size())) {
return {}; // return null if index is out of bounds
}
return array[static_cast<size_t>(index)];
}
case Type::Object: {
auto const& object = getObject();
auto it = object.find(key.toString());
if (it == object.end()) {
return {}; // return null if key is not found
}
return it->second;
}
case Type::String: {
auto index = key.toInteger();
if (index < 0 || index >= static_cast<int64_t>(getString().size())) {
return {}; // return null if index is out of bounds
}
return std::string(1, getString()[static_cast<size_t>(index)]);
}
default:
return {}; // return null for all other types
}
}
Value Value::operator[](size_t index) const noexcept {
if (!isArray()) return {};
auto const& array = getArray();
if (index >= array.size()) return {};
return array[index];
}
Value& Value::operator[](size_t index) noexcept {
if (!isArray()) {
m_type = Type::Array;
m_data = Array();
}
auto& array = std::get<Array>(m_data);
if (index >= array.size()) {
array.resize(index + 1);
}
return array[index];
}
Value Value::operator[](std::string const& key) const noexcept {
if (!isObject()) return {};
auto const& object = getObject();
auto it = object.find(key);
if (it == object.end()) return {};
return it->second;
}
Value& Value::operator[](std::string const &key) noexcept {
if (!isObject()) {
m_type = Type::Object;
m_data = Object();
}
auto& object = std::get<Object>(m_data);
return object[key];
}
}