Decouple locale and buffer

This commit is contained in:
Victor Zverovich 2017-12-26 17:22:07 -08:00
parent 3663414053
commit d165d9c483
3 changed files with 18 additions and 21 deletions

View File

@ -220,9 +220,9 @@ void report_error(FormatFunc func, int error_code,
} // namespace
template <typename Char>
FMT_FUNC Char internal::thousands_sep(const basic_buffer<Char>& buf) {
return std::use_facet<std::numpunct<Char>>(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<std::numpunct<Char>>(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<void>;
// Explicit instantiations for char.
template locale basic_buffer<char>::locale() const;
template char internal::thousands_sep(const basic_buffer<char>& buf);
template char internal::thousands_sep(locale_provider *lp);
template void basic_fixed_buffer<char>::grow(std::size_t);
@ -465,9 +465,7 @@ template int internal::char_traits<char>::format_float(
// Explicit instantiations for wchar_t.
template locale basic_buffer<wchar_t>::locale() const;
template wchar_t internal::thousands_sep(const basic_buffer<wchar_t>& buf);
template wchar_t internal::thousands_sep(locale_provider *lp);
template class basic_context<wchar_t>;

View File

@ -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 <typename T>
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 <typename T>
@ -868,7 +868,7 @@ class add_thousands_sep {
};
template <typename Char>
Char thousands_sep(const basic_buffer<Char>& 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_provider> locale_;
FMT_DISALLOW_COPY_AND_ASSIGN(basic_writer);
@ -2591,7 +2592,8 @@ void basic_writer<Buffer>::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<char_type>(writer.locale_.get());
fmt::basic_string_view<char_type> sep(&thousands_sep, 1);
unsigned size = static_cast<unsigned>(
num_digits + sep.size() * ((num_digits - 1) / 3));

View File

@ -19,7 +19,4 @@ class locale {
explicit locale(std::locale loc = std::locale()) : locale_(loc) {}
std::locale get() { return locale_; }
};
template <typename T>
locale basic_buffer<T>::locale() const { return fmt::locale(); }
}