From 79ed8366414e202b440dc6a48a18c1eea7872a1a Mon Sep 17 00:00:00 2001 From: sneeker Date: Fri, 20 May 2022 03:45:44 +0000 Subject: [PATCH] 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. --- lib/ident.ml | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 lib/ident.ml diff --git a/lib/ident.ml b/lib/ident.ml new file mode 100644 index 0000000..019e6c3 --- /dev/null +++ b/lib/ident.ml @@ -0,0 +1,18 @@ +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