remove Set.of_string so we dont use String.fold_left

This commit is contained in:
tali 2024-02-02 11:36:19 -05:00
parent ebdf476a25
commit 6e4b1af984
3 changed files with 20 additions and 35 deletions

View File

@ -73,32 +73,18 @@ module Set = struct
let add elt s = union s (singleton elt)
let remove elt s = diff s (singleton elt)
let of_list l =
List.fold_left (fun s m -> add m s) empty l
let to_string s =
let bs = Bytes.create 7 in
let bit ch elt i =
if mem elt s then (Bytes.set bs i ch; i + 1)
else i
in
let n =
0 |> bit 'i' `i |> bit 'm' `m |> bit 'n' `n |> bit 'o' `o |>
bit 's' `s |> bit 't' `t |> bit 'w' `w
in
Bytes.sub_string bs 0 n
Seq.filter_map
(fun m -> if mem m s then Some (to_char m) else None)
(List.to_seq [`i; `m; `n; `o; `s; `t; `w]) |>
String.of_seq
let pp ppf s =
Format.pp_print_string ppf (to_string s)
let of_string s =
let chr = function
| 'i' -> `i | 'm' -> `m | 'n' -> `n | 'o' -> `o
| 's' -> `s | 't' -> `t | 'w' -> `w
| _ -> invalid_arg "Irc.Mode.Set.of_string"
in
String.fold_left (fun s c -> add (chr c) s) empty s
let of_list l =
List.fold_left (fun s m -> add m s) empty l
type change = {
add : t;
rem : t;
@ -281,14 +267,14 @@ let%expect_test _ =
let print_change_nl c = Format.kasprintf print_string "%a\n" Set.pp_change c in
print_set_nl Set.empty; [%expect {| [] |}];
print_set_nl Set.(of_string "i"); [%expect {| [i] |}];
print_set_nl Set.(of_string "no"); [%expect {| [no] |}];
print_set_nl Set.(of_string "sm"); [%expect {| [ms] |}];
print_set_nl Set.(of_string "wi"); [%expect {| [iw] |}];
print_bool_nl Set.(mem `i (of_string "ins")); [%expect "true"];
print_bool_nl Set.(mem `w (of_string "ins")); [%expect "false"];
print_bool_nl Set.(mem `w (of_string "wwww")); [%expect "true"];
print_bool_nl Set.(mem `t (of_string "imnosw")); [%expect "false"];
print_set_nl Set.(of_list [`i]); [%expect {| [i] |}];
print_set_nl Set.(of_list [`n;`o]); [%expect {| [no] |}];
print_set_nl Set.(of_list [`s;`m]); [%expect {| [ms] |}];
print_set_nl Set.(of_list [`w;`i]); [%expect {| [iw] |}];
print_bool_nl Set.(mem `i (of_list [`i;`n;`s])); [%expect "true"];
print_bool_nl Set.(mem `w (of_list [`i;`n;`s])); [%expect "false"];
print_bool_nl Set.(mem `w (of_list [`w;`w;`w;`w])); [%expect "true"];
print_bool_nl Set.(mem `t (of_list [`i;`m;`n;`o;`s;`w])); [%expect "false"];
let print_parse_error f =
try f () |> ignore; print_endline "()"
@ -305,7 +291,8 @@ let%expect_test _ =
print_change_nl (Parse.user_modes "-o+o"); [%expect {| +o |}];
print_parse_error (fun () -> Parse.user_modes "+I"); [%expect {| unknown mode I |}];
let m, c = Set.normalize (Set.of_string "iw") (Parse.user_modes "-w+io") in
let m = Set.of_list [`i;`w] in
let m, c = Set.normalize m (Parse.user_modes "-w+io") in
Format.printf "%a -> [%a]\n@." Set.pp_change c Set.pp m;
[%expect {| +o-w -> [io] |}];

View File

@ -45,6 +45,7 @@ module Set : sig
val empty : t
val singleton : [< elt] -> t
val of_list : [< elt] list -> t
val mem : [< elt] -> t -> bool
val add : [< elt] -> t -> t
val remove : [< elt] -> t -> t
@ -55,9 +56,6 @@ module Set : sig
val pp : Format.formatter -> t -> unit
val to_string : t -> string
val of_string : string -> t
val of_list : [< elt] list -> t
(* val to_list : t -> elt list *)
type change = {
add : t;

View File

@ -617,8 +617,8 @@ let list_whois t user =
reply t ("320", [nick; "is a cat, meow :3"]);
reply t ("379", [nick; Fmt.str "is using modes +%s"
(Mode.Set.to_string (User.mode user))]);
let mode = Mode.Set.{ add = User.mode user; rem = empty }in
reply t ("379", [nick; Fmt.str "is using modes %a" Mode.Set.pp_change mode]);
Option.iter
(fun text ->