fix edge case with trailing parameter serialization

This commit is contained in:
tali 2024-01-09 20:10:12 -05:00
parent 9b144cb712
commit 347d19df59
1 changed files with 11 additions and 5 deletions

View File

@ -30,9 +30,12 @@ type t = {
trailing : bool;
} [@@deriving show { with_path = false }]
let is_param_trailing p =
String.starts_with p ~prefix:":" || String.contains p ' '
let rec is_params_trailing = function
| [] -> false
| [tr] -> String.contains tr ' '
| [p] -> is_param_trailing p
| _ :: tl -> is_params_trailing tl
let make ?(prefix = No_prefix) ?(always_trailing = false) command params =
@ -48,12 +51,12 @@ let write buf t =
Buffer.add_string buf t.command;
let rec add_params = function
| [] -> ()
| [tr] when t.trailing ->
| [p] when t.trailing ->
Buffer.add_string buf " :";
Buffer.add_string buf tr
| hd::tl ->
Buffer.add_string buf p
| p::tl ->
Buffer.add_char buf ' ';
Buffer.add_string buf hd;
Buffer.add_string buf p;
add_params tl
in
add_params t.params;
@ -168,6 +171,9 @@ let%expect_test _ =
make "NICK" ["tali"] |> print_msg_nl;
[%expect {| "NICK tali\r\n" |}];
make "NICK" [":)"] |> print_msg_nl;
[%expect {| "NICK ::)\r\n" |}];
make "USER" ["milo"; "0"; "*"; "milo"] ~always_trailing:true |> print_msg_nl;
[%expect {| "USER milo 0 * :milo\r\n" |}];