Skip to content

Commit

Permalink
oprint: Truncate strings only after 8 bytes
Browse files Browse the repository at this point in the history
When we run

    List.init 300 (Fun.const "a")

we can see that the toplevel in one case prints

    ""... (* string length 1; truncated *)

instead of just "a". The comment that the string was truncated takes at
least 36 characters, so truncating does not make much sense here.
If we are going to print the comment, it doesn't hurt much to also show
a part of the string to the user.

A byte can be printed as up to 4 characters, due to escaping.
So printing strings of length up to 8 bytes will always be shorter
without truncating.

There are still cases when truncating gives a longer text than not,
but it's unavoidable if the length of the printed prefix is a
nondecreasing function of the string length.
  • Loading branch information
wikku committed Aug 27, 2021
1 parent fb99b2b commit c644bdf
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Changes
Expand Up @@ -51,6 +51,9 @@ Working version
(Nicolás Ojeda Bär, report by Gabriel Scherer, review by Daniel Bünzli,
Gabriel Scherer and David Allsopp)

- #10565: Toplevel value printing: truncate strings only after 8 bytes.
(Wiktor Kuchta, review by Xavier Leroy)

### Manual and documentation:

- #7812, #10475: reworded the description of the behaviors of
Expand Down
1 change: 1 addition & 0 deletions typing/oprint.ml
Expand Up @@ -188,6 +188,7 @@ let print_out_value ppf tree =
| Oval_string (s, maxlen, kind) ->
begin try
let len = String.length s in
let maxlen = max maxlen 8 in (* always show a little prefix *)
let s = if len > maxlen then String.sub s 0 maxlen else s in
begin match kind with
| Ostr_bytes -> fprintf ppf "Bytes.of_string %S" s
Expand Down

0 comments on commit c644bdf

Please sign in to comment.