Skip to content

Commit

Permalink
Fix copy_str performance
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman-Koshelev committed Sep 1, 2021
1 parent 34caecd commit 10905c8
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -740,19 +740,26 @@ inline auto get_container(std::back_insert_iterator<Container> it)
}

template <typename Char, typename InputIt, typename OutputIt>
FMT_CONSTEXPR auto copy_str(InputIt begin, InputIt end, OutputIt out)
FMT_CONSTEXPR auto copy_str_constexpr(InputIt begin, InputIt end, OutputIt out)
-> OutputIt {
while (begin != end) *out++ = static_cast<Char>(*begin++);
return out;
}

template <typename Char, FMT_ENABLE_IF(std::is_same<Char, char>::value)>
FMT_CONSTEXPR auto copy_str(const Char* begin, const Char* end, Char* out)
-> Char* {
template <typename Char, typename InputIt, typename OutputIt>
FMT_CONSTEXPR auto copy_str(InputIt begin, InputIt end, OutputIt out)
-> OutputIt {
return copy_str_constexpr<Char>(begin, end, out);
}

template <typename Char, typename Tp, typename Up,
FMT_ENABLE_IF(std::is_same<remove_reference_t<Tp>, Up>::value && is_char<Up>::value)>
FMT_CONSTEXPR auto copy_str(Tp* begin, Tp* end, Up* out)
-> Up* {
if (is_constant_evaluated())
return copy_str<Char, const Char*, Char*>(begin, end, out);
return copy_str_constexpr<Char>(begin, end, out);
auto size = to_unsigned(end - begin);
memcpy(out, begin, size);
memcpy(out, begin, size * sizeof(Up));
return out + size;
}

Expand Down

0 comments on commit 10905c8

Please sign in to comment.