MMgr v2.3.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 retains 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/** @addtogroup mod_ascii
23 * @{ */
24
25#include "mmgr.h"
26
27EMBED_BEGIN_DECLS
28
29/**
30 * @brief Sixteen bytes with one bit for each of the code points 0 to 127.
31 *
32 * @note A shift and a mask locate a code point, not a search. Code point n is bit
33 * (n & 7) of bits[n >> 3]. Any class answers in the same three operations.
34 */
35typedef struct
36{
37 uint8_t bits[16]; /**< The bitmap, with code point 0 at the low bit of bits[0]. */
39
40/**
41 * @brief Asserts an MmgrAsciiMask is exactly sixteen bytes.
42 *
43 * @note mmgr_ascii_in reads bits[byte >> 3] for every byte below 0x80. All sixteen have to be
44 * there.
45 * @note Sixteen reach code point 127 and no further, as leaves a byte of 0x80 or above in
46 * no class at all.
47 */
48EMBED_STATIC_ASSERT(sizeof(MmgrAsciiMask) == 16u, "an ASCII class mask is exactly 128 bits");
49
50/**
51 * @brief Character class selector, numbered from 0.
52 *
53 * @note MMGR_ASCII_CLASSES is the enumerator count and not a class. As an argument it is out of range.
54 */
55typedef enum
56{
57 MMGR_ASCII_NUM = 0, /**< '0' to '9'. */
58 MMGR_ASCII_ALPHA, /**< 'A' to 'Z' and 'a' to 'z'. */
59 MMGR_ASCII_ALNUM, /**< '0' to '9', 'A' to 'Z' and 'a' to 'z'. */
60 MMGR_ASCII_UPPER, /**< 'A' to 'Z'. */
61 MMGR_ASCII_LOWER, /**< 'a' to 'z'. */
62 MMGR_ASCII_HEX, /**< '0' to '9', 'A' to 'F' and 'a' to 'f'. */
63 MMGR_ASCII_PUNCT, /**< '!' to '/', ':' to '@', '[' to '\`' and '{' to '~'. */
64 MMGR_ASCII_SPACE, /**< 9 to 13, and 32. */
65 MMGR_ASCII_CTRL, /**< 0 to 31, and 127. */
66 MMGR_ASCII_PRINT, /**< 32 to 126. */
67 MMGR_ASCII_CLASSES /**< Enumerator count and not a class. */
69
70/**
71 * @brief Arguments to mmgr_ascii_in: the class and the byte to test.
72 */
73typedef struct
74{
75 const MmgrAsciiClass type; /**< Class to test against, below MMGR_ASCII_CLASSES. */
76 const uint8_t byte; /**< Code point to look up; 0x80 and above are in no class. */
77} AsciiCfg;
78
79/**
80 * @brief Type of the ascii dispatch table.
81 *
82 * @note EMBED_TABLE_LAYOUT asserts the three members sit at consecutive EMBED_FUNCTION_POINTER_BYTES offsets, with
83 * nothing else.
84 * @note lower and upper read only the byte.
85 */
86typedef struct
87{
88 embed_bool (*in)(const AsciiCfg *args); /**< Whether a byte belongs to a class. */
89 uint8_t (*lower)(const AsciiCfg *args); /**< The byte with 'A' to 'Z' folded to 'a' to 'z'. */
90 uint8_t (*upper)(const AsciiCfg *args); /**< The byte with 'a' to 'z' folded to 'A' to 'Z'. */
92EMBED_TABLE_LAYOUT(AsciiPersonaBitorumNs, in, lower, upper);
93
94/**
95 * @brief Returns whether args->byte has its bit set in the type bitmap.
96 *
97 * @param[in] args Class and byte to test [BORROWS].
98 * @return EMBED_TRUE when the bit holds, EMBED_FALSE otherwise.
99 * @note Bytes 0x80 and above return EMBED_FALSE.
100 * @warning args->type must be below MMGR_ASCII_CLASSES, and nothing holds it there outside a
101 * MMGR_DEBUG_CHECKS build. It indexes the bitmap, and a byte under 0x80 then reads
102 * past the table.
103 */
104EMBED_HOST_DEVICE embed_bool mmgr_ascii_in(const AsciiCfg *args);
105
106/**
107 * @brief Returns args->byte with an upper case letter folded to its lower case, as tolower folds it.
108 *
109 * @param[in] args The byte, and args->type, which the call does not read [BORROWS].
110 * @return 'a' to 'z' for 'A' to 'Z', and any other byte as it is.
111 */
112EMBED_HOST_DEVICE uint8_t mmgr_ascii_lower(const AsciiCfg *args);
113
114/**
115 * @brief Returns args->byte with a lower case letter folded to its upper case, as toupper folds it.
116 *
117 * @param[in] args The byte, and args->type, which the call does not read [BORROWS].
118 * @return 'A' to 'Z' for 'a' to 'z', and any other byte as it is.
119 */
120EMBED_HOST_DEVICE uint8_t mmgr_ascii_upper(const AsciiCfg *args);
121
122/**
123 * @brief Dispatch table instance named ascii, with each member set to its mmgr_ascii_ function.
124 */
125EMBED_TABLE_STORAGE AsciiPersonaBitorumNs ascii EMBED_UNUSED = {
126 .in = mmgr_ascii_in,
127 .lower = mmgr_ascii_lower,
128 .upper = mmgr_ascii_upper,
129};
130
131EMBED_END_DECLS
132
133/** @} */
134
135#endif
static const AsciiPersonaBitorumNs ascii
Dispatch table instance named ascii, with each member set to its mmgr_ascii_ function.
EMBED_HOST_DEVICE embed_bool mmgr_ascii_in(const AsciiCfg *args)
Returns whether args->byte has its bit set in the type bitmap.
MmgrAsciiClass
Asserts an MmgrAsciiMask is exactly sixteen bytes.
EMBED_HOST_DEVICE uint8_t mmgr_ascii_lower(const AsciiCfg *args)
Returns args->byte with an upper case letter folded to its lower case, as tolower folds it.
EMBED_HOST_DEVICE uint8_t mmgr_ascii_upper(const AsciiCfg *args)
Returns args->byte with a lower case letter folded to its upper case, as toupper folds it.
@ MMGR_ASCII_SPACE
@ 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 MmgrAsciiClass type
const uint8_t byte
Type of the ascii dispatch table.
embed_bool(*) in(const AsciiCfg *args)
uint8_t(*) upper(const AsciiCfg *args)
uint8_t(*) lower(const AsciiCfg *args)
Sixteen bytes with one bit for each of the code points 0 to 127.