/** This file is a part of rexy's general purpose library Copyright (C) 2022 rexy712 This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #ifndef REXY_FORMAT_TPP #define REXY_FORMAT_TPP #include "detail/format/format_args.tpp" #include "detail/format/formatter.tpp" #include "detail/format/storage.tpp" #include "detail/format/format_string.tpp" #include "detail/format/arg_store.tpp" #include "detail/format/output_buffer.tpp" #include "detail/format/basic_types.tpp" #include "detail/format/specs_handler.tpp" #include "detail/format/parse.tpp" #include "detail/format/context_handler.tpp" #include "detail/format/named_args.tpp" #include "detail/format/parse_context.tpp" #include "detail/format/format_context.tpp" #include //back_insert_iterator #include //move, forward #include //is_same #include //locale #include //size_t namespace rexy{ namespace fmt::detail{ template OutIt vformat_to(OutIt out, basic_string_view> fmt, impl_format_args> args){ using char_type = Char; using format_handler_t = fmt::detail::format_handler; if constexpr(std::is_same_v>){ fmt::detail::format_handler handler{out, fmt, args}; fmt::detail::parse::perform_parse(handler, fmt); return std::move(out); }else{ fmt::detail::basic_format_output_buffer outbuf{out}; fmt::detail::format_handler handler{fmt::detail::output_iterator_t{outbuf}, fmt, args}; fmt::detail::parse::perform_parse(handler, fmt); return std::move(outbuf.out()); } } template OutIt vformat_to(OutIt out, const std::locale& loc, basic_string_view> fmt, impl_format_args> args){ using char_type = Char; using format_handler_t = fmt::detail::format_handler; if constexpr(std::is_same_v>){ fmt::detail::format_handler handler{out, fmt, args, loc}; fmt::detail::parse::perform_parse(handler, fmt); return std::move(out); }else{ fmt::detail::basic_format_output_buffer outbuf{out}; fmt::detail::format_handler handler{fmt::detail::output_iterator_t{outbuf}, fmt, args, loc}; fmt::detail::parse::perform_parse(handler, fmt); return std::move(outbuf.out()); } } template basic_format_arg_store,Args...> make_format_args(Args&&... args){ return basic_format_arg_store,Args...>{std::forward(args)...}; } } template fmt::detail::basic_format_arg_store make_format_args(Args&&... args){ return fmt::detail::basic_format_arg_store{std::forward(args)...}; } template fmt::detail::basic_format_arg_store make_wformat_args(Args&&... args){ return fmt::detail::basic_format_arg_store{std::forward(args)...}; } //////////////////////////////////////char overloads/////////////////////////////////////////////// template OutIt vformat_to(OutIt out, string_view fmt, format_args args){ return fmt::detail::vformat_to(std::move(out), fmt, std::move(args)); } template OutIt vformat_to(OutIt out, const std::locale& loc, string_view fmt, format_args args){ return fmt::detail::vformat_to(std::move(out), loc, fmt, std::move(args)); } string vformat(string_view fmt, format_args args){ using outit_t = std::back_insert_iterator; string output; outit_t out{output}; vformat_to(std::move(out), fmt, std::move(args)); return output; } string vformat(const std::locale& loc, string_view fmt, format_args args){ using outit_t = std::back_insert_iterator; string output; outit_t out{output}; vformat_to(std::move(out), loc, fmt, std::move(args)); return output; } template OutIt format_to(OutIt out, fmt::detail::format_string fmt, Args&&... args){ return vformat_to(std::move(out), fmt.str, make_format_args(std::forward(args)...)); } template OutIt format_to(OutIt out, const std::locale& loc, fmt::detail::format_string fmt, Args&&... args){ return vformat_to(std::move(out), loc, fmt.str, make_format_args(std::forward(args)...)); } template format_to_n_result format_to_n(OutIt out, std::size_t max, fmt::detail::format_string fmt, Args&&... args){ fmt::detail::basic_format_output_n_buffer outbuf{std::move(out), max}; vformat_to(fmt::detail::output_iterator_t{outbuf}, fmt.str, make_format_args(std::forward(args)...)); return {outbuf.out(), outbuf.count()}; } template format_to_n_result format_to_n(OutIt out, const std::locale& loc, std::size_t max, fmt::detail::format_string fmt, Args&&... args){ fmt::detail::basic_format_output_n_buffer outbuf{std::move(out), max}; vformat_to(fmt::detail::output_iterator_t{outbuf}, loc, fmt.str, make_format_args(std::forward(args)...)); return {outbuf.out(), outbuf.count()}; } template string format(fmt::detail::format_string fmt, Args&&... args){ using outit_t = std::back_insert_iterator; string output; outit_t out{output}; vformat_to(std::move(out), fmt.str, make_format_args(std::forward(args)...)); return output; } template string format(const std::locale& loc, fmt::detail::format_string fmt, Args&&... args){ using outit_t = std::back_insert_iterator; string output; outit_t out{output}; vformat_to(std::move(out), loc, fmt.str, make_format_args(std::forward(args)...)); return output; } template std::size_t formatted_size(fmt::detail::format_string fmt, Args&&... args){ fmt::detail::basic_format_size_buffer counter; vformat_to(fmt::detail::output_iterator_t{counter}, fmt.str, make_format_args(std::forward(args)...)); return counter.count(); } template std::size_t formatted_size(const std::locale& loc, fmt::detail::format_string fmt, Args&&... args){ fmt::detail::basic_format_size_buffer counter; vformat_to(fmt::detail::output_iterator_t{counter}, loc, fmt.str, make_format_args(std::forward(args)...)); return counter.count(); } //////////////////////////////////////wchar_t overloads/////////////////////////////////////////////// template OutIt vformat_to(OutIt out, wstring_view fmt, wformat_args args){ return fmt::detail::vformat_to(std::move(out), fmt, std::move(args)); } template OutIt vformat_to(OutIt out, const std::locale& loc, wstring_view fmt, wformat_args args){ return fmt::detail::vformat_to(std::move(out), loc, fmt, std::move(args)); } wstring vformat(wstring_view fmt, wformat_args args){ using outit_t = std::back_insert_iterator; wstring output; outit_t out{output}; vformat_to(std::move(out), fmt, std::move(args)); return output; } wstring vformat(const std::locale& loc, wstring_view fmt, wformat_args args){ using outit_t = std::back_insert_iterator; wstring output; outit_t out{output}; vformat_to(std::move(out), loc, fmt, std::move(args)); return output; } template OutIt format_to(OutIt out, fmt::detail::wformat_string fmt, Args&&... args){ return vformat_to(std::move(out), fmt.str, make_wformat_args(std::forward(args)...)); } template OutIt format_to(OutIt out, const std::locale& loc, fmt::detail::wformat_string fmt, Args&&... args){ return vformat_to(std::move(out), loc, fmt.str, make_wformat_args(std::forward(args)...)); } template format_to_n_result format_to_n(OutIt out, std::size_t max, fmt::detail::wformat_string fmt, Args&&... args){ fmt::detail::basic_format_output_n_buffer outbuf{std::move(out), max}; vformat_to(fmt::detail::output_iterator_t{outbuf}, fmt.str, make_wformat_args(std::forward(args)...)); return {outbuf.out(), outbuf.count()}; } template format_to_n_result format_to_n(OutIt out, const std::locale& loc, std::size_t max, fmt::detail::wformat_string fmt, Args&&... args){ fmt::detail::basic_format_output_n_buffer outbuf{std::move(out), max}; vformat_to(fmt::detail::output_iterator_t{outbuf}, loc, fmt.str, make_wformat_args(std::forward(args)...)); return {outbuf.out(), outbuf.count()}; } template wstring format(fmt::detail::wformat_string fmt, Args&&... args){ using outit_t = std::back_insert_iterator; wstring output; outit_t out{output}; vformat_to(out, fmt.str, make_wformat_args(std::forward(args)...)); return output; } template wstring format(const std::locale& loc, fmt::detail::wformat_string fmt, Args&&... args){ using outit_t = std::back_insert_iterator; wstring output; outit_t out{output}; vformat_to(out, fmt.str, make_wformat_args(std::forward(args)...)); return output; } template std::size_t formatted_size(fmt::detail::wformat_string fmt, Args&&... args){ } template std::size_t formatted_size(const std::locale& loc, fmt::detail::wformat_string fmt, Args&&... args); //////////////////////////////////////print char overloads/////////////////////////////////////////////// template std::size_t print(fmt::detail::format_string fmt, Args&&... args){ fmt::detail::basic_format_output_buffer> out{fmt::detail::print_iterator{stdout}}; vformat_to(fmt::detail::output_iterator_t{out}, fmt.str, make_format_args(std::forward(args)...)); return out.count(); } template std::size_t print(FILE* stream, fmt::detail::format_string fmt, Args&&... args){ fmt::detail::basic_format_output_buffer> out{fmt::detail::print_iterator{stream}}; vformat_to(fmt::detail::output_iterator_t{out}, fmt.str, make_format_args(std::forward(args)...)); return out.count(); } template std::size_t println(fmt::detail::format_string fmt, Args&&... args){ fmt::detail::basic_format_output_buffer> out{fmt::detail::print_iterator{stdout}}; vformat_to(fmt::detail::output_iterator_t{out}, fmt.str, make_format_args(std::forward(args)...)); out.push_back('\n'); return out.count(); } template std::size_t println(FILE* stream, fmt::detail::format_string fmt, Args&&... args){ fmt::detail::basic_format_output_buffer> out{fmt::detail::print_iterator{stream}}; vformat_to(fmt::detail::output_iterator_t{out}, fmt.str, make_format_args(std::forward(args)...)); out.push_back('\n'); return out.count(); } std::size_t vprint_unicode(string_view fmt, format_args args){ fmt::detail::basic_format_output_buffer> out{fmt::detail::print_iterator{stdout}}; vformat_to(fmt::detail::output_iterator_t{out}, fmt, args); return out.count(); } std::size_t vprint_unicode(FILE* stream, string_view fmt, format_args args){ fmt::detail::basic_format_output_buffer> out{fmt::detail::print_iterator{stream}}; vformat_to(fmt::detail::output_iterator_t{out}, fmt, args); return out.count(); } //creates a runtime argument from a c string template constexpr auto arg(const Char* name, T&& t){ return fmt::detail::runtime_arg{std::forward(t), name}; } //creates a runtime argument from a string view template constexpr auto arg(rexy::basic_string_view name, T&& t){ return fmt::detail::runtime_arg{std::forward(t), name}; } //create a compile time argument from a string literal template constexpr auto arg(T&& t){ return fmt::detail::static_arg{t}; } inline namespace fmt_literals{ //create a compile time argument from a string literal template constexpr auto operator""_a(void){ return fmt::detail::arg_literal_result{}; } } } #undef REXY_REAL_STRINGIFY #undef REXY_STRINGIFY #undef REXY_THROW_FORMAT_ERROR #endif