我要提问
ARTICLE DETAIL

资讯详情

前沿编程新知与开发实战干货的深度解读。

C/C++自定义std::format格式化

C/C++自定义std::format格式化 C#include string #include sstream #include algorithm #include iostream #include iomanip template class T0, class T1, class void struct _has_stream_bit_shl : std::false_type {}; template class T0, class T1 struct _has_stream_bit_shlT0, T1, std::void_tdecltype( std::declvalT0() std::declvalT1() ) : std::true_type {}; template class T, class void struct _has_range_begin_end : std::false_type {}; template class T struct _has_range_begin_endT, std::void_tdecltype( std::begin(std::declvalT()) ! std::end(std::declvalT()) ) : std::true_type {}; template class T, class void struct _has_tuple_size : std::false_type {}; template class T struct _has_tuple_sizeT, std::void_tdecltype( std::tuple_sizeT::value ) : std::true_type {}; struct _to_stream_impl { template class Os, class T, std::size_t ...Is static void _helper_tuple_to_stream(Os os, T const t, std::string_view fms, std::index_sequenceIs...) { os (; (to_stream(os, std::get0(t), fms), ..., (os , to_stream(os, std::getIs 1(t), fms))); os ); } template class Os, class T, std::enable_if_t!_has_stream_bit_shlOs, T const::value (std::tuple_sizeT::value 1), int 0 static void to_stream(Os os, T const t, std::string_view fms) { return _helper_tuple_to_stream(os, t, fms, std::make_index_sequencestd::tuple_size_vT -1{}); } template class Os, class T static auto to_stream(Os os, T const t, std::string_view fms) - decltype((std::enable_if_t!_has_stream_bit_shlOs, T const::value !_has_range_begin_endT::value, void)(std::declvalT()[0], std::declvalT().length())) { os [glm: ; for (int i 0; i t.length(); i) { to_stream(os, t[i], fms); os , ; } os ]; } template class Os, class T, std::enable_if_t!_has_stream_bit_shlOs, T const::value (std::tuple_sizeT::value 0), int 0 static void to_stream(Os os, T const t, std::string_view fms) { os (); } template class Os, class T, std::enable_if_t!_has_stream_bit_shlOs, T const::value !_has_tuple_sizeT::value _has_range_begin_endT::value, int 0 static void to_stream(Os os, T const t, std::string_view fms) { auto it std::begin(t); auto eit std::end(t); os [; if (it ! eit) { to_stream(os, *it, fms); it; for (; it ! eit; it) { os ; to_stream(os, *it, fms); } } os ]; } template class Os, class T, std::enable_if_t_has_stream_bit_shlOs, T const::value !std::is_enumT::value, int 0 static void to_stream(Os os, T const t, std::string_view fms) { auto flgs os.flags(); if (fms.size() ! 0) { if (fms.size() ! 0 fms[0] -) { fms fms.substr(1); os std::right; } if (fms.size() ! 0 fms[0] 0) { fms fms.substr(1); os std::setfill(0); } { int tmp 0; while (fms.size() ! 0 0 fms[0] 9 fms[0]) { tmp * 10; tmp fms[0] - 0; fms fms.substr(1); } if (tmp ! 0) os std::setw(tmp); } if (fms.size() ! 0 fms[0] .) { fms fms.substr(1); int tmp 0; while (fms.size() ! 0 0 fms[0] 9 fms[0]) { tmp * 10; tmp fms[0] - 0; fms fms.substr(1); } os std::setprecision(tmp); } if (fms.size() ! 0) { switch (fms[0]) { case x: os std::hex; break; case d: os std::dec; break; case o: os std::oct; break; }; } } os t; os.flags(flgs); } template class Os, class T, std::enable_if_tstd::is_enumT::value _has_stream_bit_shlOs, typename std::underlying_typeT::type const::value, int 0 static void to_stream(Os os, T const t, std::string_view fms) { os std::underlying_type_tT{t}; } }; template class Os, class T void to_stream(Os os, T const t, std::string_view fms) { _to_stream_impl::to_stream(os, t, fms); } template class T std::string to_string(T const t, std::string_view fms) { std::ostringstream ss; to_stream(ss, t, fms); return ss.str(); } template class T std::string to_string(T const t) { if constexpr (std::is_convertible_vT, std::string) { return t; } else { std::ostringstream ss; to_stream(ss, t, {}); return ss.str(); } } template int curr, class Os, class It, class ...Args bool __format(Os os, It fb, It fe, std::tupleArgs const ... const args) { if constexpr (curr sizeof...(Args)) { if (fb ! fe) os std::string_view(fb, fe - fb); return curr sizeof...(Args); } else { if (fb fe) return false; auto ib std::find(fb, fe, {); if (ib fe) return false; os std::string_view(fb, ib - fb); ib; auto ie std::find(ib, fe, }); if (ie fe) return false; ie; auto fms std::string_view(ib, ie - ib); if (auto i fms.find(:); i ! std::string_view::npos) { fms fms.substr(i 1); } to_stream(os, std::getcurr(args), fms); return __formatcurr 1(os, ie, fe, args); } } template class Os, class ...Args bool format_to(Os os, std::string_view fmt, Args const ...args) { return __format0(os, fmt.data(), fmt.data() fmt.size(), std::tupleArgs const ...(args...)); } template class ...Args std::string format(std::string_view fmt, Args const ...args) { std::stringstream ss; format_to(ss, fmt, args...); return ss.str(); } void test() { std::cout format(add({}{}){}\n, 1, 2, 3); }C#include cstdio #include cstring #include cstdlib #include stdarg.h #include string template class ...Ts std::string cformat(const char *fmt, Ts ...ts) { int n snprintf(nullptr, 0, fmt, std::forwardTs(ts)...); if (n 0) return {}; std::string res; res.resize(n 2); n snprintf(res.data(), n 1, fmt, std::forwardTs(ts)...); res.resize(n); return res; }输出 add(12)3创作不易小小的支持一下吧
返回列表