Remove redundant size argument to write_padded

This commit is contained in:
Victor Zverovich
2018-10-07 09:28:45 -07:00
parent 655ce53383
commit 2b5acad4a9

View File

@ -1461,6 +1461,7 @@ class arg_formatter_base {
char_type value; char_type value;
size_t size() const { return 1; } size_t size() const { return 1; }
size_t width() const { return 1; }
template <typename It> template <typename It>
void operator()(It &&it) const { *it++ = value; } void operator()(It &&it) const { *it++ = value; }
@ -1468,7 +1469,7 @@ class arg_formatter_base {
void write_char(char_type value) { void write_char(char_type value) {
if (specs_) if (specs_)
writer_.write_padded(1, *specs_, char_writer{value}); writer_.write_padded(*specs_, char_writer{value});
else else
writer_.write(value); writer_.write(value);
} }
@ -2460,7 +2461,7 @@ class basic_writer {
// <left-padding><value><right-padding> // <left-padding><value><right-padding>
// where <value> is written by f(it). // where <value> is written by f(it).
template <typename F> template <typename F>
void write_padded(std::size_t size, const align_spec &spec, F &&f); void write_padded(const align_spec &spec, F &&f);
template <typename F> template <typename F>
struct padded_int_writer { struct padded_int_writer {
@ -2471,6 +2472,7 @@ class basic_writer {
F f; F f;
size_t size() const { return size_; } size_t size() const { return size_; }
size_t width() const { return size_; }
template <typename It> template <typename It>
void operator()(It &&it) const { void operator()(It &&it) const {
@ -2503,7 +2505,7 @@ class basic_writer {
align_spec as = spec; align_spec as = spec;
if (spec.align() == ALIGN_DEFAULT) if (spec.align() == ALIGN_DEFAULT)
as.align_ = ALIGN_RIGHT; as.align_ = ALIGN_RIGHT;
write_padded(size, as, padded_int_writer<F>{size, prefix, fill, padding, f}); write_padded(as, padded_int_writer<F>{size, prefix, fill, padding, f});
} }
// Writes a decimal integer. // Writes a decimal integer.
@ -2669,7 +2671,10 @@ class basic_writer {
char sign; char sign;
const char *str; const char *str;
size_t size() const { return static_cast<std::size_t>(INF_SIZE); } size_t size() const {
return static_cast<std::size_t>(INF_SIZE + (sign ? 1 : 0));
}
size_t width() const { return size(); }
template <typename It> template <typename It>
void operator()(It &&it) const { void operator()(It &&it) const {
@ -2686,6 +2691,7 @@ class basic_writer {
internal::buffer &buffer; internal::buffer &buffer;
size_t size() const { return buffer.size() + (sign ? 1 : 0); } size_t size() const { return buffer.size() + (sign ? 1 : 0); }
size_t width() const { return size(); }
template <typename It> template <typename It>
void operator()(It &&it) { void operator()(It &&it) {
@ -2709,7 +2715,8 @@ class basic_writer {
const Char *s; const Char *s;
size_t size_; size_t size_;
size_t size() const { size_t size() const { return size_; }
size_t width() const {
return internal::count_code_points(basic_string_view<Char>(s, size_)); return internal::count_code_points(basic_string_view<Char>(s, size_));
} }
@ -2722,7 +2729,7 @@ class basic_writer {
// Writes a formatted string. // Writes a formatted string.
template <typename Char> template <typename Char>
void write_str(const Char *s, std::size_t size, const align_spec &spec) { void write_str(const Char *s, std::size_t size, const align_spec &spec) {
write_padded(size, spec, str_writer<Char>{s, size}); write_padded(spec, str_writer<Char>{s, size});
} }
template <typename Char> template <typename Char>
@ -2811,10 +2818,10 @@ class basic_writer {
template <typename Range> template <typename Range>
template <typename F> template <typename F>
void basic_writer<Range>::write_padded( void basic_writer<Range>::write_padded(const align_spec &spec, F &&f) {
std::size_t size, const align_spec &spec, F &&f) { unsigned width = spec.width(); // User-perceived width (in code points).
unsigned width = spec.width(); size_t size = f.size(); // The number of code units.
size_t num_code_points = width != 0 ? f.size() : size; size_t num_code_points = width != 0 ? f.width() : size;
if (width <= num_code_points) if (width <= num_code_points)
return f(reserve(size)); return f(reserve(size));
auto &&it = reserve(width + (size - num_code_points)); auto &&it = reserve(width + (size - num_code_points));
@ -2906,8 +2913,7 @@ void basic_writer<Range>::write_double(T value, const format_specs &spec) {
format_specs spec; format_specs spec;
char sign; char sign;
void operator()(const char *str) const { void operator()(const char *str) const {
writer.write_padded(INF_SIZE + (sign ? 1 : 0), spec, writer.write_padded(spec, inf_or_nan_writer{sign, str});
inf_or_nan_writer{sign, str});
} }
} write_inf_or_nan = {*this, spec, sign}; } write_inf_or_nan = {*this, spec, sign};
@ -2951,7 +2957,7 @@ void basic_writer<Range>::write_double(T value, const format_specs &spec) {
if (sign) if (sign)
++n; ++n;
} }
write_padded(n, as, double_writer{n, sign, buffer}); write_padded(as, double_writer{n, sign, buffer});
} }
template <typename Range> template <typename Range>