Format: add pp_print_substring and pp_print_substring_as#12133
Conversation
|
This is one of these little paper cuts in the stdlib. I wish this had been added in 3.12 when I started using OCaml in earnest :). |
gasche
left a comment
There was a problem hiding this comment.
I agree that it is reasonable to expose this capability, and the implementation looks fine. Approved.
(If we wanted to implement Jules' idea of precision for %s format specifiers, we would also need something like that for efficient support in Format. The other thing we would need is a new case Acc_data_substring in the type camlinternalFormat.acc.
|
We might want at least a second opinion on the labels for the function (that were picked because they sounded good enough). |
|
Personally I tend more to use But if you want to be consistent with |
|
If I understand correctly, the recommendation is to use either |
ce370b5 to
1e5ba20
Compare
|
Keeping the information that the first integer is the first character of the substring range à la |
|
Fine with me. This PR still needs a second approval. |
| val pp_print_substring : first:int -> len:int -> formatter -> string -> unit | ||
| val print_substring : first:int -> len:int -> string -> unit | ||
| (** [pp_print_substring ~first ~len ppf s] prints the substring of [s] | ||
| that starts at index [start] and stop at index [start+len] in the current | ||
| pretty-printing box. | ||
| @since 5.1 | ||
| *) |
There was a problem hiding this comment.
The docstring does not match the labels: start <-> first.
Personally, I didn't understand from the discussion why we don't use pos for the first label as in StringLabels. It seems needlessly noisy to introduce yet another convention of how to refer to the starting offset of a substring.
There was a problem hiding this comment.
I don't think that we should be beholden to the label choice of StringLabels outside of the labelled variant of the standard library and pos is an uninformative and ambiguous label name.
But if you prefer I could use it; after all Format API is already barely usable without Fmt due to its extremely verbose naming convention.
There was a problem hiding this comment.
I don't have a strong opinion, but for me pos means "position" and I have no doubt that it means the position of the (start of the) substring, and len its len. So i am fine with either pos and first.
There was a problem hiding this comment.
Note that String attributes a particular meaning to the term position (versus index, see the String module preamble). If pos is used one should check the function works when it is equal to String.length s.
There was a problem hiding this comment.
I don't have a strong opinion on this issue, so I'm happy to let @Octachron decide. In any case, the docstring should be corrected.
| (** [pp_print_substring_as ~first ~len ppf len_as s] prints the substring of [s] | ||
| that starts at index [start] and stop at index [start+len] in the current | ||
| pretty-printing box as if it were of length [len_as]. | ||
| @since 5.1 |
1e5ba20 to
12e01b3
Compare
|
I have updated the documentation and decided to go with the more uniform |
|
@nojb , are you fine with the current state? |
Yes! The |
12e01b3 to
605a43b
Compare
While looking at
Formatimplementation, I was intrigued by the discrepancy thatFormatrequires that its low-level printing devices are able to output slice of strings, butFormatnever exposes this feature to end users.This PR proposes to fix this asymmetry by adding two functions:
pp_print_substringandpp_print_substring_asto theFormatmodule. Along the way, it updatespp_print_textto use the new functionpp_print_substringfunction.Note that this PR is mostly a request for comments, I am not sure if the functions are that useful but the implementation was very straightforward.