Skip to content

Commit

Permalink
Optimize format string compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Jun 15, 2023
1 parent 6ad3012 commit 1daae55
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
7 changes: 6 additions & 1 deletion include/fmt/compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,12 @@ template <typename Char, typename T, int N> struct field {

template <typename OutputIt, typename... Args>
constexpr OutputIt format(OutputIt out, const Args&... args) const {
return write<Char>(out, 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>>) {
auto s = basic_string_view<Char>(arg);
return copy_str<Char>(s.begin(), s.end(), out);
}
return write<Char>(out, arg);
}
};

Expand Down
7 changes: 7 additions & 0 deletions include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -1486,6 +1486,13 @@ 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>
FMT_CONSTEXPR auto copy_str(R&& rng, OutputIt out) -> OutputIt {
Expand Down

0 comments on commit 1daae55

Please sign in to comment.