MMgr v1.0.0
Zero-heap memory manager in C11. One borrowed buffer, carved by asserted offset.
Loading...
Searching...
No Matches
ascii_persona_bitorum.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 ascii_persona_bitorum.h
9 * @brief ASCII class membership: mask type, class list, and the ascii dispatch table.
10 * @author dstroy0 (Douglas Quigg) <dquigg123@gmail.com>
11 * @date 2026-08-29
12 *
13 * @note Class membership as a bitmap lookup and not a chain of range compares. One shift, one
14 * mask and one test answer any class, and the cost does not grow with how many ranges the
15 * class covers.
16 * @note Covers code points 0 to 127 only. A byte of 0x80 or above is in no class here, and
17 * that keeps the table sixteen bytes and not thirty-two.
18 */
19#ifndef MMGR_ASCII_PERSONA_BITORUM_H
20#define MMGR_ASCII_PERSONA_BITORUM_H
21
22#include "mmgr.h"
23
24EMBED_BEGIN_DECLS
25
26/**
27 * @brief Sixteen bytes holding one bit for each of the code points 0 to 127.
28 *
29 * @note A code point is located by shift and mask and not by search. Code point n is bit
30 * (n & 7) of bits[n >> 3]. Any class answers in the same three operations.
31 */
32typedef struct
33{
34 uint8_t bits[16]; /**< The bitmap, with code point 0 at the low bit of bits[0]. */
36
37/**
38 * @brief Asserts an MmgrAsciiMask is exactly sixteen bytes.
39 *
40 * @note mmgr_ascii_in reads bits[byte >> 3] for every byte below 0x80. All sixteen have to be
41 * there.
42 * @note Sixteen reach code point 127 and no further, as leaves a byte of 0x80 or above in
43 * no class at all.
44 */
45EMBED_STATIC_ASSERT(sizeof(MmgrAsciiMask) == 16u, "an ASCII class mask is exactly 128 bits");
46
47/**
48 * @brief Character class selector, numbered from 0.
49 *
50 * @note MMGR_ASCII_CLASSES is the enumerator count and not a class. Passing it is out of range.
51 */
52typedef enum
53{
54 MMGR_ASCII_NUM = 0, /**< '0' to '9'. */
55 MMGR_ASCII_ALPHA, /**< 'A' to 'Z' and 'a' to 'z'. */
56 MMGR_ASCII_ALNUM, /**< '0' to '9', 'A' to 'Z' and 'a' to 'z'. */
57 MMGR_ASCII_UPPER, /**< 'A' to 'Z'. */
58 MMGR_ASCII_LOWER, /**< 'a' to 'z'. */
59 MMGR_ASCII_HEX, /**< '0' to '9', 'A' to 'F' and 'a' to 'f'. */
60 MMGR_ASCII_PUNCT, /**< '!' to '/', ':' to '@', '[' to '`' and '{' to '~'. */
61 MMGR_ASCII_SPACE, /**< 9 to 13, and 32. */
62 MMGR_ASCII_CTRL, /**< 0 to 31, and 127. */
63 MMGR_ASCII_PRINT, /**< 32 to 126. */
64 MMGR_ASCII_CLASSES /**< Enumerator count and not a class. */
66
67/**
68 * @brief Arguments to mmgr_ascii_in: the class and the byte to test.
69 */
70typedef struct
71{
72 const MmgrAsciiClass kind; /**< Class to test against, below MMGR_ASCII_CLASSES. */
73 const uint8_t byte; /**< Code point to look up; 0x80 and above are in no class. */
74} AsciiCfg;
75
76/**
77 * @brief Type of the ascii dispatch table.
78 *
79 * @note EMBED_TABLE_LAYOUT asserts the in member is at offset 0 and that the struct holds nothing else.
80 */
81typedef struct
82{
83 embed_bool (*in)(const AsciiCfg *args); /**< Whether a byte belongs to a class. */
85EMBED_TABLE_LAYOUT(AsciiPersonaBitorumNs, in);
86
87/**
88 * @brief Returns whether args->byte has its bit set in the kind bitmap.
89 *
90 * @param[in] args Class and byte to test [BORROWS].
91 * @return EMBED_TRUE when the bit is set, EMBED_FALSE otherwise.
92 * @note Bytes 0x80 and above return EMBED_FALSE.
93 * @warning args->kind must be below MMGR_ASCII_CLASSES, and nothing holds it there outside a
94 * MMGR_DEBUG_CHECKS build: the bitmap is indexed by it. A byte under 0x80 then reads
95 * past the table.
96 */
97embed_bool mmgr_ascii_in(const AsciiCfg *args);
98
99/**
100 * @brief Dispatch table instance named ascii, whose in member is set to mmgr_ascii_in.
101 */
102EMBED_TABLE_STORAGE AsciiPersonaBitorumNs ascii EMBED_UNUSED = {
103 .in = mmgr_ascii_in,
104};
105
106EMBED_END_DECLS
107
108#endif
static const AsciiPersonaBitorumNs ascii
Dispatch table instance named ascii, whose in member is set to mmgr_ascii_in.
embed_bool mmgr_ascii_in(const AsciiCfg *args)
Returns whether args->byte has its bit set in the kind bitmap.
MmgrAsciiClass
Asserts an MmgrAsciiMask is exactly sixteen bytes.
@ MMGR_ASCII_ALNUM
@ MMGR_ASCII_ALPHA
@ MMGR_ASCII_PRINT
@ MMGR_ASCII_NUM
@ MMGR_ASCII_HEX
@ MMGR_ASCII_UPPER
@ MMGR_ASCII_CLASSES
@ MMGR_ASCII_LOWER
@ MMGR_ASCII_CTRL
@ MMGR_ASCII_PUNCT
The embedded_types headers every module builds on.
Arguments to mmgr_ascii_in: the class and the byte to test.
const uint8_t byte
const MmgrAsciiClass kind
Type of the ascii dispatch table.
embed_bool(*) in(const AsciiCfg *args)
Sixteen bytes holding one bit for each of the code points 0 to 127.