From e7e1d1bb6af4fdacc7228307eda906cbc57ed3cf Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Thu, 27 Jun 2013 16:13:10 +0200 Subject: [PATCH] cosmetic changes in Bencode --- bencode.ml | 8 +++++++- bencode.mli | 3 +++ 2 files changed, 10 insertions(+), 1 deletion(-) 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