1 // Formatting library for C++ - experimental format string compilation
2 //
3 // Copyright (c) 2012 - present, Victor Zverovich and fmt contributors
4 // All rights reserved.
5 //
6 // For the license information refer to format.h.
7
8 #ifndef FMT_COMPILE_H_
9 #define FMT_COMPILE_H_
10
11 #include <vector>
12
13 #include "format.h"
14
15 FMT_BEGIN_NAMESPACE
16 namespace detail {
17
18 // A compile-time string which is compiled into fast formatting code.
19 class compiled_string {};
20
21 template <typename S>
22 struct is_compiled_string : std::is_base_of<compiled_string, S> {};
23
24 /**
25 \rst
26 Converts a string literal *s* into a format string that will be parsed at
27 compile time and converted into efficient formatting code. Requires C++17
28 ``constexpr if`` compiler support.
29
30 **Example**::
31
32 // Converts 42 into std::string using the most efficient method and no
33 // runtime format string processing.
34 std::string s = fmt::format(FMT_COMPILE("{}"), 42);
35 \endrst
36 */
37 #define FMT_COMPILE(s) FMT_STRING_IMPL(s, fmt::detail::compiled_string)
38
39 template <typename T, typename... Tail>
first(const T & value,const Tail &...)40 const T& first(const T& value, const Tail&...) {
41 return value;
42 }
43
44 // Part of a compiled format string. It can be either literal text or a
45 // replacement field.
46 template <typename Char> struct format_part {
47 enum class kind { arg_index, arg_name, text, replacement };
48
49 struct replacement {
50 arg_ref<Char> arg_id;
51 dynamic_format_specs<Char> specs;
52 };
53
54 kind part_kind;
55 union value {
56 int arg_index;
57 basic_string_view<Char> str;
58 replacement repl;
59
arg_index(index)60 FMT_CONSTEXPR value(int index = 0) : arg_index(index) {}
value(basic_string_view<Char> s)61 FMT_CONSTEXPR value(basic_string_view<Char> s) : str(s) {}
value(replacement r)62 FMT_CONSTEXPR value(replacement r) : repl(r) {}
63 } val;
64 // Position past the end of the argument id.
65 const Char* arg_id_end = nullptr;
66
67 FMT_CONSTEXPR format_part(kind k = kind::arg_index, value v = {})
part_kindformat_part68 : part_kind(k), val(v) {}
69
make_arg_indexformat_part70 static FMT_CONSTEXPR format_part make_arg_index(int index) {
71 return format_part(kind::arg_index, index);
72 }
make_arg_nameformat_part73 static FMT_CONSTEXPR format_part make_arg_name(basic_string_view<Char> name) {
74 return format_part(kind::arg_name, name);
75 }
make_textformat_part76 static FMT_CONSTEXPR format_part make_text(basic_string_view<Char> text) {
77 return format_part(kind::text, text);
78 }
make_replacementformat_part79 static FMT_CONSTEXPR format_part make_replacement(replacement repl) {
80 return format_part(kind::replacement, repl);
81 }
82 };
83
84 template <typename Char> struct part_counter {
85 unsigned num_parts = 0;
86
on_textpart_counter87 FMT_CONSTEXPR void on_text(const Char* begin, const Char* end) {
88 if (begin != end) ++num_parts;
89 }
90
on_arg_idpart_counter91 FMT_CONSTEXPR int on_arg_id() { return ++num_parts, 0; }
on_arg_idpart_counter92 FMT_CONSTEXPR int on_arg_id(int) { return ++num_parts, 0; }
on_arg_idpart_counter93 FMT_CONSTEXPR int on_arg_id(basic_string_view<Char>) {
94 return ++num_parts, 0;
95 }
96
on_replacement_fieldpart_counter97 FMT_CONSTEXPR void on_replacement_field(int, const Char*) {}
98
on_format_specspart_counter99 FMT_CONSTEXPR const Char* on_format_specs(int, const Char* begin,
100 const Char* end) {
101 // Find the matching brace.
102 unsigned brace_counter = 0;
103 for (; begin != end; ++begin) {
104 if (*begin == '{') {
105 ++brace_counter;
106 } else if (*begin == '}') {
107 if (brace_counter == 0u) break;
108 --brace_counter;
109 }
110 }
111 return begin;
112 }
113
on_errorpart_counter114 FMT_CONSTEXPR void on_error(const char*) {}
115 };
116
117 // Counts the number of parts in a format string.
118 template <typename Char>
count_parts(basic_string_view<Char> format_str)119 FMT_CONSTEXPR unsigned count_parts(basic_string_view<Char> format_str) {
120 part_counter<Char> counter;
121 parse_format_string<true>(format_str, counter);
122 return counter.num_parts;
123 }
124
125 template <typename Char, typename PartHandler>
126 class format_string_compiler : public error_handler {
127 private:
128 using part = format_part<Char>;
129
130 PartHandler handler_;
131 part part_;
132 basic_string_view<Char> format_str_;
133 basic_format_parse_context<Char> parse_context_;
134
135 public:
format_string_compiler(basic_string_view<Char> format_str,PartHandler handler)136 FMT_CONSTEXPR format_string_compiler(basic_string_view<Char> format_str,
137 PartHandler handler)
138 : handler_(handler),
139 format_str_(format_str),
140 parse_context_(format_str) {}
141
on_text(const Char * begin,const Char * end)142 FMT_CONSTEXPR void on_text(const Char* begin, const Char* end) {
143 if (begin != end)
144 handler_(part::make_text({begin, to_unsigned(end - begin)}));
145 }
146
on_arg_id()147 FMT_CONSTEXPR int on_arg_id() {
148 part_ = part::make_arg_index(parse_context_.next_arg_id());
149 return 0;
150 }
151
on_arg_id(int id)152 FMT_CONSTEXPR int on_arg_id(int id) {
153 parse_context_.check_arg_id(id);
154 part_ = part::make_arg_index(id);
155 return 0;
156 }
157
on_arg_id(basic_string_view<Char> id)158 FMT_CONSTEXPR int on_arg_id(basic_string_view<Char> id) {
159 part_ = part::make_arg_name(id);
160 return 0;
161 }
162
on_replacement_field(int,const Char * ptr)163 FMT_CONSTEXPR void on_replacement_field(int, const Char* ptr) {
164 part_.arg_id_end = ptr;
165 handler_(part_);
166 }
167
on_format_specs(int,const Char * begin,const Char * end)168 FMT_CONSTEXPR const Char* on_format_specs(int, const Char* begin,
169 const Char* end) {
170 auto repl = typename part::replacement();
171 dynamic_specs_handler<basic_format_parse_context<Char>> handler(
172 repl.specs, parse_context_);
173 auto it = parse_format_specs(begin, end, handler);
174 if (*it != '}') on_error("missing '}' in format string");
175 repl.arg_id = part_.part_kind == part::kind::arg_index
176 ? arg_ref<Char>(part_.val.arg_index)
177 : arg_ref<Char>(part_.val.str);
178 auto part = part::make_replacement(repl);
179 part.arg_id_end = begin;
180 handler_(part);
181 return it;
182 }
183 };
184
185 // Compiles a format string and invokes handler(part) for each parsed part.
186 template <bool IS_CONSTEXPR, typename Char, typename PartHandler>
compile_format_string(basic_string_view<Char> format_str,PartHandler handler)187 FMT_CONSTEXPR void compile_format_string(basic_string_view<Char> format_str,
188 PartHandler handler) {
189 parse_format_string<IS_CONSTEXPR>(
190 format_str,
191 format_string_compiler<Char, PartHandler>(format_str, handler));
192 }
193
194 template <typename OutputIt, typename Context, typename Id>
format_arg(basic_format_parse_context<typename Context::char_type> & parse_ctx,Context & ctx,Id arg_id)195 void format_arg(
196 basic_format_parse_context<typename Context::char_type>& parse_ctx,
197 Context& ctx, Id arg_id) {
198 ctx.advance_to(visit_format_arg(
199 arg_formatter<OutputIt, typename Context::char_type>(ctx, &parse_ctx),
200 ctx.arg(arg_id)));
201 }
202
203 // vformat_to is defined in a subnamespace to prevent ADL.
204 namespace cf {
205 template <typename Context, typename OutputIt, typename CompiledFormat>
206 auto vformat_to(OutputIt out, CompiledFormat& cf,
207 basic_format_args<Context> args) -> typename Context::iterator {
208 using char_type = typename Context::char_type;
209 basic_format_parse_context<char_type> parse_ctx(
210 to_string_view(cf.format_str_));
211 Context ctx(out, args);
212
213 const auto& parts = cf.parts();
214 for (auto part_it = std::begin(parts); part_it != std::end(parts);
215 ++part_it) {
216 const auto& part = *part_it;
217 const auto& value = part.val;
218
219 using format_part_t = format_part<char_type>;
220 switch (part.part_kind) {
221 case format_part_t::kind::text: {
222 const auto text = value.str;
223 auto output = ctx.out();
224 auto&& it = reserve(output, text.size());
225 it = std::copy_n(text.begin(), text.size(), it);
226 ctx.advance_to(output);
227 break;
228 }
229
230 case format_part_t::kind::arg_index:
231 advance_to(parse_ctx, part.arg_id_end);
232 detail::format_arg<OutputIt>(parse_ctx, ctx, value.arg_index);
233 break;
234
235 case format_part_t::kind::arg_name:
236 advance_to(parse_ctx, part.arg_id_end);
237 detail::format_arg<OutputIt>(parse_ctx, ctx, value.str);
238 break;
239
240 case format_part_t::kind::replacement: {
241 const auto& arg_id_value = value.repl.arg_id.val;
242 const auto arg = value.repl.arg_id.kind == arg_id_kind::index
243 ? ctx.arg(arg_id_value.index)
244 : ctx.arg(arg_id_value.name);
245
246 auto specs = value.repl.specs;
247
248 handle_dynamic_spec<width_checker>(specs.width, specs.width_ref, ctx);
249 handle_dynamic_spec<precision_checker>(specs.precision,
250 specs.precision_ref, ctx);
251
252 error_handler h;
253 numeric_specs_checker<error_handler> checker(h, arg.type());
254 if (specs.align == align::numeric) checker.require_numeric_argument();
255 if (specs.sign != sign::none) checker.check_sign();
256 if (specs.alt) checker.require_numeric_argument();
257 if (specs.precision >= 0) checker.check_precision();
258
259 advance_to(parse_ctx, part.arg_id_end);
260 ctx.advance_to(
261 visit_format_arg(arg_formatter<OutputIt, typename Context::char_type>(
262 ctx, nullptr, &specs),
263 arg));
264 break;
265 }
266 }
267 }
268 return ctx.out();
269 }
270 } // namespace cf
271
272 struct basic_compiled_format {};
273
274 template <typename S, typename = void>
275 struct compiled_format_base : basic_compiled_format {
276 using char_type = char_t<S>;
277 using parts_container = std::vector<detail::format_part<char_type>>;
278
279 parts_container compiled_parts;
280
compiled_format_basecompiled_format_base281 explicit compiled_format_base(basic_string_view<char_type> format_str) {
282 compile_format_string<false>(format_str,
283 [this](const format_part<char_type>& part) {
284 compiled_parts.push_back(part);
285 });
286 }
287
partscompiled_format_base288 const parts_container& parts() const { return compiled_parts; }
289 };
290
291 template <typename Char, unsigned N> struct format_part_array {
292 format_part<Char> data[N] = {};
293 FMT_CONSTEXPR format_part_array() = default;
294 };
295
296 template <typename Char, unsigned N>
compile_to_parts(basic_string_view<Char> format_str)297 FMT_CONSTEXPR format_part_array<Char, N> compile_to_parts(
298 basic_string_view<Char> format_str) {
299 format_part_array<Char, N> parts;
300 unsigned counter = 0;
301 // This is not a lambda for compatibility with older compilers.
302 struct {
303 format_part<Char>* parts;
304 unsigned* counter;
305 FMT_CONSTEXPR void operator()(const format_part<Char>& part) {
306 parts[(*counter)++] = part;
307 }
308 } collector{parts.data, &counter};
309 compile_format_string<true>(format_str, collector);
310 if (counter < N) {
311 parts.data[counter] =
312 format_part<Char>::make_text(basic_string_view<Char>());
313 }
314 return parts;
315 }
316
constexpr_max(const T & a,const T & b)317 template <typename T> constexpr const T& constexpr_max(const T& a, const T& b) {
318 return (a < b) ? b : a;
319 }
320
321 template <typename S>
322 struct compiled_format_base<S, enable_if_t<is_compile_string<S>::value>>
323 : basic_compiled_format {
324 using char_type = char_t<S>;
325
326 FMT_CONSTEXPR explicit compiled_format_base(basic_string_view<char_type>) {}
327
328 // Workaround for old compilers. Format string compilation will not be
329 // performed there anyway.
330 #if FMT_USE_CONSTEXPR
331 static FMT_CONSTEXPR_DECL const unsigned num_format_parts =
332 constexpr_max(count_parts(to_string_view(S())), 1u);
333 #else
334 static const unsigned num_format_parts = 1;
335 #endif
336
337 using parts_container = format_part<char_type>[num_format_parts];
338
339 const parts_container& parts() const {
340 static FMT_CONSTEXPR_DECL const auto compiled_parts =
341 compile_to_parts<char_type, num_format_parts>(
342 detail::to_string_view(S()));
343 return compiled_parts.data;
344 }
345 };
346
347 template <typename S, typename... Args>
348 class compiled_format : private compiled_format_base<S> {
349 public:
350 using typename compiled_format_base<S>::char_type;
351
352 private:
353 basic_string_view<char_type> format_str_;
354
355 template <typename Context, typename OutputIt, typename CompiledFormat>
356 friend auto cf::vformat_to(OutputIt out, CompiledFormat& cf,
357 basic_format_args<Context> args) ->
358 typename Context::iterator;
359
360 public:
361 compiled_format() = delete;
362 explicit constexpr compiled_format(basic_string_view<char_type> format_str)
363 : compiled_format_base<S>(format_str), format_str_(format_str) {}
364 };
365
366 #ifdef __cpp_if_constexpr
367 template <typename... Args> struct type_list {};
368
369 // Returns a reference to the argument at index N from [first, rest...].
370 template <int N, typename T, typename... Args>
371 constexpr const auto& get([[maybe_unused]] const T& first,
372 [[maybe_unused]] const Args&... rest) {
373 static_assert(N < 1 + sizeof...(Args), "index is out of bounds");
374 if constexpr (N == 0)
375 return first;
376 else
377 return get<N - 1>(rest...);
378 }
379
380 template <int N, typename> struct get_type_impl;
381
382 template <int N, typename... Args> struct get_type_impl<N, type_list<Args...>> {
383 using type = remove_cvref_t<decltype(get<N>(std::declval<Args>()...))>;
384 };
385
386 template <int N, typename T>
387 using get_type = typename get_type_impl<N, T>::type;
388
389 template <typename T> struct is_compiled_format : std::false_type {};
390
391 template <typename Char> struct text {
392 basic_string_view<Char> data;
393 using char_type = Char;
394
395 template <typename OutputIt, typename... Args>
396 OutputIt format(OutputIt out, const Args&...) const {
397 return write<Char>(out, data);
398 }
399 };
400
401 template <typename Char>
402 struct is_compiled_format<text<Char>> : std::true_type {};
403
404 template <typename Char>
405 constexpr text<Char> make_text(basic_string_view<Char> s, size_t pos,
406 size_t size) {
407 return {{&s[pos], size}};
408 }
409
410 template <typename Char> struct code_unit {
411 Char value;
412 using char_type = Char;
413
414 template <typename OutputIt, typename... Args>
415 OutputIt format(OutputIt out, const Args&...) const {
416 return write<Char>(out, value);
417 }
418 };
419
420 template <typename Char>
421 struct is_compiled_format<code_unit<Char>> : std::true_type {};
422
423 // A replacement field that refers to argument N.
424 template <typename Char, typename T, int N> struct field {
425 using char_type = Char;
426
427 template <typename OutputIt, typename... Args>
428 OutputIt format(OutputIt out, const Args&... args) const {
429 // This ensures that the argument type is convertile to `const T&`.
430 const T& arg = get<N>(args...);
431 return write<Char>(out, arg);
432 }
433 };
434
435 template <typename Char, typename T, int N>
436 struct is_compiled_format<field<Char, T, N>> : std::true_type {};
437
438 // A replacement field that refers to argument N and has format specifiers.
439 template <typename Char, typename T, int N> struct spec_field {
440 using char_type = Char;
441 mutable formatter<T, Char> fmt;
442
443 template <typename OutputIt, typename... Args>
444 OutputIt format(OutputIt out, const Args&... args) const {
445 // This ensures that the argument type is convertile to `const T&`.
446 const T& arg = get<N>(args...);
447 const auto& vargs =
448 make_format_args<basic_format_context<OutputIt, Char>>(args...);
449 basic_format_context<OutputIt, Char> ctx(out, vargs);
450 return fmt.format(arg, ctx);
451 }
452 };
453
454 template <typename Char, typename T, int N>
455 struct is_compiled_format<spec_field<Char, T, N>> : std::true_type {};
456
457 template <typename L, typename R> struct concat {
458 L lhs;
459 R rhs;
460 using char_type = typename L::char_type;
461
462 template <typename OutputIt, typename... Args>
463 OutputIt format(OutputIt out, const Args&... args) const {
464 out = lhs.format(out, args...);
465 return rhs.format(out, args...);
466 }
467 };
468
469 template <typename L, typename R>
470 struct is_compiled_format<concat<L, R>> : std::true_type {};
471
472 template <typename L, typename R>
473 constexpr concat<L, R> make_concat(L lhs, R rhs) {
474 return {lhs, rhs};
475 }
476
477 struct unknown_format {};
478
479 template <typename Char>
480 constexpr size_t parse_text(basic_string_view<Char> str, size_t pos) {
481 for (size_t size = str.size(); pos != size; ++pos) {
482 if (str[pos] == '{' || str[pos] == '}') break;
483 }
484 return pos;
485 }
486
487 template <typename Args, size_t POS, int ID, typename S>
488 constexpr auto compile_format_string(S format_str);
489
490 template <typename Args, size_t POS, int ID, typename T, typename S>
491 constexpr auto parse_tail(T head, S format_str) {
492 if constexpr (POS !=
493 basic_string_view<typename S::char_type>(format_str).size()) {
494 constexpr auto tail = compile_format_string<Args, POS, ID>(format_str);
495 if constexpr (std::is_same<remove_cvref_t<decltype(tail)>,
496 unknown_format>())
497 return tail;
498 else
499 return make_concat(head, tail);
500 } else {
501 return head;
502 }
503 }
504
505 template <typename T, typename Char> struct parse_specs_result {
506 formatter<T, Char> fmt;
507 size_t end;
508 int next_arg_id;
509 };
510
511 template <typename T, typename Char>
512 constexpr parse_specs_result<T, Char> parse_specs(basic_string_view<Char> str,
513 size_t pos, int arg_id) {
514 str.remove_prefix(pos);
515 auto ctx = basic_format_parse_context<Char>(str, {}, arg_id + 1);
516 auto f = formatter<T, Char>();
517 auto end = f.parse(ctx);
518 return {f, pos + (end - str.data()) + 1, ctx.next_arg_id()};
519 }
520
521 // Compiles a non-empty format string and returns the compiled representation
522 // or unknown_format() on unrecognized input.
523 template <typename Args, size_t POS, int ID, typename S>
524 constexpr auto compile_format_string(S format_str) {
525 using char_type = typename S::char_type;
526 constexpr basic_string_view<char_type> str = format_str;
527 if constexpr (str[POS] == '{') {
528 if (POS + 1 == str.size())
529 throw format_error("unmatched '{' in format string");
530 if constexpr (str[POS + 1] == '{') {
531 return parse_tail<Args, POS + 2, ID>(make_text(str, POS, 1), format_str);
532 } else if constexpr (str[POS + 1] == '}') {
533 using type = get_type<ID, Args>;
534 return parse_tail<Args, POS + 2, ID + 1>(field<char_type, type, ID>(),
535 format_str);
536 } else if constexpr (str[POS + 1] == ':') {
537 using type = get_type<ID, Args>;
538 constexpr auto result = parse_specs<type>(str, POS + 2, ID);
539 return parse_tail<Args, result.end, result.next_arg_id>(
540 spec_field<char_type, type, ID>{result.fmt}, format_str);
541 } else {
542 return unknown_format();
543 }
544 } else if constexpr (str[POS] == '}') {
545 if (POS + 1 == str.size())
546 throw format_error("unmatched '}' in format string");
547 return parse_tail<Args, POS + 2, ID>(make_text(str, POS, 1), format_str);
548 } else {
549 constexpr auto end = parse_text(str, POS + 1);
550 if constexpr (end - POS > 1) {
551 return parse_tail<Args, end, ID>(make_text(str, POS, end - POS),
552 format_str);
553 } else {
554 return parse_tail<Args, end, ID>(code_unit<char_type>{str[POS]},
555 format_str);
556 }
557 }
558 }
559
560 template <typename... Args, typename S,
561 FMT_ENABLE_IF(is_compile_string<S>::value ||
562 detail::is_compiled_string<S>::value)>
563 constexpr auto compile(S format_str) {
564 constexpr basic_string_view<typename S::char_type> str = format_str;
565 if constexpr (str.size() == 0) {
566 return detail::make_text(str, 0, 0);
567 } else {
568 constexpr auto result =
569 detail::compile_format_string<detail::type_list<Args...>, 0, 0>(
570 format_str);
571 if constexpr (std::is_same<remove_cvref_t<decltype(result)>,
572 detail::unknown_format>()) {
573 return detail::compiled_format<S, Args...>(to_string_view(format_str));
574 } else {
575 return result;
576 }
577 }
578 }
579 #else
580 template <typename... Args, typename S,
581 FMT_ENABLE_IF(is_compile_string<S>::value)>
582 constexpr auto compile(S format_str) -> detail::compiled_format<S, Args...> {
583 return detail::compiled_format<S, Args...>(to_string_view(format_str));
584 }
585 #endif // __cpp_if_constexpr
586
587 // Compiles the format string which must be a string literal.
588 template <typename... Args, typename Char, size_t N>
589 auto compile(const Char (&format_str)[N])
590 -> detail::compiled_format<const Char*, Args...> {
591 return detail::compiled_format<const Char*, Args...>(
592 basic_string_view<Char>(format_str, N - 1));
593 }
594 } // namespace detail
595
596 // DEPRECATED! use FMT_COMPILE instead.
597 template <typename... Args>
598 FMT_DEPRECATED auto compile(const Args&... args)
599 -> decltype(detail::compile(args...)) {
600 return detail::compile(args...);
601 }
602
603 #if FMT_USE_CONSTEXPR
604 # ifdef __cpp_if_constexpr
605
606 template <typename CompiledFormat, typename... Args,
607 typename Char = typename CompiledFormat::char_type,
608 FMT_ENABLE_IF(detail::is_compiled_format<CompiledFormat>::value)>
609 FMT_INLINE std::basic_string<Char> format(const CompiledFormat& cf,
610 const Args&... args) {
611 basic_memory_buffer<Char> buffer;
612 cf.format(detail::buffer_appender<Char>(buffer), args...);
613 return to_string(buffer);
614 }
615
616 template <typename OutputIt, typename CompiledFormat, typename... Args,
617 FMT_ENABLE_IF(detail::is_compiled_format<CompiledFormat>::value)>
618 OutputIt format_to(OutputIt out, const CompiledFormat& cf,
619 const Args&... args) {
620 return cf.format(out, args...);
621 }
622 # endif // __cpp_if_constexpr
623 #endif // FMT_USE_CONSTEXPR
624
625 template <typename CompiledFormat, typename... Args,
626 typename Char = typename CompiledFormat::char_type,
627 FMT_ENABLE_IF(std::is_base_of<detail::basic_compiled_format,
628 CompiledFormat>::value)>
629 std::basic_string<Char> format(const CompiledFormat& cf, const Args&... args) {
630 basic_memory_buffer<Char> buffer;
631 using context = buffer_context<Char>;
632 detail::cf::vformat_to<context>(detail::buffer_appender<Char>(buffer), cf,
633 make_format_args<context>(args...));
634 return to_string(buffer);
635 }
636
637 template <typename S, typename... Args,
638 FMT_ENABLE_IF(detail::is_compiled_string<S>::value)>
639 FMT_INLINE std::basic_string<typename S::char_type> format(const S&,
640 Args&&... args) {
641 #ifdef __cpp_if_constexpr
642 if constexpr (std::is_same<typename S::char_type, char>::value) {
643 constexpr basic_string_view<typename S::char_type> str = S();
644 if (str.size() == 2 && str[0] == '{' && str[1] == '}')
645 return fmt::to_string(detail::first(args...));
646 }
647 #endif
648 constexpr auto compiled = detail::compile<Args...>(S());
649 return format(compiled, std::forward<Args>(args)...);
650 }
651
652 template <typename OutputIt, typename CompiledFormat, typename... Args,
653 FMT_ENABLE_IF(std::is_base_of<detail::basic_compiled_format,
654 CompiledFormat>::value)>
655 OutputIt format_to(OutputIt out, const CompiledFormat& cf,
656 const Args&... args) {
657 using char_type = typename CompiledFormat::char_type;
658 using context = format_context_t<OutputIt, char_type>;
659 return detail::cf::vformat_to<context>(out, cf,
660 make_format_args<context>(args...));
661 }
662
663 template <typename OutputIt, typename S, typename... Args,
664 FMT_ENABLE_IF(detail::is_compiled_string<S>::value)>
665 OutputIt format_to(OutputIt out, const S&, const Args&... args) {
666 constexpr auto compiled = detail::compile<Args...>(S());
667 return format_to(out, compiled, args...);
668 }
669
670 template <typename OutputIt, typename CompiledFormat, typename... Args>
671 auto format_to_n(OutputIt out, size_t n, const CompiledFormat& cf,
672 const Args&... args) ->
673 typename std::enable_if<
674 detail::is_output_iterator<OutputIt,
675 typename CompiledFormat::char_type>::value &&
676 std::is_base_of<detail::basic_compiled_format,
677 CompiledFormat>::value,
678 format_to_n_result<OutputIt>>::type {
679 auto it =
680 format_to(detail::truncating_iterator<OutputIt>(out, n), cf, args...);
681 return {it.base(), it.count()};
682 }
683
684 template <typename OutputIt, typename S, typename... Args,
685 FMT_ENABLE_IF(detail::is_compiled_string<S>::value)>
686 format_to_n_result<OutputIt> format_to_n(OutputIt out, size_t n, const S&,
687 const Args&... args) {
688 constexpr auto compiled = detail::compile<Args...>(S());
689 auto it = format_to(detail::truncating_iterator<OutputIt>(out, n), compiled,
690 args...);
691 return {it.base(), it.count()};
692 }
693
694 template <typename CompiledFormat, typename... Args>
695 size_t formatted_size(const CompiledFormat& cf, const Args&... args) {
696 return format_to(detail::counting_iterator(), cf, args...).count();
697 }
698
699 FMT_END_NAMESPACE
700
701 #endif // FMT_COMPILE_H_
702