Remove iterator dependency

This commit is contained in:
Victor Zverovich 2024-01-10 18:27:56 -08:00
parent c5340539f9
commit b71ef65b6e
4 changed files with 36 additions and 32 deletions

View File

@ -8,6 +8,8 @@
#ifndef FMT_COMPILE_H_ #ifndef FMT_COMPILE_H_
#define FMT_COMPILE_H_ #define FMT_COMPILE_H_
#include <iterator> // std::back_inserter
#include "format.h" #include "format.h"
FMT_BEGIN_NAMESPACE FMT_BEGIN_NAMESPACE

View File

@ -1411,7 +1411,7 @@ FMT_FUNC void format_system_error(detail::buffer<char>& out, int error_code,
const char* message) noexcept { const char* message) noexcept {
FMT_TRY { FMT_TRY {
auto ec = std::error_code(error_code, std::generic_category()); auto ec = std::error_code(error_code, std::generic_category());
write(std::back_inserter(out), std::system_error(ec, message).what()); detail::write(appender(out), std::system_error(ec, message).what());
return; return;
} }
FMT_CATCH(...) {} FMT_CATCH(...) {}

View File

@ -42,9 +42,8 @@
#include <cstdint> // uint32_t #include <cstdint> // uint32_t
#include <cstring> // std::memcpy #include <cstring> // std::memcpy
#include <initializer_list> // std::initializer_list #include <initializer_list> // std::initializer_list
#include <iterator>
#include <limits> // std::numeric_limits #include <limits> // std::numeric_limits
#include <memory> // std::uninitialized_copy #include <memory> // std::uninitialized_copy_n
#include <stdexcept> // std::runtime_error #include <stdexcept> // std::runtime_error
#include <string> // std::basic_string #include <string> // std::basic_string
#include <system_error> // std::system_error #include <system_error> // std::system_error
@ -515,7 +514,21 @@ FMT_INLINE void assume(bool condition) {
#endif #endif
} }
template <typename Char, typename InputIt, typename OutputIt> 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) FMT_CONSTEXPR auto copy_str(InputIt begin, InputIt end, OutputIt out)
-> OutputIt { -> OutputIt {
while (begin != end) *out++ = static_cast<Char>(*begin++); while (begin != end) *out++ = static_cast<Char>(*begin++);
@ -532,19 +545,6 @@ FMT_CONSTEXPR auto copy_str(T* begin, T* end, U* out) -> U* {
return out + size; return out + size;
} }
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>
auto copy_str(InputIt begin, InputIt end,
std::back_insert_iterator<std::string> out)
-> std::back_insert_iterator<std::string> {
get_container(out).append(begin, end);
return out;
}
template <typename Char, typename R, typename OutputIt> template <typename Char, typename R, typename OutputIt>
FMT_CONSTEXPR auto copy_str(R&& rng, OutputIt out) -> OutputIt { FMT_CONSTEXPR auto copy_str(R&& rng, OutputIt out) -> OutputIt {
return detail::copy_str<Char>(rng.begin(), rng.end(), out); return detail::copy_str<Char>(rng.begin(), rng.end(), out);
@ -567,14 +567,15 @@ inline auto get_data(Container& c) -> typename Container::value_type* {
// Attempts to reserve space for n extra characters in the output range. // Attempts to reserve space for n extra characters in the output range.
// Returns a pointer to the reserved range or a reference to it. // Returns a pointer to the reserved range or a reference to it.
template <typename Container, FMT_ENABLE_IF(is_contiguous<Container>::value)> template <typename OutputIt,
FMT_ENABLE_IF(is_back_insert_iterator<OutputIt>::value&&
is_contiguous<typename OutputIt::container>::value)>
#if FMT_CLANG_VERSION >= 307 && !FMT_ICC_VERSION #if FMT_CLANG_VERSION >= 307 && !FMT_ICC_VERSION
__attribute__((no_sanitize("undefined"))) __attribute__((no_sanitize("undefined")))
#endif #endif
inline auto inline auto
reserve(std::back_insert_iterator<Container> it, size_t n) -> reserve(OutputIt it, size_t n) -> typename OutputIt::value_type* {
typename Container::value_type* { auto& c = get_container(it);
Container& c = get_container(it);
size_t size = c.size(); size_t size = c.size();
c.resize(size + n); c.resize(size + n);
return get_data(c) + size; return get_data(c) + size;
@ -608,10 +609,12 @@ template <typename T> auto to_pointer(basic_appender<T> it, size_t n) -> T* {
return buf.data() + size; return buf.data() + size;
} }
template <typename Container, FMT_ENABLE_IF(is_contiguous<Container>::value)> template <typename OutputIt,
inline auto base_iterator(std::back_insert_iterator<Container> it, FMT_ENABLE_IF(is_back_insert_iterator<OutputIt>::value&&
typename Container::value_type*) is_contiguous<typename OutputIt::container>::value)>
-> std::back_insert_iterator<Container> { inline auto base_iterator(OutputIt it,
typename OutputIt::containter_type::value_type*)
-> OutputIt {
return it; return it;
} }
@ -4282,7 +4285,7 @@ template <typename T> struct nested_formatter {
auto write_padded(format_context& ctx, F write) const -> decltype(ctx.out()) { auto write_padded(format_context& ctx, F write) const -> decltype(ctx.out()) {
if (width_ == 0) return write(ctx.out()); if (width_ == 0) return write(ctx.out());
auto buf = memory_buffer(); auto buf = memory_buffer();
write(std::back_inserter(buf)); write(appender(buf));
auto specs = format_specs<>(); auto specs = format_specs<>();
specs.width = width_; specs.width = width_;
specs.fill = fill_; specs.fill = fill_;

View File

@ -431,8 +431,7 @@ class FMT_API ostream {
output to the file. output to the file.
*/ */
template <typename... T> void print(format_string<T...> fmt, T&&... args) { template <typename... T> void print(format_string<T...> fmt, T&&... args) {
vformat_to(std::back_inserter(buffer_), fmt, vformat_to(appender(buffer_), fmt, fmt::make_format_args(args...));
fmt::make_format_args(args...));
} }
}; };