teptris 0.2.42-arm-linux → 0.2.47-arm-linux
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/ext/teptris_ext/engine/README.md +1 -1
- data/ext/teptris_ext/engine/src/include/teptris/teptris.h +13 -0
- data/ext/teptris_ext/engine/src/include/teptris/version.h +2 -2
- data/ext/teptris_ext/engine/src/teptris/api.c +53 -0
- data/ext/teptris_ext/materialize.c +385 -14
- data/lib/teptris/3.3/teptris_ext.so +0 -0
- data/lib/teptris/3.4/teptris_ext.so +0 -0
- data/lib/teptris/4.0/teptris_ext.so +0 -0
- data/lib/teptris/toml.rb +61 -0
- data/lib/teptris/version.rb +1 -1
- metadata +6 -4
- data/lib/teptris/3.2/teptris_ext.so +0 -0
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 5fbc2453ab0e06567907fc4baa61729b192a8bd998eccab870f4edeff8f51599
|
|
4
|
+
data.tar.gz: 1764e0c4c16a03eaa22f4defcdf0213c9f7cb5633ed4cccb1110770f52db0590
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 0bbf7c73d750047f422618dda1ba9144e1d2b87b693b0e7a723bab3fc4d3d6a785a61542614f87bec1fea192e4d164caa4aaa942a4840cfaa502a2f29238eff9
|
|
7
|
+
data.tar.gz: 19e10c96d3fcf53aba977a2ba060ac02df62ecce174ce50820295425be2727dd676b1cb48399dff100514753b5ca1f15a3887824a9952f7b3fa144ab833927cf
|
|
@@ -91,6 +91,19 @@ TEPTRIS_API teptris_status teptris_parse(const char *data, size_t len,
|
|
|
91
91
|
const teptris_options *opts,
|
|
92
92
|
teptris_document **out);
|
|
93
93
|
|
|
94
|
+
/* Parse N TOML documents in a single call. Caller pre-allocates
|
|
95
|
+
* `out_docs[N]` and `out_statuses[N]` and frees each doc with
|
|
96
|
+
* teptris_document_free. Per-document parse failures are reported
|
|
97
|
+
* through out_statuses[i] (out_docs[i] is set so the caller can read
|
|
98
|
+
* the error via teptris_document_error and free). TEPTRIS_ERR_ALLOC
|
|
99
|
+
* means no documents were created — caller must not free. The `opts`
|
|
100
|
+
* pointer is shared across the batch. n == 0 is TEPTRIS_OK and a
|
|
101
|
+
* no-op. */
|
|
102
|
+
TEPTRIS_API teptris_status teptris_parse_batch(
|
|
103
|
+
const char *const *data, const size_t *lens, size_t n,
|
|
104
|
+
const teptris_options *opts,
|
|
105
|
+
teptris_document **out_docs, teptris_status *out_statuses);
|
|
106
|
+
|
|
94
107
|
/* Memory: frees every allocation reachable from the document. */
|
|
95
108
|
TEPTRIS_API void teptris_document_free(teptris_document *doc);
|
|
96
109
|
|
|
@@ -34,6 +34,59 @@ teptris_status teptris_parse(const char *data, size_t len,
|
|
|
34
34
|
return st;
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
+
teptris_status teptris_parse_batch(const char *const *data, const size_t *lens,
|
|
38
|
+
size_t n, const teptris_options *opts,
|
|
39
|
+
teptris_document **out_docs,
|
|
40
|
+
teptris_status *out_statuses)
|
|
41
|
+
{
|
|
42
|
+
if (n == 0) {
|
|
43
|
+
return TEPTRIS_OK;
|
|
44
|
+
}
|
|
45
|
+
if (data == NULL || lens == NULL || out_docs == NULL ||
|
|
46
|
+
out_statuses == NULL) {
|
|
47
|
+
return TEPTRIS_ERR_ARG;
|
|
48
|
+
}
|
|
49
|
+
/* Zero the per-doc outputs up front: on the alloc-fail path every
|
|
50
|
+
* entry must look like "no doc to free" so callers don't double-free
|
|
51
|
+
* a partial batch. */
|
|
52
|
+
for (size_t i = 0; i < n; i++) {
|
|
53
|
+
out_docs[i] = NULL;
|
|
54
|
+
out_statuses[i] = TEPTRIS_OK;
|
|
55
|
+
}
|
|
56
|
+
uint32_t max_depth =
|
|
57
|
+
(opts != NULL && opts->max_depth != 0) ? opts->max_depth : 512;
|
|
58
|
+
for (size_t i = 0; i < n; i++) {
|
|
59
|
+
teptris_document *doc = calloc(1, sizeof(teptris_document));
|
|
60
|
+
if (doc == NULL) {
|
|
61
|
+
/* Free any documents we already created; statuses for the
|
|
62
|
+
* failed slot onward were zero-initialized, so out_statuses
|
|
63
|
+
* stays consistent. */
|
|
64
|
+
for (size_t j = 0; j < i; j++) {
|
|
65
|
+
teptris_arena_destroy(&out_docs[j]->arena);
|
|
66
|
+
free(out_docs[j]);
|
|
67
|
+
out_docs[j] = NULL;
|
|
68
|
+
}
|
|
69
|
+
return TEPTRIS_ERR_ALLOC;
|
|
70
|
+
}
|
|
71
|
+
teptris_arena_init(&doc->arena);
|
|
72
|
+
doc->max_depth = max_depth;
|
|
73
|
+
doc->err.status = TEPTRIS_OK;
|
|
74
|
+
doc->err.message = "";
|
|
75
|
+
teptris_status st = teptris_parser_run(doc, data[i], lens[i]);
|
|
76
|
+
if (st != TEPTRIS_OK && doc->err.status == TEPTRIS_OK) {
|
|
77
|
+
doc->err.status = st;
|
|
78
|
+
if (st == TEPTRIS_ERR_ALLOC) {
|
|
79
|
+
snprintf(doc->err_msg, sizeof(doc->err_msg),
|
|
80
|
+
"out of memory");
|
|
81
|
+
}
|
|
82
|
+
doc->err.message = doc->err_msg;
|
|
83
|
+
}
|
|
84
|
+
out_docs[i] = doc;
|
|
85
|
+
out_statuses[i] = st;
|
|
86
|
+
}
|
|
87
|
+
return TEPTRIS_OK;
|
|
88
|
+
}
|
|
89
|
+
|
|
37
90
|
void teptris_document_free(teptris_document *doc)
|
|
38
91
|
{
|
|
39
92
|
if (doc == NULL) {
|
|
@@ -21,18 +21,25 @@ static int64_t days_from_civil(int32_t y, uint8_t m, uint8_t d) {
|
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
/* exact (no double) Time materialization: nsec stays nsec. Offset
|
|
24
|
-
* datetimes
|
|
25
|
-
* Time.local with Rational seconds (
|
|
26
|
-
*
|
|
24
|
+
* datetimes carry a fixed utc offset via rb_time_num_new; local
|
|
25
|
+
* datetimes go through Time.local with Rational seconds (both treat
|
|
26
|
+
* the epoch value as wall-clock for locals / instant+offset for
|
|
27
|
+
* fixed zones). No struct timespec crosses the Ruby boundary:
|
|
28
|
+
* armhf glibc rubies build with _TIME_BITS=64 (16-byte timespec)
|
|
29
|
+
* while an ext compiled without it lays out 8 bytes — the struct
|
|
30
|
+
* APIs then read (load) or write (dump) past each side's layout. */
|
|
27
31
|
static VALUE time_from_dt(const teptris_datetime *d, bool has_offset) {
|
|
28
32
|
if (has_offset) {
|
|
29
33
|
int64_t days = days_from_civil(d->year, d->month, d->day);
|
|
30
34
|
int64_t secs =
|
|
31
35
|
days * 86400 + d->hour * 3600 + d->minute * 60 + d->second;
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
+
VALUE num = LL2NUM(secs - d->offset_seconds);
|
|
37
|
+
if (d->nanosecond != 0) {
|
|
38
|
+
num = rb_funcall(num, '+', 1,
|
|
39
|
+
rb_Rational(LL2NUM((int64_t)d->nanosecond),
|
|
40
|
+
LL2NUM(1000000000)));
|
|
41
|
+
}
|
|
42
|
+
return rb_time_num_new(num, INT2FIX(d->offset_seconds));
|
|
36
43
|
}
|
|
37
44
|
VALUE args[6] = {INT2FIX(d->year), INT2FIX(d->month), INT2FIX(d->day),
|
|
38
45
|
INT2FIX(d->hour), INT2FIX(d->minute),
|
|
@@ -51,7 +58,19 @@ static VALUE dt_string(const teptris_datetime *d, int kind) {
|
|
|
51
58
|
const char *head = (kind == TEPTRIS_TIME_LOCAL) ? "" : "";
|
|
52
59
|
(void)head;
|
|
53
60
|
if (kind == TEPTRIS_TIME_LOCAL) {
|
|
54
|
-
|
|
61
|
+
/* tomlib's contract keeps the fraction at millisecond
|
|
62
|
+
* precision (00:32:00.999); drop it entirely when zero.
|
|
63
|
+
* The engine preserves full nanoseconds — the binding
|
|
64
|
+
* truncates to the reference shape. */
|
|
65
|
+
if (d->nanosecond != 0) {
|
|
66
|
+
unsigned ms = (d->nanosecond + 500000u) / 1000000u;
|
|
67
|
+
if (ms == 1000u) ms = 999u; /* rounding overflow guard */
|
|
68
|
+
l = snprintf(buf, sizeof buf, "%02u:%02u:%02u.%03u",
|
|
69
|
+
d->hour, d->minute, d->second, ms);
|
|
70
|
+
} else {
|
|
71
|
+
l = snprintf(buf, sizeof buf, "%02u:%02u:%02u",
|
|
72
|
+
d->hour, d->minute, d->second);
|
|
73
|
+
}
|
|
55
74
|
} else {
|
|
56
75
|
l = snprintf(buf, sizeof buf, "%04d-%02u-%02uT%02u:%02u:%02u",
|
|
57
76
|
d->year, d->month, d->day, d->hour, d->minute, d->second);
|
|
@@ -168,7 +187,7 @@ static VALUE ext_version(VALUE self) {
|
|
|
168
187
|
|
|
169
188
|
/* ------------------------------------------------------------------ dump */
|
|
170
189
|
|
|
171
|
-
static ID id_utc_offset, id_to_s;
|
|
190
|
+
static ID id_utc_offset, id_to_s, id_to_i, id_nsec;
|
|
172
191
|
|
|
173
192
|
static void dump_check(teptris_status st) {
|
|
174
193
|
if (st == TEPTRIS_OK) return;
|
|
@@ -194,12 +213,12 @@ static void civil_from_days(int64_t z, int32_t *y, uint8_t *m, uint8_t *d) {
|
|
|
194
213
|
*d = (uint8_t)dd;
|
|
195
214
|
}
|
|
196
215
|
|
|
197
|
-
/*
|
|
198
|
-
*
|
|
216
|
+
/* the instant and its subsecond part come back as Integers (to_i
|
|
217
|
+
* floors, nsec stays in [0, 1e9) — same split the timespec gave);
|
|
218
|
+
* the civil fields are integer math on epoch + offset */
|
|
199
219
|
static void fill_dt_from_time(VALUE v, teptris_datetime *dt) {
|
|
200
|
-
struct timespec ts = rb_time_timespec(v);
|
|
201
220
|
dt->offset_seconds = NUM2INT(rb_funcall(v, id_utc_offset, 0));
|
|
202
|
-
int64_t secs = (
|
|
221
|
+
int64_t secs = NUM2LL(rb_funcall(v, id_to_i, 0)) + dt->offset_seconds;
|
|
203
222
|
int64_t days = secs / 86400;
|
|
204
223
|
int32_t rem = (int32_t)(secs % 86400);
|
|
205
224
|
if (rem < 0) { /* floor division: keep rem in [0, 86400) */
|
|
@@ -210,7 +229,7 @@ static void fill_dt_from_time(VALUE v, teptris_datetime *dt) {
|
|
|
210
229
|
dt->hour = (uint8_t)(rem / 3600);
|
|
211
230
|
dt->minute = (uint8_t)((rem / 60) % 60);
|
|
212
231
|
dt->second = (uint8_t)(rem % 60);
|
|
213
|
-
dt->nanosecond = (uint32_t)
|
|
232
|
+
dt->nanosecond = (uint32_t)NUM2LL(rb_funcall(v, id_nsec, 0));
|
|
214
233
|
}
|
|
215
234
|
|
|
216
235
|
static void dump_scalar(teptris_builder *b, const char *key, size_t klen,
|
|
@@ -590,9 +609,359 @@ static VALUE ext_plan_emit(VALUE self, VALUE rb_plan, VALUE toml)
|
|
|
590
609
|
return out;
|
|
591
610
|
}
|
|
592
611
|
|
|
612
|
+
|
|
613
|
+
/* ---- lazy mode (#79) ----------------------------------------------
|
|
614
|
+
* load_lazy returns a LazyValue wrapping the parsed tree: scalar
|
|
615
|
+
* access materializes scalars only, tables/arrays stay wrapped until
|
|
616
|
+
* walked. The document and the borrowed-input String live in a
|
|
617
|
+
* single owner object every wrapper references, so GC lifetime is
|
|
618
|
+
* one edge deep. Free order is GC-driven via TypedData dfree. */
|
|
619
|
+
|
|
620
|
+
static VALUE cLazyOwner, cLazyValue;
|
|
621
|
+
|
|
622
|
+
typedef struct {
|
|
623
|
+
teptris_document *doc;
|
|
624
|
+
VALUE input; /* the borrowed views' buffer */
|
|
625
|
+
} lazy_owner;
|
|
626
|
+
|
|
627
|
+
typedef struct {
|
|
628
|
+
VALUE owner; /* LazyOwner: keeps doc + input alive */
|
|
629
|
+
const teptris_node *node;
|
|
630
|
+
} lazy_value;
|
|
631
|
+
|
|
632
|
+
static void lazy_owner_free(void *p) {
|
|
633
|
+
lazy_owner *o = (lazy_owner *)p;
|
|
634
|
+
if (o->doc != NULL) teptris_document_free(o->doc);
|
|
635
|
+
xfree(o);
|
|
636
|
+
}
|
|
637
|
+
static size_t lazy_owner_size(const void *p) { return sizeof(lazy_owner); }
|
|
638
|
+
static const rb_data_type_t lazy_owner_type = {
|
|
639
|
+
"TeptrisExt/LazyOwner",
|
|
640
|
+
{NULL, lazy_owner_free, lazy_owner_size, NULL, 0},
|
|
641
|
+
NULL, NULL, RUBY_TYPED_FREE_IMMEDIATELY};
|
|
642
|
+
|
|
643
|
+
static void lazy_value_mark(void *p) {
|
|
644
|
+
lazy_value *v = (lazy_value *)p;
|
|
645
|
+
rb_gc_mark(v->owner);
|
|
646
|
+
}
|
|
647
|
+
static void lazy_value_free(void *p) { xfree(p); }
|
|
648
|
+
static size_t lazy_value_size(const void *p) { return sizeof(lazy_value); }
|
|
649
|
+
static const rb_data_type_t lazy_value_type = {
|
|
650
|
+
"TeptrisExt/LazyValue",
|
|
651
|
+
{lazy_value_mark, lazy_value_free, lazy_value_size, NULL, 0},
|
|
652
|
+
NULL, NULL, RUBY_TYPED_FREE_IMMEDIATELY};
|
|
653
|
+
|
|
654
|
+
static VALUE lazy_wrap(VALUE owner, const teptris_node *n) {
|
|
655
|
+
lazy_value *v;
|
|
656
|
+
VALUE obj = TypedData_Make_Struct(cLazyValue, lazy_value,
|
|
657
|
+
&lazy_value_type, v);
|
|
658
|
+
v->owner = owner;
|
|
659
|
+
v->node = n;
|
|
660
|
+
return obj;
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
static lazy_value *lazy_self(VALUE self) {
|
|
664
|
+
lazy_value *v;
|
|
665
|
+
TypedData_Get_Struct(self, lazy_value, &lazy_value_type, v);
|
|
666
|
+
return v;
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
static VALUE ext_load_lazy(VALUE self, VALUE toml) {
|
|
670
|
+
StringValue(toml);
|
|
671
|
+
lazy_owner *o;
|
|
672
|
+
VALUE owner = TypedData_Make_Struct(cLazyOwner, lazy_owner,
|
|
673
|
+
&lazy_owner_type, o);
|
|
674
|
+
o->doc = NULL;
|
|
675
|
+
o->input = rb_str_new_frozen(toml); /* borrowed views: own a copy */
|
|
676
|
+
teptris_status st = teptris_parse(RSTRING_PTR(o->input),
|
|
677
|
+
(size_t)RSTRING_LEN(o->input),
|
|
678
|
+
NULL, &o->doc);
|
|
679
|
+
if (st != TEPTRIS_OK) {
|
|
680
|
+
const teptris_error *e = teptris_document_error(o->doc);
|
|
681
|
+
VALUE ex = rb_exc_new(eParseError, e->message,
|
|
682
|
+
(long)strlen(e->message));
|
|
683
|
+
rb_iv_set(ex, "@line", SIZET2NUM(e->line));
|
|
684
|
+
rb_iv_set(ex, "@column", SIZET2NUM(e->column));
|
|
685
|
+
rb_exc_raise(ex); /* owner GC-frees the failed doc */
|
|
686
|
+
}
|
|
687
|
+
return lazy_wrap(owner, teptris_document_root(o->doc));
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
static VALUE lazy_kind(VALUE self) {
|
|
691
|
+
switch (teptris_node_kind(lazy_self(self)->node)) {
|
|
692
|
+
case TEPTRIS_TABLE: return ID2SYM(rb_intern("table"));
|
|
693
|
+
case TEPTRIS_ARRAY: return ID2SYM(rb_intern("array"));
|
|
694
|
+
default: return ID2SYM(rb_intern("scalar"));
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
static VALUE lazy_size(VALUE self) {
|
|
699
|
+
const teptris_node *n = lazy_self(self)->node;
|
|
700
|
+
switch (teptris_node_kind(n)) {
|
|
701
|
+
case TEPTRIS_TABLE: return SIZET2NUM(teptris_node_table_length(n));
|
|
702
|
+
case TEPTRIS_ARRAY: return SIZET2NUM(teptris_node_array_length(n));
|
|
703
|
+
default: return Qnil;
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
/* table: iterate entries; array: iterate items; scalar: raise */
|
|
708
|
+
static VALUE lazy_each(VALUE self) {
|
|
709
|
+
RETURN_SIZED_ENUMERATOR(self, 0, 0, 0);
|
|
710
|
+
const teptris_node *n = lazy_self(self)->node;
|
|
711
|
+
switch (teptris_node_kind(n)) {
|
|
712
|
+
case TEPTRIS_TABLE: {
|
|
713
|
+
size_t len = teptris_node_table_length(n);
|
|
714
|
+
for (size_t i = 0; i < len; i++) {
|
|
715
|
+
teptris_view key;
|
|
716
|
+
const teptris_node *v = teptris_node_table_at(n, i, &key);
|
|
717
|
+
VALUE kv = rb_ary_new_capa(2);
|
|
718
|
+
rb_ary_push(kv, rb_enc_str_new(key.ptr, (long)key.len,
|
|
719
|
+
rb_utf8_encoding()));
|
|
720
|
+
rb_ary_push(kv, lazy_wrap(lazy_self(self)->owner, v));
|
|
721
|
+
rb_yield(kv);
|
|
722
|
+
}
|
|
723
|
+
break;
|
|
724
|
+
}
|
|
725
|
+
case TEPTRIS_ARRAY: {
|
|
726
|
+
size_t len = teptris_node_array_length(n);
|
|
727
|
+
for (size_t i = 0; i < len; i++)
|
|
728
|
+
rb_yield(lazy_wrap(lazy_self(self)->owner,
|
|
729
|
+
teptris_node_array_at(n, i)));
|
|
730
|
+
break;
|
|
731
|
+
}
|
|
732
|
+
default:
|
|
733
|
+
rb_raise(eError, "scalar node has nothing to iterate");
|
|
734
|
+
}
|
|
735
|
+
return self;
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
/* [] is polymorphic by node kind: tables take string keys (hash
|
|
739
|
+
* index), arrays take integer indices (contiguous array) - both O(1)
|
|
740
|
+
* per access, which is the whole point of the lazy mode */
|
|
741
|
+
static VALUE lazy_get(VALUE self, VALUE key) {
|
|
742
|
+
lazy_value *v = lazy_self(self);
|
|
743
|
+
const teptris_node *child = NULL;
|
|
744
|
+
switch (teptris_node_kind(v->node)) {
|
|
745
|
+
case TEPTRIS_TABLE: {
|
|
746
|
+
const char *k;
|
|
747
|
+
long kl;
|
|
748
|
+
if (RB_TYPE_P(key, T_STRING)) {
|
|
749
|
+
k = RSTRING_PTR(key); kl = RSTRING_LEN(key);
|
|
750
|
+
} else {
|
|
751
|
+
key = rb_funcall(key, rb_intern("to_s"), 0);
|
|
752
|
+
k = RSTRING_PTR(key); kl = RSTRING_LEN(key);
|
|
753
|
+
}
|
|
754
|
+
child = teptris_node_table_get(v->node, k, (size_t)kl);
|
|
755
|
+
break;
|
|
756
|
+
}
|
|
757
|
+
case TEPTRIS_ARRAY: {
|
|
758
|
+
long i = NUM2LONG(key);
|
|
759
|
+
if (i < 0) i += (long)teptris_node_array_length(v->node);
|
|
760
|
+
child = teptris_node_array_at(v->node, (size_t)i);
|
|
761
|
+
break;
|
|
762
|
+
}
|
|
763
|
+
default:
|
|
764
|
+
rb_raise(eError, "scalar node is not subscriptable");
|
|
765
|
+
}
|
|
766
|
+
if (child == NULL) return Qnil;
|
|
767
|
+
return lazy_wrap(v->owner, child);
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
static VALUE lazy_dig(int argc, VALUE *argv, VALUE self) {
|
|
771
|
+
VALUE cur = self;
|
|
772
|
+
for (long i = 0; i < argc; i++) {
|
|
773
|
+
VALUE nxt = rb_funcall(cur, rb_intern("[]"), 1, argv[i]);
|
|
774
|
+
if (nxt == Qnil || nxt == Qundef) return Qnil;
|
|
775
|
+
cur = nxt;
|
|
776
|
+
}
|
|
777
|
+
return cur;
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
/* full materialization: the eager path, owned */
|
|
781
|
+
static VALUE lazy_to(VALUE self) {
|
|
782
|
+
lazy_value *v = lazy_self(self);
|
|
783
|
+
return obj_from_node(v->node, 0);
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
/* ---------------------------------------------------------------- batch --
|
|
787
|
+
* Many-small batch path (teptris-ruby#108 ask 3, complements the C
|
|
788
|
+
* `teptris_parse_batch`): accept an Array of TOML strings + an options
|
|
789
|
+
* hash, parse every entry in one C call (one opts struct, one
|
|
790
|
+
* per-doc-status report), and return one Ruby Hash per document. The
|
|
791
|
+
* options bag is parsed ONCE and shared across the batch — per-doc
|
|
792
|
+
* safe_load/datetime_policy work happens in C, not in N Ruby frames.
|
|
793
|
+
* On the alloc-fail path the C call leaves no partial docs; the Ruby
|
|
794
|
+
* caller sees a Teptris::Error. Per-doc parse failures are collected
|
|
795
|
+
* alongside the successes in a parallel array (the lazy variant
|
|
796
|
+
* raises on the first failure so callers can `rescue` without losing
|
|
797
|
+
* the partial batch). */
|
|
798
|
+
|
|
799
|
+
/* Combined scratch layout — one alloc, four arrays packed end-to-end.
|
|
800
|
+
* Splitting into four mallocs showed up as the dominant cost in the
|
|
801
|
+
* per-doc benchmark for tiny docs (4× malloc/free per call, 16–64 KB
|
|
802
|
+
* of scratch per call). One alloc amortizes the heap and the cache. */
|
|
803
|
+
typedef struct {
|
|
804
|
+
const char **data;
|
|
805
|
+
size_t *lens;
|
|
806
|
+
teptris_document **docs;
|
|
807
|
+
teptris_status *statuses;
|
|
808
|
+
} batch_scratch;
|
|
809
|
+
|
|
810
|
+
static void batch_scratch_free(batch_scratch *s) {
|
|
811
|
+
free(s->data);
|
|
812
|
+
free(s);
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
static batch_scratch *batch_scratch_alloc(long n) {
|
|
816
|
+
batch_scratch *s = (batch_scratch *)malloc(sizeof(*s));
|
|
817
|
+
if (s == NULL) return NULL;
|
|
818
|
+
size_t bytes =
|
|
819
|
+
(size_t)n * (sizeof(const char *) + sizeof(size_t) +
|
|
820
|
+
sizeof(teptris_document *) + sizeof(teptris_status));
|
|
821
|
+
void *blob = malloc(bytes);
|
|
822
|
+
if (blob == NULL) {
|
|
823
|
+
free(s);
|
|
824
|
+
return NULL;
|
|
825
|
+
}
|
|
826
|
+
char *p = (char *)blob;
|
|
827
|
+
s->data = (const char **)p;
|
|
828
|
+
p += (size_t)n * sizeof(const char *);
|
|
829
|
+
s->lens = (size_t *)p;
|
|
830
|
+
p += (size_t)n * sizeof(size_t);
|
|
831
|
+
s->docs = (teptris_document **)p;
|
|
832
|
+
p += (size_t)n * sizeof(teptris_document *);
|
|
833
|
+
s->statuses = (teptris_status *)p;
|
|
834
|
+
return s;
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
static unsigned flags_from_opts(VALUE opts) {
|
|
838
|
+
unsigned flags = 0;
|
|
839
|
+
if (NIL_P(opts)) return 0;
|
|
840
|
+
VALUE sd = rb_hash_aref(opts, ID2SYM(rb_intern("string_datetimes")));
|
|
841
|
+
VALUE ft = rb_hash_aref(opts, ID2SYM(rb_intern("forbid_time")));
|
|
842
|
+
VALUE fd = rb_hash_aref(opts, ID2SYM(rb_intern("forbid_date")));
|
|
843
|
+
if (RTEST(sd)) flags |= FMT_STRING_DATES;
|
|
844
|
+
if (RTEST(ft)) flags |= FMT_FORBID_TIME;
|
|
845
|
+
if (RTEST(fd)) flags |= FMT_FORBID_DATE;
|
|
846
|
+
return flags;
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
/* Parse-only batch (returns an Array of Lazy wrappers, one per doc):
|
|
850
|
+
* eager parse, deferred materialization. Same per-doc error semantics
|
|
851
|
+
* as ext_load_lazy. */
|
|
852
|
+
static VALUE ext_load_lazy_batch(VALUE self, VALUE strings) {
|
|
853
|
+
Check_Type(strings, T_ARRAY);
|
|
854
|
+
long n = RARRAY_LEN(strings);
|
|
855
|
+
if (n == 0) return rb_ary_new();
|
|
856
|
+
batch_scratch *s = batch_scratch_alloc(n);
|
|
857
|
+
if (s == NULL) rb_raise(eError, "out of memory batching %ld documents", n);
|
|
858
|
+
for (long i = 0; i < n; i++) {
|
|
859
|
+
VALUE str = rb_ary_entry(strings, i);
|
|
860
|
+
StringValue(str); /* raises TypeError if not coercible */
|
|
861
|
+
s->data[i] = RSTRING_PTR(str);
|
|
862
|
+
s->lens[i] = (size_t)RSTRING_LEN(str);
|
|
863
|
+
}
|
|
864
|
+
teptris_status batch =
|
|
865
|
+
teptris_parse_batch(s->data, s->lens, (size_t)n, NULL,
|
|
866
|
+
s->docs, s->statuses);
|
|
867
|
+
if (batch != TEPTRIS_OK) {
|
|
868
|
+
batch_scratch_free(s);
|
|
869
|
+
rb_raise(eError, "batch parse failed (status %d)", (int)batch);
|
|
870
|
+
}
|
|
871
|
+
long first_fail = -1;
|
|
872
|
+
for (long i = 0; i < n; i++) {
|
|
873
|
+
if (s->statuses[i] != TEPTRIS_OK && first_fail == -1) first_fail = i;
|
|
874
|
+
}
|
|
875
|
+
if (first_fail != -1) {
|
|
876
|
+
const teptris_error *e = teptris_document_error(s->docs[first_fail]);
|
|
877
|
+
VALUE ex =
|
|
878
|
+
rb_exc_new(eParseError, e->message, (long)strlen(e->message));
|
|
879
|
+
rb_iv_set(ex, "@line", SIZET2NUM(e->line));
|
|
880
|
+
rb_iv_set(ex, "@column", SIZET2NUM(e->column));
|
|
881
|
+
for (long i = 0; i < n; i++) teptris_document_free(s->docs[i]);
|
|
882
|
+
batch_scratch_free(s);
|
|
883
|
+
rb_exc_raise(ex);
|
|
884
|
+
}
|
|
885
|
+
VALUE out = rb_ary_new_capa(n);
|
|
886
|
+
for (long i = 0; i < n; i++) {
|
|
887
|
+
lazy_owner *o;
|
|
888
|
+
VALUE owner = TypedData_Make_Struct(cLazyOwner, lazy_owner,
|
|
889
|
+
&lazy_owner_type, o);
|
|
890
|
+
o->doc = s->docs[i];
|
|
891
|
+
o->input = rb_ary_entry(strings, i);
|
|
892
|
+
rb_ary_push(out, lazy_wrap(owner, teptris_document_root(s->docs[i])));
|
|
893
|
+
}
|
|
894
|
+
batch_scratch_free(s);
|
|
895
|
+
return out;
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
/* Eager batch: returns Array of Hashes with the same datetime contract
|
|
899
|
+
* as ext_load. opts is parsed once; safe_load semantics apply uniformly.
|
|
900
|
+
* The caller's input Array is read-only; its strings stay referenced
|
|
901
|
+
* for the duration of the C call (no separate keeper Array). */
|
|
902
|
+
static VALUE ext_load_batch(int argc, VALUE *argv, VALUE self) {
|
|
903
|
+
VALUE strings, opts;
|
|
904
|
+
rb_scan_args(argc, argv, "11", &strings, &opts);
|
|
905
|
+
Check_Type(strings, T_ARRAY);
|
|
906
|
+
long n = RARRAY_LEN(strings);
|
|
907
|
+
if (n == 0) return rb_ary_new();
|
|
908
|
+
unsigned flags = flags_from_opts(opts);
|
|
909
|
+
batch_scratch *s = batch_scratch_alloc(n);
|
|
910
|
+
if (s == NULL) rb_raise(eError, "out of memory batching %ld documents", n);
|
|
911
|
+
for (long i = 0; i < n; i++) {
|
|
912
|
+
VALUE str = rb_ary_entry(strings, i);
|
|
913
|
+
StringValue(str);
|
|
914
|
+
s->data[i] = RSTRING_PTR(str);
|
|
915
|
+
s->lens[i] = (size_t)RSTRING_LEN(str);
|
|
916
|
+
}
|
|
917
|
+
teptris_status batch =
|
|
918
|
+
teptris_parse_batch(s->data, s->lens, (size_t)n, NULL,
|
|
919
|
+
s->docs, s->statuses);
|
|
920
|
+
if (batch != TEPTRIS_OK) {
|
|
921
|
+
batch_scratch_free(s);
|
|
922
|
+
rb_raise(eError, "batch parse failed (status %d)", (int)batch);
|
|
923
|
+
}
|
|
924
|
+
long first_fail = -1;
|
|
925
|
+
for (long i = 0; i < n; i++) {
|
|
926
|
+
if (s->statuses[i] != TEPTRIS_OK && first_fail == -1) first_fail = i;
|
|
927
|
+
}
|
|
928
|
+
if (first_fail != -1) {
|
|
929
|
+
const teptris_error *e = teptris_document_error(s->docs[first_fail]);
|
|
930
|
+
VALUE ex =
|
|
931
|
+
rb_exc_new(eParseError, e->message, (long)strlen(e->message));
|
|
932
|
+
rb_iv_set(ex, "@line", SIZET2NUM(e->line));
|
|
933
|
+
rb_iv_set(ex, "@column", SIZET2NUM(e->column));
|
|
934
|
+
for (long i = 0; i < n; i++) teptris_document_free(s->docs[i]);
|
|
935
|
+
batch_scratch_free(s);
|
|
936
|
+
rb_exc_raise(ex);
|
|
937
|
+
}
|
|
938
|
+
VALUE out = rb_ary_new_capa(n);
|
|
939
|
+
for (long i = 0; i < n; i++) {
|
|
940
|
+
const teptris_node *root = teptris_document_root(s->docs[i]);
|
|
941
|
+
rb_ary_store(out, i, obj_from_node(root, flags));
|
|
942
|
+
teptris_document_free(s->docs[i]);
|
|
943
|
+
}
|
|
944
|
+
batch_scratch_free(s);
|
|
945
|
+
return out;
|
|
946
|
+
}
|
|
947
|
+
|
|
593
948
|
void Init_teptris_ext(void) {
|
|
594
949
|
VALUE m = rb_define_module("TeptrisExt");
|
|
595
950
|
rb_define_module_function(m, "load", ext_load, -1);
|
|
951
|
+
rb_define_module_function(m, "load_lazy", ext_load_lazy, 1);
|
|
952
|
+
rb_define_module_function(m, "load_batch", ext_load_batch, -1);
|
|
953
|
+
rb_define_module_function(m, "load_lazy_batch", ext_load_lazy_batch, 1);
|
|
954
|
+
cLazyOwner = rb_define_class_under(m, "LazyOwner", rb_cObject);
|
|
955
|
+
cLazyValue = rb_define_class_under(m, "LazyValue", rb_cObject);
|
|
956
|
+
rb_define_method(cLazyValue, "kind", lazy_kind, 0);
|
|
957
|
+
rb_define_method(cLazyValue, "size", lazy_size, 0);
|
|
958
|
+
rb_define_method(cLazyValue, "each", lazy_each, 0);
|
|
959
|
+
rb_define_method(cLazyValue, "[]", lazy_get, 1);
|
|
960
|
+
rb_define_method(cLazyValue, "at", lazy_get, 1);
|
|
961
|
+
rb_define_method(cLazyValue, "dig", lazy_dig, -1);
|
|
962
|
+
rb_define_method(cLazyValue, "to_h", lazy_to, 0);
|
|
963
|
+
rb_define_method(cLazyValue, "to_a", lazy_to, 0);
|
|
964
|
+
rb_define_method(cLazyValue, "value", lazy_to, 0);
|
|
596
965
|
rb_define_module_function(m, "dump", ext_dump, 1);
|
|
597
966
|
rb_define_module_function(m, "engine_version", ext_version, 0);
|
|
598
967
|
rb_define_module_function(m, "plan_build", ext_plan_build, 2);
|
|
@@ -605,4 +974,6 @@ void Init_teptris_ext(void) {
|
|
|
605
974
|
cDate = rb_const_get(rb_cObject, rb_intern("Date"));
|
|
606
975
|
id_utc_offset = rb_intern("utc_offset");
|
|
607
976
|
id_to_s = rb_intern("to_s");
|
|
977
|
+
id_to_i = rb_intern("to_i");
|
|
978
|
+
id_nsec = rb_intern("nsec");
|
|
608
979
|
}
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
data/lib/teptris/toml.rb
CHANGED
|
@@ -36,6 +36,42 @@ module Teptris
|
|
|
36
36
|
TeptrisExt.load(toml, opts)
|
|
37
37
|
end
|
|
38
38
|
|
|
39
|
+
# Lazy load: returns a wrapper that parses eagerly (one pass)
|
|
40
|
+
# but materializes host objects only along the paths actually
|
|
41
|
+
# accessed. Same datetime contract as load; to_h/to_a flatten to
|
|
42
|
+
# the eager shape.
|
|
43
|
+
def load_lazy(toml)
|
|
44
|
+
raise ArgumentError, "input must be a String" unless toml.is_a?(String)
|
|
45
|
+
|
|
46
|
+
TeptrisExt.load_lazy(toml)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Many-small batch path (teptris-ruby#108 ask 3): parse N inputs
|
|
50
|
+
# in one C call, share the safe_load/datetime_policy options
|
|
51
|
+
# struct across the batch, return one eager Hash per document.
|
|
52
|
+
# Same datetime contract as load. A failing document raises
|
|
53
|
+
# Teptris::ParseError carrying the first failure's line/column.
|
|
54
|
+
def load_batch(tomls, safe_load: nil, datetime_policy: :native)
|
|
55
|
+
_validate_batch!(tomls, safe_load, datetime_policy)
|
|
56
|
+
TeptrisExt.load_batch(tomls, _safe_load_opts(safe_load, datetime_policy))
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Lazy twin of load_batch: parse N inputs eagerly, hand back an
|
|
60
|
+
# Array of Lazy wrappers — materialization happens only on access.
|
|
61
|
+
# Same per-doc error semantics as load_lazy (first failure raises).
|
|
62
|
+
def load_lazy_batch(tomls)
|
|
63
|
+
_validate_batch!(tomls, nil, :native)
|
|
64
|
+
TeptrisExt.load_lazy_batch(tomls)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# Many-small path with file paths: read each file, then call
|
|
68
|
+
# load_batch. Symmetric with load_file's single-doc shape.
|
|
69
|
+
def load_files(paths, safe_load: nil, datetime_policy: :native)
|
|
70
|
+
_validate_batch!(paths, safe_load, datetime_policy, kind: :paths)
|
|
71
|
+
load_batch(paths.map { |p| File.read(p) },
|
|
72
|
+
safe_load: safe_load, datetime_policy: datetime_policy)
|
|
73
|
+
end
|
|
74
|
+
|
|
39
75
|
# One-pass schema materialization over a compiled
|
|
40
76
|
# Teptris::Descriptor: unplanned keys are never materialized
|
|
41
77
|
# (teptris#46).
|
|
@@ -61,6 +97,31 @@ module Teptris
|
|
|
61
97
|
def dump(obj)
|
|
62
98
|
TeptrisExt.dump(obj)
|
|
63
99
|
end
|
|
100
|
+
|
|
101
|
+
private
|
|
102
|
+
|
|
103
|
+
def _validate_batch!(input, safe_load, datetime_policy, kind: :strings)
|
|
104
|
+
unless input.is_a?(Array)
|
|
105
|
+
raise ArgumentError, "expected Array of #{kind}, got #{input.class}"
|
|
106
|
+
end
|
|
107
|
+
if !safe_load.nil? && !safe_load.is_a?(Array) && !safe_load.is_a?(Symbol)
|
|
108
|
+
raise ArgumentError, "safe_load must be nil, an Array, or a Symbol"
|
|
109
|
+
end
|
|
110
|
+
unless %i[native string].include?(datetime_policy)
|
|
111
|
+
raise ArgumentError, "datetime_policy must be :native or :string"
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def _safe_load_opts(safe_load, datetime_policy)
|
|
116
|
+
opts = {}
|
|
117
|
+
opts[:string_datetimes] = true if datetime_policy == :string
|
|
118
|
+
if safe_load && datetime_policy == :native
|
|
119
|
+
permitted = Array(safe_load) + [:__scalar]
|
|
120
|
+
opts[:forbid_time] = true unless permitted.include?(Time)
|
|
121
|
+
opts[:forbid_date] = true unless permitted.include?(Date)
|
|
122
|
+
end
|
|
123
|
+
opts.empty? ? nil : opts
|
|
124
|
+
end
|
|
64
125
|
end
|
|
65
126
|
end
|
|
66
127
|
end
|
data/lib/teptris/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: teptris
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.2.
|
|
4
|
+
version: 0.2.47
|
|
5
5
|
platform: arm-linux
|
|
6
6
|
authors:
|
|
7
7
|
- teptris contributors
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-09-
|
|
11
|
+
date: 2026-09-23 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description: A native C extension over libteptris, a C99 TOML 1.1 parser/emitter.
|
|
14
14
|
API shape mirrors the tomlib gem.
|
|
@@ -51,7 +51,9 @@ files:
|
|
|
51
51
|
- ext/teptris_ext/extconf.rb
|
|
52
52
|
- ext/teptris_ext/materialize.c
|
|
53
53
|
- lib/teptris.rb
|
|
54
|
-
- lib/teptris/3.
|
|
54
|
+
- lib/teptris/3.3/teptris_ext.so
|
|
55
|
+
- lib/teptris/3.4/teptris_ext.so
|
|
56
|
+
- lib/teptris/4.0/teptris_ext.so
|
|
55
57
|
- lib/teptris/descriptor.rb
|
|
56
58
|
- lib/teptris/error.rb
|
|
57
59
|
- lib/teptris/toml.rb
|
|
@@ -75,7 +77,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
75
77
|
- !ruby/object:Gem::Version
|
|
76
78
|
version: '0'
|
|
77
79
|
requirements: []
|
|
78
|
-
rubygems_version: 3.
|
|
80
|
+
rubygems_version: 3.5.22
|
|
79
81
|
signing_key:
|
|
80
82
|
specification_version: 4
|
|
81
83
|
summary: TOML for Ruby at libteptris speed (native C extension)
|
|
Binary file
|