Files
meowc/lib/ident.ml
T
milner 14377f6794 feat(ident): normalise names into C macro identifiers
Uppercases, replaces non-alphanumerics with underscores, and derives the
HAVE_X and SIZEOF_X forms the generated header uses.
2022-05-20 03:45:44 +00:00

19 lines
620 B
OCaml

let cident s =
let b = Buffer.create (String.length s + 4) in
String.iter
(fun c ->
match c with
| 'a' .. 'z' | 'A' .. 'Z' | '0' .. '9' -> Buffer.add_char b (Char.uppercase_ascii c)
| '*' -> Buffer.add_string b "_P"
| _ -> Buffer.add_char b '_')
s;
let raw = Buffer.contents b in
let parts = String.split_on_char '_' raw |> List.filter (fun p -> p <> "") in
String.concat "_" parts
let lower s = String.lowercase_ascii (cident s)
let have s = "HAVE_" ^ cident s
let have_var s = "have_" ^ lower s
let sizeof_name s = "SIZEOF_" ^ cident s
let sizeof_var s = "sizeof_" ^ lower s