Skip to content

Fix localized Boolean names - #5037

Open
vera-rubin wants to merge 4 commits into
fmtlib:mainfrom
vera-rubin:fix/localized-bool
Open

vera-rubin wants to merge 4 commits into
fmtlib:mainfrom
vera-rubin:fix/localized-bool

Conversation

@vera-rubin

Copy link
Copy Markdown
Contributor

fmt::format(loc, "{:L} {:Ls}", true, false) returns "true false" even when the locale's std::numpunct facet supplies different names. With the L option, the textual representation of bool should come from numpunct::truename() and numpunct::falsename() (format.string.std), which is what std::format does.

struct yes_no : std::numpunct<char> {
  std::string do_truename() const override { return "yes"; }
  std::string do_falsename() const override { return "no"; }
};
auto loc = std::locale(std::locale::classic(), new yes_no());
fmt::print("{}\n", fmt::format(loc, "{:L} {:Ls} {:5L}|", true, false, true));
// before: "true false true |"
// after:  "yes no yes  |"

The bool writer now takes the names from the locale when L is given and writes them as a padded string, for both char and wchar_t. The lookup lives next to thousands_sep_impl/decimal_point_impl and is instantiated in src/format.cc the same way. Unlocalized output, the integer presentations ({:Ld}) and FMT_USE_LOCALE=0 builds are unchanged.

The new locale_test.format_bool in xchar-test.cc covers {:L}, {:Ls}, padding, the unlocalized and {:Ld} cases, the classic locale, and wide strings. It fails without the change (6 assertions show true/false) and passes with it. The full test suite passes (C++20 Release; xchar-test and format-test also at C++11).

@vera-rubin
vera-rubin requested a review from vitaut as a code owner November 22, 2026 01:34
@vitaut vitaut closed this Nov 29, 2026
@vitaut vitaut reopened this Nov 29, 2026
Indent the preprocessor directives nested in #ifndef FMT_HEADER_ONLY.
Comment thread include/fmt/format.h
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()};

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.
@vera-rubin

Copy link
Copy Markdown
Contributor Author

Thanks @vibhor-aggr, good catch. Other character types now take the narrow names only when they are ASCII and keep true/false otherwise, so nothing is widened byte by byte. I added a test with a UTF-8 truename for char16_t and char32_t.

Comment thread test/xchar-test.cc Outdated
Comment on lines +365 to +367
std::string do_truename() const override { return "s\xc3\xad"; }
std::string do_falsename() const override { return "no"; }
};

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

Suggested change
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.
@vera-rubin

Copy link
Copy Markdown
Contributor Author

Thanks, that was from my new test facet. I've applied your suggestion and moved do_truename out of line, so the -Wweak-vtables error should be gone on the next run.

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.

3 participants