@@ -67,28 +67,28 @@ template<std::forward_iterator It, typename Specs>
6767 return mp_units::detail::parse_dynamic_spec (it, end, specs.width , specs.width_ref , ctx);
6868}
6969
70- template <typename Char, std::output_iterator<Char> It>
71- constexpr It format_global_buffer (It out, const fill_align_width_format_specs<Char>& specs)
70+ /* *
71+ * @brief Writes a string to an output iterator with fill/align/width padding.
72+ *
73+ * Avoids instantiating format_to/vformat_to infrastructure for the common padding use-case
74+ * inside formatter<Unit>, formatter<Dimension>, and formatter<quantity> specializations.
75+ */
76+ template <typename Char, std::output_iterator<Char> Out>
77+ constexpr Out write_padded (Out out, std::basic_string_view<Char> s, int width, fmt_align align,
78+ const fill_t <Char>& fill)
7279{
73- MP_UNITS_STD_FMT::format_to (out, " {{:" );
74- if (specs.fill .size () != 1 || specs.fill [0 ] != ' ' ) {
75- MP_UNITS_STD_FMT::format_to (out, " {}" , specs.fill .data ());
76- }
77- switch (specs.align ) {
78- case fmt_align::left:
79- MP_UNITS_STD_FMT::format_to (out, " <" );
80- break ;
81- case fmt_align::right:
82- MP_UNITS_STD_FMT::format_to (out, " >" );
83- break ;
84- case fmt_align::center:
85- MP_UNITS_STD_FMT::format_to (out, " ^" );
86- break ;
87- default :
88- break ;
89- }
90- if (specs.width >= 1 ) MP_UNITS_STD_FMT::format_to (out, " {}" , specs.width );
91- return MP_UNITS_STD_FMT::format_to (out, " }}" );
80+ const int len = static_cast <int >(s.size ());
81+ const int pad = (width > len) ? width - len : 0 ;
82+ const int lpad = (align == fmt_align::center) ? pad / 2 : (align == fmt_align::right) ? pad : 0 ;
83+ const int rpad = pad - lpad;
84+ auto write_fill = [&](int n) {
85+ for (int i = 0 ; i < n; ++i)
86+ for (std::size_t j = 0 ; j < fill.size (); ++j) *out++ = fill[j];
87+ };
88+ write_fill (lpad);
89+ for (Char c : s) *out++ = c;
90+ write_fill (rpad);
91+ return out;
9292}
9393
9494MP_UNITS_EXPORT_END
0 commit comments