mirror of
https://github.com/fmtlib/fmt.git
synced 2024-11-14 15:01:05 +00:00
Implement grouping
This commit is contained in:
parent
1b94271ff6
commit
58a5563a9f
@ -133,12 +133,13 @@ struct localize_int {
|
||||
appender out;
|
||||
const format_specs& specs;
|
||||
std::string sep;
|
||||
std::string grouping;
|
||||
|
||||
template <typename T, FMT_ENABLE_IF(detail::is_integer<T>::value)>
|
||||
void operator()(T value) {
|
||||
auto arg = make_write_int_arg(value, specs.sign);
|
||||
write_int(out, static_cast<uint64_or_128_t<T>>(arg.abs_value), arg.prefix,
|
||||
specs, digit_grouping<char>("\3", sep));
|
||||
specs, digit_grouping<char>(grouping, sep));
|
||||
}
|
||||
template <typename T, FMT_ENABLE_IF(!detail::is_integer<T>::value)>
|
||||
void operator()(T) {}
|
||||
@ -152,7 +153,8 @@ template <>
|
||||
FMT_API FMT_FUNC void format_facet<std::locale>::do_put(
|
||||
appender out, basic_format_arg<format_context> val,
|
||||
const format_specs& specs) const {
|
||||
visit_format_arg(detail::localize_int{out, specs, separator_}, val);
|
||||
visit_format_arg(detail::localize_int{out, specs, separator_, grouping_},
|
||||
val);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
@ -33,13 +33,14 @@
|
||||
#ifndef FMT_FORMAT_H_
|
||||
#define FMT_FORMAT_H_
|
||||
|
||||
#include <cmath> // std::signbit
|
||||
#include <cstdint> // uint32_t
|
||||
#include <cstring> // std::memcpy
|
||||
#include <limits> // std::numeric_limits
|
||||
#include <memory> // std::uninitialized_copy
|
||||
#include <stdexcept> // std::runtime_error
|
||||
#include <system_error> // std::system_error
|
||||
#include <cmath> // std::signbit
|
||||
#include <cstdint> // uint32_t
|
||||
#include <cstring> // std::memcpy
|
||||
#include <initializer_list> // std::initializer_list
|
||||
#include <limits> // std::numeric_limits
|
||||
#include <memory> // std::uninitialized_copy
|
||||
#include <stdexcept> // std::runtime_error
|
||||
#include <system_error> // std::system_error
|
||||
|
||||
#ifdef __cpp_lib_bit_cast
|
||||
# include <bit> // std::bitcast
|
||||
@ -990,6 +991,7 @@ constexpr auto compile_string_to_view(detail::std_string_view<Char> s)
|
||||
template <typename Locale> class format_facet : public Locale::facet {
|
||||
private:
|
||||
std::string separator_;
|
||||
std::string grouping_;
|
||||
|
||||
protected:
|
||||
virtual void do_put(appender out, basic_format_arg<format_context> val,
|
||||
@ -998,8 +1000,9 @@ template <typename Locale> class format_facet : public Locale::facet {
|
||||
public:
|
||||
static FMT_API typename Locale::id id;
|
||||
|
||||
explicit format_facet(string_view sep = ",")
|
||||
: separator_(sep.data(), sep.size()) {}
|
||||
explicit format_facet(string_view sep = ",",
|
||||
std::initializer_list<unsigned char> g = {3})
|
||||
: separator_(sep.data(), sep.size()), grouping_(g.begin(), g.end()) {}
|
||||
|
||||
void put(appender out, basic_format_arg<format_context> val,
|
||||
const format_specs& specs) const {
|
||||
|
@ -2348,11 +2348,17 @@ TEST(format_test, format_facet) {
|
||||
TEST(format_test, format_facet_separator) {
|
||||
// U+2019 RIGHT SINGLE QUOTATION MARK is a digit separator in the de_CH
|
||||
// locale.
|
||||
auto loc = std::locale(std::locale(),
|
||||
new fmt::format_facet<std::locale>("\xe2\x80\x99"));
|
||||
auto loc =
|
||||
std::locale({}, new fmt::format_facet<std::locale>("\xe2\x80\x99"));
|
||||
EXPECT_EQ(fmt::format(loc, "{:L}", 1000),
|
||||
"1\xe2\x80\x99"
|
||||
"000");
|
||||
}
|
||||
|
||||
TEST(format_test, format_facet_grouping) {
|
||||
auto loc =
|
||||
std::locale({}, new fmt::format_facet<std::locale>(",", {1, 2, 3}));
|
||||
EXPECT_EQ(fmt::format(loc, "{:L}", 1234567890), "1,234,567,89,0");
|
||||
}
|
||||
|
||||
#endif // FMT_STATIC_THOUSANDS_SEPARATOR
|
||||
|
Loading…
Reference in New Issue
Block a user