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

DiscIO: Make use of fmt where applicable #9150

Merged
merged 3 commits into from Oct 19, 2020

Conversation

JosJuice
Copy link
Member

Once nice benefit of fmt is that we can use positional arguments in localizable strings. This a feature which has been requested for the Korean translation of strings like "Errors were found in %zu blocks in the %s partition." and which will no doubt be useful for other languages too.

Copy link
Member

@BhaaLseN BhaaLseN left a comment

Choose a reason for hiding this comment

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

Code seems good, and you can probably ignore that debug string remark (untested)

return fmt::format(
"{:12.12} {:#04x} {:#04x} {:#06x} {:#06x} {:#010x} {:#06x} {:#06x} {:#06x} {:#010x}",
Copy link
Member

Choose a reason for hiding this comment

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

This change in width seems a bit weird, at least I can't see any indication in the docs that the width used for the alternate form includes the prefix.

Then again, it probably doesn't matter too much since this is a debug string.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've tested this code, and it works correctly if I write it like this.

For what it's worth, https://fmt.dev/latest/syntax.html contains the following example (but other than that I indeed can't find anything relevant in the documentation):

fmt::format("{:#04x}", 0);
// Result: "0x00"

StringFromFormat(Common::GetStringT("The %s partition is not properly aligned.").c_str(),
name.c_str()));
AddProblem(Severity::Medium,
fmt::format(Common::GetStringT("The {0} partition is not properly aligned."), name));
Copy link
Member

@lioncash lioncash Oct 14, 2020

Choose a reason for hiding this comment

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

Given this is used quite a bit, maybe this should be its own helper function in common, like:

template <typename... Args>
std::string GetStringTFmt(const char* string, Args&&... args)
{
    return fmt::format(GetStringT(string), std::forward<Args>(args)...);
}

So that fmt::format doesn't need to be typed all the time in conjunction with another function immediately one after another in several spots.

Copy link
Member Author

Choose a reason for hiding this comment

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

Added. I wasn't sure whether it would fit best in StringUtil.h (because it deals with string formatting) or MsgHandler.h (because it deals with translation), but I put it in StringUtil.h.

Copy link
Member Author

Choose a reason for hiding this comment

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

I moved it to MsgHandler.h because I figured we will need to include fmt/format.h in that header anyway if we migrate panic alerts over to using fmt.

Copy link
Member

Choose a reason for hiding this comment

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

Sounds good!

@JosJuice
Copy link
Member Author

Does anyone understand why the CMake build fails to find the fmt/format.h header now? Common does have fmt::fmt in its target_link_libraries. (I had the same problem before when I used fmt::format from DiscIO without adding fmt::fmt to DiscIO's target_link_libraries.)

@BhaaLseN
Copy link
Member

BhaaLseN commented Oct 14, 2020

Those errors come from AudioCommon, maybe because it is a template and the compiler inlines it or something like that? I guess it simply complains due to that indirect include?

@JosJuice
Copy link
Member Author

(19:47:39) leoetlino: looks like AudioCommon does not have Common as a dependency

Fixing that seems to have made it work.

@JosJuice
Copy link
Member Author

Okay, I don't understand what's wrong with macOS now...

@leoetlino
Copy link
Member

The issue is that fmt checks whether __EXCEPTIONS is defined to determine whether C++ exceptions are enabled, but that macro is still defined when C++ exceptions are disabled and Obj-C exceptions are enabled. Have you tried compiling with -fno-objc-exceptions?

Once nice benefit of fmt is that we can use positional arguments
in localizable strings. This a feature which has been
requested for the Korean translation of strings like
"Errors were found in %zu blocks in the %s partition."
and which will no doubt be useful for other languages too.
@lioncash lioncash merged commit 0f5bf90 into dolphin-emu:master Oct 19, 2020
10 checks passed
@JosJuice JosJuice deleted the discio-fmt branch October 19, 2020 13:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4 participants