From d165d9c4830059e51a2fb6766177317f12d068fe Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Tue, 26 Dec 2017 17:22:07 -0800 Subject: [PATCH] Decouple locale and buffer --- include/fmt/format.cc | 16 +++++++--------- include/fmt/format.h | 20 +++++++++++--------- include/fmt/locale.h | 3 --- 3 files changed, 18 insertions(+), 21 deletions(-) diff --git a/include/fmt/format.cc b/include/fmt/format.cc index 792ca1e4..1df50e59 100644 --- a/include/fmt/format.cc +++ b/include/fmt/format.cc @@ -220,9 +220,9 @@ void report_error(FormatFunc func, int error_code, } // namespace template -FMT_FUNC Char internal::thousands_sep(const basic_buffer& buf) { - return std::use_facet>(buf.locale().get()) - .thousands_sep(); +FMT_FUNC Char internal::thousands_sep(locale_provider *lp) { + std::locale loc = lp ? lp->locale().get() : std::locale(); + return std::use_facet>(loc).thousands_sep(); } FMT_FUNC void system_error::init( @@ -441,15 +441,15 @@ FMT_FUNC void vprint_colored(Color c, string_view format, format_args args) { std::fputs(RESET_COLOR, stdout); } +FMT_FUNC locale locale_provider::locale() { return fmt::locale(); } + #ifndef FMT_HEADER_ONLY template struct internal::basic_data; // Explicit instantiations for char. -template locale basic_buffer::locale() const; - -template char internal::thousands_sep(const basic_buffer& buf); +template char internal::thousands_sep(locale_provider *lp); template void basic_fixed_buffer::grow(std::size_t); @@ -465,9 +465,7 @@ template int internal::char_traits::format_float( // Explicit instantiations for wchar_t. -template locale basic_buffer::locale() const; - -template wchar_t internal::thousands_sep(const basic_buffer& buf); +template wchar_t internal::thousands_sep(locale_provider *lp); template class basic_context; diff --git a/include/fmt/format.h b/include/fmt/format.h index 1427dd7d..d72e933a 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -305,11 +305,13 @@ inline T *make_ptr(T *ptr, std::size_t) { return ptr; } // is very heavy. class locale; -/** - \rst - A contiguous memory buffer with an optional growing ability. - \endrst - */ +class locale_provider { + public: + virtual ~locale_provider() {} + virtual locale locale(); +}; + +/** A contiguous memory buffer with an optional growing ability. */ template class basic_buffer { private: @@ -381,8 +383,6 @@ class basic_buffer { T &operator[](std::size_t index) { return ptr_[index]; } const T &operator[](std::size_t index) const { return ptr_[index]; } - - virtual fmt::locale locale() const; }; template @@ -868,7 +868,7 @@ class add_thousands_sep { }; template -Char thousands_sep(const basic_buffer& buf); +Char thousands_sep(locale_provider *lp); // Formats a decimal unsigned integer value writing into buffer. // thousands_sep is a functor that is called after writing each char to @@ -2182,6 +2182,7 @@ class basic_writer { private: // Output buffer. Buffer &buffer_; + std::unique_ptr locale_; FMT_DISALLOW_COPY_AND_ASSIGN(basic_writer); @@ -2591,7 +2592,8 @@ void basic_writer::write_int(T value, const Spec& spec) { void on_num() { unsigned num_digits = internal::count_digits(abs_value); - char_type thousands_sep = internal::thousands_sep(writer.buffer_); + char_type thousands_sep = + internal::thousands_sep(writer.locale_.get()); fmt::basic_string_view sep(&thousands_sep, 1); unsigned size = static_cast( num_digits + sep.size() * ((num_digits - 1) / 3)); diff --git a/include/fmt/locale.h b/include/fmt/locale.h index 12da17e9..cf4e8d31 100644 --- a/include/fmt/locale.h +++ b/include/fmt/locale.h @@ -19,7 +19,4 @@ class locale { explicit locale(std::locale loc = std::locale()) : locale_(loc) {} std::locale get() { return locale_; } }; - -template -locale basic_buffer::locale() const { return fmt::locale(); } }