anol
anol

Reputation: 8953

Invert Format spaces and break hints

Is it possible to invert the behavior of break hints in the Format module, e.g. using standard spaces as break hints, and adding some special notation for non-breakable spaces?

The current behavior leads to situations where one might be inclined to write "Hello@ world,@ this@ is@ a@ short@ phrase", where every space is converted into a break hint to mimic the behavior as seen e.g. in text editors, HTML renderers, etc.

For instance, this Using the Format module documentation explicitly recommends using break hints:

Generally speaking, a printing routine using "format", should not directly output white spaces: the routine should use break hints instead.

This behavior not only complicates writing messages, but it also makes it very hard to grep strings in the source code.

It seems that following the established convention that "every space is a break hint, unless marked as non-breaking space" would be a better alternative.

Are there simple techniques to wrap such strings and invert their behavior, preferably that incur no excessive runtime cost and/or lead to typing issues (e.g. due to conversions between string and format)?

Upvotes: 2

Views: 84

Answers (1)

ivg
ivg

Reputation: 35210

Since 4.02 there is a Format.pp_print_text function that will take a regular text and print it substituting spaces with cuts and \n with new_lines.

Using this function you can still print using printf and other convenience functions:

printf "text '%a'" pp_print_text "hello, this a short phrase" 

In general your question is more about library design. So, it is hard to answer anything about it. It is more suited for discussion on the OCaml mailing list.

Upvotes: 3

Related Questions