Skip to content

Commit

Permalink
Partially revert / simplify recent src/formatter.cpp changes in #19
Browse files Browse the repository at this point in the history
  • Loading branch information
eddelbuettel committed Nov 10, 2024
1 parent 1ae815e commit 1db1c21
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 30 deletions.
8 changes: 8 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
2024-11-10 Dirk Eddelbuettel <[email protected]>

* src/formatter.cpp: Partially revert / simplify previous PR

2024-11-09 Dirk Eddelbuettel <[email protected]>

* inst/include/spdlog/*: New upstream release spdlog 1.15.0

2024-10-29 Dirk Eddelbuettel <[email protected]>

* DESCRIPTION (Version, Date): Roll micro release version
Expand Down
82 changes: 52 additions & 30 deletions src/formatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@

#include <RcppSpdlog>

// we accommodate C++20 where fmt::format is available as std::format
// this defines a helper function used below
#if defined(SPDLOG_USE_STD_FORMAT) && __cplusplus >= 202002L
#define NAMESPACE std
#else
#define NAMESPACE fmt
#endif

template<std::size_t... S>
NAMESPACE::format_args unpack_vector(const std::vector<std::string>& vec, std::index_sequence<S...>) {
return NAMESPACE::make_format_args(vec[S]...);
}
template<std::size_t... S>
std::format_args unpack_vector(const std::vector<std::string>& vec, std::index_sequence<S...>) {
return std::make_format_args(vec[S]...);
}

template<std::size_t size>
NAMESPACE::format_args unpack_vector(const std::vector<std::string>& vec) {
return unpack_vector(vec, std::make_index_sequence<size>());
}
template<std::size_t size>
std::format_args unpack_vector(const std::vector<std::string>& vec) {
return unpack_vector(vec, std::make_index_sequence<size>());
}

#endif

//' Simple Pass-Through Formatter to \code{fmt::format()}
//'
Expand All @@ -39,26 +39,48 @@ NAMESPACE::format_args unpack_vector(const std::vector<std::string>& vec) {
std::string formatter(const std::string s, std::vector<std::string> v) {
size_t n = v.size();
switch (n) {
case 0: return NAMESPACE::vformat(NAMESPACE::string_view(s), NAMESPACE::make_format_args());
case 1: return NAMESPACE::vformat(NAMESPACE::string_view(s), unpack_vector<1>(v));
case 2: return NAMESPACE::vformat(NAMESPACE::string_view(s), unpack_vector<2>(v));
case 3: return NAMESPACE::vformat(NAMESPACE::string_view(s), unpack_vector<3>(v));
case 4: return NAMESPACE::vformat(NAMESPACE::string_view(s), unpack_vector<4>(v));
case 5: return NAMESPACE::vformat(NAMESPACE::string_view(s), unpack_vector<5>(v));
case 6: return NAMESPACE::vformat(NAMESPACE::string_view(s), unpack_vector<6>(v));
case 7: return NAMESPACE::vformat(NAMESPACE::string_view(s), unpack_vector<7>(v));
case 8: return NAMESPACE::vformat(NAMESPACE::string_view(s), unpack_vector<8>(v));
case 9: return NAMESPACE::vformat(NAMESPACE::string_view(s), unpack_vector<9>(v));
case 10: return NAMESPACE::vformat(NAMESPACE::string_view(s), unpack_vector<10>(v));
case 11: return NAMESPACE::vformat(NAMESPACE::string_view(s), unpack_vector<11>(v));
case 12: return NAMESPACE::vformat(NAMESPACE::string_view(s), unpack_vector<12>(v));
case 13: return NAMESPACE::vformat(NAMESPACE::string_view(s), unpack_vector<13>(v));
case 14: return NAMESPACE::vformat(NAMESPACE::string_view(s), unpack_vector<14>(v));
case 15: return NAMESPACE::vformat(NAMESPACE::string_view(s), unpack_vector<15>(v));
#if defined(SPDLOG_USE_STD_FORMAT) && __cplusplus >= 202002L
case 0: return std::vformat(std::string_view(s), std::make_format_args());
case 1: return std::vformat(std::string_view(s), unpack_vector<1>(v));
case 2: return std::vformat(std::string_view(s), unpack_vector<2>(v));
case 3: return std::vformat(std::string_view(s), unpack_vector<3>(v));
case 4: return std::vformat(std::string_view(s), unpack_vector<4>(v));
case 5: return std::vformat(std::string_view(s), unpack_vector<5>(v));
case 6: return std::vformat(std::string_view(s), unpack_vector<6>(v));
case 7: return std::vformat(std::string_view(s), unpack_vector<7>(v));
case 8: return std::vformat(std::string_view(s), unpack_vector<8>(v));
case 9: return std::vformat(std::string_view(s), unpack_vector<9>(v));
case 10: return std::vformat(std::string_view(s), unpack_vector<10>(v));
case 11: return std::vformat(std::string_view(s), unpack_vector<11>(v));
case 12: return std::vformat(std::string_view(s), unpack_vector<12>(v));
case 13: return std::vformat(std::string_view(s), unpack_vector<13>(v));
case 14: return std::vformat(std::string_view(s), unpack_vector<14>(v));
case 15: return std::vformat(std::string_view(s), unpack_vector<15>(v));
default: {
Rcpp::warning("Only up to fifteen arguments support for now.");
return NAMESPACE::vformat(NAMESPACE::string_view(s), unpack_vector<12>(v));
return std::vformat(std::string_view(s), unpack_vector<12>(v));
}
#else
case 0: return fmt::format(s);
case 1: return fmt::format(s, std::string(v[0]));
case 2: return fmt::format(s, std::string(v[0]), std::string(v[1]));
case 3: return fmt::format(s, std::string(v[0]), std::string(v[1]), std::string(v[2]));
case 4: return fmt::format(s, std::string(v[0]), std::string(v[1]), std::string(v[2]), std::string(v[3]));
case 5: return fmt::format(s, std::string(v[0]), std::string(v[1]), std::string(v[2]), std::string(v[3]), std::string(v[4]));
case 6: return fmt::format(s, std::string(v[0]), std::string(v[1]), std::string(v[2]), std::string(v[3]), std::string(v[4]), std::string(v[5]));
case 7: return fmt::format(s, std::string(v[0]), std::string(v[1]), std::string(v[2]), std::string(v[3]), std::string(v[4]), std::string(v[5]), std::string(v[6]));
case 8: return fmt::format(s, std::string(v[0]), std::string(v[1]), std::string(v[2]), std::string(v[3]), std::string(v[4]), std::string(v[5]), std::string(v[6]), std::string(v[7]));
case 9: return fmt::format(s, std::string(v[0]), std::string(v[1]), std::string(v[2]), std::string(v[3]), std::string(v[4]), std::string(v[5]), std::string(v[6]), std::string(v[7]), std::string(v[8]));
case 10: return fmt::format(s, std::string(v[0]), std::string(v[1]), std::string(v[2]), std::string(v[3]), std::string(v[4]), std::string(v[5]), std::string(v[6]), std::string(v[7]), std::string(v[8]), std::string(v[9]));
case 11: return fmt::format(s, std::string(v[0]), std::string(v[1]), std::string(v[2]), std::string(v[3]), std::string(v[4]), std::string(v[5]), std::string(v[6]), std::string(v[7]), std::string(v[8]), std::string(v[9]), std::string(v[10]));
case 12: return fmt::format(s, std::string(v[0]), std::string(v[1]), std::string(v[2]), std::string(v[3]), std::string(v[4]), std::string(v[5]), std::string(v[6]), std::string(v[7]), std::string(v[8]), std::string(v[9]), std::string(v[10]), std::string(v[11]));
case 13: return fmt::format(s, std::string(v[0]), std::string(v[1]), std::string(v[2]), std::string(v[3]), std::string(v[4]), std::string(v[5]), std::string(v[6]), std::string(v[7]), std::string(v[8]), std::string(v[9]), std::string(v[10]), std::string(v[11]), std::string(v[12]));
case 14: return fmt::format(s, std::string(v[0]), std::string(v[1]), std::string(v[2]), std::string(v[3]), std::string(v[4]), std::string(v[5]), std::string(v[6]), std::string(v[7]), std::string(v[8]), std::string(v[9]), std::string(v[10]), std::string(v[11]), std::string(v[12]), std::string(v[13]));
case 15: return fmt::format(s, std::string(v[0]), std::string(v[1]), std::string(v[2]), std::string(v[3]), std::string(v[4]), std::string(v[5]), std::string(v[6]), std::string(v[7]), std::string(v[8]), std::string(v[9]), std::string(v[10]), std::string(v[11]), std::string(v[12]), std::string(v[13]), std::string(v[14]));
default: {
Rcpp::warning("Only up to fifteen arguments support for now.");
return fmt::format(s, std::string(v[0]), std::string(v[1]), std::string(v[2]), std::string(v[3]), std::string(v[4]), std::string(v[5]), std::string(v[6]), std::string(v[7]), std::string(v[8]), std::string(v[9]), std::string(v[10]), std::string(v[11]));
}
#endif
}
}

0 comments on commit 1db1c21

Please sign in to comment.