refactor: remove dependency on <ranges> header and switch to use an iterator-based copy algorithm

This commit is contained in:
Mateusz Pusz
2024-05-08 11:18:54 +02:00
parent 75f719add7
commit 3dcb8f4f49
5 changed files with 9 additions and 43 deletions

View File

@@ -39,7 +39,6 @@
#include <numeric>
#include <optional>
#include <random>
#include <ranges>
#include <sstream>
#include <string>
#include <string_view>

View File

@@ -95,7 +95,7 @@ constexpr Out copy(const symbol_text<N, M>& txt, text_encoding encoding, Out out
{
if (encoding == text_encoding::unicode) {
if constexpr (is_same_v<CharT, char8_t>)
return copy(txt.unicode(), out).out;
return ::mp_units::detail::copy(txt.unicode().begin(), txt.unicode().end(), out);
else if constexpr (is_same_v<CharT, char>) {
for (const char8_t ch : txt.unicode()) *out++ = static_cast<char>(ch);
return out;
@@ -103,7 +103,7 @@ constexpr Out copy(const symbol_text<N, M>& txt, text_encoding encoding, Out out
throw std::invalid_argument("Unicode text can't be copied to CharT output");
} else {
if constexpr (is_same_v<CharT, char>)
return copy(txt.ascii(), out).out;
return ::mp_units::detail::copy(txt.ascii().begin(), txt.ascii().end(), out);
else
throw std::invalid_argument("ASCII text can't be copied to CharT output");
}

View File

@@ -31,7 +31,6 @@
#include <compare>
#include <initializer_list>
#include <iterator>
#include <ranges>
#endif
namespace mp_units::detail {
@@ -164,44 +163,11 @@ constexpr const T& min(const T& a, const T& b)
return (b < a) ? b : a;
}
template<class I, class O>
struct in_out_result {
[[no_unique_address]] I in;
[[no_unique_address]] O out;
template<class I2, class O2>
requires std::convertible_to<const I&, I2> && std::convertible_to<const O&, O2>
constexpr operator in_out_result<I2, O2>() const&
{
return {in, out};
}
template<class I2, class O2>
requires std::convertible_to<I, I2> && std::convertible_to<O, O2>
constexpr operator in_out_result<I2, O2>() &&
{
return {std::move(in), std::move(out)};
}
};
template<class I, class O>
using copy_result = in_out_result<I, O>;
template<std::input_iterator I, std::sentinel_for<I> S, std::weakly_incrementable O>
requires std::indirectly_copyable<I, O>
constexpr copy_result<I, O> copy(I first, S last, O result)
template<class InputIt, class OutputIt>
constexpr OutputIt copy(InputIt first, InputIt last, OutputIt d_first)
{
for (; first != last; ++first, (void)++result) {
*result = *first;
}
return {std::move(first), std::move(result)};
}
template<std::ranges::input_range R, std::weakly_incrementable O>
requires std::indirectly_copyable<std::ranges::iterator_t<R>, O>
constexpr copy_result<std::ranges::borrowed_iterator_t<R>, O> copy(R&& r, O result)
{
return ::mp_units::detail::copy(std::ranges::begin(r), std::ranges::end(r), std::move(result));
for (; first != last; (void)++first, (void)++d_first) *d_first = *first;
return d_first;
}
} // namespace mp_units::detail

View File

@@ -319,7 +319,7 @@ class MP_UNITS_STD_FMT::formatter<mp_units::quantity<Reference, Rep>, Char> {
out = MP_UNITS_STD_FMT::vformat_to(out, locale, f.dimension_format_str_,
MP_UNITS_STD_FMT::make_format_args(q.dimension));
}
void on_text(const Char* begin, const Char* end) const { std::copy(begin, end, out); }
void on_text(const Char* begin, const Char* end) const { mp_units::detail::copy(begin, end, out); }
};
template<typename OutputIt, typename... Args>
quantity_formatter(const formatter&, OutputIt, Args...) -> quantity_formatter<OutputIt>;

View File

@@ -720,7 +720,8 @@ constexpr Out print_separator(Out out, const unit_symbol_formatting& fmt)
if (fmt.encoding != text_encoding::unicode)
throw std::invalid_argument(
"'unit_symbol_separator::half_high_dot' can be only used with 'text_encoding::unicode'");
out = copy(std::string_view(""), out).out;
const std::string_view dot = "";
out = detail::copy(dot.begin(), dot.end(), out);
} else {
*out++ = ' ';
}