Improve handling of thousands separator

This commit is contained in:
Victor Zverovich 2021-06-03 20:29:42 -07:00
parent 024741b476
commit 2ac0bfe59e
2 changed files with 6 additions and 5 deletions

View File

@ -106,7 +106,9 @@ template <typename Locale> Locale locale_ref::get() const {
template <typename Char> template <typename Char>
FMT_FUNC auto thousands_sep_impl(locale_ref loc) -> thousands_sep_result<Char> { FMT_FUNC auto thousands_sep_impl(locale_ref loc) -> thousands_sep_result<Char> {
auto& facet = std::use_facet<std::numpunct<Char>>(loc.get<std::locale>()); auto& facet = std::use_facet<std::numpunct<Char>>(loc.get<std::locale>());
return {facet.grouping(), facet.thousands_sep()}; auto grouping = facet.grouping();
auto thousands_sep = grouping.empty() ? Char() : facet.thousands_sep();
return {std::move(grouping), thousands_sep};
} }
template <typename Char> FMT_FUNC Char decimal_point_impl(locale_ref loc) { template <typename Char> FMT_FUNC Char decimal_point_impl(locale_ref loc) {
return std::use_facet<std::numpunct<Char>>(loc.get<std::locale>()) return std::use_facet<std::numpunct<Char>>(loc.get<std::locale>())

View File

@ -1418,11 +1418,10 @@ auto write_int_localized(OutputIt& out, UInt value, unsigned prefix,
static_assert(std::is_same<uint64_or_128_t<UInt>, UInt>::value, ""); static_assert(std::is_same<uint64_or_128_t<UInt>, UInt>::value, "");
const auto sep_size = 1; const auto sep_size = 1;
auto ts = thousands_sep<Char>(loc); auto ts = thousands_sep<Char>(loc);
const std::string& groups = ts.grouping; if (!ts.thousands_sep) return false;
Char sep = ts.thousands_sep;
if (!sep || groups.empty()) return false;
int num_digits = count_digits(value); int num_digits = count_digits(value);
int size = num_digits, n = num_digits; int size = num_digits, n = num_digits;
const std::string& groups = ts.grouping;
std::string::const_iterator group = groups.cbegin(); std::string::const_iterator group = groups.cbegin();
while (group != groups.cend() && n > *group && *group > 0 && while (group != groups.cend() && n > *group && *group > 0 &&
*group != max_value<char>()) { *group != max_value<char>()) {
@ -1437,7 +1436,7 @@ auto write_int_localized(OutputIt& out, UInt value, unsigned prefix,
if (prefix != 0) ++size; if (prefix != 0) ++size;
const auto usize = to_unsigned(size); const auto usize = to_unsigned(size);
buffer.resize(usize); buffer.resize(usize);
basic_string_view<Char> s(&sep, sep_size); basic_string_view<Char> s(&ts.thousands_sep, sep_size);
// Index of a decimal digit with the least significant digit having index 0. // Index of a decimal digit with the least significant digit having index 0.
int digit_index = 0; int digit_index = 0;
group = groups.cbegin(); group = groups.cbegin();