teptris 0.3.22.0 → 0.3.24.0

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.
@@ -0,0 +1,20 @@
1
+ /* Unicode property ranges for the schema regex \p{...}. Generated. */
2
+ #ifndef TEPTRIS_SCHEMA_UNICODE_PROPS_H
3
+ #define TEPTRIS_SCHEMA_UNICODE_PROPS_H
4
+
5
+ #include <stdint.h>
6
+
7
+ typedef struct {
8
+ uint32_t lo, hi;
9
+ } teptris_uc_range;
10
+
11
+ typedef struct {
12
+ const char *name;
13
+ const teptris_uc_range *ranges;
14
+ int n;
15
+ } teptris_uc_prop;
16
+
17
+ extern const teptris_uc_prop teptris_unicode_props[];
18
+ extern const int teptris_n_unicode_props;
19
+
20
+ #endif
@@ -41,13 +41,26 @@ typedef struct {
41
41
  size_t n_keys, cap_keys;
42
42
  size_t *idx;
43
43
  size_t n_idx, cap_idx;
44
+ /* stamp-validated open-addressing indexes: a slot holds the
45
+ * entry's position when inserted, so a trial's truncate
46
+ * (n_keys rewind) self-invalidates without a rebuild */
47
+ struct { const char *key; size_t stamp; } *khash;
48
+ size_t khash_cap; /* power of two, 0 = absent */
49
+ struct { size_t val; size_t stamp; } *ihash;
50
+ size_t ihash_cap;
44
51
  } *frames;
45
52
  int n_frames, cap_frames;
46
- /* dynamic-scope stack for $dynamicRef / $recursiveRef: the
47
- * innermost anchor on the evaluation path wins */
53
+ /* dynamic-scope stack for $dynamicRef / $recursiveRef: one frame
54
+ * per applied schema object, in evaluation order. Each entry
55
+ * names its anchor and the frame owner (the applied object), so
56
+ * resolution walks RESOURCES outermost-first (2020-12 8.2.3.2:
57
+ * the outermost resource in the dynamic scope that defines the
58
+ * fragment wins, its first anchor in document order) */
48
59
  struct dyn_scope {
49
60
  const char *name;
50
61
  sch_node *node;
62
+ const jv *res; /* the anchor's resource (nearest $id) */
63
+ sch_node *owner; /* the applied object that entered it */
51
64
  } *scopes;
52
65
  int n_scopes, cap_scopes;
53
66
  } vctx;
@@ -200,6 +213,9 @@ static bool frame_grow(vctx *vc) {
200
213
  return true;
201
214
  }
202
215
 
216
+ static void frame_khash_insert(struct eval_frame *f, const char *key);
217
+ static void frame_ihash_insert(struct eval_frame *f, size_t idx);
218
+
203
219
  static bool frame_push(vctx *vc, const sch_svalue *v) {
204
220
  if (!frame_grow(vc)) return false;
205
221
  struct eval_frame *f = &vc->frames[vc->n_frames++];
@@ -227,6 +243,9 @@ static void frame_merge_up(vctx *vc) {
227
243
  }
228
244
  memcpy(p->keys + p->n_keys, f->keys, f->n_keys * sizeof(*f->keys));
229
245
  p->n_keys += f->n_keys;
246
+ for (size_t i = p->n_keys - f->n_keys; i < p->n_keys; i++) {
247
+ frame_khash_insert(p, p->keys[i]);
248
+ }
230
249
  }
231
250
  if (f->n_idx > 0) {
232
251
  if (p->n_idx + f->n_idx > p->cap_idx) {
@@ -239,6 +258,9 @@ static void frame_merge_up(vctx *vc) {
239
258
  }
240
259
  memcpy(p->idx + p->n_idx, f->idx, f->n_idx * sizeof(*f->idx));
241
260
  p->n_idx += f->n_idx;
261
+ for (size_t i = p->n_idx - f->n_idx; i < p->n_idx; i++) {
262
+ frame_ihash_insert(p, p->idx[i]);
263
+ }
242
264
  }
243
265
  }
244
266
 
@@ -250,23 +272,111 @@ static void frame_pop(vctx *vc) {
250
272
  struct eval_frame *f = &vc->frames[vc->n_frames - 1];
251
273
  free(f->keys);
252
274
  free(f->idx);
275
+ free(f->khash);
276
+ free(f->ihash);
253
277
  vc->n_frames--;
254
278
  }
255
279
 
256
- static bool frame_key_seen(const struct eval_frame *f, const char *key) {
280
+ static uint64_t frame_mix(uint64_t x) {
281
+ x ^= x >> 33;
282
+ x *= 0xff51afd7ed558ccdULL;
283
+ x ^= x >> 33;
284
+ x *= 0xc4ceb9fe1a85ec53ULL;
285
+ x ^= x >> 33;
286
+ return x;
287
+ }
288
+
289
+ static uint64_t frame_str_hash(const char *s) {
290
+ uint64_t h = 1469598103934665603ULL;
291
+ for (; *s != '\0'; s++) {
292
+ h ^= (unsigned char)*s;
293
+ h *= 1099511628211ULL;
294
+ }
295
+ return h;
296
+ }
297
+
298
+ /* rebuild a key index from the frame's valid entries */
299
+ static void frame_khash_build(struct eval_frame *f) {
300
+ size_t cap = 16;
301
+ while (cap * 7 / 10 < f->n_keys + 1) cap *= 2;
302
+ void *nh = realloc(f->khash, cap * sizeof(*f->khash));
303
+ if (nh == NULL) return;
304
+ f->khash = nh;
305
+ f->khash_cap = cap;
306
+ for (size_t i = 0; i < cap; i++) f->khash[i].stamp = (size_t)-1;
257
307
  for (size_t i = 0; i < f->n_keys; i++) {
258
- if (strcmp(f->keys[i], key) == 0) return true;
308
+ size_t slot = frame_mix(frame_str_hash(f->keys[i])) & (cap - 1);
309
+ while (f->khash[slot].stamp != (size_t)-1) {
310
+ slot = (slot + 1) & (cap - 1);
311
+ }
312
+ f->khash[slot].key = f->keys[i];
313
+ f->khash[slot].stamp = i;
314
+ }
315
+ }
316
+
317
+ static bool frame_key_seen(const struct eval_frame *f, const char *key) {
318
+ if (f->khash_cap == 0) return false;
319
+ size_t slot = frame_mix(frame_str_hash(key)) & (f->khash_cap - 1);
320
+ while (f->khash[slot].stamp != (size_t)-1) {
321
+ if (f->khash[slot].stamp < f->n_keys &&
322
+ strcmp(f->khash[slot].key, key) == 0) {
323
+ return true;
324
+ }
325
+ slot = (slot + 1) & (f->khash_cap - 1);
259
326
  }
260
327
  return false;
261
328
  }
262
329
 
263
- static bool frame_idx_seen(const struct eval_frame *f, size_t idx) {
330
+ static void frame_khash_insert(struct eval_frame *f, const char *key) {
331
+ if (f->n_keys + 1 > f->khash_cap * 7 / 10) frame_khash_build(f);
332
+ size_t slot = frame_mix(frame_str_hash(key)) & (f->khash_cap - 1);
333
+ while (f->khash[slot].stamp != (size_t)-1) {
334
+ slot = (slot + 1) & (f->khash_cap - 1);
335
+ }
336
+ f->khash[slot].key = key;
337
+ f->khash[slot].stamp = f->n_keys - 1;
338
+ }
339
+
340
+ static void frame_ihash_build(struct eval_frame *f) {
341
+ size_t cap = 16;
342
+ while (cap * 7 / 10 < f->n_idx + 1) cap *= 2;
343
+ void *nh = realloc(f->ihash, cap * sizeof(*f->ihash));
344
+ if (nh == NULL) return;
345
+ f->ihash = nh;
346
+ f->ihash_cap = cap;
347
+ for (size_t i = 0; i < cap; i++) f->ihash[i].stamp = (size_t)-1;
264
348
  for (size_t i = 0; i < f->n_idx; i++) {
265
- if (f->idx[i] == idx) return true;
349
+ size_t slot = frame_mix((uint64_t)f->idx[i]) & (cap - 1);
350
+ while (f->ihash[slot].stamp != (size_t)-1) {
351
+ slot = (slot + 1) & (cap - 1);
352
+ }
353
+ f->ihash[slot].val = f->idx[i];
354
+ f->ihash[slot].stamp = i;
355
+ }
356
+ }
357
+
358
+ static bool frame_idx_seen(const struct eval_frame *f, size_t idx) {
359
+ if (f->ihash_cap == 0) return false;
360
+ size_t slot = frame_mix((uint64_t)idx) & (f->ihash_cap - 1);
361
+ while (f->ihash[slot].stamp != (size_t)-1) {
362
+ if (f->ihash[slot].stamp < f->n_idx && f->ihash[slot].val == idx) {
363
+ return true;
364
+ }
365
+ slot = (slot + 1) & (f->ihash_cap - 1);
266
366
  }
267
367
  return false;
268
368
  }
269
369
 
370
+ static void frame_ihash_insert(struct eval_frame *f, size_t idx) {
371
+ if (f->n_idx + 1 > f->ihash_cap * 7 / 10) frame_ihash_build(f);
372
+ size_t slot = frame_mix((uint64_t)idx) & (f->ihash_cap - 1);
373
+ while (f->ihash[slot].stamp != (size_t)-1) {
374
+ slot = (slot + 1) & (f->ihash_cap - 1);
375
+ }
376
+ f->ihash[slot].val = idx;
377
+ f->ihash[slot].stamp = f->n_idx - 1;
378
+ }
379
+
270
380
  static void frame_add_key(vctx *vc, const char *key) {
271
381
  if (vc->n_frames == 0) return;
272
382
  struct eval_frame *f = &vc->frames[vc->n_frames - 1];
@@ -278,6 +388,7 @@ static void frame_add_key(vctx *vc, const char *key) {
278
388
  f->keys = nk;
279
389
  }
280
390
  f->keys[f->n_keys++] = key;
391
+ frame_khash_insert(f, key);
281
392
  }
282
393
 
283
394
  static void frame_add_idx(vctx *vc, size_t idx) {
@@ -291,6 +402,7 @@ static void frame_add_idx(vctx *vc, size_t idx) {
291
402
  f->idx = ni;
292
403
  }
293
404
  f->idx[f->n_idx++] = idx;
405
+ frame_ihash_insert(f, idx);
294
406
  }
295
407
 
296
408
  /* drop the annotations a trial contributed to its merge target (the
@@ -307,14 +419,17 @@ static void frames_free(vctx *vc) {
307
419
  for (int i = 0; i < vc->n_frames; i++) {
308
420
  free(vc->frames[i].keys);
309
421
  free(vc->frames[i].idx);
422
+ free(vc->frames[i].khash);
423
+ free(vc->frames[i].ihash);
310
424
  }
311
425
  free(vc->frames);
312
426
  }
313
427
 
314
428
  /* ------------------------------------------------- dynamic scopes -- */
315
429
 
316
- static bool scope_push(vctx *vc, const char *name, sch_node *node) {
317
- if (name == NULL || node == NULL) return true;
430
+ static bool scope_push(vctx *vc, const char *name, sch_node *node,
431
+ const jv *res, sch_node *owner) {
432
+ if (name == NULL || node == NULL || owner == NULL) return true;
318
433
  if (vc->n_scopes == vc->cap_scopes) {
319
434
  vc->cap_scopes = vc->cap_scopes == 0 ? 16 : vc->cap_scopes * 2;
320
435
  void *ns = realloc(vc->scopes, vc->cap_scopes * sizeof(*vc->scopes));
@@ -323,14 +438,47 @@ static bool scope_push(vctx *vc, const char *name, sch_node *node) {
323
438
  }
324
439
  vc->scopes[vc->n_scopes].name = name;
325
440
  vc->scopes[vc->n_scopes].node = node;
441
+ vc->scopes[vc->n_scopes].res = res;
442
+ vc->scopes[vc->n_scopes].owner = owner;
326
443
  vc->n_scopes++;
327
444
  return true;
328
445
  }
329
446
 
330
- /* the innermost dynamic anchor for a name on the evaluation path */
447
+ /* 2020-12 $dynamicRef: walk the RESOURCES of the evaluation path
448
+ * outermost-first (the resource of each applied object, in frame
449
+ * order); the first resource that DEFINES an identically-named
450
+ * $dynamicAnchor — the anchor's nearest-$id resource anywhere in the
451
+ * stack — wins, and its first matching anchor in document order is
452
+ * the target (2020-12 8.2.3.2 + appendix C) */
331
453
  static sch_node *scope_resolve(vctx *vc, const char *name) {
332
- for (int i = vc->n_scopes - 1; i >= 0; i--) {
333
- if (strcmp(vc->scopes[i].name, name) == 0) return vc->scopes[i].node;
454
+ for (int i = 0; i < vc->n_scopes; i++) {
455
+ const jv *res = vc->scopes[i].owner->res_id;
456
+ bool seen = false;
457
+ for (int k = 0; k < i; k++) {
458
+ if (vc->scopes[k].owner->res_id == res) {
459
+ seen = true;
460
+ break;
461
+ }
462
+ }
463
+ if (seen) continue;
464
+ for (int j = 0; j < vc->n_scopes; j++) {
465
+ if (vc->scopes[j].res != res) continue;
466
+ if (strcmp(vc->scopes[j].name, name) == 0) {
467
+ return vc->scopes[j].node;
468
+ }
469
+ }
470
+ }
471
+ return NULL;
472
+ }
473
+
474
+ /* 2019-09 $recursiveRef: the first resource on the evaluation path
475
+ * (outermost first) whose ROOT carries $recursiveAnchor: true; the
476
+ * target is that resource's root */
477
+ static sch_node *scope_resolve_recursive(vctx *vc) {
478
+ for (int i = 0; i < vc->n_scopes; i++) {
479
+ if (vc->scopes[i].owner->recursive_anchor != NULL) {
480
+ return vc->scopes[i].owner;
481
+ }
334
482
  }
335
483
  return NULL;
336
484
  }
@@ -346,6 +494,15 @@ static bool sv_equal(const sch_svalue *a, const sch_svalue *b) {
346
494
  * equals a datetime text */
347
495
  int ad = a->kind >= SV_DATETIME;
348
496
  int bd = b->kind >= SV_DATETIME;
497
+ /* numeric identity per JSON Schema: an integer and a float
498
+ * with the same value are equal (bools stay distinct - their
499
+ * kinds differ, so they never reach this branch) */
500
+ if ((a->kind == SV_INT || a->kind == SV_FLOAT) &&
501
+ (b->kind == SV_INT || b->kind == SV_FLOAT)) {
502
+ long double av = a->kind == SV_INT ? (long double)a->i : (long double)a->f;
503
+ long double bv = b->kind == SV_INT ? (long double)b->i : (long double)b->f;
504
+ return av == bv;
505
+ }
349
506
  if ((ad || bd) && (a->kind == SV_STRING || b->kind == SV_STRING)) {
350
507
  const sch_svalue *dt = ad ? a : b;
351
508
  const sch_svalue *st = ad ? b : a;
@@ -676,15 +833,31 @@ static int sv_to_st(const sch_svalue *v) {
676
833
  switch (v->kind) {
677
834
  case SV_STRING: return ST_STRING;
678
835
  case SV_INT: return ST_INTEGER | ST_NUMBER;
679
- case SV_FLOAT: return ST_NUMBER;
836
+ case SV_FLOAT:
837
+ /* a float with a zero fractional part is an integer (the
838
+ * 2020-12 rule the suite tests) */
839
+ return (v->f == v->f && v->f != INFINITY && v->f != -INFINITY &&
840
+ v->f == (double)(int64_t)v->f)
841
+ ? ST_INTEGER | ST_NUMBER
842
+ : ST_NUMBER;
680
843
  case SV_BOOL: return ST_BOOLEAN;
681
844
  case SV_TABLE: return ST_OBJECT;
682
845
  case SV_ARRAY: return ST_ARRAY;
683
846
  case SV_DATETIME: case SV_DATE: case SV_TIME: return ST_STRING;
847
+ case SV_NULL: return ST_NULL;
684
848
  default: return 0;
685
849
  }
686
850
  }
687
851
 
852
+ /* one application = one scope frame: every anchor inside the applied
853
+ * schema's own subtree is in scope below it, owned by this object */
854
+ static void scope_frame_push(vctx *vc, sch_node *n) {
855
+ for (size_t i = 0; i < n->n_self_scope; i++) {
856
+ scope_push(vc, n->self_scope[i].name, n->self_scope[i].node,
857
+ n->self_scope[i].res, n);
858
+ }
859
+ }
860
+
688
861
  static void val(vctx *vc, sch_node *n, const sch_svalue *v) {
689
862
  if (n == NULL || v == NULL) return;
690
863
  if (vc->depth >= MAX_DEPTH) {
@@ -697,8 +870,7 @@ static void val(vctx *vc, sch_node *n, const sch_svalue *v) {
697
870
  * scope for the instance path below it */
698
871
  frame_push(vc, v);
699
872
  int scopes_base = vc->n_scopes;
700
- scope_push(vc, n->dynamic_anchor, n);
701
- scope_push(vc, n->recursive_anchor, n);
873
+ scope_frame_push(vc, n);
702
874
 
703
875
  /* a dynamic $ref resolves through the innermost scope entered on
704
876
  * the evaluation path; the compile-resolved target is the static
@@ -706,7 +878,8 @@ static void val(vctx *vc, sch_node *n, const sch_svalue *v) {
706
878
  if (n->dynamic_ref != NULL) {
707
879
  const char *name = n->dynamic_ref[0] == '#' ? n->dynamic_ref + 1
708
880
  : n->dynamic_ref;
709
- sch_node *target = scope_resolve(vc, name);
881
+ sch_node *target = n->dynamic_recursive ? scope_resolve_recursive(vc)
882
+ : scope_resolve(vc, name);
710
883
  if (target == NULL) target = n->dynamic_ref_target;
711
884
  if (target != NULL) {
712
885
  if (vc->ref_depth >= MAX_DEPTH) {
@@ -715,6 +888,7 @@ static void val(vctx *vc, sch_node *n, const sch_svalue *v) {
715
888
  vc->ref_depth++;
716
889
  spush(vc, "$dynamicRef");
717
890
  val(vc, target, v);
891
+ vc->n_scopes = scopes_base;
718
892
  spop(vc);
719
893
  vc->ref_depth--;
720
894
  }
@@ -731,9 +905,16 @@ static void val(vctx *vc, sch_node *n, const sch_svalue *v) {
731
905
  vc->ref_depth++;
732
906
  spush(vc, "$ref");
733
907
  val(vc, n->ref, v);
908
+ vc->n_scopes = scopes_base;
734
909
  spop(vc);
735
910
  vc->ref_depth--;
736
911
  }
912
+ /* draft 07 and older: the $ref replaces every sibling */
913
+ if (n->ref_only) {
914
+ vc->n_scopes = scopes_base;
915
+ frame_pop(vc);
916
+ return;
917
+ }
737
918
  }
738
919
 
739
920
  if (n->is_false) {
@@ -767,6 +948,9 @@ static void val(vctx *vc, sch_node *n, const sch_svalue *v) {
767
948
  verr(vc, "value does not equal the constant");
768
949
  }
769
950
  }
951
+ if (n->enum_empty) {
952
+ verr(vc, "value does not match the empty enum (nothing matches)");
953
+ }
770
954
  if (n->n_enum > 0) {
771
955
  bool hit = false;
772
956
  for (size_t i = 0; i < n->n_enum && !hit; i++) {
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Teptris
4
- VERSION = "0.3.22.0"
4
+ VERSION = "0.3.24.0"
5
5
  end
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.3.22.0
4
+ version: 0.3.24.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - teptris contributors
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-11-03 00:00:00.000000000 Z
11
+ date: 2026-11-04 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.
@@ -56,10 +56,16 @@ files:
56
56
  - ext/teptris_ext/engine/src/teptris/schema/formats.c
57
57
  - ext/teptris_ext/engine/src/teptris/schema/json_read.c
58
58
  - ext/teptris_ext/engine/src/teptris/schema/json_read.h
59
+ - ext/teptris_ext/engine/src/teptris/schema/metaschemas.c
60
+ - ext/teptris_ext/engine/src/teptris/schema/metaschemas.h
61
+ - ext/teptris_ext/engine/src/teptris/schema/refs.c
62
+ - ext/teptris_ext/engine/src/teptris/schema/refs.h
59
63
  - ext/teptris_ext/engine/src/teptris/schema/regex.c
60
64
  - ext/teptris_ext/engine/src/teptris/schema/regex.h
61
65
  - ext/teptris_ext/engine/src/teptris/schema/schema.c
62
66
  - ext/teptris_ext/engine/src/teptris/schema/schema_internal.h
67
+ - ext/teptris_ext/engine/src/teptris/schema/unicode_props.c
68
+ - ext/teptris_ext/engine/src/teptris/schema/unicode_props.h
63
69
  - ext/teptris_ext/engine/src/teptris/schema/validate.c
64
70
  - ext/teptris_ext/engine/src/teptris/version.c
65
71
  - ext/teptris_ext/extconf.rb