From 6498bc6d316d66f96353960c612cf0cabb041a86 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sun, 20 Oct 2019 19:29:24 -0700 Subject: [PATCH] Simplify grisu_writer --- include/fmt/format.h | 75 ++++++++++++++++++++++---------------------- 1 file changed, 38 insertions(+), 37 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index fc980fb2..9ca0be18 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -1112,6 +1112,41 @@ It grisu_prettify(const char* digits, int size, int exp, It it, return it; } +template class grisu_writer { + private: + const char* digits_; + int num_digits_; + int exp_; + size_t size_; + gen_digits_params params_; + Char decimal_point_; + + public: + grisu_writer(const char* digits, int num_digits, int exp, + gen_digits_params params, Char decimal_point) + : digits_(digits), + num_digits_(num_digits), + exp_(exp), + params_(params), + decimal_point_(decimal_point) { + int full_exp = num_digits + exp - 1; + int precision = params.num_digits > 0 ? params.num_digits : 16; + params_.fixed |= full_exp >= -4 && full_exp < precision; + auto it = grisu_prettify(digits, num_digits, exp, counting_iterator(), + params_, '\0'); + size_ = it.count(); + } + + size_t size() const { return size_ + (params_.sign ? 1 : 0); } + size_t width() const { return size(); } + + template void operator()(It&& it) { + if (params_.sign) *it++ = static_cast(data::signs[params_.sign]); + it = + grisu_prettify(digits_, num_digits_, exp_, it, params_, decimal_point_); + } +}; + namespace grisu_options { enum { fixed = 1, grisu2 = 2, binary32 = 4 }; } @@ -1310,41 +1345,6 @@ void arg_map::init(const basic_format_args& args) { } } -template class grisu_writer { - private: - buffer& digits_; - size_t size_; - int exp_; - gen_digits_params params_; - Char decimal_point_; - - public: - grisu_writer(buffer& digits, int exp, - const internal::gen_digits_params& params, Char decimal_point) - : digits_(digits), - exp_(exp), - params_(params), - decimal_point_(decimal_point) { - int num_digits = static_cast(digits.size()); - int full_exp = num_digits + exp - 1; - int precision = params.num_digits > 0 ? params.num_digits : 16; - params_.fixed |= full_exp >= -4 && full_exp < precision; - auto it = grisu_prettify(digits.data(), num_digits, exp, - counting_iterator(), params_, '\0'); - size_ = it.count(); - } - - size_t size() const { return size_ + (params_.sign ? 1 : 0); } - size_t width() const { return size(); } - - template void operator()(It&& it) { - if (params_.sign) *it++ = static_cast(data::signs[params_.sign]); - int num_digits = static_cast(digits_.size()); - it = grisu_prettify(digits_.data(), num_digits, exp_, it, params_, - decimal_point_); - } -}; - // This template provides operations for formatting and writing data into a // character range. template class basic_writer { @@ -2840,8 +2840,9 @@ void internal::basic_writer::write_fp(T value, params.num_digits = precision; params.trailing_zeros = (precision != 0 && (handler.fixed || !specs.type)) || specs.alt; - write_padded(as, - grisu_writer(buffer, exp, params, decimal_point)); + int num_digits = static_cast(buffer.size()); + write_padded(as, grisu_writer(buffer.data(), num_digits, exp, + params, decimal_point)); } else { write_padded(as, double_writer{sign, buffer, decimal_point_pos, decimal_point});