yeptris 0.6.64.0-riscv64-linux-gnu

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.
Files changed (164) hide show
  1. checksums.yaml +7 -0
  2. data/README.adoc +150 -0
  3. data/ext/build_windows_native.rb +60 -0
  4. data/ext/libyeptris/extconf.rb +57 -0
  5. data/ext/yeptris_native/cbor_ruby.c +198 -0
  6. data/ext/yeptris_native/extconf.rb +99 -0
  7. data/ext/yeptris_native/json_ruby.c +414 -0
  8. data/ext/yeptris_native/version.h +29 -0
  9. data/ext/yeptris_native/yeptris_native.c +507 -0
  10. data/lib/yeptris/3.3/native.so +0 -0
  11. data/lib/yeptris/cbor.rb +169 -0
  12. data/lib/yeptris/document.rb +319 -0
  13. data/lib/yeptris/ffi.rb +572 -0
  14. data/lib/yeptris/json/descriptor.rb +266 -0
  15. data/lib/yeptris/json.rb +502 -0
  16. data/lib/yeptris/materializer.rb +389 -0
  17. data/lib/yeptris/node.rb +482 -0
  18. data/lib/yeptris/psych/class_loader.rb +24 -0
  19. data/lib/yeptris/psych/coder_shim.rb +55 -0
  20. data/lib/yeptris/psych/drop_in.rb +63 -0
  21. data/lib/yeptris/psych/encodable.rb +14 -0
  22. data/lib/yeptris/psych/handler.rb +95 -0
  23. data/lib/yeptris/psych/parser.rb +114 -0
  24. data/lib/yeptris/psych/scalar_scanner.rb +149 -0
  25. data/lib/yeptris/psych/tree_builder.rb +119 -0
  26. data/lib/yeptris/psych/visitors.rb +670 -0
  27. data/lib/yeptris/psych.rb +796 -0
  28. data/lib/yeptris/schema.rb +253 -0
  29. data/lib/yeptris/validation.rb +320 -0
  30. data/lib/yeptris/valueml.rb +477 -0
  31. data/lib/yeptris/yaml/descriptor.rb +56 -0
  32. data/lib/yeptris/yaml.rb +668 -0
  33. data/lib/yeptris.rb +130 -0
  34. data/libyeptris.so +0 -0
  35. data/vendor/libyeptris/CMakeLists.txt +280 -0
  36. data/vendor/libyeptris/cmake/yeptris-config.cmake.in +5 -0
  37. data/vendor/libyeptris/cmake/yeptris.pc.in +11 -0
  38. data/vendor/libyeptris/src/CMakeLists.txt +183 -0
  39. data/vendor/libyeptris/src/include/yeptris/api.h +32 -0
  40. data/vendor/libyeptris/src/include/yeptris/cbor.h +174 -0
  41. data/vendor/libyeptris/src/include/yeptris/dom.h +196 -0
  42. data/vendor/libyeptris/src/include/yeptris/emit.h +128 -0
  43. data/vendor/libyeptris/src/include/yeptris/error.h +42 -0
  44. data/vendor/libyeptris/src/include/yeptris/events.h +171 -0
  45. data/vendor/libyeptris/src/include/yeptris/json.h +57 -0
  46. data/vendor/libyeptris/src/include/yeptris/json.hpp +258 -0
  47. data/vendor/libyeptris/src/include/yeptris/jsonc_compat.h +115 -0
  48. data/vendor/libyeptris/src/include/yeptris/marshal.h +75 -0
  49. data/vendor/libyeptris/src/include/yeptris/parse.h +121 -0
  50. data/vendor/libyeptris/src/include/yeptris/plan.h +100 -0
  51. data/vendor/libyeptris/src/include/yeptris/resolve.h +81 -0
  52. data/vendor/libyeptris/src/include/yeptris/schema.h +121 -0
  53. data/vendor/libyeptris/src/include/yeptris/tape.h +128 -0
  54. data/vendor/libyeptris/src/include/yeptris/types.h +38 -0
  55. data/vendor/libyeptris/src/include/yeptris/validate.h +401 -0
  56. data/vendor/libyeptris/src/include/yeptris/values.h +108 -0
  57. data/vendor/libyeptris/src/include/yeptris/version.h.in +29 -0
  58. data/vendor/libyeptris/src/include/yeptris/visit.h +77 -0
  59. data/vendor/libyeptris/src/include/yeptris/yajl_compat.h +135 -0
  60. data/vendor/libyeptris/src/include/yeptris.h +24 -0
  61. data/vendor/libyeptris/src/yeptris/build.c +393 -0
  62. data/vendor/libyeptris/src/yeptris/cbor/cbor.h +35 -0
  63. data/vendor/libyeptris/src/yeptris/cbor/decode.c +1054 -0
  64. data/vendor/libyeptris/src/yeptris/cbor/encode.c +836 -0
  65. data/vendor/libyeptris/src/yeptris/cbor/encode_priv.h +32 -0
  66. data/vendor/libyeptris/src/yeptris/cbor/from_marshal.c +982 -0
  67. data/vendor/libyeptris/src/yeptris/cbor/sink.h +36 -0
  68. data/vendor/libyeptris/src/yeptris/common/chartype.c +41 -0
  69. data/vendor/libyeptris/src/yeptris/common/chartype.h +77 -0
  70. data/vendor/libyeptris/src/yeptris/common/cpu.c +81 -0
  71. data/vendor/libyeptris/src/yeptris/common/cpu.h +40 -0
  72. data/vendor/libyeptris/src/yeptris/common/error.c +65 -0
  73. data/vendor/libyeptris/src/yeptris/common/error.h +81 -0
  74. data/vendor/libyeptris/src/yeptris/common/mutex.h +45 -0
  75. data/vendor/libyeptris/src/yeptris/common/nametab.c +275 -0
  76. data/vendor/libyeptris/src/yeptris/common/nametab.h +68 -0
  77. data/vendor/libyeptris/src/yeptris/common/port.h +74 -0
  78. data/vendor/libyeptris/src/yeptris/common/simd_text.c +49 -0
  79. data/vendor/libyeptris/src/yeptris/common/simd_text.h +342 -0
  80. data/vendor/libyeptris/src/yeptris/common/simd_text_avx2.c +672 -0
  81. data/vendor/libyeptris/src/yeptris/common/simd_text_neon.c +793 -0
  82. data/vendor/libyeptris/src/yeptris/common/simd_text_scalar.c +324 -0
  83. data/vendor/libyeptris/src/yeptris/common/string_view.c +34 -0
  84. data/vendor/libyeptris/src/yeptris/common/string_view.h +93 -0
  85. data/vendor/libyeptris/src/yeptris/doc.h +86 -0
  86. data/vendor/libyeptris/src/yeptris/dom/dom.c +1312 -0
  87. data/vendor/libyeptris/src/yeptris/dom/dom.h +332 -0
  88. data/vendor/libyeptris/src/yeptris/dom/hpool.c +107 -0
  89. data/vendor/libyeptris/src/yeptris/dom/mapindex.c +173 -0
  90. data/vendor/libyeptris/src/yeptris/dom/mapindex.h +61 -0
  91. data/vendor/libyeptris/src/yeptris/dom/mutate.c +431 -0
  92. data/vendor/libyeptris/src/yeptris/dom/ytape.c +2323 -0
  93. data/vendor/libyeptris/src/yeptris/dom/ytape.h +96 -0
  94. data/vendor/libyeptris/src/yeptris/emit/emit.c +670 -0
  95. data/vendor/libyeptris/src/yeptris/emit/float/api.h +43 -0
  96. data/vendor/libyeptris/src/yeptris/emit/float/dragon.c +405 -0
  97. data/vendor/libyeptris/src/yeptris/emit/float/floatint.h +231 -0
  98. data/vendor/libyeptris/src/yeptris/emit/float/fpconv.c +428 -0
  99. data/vendor/libyeptris/src/yeptris/emit/float/print.c +396 -0
  100. data/vendor/libyeptris/src/yeptris/emit/style.c +21 -0
  101. data/vendor/libyeptris/src/yeptris/emit/style.h +32 -0
  102. data/vendor/libyeptris/src/yeptris/emit/writer.c +1951 -0
  103. data/vendor/libyeptris/src/yeptris/emit/writer.h +111 -0
  104. data/vendor/libyeptris/src/yeptris/encoding/bom.c +31 -0
  105. data/vendor/libyeptris/src/yeptris/encoding/encoding.h +63 -0
  106. data/vendor/libyeptris/src/yeptris/encoding/transcode.c +157 -0
  107. data/vendor/libyeptris/src/yeptris/encoding/utf8_validate.c +178 -0
  108. data/vendor/libyeptris/src/yeptris/events/capture.c +166 -0
  109. data/vendor/libyeptris/src/yeptris/events/capture.h +39 -0
  110. data/vendor/libyeptris/src/yeptris/events/iterparse.c +298 -0
  111. data/vendor/libyeptris/src/yeptris/events/pull.c +117 -0
  112. data/vendor/libyeptris/src/yeptris/events/push.c +151 -0
  113. data/vendor/libyeptris/src/yeptris/events/recorder.c +144 -0
  114. data/vendor/libyeptris/src/yeptris/events/values.c +630 -0
  115. data/vendor/libyeptris/src/yeptris/events/values_priv.h +53 -0
  116. data/vendor/libyeptris/src/yeptris/events/yaml_compat.c +86 -0
  117. data/vendor/libyeptris/src/yeptris/events/yaml_compat.h +90 -0
  118. data/vendor/libyeptris/src/yeptris/jsonapi/jsonc_compat.c +687 -0
  119. data/vendor/libyeptris/src/yeptris/jsonapi/yajl_compat.c +728 -0
  120. data/vendor/libyeptris/src/yeptris/marshal/to_json.c +650 -0
  121. data/vendor/libyeptris/src/yeptris/marshal.c +988 -0
  122. data/vendor/libyeptris/src/yeptris/memory/allocator.c +20 -0
  123. data/vendor/libyeptris/src/yeptris/memory/allocator.h +43 -0
  124. data/vendor/libyeptris/src/yeptris/memory/arena.c +150 -0
  125. data/vendor/libyeptris/src/yeptris/memory/arena.h +52 -0
  126. data/vendor/libyeptris/src/yeptris/memory/pool.c +133 -0
  127. data/vendor/libyeptris/src/yeptris/memory/pool.h +36 -0
  128. data/vendor/libyeptris/src/yeptris/parse/engine.c +6528 -0
  129. data/vendor/libyeptris/src/yeptris/parse/engine.h +96 -0
  130. data/vendor/libyeptris/src/yeptris/parse/events.h +137 -0
  131. data/vendor/libyeptris/src/yeptris/parse/numbers.c +374 -0
  132. data/vendor/libyeptris/src/yeptris/parse/numbers.h +33 -0
  133. data/vendor/libyeptris/src/yeptris/parse/scalars.c +435 -0
  134. data/vendor/libyeptris/src/yeptris/parse/scalars.h +87 -0
  135. data/vendor/libyeptris/src/yeptris/parse/stream_iter.c +358 -0
  136. data/vendor/libyeptris/src/yeptris/parse.c +1074 -0
  137. data/vendor/libyeptris/src/yeptris/plan.c +824 -0
  138. data/vendor/libyeptris/src/yeptris/resolve/compat11.c +339 -0
  139. data/vendor/libyeptris/src/yeptris/resolve/core12.c +153 -0
  140. data/vendor/libyeptris/src/yeptris/resolve/full12.c +201 -0
  141. data/vendor/libyeptris/src/yeptris/resolve/json12.c +109 -0
  142. data/vendor/libyeptris/src/yeptris/resolve/resolver.h +103 -0
  143. data/vendor/libyeptris/src/yeptris/resolve/tags.c +250 -0
  144. data/vendor/libyeptris/src/yeptris/scan/json.c +839 -0
  145. data/vendor/libyeptris/src/yeptris/scan/json.h +116 -0
  146. data/vendor/libyeptris/src/yeptris/scan/scan.c +862 -0
  147. data/vendor/libyeptris/src/yeptris/scan/scan.h +177 -0
  148. data/vendor/libyeptris/src/yeptris/schema.c +309 -0
  149. data/vendor/libyeptris/src/yeptris/tape.c +2375 -0
  150. data/vendor/libyeptris/src/yeptris/tape_in.h +39 -0
  151. data/vendor/libyeptris/src/yeptris/validate/compile.c +306 -0
  152. data/vendor/libyeptris/src/yeptris/validate/internal.h +347 -0
  153. data/vendor/libyeptris/src/yeptris/validate/jsonschema.c +1922 -0
  154. data/vendor/libyeptris/src/yeptris/validate/output.c +246 -0
  155. data/vendor/libyeptris/src/yeptris/validate/regexp.c +1600 -0
  156. data/vendor/libyeptris/src/yeptris/validate/registry.c +118 -0
  157. data/vendor/libyeptris/src/yeptris/validate/resources.c +601 -0
  158. data/vendor/libyeptris/src/yeptris/validate/stream.c +1155 -0
  159. data/vendor/libyeptris/src/yeptris/validate/validate.c +1043 -0
  160. data/vendor/libyeptris/src/yeptris/version.c +7 -0
  161. data/vendor/libyeptris/src/yeptris/visit/dom_visit.c +115 -0
  162. data/vendor/libyeptris/src/yeptris/visit/json_visit.c +293 -0
  163. data/vendor/libyeptris/src/yeptris/visit/yaml_visit.c +127 -0
  164. metadata +221 -0
