tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
format_to.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2
3#ifndef TETL_FORMAT_FORMAT_TO_HPP
4#define TETL_FORMAT_FORMAT_TO_HPP
5
10
11namespace etl {
12template <typename Iter>
13using diff_t = typename etl::iterator_traits<etl::remove_cvref_t<Iter>>::difference_type;
14
19template <typename OutputIt, typename... Args>
20auto format_to(OutputIt out, etl::string_view fmt, Args const&... args) -> OutputIt
21{
22 auto ctx = format_context{out};
23
24 // Format leading text before the first argument.
25 auto const slices = detail::split_at_next_argument(fmt);
26 detail::format_escaped_sequences(slices.first, ctx);
27
28 // Save rest of format string. Supress warning if format_to was called
29 // without arguments.
30 auto rest = slices.second;
32
33 ([&] {
34 // Format argument
35 detail::format_argument(args, ctx);
36
37 // Split format text at next argument
38 auto const restSlices = detail::split_at_next_argument(rest);
39 detail::format_escaped_sequences(restSlices.first, ctx);
40
41 // Save rest of format string for the next arguments
42 rest = restSlices.second;
43 }(), ...);
44
45 // Anything left over after the last argument.
46 if (auto const trailing = detail::split_at_next_argument(rest); !trailing.first.empty()) {
47 detail::format_escaped_sequences(trailing.first, ctx);
48 TETL_ASSERT(trailing.second.empty());
49 }
50
51 return ctx.out();
52}
53
58template <typename Out>
63
68template <typename OutputIter, typename... Args>
69auto format_to_n(OutputIter out, diff_t<OutputIter> n, etl::string_view fmt, Args const&... args)
71{
73
74 auto indices = etl::static_vector<etl::size_t, sizeof...(args)>{};
75 auto result = format_to_n_result<OutputIter>{out, {}};
76
77 auto writeChar = [&result](auto ch) {
78 *result.out++ = ch;
79 result.size++;
80 };
81
82 auto varStart = etl::size_t{};
83 for (decltype(fmt)::size_type i{}; i < fmt.size(); ++i) {
84 auto ch = fmt[i];
85 if (ch == '{') {
86 if ((fmt.size() > i + 1) && (fmt[i + 1] == '{')) {
87 ++i;
88 writeChar('{');
89 continue;
90 }
91
92 varStart = i;
93 continue;
94 }
95
96 if (ch == '}') {
97 if ((fmt.size() > i + 1) && (fmt[i + 1] == '}')) {
98 ++i;
99 writeChar('}');
100 continue;
101 }
102
103 indices.push_back(varStart);
104 writeChar('0');
105 continue;
106 }
107
108 writeChar(ch);
109 }
110
111 if (indices.size() > 0) {
112 [[maybe_unused]] auto replaceCharAt = [n](auto output, auto pos, char val) {
114 // TETL_ASSERT((long)pos < n);
115 output[pos] = val;
116 };
117
118 [[maybe_unused]] typename decltype(indices)::size_type i{};
119 (replaceCharAt(out, indices[i++], args), ...);
120 }
121
122 return result;
123}
124} // namespace etl
125
126#endif // TETL_FORMAT_FORMAT_TO_HPP
#define TETL_ASSERT(...)
Definition assert.hpp:63
basic_string_view< char, etl::char_traits< char > > string_view
Typedef for common character type char
Definition basic_string_view.hpp:704
Definition adjacent_find.hpp:8
typename etl::iterator_traits< etl::remove_cvref_t< Iter > >::difference_type diff_t
Definition format_to.hpp:13
auto format_to(OutputIt out, etl::string_view fmt, Args const &... args) -> OutputIt
Format args according to the format string fmt, and write the result to the output iterator out.
Definition format_to.hpp:20
constexpr auto ignore_unused(Types &&...) -> void
Explicitly ignore arguments or variables.
Definition ignore_unused.hpp:17
TETL_BUILTIN_SIZET size_t
etl::size_t is the unsigned integer type of the result of the sizeof operator.
Definition size_t.hpp:14
auto format_to_n(OutputIter out, diff_t< OutputIter > n, etl::string_view fmt, Args const &... args) -> format_to_n_result< OutputIter >
Format args according to the format string fmt, and write the result to the output iterator out....
Definition format_to.hpp:69
basic_format_context< back_insert_iterator< detail::fmt_buffer< char > >, char > format_context
Provides access to formatting state consisting of the formatting arguments and the output iterator.
Definition basic_format_context.hpp:55
etl::format_to_n_result has no base classes, or members other than out, size and implicitly declared ...
Definition format_to.hpp:59
Out out
Definition format_to.hpp:60
diff_t< Out > size
Definition format_to.hpp:61
iterator_traits is the type trait class that provides uniform interface to the properties of LegacyIt...
Definition iterator_traits.hpp:47
Dynamically-resizable fixed-capacity vector.
Definition static_vector.hpp:329