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 Aug 31, 2021
1 parent 34caecd commit c9eef45
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -740,19 +740,32 @@ 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 InputIt, typename OutputIt>
FMT_CONSTEXPR
typename std::enable_if
<
std::is_same<typename std::remove_const<InputIt>::type, OutputIt>::value &&
std::is_trivially_copy_assignable<OutputIt>::value,
OutputIt*
>::type
copy_str(InputIt* begin, InputIt* end, OutputIt* out)
{
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(OutputIt));
return out + size;
}

Expand Down

0 comments on commit c9eef45

Please sign in to comment.