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

Fix formatting std::chrono::duration types to wide strings #1533

Merged
merged 13 commits into from
Jan 24, 2020
Merged

Fix formatting std::chrono::duration types to wide strings #1533

merged 13 commits into from
Jan 24, 2020

Conversation

temghost03ajfksdf
Copy link
Contributor

I was getting several compiler errors when trying to format a std::chrono::duration to a wide string, and found that various parts of chrono.h had a hard dependency on narrow chars. I tried to make it agnostic as possible, and it seems to work fine for me now, although I haven't tested it with anything besides std::chrono::duration<double, std::milli>.

If anything needs to be changed or fixed to get this merged just let me know.

I agree that my contributions are licensed under the {fmt} license, and agree to future changes to the licensing.

@vitaut
Copy link
Contributor

vitaut commented Jan 23, 2020

Thanks for the PR! Looks good but could you add a test case checking that format compiles when passing a wide format string an a chrono object?

@temghost03ajfksdf
Copy link
Contributor Author

I have committed a wide version of the FormatDefault case, will that work?

@temghost03ajfksdf
Copy link
Contributor Author

temghost03ajfksdf commented Jan 23, 2020

Hmm, seems to be failing with the 42µs case. I might need to look at this more.

  Actual: L"42\xFFC2\xFFB5s"
Expected: L"42µs"
Which is: L"42\xC2\xB5s"

Edit: Yep, it looks like I missed a couple things with format_duration_unit/get_units. I'll let you know when it is fixed.

I think might be a source file encoding issue, so I used \u00B5 instead.
@temghost03ajfksdf
Copy link
Contributor Author

After some trial and error, I believe the µ issue was from the encoding of the source file itself, so I specialized all of the get_units for wchar_t and in the wide version I used \u00B5 instead of the µ symbol, to make sure it is encoded correctly.

I also added Char template overloads everywhere necessary to make sure everything is being done with wide characters. I think this should pass all the tests.

include/fmt/chrono.h Outdated Show resolved Hide resolved
include/fmt/chrono.h Outdated Show resolved Hide resolved
include/fmt/chrono.h Outdated Show resolved Hide resolved
if (isnan(val)) return write_nan();
auto locale = context.locale().template get<std::locale>();
auto& facet = std::use_facet<std::time_put<char_type>>(locale);
std::basic_ostringstream<char_type> os;
os.imbue(locale);
facet.put(os, os, ' ', &time, format, format + std::strlen(format));
facet.put(os, os, ' ', &time, format, modifier);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea.

Comment on lines +767 to +768
if (const char* unit = get_units<Period>()) {
string_view s(unit);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest making unit a string_view and removing s.

Copy link
Contributor Author

@temghost03ajfksdf temghost03ajfksdf Jan 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll need to rework some things to do this properly since get_units can return a nullptr, which fmt's string_view doesn't handle properly as far as I know. Since it passes the pointer to std::char_traits<T>::length() which will dereference it without checking.

So it either needs to stay as-is, or I could revise get_units to return string_views, and specifically construct a null-data 0-length string_view from there, which wouldn't invoke the length call.

Personally I think it would be cleaner to leave this as-is.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's leave as is this then.

include/fmt/chrono.h Outdated Show resolved Hide resolved
include/fmt/chrono.h Outdated Show resolved Hide resolved
@vitaut vitaut merged commit 1acb73f into fmtlib:master Jan 24, 2020
@vitaut
Copy link
Contributor

vitaut commented Jan 24, 2020

Merged, thanks!

@temghost03ajfksdf temghost03ajfksdf deleted the zeffy-patch-1 branch January 24, 2020 03:04
sthagen added a commit to sthagen/fmtlib-fmt that referenced this pull request Jan 24, 2020
Fix formatting std::chrono::duration types to wide strings (fmtlib#1533)
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

Successfully merging this pull request may close these issues.

2 participants