@@ -0,0 +1,670 @@
1
+ /* emit.c — public serialization API (TODO.impl/13).
2
+ *
3
+ * Two passes of the ONE writer: dry (exact size), wet (linear writes
4
+ * into the caller's buffer — zero reallocations by construction). */
5
+
6
+ #include <limits.h>
7
+ #include <stddef.h>
8
+ #include <stdlib.h>
9
+ #include <string.h>
10
+
11
+ #include "../../include/yeptris/emit.h"
12
+ #include "../doc.h"
13
+ #include "writer.h"
14
+
15
+ /* Every field reads behind ITS OWN offsetof gate — never the whole
16
+ * struct's sizeof. The #909/#903 append grew sizeof(*opts) to 24, and
17
+ * a `size >= sizeof(*opts)` gate silently orphaned the ORIGINAL
18
+ * fields for callers compiled against the 16-byte struct (the Ruby
19
+ * binding): best_width fell back to 80 and canonical fell off. The
20
+ * versioning contract is per field: declared-present or default. */
21
+ static int opts_canonical(const yeptris_emit_options* opts) {
22
+ return opts != NULL &&
23
+ opts->size >= (uint32_t)(offsetof(yeptris_emit_options, canonical) + sizeof(int)) &&
24
+ opts->canonical;
25
+ }
26
+
27
+ static int opts_indent(const yeptris_emit_options* opts) {
28
+ /* size-versioned (#903); out of range rides the default (libyaml
29
+ * clamps 2..9 the same way) */
30
+ int ind = 2;
31
+ if (opts != NULL &&
32
+ opts->size >= (uint32_t)(offsetof(yeptris_emit_options, best_indent) + sizeof(int))) {
33
+ ind = opts->best_indent;
34
+ }
35
+ if (ind < 2 || ind > 9) {
36
+ ind = 2;
37
+ }
38
+ return ind;
39
+ }
40
+
41
+ static int opts_width(const yeptris_emit_options* opts) {
42
+ /* libyaml's emitter-init clamp, spelled per the caller's declared
43
+ * width (#1257): `best_width < 0` means NEVER fold — Psych's
44
+ * `line_width: -1` unwrap, applied to the flow wrap and the block
45
+ * fold alike; `0 <= best_width <= best_indent*2` snaps to 80
46
+ * (libyaml's too-narrow clamp); absent or 0 rides the default 80. */
47
+ if (opts != NULL &&
48
+ opts->size >= (uint32_t)(offsetof(yeptris_emit_options, best_width) + sizeof(int)) &&
49
+ opts->best_width < 0) {
50
+ return INT_MAX;
51
+ }
52
+ int ind = opts_indent(opts);
53
+ if (opts == NULL ||
54
+ opts->size < (uint32_t)(offsetof(yeptris_emit_options, best_width) + sizeof(int)) ||
55
+ opts->best_width == 0) {
56
+ return 80; /* libyaml best_width parity */
57
+ }
58
+ if (opts->best_width <= ind * 2) {
59
+ return 80; /* the too-narrow clamp (libyaml: w <= best_indent*2) */
60
+ }
61
+ return opts->best_width;
62
+ }
63
+
64
+ static int opts_doc_marker(const yeptris_emit_options* opts) {
65
+ /* size-versioned: callers compiled against the older struct keep
66
+ * the implicit single-document behavior */
67
+ if (opts == NULL ||
68
+ opts->size < (uint32_t)(offsetof(yeptris_emit_options, explicit_doc_start) + sizeof(int))) {
69
+ return 0;
70
+ }
71
+ return opts->explicit_doc_start != 0;
72
+ }
73
+
74
+ static int opts_block_fold(const yeptris_emit_options* opts) {
75
+ /* size-versioned (#903, the #1257 flip): DEFAULT ON — block output
76
+ * folds at best_width like Psych/libyaml, so a host flipping
77
+ * engines never changes stored bytes by accident. An explicit
78
+ * block_fold = 0 (a caller passing the full struct) still
79
+ * unwraps, as does best_width < 0 (the width clamp). */
80
+ if (opts == NULL ||
81
+ opts->size < (uint32_t)(offsetof(yeptris_emit_options, block_fold) + sizeof(int))) {
82
+ return 1;
83
+ }
84
+ return opts->block_fold != 0;
85
+ }
86
+
87
+ /* The emitter's SCRATCH rides the document's allocator (#904 item 3):
88
+ * sc_dec and the streaming window are emitter-internal (never leak
89
+ * into caller hands), so the fail-injection harness — a counting
90
+ * allocator passed at the document's build — now reaches them. The
91
+ * CALLER-RETURNED output buffers stay system-malloc on purpose: their
92
+ * release contract is free() (emit.h "malloc'd buffer, caller frees"),
93
+ * and a failing allocator's pointer cannot be freed by the host. */
94
+ static const yep_allocator* emit_sys(const yeptris_document* doc) {
95
+ return doc->sys != NULL ? doc->sys : yep_system_allocator();
96
+ }
97
+
98
+ YEPTRIS_API size_t yeptris_serialize_into_ex(YeptrisDocument handle,
99
+ const yeptris_emit_options* opts, char* buf,
100
+ size_t cap) {
101
+ if (handle == NULL) {
102
+ return 0;
103
+ }
104
+ yep_emitter em;
105
+ em.w.sc_dec = NULL;
106
+ em.w.sc_i = 0;
107
+ em.w.cap = 0;
108
+ em.w.grow = 0;
109
+ em.w.oom = 0;
110
+ em.w.bounded = 0;
111
+ em.doc = (const yeptris_document*)handle;
112
+ if (yep_doc_dom((yeptris_document*)handle) == NULL) { /* #342 lazy */
113
+ return 0;
114
+ }
115
+ em.w.p = NULL;
116
+ em.w.last = 0;
117
+ em.w.force_flow = 0;
118
+ em.w.in_flow = 0;
119
+ em.w.flow_item = 0;
120
+ em.w.unserializable = 0;
121
+ em.w.canonical = opts_canonical(opts);
122
+ em.w.explicit_doc_start = opts_doc_marker(opts);
123
+ em.w.json = 0;
124
+ em.w.json_compact = 0;
125
+ em.w.json_pretty = 0;
126
+ em.w.pretty_depth = 0;
127
+ em.w.best_width = opts_width(opts);
128
+ em.w.indent = opts_indent(opts);
129
+ em.w.block_fold = opts_block_fold(opts);
130
+ em.w.col = 0;
131
+ em.w.sv_input = handle ? ((const yeptris_document*)handle)->dom->input_base : NULL;
132
+ em.w.sv_arena = handle ? ((const yeptris_document*)handle)->dom->str : NULL;
133
+ em.w.resolver = handle ? ((const yeptris_document*)handle)->dom->resolver : NULL;
134
+ em.w.sink = NULL;
135
+ em.w.watermark = 0;
136
+ em.w.sink_aborted = 0;
137
+ em.w.flushed = 0;
138
+ memset(&em.canon_names, 0, sizeof(em.canon_names));
139
+ if (em.w.canonical && !yep_nametab_init(&em.canon_names, emit_sys(em.doc))) {
140
+ return 0;
141
+ }
142
+ em.w.sc_dec =
143
+ (uint8_t*)yep_alloc(emit_sys(em.doc), em.doc->dom->ncount > 0 ? em.doc->dom->ncount : 1);
144
+ if (em.w.sc_dec == NULL) {
145
+ if (em.canon_names.sys != NULL) {
146
+ yep_nametab_free(&em.canon_names);
147
+ }
148
+ return 0;
149
+ }
150
+ size_t need = yep_emit_run(&em, 1);
151
+ if (buf == NULL || cap < need + 1) {
152
+ yep_free(emit_sys(em.doc), em.w.sc_dec);
153
+ if (em.canon_names.sys != NULL) {
154
+ yep_nametab_free(&em.canon_names);
155
+ }
156
+ return need;
157
+ }
158
+ em.w.p = buf;
159
+ size_t wrote = yep_emit_run(&em, 0);
160
+ yep_free(emit_sys(em.doc), em.w.sc_dec);
161
+ if (em.canon_names.sys != NULL) {
162
+ yep_nametab_free(&em.canon_names);
163
+ }
164
+ buf[wrote] = '\0';
165
+ return wrote;
166
+ }
167
+
168
+ YEPTRIS_API size_t yeptris_serialize_into_trusted(YeptrisDocument handle,
169
+ const yeptris_emit_options* opts, char* buf,
170
+ size_t cap) {
171
+ if (handle == NULL || buf == NULL || cap == 0) {
172
+ return 0;
173
+ }
174
+ yep_emitter em;
175
+ em.w.sc_dec = NULL;
176
+ em.w.sc_i = 0;
177
+ em.w.cap = 0;
178
+ em.w.grow = 0;
179
+ em.w.oom = 0;
180
+ em.w.bounded = 0;
181
+ em.doc = (const yeptris_document*)handle;
182
+ if (yep_doc_dom((yeptris_document*)handle) == NULL) { /* #342 lazy */
183
+ return 0;
184
+ }
185
+ em.w.p = buf;
186
+ em.w.last = 0;
187
+ em.w.force_flow = 0;
188
+ em.w.in_flow = 0;
189
+ em.w.flow_item = 0;
190
+ em.w.unserializable = 0;
191
+ em.w.canonical = opts_canonical(opts);
192
+ em.w.explicit_doc_start = opts_doc_marker(opts);
193
+ em.w.json = 0;
194
+ em.w.json_compact = 0;
195
+ em.w.json_pretty = 0;
196
+ em.w.pretty_depth = 0;
197
+ em.w.best_width = opts_width(opts);
198
+ em.w.indent = opts_indent(opts);
199
+ em.w.block_fold = opts_block_fold(opts);
200
+ em.w.col = 0;
201
+ em.w.sv_input = ((const yeptris_document*)handle)->dom->input_base;
202
+ em.w.sv_arena = ((const yeptris_document*)handle)->dom->str;
203
+ em.w.resolver = ((const yeptris_document*)handle)->dom->resolver;
204
+ em.w.sink = NULL;
205
+ em.w.watermark = 0;
206
+ em.w.sink_aborted = 0;
207
+ em.w.flushed = 0;
208
+ memset(&em.canon_names, 0, sizeof(em.canon_names));
209
+ if (em.w.canonical && !yep_nametab_init(&em.canon_names, emit_sys(em.doc))) {
210
+ return 0;
211
+ }
212
+ /* #352: ONE bounded wet pass — no memo, no dry run (the emit-split
213
+ * table's 2p/1p column priced the dry pass at ~0.55x). Routes derive
214
+ * on demand (the streaming route's bytes); an oversized write sets
215
+ * oom and the pass keeps counting, so the exact size falls out of
216
+ * the same walk and the retry writes identical bytes. */
217
+ em.w.cap = cap;
218
+ em.w.grow = 1;
219
+ em.w.bounded = 1;
220
+ size_t wrote = yep_emit_run(&em, 0);
221
+ em.w.bounded = 0;
222
+ int fit = !em.w.oom && !em.w.unserializable;
223
+ size_t ret = fit ? wrote : em.w.len;
224
+ if (em.canon_names.sys != NULL) {
225
+ yep_nametab_free(&em.canon_names);
226
+ }
227
+ if (!fit) {
228
+ return ret; /* ret >= cap: buf holds a partial document */
229
+ }
230
+ buf[wrote] = '\0';
231
+ return ret; /* ret < cap: the document is complete */
232
+ }
233
+
234
+ YEPTRIS_API void yeptris_free(void* p) {
235
+ free(p);
236
+ }
237
+
238
+ YEPTRIS_API char* yeptris_serialize_ex(YeptrisDocument handle, const yeptris_emit_options* opts,
239
+ size_t* len) {
240
+ if (handle == NULL) {
241
+ return NULL;
242
+ }
243
+ yep_emitter em;
244
+ em.w.sc_dec = NULL;
245
+ em.w.sc_i = 0;
246
+ em.w.cap = 0;
247
+ em.w.grow = 0;
248
+ em.w.oom = 0;
249
+ em.w.bounded = 0;
250
+ em.doc = (const yeptris_document*)handle;
251
+ if (yep_doc_dom((yeptris_document*)handle) == NULL) { /* #342 lazy */
252
+ return NULL;
253
+ }
254
+ em.w.p = NULL;
255
+ em.w.last = 0;
256
+ em.w.force_flow = 0;
257
+ em.w.in_flow = 0;
258
+ em.w.flow_item = 0;
259
+ em.w.unserializable = 0;
260
+ em.w.canonical = opts_canonical(opts);
261
+ em.w.explicit_doc_start = opts_doc_marker(opts);
262
+ em.w.json = 0;
263
+ em.w.json_compact = 0;
264
+ em.w.json_pretty = 0;
265
+ em.w.pretty_depth = 0;
266
+ em.w.best_width = opts_width(opts);
267
+ em.w.indent = opts_indent(opts);
268
+ em.w.block_fold = opts_block_fold(opts);
269
+ em.w.col = 0;
270
+ em.w.sv_input = handle ? ((const yeptris_document*)handle)->dom->input_base : NULL;
271
+ em.w.sv_arena = handle ? ((const yeptris_document*)handle)->dom->str : NULL;
272
+ em.w.resolver = handle ? ((const yeptris_document*)handle)->dom->resolver : NULL;
273
+ em.w.sink = NULL;
274
+ em.w.watermark = 0;
275
+ em.w.sink_aborted = 0;
276
+ em.w.flushed = 0;
277
+ memset(&em.canon_names, 0, sizeof(em.canon_names));
278
+ if (em.w.canonical && !yep_nametab_init(&em.canon_names, emit_sys(em.doc))) {
279
+ return NULL;
280
+ }
281
+ /* #352 slice 2: ONE wet pass into a growable buffer — the dry
282
+ * exact-sizing pass measured 0.54-0.73x of this route on CI-fresh
283
+ * runners (the emit-split table's 2p/1p column). The estimate
284
+ * starts at the input size (+64) and doubles on demand; a final
285
+ * shrink-to-fit keeps the returned allocation tight. No dry pass,
286
+ * so no decisions table. */
287
+ /* raw malloc ON PURPOSE (#904 item 3): this buffer is RETURNED —
288
+ * the release contract is the host's free() (emit.h), so it must
289
+ * come from the system allocator, not the document's */
290
+ size_t est = em.doc->dom->input_len + 64;
291
+ if (est < 256) {
292
+ est = 256;
293
+ }
294
+ char* out = (char*)malloc(est);
295
+ if (out == NULL) {
296
+ if (em.canon_names.sys != NULL) {
297
+ yep_nametab_free(&em.canon_names);
298
+ }
299
+ return NULL;
300
+ }
301
+ em.w.p = out;
302
+ em.w.cap = est;
303
+ em.w.grow = 1;
304
+ size_t wrote = yep_emit_run(&em, 0);
305
+ em.w.grow = 0;
306
+ /* wr_grow reallocs through w->p — the local `out` is stale after
307
+ * any growth; every post-run touch goes through the writer's
308
+ * pointer (the first round's abort was exactly this aliasing) */
309
+ out = em.w.p;
310
+ if (em.w.oom || em.w.unserializable) {
311
+ free(out);
312
+ if (em.canon_names.sys != NULL) {
313
+ yep_nametab_free(&em.canon_names);
314
+ }
315
+ return NULL;
316
+ }
317
+ if (em.w.cap - (wrote + 1) > em.w.cap / 8) {
318
+ char* tight = (char*)realloc(out, wrote + 1);
319
+ if (tight != NULL) {
320
+ out = tight;
321
+ }
322
+ }
323
+ if (em.canon_names.sys != NULL) {
324
+ yep_nametab_free(&em.canon_names);
325
+ }
326
+ out[wrote] = '\0';
327
+ if (len != NULL) {
328
+ *len = wrote;
329
+ }
330
+ return out;
331
+ }
332
+
333
+ /* #739: the ONE-pass JSON route — one wet pass into a growable
334
+ * buffer; the dry exact-sizing pass priced the compact lane ~2x
335
+ * (the #352 slice-2 pattern). The estimate starts at the input size
336
+ * (+64) and doubles on demand; shrink-to-fit at the end. Owns the
337
+ * emitter's canon_names cleanup. */
338
+ static char* yep_json_run_growable(yep_emitter* em, size_t* len) {
339
+ /* raw malloc ON PURPOSE (#904 item 3): the buffer is RETURNED —
340
+ * the host's free() owns it (emit.h), system allocator only */
341
+ size_t est = em->doc->dom->input_len + 64;
342
+ if (est < 256) {
343
+ est = 256;
344
+ }
345
+ char* out = (char*)malloc(est);
346
+ if (out == NULL) {
347
+ if (em->canon_names.sys != NULL) {
348
+ yep_nametab_free(&em->canon_names);
349
+ }
350
+ return NULL;
351
+ }
352
+ em->w.p = out;
353
+ em->w.cap = est;
354
+ em->w.grow = 1;
355
+ size_t wrote = yep_emit_run(em, 0);
356
+ em->w.grow = 0;
357
+ out = em->w.p; /* wr_grow reallocs through w->p — the local is stale */
358
+ if (em->w.oom || em->w.unserializable) {
359
+ free(out);
360
+ if (em->canon_names.sys != NULL) {
361
+ yep_nametab_free(&em->canon_names);
362
+ }
363
+ return NULL;
364
+ }
365
+ if (em->w.cap - (wrote + 1) > em->w.cap / 8) {
366
+ char* tight = (char*)realloc(out, wrote + 1);
367
+ if (tight != NULL) {
368
+ out = tight;
369
+ }
370
+ }
371
+ if (em->canon_names.sys != NULL) {
372
+ yep_nametab_free(&em->canon_names);
373
+ }
374
+ out[wrote] = '\0';
375
+ if (len != NULL) {
376
+ *len = wrote;
377
+ }
378
+ return out;
379
+ }
380
+
381
+ YEPTRIS_API char* yeptris_serialize_json(YeptrisDocument handle, size_t* len) {
382
+ if (handle == NULL) {
383
+ return NULL;
384
+ }
385
+ yep_emitter em;
386
+ em.w.sc_dec = NULL;
387
+ em.w.sc_i = 0;
388
+ em.w.cap = 0;
389
+ em.w.grow = 0;
390
+ em.w.oom = 0;
391
+ em.w.bounded = 0;
392
+ em.doc = (const yeptris_document*)handle;
393
+ if (yep_doc_dom((yeptris_document*)handle) == NULL) { /* #342 lazy */
394
+ return NULL;
395
+ }
396
+ em.w.p = NULL;
397
+ em.w.last = 0;
398
+ em.w.force_flow = 0;
399
+ em.w.in_flow = 0;
400
+ em.w.flow_item = 0;
401
+ em.w.unserializable = 0;
402
+ em.w.canonical = 0;
403
+ em.w.explicit_doc_start = 0; /* JSON output carries no --- */
404
+ em.w.json = 1;
405
+ em.w.json_compact = 0;
406
+ em.w.json_pretty = 0;
407
+ em.w.pretty_depth = 0;
408
+ em.w.best_width = 0; /* JSON: no folding (single-line output) */
409
+ em.w.indent = 2;
410
+ em.w.block_fold = 0;
411
+ em.w.col = 0;
412
+ em.w.sv_input = handle ? ((const yeptris_document*)handle)->dom->input_base : NULL;
413
+ em.w.sv_arena = handle ? ((const yeptris_document*)handle)->dom->str : NULL;
414
+ em.w.resolver = handle ? ((const yeptris_document*)handle)->dom->resolver : NULL;
415
+ em.w.sink = NULL;
416
+ em.w.watermark = 0;
417
+ em.w.sink_aborted = 0;
418
+ em.w.flushed = 0;
419
+ memset(&em.canon_names, 0, sizeof(em.canon_names));
420
+ return yep_json_run_growable(&em, len);
421
+ }
422
+
423
+ YEPTRIS_API char* yeptris_serialize_json_ex(YeptrisDocument handle, size_t* len, int compact) {
424
+ if (handle == NULL) {
425
+ return NULL;
426
+ }
427
+ if (handle == NULL) {
428
+ return NULL;
429
+ }
430
+ yep_emitter em;
431
+ em.w.sc_dec = NULL;
432
+ em.w.sc_i = 0;
433
+ em.w.cap = 0;
434
+ em.w.grow = 0;
435
+ em.w.oom = 0;
436
+ em.w.bounded = 0;
437
+ em.doc = (const yeptris_document*)handle;
438
+ if (yep_doc_dom((yeptris_document*)handle) == NULL) { /* #342 lazy */
439
+ return NULL;
440
+ }
441
+ em.w.p = NULL;
442
+ em.w.last = 0;
443
+ em.w.force_flow = 0;
444
+ em.w.in_flow = 0;
445
+ em.w.flow_item = 0;
446
+ em.w.unserializable = 0;
447
+ em.w.canonical = 0;
448
+ em.w.explicit_doc_start = 0; /* JSON output carries no --- */
449
+ em.w.json = 1;
450
+ em.w.json_compact = compact ? 1 : 0;
451
+ em.w.json_pretty = 0;
452
+ em.w.pretty_depth = 0;
453
+ em.w.best_width = 0; /* JSON: no folding (single-line output) */
454
+ em.w.indent = 2;
455
+ em.w.block_fold = 0;
456
+ em.w.col = 0;
457
+ em.w.sv_input = handle ? ((const yeptris_document*)handle)->dom->input_base : NULL;
458
+ em.w.sv_arena = handle ? ((const yeptris_document*)handle)->dom->str : NULL;
459
+ em.w.resolver = handle ? ((const yeptris_document*)handle)->dom->resolver : NULL;
460
+ em.w.sink = NULL;
461
+ em.w.watermark = 0;
462
+ em.w.sink_aborted = 0;
463
+ em.w.flushed = 0;
464
+ memset(&em.canon_names, 0, sizeof(em.canon_names));
465
+ return yep_json_run_growable(&em, len);
466
+ }
467
+
468
+ YEPTRIS_API char* yeptris_serialize_json_pretty(YeptrisDocument handle, size_t* len,
469
+ const char* indent, size_t indent_len) {
470
+ if (handle == NULL) {
471
+ return NULL;
472
+ }
473
+ return yep_serialize_json_pretty_ex((const yeptris_document*)handle, len, indent, indent_len);
474
+ }
475
+
476
+ char* yep_serialize_json_compact(const yeptris_document* doc, size_t* len) {
477
+ if (doc == NULL) {
478
+ return NULL;
479
+ }
480
+ yep_emitter em;
481
+ em.w.sc_dec = NULL;
482
+ em.w.sc_i = 0;
483
+ em.w.cap = 0;
484
+ em.w.grow = 0;
485
+ em.w.oom = 0;
486
+ em.w.bounded = 0;
487
+ em.doc = doc;
488
+ em.w.p = NULL;
489
+ em.w.last = 0;
490
+ em.w.force_flow = 0;
491
+ em.w.in_flow = 0;
492
+ em.w.flow_item = 0;
493
+ em.w.unserializable = 0;
494
+ em.w.canonical = 0;
495
+ em.w.explicit_doc_start = 0; /* JSON output carries no --- */
496
+ em.w.json = 1;
497
+ em.w.json_compact = 1;
498
+ em.w.json_pretty = 0;
499
+ em.w.pretty_depth = 0;
500
+ em.w.best_width = 0;
501
+ em.w.indent = 2;
502
+ em.w.block_fold = 0;
503
+ em.w.col = 0;
504
+ em.w.sv_input = doc->dom->input_base;
505
+ em.w.sv_arena = doc->dom->str;
506
+ em.w.resolver = doc->dom->resolver;
507
+ em.w.sink = NULL;
508
+ em.w.watermark = 0;
509
+ em.w.sink_aborted = 0;
510
+ em.w.flushed = 0;
511
+ memset(&em.canon_names, 0, sizeof(em.canon_names));
512
+ return yep_json_run_growable(&em, len);
513
+ }
514
+
515
+ char* yep_serialize_json_pretty(const yeptris_document* doc, size_t* len) {
516
+ return yep_serialize_json_pretty_ex(doc, len, NULL, 2);
517
+ }
518
+
519
+ char* yep_serialize_json_pretty_ex(const yeptris_document* doc, size_t* len, const char* indent,
520
+ size_t indent_len) {
521
+ if (doc == NULL) {
522
+ return NULL;
523
+ }
524
+ if (yep_doc_dom((yeptris_document*)doc) == NULL) { /* #342 lazy */
525
+ return NULL;
526
+ }
527
+ yep_emitter em;
528
+ em.w.sc_dec = NULL;
529
+ em.w.sc_i = 0;
530
+ em.w.cap = 0;
531
+ em.w.grow = 0;
532
+ em.w.oom = 0;
533
+ em.w.bounded = 0;
534
+ em.doc = doc;
535
+ em.w.p = NULL;
536
+ em.w.last = 0;
537
+ em.w.force_flow = 0;
538
+ em.w.in_flow = 0;
539
+ em.w.flow_item = 0;
540
+ em.w.unserializable = 0;
541
+ em.w.canonical = 0;
542
+ em.w.explicit_doc_start = 0; /* JSON output carries no --- */
543
+ em.w.json = 1;
544
+ em.w.json_compact = 0;
545
+ em.w.json_pretty = 1;
546
+ em.w.pretty_depth = 0;
547
+ em.w.json_indent = indent;
548
+ em.w.json_indent_len = indent == NULL ? 2 : (int)indent_len;
549
+ em.w.best_width = 0;
550
+ em.w.indent = 2;
551
+ em.w.block_fold = 0;
552
+ em.w.col = 0;
553
+ em.w.sv_input = doc->dom->input_base;
554
+ em.w.sv_arena = doc->dom->str;
555
+ em.w.resolver = doc->dom->resolver;
556
+ em.w.sink = NULL;
557
+ em.w.watermark = 0;
558
+ em.w.sink_aborted = 0;
559
+ em.w.flushed = 0;
560
+ memset(&em.canon_names, 0, sizeof(em.canon_names));
561
+ if (em.w.canonical && !yep_nametab_init(&em.canon_names, emit_sys(doc))) {
562
+ return NULL;
563
+ }
564
+ /* raw malloc ON PURPOSE (#904 item 3): the dry-wet pair's exact-
565
+ * sized output is RETURNED — the host's free() owns it */
566
+ size_t need = yep_emit_run(&em, 1);
567
+ char* out = malloc(need + 1);
568
+ if (out == NULL) {
569
+ if (em.canon_names.sys != NULL) {
570
+ yep_nametab_free(&em.canon_names);
571
+ }
572
+ return NULL;
573
+ }
574
+ em.w.p = out;
575
+ size_t wrote = yep_emit_run(&em, 0);
576
+ if (em.canon_names.sys != NULL) {
577
+ yep_nametab_free(&em.canon_names);
578
+ }
579
+ out[wrote] = '\0';
580
+ if (len != NULL) {
581
+ *len = wrote;
582
+ }
583
+ return out;
584
+ }
585
+
586
+ YEPTRIS_API size_t yeptris_serialize_into(YeptrisDocument handle, char* buf, size_t cap) {
587
+ return yeptris_serialize_into_ex(handle, NULL, buf, cap);
588
+ }
589
+
590
+ YEPTRIS_API char* yeptris_serialize(YeptrisDocument handle, size_t* len) {
591
+ return yeptris_serialize_ex(handle, NULL, len);
592
+ }
593
+
594
+ /* Streaming writer (13C): the writer appends linearly (nothing is
595
+ * back-patched), so flushing at a high-water mark is always safe —
596
+ * memory stays bounded by the mark, never the document. */
597
+ YEPTRIS_API size_t yeptris_serialize_stream(YeptrisDocument handle,
598
+ const yeptris_emit_options* opts,
599
+ yeptris_emit_sink sink, void* ctx) {
600
+ if (handle == NULL || sink == NULL) {
601
+ return 0;
602
+ }
603
+ yep_emitter em;
604
+ em.w.sc_dec = NULL;
605
+ em.w.sc_i = 0;
606
+ em.w.cap = 0;
607
+ em.w.grow = 0;
608
+ em.w.oom = 0;
609
+ em.w.bounded = 0;
610
+ em.doc = (const yeptris_document*)handle;
611
+ if (yep_doc_dom((yeptris_document*)handle) == NULL) { /* #342 lazy */
612
+ return 0;
613
+ }
614
+ em.w.p = NULL;
615
+ em.w.last = 0;
616
+ em.w.force_flow = 0;
617
+ em.w.in_flow = 0;
618
+ em.w.flow_item = 0;
619
+ em.w.unserializable = 0;
620
+ em.w.canonical = opts_canonical(opts);
621
+ em.w.explicit_doc_start = opts_doc_marker(opts);
622
+ em.w.json = 0;
623
+ em.w.json_compact = 0;
624
+ em.w.json_pretty = 0;
625
+ em.w.pretty_depth = 0;
626
+ em.w.best_width = opts_width(opts);
627
+ em.w.indent = opts_indent(opts);
628
+ em.w.block_fold = opts_block_fold(opts);
629
+ em.w.col = 0;
630
+ em.w.sv_input = handle ? ((const yeptris_document*)handle)->dom->input_base : NULL;
631
+ em.w.sv_arena = handle ? ((const yeptris_document*)handle)->dom->str : NULL;
632
+ em.w.resolver = handle ? ((const yeptris_document*)handle)->dom->resolver : NULL;
633
+ em.w.sink = sink;
634
+ em.w.sink_ctx = ctx;
635
+ em.w.watermark = YEP_EMIT_WATERMARK;
636
+ memset(&em.canon_names, 0, sizeof(em.canon_names));
637
+ if (em.w.canonical && !yep_nametab_init(&em.canon_names, emit_sys(em.doc))) {
638
+ return 0;
639
+ }
640
+ /* one scratch window; the writer flushes it when it fills.
641
+ * Document allocator (#904 item 3): emitter-internal scratch —
642
+ * flushed out through the sink, never returned — so the
643
+ * fail-injection harness reaches it. */
644
+ char* window = yep_alloc(emit_sys(em.doc), YEP_EMIT_WATERMARK * 2);
645
+ if (window == NULL) {
646
+ if (em.canon_names.sys != NULL) {
647
+ yep_nametab_free(&em.canon_names);
648
+ }
649
+ return 0;
650
+ }
651
+ em.w.p = window;
652
+ /* the writer owns the window size now: wr_put's chunked path and
653
+ * the fused member write bounds-check against it (a scalar larger
654
+ * than the window used to memcpy straight past it) */
655
+ em.w.cap = YEP_EMIT_WATERMARK * 2;
656
+ em.w.sink_aborted = 0;
657
+ size_t total = yep_emit_run(&em, 0);
658
+ if (em.w.len > 0 && !em.w.sink_aborted) {
659
+ if (sink(ctx, window, em.w.len) != 0) {
660
+ em.w.sink_aborted = 1;
661
+ } else {
662
+ /* total already counted by the flush hooks */
663
+ }
664
+ }
665
+ yep_free(emit_sys(em.doc), window);
666
+ if (em.canon_names.sys != NULL) {
667
+ yep_nametab_free(&em.canon_names);
668
+ }
669
+ return em.w.sink_aborted ? 0 : total;
670
+ }
@@ -0,0 +1,43 @@
1
+ /* api.h — the emitter-facing float printer boundary (TODO.impl/14).
2
+ *
3
+ * Thin adaptation over the clean-room float printer TUs
4
+ * (TODO.impl/14 — no third-party code). Non-finite
5
+ * values print as the YAML core-schema words (.inf/-.inf/.nan), never
6
+ * ryu's "Infinity"/"NaN" — the resolver and the printer agree.
7
+ *
8
+ * Buffer contracts (caller provides; asserted by tests):
9
+ * shortest: 32 bytes; fixed: 2 + 309 + 1 + precision.
10
+ */
11
+ #ifndef YEP_FLOAT_API_H
12
+ #define YEP_FLOAT_API_H
13
+
14
+ #include <stddef.h>
15
+ #include <stdint.h>
16
+
17
+ #ifdef __cplusplus
18
+ extern "C" {
19
+ #endif
20
+
21
+ /* Shortest round-trip decimal for a double. Returns the length written. */
22
+ int yep_d2s_shortest(double d, char* buf);
23
+
24
+ /* Shortest round-trip decimal for a float. Returns the length written. */
25
+ int yep_f2s_shortest(float f, char* buf);
26
+
27
+ /* Fixed-notation output with exactly `precision` fraction digits
28
+ * (Psych Float#to_s / explicit-precision emission). Returns length. */
29
+ int yep_d2fixed(double d, uint32_t precision, char* buf);
30
+
31
+ /* YAML words for non-finite values (finite input returns 0). */
32
+ int yep_d2s_nonfinite(double d, char* buf);
33
+
34
+ /* The json-gem float byte shape (grisu2 digits + fpconv emit
35
+ * windows, vendored in fpconv.c) for JSON-mode canonical scalars
36
+ * (yeptris#651). 32-byte buffer. Returns the length written. */
37
+ int yep_d2s_json(double d, char* buf);
38
+
39
+ #ifdef __cplusplus
40
+ }
41
+ #endif
42
+
43
+ #endif /* YEP_FLOAT_API_H */