tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
formatter.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2
3#ifndef TETL_FORMAT_FORMATTER_HPP
4#define TETL_FORMAT_FORMATTER_HPP
5
12
13namespace etl {
18template <typename T, typename CharT = char>
19struct formatter {
20 formatter() = delete;
21 formatter(formatter const& other) = delete;
22 auto operator=(formatter const& other) -> formatter = delete;
23};
24
26template <>
27struct formatter<char, char> {
28 template <typename FormatContext>
29 constexpr auto format(char val, FormatContext& fc) -> decltype(fc.out())
30 {
31 auto pos = fc.out();
32 *pos = val;
33 return pos++;
34 }
35};
36
37template <>
38struct formatter<char const*, char> {
39 template <typename FormatContext>
40 constexpr auto format(char const* val, FormatContext& fc) -> decltype(fc.out())
41 {
42 return etl::copy(val, val + etl::strlen(val), fc.out());
43 }
44};
45
46template <etl::size_t N>
47struct formatter<char[N], char> {
48 template <typename FormatContext>
49 constexpr auto format(char const* val, FormatContext& fc) -> decltype(fc.out())
50 {
51 return etl::copy(val, val + N, fc.out());
52 }
53};
54
55template <>
56struct formatter<etl::string_view, char> {
57 template <typename FormatContext>
58 constexpr auto format(etl::string_view str, FormatContext& fc) -> decltype(fc.out())
59 {
60 return etl::copy(begin(str), end(str), fc.out());
61 }
62};
63
64template <etl::size_t Capacity>
65struct formatter<etl::inplace_string<Capacity>, char> {
66 template <typename FormatContext>
67 constexpr auto format(etl::inplace_string<Capacity> const& str, FormatContext& fc) -> decltype(fc.out())
68 {
69 return formatter<etl::string_view>().format(str, fc);
70 }
71};
72
73namespace detail {
74template <typename Integer, typename FormatContext>
75constexpr auto integer_format(Integer v, FormatContext& fc) -> decltype(fc.out())
76{
77 char buf[32]{};
78 auto res = strings::from_integer(v, begin(buf), sizeof(buf), 10);
79 if (res.error == strings::from_integer_error::none) {
80 auto str = string_view{begin(buf)};
81 return formatter<string_view>().format(str, fc);
82 }
83 return formatter<string_view>().format("", fc);
84}
85} // namespace detail
86
87template <>
88struct formatter<short, char> {
89 template <typename FormatContext>
90 constexpr auto format(short v, FormatContext& fc) -> decltype(fc.out())
91 {
92 return detail::integer_format(v, fc);
93 }
94};
95
96template <>
97struct formatter<int, char> {
98 template <typename FormatContext>
99 constexpr auto format(int v, FormatContext& fc) -> decltype(fc.out())
100 {
101 return detail::integer_format(v, fc);
102 }
103};
104
105template <>
106struct formatter<long, char> {
107 template <typename FormatContext>
108 constexpr auto format(long v, FormatContext& fc) -> decltype(fc.out())
109 {
110 return detail::integer_format(v, fc);
111 }
112};
113
114template <>
115struct formatter<long long, char> {
116 template <typename FormatContext>
117 constexpr auto format(long long v, FormatContext& fc) -> decltype(fc.out())
118 {
119 return detail::integer_format(v, fc);
120 }
121};
122
123template <>
124struct formatter<unsigned short, char> {
125 template <typename FormatContext>
126 constexpr auto format(unsigned short v, FormatContext& fc) -> decltype(fc.out())
127 {
128 return detail::integer_format(v, fc);
129 }
130};
131
132template <>
133struct formatter<unsigned, char> {
134 template <typename FormatContext>
135 constexpr auto format(int v, FormatContext& fc) -> decltype(fc.out())
136 {
137 return detail::integer_format(v, fc);
138 }
139};
140
141template <>
142struct formatter<unsigned long, char> {
143 template <typename FormatContext>
144 constexpr auto format(unsigned long v, FormatContext& fc) -> decltype(fc.out())
145 {
146 return detail::integer_format(v, fc);
147 }
148};
149
150template <>
151struct formatter<unsigned long long, char> {
152 template <typename FormatContext>
153 constexpr auto format(unsigned long long v, FormatContext& fc) -> decltype(fc.out())
154 {
155 return detail::integer_format(v, fc);
156 }
157};
158
159} // namespace etl
160
161#endif // TETL_FORMAT_FORMATTER_HPP
constexpr auto copy(InputIt first, InputIt last, OutputIt destination) -> OutputIt
Copies the elements in the range, defined by [first, last), to another range beginning at destination...
Definition copy.hpp:18
constexpr auto strlen(char const *str) -> etl::size_t
Returns the length of the C string str.
Definition strlen.hpp:13
constexpr auto end(C &c) -> decltype(c.end())
Returns an iterator to the end (i.e. the element after the last element) of the given container c or ...
Definition end.hpp:14
constexpr auto begin(C &c) -> decltype(c.begin())
Returns an iterator to the beginning of the given container c or array array. These templates rely on...
Definition begin.hpp:20
basic_string_view< char, etl::char_traits< char > > string_view
Typedef for common character type char
Definition basic_string_view.hpp:704
@ none
Definition from_integer.hpp:20
constexpr auto from_integer(Int num, char *str, size_t length, int base) -> from_integer_result
Definition from_integer.hpp:30
Definition adjacent_find.hpp:8
basic_inplace_string< char, Capacity > inplace_string
Typedef for a basic_inplace_string using 'char'.
Definition basic_inplace_string.hpp:1330
constexpr auto format(char val, FormatContext &fc) -> decltype(fc.out())
Definition formatter.hpp:29
constexpr auto format(char const *val, FormatContext &fc) -> decltype(fc.out())
Definition formatter.hpp:40
constexpr auto format(char const *val, FormatContext &fc) -> decltype(fc.out())
Definition formatter.hpp:49
constexpr auto format(etl::inplace_string< Capacity > const &str, FormatContext &fc) -> decltype(fc.out())
Definition formatter.hpp:67
constexpr auto format(etl::string_view str, FormatContext &fc) -> decltype(fc.out())
Definition formatter.hpp:58
constexpr auto format(int v, FormatContext &fc) -> decltype(fc.out())
Definition formatter.hpp:99
constexpr auto format(long v, FormatContext &fc) -> decltype(fc.out())
Definition formatter.hpp:108
constexpr auto format(long long v, FormatContext &fc) -> decltype(fc.out())
Definition formatter.hpp:117
constexpr auto format(short v, FormatContext &fc) -> decltype(fc.out())
Definition formatter.hpp:90
constexpr auto format(int v, FormatContext &fc) -> decltype(fc.out())
Definition formatter.hpp:135
constexpr auto format(unsigned long v, FormatContext &fc) -> decltype(fc.out())
Definition formatter.hpp:144
constexpr auto format(unsigned long long v, FormatContext &fc) -> decltype(fc.out())
Definition formatter.hpp:153
constexpr auto format(unsigned short v, FormatContext &fc) -> decltype(fc.out())
Definition formatter.hpp:126
formatter()=delete
formatter(formatter const &other)=delete
auto operator=(formatter const &other) -> formatter=delete