MMgr v2.3.0
Zero-heap memory manager in C11. One borrowed buffer, carved by asserted offset.
Loading...
Searching...
No Matches
numeros_scribo.h
Go to the documentation of this file.
1/* MMgr - Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2 * SPDX-License-Identifier: AGPL-3.0-or-later OR LicenseRef-Commercial OR LicenseRef-Educational
3 *
4 * Every use falls under AGPL-3.0-or-later unless you hold explicit permission, which is either a
5 * negotiated commercial licensing contract or an educator's license issued to you personally.
6 */
7/**
8 * @file numeros_scribo.h
9 * @brief Formatted output: the field types, the value union, the macros that build both, and the numer table.
10 * @author dstroy0 (Douglas Quigg) <dquigg123@gmail.com>
11 * @date 2026-08-29
12 *
13 * @note A spec is an mmgr_field array with MMGR_FK_END at its end. Every field but MMGR_FK_LIT takes one
14 * mmgr_fval.
15 * @note Includes verba_scribo.h, which is where mmgr_config.h and the verba table come from.
16 */
17#ifndef MMGR_NUMEROS_SCRIBO_H
18#define MMGR_NUMEROS_SCRIBO_H
19
20/** @addtogroup mod_numer
21 * @{ */
22
24
25EMBED_BEGIN_DECLS
26
27/**
28 * @brief What one field formats, and which mmgr_fval arm it reads.
29 *
30 * @note numeros_scribo.c indexes s_type by this value, and so MMGR_FK_XML is the largest one it accepts.
31 * @note One byte on every compiler, through EMBED_ENUM_BYTE.
32 */
33EMBED_ENUM_BYTE(mmgr_fk, MMGR_FK_END = 0, /**< Ends a spec. numer_build stops at this field. */
34 MMGR_FK_LIT, /**< Literal text, taken from mmgr_field::lit and mmgr_field::len. */
35 MMGR_FK_STR, /**< String from as.s, through mmgr_verba_put. */
36 MMGR_FK_U32, /**< Unsigned 32-bit from as.u32, base 10, through mmgr_verba_u32. */
37 MMGR_FK_U64, /**< Unsigned 64-bit from as.u64, base 10, through mmgr_verba_u64. */
38 MMGR_FK_I64, /**< Signed 64-bit from as.i64, base 10, through mmgr_verba_i64. */
39 MMGR_FK_DEC, /**< Unsigned 32-bit from as.u32, base 10, through mmgr_verba_u32w. */
40 MMGR_FK_HEX, /**< Unsigned 64-bit from as.u64, base 16, through mmgr_verba_hex. */
41 MMGR_FK_OCT, /**< Unsigned 64-bit from as.u64, base 8, through mmgr_verba_uint. */
42 MMGR_FK_G, /**< Double from as.d, through mmgr_verba_g, with six significant digits by default. */
43 MMGR_FK_FIX, /**< Double from as.d, through mmgr_verba_fixed. */
44 MMGR_FK_CH, /**< Single character from as.c, through mmgr_verba_ch. */
45 MMGR_FK_JSON, /**< String from as.s, through mmgr_verba_json. */
46 MMGR_FK_XML /**< String from as.s, through mmgr_verba_xml. */
47);
48
49/**
50 * @brief One field of a spec.
51 *
52 * @note numer_build reads literal and bytes only for MMGR_FK_LIT, and width only for the other types.
53 */
54typedef struct
55{
56 mmgr_fk type; /**< What this field formats. */
57 uint8_t width; /**< Width to give the value, or 0 to take the type's default from s_type. */
58 uint16_t bytes; /**< Bytes of literal to write, for MMGR_FK_LIT. */
59 const char *literal; /**< Literal text, for MMGR_FK_LIT [BORROWS]. */
61
62/** @brief Expands to an mmgr_field of type MMGR_FK_STR, with width 0, bytes 0 and literal NULL. */
63#define MMGR_STR {MMGR_FK_STR, 0u, 0u, NULL}
64
65/** @brief Expands to an mmgr_field of type MMGR_FK_U32, with width 0, bytes 0 and literal NULL. */
66#define MMGR_U32 {MMGR_FK_U32, 0u, 0u, NULL}
67
68/** @brief Expands to an mmgr_field of type MMGR_FK_U64, with width 0, bytes 0 and literal NULL. */
69#define MMGR_U64 {MMGR_FK_U64, 0u, 0u, NULL}
70
71/** @brief Expands to an mmgr_field of type MMGR_FK_I64, with width 0, bytes 0 and literal NULL. */
72#define MMGR_I64 {MMGR_FK_I64, 0u, 0u, NULL}
73
74/** @brief Expands to an mmgr_field of type MMGR_FK_CH, with width 0, bytes 0 and literal NULL. */
75#define MMGR_CH {MMGR_FK_CH, 0u, 0u, NULL}
76
77/** @brief Expands to an mmgr_field of type MMGR_FK_JSON, with width 0, bytes 0 and literal NULL. */
78#define MMGR_JSON {MMGR_FK_JSON, 0u, 0u, NULL}
79
80/** @brief Expands to an mmgr_field of type MMGR_FK_XML, with width 0, bytes 0 and literal NULL. */
81#define MMGR_XML {MMGR_FK_XML, 0u, 0u, NULL}
82
83/**
84 * @brief Expands to an mmgr_field of type MMGR_FK_END, with width 0, bytes 0 and literal NULL.
85 *
86 * @note numer_build stops at this field, and so every spec array needs one as its last entry.
87 */
88#define MMGR_END {MMGR_FK_END, 0u, 0u, NULL}
89
90/**
91 * @brief One value, tagged with the type that selects which arm of as holds it.
92 *
93 * @note numer_build requires type to equal the field's type, and abandons the whole write when it does not.
94 * @note numer_emit takes width from here, where numer_build takes it from the field.
95 * @warning numer_emit_one reads the arm that type names and no other. A value whose type and
96 * filled arm disagree formats whatever that arm holds.
97 */
98typedef struct
99{
100 mmgr_fk type; /**< Which arm of as holds the value. */
101 union {
102 const char *text; /**< Read for MMGR_FK_STR, MMGR_FK_JSON and MMGR_FK_XML [BORROWS]. */
103 uint32_t u32; /**< Read for MMGR_FK_U32 and MMGR_FK_DEC. */
104 uint64_t u64; /**< Read for MMGR_FK_U64, MMGR_FK_HEX and MMGR_FK_OCT. */
105 int64_t i64; /**< Read for MMGR_FK_I64. */
106 double real; /**< Read for MMGR_FK_G and MMGR_FK_FIX. */
107 char character; /**< Read for MMGR_FK_CH. */
108 } as; /**< The value, under the arm that type names. */
109 uint8_t width; /**< Width numer_emit gives this value, or 0 to take the type's default from s_type. */
110} mmgr_fval;
111
112/**
113 * @brief Expands to an mmgr_fval of type MMGR_FK_STR, with text_ in as.text and width 0.
114 *
115 * @param[in] text_ String placed in as.text [BORROWS].
116 */
117#define MMGR_VSTR(text_) {MMGR_FK_STR, {.text = (text_)}, 0u}
118
119/**
120 * @brief Expands to an mmgr_fval of type MMGR_FK_U32, with value_ in as.u32 and width 0.
121 *
122 * @param[in] value_ Value placed in as.u32.
123 */
124#define MMGR_VU32(value_) {MMGR_FK_U32, {.u32 = (value_)}, 0u}
125
126/**
127 * @brief Expands to an mmgr_fval of type MMGR_FK_U64, with value_ in as.u64 and width 0.
128 *
129 * @param[in] value_ Value placed in as.u64.
130 */
131#define MMGR_VU64(value_) {MMGR_FK_U64, {.u64 = (value_)}, 0u}
132
133/**
134 * @brief Expands to an mmgr_fval of type MMGR_FK_I64, with value_ in as.i64 and width 0.
135 *
136 * @param[in] value_ Value placed in as.i64.
137 */
138#define MMGR_VI64(value_) {MMGR_FK_I64, {.i64 = (value_)}, 0u}
139
140/**
141 * @brief Expands to an mmgr_fval of type MMGR_FK_DEC, with value_ in as.u32 and width 0.
142 *
143 * @param[in] value_ Value placed in as.u32.
144 */
145#define MMGR_VDEC(value_) {MMGR_FK_DEC, {.u32 = (value_)}, 0u}
146
147/**
148 * @brief Expands to an mmgr_fval of type MMGR_FK_HEX, with value_ in as.u64 and width 0.
149 *
150 * @param[in] value_ Value placed in as.u64.
151 */
152#define MMGR_VHEX(value_) {MMGR_FK_HEX, {.u64 = (value_)}, 0u}
153
154/**
155 * @brief Expands to an mmgr_fval of type MMGR_FK_OCT, with value_ in as.u64 and width 0.
156 *
157 * @param[in] value_ Value placed in as.u64.
158 */
159#define MMGR_VOCT(value_) {MMGR_FK_OCT, {.u64 = (value_)}, 0u}
160
161/**
162 * @brief Expands to an mmgr_fval of type MMGR_FK_G, with value_ in as.real and width 0.
163 *
164 * @param[in] value_ Value placed in as.real.
165 */
166#define MMGR_VG(value_) {MMGR_FK_G, {.real = (value_)}, 0u}
167
168/**
169 * @brief Expands to an mmgr_fval of type MMGR_FK_FIX, with value_ in as.real and width 0.
170 *
171 * @param[in] value_ Value placed in as.real.
172 */
173#define MMGR_VFIX(value_) {MMGR_FK_FIX, {.real = (value_)}, 0u}
174
175/**
176 * @brief Expands to an mmgr_fval of type MMGR_FK_CH, with character_ in as.character and width 0.
177 *
178 * @param[in] character_ Character placed in as.character.
179 */
180#define MMGR_VCH(character_) {MMGR_FK_CH, {.character = (character_)}, 0u}
181
182/**
183 * @brief Expands to an mmgr_fval of type MMGR_FK_JSON, with text_ in as.text and width 0.
184 *
185 * @param[in] text_ String placed in as.text [BORROWS].
186 */
187#define MMGR_VJSON(text_) {MMGR_FK_JSON, {.text = (text_)}, 0u}
188
189/**
190 * @brief Expands to an mmgr_fval of type MMGR_FK_XML, with text_ in as.text and width 0.
191 *
192 * @param[in] text_ String placed in as.text [BORROWS].
193 */
194#define MMGR_VXML(text_) {MMGR_FK_XML, {.text = (text_)}, 0u}
195
196/**
197 * @brief Expands to an mmgr_fval of type MMGR_FK_DEC, with value_ in as.u32 and width_ in width.
198 *
199 * @param[in] value_ Value placed in as.u32.
200 * @param[in] width_ Width placed in the width member.
201 * @note numer_emit uses that width. numer_build takes the width from the field instead.
202 */
203#define MMGR_VDECW(value_, width_) {MMGR_FK_DEC, {.u32 = (value_)}, (width_)}
204
205/**
206 * @brief Expands to an mmgr_fval of type MMGR_FK_HEX, with value_ in as.u64 and width_ in width.
207 *
208 * @param[in] value_ Value placed in as.u64.
209 * @param[in] width_ Width placed in the width member.
210 * @note numer_emit uses that width. numer_build takes the width from the field instead.
211 */
212#define MMGR_VHEXW(value_, width_) {MMGR_FK_HEX, {.u64 = (value_)}, (width_)}
213
214/**
215 * @brief Expands to an mmgr_fval of type MMGR_FK_OCT, with value_ in as.u64 and width_ in width.
216 *
217 * @param[in] value_ Value placed in as.u64.
218 * @param[in] width_ Width placed in the width member.
219 * @note numer_emit uses that width. numer_build takes the width from the field instead.
220 */
221#define MMGR_VOCTW(value_, width_) {MMGR_FK_OCT, {.u64 = (value_)}, (width_)}
222
223/**
224 * @brief Expands to an mmgr_fval of type MMGR_FK_G, with value_ in as.real and width_ in width.
225 *
226 * @param[in] value_ Value placed in as.real.
227 * @param[in] width_ Width placed in the width member.
228 * @note numer_emit_one passes that width to mmgr_verba_g as VerbaFractioCfg::sig, the significant
229 * digits it retains.
230 * @note numer_emit uses it. numer_build takes the width from the field instead.
231 */
232#define MMGR_VGW(value_, width_) {MMGR_FK_G, {.real = (value_)}, (width_)}
233
234/**
235 * @brief Expands to an mmgr_fval of type MMGR_FK_FIX, with value_ in as.real and width_ in width.
236 *
237 * @param[in] value_ Value placed in as.real.
238 * @param[in] width_ Width placed in the width member.
239 * @note numer_emit_one passes that width to mmgr_verba_fixed as VerbaFractioCfg::decimals, the digits
240 * it writes after the point.
241 * @note numer_emit uses it. numer_build takes the width from the field instead.
242 */
243#define MMGR_VFIXW(value_, width_) {MMGR_FK_FIX, {.real = (value_)}, (width_)}
244
245/**
246 * @brief Arguments for the four numer calls.
247 *
248 * @note build and append read all six members. emit and emit_append leave spec alone.
249 * @note at is the cursor, as in verba. The build and emit entries begin there and return where they
250 * finished. So a run of writes passes the cursor along and does not measure the text again
251 * between each. Leave
252 * it unset and a call starts at the first byte, as a single write wants.
253 */
254typedef struct
255{
256 char *const out; /**< Destination buffer [BORROWS]. */
257 const size_t cap; /**< Bytes available in out. */
258 const size_t at; /**< Offset where the write begins; 0 for the first write. */
259 const mmgr_field *const spec; /**< Field list, with MMGR_FK_END at its end [BORROWS]. */
260 const mmgr_fval *const vals; /**< Values to place into the fields [BORROWS]. */
261 const size_t nvals; /**< Values in vals. */
262} NumerosCfg;
263
264/**
265 * @brief Type of the numer dispatch table.
266 *
267 * @note EMBED_TABLE_LAYOUT asserts the four members sit at consecutive EMBED_FUNCTION_POINTER_BYTES offsets, with
268 * nothing else.
269 * @note build and emit begin at NumerosCfg::at, which is out's first byte while the caller leaves it
270 * unset. The two append members begin past the text already there.
271 */
272typedef struct
273{
274 size_t (*build)(const NumerosCfg *args); /**< Writes a spec and its values. */
275 size_t (*append)(const NumerosCfg *args); /**< Writes a spec and its values after the text in out. */
276 size_t (*emit)(const NumerosCfg *args); /**< Writes values, with no field list. */
277 size_t (*emit_append)(const NumerosCfg *args); /**< Writes values after the text in out, with no field list. */
279EMBED_TABLE_LAYOUT(NumerosScriboNs, build, append, emit, emit_append);
280
281/**
282 * @brief Writes args->spec and args->vals into args->out, from args->at on.
283 *
284 * @param[in] args Buffer, capacity, the field list and the values [BORROWS].
285 * @return Length of the whole string in args->out, with its terminator not in the count, or 0
286 * when nothing went in.
287 * @note An MMGR_FK_LIT field writes its own text. Every other field consumes the next value in
288 * args->vals.
289 * @note Returns 0 and puts the terminator back at args->at when a value is absent, when a type differs,
290 * or when values remain. That leaves the text written before this call whole.
291 * @warning args->spec must reach an MMGR_FK_END field, and args->vals must hold args->nvals values.
292 */
293EMBED_HOST_DEVICE size_t mmgr_numer_build(const NumerosCfg *args);
294
295/**
296 * @brief Writes args->spec and args->vals into args->out after the string already there.
297 *
298 * @param[in] args Buffer, capacity, the field list and the values [BORROWS].
299 * @return Length of the whole string in args->out, or 0 when nothing went in.
300 * @note Carries on at args->at when the caller threaded one, and measures the existing string with
301 * cellul.len only when args->at is 0. Either way the build gets the whole of args->cap and
302 * that offset.
303 * @note Puts the terminator back at the offset it began from and returns 0 when the build writes nothing.
304 * @warning args->spec must reach an MMGR_FK_END field. The args->out buffer must hold a terminated
305 * string when args->at is 0, since that is the only time the call measures the length.
306 */
307EMBED_HOST_DEVICE size_t mmgr_numer_append(const NumerosCfg *args);
308
309/**
310 * @brief Writes args->vals into args->out, from args->at on and with no field list.
311 *
312 * @param[in] args Buffer, capacity and the values [BORROWS].
313 * @return Length of the whole string in args->out, with its terminator not in the count, or 0
314 * when nothing went in.
315 * @note Each value carries its own width, and no type check runs, since there are no fields to match against.
316 * @warning args->vals must hold args->nvals values.
317 * @warning A value of type MMGR_FK_END or MMGR_FK_LIT formats nothing, which abandons the write and returns 0.
318 */
319EMBED_HOST_DEVICE size_t mmgr_numer_emit(const NumerosCfg *args);
320
321/**
322 * @brief Writes args->vals into args->out after the string already there, reading no field list.
323 *
324 * @param[in] args Buffer, capacity and the values [BORROWS].
325 * @return Length of the whole string in args->out, or 0 when nothing went in.
326 * @note Carries on at args->at when the caller threaded one, and measures the existing string with
327 * cellul.len only when args->at is 0. Either way the emit gets the whole of args->cap and
328 * that offset.
329 * @note Puts the terminator back at the offset it began from and returns 0 when the emit writes nothing.
330 * @warning args->vals must hold args->nvals values. The args->out buffer must hold a terminated
331 * string when args->at is 0, since that is the only time the call measures the length.
332 */
333EMBED_HOST_DEVICE size_t mmgr_numer_emit_append(const NumerosCfg *args);
334
335/**
336 * @brief Dispatch table instance named numer.
337 *
338 * @note Each member calls the mmgr_numer_ function of the same name, one to one.
339 * @note mmgr_numer_append reaches build through this table, and mmgr_numer_emit_append reaches emit.
340 */
341EMBED_TABLE_STORAGE NumerosScriboNs numer EMBED_UNUSED = {
342 .build = mmgr_numer_build,
343 .append = mmgr_numer_append,
344 .emit = mmgr_numer_emit,
345 .emit_append = mmgr_numer_emit_append,
346};
347
348EMBED_END_DECLS
349
350/** @} */
351
352#endif
EMBED_HOST_DEVICE size_t mmgr_numer_emit_append(const NumerosCfg *args)
Writes args->vals into args->out after the string already there, reading no field list.
mmgr_fk
What one field formats, and which mmgr_fval arm it reads.
EMBED_HOST_DEVICE size_t mmgr_numer_append(const NumerosCfg *args)
Writes args->spec and args->vals into args->out after the string already there.
EMBED_HOST_DEVICE size_t mmgr_numer_emit(const NumerosCfg *args)
Writes args->vals into args->out, from args->at on and with no field list.
EMBED_HOST_DEVICE size_t mmgr_numer_build(const NumerosCfg *args)
Writes args->spec and args->vals into args->out, from args->at on.
static const NumerosScriboNs numer
Dispatch table instance named numer.
@ MMGR_FK_OCT
@ MMGR_FK_JSON
@ MMGR_FK_LIT
@ MMGR_FK_I64
@ MMGR_FK_HEX
@ MMGR_FK_FIX
@ MMGR_FK_STR
@ MMGR_FK_END
@ MMGR_FK_G
@ MMGR_FK_U32
@ MMGR_FK_U64
@ MMGR_FK_XML
@ MMGR_FK_DEC
@ MMGR_FK_CH
Arguments for the four numer calls.
const size_t cap
const size_t at
const mmgr_fval *const vals
const size_t nvals
const mmgr_field *const spec
char *const out
Type of the numer dispatch table.
size_t(*) emit_append(const NumerosCfg *args)
size_t(*) append(const NumerosCfg *args)
size_t(*) build(const NumerosCfg *args)
size_t(*) emit(const NumerosCfg *args)
One field of a spec.
const char * literal
uint8_t width
uint16_t bytes
One value, tagged with the type that selects which arm of as holds it.
uint8_t width
Text and number writes, the limits, and one table per type of thing written.