From 679534597d7cd07369a65b357e3c9415ca7fe91c Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Fri, 31 Mar 2023 22:45:24 -0400 Subject: [PATCH] fix(pp): slicing needs attention --- src/pp/containers_pp.ml | 8 ++++---- tests/core/t_pp.ml | 5 +++++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/pp/containers_pp.ml b/src/pp/containers_pp.ml index 5b8a68ed..51da3717 100644 --- a/src/pp/containers_pp.ml +++ b/src/pp/containers_pp.ml @@ -89,10 +89,10 @@ let split_text_ (str : string) : t = match String.index_from str !i '\n' with | exception Not_found -> (* last chunk *) - if !i + 1 < len then cur := !cur ^ text_sub_ str !i (len - 1 - !i); + if !i + 1 < len then cur := !cur ^ text_sub_ str !i (len - !i); i := len | j -> - cur := !cur ^ text_sub_ str !i (j - 1 - !i) ^ nl; + cur := !cur ^ text_sub_ str !i (j - !i) ^ nl; i := j + 1 done; group !cur @@ -288,8 +288,8 @@ module Pretty = struct let to_format ~width out self : unit = (* TODO: more efficient implementation based on: - open a vbox; make custom out that directly emit Format.pp_foo calls; - render to this out. *) + open a vbox; make custom out that directly emit Format.pp_foo calls; + render to this out. *) CCFormat.string_lines out (to_string ~width self) end diff --git a/tests/core/t_pp.ml b/tests/core/t_pp.ml index ca4f2f86..0769acfd 100644 --- a/tests/core/t_pp.ml +++ b/tests/core/t_pp.ml @@ -6,6 +6,11 @@ let spf = Printf.sprintf let () = eq "hello world" (Flatten.to_string @@ text "hello" ^ newline ^ text "world") +let () = + eq ~name:"split text" ~printer:(spf "%S") "let rec f x =\n x+2\n" + (let d = text "let rec f x =\n x+2\n" in + Pretty.to_string ~width:15 d) + let () = eq ~name:"l1" ~printer:(spf "%S") "[0; 1; 2; 3;\n 4; 5; 6; 7;\n 8; 9]" (let d = Dump.list (List.init 10 int) in