Cleanup copy functions and move to base.h

This commit is contained in:
Victor Zverovich 2024-01-13 09:21:42 -08:00
parent 59baac522e
commit da0f84c42c
5 changed files with 83 additions and 88 deletions

View File

@ -1123,6 +1123,40 @@ using appender = basic_appender<char>;
namespace detail { namespace detail {
// An optimized version of std::copy with the output value type (T).
template <typename T, typename InputIt>
auto copy(InputIt begin, InputIt end, appender out) -> appender {
get_container(out).append(begin, end);
return out;
}
template <typename T, typename InputIt, typename OutputIt,
FMT_ENABLE_IF(is_back_insert_iterator<OutputIt>::value)>
auto copy(InputIt begin, InputIt end, OutputIt out) -> OutputIt {
get_container(out).append(begin, end);
return out;
}
template <typename T, typename InputIt, typename OutputIt,
FMT_ENABLE_IF(!is_back_insert_iterator<OutputIt>::value)>
FMT_CONSTEXPR auto copy(InputIt begin, InputIt end, OutputIt out) -> OutputIt {
while (begin != end) *out++ = static_cast<T>(*begin++);
return out;
}
template <typename T>
FMT_CONSTEXPR auto copy(const T* begin, const T* end, T* out) -> T* {
if (is_constant_evaluated()) return copy<T, const T*, T*>(begin, end, out);
auto size = to_unsigned(end - begin);
if (size > 0) memcpy(out, begin, size * sizeof(T));
return out + size;
}
template <typename T, typename V, typename OutputIt>
FMT_CONSTEXPR auto copy(basic_string_view<V> s, OutputIt out) -> OutputIt {
return copy<T>(s.begin(), s.end(), out);
}
template <typename Context, typename T> template <typename Context, typename T>
constexpr auto has_const_formatter_impl(T*) constexpr auto has_const_formatter_impl(T*)
-> decltype(typename Context::template formatter_type<T>().format( -> decltype(typename Context::template formatter_type<T>().format(

View File

@ -382,9 +382,9 @@ auto write_encoded_tm_str(OutputIt out, string_view in, const std::locale& loc)
to_utf8<code_unit, basic_memory_buffer<char, unit_t::max_size * 4>>(); to_utf8<code_unit, basic_memory_buffer<char, unit_t::max_size * 4>>();
if (!u.convert({unit.buf, to_unsigned(unit.end - unit.buf)})) if (!u.convert({unit.buf, to_unsigned(unit.end - unit.buf)}))
FMT_THROW(format_error("failed to format time")); FMT_THROW(format_error("failed to format time"));
return copy_str<char>(u.c_str(), u.c_str() + u.size(), out); return copy<char>(u.c_str(), u.c_str() + u.size(), out);
} }
return copy_str<char>(in.data(), in.data() + in.size(), out); return copy<char>(in.data(), in.data() + in.size(), out);
} }
template <typename Char, typename OutputIt, template <typename Char, typename OutputIt,
@ -393,7 +393,7 @@ auto write_tm_str(OutputIt out, string_view sv, const std::locale& loc)
-> OutputIt { -> OutputIt {
codecvt_result<Char> unit; codecvt_result<Char> unit;
write_codecvt(unit, sv, loc); write_codecvt(unit, sv, loc);
return copy_str<Char>(unit.buf, unit.end, out); return copy<Char>(unit.buf, unit.end, out);
} }
template <typename Char, typename OutputIt, template <typename Char, typename OutputIt,
@ -1364,7 +1364,7 @@ class tm_writer {
auto out() const -> OutputIt { return out_; } auto out() const -> OutputIt { return out_; }
FMT_CONSTEXPR void on_text(const Char* begin, const Char* end) { FMT_CONSTEXPR void on_text(const Char* begin, const Char* end) {
out_ = copy_str<Char>(begin, end, out_); out_ = copy<Char>(begin, end, out_);
} }
void on_abbr_weekday() { void on_abbr_weekday() {
@ -1437,7 +1437,7 @@ class tm_writer {
write_digit2_separated(buf, to_unsigned(tm_mon() + 1), write_digit2_separated(buf, to_unsigned(tm_mon() + 1),
to_unsigned(tm_mday()), to_unsigned(tm_mday()),
to_unsigned(split_year_lower(tm_year())), '/'); to_unsigned(split_year_lower(tm_year())), '/');
out_ = copy_str<Char>(std::begin(buf), std::end(buf), out_); out_ = copy<Char>(std::begin(buf), std::end(buf), out_);
} }
void on_iso_date() { void on_iso_date() {
auto year = tm_year(); auto year = tm_year();
@ -1453,7 +1453,7 @@ class tm_writer {
write_digit2_separated(buf + 2, static_cast<unsigned>(year % 100), write_digit2_separated(buf + 2, static_cast<unsigned>(year % 100),
to_unsigned(tm_mon() + 1), to_unsigned(tm_mday()), to_unsigned(tm_mon() + 1), to_unsigned(tm_mday()),
'-'); '-');
out_ = copy_str<Char>(std::begin(buf) + offset, std::end(buf), out_); out_ = copy<Char>(std::begin(buf) + offset, std::end(buf), out_);
} }
void on_utc_offset(numeric_system ns) { format_utc_offset_impl(tm_, ns); } void on_utc_offset(numeric_system ns) { format_utc_offset_impl(tm_, ns); }
@ -1586,7 +1586,7 @@ class tm_writer {
char buf[8]; char buf[8];
write_digit2_separated(buf, to_unsigned(tm_hour12()), write_digit2_separated(buf, to_unsigned(tm_hour12()),
to_unsigned(tm_min()), to_unsigned(tm_sec()), ':'); to_unsigned(tm_min()), to_unsigned(tm_sec()), ':');
out_ = copy_str<Char>(std::begin(buf), std::end(buf), out_); out_ = copy<Char>(std::begin(buf), std::end(buf), out_);
*out_++ = ' '; *out_++ = ' ';
on_am_pm(); on_am_pm();
} else { } else {

View File

@ -15,9 +15,9 @@
FMT_BEGIN_NAMESPACE FMT_BEGIN_NAMESPACE
namespace detail { namespace detail {
template <typename Char, typename InputIt> template <typename T, typename InputIt>
FMT_CONSTEXPR inline auto copy_str(InputIt begin, InputIt end, FMT_CONSTEXPR inline auto copy(InputIt begin, InputIt end, counting_iterator it)
counting_iterator it) -> counting_iterator { -> counting_iterator {
return it + (end - begin); return it + (end - begin);
} }
@ -148,7 +148,7 @@ template <typename Char, typename T, int N> struct field {
const T& arg = get_arg_checked<T, N>(args...); const T& arg = get_arg_checked<T, N>(args...);
if constexpr (std::is_convertible_v<T, basic_string_view<Char>>) { if constexpr (std::is_convertible_v<T, basic_string_view<Char>>) {
auto s = basic_string_view<Char>(arg); auto s = basic_string_view<Char>(arg);
return copy_str<Char>(s.begin(), s.end(), out); return copy<Char>(s.begin(), s.end(), out);
} }
return write<Char>(out, arg); return write<Char>(out, arg);
} }

View File

@ -513,42 +513,6 @@ FMT_INLINE void assume(bool condition) {
#endif #endif
} }
template <typename Char, typename InputIt>
auto copy_str(InputIt begin, InputIt end, appender out) -> appender {
get_container(out).append(begin, end);
return out;
}
template <typename Char, typename InputIt, typename OutputIt,
FMT_ENABLE_IF(is_back_insert_iterator<OutputIt>::value)>
auto copy_str(InputIt begin, InputIt end, OutputIt out) -> OutputIt {
get_container(out).append(begin, end);
return out;
}
template <typename Char, typename InputIt, typename OutputIt,
FMT_ENABLE_IF(!is_back_insert_iterator<OutputIt>::value)>
FMT_CONSTEXPR auto copy_str(InputIt begin, InputIt end, OutputIt out)
-> OutputIt {
while (begin != end) *out++ = static_cast<Char>(*begin++);
return out;
}
template <typename Char, typename T, typename U,
FMT_ENABLE_IF(
std::is_same<remove_const_t<T>, U>::value&& is_char<U>::value)>
FMT_CONSTEXPR auto copy_str(T* begin, T* end, U* out) -> U* {
if (is_constant_evaluated()) return copy_str<Char, T*, U*>(begin, end, out);
auto size = to_unsigned(end - begin);
if (size > 0) memcpy(out, begin, size * sizeof(U));
return out + size;
}
template <typename Char, typename R, typename OutputIt>
FMT_CONSTEXPR auto copy_str(R&& rng, OutputIt out) -> OutputIt {
return detail::copy_str<Char>(rng.begin(), rng.end(), out);
}
// An approximation of iterator_t for pre-C++20 systems. // An approximation of iterator_t for pre-C++20 systems.
template <typename T> template <typename T>
using iterator_t = decltype(std::begin(std::declval<T&>())); using iterator_t = decltype(std::begin(std::declval<T&>()));
@ -640,9 +604,9 @@ FMT_CONSTEXPR20 auto fill_n(T* out, Size count, char value) -> T* {
} }
template <typename OutChar, typename InputIt, typename OutputIt> template <typename OutChar, typename InputIt, typename OutputIt>
FMT_CONSTEXPR FMT_NOINLINE auto copy_str_noinline(InputIt begin, InputIt end, FMT_CONSTEXPR FMT_NOINLINE auto copy_noinline(InputIt begin, InputIt end,
OutputIt out) -> OutputIt { OutputIt out) -> OutputIt {
return copy_str<OutChar>(begin, end, out); return copy<OutChar>(begin, end, out);
} }
// A public domain branchless UTF-8 decoder by Christopher Wellons: // A public domain branchless UTF-8 decoder by Christopher Wellons:
@ -723,7 +687,7 @@ FMT_CONSTEXPR void for_each_codepoint(string_view s, F f) {
} }
if (auto num_chars_left = s.data() + s.size() - p) { if (auto num_chars_left = s.data() + s.size() - p) {
char buf[2 * block_size - 1] = {}; char buf[2 * block_size - 1] = {};
copy_str<char>(p, p + num_chars_left, buf); copy<char>(p, p + num_chars_left, buf);
const char* buf_ptr = buf; const char* buf_ptr = buf;
do { do {
auto end = decode(buf_ptr, p); auto end = decode(buf_ptr, p);
@ -952,7 +916,7 @@ class basic_memory_buffer : public detail::buffer<T> {
size_t size = other.size(), capacity = other.capacity(); size_t size = other.size(), capacity = other.capacity();
if (data == other.store_) { if (data == other.store_) {
this->set(store_, capacity); this->set(store_, capacity);
detail::copy_str<T>(other.store_, other.store_ + size, store_); detail::copy<T>(other.store_, other.store_ + size, store_);
} else { } else {
this->set(data, capacity); this->set(data, capacity);
// Set pointer to the inline array so that delete is not called // Set pointer to the inline array so that delete is not called
@ -1035,7 +999,7 @@ namespace detail_exported {
#if FMT_USE_NONTYPE_TEMPLATE_ARGS #if FMT_USE_NONTYPE_TEMPLATE_ARGS
template <typename Char, size_t N> struct fixed_string { template <typename Char, size_t N> struct fixed_string {
constexpr fixed_string(const Char (&str)[N]) { constexpr fixed_string(const Char (&str)[N]) {
detail::copy_str<Char, const Char*, Char*>(static_cast<const Char*>(str), detail::copy<Char, const Char*, Char*>(static_cast<const Char*>(str),
str + N, data); str + N, data);
} }
Char data[N] = {}; Char data[N] = {};
@ -1393,7 +1357,7 @@ FMT_CONSTEXPR inline auto format_decimal(Iterator out, UInt value, int size)
// Buffer is large enough to hold all digits (digits10 + 1). // Buffer is large enough to hold all digits (digits10 + 1).
Char buffer[digits10<UInt>() + 1] = {}; Char buffer[digits10<UInt>() + 1] = {};
auto end = format_decimal(buffer, value, size).end; auto end = format_decimal(buffer, value, size).end;
return {out, detail::copy_str_noinline<Char>(buffer, end, out)}; return {out, detail::copy_noinline<Char>(buffer, end, out)};
} }
template <unsigned BASE_BITS, typename Char, typename UInt> template <unsigned BASE_BITS, typename Char, typename UInt>
@ -1420,7 +1384,7 @@ FMT_CONSTEXPR inline auto format_uint(It out, UInt value, int num_digits,
// Buffer should be large enough to hold all digits (digits / BASE_BITS + 1). // Buffer should be large enough to hold all digits (digits / BASE_BITS + 1).
char buffer[num_bits<UInt>() / BASE_BITS + 1] = {}; char buffer[num_bits<UInt>() / BASE_BITS + 1] = {};
format_uint<BASE_BITS>(buffer, value, num_digits, upper); format_uint<BASE_BITS>(buffer, value, num_digits, upper);
return detail::copy_str_noinline<Char>(buffer, buffer + num_digits, out); return detail::copy_noinline<Char>(buffer, buffer + num_digits, out);
} }
// A converter from UTF-8 to UTF-16. // A converter from UTF-8 to UTF-16.
@ -1776,8 +1740,7 @@ FMT_NOINLINE FMT_CONSTEXPR auto fill(OutputIt it, size_t n,
auto fill_size = fill.size(); auto fill_size = fill.size();
if (fill_size == 1) return detail::fill_n(it, n, fill[0]); if (fill_size == 1) return detail::fill_n(it, n, fill[0]);
auto data = fill.data(); auto data = fill.data();
for (size_t i = 0; i < n; ++i) for (size_t i = 0; i < n; ++i) it = copy<Char>(data, data + fill_size, it);
it = copy_str<Char>(data, data + fill_size, it);
return it; return it;
} }
@ -1813,10 +1776,10 @@ constexpr auto write_padded(OutputIt out, const format_specs<Char>& specs,
template <align::type align = align::left, typename Char, typename OutputIt> template <align::type align = align::left, typename Char, typename OutputIt>
FMT_CONSTEXPR auto write_bytes(OutputIt out, string_view bytes, FMT_CONSTEXPR auto write_bytes(OutputIt out, string_view bytes,
const format_specs<Char>& specs) -> OutputIt { const format_specs<Char>& specs) -> OutputIt {
return write_padded<align>( return write_padded<align>(out, specs, bytes.size(),
out, specs, bytes.size(), [bytes](reserve_iterator<OutputIt> it) { [bytes](reserve_iterator<OutputIt> it) {
const char* data = bytes.data(); const char* data = bytes.data();
return copy_str<Char>(data, data + bytes.size(), it); return copy<Char>(data, data + bytes.size(), it);
}); });
} }
@ -1913,7 +1876,7 @@ auto write_codepoint(OutputIt out, char prefix, uint32_t cp) -> OutputIt {
Char buf[width]; Char buf[width];
fill_n(buf, width, static_cast<Char>('0')); fill_n(buf, width, static_cast<Char>('0'));
format_uint<4>(buf, cp, width); format_uint<4>(buf, cp, width);
return copy_str<Char>(buf, buf + width, out); return copy<Char>(buf, buf + width, out);
} }
template <typename OutputIt, typename Char> template <typename OutputIt, typename Char>
@ -1964,7 +1927,7 @@ auto write_escaped_string(OutputIt out, basic_string_view<Char> str)
auto begin = str.begin(), end = str.end(); auto begin = str.begin(), end = str.end();
do { do {
auto escape = find_escape(begin, end); auto escape = find_escape(begin, end);
out = copy_str<Char>(begin, escape.begin, out); out = copy<Char>(begin, escape.begin, out);
begin = escape.end; begin = escape.end;
if (!begin) break; if (!begin) break;
out = write_escaped_cp<OutputIt, Char>(out, escape); out = write_escaped_cp<OutputIt, Char>(out, escape);
@ -2115,8 +2078,7 @@ template <typename Char> class digit_grouping {
for (int i = 0, sep_index = static_cast<int>(separators.size() - 1); for (int i = 0, sep_index = static_cast<int>(separators.size() - 1);
i < num_digits; ++i) { i < num_digits; ++i) {
if (num_digits - i == separators[sep_index]) { if (num_digits - i == separators[sep_index]) {
out = out = copy<Char>(thousands_sep_.data(),
copy_str<Char>(thousands_sep_.data(),
thousands_sep_.data() + thousands_sep_.size(), out); thousands_sep_.data() + thousands_sep_.size(), out);
--sep_index; --sep_index;
} }
@ -2386,7 +2348,7 @@ FMT_CONSTEXPR auto write(OutputIt out, basic_string_view<Char> s,
return write_padded(out, specs, size, width, return write_padded(out, specs, size, width,
[=](reserve_iterator<OutputIt> it) { [=](reserve_iterator<OutputIt> it) {
if (is_debug) return write_escaped_string(it, s); if (is_debug) return write_escaped_string(it, s);
return copy_str<Char>(data, data + size, it); return copy<Char>(data, data + size, it);
}); });
} }
template <typename Char, typename OutputIt> template <typename Char, typename OutputIt>
@ -2547,7 +2509,7 @@ FMT_CONSTEXPR20 auto write_nonfinite(OutputIt out, bool isnan,
if (is_zero_fill) specs.fill[0] = static_cast<Char>(' '); if (is_zero_fill) specs.fill[0] = static_cast<Char>(' ');
return write_padded(out, specs, size, [=](reserve_iterator<OutputIt> it) { return write_padded(out, specs, size, [=](reserve_iterator<OutputIt> it) {
if (sign) *it++ = detail::sign<Char>(sign); if (sign) *it++ = detail::sign<Char>(sign);
return copy_str<Char>(str, str + str_size, it); return copy<Char>(str, str + str_size, it);
}); });
} }
@ -2569,7 +2531,7 @@ inline auto get_significand_size(const dragonbox::decimal_fp<T>& f) -> int {
template <typename Char, typename OutputIt> template <typename Char, typename OutputIt>
constexpr auto write_significand(OutputIt out, const char* significand, constexpr auto write_significand(OutputIt out, const char* significand,
int significand_size) -> OutputIt { int significand_size) -> OutputIt {
return copy_str<Char>(significand, significand + significand_size, out); return copy<Char>(significand, significand + significand_size, out);
} }
template <typename Char, typename OutputIt, typename UInt> template <typename Char, typename OutputIt, typename UInt>
inline auto write_significand(OutputIt out, UInt significand, inline auto write_significand(OutputIt out, UInt significand,
@ -2622,18 +2584,18 @@ inline auto write_significand(OutputIt out, UInt significand,
Char buffer[digits10<UInt>() + 2]; Char buffer[digits10<UInt>() + 2];
auto end = write_significand(buffer, significand, significand_size, auto end = write_significand(buffer, significand, significand_size,
integral_size, decimal_point); integral_size, decimal_point);
return detail::copy_str_noinline<Char>(buffer, end, out); return detail::copy_noinline<Char>(buffer, end, out);
} }
template <typename OutputIt, typename Char> template <typename OutputIt, typename Char>
FMT_CONSTEXPR auto write_significand(OutputIt out, const char* significand, FMT_CONSTEXPR auto write_significand(OutputIt out, const char* significand,
int significand_size, int integral_size, int significand_size, int integral_size,
Char decimal_point) -> OutputIt { Char decimal_point) -> OutputIt {
out = detail::copy_str_noinline<Char>(significand, out = detail::copy_noinline<Char>(significand, significand + integral_size,
significand + integral_size, out); out);
if (!decimal_point) return out; if (!decimal_point) return out;
*out++ = decimal_point; *out++ = decimal_point;
return detail::copy_str_noinline<Char>(significand + integral_size, return detail::copy_noinline<Char>(significand + integral_size,
significand + significand_size, out); significand + significand_size, out);
} }
@ -2651,7 +2613,7 @@ FMT_CONSTEXPR20 auto write_significand(OutputIt out, T significand,
integral_size, decimal_point); integral_size, decimal_point);
grouping.apply( grouping.apply(
out, basic_string_view<Char>(buffer.data(), to_unsigned(integral_size))); out, basic_string_view<Char>(buffer.data(), to_unsigned(integral_size)));
return detail::copy_str_noinline<Char>(buffer.data() + integral_size, return detail::copy_noinline<Char>(buffer.data() + integral_size,
buffer.end(), out); buffer.end(), out);
} }
@ -2936,7 +2898,7 @@ class bigint {
auto size = other.bigits_.size(); auto size = other.bigits_.size();
bigits_.resize(size); bigits_.resize(size);
auto data = other.bigits_.data(); auto data = other.bigits_.data();
copy_str<bigit>(data, data + size, bigits_.data()); copy<bigit>(data, data + size, bigits_.data());
exp_ = other.exp_; exp_ = other.exp_;
} }
@ -3741,7 +3703,7 @@ template <typename Char, typename OutputIt>
FMT_CONSTEXPR auto write(OutputIt out, basic_string_view<Char> value) FMT_CONSTEXPR auto write(OutputIt out, basic_string_view<Char> value)
-> OutputIt { -> OutputIt {
auto it = reserve(out, value.size()); auto it = reserve(out, value.size());
it = copy_str_noinline<Char>(value.begin(), value.end(), it); it = copy_noinline<Char>(value.begin(), value.end(), it);
return base_iterator(out, it); return base_iterator(out, it);
} }

View File

@ -265,8 +265,7 @@ template <typename FormatContext> struct format_tuple_element {
template <typename T> template <typename T>
void operator()(const formatter<T, char_type>& f, const T& v) { void operator()(const formatter<T, char_type>& f, const T& v) {
if (i > 0) if (i > 0) ctx.advance_to(detail::copy<char_type>(separator, ctx.out()));
ctx.advance_to(detail::copy_str<char_type>(separator, ctx.out()));
ctx.advance_to(f.format(v, ctx)); ctx.advance_to(f.format(v, ctx));
++i; ++i;
} }
@ -327,11 +326,11 @@ struct formatter<Tuple, Char,
template <typename FormatContext> template <typename FormatContext>
auto format(const Tuple& value, FormatContext& ctx) const auto format(const Tuple& value, FormatContext& ctx) const
-> decltype(ctx.out()) { -> decltype(ctx.out()) {
ctx.advance_to(detail::copy_str<Char>(opening_bracket_, ctx.out())); ctx.advance_to(detail::copy<Char>(opening_bracket_, ctx.out()));
detail::for_each2( detail::for_each2(
formatters_, value, formatters_, value,
detail::format_tuple_element<FormatContext>{0, ctx, separator_}); detail::format_tuple_element<FormatContext>{0, ctx, separator_});
return detail::copy_str<Char>(closing_bracket_, ctx.out()); return detail::copy<Char>(closing_bracket_, ctx.out());
} }
}; };
@ -439,18 +438,18 @@ struct range_formatter<
auto format(R&& range, FormatContext& ctx) const -> decltype(ctx.out()) { auto format(R&& range, FormatContext& ctx) const -> decltype(ctx.out()) {
detail::range_mapper<buffered_context<Char>> mapper; detail::range_mapper<buffered_context<Char>> mapper;
auto out = ctx.out(); auto out = ctx.out();
out = detail::copy_str<Char>(opening_bracket_, out); out = detail::copy<Char>(opening_bracket_, out);
int i = 0; int i = 0;
auto it = detail::range_begin(range); auto it = detail::range_begin(range);
auto end = detail::range_end(range); auto end = detail::range_end(range);
for (; it != end; ++it) { for (; it != end; ++it) {
if (i > 0) out = detail::copy_str<Char>(separator_, out); if (i > 0) out = detail::copy<Char>(separator_, out);
ctx.advance_to(out); ctx.advance_to(out);
auto&& item = *it; auto&& item = *it;
out = underlying_.format(mapper.map(item), ctx); out = underlying_.format(mapper.map(item), ctx);
++i; ++i;
} }
out = detail::copy_str<Char>(closing_bracket_, out); out = detail::copy<Char>(closing_bracket_, out);
return out; return out;
} }
}; };
@ -568,7 +567,7 @@ struct formatter<join_view<It, Sentinel, Char>, Char> {
out = value_formatter_.format(*it, ctx); out = value_formatter_.format(*it, ctx);
++it; ++it;
while (it != value.end) { while (it != value.end) {
out = detail::copy_str<Char>(value.sep.begin(), value.sep.end(), out); out = detail::copy<Char>(value.sep.begin(), value.sep.end(), out);
ctx.advance_to(out); ctx.advance_to(out);
out = value_formatter_.format(*it, ctx); out = value_formatter_.format(*it, ctx);
++it; ++it;