usamin 7.7.8 → 7.7.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/README.md +3 -2
- data/ext/usamin/usamin.cpp +413 -1
- data/lib/usamin/overwrite.rb +17 -3
- data/lib/usamin/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: db59b9c360567cf33fca6506bbd06ad9b31c1382a09b31d13cd1c81c530fbdd0
|
4
|
+
data.tar.gz: c675678f1eec5cd39fae70733f36302664e020981a5d850c12536d755e9c9d7d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 78f82ae393b94944759de05c6e8dcc01524b9e60041f9fb13c72dbf43319265f6ad3aef8c226f23dcd8e205ee8385d0a773b7a1271a403b81bc6f093fca928ac
|
7
|
+
data.tar.gz: e51369d0d04fb97f9018c0734f3f72df4c404388234baa5f9db61b8c36380f2c197626df512906d3241dbbdc75938946a8d657e086f0263d3b94dc63afa46f03
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -69,9 +69,10 @@ Usamin.parse(json)
|
|
69
69
|
|
70
70
|
```ruby
|
71
71
|
data = Usamin.load(json)
|
72
|
+
#=> => [{...}, {...}]
|
72
73
|
```
|
73
74
|
|
74
|
-
Here, `data` is not
|
75
|
+
Here, `data` is not an Array, but this can be handled like an Array.
|
75
76
|
|
76
77
|
```ruby
|
77
78
|
data.size
|
@@ -107,7 +108,7 @@ Usamin.load(json).eval_r
|
|
107
108
|
#### Notes about lazy loading data
|
108
109
|
|
109
110
|
- Frozen. Modification is not allowed.
|
110
|
-
-
|
111
|
+
- Hash objects are based on not hash tables but arrays. An index access to an object costs O(n).
|
111
112
|
|
112
113
|
### Generating
|
113
114
|
|
data/ext/usamin/usamin.cpp
CHANGED
@@ -39,7 +39,7 @@ typedef rapidjson::GenericDocument<rapidjson::UTF8<>, rapidjson::MemoryPoolAlloc
|
|
39
39
|
|
40
40
|
rb_encoding *utf8;
|
41
41
|
int utf8index;
|
42
|
-
ID id_to_s;
|
42
|
+
ID id_dig, id_to_s;
|
43
43
|
VALUE rb_cUsaminValue, rb_cUsaminHash, rb_cUsaminArray, rb_eUsaminError, rb_eParserError;
|
44
44
|
VALUE utf8value, sym_fast, sym_indent, sym_single_line_array;
|
45
45
|
|
@@ -270,6 +270,110 @@ static inline VALUE eval_array_r(RubynizedValue &value) {
|
|
270
270
|
return ret;
|
271
271
|
}
|
272
272
|
|
273
|
+
static bool eql_array(RubynizedValue&, RubynizedValue&);
|
274
|
+
static bool eql_object(RubynizedValue&, RubynizedValue&);
|
275
|
+
|
276
|
+
static bool eql_value(RubynizedValue &self, RubynizedValue &other) {
|
277
|
+
if (self.GetType() != other.GetType())
|
278
|
+
return false;
|
279
|
+
switch (self.GetType()) {
|
280
|
+
case rapidjson::kObjectType:
|
281
|
+
return eql_object(self, other);
|
282
|
+
case rapidjson::kArrayType:
|
283
|
+
return eql_array(self, other);
|
284
|
+
case rapidjson::kStringType:
|
285
|
+
return self == other;
|
286
|
+
case rapidjson::kNumberType:
|
287
|
+
return self == other && self.IsInt() == other.IsInt() &&
|
288
|
+
self.IsUint() == other.IsUint() && self.IsInt64() == other.IsInt64() &&
|
289
|
+
self.IsUint64() == other.IsUint64() && self.IsDouble() == other.IsDouble();
|
290
|
+
default:
|
291
|
+
return true;
|
292
|
+
}
|
293
|
+
}
|
294
|
+
|
295
|
+
static bool eql_object(RubynizedValue &self, RubynizedValue &other) {
|
296
|
+
if (self.MemberCount() != other.MemberCount())
|
297
|
+
return false;
|
298
|
+
for (auto &m : self.GetObject()) {
|
299
|
+
if (!other.HasMember(m.name))
|
300
|
+
return false;
|
301
|
+
if (!eql_value(m.value, other[m.name]))
|
302
|
+
return false;
|
303
|
+
}
|
304
|
+
return true;
|
305
|
+
}
|
306
|
+
|
307
|
+
static bool eql_array(RubynizedValue &self, RubynizedValue &other) {
|
308
|
+
if (self.Size() != other.Size())
|
309
|
+
return false;
|
310
|
+
for (rapidjson::SizeType i = 0; i < self.Size(); i++) {
|
311
|
+
if (!eql_value(self[i], other[i]))
|
312
|
+
return false;
|
313
|
+
}
|
314
|
+
return true;
|
315
|
+
}
|
316
|
+
|
317
|
+
static st_index_t hash_object(RubynizedValue &value);
|
318
|
+
static st_index_t hash_array(RubynizedValue &value);
|
319
|
+
|
320
|
+
static st_index_t hash_value(RubynizedValue &value) {
|
321
|
+
auto type = value.GetType();
|
322
|
+
st_index_t h = rb_hash_start((st_index_t)type);
|
323
|
+
rb_hash_uint(h, (st_index_t)hash_value);
|
324
|
+
switch (type) {
|
325
|
+
case rapidjson::kNullType:
|
326
|
+
h = rb_hash_uint(h, NUM2LONG(rb_hash(Qnil)));
|
327
|
+
break;
|
328
|
+
case rapidjson::kFalseType:
|
329
|
+
h = rb_hash_uint(h, NUM2LONG(rb_hash(Qfalse)));
|
330
|
+
break;
|
331
|
+
case rapidjson::kTrueType:
|
332
|
+
h = rb_hash_uint(h, NUM2LONG(rb_hash(Qtrue)));
|
333
|
+
break;
|
334
|
+
case rapidjson::kObjectType:
|
335
|
+
h = rb_hash_uint(h, hash_object(value));
|
336
|
+
break;
|
337
|
+
case rapidjson::kArrayType:
|
338
|
+
h = rb_hash_uint(h, hash_array(value));
|
339
|
+
break;
|
340
|
+
case rapidjson::kStringType:
|
341
|
+
h = rb_hash_uint(h, rb_str_hash(eval_str(value)));
|
342
|
+
break;
|
343
|
+
case rapidjson::kNumberType:
|
344
|
+
if (value.IsInt())
|
345
|
+
h = rb_hash_uint(h, 0x770);
|
346
|
+
if (value.IsUint())
|
347
|
+
h = rb_hash_uint(h, 0x771);
|
348
|
+
if (value.IsInt64())
|
349
|
+
h = rb_hash_uint(h, 0x772);
|
350
|
+
if (value.IsUint64())
|
351
|
+
h = rb_hash_uint(h, 0x773);
|
352
|
+
if (value.IsDouble())
|
353
|
+
h = rb_hash_uint(h, 0x774);
|
354
|
+
h = rb_hash_uint(h, NUM2LONG(rb_hash(eval_num(value))));
|
355
|
+
}
|
356
|
+
return rb_hash_end(h);
|
357
|
+
}
|
358
|
+
|
359
|
+
static st_index_t hash_object(RubynizedValue &value) {
|
360
|
+
st_index_t h = rb_hash_start(value.MemberCount());
|
361
|
+
h = rb_hash_uint(h, (st_index_t)hash_object);
|
362
|
+
for (auto &m : value.GetObject()) {
|
363
|
+
h = rb_hash_uint(h, rb_str_hash(eval_str(m.name)));
|
364
|
+
h = rb_hash_uint(h, hash_value(m.value));
|
365
|
+
}
|
366
|
+
return rb_hash_end(h);
|
367
|
+
}
|
368
|
+
|
369
|
+
static st_index_t hash_array(RubynizedValue &value) {
|
370
|
+
st_index_t h = rb_hash_start(value.Size());
|
371
|
+
h = rb_hash_uint(h, (st_index_t)hash_array);
|
372
|
+
for (auto &v : value.GetArray())
|
373
|
+
h = rb_hash_uint(h, hash_value(v));
|
374
|
+
return rb_hash_end(h);
|
375
|
+
}
|
376
|
+
|
273
377
|
|
274
378
|
template <class Writer> static inline void write_str(Writer&, const VALUE);
|
275
379
|
template <class Writer> static inline void write_hash(Writer&, const VALUE);
|
@@ -515,6 +619,32 @@ static VALUE w_parse(const int argc, const VALUE *argv, const VALUE self) {
|
|
515
619
|
return eval_r(doc);
|
516
620
|
}
|
517
621
|
|
622
|
+
/*
|
623
|
+
* Returns whether the value is equals to the other value.
|
624
|
+
*
|
625
|
+
* @return [Boolean]
|
626
|
+
*/
|
627
|
+
static VALUE w_value_equal(const VALUE self, const VALUE other) {
|
628
|
+
if (self == other)
|
629
|
+
return Qtrue;
|
630
|
+
UsaminValue *value_self = get_value(self);
|
631
|
+
check_value(value_self);
|
632
|
+
if (rb_obj_is_kind_of(other, rb_cUsaminValue)) {
|
633
|
+
UsaminValue *value_other = get_value(other);
|
634
|
+
check_value(value_other);
|
635
|
+
return value_self->value->operator==(*value_other->value) ? Qtrue : Qfalse;
|
636
|
+
} else if (value_self->value->IsArray() && RB_TYPE_P(other, T_ARRAY)) {
|
637
|
+
if (value_self->value->Size() != RARRAY_LEN(other))
|
638
|
+
return Qfalse;
|
639
|
+
return rb_equal(other, eval_array(*value_self->value, value_self->root_document));
|
640
|
+
} else if (value_self->value->IsObject() && RB_TYPE_P(other, T_HASH)) {
|
641
|
+
if (value_self->value->MemberCount() != RHASH_SIZE(other))
|
642
|
+
return Qfalse;
|
643
|
+
return rb_equal(other, eval_object(*value_self->value, value_self->root_document));
|
644
|
+
}
|
645
|
+
return Qfalse;
|
646
|
+
}
|
647
|
+
|
518
648
|
/*
|
519
649
|
* Returns whether the value is array.
|
520
650
|
*/
|
@@ -560,6 +690,21 @@ static VALUE w_value_eval_r(const VALUE self) {
|
|
560
690
|
return eval_r(*(value->value));
|
561
691
|
}
|
562
692
|
|
693
|
+
/*
|
694
|
+
* @return [Boolean]
|
695
|
+
*/
|
696
|
+
static VALUE w_value_eql(const VALUE self, const VALUE other) {
|
697
|
+
if (self == other)
|
698
|
+
return Qtrue;
|
699
|
+
if (!rb_obj_is_kind_of(other, rb_cUsaminValue))
|
700
|
+
return Qfalse;
|
701
|
+
UsaminValue *value_self = get_value(self);
|
702
|
+
UsaminValue *value_other = get_value(other);
|
703
|
+
check_value(value_self);
|
704
|
+
check_value(value_other);
|
705
|
+
return eql_value(*value_self->value, *value_other->value) ? Qtrue : Qfalse;
|
706
|
+
}
|
707
|
+
|
563
708
|
/*
|
564
709
|
* Always true.
|
565
710
|
*/
|
@@ -567,6 +712,15 @@ static VALUE w_value_isfrozen(const VALUE self) {
|
|
567
712
|
return Qtrue;
|
568
713
|
}
|
569
714
|
|
715
|
+
/*
|
716
|
+
* @return [Integer]
|
717
|
+
*/
|
718
|
+
static VALUE w_value_hash(const VALUE self) {
|
719
|
+
UsaminValue *value = get_value(self);
|
720
|
+
check_value(value);
|
721
|
+
return ST2FIX(hash_value(*value->value));
|
722
|
+
}
|
723
|
+
|
570
724
|
/*
|
571
725
|
* Returns root object.
|
572
726
|
*
|
@@ -657,6 +811,20 @@ static VALUE w_hash_compact(const VALUE self) {
|
|
657
811
|
return hash;
|
658
812
|
}
|
659
813
|
|
814
|
+
/*
|
815
|
+
* @return [Object | nil]
|
816
|
+
*/
|
817
|
+
static VALUE w_hash_dig(const int argc, const VALUE *argv, const VALUE self) {
|
818
|
+
rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
|
819
|
+
VALUE value = w_hash_operator_indexer(self, argv[0]);
|
820
|
+
if (argc == 1)
|
821
|
+
return value;
|
822
|
+
else if (value == Qnil)
|
823
|
+
return Qnil;
|
824
|
+
else
|
825
|
+
return rb_funcall3(value, id_dig, argc - 1, argv + 1);
|
826
|
+
}
|
827
|
+
|
660
828
|
static VALUE hash_enum_size(const VALUE self, const VALUE args, const VALUE eobj) {
|
661
829
|
return UINT2NUM(get_value(self)->value->MemberCount());
|
662
830
|
}
|
@@ -767,6 +935,49 @@ static VALUE w_hash_fetch_values(const int argc, VALUE *argv, const VALUE self)
|
|
767
935
|
return ret;
|
768
936
|
}
|
769
937
|
|
938
|
+
static void flatten_array(RubynizedValue &value, VALUE array, int level, VALUE root_document);
|
939
|
+
|
940
|
+
/*
|
941
|
+
* @overload flatten(level = 1)
|
942
|
+
* @return [::Array<Object>]
|
943
|
+
*/
|
944
|
+
static VALUE w_hash_flatten(const int argc, const VALUE *argv, const VALUE self) {
|
945
|
+
rb_check_arity(argc, 0, 1);
|
946
|
+
UsaminValue *value = get_value(self);
|
947
|
+
check_object(value);
|
948
|
+
|
949
|
+
int level = 1;
|
950
|
+
if (argc == 1 && !NIL_P(argv[0])) {
|
951
|
+
level = NUM2INT(argv[0]);
|
952
|
+
if (level < 0)
|
953
|
+
level = -1;
|
954
|
+
}
|
955
|
+
|
956
|
+
if (level == 0) {
|
957
|
+
VALUE ret = rb_ary_new2(value->value->MemberCount());
|
958
|
+
for (auto &m : value->value->GetObject())
|
959
|
+
rb_ary_push(ret, rb_ary_new3(2, eval_str(m.name), eval(m.value, value->root_document)));
|
960
|
+
return ret;
|
961
|
+
}
|
962
|
+
|
963
|
+
VALUE ret = rb_ary_new2(value->value->MemberCount() * 2);
|
964
|
+
if (level == 1) {
|
965
|
+
for (auto &m : value->value->GetObject()) {
|
966
|
+
rb_ary_push(ret, eval_str(m.name));
|
967
|
+
rb_ary_push(ret, eval(m.value, value->root_document));
|
968
|
+
}
|
969
|
+
} else {
|
970
|
+
for (auto &m : value->value->GetObject()) {
|
971
|
+
rb_ary_push(ret, eval_str(m.name));
|
972
|
+
if (m.value.IsArray())
|
973
|
+
flatten_array(m.value, ret, level > 0 ? level - 2 : level, value->root_document);
|
974
|
+
else
|
975
|
+
rb_ary_push(ret, eval(m.value, value->root_document));
|
976
|
+
}
|
977
|
+
}
|
978
|
+
return ret;
|
979
|
+
}
|
980
|
+
|
770
981
|
/*
|
771
982
|
* @note This method has linear time complexity.
|
772
983
|
*/
|
@@ -800,6 +1011,36 @@ static VALUE w_hash_key(const VALUE self, const VALUE val) {
|
|
800
1011
|
return Qnil;
|
801
1012
|
}
|
802
1013
|
|
1014
|
+
/*
|
1015
|
+
* @return [String]
|
1016
|
+
*/
|
1017
|
+
static VALUE w_hash_inspect(const VALUE self) {
|
1018
|
+
UsaminValue *value = get_value(self);
|
1019
|
+
check_object(value);
|
1020
|
+
VALUE ret = rb_str_new2("{");
|
1021
|
+
bool first = true;
|
1022
|
+
for (auto &m : value->value->GetObject()) {
|
1023
|
+
if (!first)
|
1024
|
+
ret = rb_str_cat2(ret, ", ");
|
1025
|
+
ret = rb_str_append(ret, rb_inspect(eval_str(m.name)));
|
1026
|
+
ret = rb_str_cat2(ret, "=>");
|
1027
|
+
switch (m.value.GetType()) {
|
1028
|
+
case rapidjson::kObjectType:
|
1029
|
+
ret = rb_str_cat2(ret, "{...}");
|
1030
|
+
break;
|
1031
|
+
case rapidjson::kArrayType:
|
1032
|
+
ret = rb_str_cat2(ret, "[...]");
|
1033
|
+
break;
|
1034
|
+
default:
|
1035
|
+
ret = rb_str_append(ret, rb_inspect(eval(m.value, value->root_document)));
|
1036
|
+
break;
|
1037
|
+
}
|
1038
|
+
first = false;
|
1039
|
+
}
|
1040
|
+
ret = rb_str_cat2(ret, "}");
|
1041
|
+
return ret;
|
1042
|
+
}
|
1043
|
+
|
803
1044
|
/*
|
804
1045
|
* @return [::Array<String>]
|
805
1046
|
*/
|
@@ -1068,6 +1309,20 @@ static VALUE w_array_compact(const VALUE self, const VALUE nth) {
|
|
1068
1309
|
return ret;
|
1069
1310
|
}
|
1070
1311
|
|
1312
|
+
/*
|
1313
|
+
* @return [Object | nil]
|
1314
|
+
*/
|
1315
|
+
static VALUE w_array_dig(const int argc, const VALUE *argv, const VALUE self) {
|
1316
|
+
rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
|
1317
|
+
VALUE value = w_array_at(self, argv[0]);
|
1318
|
+
if (argc == 1)
|
1319
|
+
return value;
|
1320
|
+
else if (value == Qnil)
|
1321
|
+
return Qnil;
|
1322
|
+
else
|
1323
|
+
return rb_funcall3(value, id_dig, argc - 1, argv + 1);
|
1324
|
+
}
|
1325
|
+
|
1071
1326
|
static VALUE array_enum_size(const VALUE self, const VALUE args, const VALUE eobj) {
|
1072
1327
|
return UINT2NUM(get_value(self)->value->Size());
|
1073
1328
|
}
|
@@ -1173,6 +1428,39 @@ static VALUE w_array_find_index(const int argc, const VALUE *argv, const VALUE s
|
|
1173
1428
|
return Qnil;
|
1174
1429
|
}
|
1175
1430
|
|
1431
|
+
static void flatten_array(RubynizedValue &value, VALUE array, int level, VALUE root_document) {
|
1432
|
+
if (level == 0)
|
1433
|
+
for (auto &v : value.GetArray())
|
1434
|
+
rb_ary_push(array, eval(v, root_document));
|
1435
|
+
else
|
1436
|
+
for (auto &v : value.GetArray())
|
1437
|
+
if (v.IsArray())
|
1438
|
+
flatten_array(v, array, level > 0 ? level - 1 : level, root_document);
|
1439
|
+
else
|
1440
|
+
rb_ary_push(array, eval(v, root_document));
|
1441
|
+
}
|
1442
|
+
|
1443
|
+
/*
|
1444
|
+
* @overload flatten(lv = nil)
|
1445
|
+
* @return [::Array<Object>]
|
1446
|
+
*/
|
1447
|
+
static VALUE w_array_flatten(const int argc, const VALUE *argv, const VALUE self) {
|
1448
|
+
rb_check_arity(argc, 0, 1);
|
1449
|
+
UsaminValue *value = get_value(self);
|
1450
|
+
check_array(value);
|
1451
|
+
|
1452
|
+
int level = -1;
|
1453
|
+
if (argc == 1 && !NIL_P(argv[0])) {
|
1454
|
+
level = NUM2INT(argv[0]);
|
1455
|
+
if (level <= 0)
|
1456
|
+
return eval_array(*value->value, value->root_document);
|
1457
|
+
}
|
1458
|
+
|
1459
|
+
VALUE ret = rb_ary_new2(value->value->Size());
|
1460
|
+
flatten_array(*value->value, ret, level, value->root_document);
|
1461
|
+
return ret;
|
1462
|
+
}
|
1463
|
+
|
1176
1464
|
/*
|
1177
1465
|
* @overload index(val)
|
1178
1466
|
* @param [Object] val
|
@@ -1227,6 +1515,34 @@ static VALUE w_array_include(const VALUE self, const VALUE val) {
|
|
1227
1515
|
return Qfalse;
|
1228
1516
|
}
|
1229
1517
|
|
1518
|
+
/*
|
1519
|
+
* @return [String]
|
1520
|
+
*/
|
1521
|
+
static VALUE w_array_inspect(const VALUE self) {
|
1522
|
+
UsaminValue *value = get_value(self);
|
1523
|
+
check_array(value);
|
1524
|
+
VALUE ret = rb_str_new2("[");
|
1525
|
+
bool first = true;
|
1526
|
+
for (auto &v : value->value->GetArray()) {
|
1527
|
+
if (!first)
|
1528
|
+
ret = rb_str_cat2(ret, ", ");
|
1529
|
+
switch (v.GetType()) {
|
1530
|
+
case rapidjson::kObjectType:
|
1531
|
+
ret = rb_str_cat2(ret, "{...}");
|
1532
|
+
break;
|
1533
|
+
case rapidjson::kArrayType:
|
1534
|
+
ret = rb_str_cat2(ret, "[...]");
|
1535
|
+
break;
|
1536
|
+
default:
|
1537
|
+
ret = rb_str_append(ret, rb_inspect(eval(v, value->root_document)));
|
1538
|
+
break;
|
1539
|
+
}
|
1540
|
+
first = false;
|
1541
|
+
}
|
1542
|
+
ret = rb_str_cat2(ret, "]");
|
1543
|
+
return ret;
|
1544
|
+
}
|
1545
|
+
|
1230
1546
|
/*
|
1231
1547
|
* @overload last
|
1232
1548
|
* @return [Object | nil]
|
@@ -1262,6 +1578,86 @@ static VALUE w_array_length(const VALUE self) {
|
|
1262
1578
|
return UINT2NUM(value->value->Size());
|
1263
1579
|
}
|
1264
1580
|
|
1581
|
+
/*
|
1582
|
+
* @return [::Array<Object>]
|
1583
|
+
*/
|
1584
|
+
static VALUE w_array_reverse(const VALUE self) {
|
1585
|
+
UsaminValue *value = get_value(self);
|
1586
|
+
check_array(value);
|
1587
|
+
|
1588
|
+
VALUE ret = rb_ary_new2(value->value->Size());
|
1589
|
+
for (rapidjson::SizeType i = 0, j = value->value->Size() - 1; i < value->value->Size(); i++, j--)
|
1590
|
+
rb_ary_push(ret, eval((*value->value)[j], value->root_document));
|
1591
|
+
return ret;
|
1592
|
+
}
|
1593
|
+
|
1594
|
+
/*
|
1595
|
+
* @overload rindex(val)
|
1596
|
+
* @param [Object] val
|
1597
|
+
* @return [Integer | nil]
|
1598
|
+
*
|
1599
|
+
* @overload rindex
|
1600
|
+
* @yield [item]
|
1601
|
+
* @yieldparam item [Object]
|
1602
|
+
* @return [Integer | nil]
|
1603
|
+
*/
|
1604
|
+
static VALUE w_array_rindex(const int argc, const VALUE *argv, const VALUE self) {
|
1605
|
+
rb_check_arity(argc, 0, 1);
|
1606
|
+
UsaminValue *value = get_value(self);
|
1607
|
+
check_array(value);
|
1608
|
+
|
1609
|
+
if (argc == 1) {
|
1610
|
+
for (rapidjson::SizeType i = 0, j = value->value->Size() - 1; i < value->value->Size(); i++, j--) {
|
1611
|
+
if (rb_equal(argv[0], eval_r((*value->value)[j])) == Qtrue)
|
1612
|
+
return UINT2NUM(j);
|
1613
|
+
}
|
1614
|
+
return Qnil;
|
1615
|
+
}
|
1616
|
+
|
1617
|
+
RETURN_SIZED_ENUMERATOR(self, 0, nullptr, array_enum_size);
|
1618
|
+
for (rapidjson::SizeType i = 0, j = value->value->Size() - 1; i < value->value->Size(); i++, j--) {
|
1619
|
+
if (RTEST(rb_yield(eval((*value->value)[j], value->root_document))))
|
1620
|
+
return UINT2NUM(j);
|
1621
|
+
}
|
1622
|
+
return Qnil;
|
1623
|
+
}
|
1624
|
+
|
1625
|
+
/*
|
1626
|
+
* @overload rotate(cnt = 1)
|
1627
|
+
* @return [::Array<Object>]
|
1628
|
+
*/
|
1629
|
+
static VALUE w_array_rotate(const int argc, const VALUE *argv, const VALUE self) {
|
1630
|
+
rb_check_arity(argc, 0, 1);
|
1631
|
+
UsaminValue *value = get_value(self);
|
1632
|
+
check_array(value);
|
1633
|
+
|
1634
|
+
switch (value->value->Size()) {
|
1635
|
+
case 0:
|
1636
|
+
return rb_ary_new();
|
1637
|
+
case 1:
|
1638
|
+
return rb_ary_new3(1, eval(value->value->operator[](0), value->root_document));
|
1639
|
+
}
|
1640
|
+
|
1641
|
+
int cnt = argc == 1 ? NUM2INT(argv[0]) : 1;
|
1642
|
+
if (cnt >= 0) {
|
1643
|
+
cnt = cnt % value->value->Size();
|
1644
|
+
} else {
|
1645
|
+
cnt = -cnt % value->value->Size();
|
1646
|
+
if (cnt)
|
1647
|
+
cnt = value->value->Size() - cnt;
|
1648
|
+
}
|
1649
|
+
if (cnt == 0)
|
1650
|
+
return eval_array(*(value->value), value->root_document);
|
1651
|
+
|
1652
|
+
rapidjson::SizeType ucnt = cnt;
|
1653
|
+
VALUE ret = rb_ary_new2(value->value->Size());
|
1654
|
+
for (rapidjson::SizeType i = ucnt; i < value->value->Size(); i++)
|
1655
|
+
rb_ary_push(ret, eval((*value->value)[i], value->root_document));
|
1656
|
+
for (rapidjson::SizeType i = 0; i < ucnt; i++)
|
1657
|
+
rb_ary_push(ret, eval((*value->value)[i], value->root_document));
|
1658
|
+
return ret;
|
1659
|
+
}
|
1660
|
+
|
1265
1661
|
/*
|
1266
1662
|
* @overload slice(nth)
|
1267
1663
|
* @param [Integer] nth
|
@@ -1371,6 +1767,7 @@ extern "C" void Init_usamin(void) {
|
|
1371
1767
|
sym_fast = rb_id2sym(rb_intern("fast"));
|
1372
1768
|
sym_indent = rb_id2sym(rb_intern("indent"));
|
1373
1769
|
sym_single_line_array = rb_id2sym(rb_intern("single_line_array"));
|
1770
|
+
id_dig = rb_intern("dig");
|
1374
1771
|
id_to_s = rb_intern("to_s");
|
1375
1772
|
|
1376
1773
|
VALUE rb_mUsamin = rb_define_module("Usamin");
|
@@ -1382,12 +1779,16 @@ extern "C" void Init_usamin(void) {
|
|
1382
1779
|
rb_cUsaminValue = rb_define_class_under(rb_mUsamin, "Value", rb_cObject);
|
1383
1780
|
rb_undef_alloc_func(rb_cUsaminValue);
|
1384
1781
|
rb_undef_method(rb_cUsaminValue, "initialize");
|
1782
|
+
rb_define_method(rb_cUsaminValue, "==", RUBY_METHOD_FUNC(w_value_equal), 1);
|
1783
|
+
rb_define_method(rb_cUsaminValue, "===", RUBY_METHOD_FUNC(w_value_equal), 1);
|
1385
1784
|
rb_define_method(rb_cUsaminValue, "array?", RUBY_METHOD_FUNC(w_value_isarray), 0);
|
1386
1785
|
rb_define_method(rb_cUsaminValue, "hash?", RUBY_METHOD_FUNC(w_value_isobject), 0);
|
1387
1786
|
rb_define_method(rb_cUsaminValue, "object?", RUBY_METHOD_FUNC(w_value_isobject), 0);
|
1388
1787
|
rb_define_method(rb_cUsaminValue, "eval", RUBY_METHOD_FUNC(w_value_eval), 0);
|
1389
1788
|
rb_define_method(rb_cUsaminValue, "eval_r", RUBY_METHOD_FUNC(w_value_eval_r), 0);
|
1789
|
+
rb_define_method(rb_cUsaminValue, "eql?", RUBY_METHOD_FUNC(w_value_eql), 1);
|
1390
1790
|
rb_define_method(rb_cUsaminValue, "frozen?", RUBY_METHOD_FUNC(w_value_isfrozen), 0);
|
1791
|
+
rb_define_method(rb_cUsaminValue, "hash", RUBY_METHOD_FUNC(w_value_hash), 0);
|
1391
1792
|
rb_define_method(rb_cUsaminValue, "root", RUBY_METHOD_FUNC(w_value_root), 0);
|
1392
1793
|
|
1393
1794
|
rb_cUsaminHash = rb_define_class_under(rb_mUsamin, "Hash", rb_cUsaminValue);
|
@@ -1397,6 +1798,7 @@ extern "C" void Init_usamin(void) {
|
|
1397
1798
|
rb_define_method(rb_cUsaminHash, "[]", RUBY_METHOD_FUNC(w_hash_operator_indexer), 1);
|
1398
1799
|
rb_define_method(rb_cUsaminHash, "assoc", RUBY_METHOD_FUNC(w_hash_assoc), 1);
|
1399
1800
|
rb_define_method(rb_cUsaminHash, "compact", RUBY_METHOD_FUNC(w_hash_compact), 0);
|
1801
|
+
rb_define_method(rb_cUsaminHash, "dig", RUBY_METHOD_FUNC(w_hash_dig), -1);
|
1400
1802
|
rb_define_method(rb_cUsaminHash, "each", RUBY_METHOD_FUNC(w_hash_each), 0);
|
1401
1803
|
rb_define_method(rb_cUsaminHash, "each_pair", RUBY_METHOD_FUNC(w_hash_each), 0);
|
1402
1804
|
rb_define_method(rb_cUsaminHash, "each_key", RUBY_METHOD_FUNC(w_hash_each_key), 0);
|
@@ -1404,6 +1806,7 @@ extern "C" void Init_usamin(void) {
|
|
1404
1806
|
rb_define_method(rb_cUsaminHash, "empty?", RUBY_METHOD_FUNC(w_hash_isempty), 0);
|
1405
1807
|
rb_define_method(rb_cUsaminHash, "fetch", RUBY_METHOD_FUNC(w_hash_fetch), -1);
|
1406
1808
|
rb_define_method(rb_cUsaminHash, "fetch_values", RUBY_METHOD_FUNC(w_hash_fetch_values), -1);
|
1809
|
+
rb_define_method(rb_cUsaminHash, "flatten", RUBY_METHOD_FUNC(w_hash_flatten), -1);
|
1407
1810
|
rb_define_method(rb_cUsaminHash, "has_key?", RUBY_METHOD_FUNC(w_hash_haskey), 1);
|
1408
1811
|
rb_define_method(rb_cUsaminHash, "include?", RUBY_METHOD_FUNC(w_hash_haskey), 1);
|
1409
1812
|
rb_define_method(rb_cUsaminHash, "key?", RUBY_METHOD_FUNC(w_hash_haskey), 1);
|
@@ -1412,6 +1815,8 @@ extern "C" void Init_usamin(void) {
|
|
1412
1815
|
rb_define_method(rb_cUsaminHash, "value?", RUBY_METHOD_FUNC(w_hash_hasvalue), 1);
|
1413
1816
|
rb_define_method(rb_cUsaminHash, "key", RUBY_METHOD_FUNC(w_hash_key), 1);
|
1414
1817
|
rb_define_method(rb_cUsaminHash, "index", RUBY_METHOD_FUNC(w_hash_key), 1);
|
1818
|
+
rb_define_method(rb_cUsaminHash, "inspect", RUBY_METHOD_FUNC(w_hash_inspect), 0);
|
1819
|
+
rb_define_method(rb_cUsaminHash, "to_s", RUBY_METHOD_FUNC(w_hash_inspect), 0);
|
1415
1820
|
rb_define_method(rb_cUsaminHash, "keys", RUBY_METHOD_FUNC(w_hash_keys), 0);
|
1416
1821
|
rb_define_method(rb_cUsaminHash, "length", RUBY_METHOD_FUNC(w_hash_length), 0);
|
1417
1822
|
rb_define_method(rb_cUsaminHash, "size", RUBY_METHOD_FUNC(w_hash_length), 0);
|
@@ -1435,16 +1840,23 @@ extern "C" void Init_usamin(void) {
|
|
1435
1840
|
rb_define_method(rb_cUsaminArray, "[]", RUBY_METHOD_FUNC(w_array_operator_indexer), -1);
|
1436
1841
|
rb_define_method(rb_cUsaminArray, "at", RUBY_METHOD_FUNC(w_array_at), 1);
|
1437
1842
|
rb_define_method(rb_cUsaminArray, "compact", RUBY_METHOD_FUNC(w_array_compact), 0);
|
1843
|
+
rb_define_method(rb_cUsaminArray, "dig", RUBY_METHOD_FUNC(w_array_dig), -1);
|
1438
1844
|
rb_define_method(rb_cUsaminArray, "each", RUBY_METHOD_FUNC(w_array_each), 0);
|
1439
1845
|
rb_define_method(rb_cUsaminArray, "each_index", RUBY_METHOD_FUNC(w_array_each_index), 0);
|
1440
1846
|
rb_define_method(rb_cUsaminArray, "empty?", RUBY_METHOD_FUNC(w_array_isempty), 0);
|
1441
1847
|
rb_define_method(rb_cUsaminArray, "fetch", RUBY_METHOD_FUNC(w_array_fetch), -1);
|
1442
1848
|
rb_define_method(rb_cUsaminArray, "find_index", RUBY_METHOD_FUNC(w_array_find_index), -1);
|
1849
|
+
rb_define_method(rb_cUsaminArray, "flatten", RUBY_METHOD_FUNC(w_array_flatten), -1);
|
1443
1850
|
rb_define_method(rb_cUsaminArray, "index", RUBY_METHOD_FUNC(w_array_index), -1);
|
1444
1851
|
rb_define_method(rb_cUsaminArray, "first", RUBY_METHOD_FUNC(w_array_first), -1);
|
1445
1852
|
rb_define_method(rb_cUsaminArray, "include?", RUBY_METHOD_FUNC(w_array_include), 1);
|
1853
|
+
rb_define_method(rb_cUsaminArray, "inspect", RUBY_METHOD_FUNC(w_array_inspect), 0);
|
1854
|
+
rb_define_method(rb_cUsaminArray, "to_s", RUBY_METHOD_FUNC(w_array_inspect), 0);
|
1446
1855
|
rb_define_method(rb_cUsaminArray, "last", RUBY_METHOD_FUNC(w_array_last), -1);
|
1447
1856
|
rb_define_method(rb_cUsaminArray, "length", RUBY_METHOD_FUNC(w_array_length), 0);
|
1857
|
+
rb_define_method(rb_cUsaminArray, "reverse", RUBY_METHOD_FUNC(w_array_reverse), 0);
|
1858
|
+
rb_define_method(rb_cUsaminArray, "rindex", RUBY_METHOD_FUNC(w_array_rindex), -1);
|
1859
|
+
rb_define_method(rb_cUsaminArray, "rotate", RUBY_METHOD_FUNC(w_array_rotate), -1);
|
1448
1860
|
rb_define_method(rb_cUsaminArray, "size", RUBY_METHOD_FUNC(w_array_length), 0);
|
1449
1861
|
rb_define_method(rb_cUsaminArray, "slice", RUBY_METHOD_FUNC(w_array_slice), -1);
|
1450
1862
|
rb_define_method(rb_cUsaminArray, "to_ary", RUBY_METHOD_FUNC(w_array_eval), 0);
|
data/lib/usamin/overwrite.rb
CHANGED
@@ -10,15 +10,29 @@ module JSON
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def self.parse(source, **options)
|
13
|
-
Usamin.parse(
|
13
|
+
Usamin.parse(source)
|
14
14
|
end
|
15
15
|
|
16
16
|
def self.load(source, proc = nil, **options)
|
17
|
-
ret = Usamin.parse(
|
18
|
-
|
17
|
+
ret = Usamin.parse(source)
|
18
|
+
LoadProcCaller.load_call_proc(ret, proc) if proc
|
19
19
|
ret
|
20
20
|
end
|
21
21
|
|
22
|
+
class LoadProcCaller
|
23
|
+
def self.load_call_proc(object, proc)
|
24
|
+
if object.is_a?(Array)
|
25
|
+
object.each{|e| load_call_proc(e, proc)}
|
26
|
+
elsif object.is_a?(Hash)
|
27
|
+
object.each do |k, v|
|
28
|
+
proc.call(k)
|
29
|
+
load_call_proc(v, proc)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
proc.call(object)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
22
36
|
class << self
|
23
37
|
alias fast_generate generate
|
24
38
|
alias restore load
|
data/lib/usamin/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: usamin
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 7.7.
|
4
|
+
version: 7.7.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ishotihadus
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-08-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -107,7 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
107
107
|
- !ruby/object:Gem::Version
|
108
108
|
version: '0'
|
109
109
|
requirements: []
|
110
|
-
rubygems_version: 3.0.
|
110
|
+
rubygems_version: 3.0.3
|
111
111
|
signing_key:
|
112
112
|
specification_version: 4
|
113
113
|
summary: A RapidJSON Wrapper for Ruby
|