-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Request: Custom argument formatting for PrintfFormatter #335
Comments
Thanks for the suggestion. Adding custom argument formatter support to |
Okay, so I hacked out a proof of concept extension that seems to have the desired behavior. I was a little out of my comfort zone here, which means it was rewarding work, but I'd appreciate a review and I'd like some feedback from you regarding what you'd like to see before pulling. Here are some of my thoughts:
|
Yes, although I haven't tested your changes they do look right.
I'm following Google C++ Style Guide with two exceptions: (1) exceptions are allowed, (2) function names are lowercase.
Makes sense. I'd only rename
Do you mean printf and Python-like formatting APIs? While I agree that its worth doing, I suggest addressing in a separate PR to keep changes manageable.
I'm not sure it's necessary. Users will probably need to subclass
Any suggestions to improve naming consistency are welcome. |
And thanks for working on this! |
I renamed printf_formatter.h as suggested. Regarding point 5, I have to apologize if I'm being a little dense, but I'm not sure I follow you: To customize the BasicFormatter, I create a custom formatter by subclassing the BasicArgFormatter For the PrintfArgFormatter, I modified ArgFormatterBase, as it seemed to fulfill the same role the PrintfArtfFormatter as the BasicArgFormatter does for BasicFormatter. This allowed me to make an apparently successful test by defining the following custom formatter (you can see the full test in tests/custom-formatter.cc): // A custom argument formatter that doesn't print `-` for floating-point values
// rounded to 0.
class CustomPAF : public fmt::internal::ArgFormatterBase<CustomPAF, char>
{
public:
CustomPAF(fmt::BasicWriter<char> &w, fmt::FormatSpec &s):
fmt::internal::ArgFormatterBase<CustomPAF, char>(w, s) {}
void visit_double(double value) {
if (round(value * pow(10, spec().precision())) == 0)
value = 0;
fmt::internal::ArgFormatterBase<CustomPAF, char>::visit_double(value);
}
}; Which seems to work at least. Should I have been modifying PrintfArgFormatter instead/additionally? |
Printf argument formatting is implemented in
Hope it makes sense. |
Thanks, your notes helped a great deal. I figured out my error, and made the necessary changes. I'll push a commit tomorrow. |
Great! Looking forward for a pull request =). |
Sorry about the pollution with the pull requests. Is there a way to execute the tests without making a pull request? Esp with the windows build I don't have a way to test locally. |
I don't mind if you use a work-in-progress PR to check portability. I'm doing the same with branches as I don't have access to a Windows machine. BTW you can change a PR in place by modifying the original branch. Another option is to add your clone of the project to Travis/Appveyor. |
|
As discussed in issue 331
I would find it very helpful to be able to specify custom argument formatting for the printf interface.
Please let me know if I can help in any way.
The text was updated successfully, but these errors were encountered: