Decouple locale and buffer
This commit is contained in:
parent
3663414053
commit
d165d9c483
@ -220,9 +220,9 @@ void report_error(FormatFunc func, int error_code,
|
|||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
template <typename Char>
|
template <typename Char>
|
||||||
FMT_FUNC Char internal::thousands_sep(const basic_buffer<Char>& buf) {
|
FMT_FUNC Char internal::thousands_sep(locale_provider *lp) {
|
||||||
return std::use_facet<std::numpunct<Char>>(buf.locale().get())
|
std::locale loc = lp ? lp->locale().get() : std::locale();
|
||||||
.thousands_sep();
|
return std::use_facet<std::numpunct<Char>>(loc).thousands_sep();
|
||||||
}
|
}
|
||||||
|
|
||||||
FMT_FUNC void system_error::init(
|
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);
|
std::fputs(RESET_COLOR, stdout);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
FMT_FUNC locale locale_provider::locale() { return fmt::locale(); }
|
||||||
|
|
||||||
#ifndef FMT_HEADER_ONLY
|
#ifndef FMT_HEADER_ONLY
|
||||||
|
|
||||||
template struct internal::basic_data<void>;
|
template struct internal::basic_data<void>;
|
||||||
|
|
||||||
// Explicit instantiations for char.
|
// Explicit instantiations for char.
|
||||||
|
|
||||||
template locale basic_buffer<char>::locale() const;
|
template char internal::thousands_sep(locale_provider *lp);
|
||||||
|
|
||||||
template char internal::thousands_sep(const basic_buffer<char>& buf);
|
|
||||||
|
|
||||||
template void basic_fixed_buffer<char>::grow(std::size_t);
|
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.
|
// Explicit instantiations for wchar_t.
|
||||||
|
|
||||||
template locale basic_buffer<wchar_t>::locale() const;
|
template wchar_t internal::thousands_sep(locale_provider *lp);
|
||||||
|
|
||||||
template wchar_t internal::thousands_sep(const basic_buffer<wchar_t>& buf);
|
|
||||||
|
|
||||||
template class basic_context<wchar_t>;
|
template class basic_context<wchar_t>;
|
||||||
|
|
||||||
|
@ -305,11 +305,13 @@ inline T *make_ptr(T *ptr, std::size_t) { return ptr; }
|
|||||||
// is very heavy.
|
// is very heavy.
|
||||||
class locale;
|
class locale;
|
||||||
|
|
||||||
/**
|
class locale_provider {
|
||||||
\rst
|
public:
|
||||||
A contiguous memory buffer with an optional growing ability.
|
virtual ~locale_provider() {}
|
||||||
\endrst
|
virtual locale locale();
|
||||||
*/
|
};
|
||||||
|
|
||||||
|
/** A contiguous memory buffer with an optional growing ability. */
|
||||||
template <typename T>
|
template <typename T>
|
||||||
class basic_buffer {
|
class basic_buffer {
|
||||||
private:
|
private:
|
||||||
@ -381,8 +383,6 @@ class basic_buffer {
|
|||||||
|
|
||||||
T &operator[](std::size_t index) { return ptr_[index]; }
|
T &operator[](std::size_t index) { return ptr_[index]; }
|
||||||
const T &operator[](std::size_t index) const { return ptr_[index]; }
|
const T &operator[](std::size_t index) const { return ptr_[index]; }
|
||||||
|
|
||||||
virtual fmt::locale locale() const;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
@ -868,7 +868,7 @@ class add_thousands_sep {
|
|||||||
};
|
};
|
||||||
|
|
||||||
template <typename Char>
|
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.
|
// Formats a decimal unsigned integer value writing into buffer.
|
||||||
// thousands_sep is a functor that is called after writing each char to
|
// thousands_sep is a functor that is called after writing each char to
|
||||||
@ -2182,6 +2182,7 @@ class basic_writer {
|
|||||||
private:
|
private:
|
||||||
// Output buffer.
|
// Output buffer.
|
||||||
Buffer &buffer_;
|
Buffer &buffer_;
|
||||||
|
std::unique_ptr<locale_provider> locale_;
|
||||||
|
|
||||||
FMT_DISALLOW_COPY_AND_ASSIGN(basic_writer);
|
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() {
|
void on_num() {
|
||||||
unsigned num_digits = internal::count_digits(abs_value);
|
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);
|
fmt::basic_string_view<char_type> sep(&thousands_sep, 1);
|
||||||
unsigned size = static_cast<unsigned>(
|
unsigned size = static_cast<unsigned>(
|
||||||
num_digits + sep.size() * ((num_digits - 1) / 3));
|
num_digits + sep.size() * ((num_digits - 1) / 3));
|
||||||
|
@ -19,7 +19,4 @@ class locale {
|
|||||||
explicit locale(std::locale loc = std::locale()) : locale_(loc) {}
|
explicit locale(std::locale loc = std::locale()) : locale_(loc) {}
|
||||||
std::locale get() { return locale_; }
|
std::locale get() { return locale_; }
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
locale basic_buffer<T>::locale() const { return fmt::locale(); }
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user