diff --git a/bencode.ml b/bencode.ml index f9dacd76..e8e220bf 100644 --- a/bencode.ml +++ b/bencode.ml @@ -88,7 +88,7 @@ let rec pretty fmt t = match t with | S s -> Format.fprintf fmt "@[\"%s\"@]" s | L l -> Format.fprintf fmt "@[[@,"; - List.iter (fun t' -> Format.fprintf fmt "%a@ " pretty t') l; + List.iteri (fun i t' -> (if i > 0 then Format.pp_print_char fmt ' '); pretty fmt t') l; Format.fprintf fmt "]@]"; | D d -> Format.fprintf fmt "@[{@,"; @@ -96,6 +96,12 @@ let rec pretty fmt t = match t with (fun k t' -> Format.fprintf fmt "%a -> %a@ " pretty (S k) pretty t') d; Format.fprintf fmt "}@]"; + () + +let pretty_to_str t = + let b = Buffer.create 15 in + Format.fprintf (Format.formatter_of_buffer b) "%a@?" pretty t; + Buffer.contents b (** {2 Deserialization (decoding)} *) diff --git a/bencode.mli b/bencode.mli index 5e9b64ea..fff1bf0b 100644 --- a/bencode.mli +++ b/bencode.mli @@ -52,6 +52,9 @@ val fmt : Format.formatter -> t -> unit val pretty : Format.formatter -> t -> unit (** Print the tree itself, not its encoding *) +val pretty_to_str : t -> string + (** Print the tree into a string *) + (** {2 Deserialization (decoding)} *) (** Deserialization is based on the {! decoder} type. Parsing can be