Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Truncate marker for strings #1126

Closed
mordante opened this issue Apr 20, 2019 · 2 comments
Closed

Truncate marker for strings #1126

mordante opened this issue Apr 20, 2019 · 2 comments

Comments

@mordante
Copy link
Contributor

When using the precision for string output the string may get truncated. It would be great if the output has a marker indicating the output is truncated. I created a small proof of concept patch that provides this behaviour [1]. The patch is against 5.3.0 in a local project so the git index is bogus. It modifies the following syntax:
["." precision]
to
["." precision["," truncate]]
Where truncate is a single character. That means the following code:
fmt::print(FMT_STRING("{:.5}\n{:.5,#}\n"), "foobar", "foobar");
will output

fooba
foob#

The patch doesn't work with the { arg_id } precision syntax. I actually would like the ellipse …, but that seems related to #1109.

Is this behaviour wanted and would this be the proper approach?

[1] https://filebin.ca/4eMhuevLFfia/truncate.patch

@vitaut
Copy link
Contributor

vitaut commented Apr 21, 2019

Thanks for the suggestions. I would rather not complicate the format specifications for such a relatively narrow use case but you can implement this using the extension API without changing the core library by wrapping a string in some type:

struct trunc {
  std::string_view s;
};

struct fmt::formatter<trunc> {
  // Implement custom parsing and formatting with truncation marker.
};

fmt::print(FMT_STRING("{:.5}\n{:.5,#}\n"), "foobar", trunc("foobar"));

Truncation marker can either be part of a format string or an argument to trunc. This will also work with named arguments.

@vitaut vitaut closed this as completed Apr 21, 2019
@mordante
Copy link
Contributor Author

Thanks for suggesting an alternative way to get this done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants