|
| 1 | +#ifndef CTFMT__TO_STRING__HPP |
| 2 | +#define CTFMT__TO_STRING__HPP |
| 3 | + |
| 4 | +#include "utility.hpp" |
| 5 | +#include "../ctll/parser.hpp" |
| 6 | +#include "fmt.hpp" |
| 7 | +#include "actions.hpp" |
| 8 | +#include <string_view> |
| 9 | +#include <string> |
| 10 | +#include <tuple> |
| 11 | +#include "utility.hpp" |
| 12 | +#include <cassert> |
| 13 | +#include <array> |
| 14 | + |
| 15 | +namespace ctfmt { |
| 16 | + |
| 17 | +struct buffer_wrap { |
| 18 | + |
| 19 | +}; |
| 20 | + |
| 21 | +struct view_wrap { |
| 22 | + std::string_view view; |
| 23 | + constexpr view_wrap(std::string_view view) noexcept: view{view} { } |
| 24 | + constexpr size_t size() const noexcept { |
| 25 | + return view.size(); |
| 26 | + } |
| 27 | + constexpr auto begin() const noexcept { |
| 28 | + return view.begin(); |
| 29 | + } |
| 30 | + constexpr auto end() const noexcept { |
| 31 | + return view.end(); |
| 32 | + } |
| 33 | +}; |
| 34 | + |
| 35 | +CTLL_FORCE_INLINE constexpr view_wrap input_wrap(const char * str) { |
| 36 | + return view_wrap{std::string_view(str)}; |
| 37 | +} |
| 38 | + |
| 39 | +CTLL_FORCE_INLINE constexpr view_wrap input_wrap(std::string_view v) { |
| 40 | + return view_wrap{v}; |
| 41 | +} |
| 42 | + |
| 43 | +CTLL_FORCE_INLINE inline view_wrap input_wrap(const std::string & s) { |
| 44 | + return view_wrap{std::string_view(s)}; |
| 45 | +} |
| 46 | + |
| 47 | +template <typename... Seq, typename... Args> CTFMT_FORCE_INLINE auto calculate_size_with_impl(sequence<Seq...>, const std::tuple<Args...> & args) { |
| 48 | + return (calculate_size(Seq(), args) + ... + 0); |
| 49 | +} |
| 50 | + |
| 51 | +template <typename Seq, typename... Args> CTFMT_FORCE_INLINE auto calculate_size_with(const std::tuple<Args...> & args) { |
| 52 | + return calculate_size_with_impl(Seq(), args); |
| 53 | +} |
| 54 | + |
| 55 | +template <typename... Seq, typename It, typename... Args> CTFMT_FORCE_INLINE void format_with_impl(sequence<Seq...>, It begin, It end, const std::tuple<Args ...> & args) { |
| 56 | + (format_into(Seq(), begin, end, args), ...); |
| 57 | +} |
| 58 | + |
| 59 | +template <typename Seq, typename It, typename... Args> CTFMT_FORCE_INLINE void format_with(It begin, It end, const std::tuple<Args ...> & args) { |
| 60 | + format_with_impl(Seq(), begin, end, args); |
| 61 | +} |
| 62 | + |
| 63 | +template <typename Seq> struct format_object { |
| 64 | + template <typename... Args> CTFMT_FORCE_INLINE auto format(std::tuple<Args...> args) { |
| 65 | + std::string output; |
| 66 | + output.resize(calculate_size_with<Seq>(args)); |
| 67 | + format_with<Seq>(output.begin(), output.end(), args); |
| 68 | + return output; |
| 69 | + } |
| 70 | + template <size_t Size, typename... Args> CTFMT_FORCE_INLINE auto format(std::array<char, Size> & buffer, std::tuple<Args...> args) { |
| 71 | + size_t size = calculate_size_with<Seq>(args); |
| 72 | + assert(Size >= size); |
| 73 | + format_with<Seq>(buffer.begin(), buffer.end(), args); |
| 74 | + return std::string_view(buffer.begin(), size); |
| 75 | + } |
| 76 | + template <typename... Args> CTFMT_FORCE_INLINE auto operator()(Args && ... args) { |
| 77 | + return format(std::make_tuple(input_wrap(std::forward<Args>(args))...)); |
| 78 | + } |
| 79 | + template <size_t Size, typename... Args> CTFMT_FORCE_INLINE auto operator()(std::array<char, Size> & buffer, Args && ... args) { |
| 80 | + return format(buffer, std::make_tuple(input_wrap(std::forward<Args>(args))...)); |
| 81 | + } |
| 82 | +}; |
| 83 | + |
| 84 | +#if __cpp_nontype_template_parameter_class |
| 85 | +template <ctll::basic_fixed_string input> CTFMT_FLATTEN constexpr auto format() noexcept { |
| 86 | + constexpr auto _input = input; |
| 87 | +#else |
| 88 | +template <auto & input> CTFMT_FLATTEN constexpr auto format() noexcept { |
| 89 | + constexpr auto & _input = input; |
| 90 | +#endif |
| 91 | + |
| 92 | + //using tmp = typename ctll::parser<ctfmt::fmt, _input, ctfmt::actions>::template output<ctll::list<>>; |
| 93 | + using tmp = typename ctll::parser<ctfmt::fmt, _input, ctfmt::actions>::template output<ctll::list<sequence<>>>; |
| 94 | + static_assert(tmp(), "Format Expression contains syntax error."); |
| 95 | + return format_object<decltype(ctll::front(typename tmp::output_type()))>(); |
| 96 | +} |
| 97 | + |
| 98 | +} |
| 99 | + |
| 100 | +#endif |
0 commit comments