Skip to content

Commit

Permalink
Mostly graphical improvements to the explain plugin (#733)
Browse files Browse the repository at this point in the history
  • Loading branch information
AltGr authored Oct 31, 2024
2 parents 1944b86 + 57f85ff commit 4dc049f
Show file tree
Hide file tree
Showing 8 changed files with 252 additions and 71 deletions.
11 changes: 9 additions & 2 deletions compiler/catala_utils/string.ml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
include Stdlib.String

let to_ascii : string -> string = Ubase.from_utf8

let to_id s =
to_ascii s
|> map (function
| ('a' .. 'z' | 'A' .. 'Z' | '0' .. '9') as c -> c
| _ -> '_')

let is_uppercase_ascii = function 'A' .. 'Z' -> true | _ -> false

let begins_with_uppercase (s : string) : bool =
Expand All @@ -29,7 +36,7 @@ let begins_with_uppercase (s : string) : bool =
let to_snake_case (s : string) : string =
let out = Buffer.create (2 * length s) in
s
|> to_ascii
|> to_id
|> iteri (fun i c ->
if is_uppercase_ascii c && 0 <> i && get s (i - 1) <> '_' then
Buffer.add_char out '_';
Expand All @@ -40,7 +47,7 @@ let to_camel_case (s : string) : string =
let last_was_underscore = ref true in
let out = Buffer.create (length s) in
s
|> to_ascii
|> to_id
|> iter (function
| '_' -> last_was_underscore := true
| c ->
Expand Down
4 changes: 4 additions & 0 deletions compiler/catala_utils/string.mli
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ val to_ascii : string -> string
(** Removes all non-ASCII diacritics from a string by converting them to their
base letter in the Latin alphabet. *)

val to_id : string -> string
(** Like [to_ascii], but in addition replaces any non-alphanumeric character by
[_] *)

val is_uppercase_ascii : char -> bool
(** [is_uppercase c] returns if [c] is in the set ['A'...'Z']. *)

Expand Down
2 changes: 1 addition & 1 deletion compiler/plugins/api_web.ml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module To_jsoo = struct
other modules: here everything is flattened in the current namespace *)
let format_struct_name ppf name =
StructName.to_string name
|> String.to_ascii
|> String.to_id
|> String.uncapitalize_ascii
|> String.map (function '.' -> '_' | c -> c)
|> Format.pp_print_string ppf
Expand Down
Loading

0 comments on commit 4dc049f

Please sign in to comment.