Repository navigation
Fix localized Boolean names - #5037
vera-rubin wants to merge 4 commits into
Conversation
Indent the preprocessor directives nested in #ifndef FMT_HEADER_ONLY.
| template <typename Char> | ||
| inline auto bool_name(bool value, locale_ref loc) -> std::basic_string<Char> { | ||
| auto name = bool_name_impl<char>(value, loc); | ||
| return {name.begin(), name.end()}; |
There was a problem hiding this comment.
This range constructor widens individual bytes of the narrow locale name rather than characters. With a std::numpunct<char> whose do_truename() returns "s\xc3\xad" (UTF-8 for “sí”), fmt::format(loc, U"{:L}", true) on this head produces 0x73, 0xffffffc3, 0xffffffad with -fsigned-char. The latter two are outside the Unicode range. With -funsigned-char it produces 0x73, 0xc3, 0xad rather than 0x73, 0xed. The UTF-16 overload also widens the two bytes separately.
I verified this in header-only GCC 8.5/C++17 builds against the merge base and head. The base emits ASCII true for these calls; the head correctly formats narrow and wchar_t names, and an ASCII no works for all four character types. If this change is intended only for char and wchar_t, keeping the old ASCII Boolean path for other character types avoids introducing invalid output; otherwise the conversion needs a defined encoding rather than byte widening.
Codex-assisted review.
Character types other than char and wchar_t only get the narrow locale's names when they are ASCII and keep "true"/"false" otherwise.
|
Thanks @vibhor-aggr, good catch. Other character types now take the narrow names only when they are ASCII and keep |
| std::string do_truename() const override { return "s\xc3\xad"; } | ||
| std::string do_falsename() const override { return "no"; } | ||
| }; |
There was a problem hiding this comment.
The revised fallback addresses my earlier encoding concern. On a347d929 I checked both signed- and unsigned-char GCC builds: narrow and wide output preserved “sí”; UTF-16 and UTF-32 returned true for the non-ASCII name and still localized the ASCII false name to no.
There is a separate build failure before the tests run: macOS and Linux Clang 14/libc++ both stop here with -Werror,-Wweak-vtables. This concrete facet defines every virtual method inline. Giving one override an out-of-line definition follows the existing test facet without changing the production code or weakening the warning. I did not rerun Clang locally.
| std::string do_truename() const override { return "s\xc3\xad"; } | |
| std::string do_falsename() const override { return "no"; } | |
| }; | |
| std::string do_truename() const override; | |
| std::string do_falsename() const override { return "no"; } | |
| }; | |
| std::string non_ascii_bool_names::do_truename() const { return "s\xc3\xad"; } |
Define one virtual method of the test facet out of line.
|
Thanks, that was from my new test facet. I've applied your suggestion and moved |
fmt::format(loc, "{:L} {:Ls}", true, false)returns"true false"even when the locale'sstd::numpunctfacet supplies different names. With theLoption, the textual representation ofboolshould come fromnumpunct::truename()andnumpunct::falsename()(format.string.std), which is whatstd::formatdoes.The
boolwriter now takes the names from the locale whenLis given and writes them as a padded string, for bothcharandwchar_t. The lookup lives next tothousands_sep_impl/decimal_point_impland is instantiated insrc/format.ccthe same way. Unlocalized output, the integer presentations ({:Ld}) andFMT_USE_LOCALE=0builds are unchanged.The new
locale_test.format_boolinxchar-test.cccovers{:L},{:Ls}, padding, the unlocalized and{:Ld}cases, the classic locale, and wide strings. It fails without the change (6 assertions showtrue/false) and passes with it. The full test suite passes (C++20 Release; xchar-test and format-test also at C++11).