1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
//! Lua ctype — character-classification table and predicates.
//!
//! Ported from `reference/lua-5.4.7/src/lctype.c` and `lctype.h`.
//!
//! Lua ships its own ctype replacements, optimised for its specific needs.
//! These do **not** match the standard C `<ctype.h>` semantics exactly; in
//! particular `lislalpha` / `lislalnum` treat `'_'` as alphabetic, and the
//! table is seeded for ASCII byte ranges only (with high bytes left at 0x00
//! unless `LUA_UCID` is enabled — see PORT NOTE below).
//!
//! On ASCII targets (`LUA_USE_CTYPE=0`, the default) the implementation is a
//! 257-entry byte lookup table. Each entry is a bitfield:
//!
//! | bit | name | meaning |
//! |-----|-----------|----------------------------------------------|
//! | 0 | ALPHABIT | Lua-alphabetic: ASCII letters plus `_` |
//! | 1 | DIGITBIT | decimal digit `0`-`9` |
//! | 2 | PRINTBIT | printable (graph + space) |
//! | 3 | SPACEBIT | whitespace (ASCII space, TAB, LF, VT, FF, CR)|
//! | 4 | XDIGITBIT | hex digit `0`-`9`, `A`-`F`, `a`-`f` |
//!
//! `test_prop(c, mask)` indexes the table as `CTYPE_TABLE[(c + 1) as usize]`,
//! which allows `c = -1` (the `EOZ` end-of-stream sentinel) without underflow.
//!
//! PORT NOTE: The C code supports a compile-time `LUA_UCID` flag that sets all
//! non-ASCII bytes (0x80-0xFF, minus invalid UTF-8 sequences) to `ALPHABIT`
//! so that Unicode identifiers are recognised. That path (`NONA = 0x01`) is
//! not translated here; only the default `NONA = 0x00` path is ported.
//! Enable it in Phase B by introducing a Cargo feature flag.
// C: #define ALPHABIT 0
const ALPHABIT: u32 = 0;
// C: #define DIGITBIT 1
const DIGITBIT: u32 = 1;
// C: #define PRINTBIT 2
const PRINTBIT: u32 = 2;
// C: #define SPACEBIT 3
const SPACEBIT: u32 = 3;
// C: #define XDIGITBIT 4
const XDIGITBIT: u32 = 4;
// C: #define MASK(B) (1 << (B))
// Inlined at each call site below as `1u8 << BIT`.
// C: #define NONA 0x00 /* non-ASCII bytes are not alphabetic by default */
// LUA_UCID disabled — all non-ASCII bytes remain 0x00.
// C: LUAI_DDEF const lu_byte luai_ctype_[UCHAR_MAX + 2] = { ... };
//
// UCHAR_MAX + 2 = 255 + 2 = 257 entries.
// Entry 0 → EOZ sentinel (c = -1; index = -1 + 1 = 0).
// Entries 1-256 → bytes 0x00-0xFF.
//
// Bit-flag legend (combined values seen in the table):
// 0x00 = no property (NUL, control chars, DEL, high bytes)
// 0x04 = PRINTBIT only (punctuation, symbols)
// 0x05 = ALPHABIT | PRINTBIT (non-hex letters + '_')
// 0x06 = DIGITBIT | PRINTBIT (this value does not appear alone; digits always have XDIGITBIT)
// 0x08 = SPACEBIT (TAB through CR)
// 0x0c = SPACEBIT | PRINTBIT (ASCII space 0x20)
// 0x15 = ALPHABIT | PRINTBIT | XDIGITBIT (A-F, a-f)
// 0x16 = DIGITBIT | PRINTBIT | XDIGITBIT (0-9)
pub static CTYPE_TABLE: = ;
// C: #define testprop(c,p) (luai_ctype_[(c)+1] & (p))
//
// `c` is an `i32` in Lua's internal representation: it is either a byte value
// 0-255, or -1 for EOZ. Adding 1 shifts the range to 0-256, all valid indices
// into the 257-element table.
// C: #define lislalpha(c) testprop(c, MASK(ALPHABIT))
//
// True for ASCII letters A-Z, a-z, and the underscore '_'.
// Includes non-ASCII bytes if LUA_UCID is enabled (not translated here).
pub
// C: #define lislalnum(c) testprop(c, (MASK(ALPHABIT) | MASK(DIGITBIT)))
//
// True for ASCII letters, digits, and '_'.
pub
// C: #define lisdigit(c) testprop(c, MASK(DIGITBIT))
//
// True for ASCII decimal digits '0'-'9'.
pub
// C: #define lisspace(c) testprop(c, MASK(SPACEBIT))
//
// True for ASCII whitespace: space (0x20), TAB (0x09), LF (0x0A),
// VT (0x0B), FF (0x0C), CR (0x0D).
pub
// C: #define lisprint(c) testprop(c, MASK(PRINTBIT))
//
// True for printable characters: ASCII space through '~' (0x20-0x7E).
pub
// C: #define lisxdigit(c) testprop(c, MASK(XDIGITBIT))
//
// True for hexadecimal digits: '0'-'9', 'A'-'F', 'a'-'f'.
pub
// C: #define ltolower(c) \
// check_exp(('A' <= (c) && (c) <= 'Z') || (c) == ((c) | ('A' ^ 'a')), \
// (c) | ('A' ^ 'a'))
//
// Converts an uppercase ASCII letter to its lowercase equivalent by setting
// bit 5 (0x20). Only safe to call on uppercase letters A-Z, or on characters
// that already have bit 5 set (lowercase letters, '.', etc.).
//
// From macros.tsv: `check_exp(c, e)` → `{ debug_assert!(c); e }`.
// `'A' ^ 'a'` = 65 ^ 97 = 32 = 0x20.
pub
// ──────────────────────────────────────────────────────────────────────────
// PORT STATUS
// source: src/lctype.c (64 lines, 0 functions — only a table + header macros)
// target_crate: lua-vm
// confidence: high
// todos: 0
// port_notes: 1
// unsafe_blocks: 0 (must be 0 outside explicit unsafe-budget crates)
// notes: Straightforward table + inline predicates; LUA_UCID path
// omitted (PORT NOTE in module doc). Phase B: add Cargo
// feature `lua-ucid` that substitutes NONA=0x01 for the
// non-ASCII rows.
// ──────────────────────────────────────────────────────────────────────────