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// SPDX-FileCopyrightText: Copyright (C) 2021 Tobias Hienzsch
3
4#ifndef TETL_FORMAT_FORMATTER_HPP
5#define TETL_FORMAT_FORMATTER_HPP
6
7#include <etl/_algorithm/copy.hpp>
8#include <etl/_cstring/strlen.hpp>
9#include <etl/_format/basic_format_context.hpp>
10#include <etl/_string/basic_inplace_string.hpp>
11#include <etl/_string_view/basic_string_view.hpp>
12#include <etl/_strings/from_integer.hpp>
13
14namespace etl {
15/// \brief The enabled specializations of formatter define formatting rules for
16/// a given type. Enabled specializations meet the Formatter requirements.
17///
18/// https://en.cppreference.com/w/cpp/utility/format/formatter
19template <typename T, typename CharT = char>
20struct formatter {
21 formatter() = delete;
22 formatter(formatter const& other) = delete;
23 auto operator=(formatter const& other) -> formatter = delete;
24};
25
26/// \brief Standard specializations for basic type char.
27template <>
28struct formatter<char, char> {
29 template <typename FormatContext>
30 constexpr auto format(char val, FormatContext& fc) -> decltype(fc.out())
31 {
32 auto pos = fc.out();
33 *pos = val;
34 return pos++;
35 }
36};
37
38template <>
39struct formatter<char const*, char> {
40 template <typename FormatContext>
41 constexpr auto format(char const* val, FormatContext& fc) -> decltype(fc.out())
42 {
43 return etl::copy(val, val + etl::strlen(val), fc.out());
44 }
45};
46
47template <etl::size_t N>
48struct formatter<char[N], char> {
49 template <typename FormatContext>
50 constexpr auto format(char const* val, FormatContext& fc) -> decltype(fc.out())
51 {
52 return etl::copy(val, val + N, fc.out());
53 }
54};
55
56template <>
57struct formatter<etl::string_view, char> {
58 template <typename FormatContext>
59 constexpr auto format(etl::string_view str, FormatContext& fc) -> decltype(fc.out())
60 {
61 return etl::copy(begin(str), end(str), fc.out());
62 }
63};
64
65template <etl::size_t Capacity>
66struct formatter<etl::inplace_string<Capacity>, char> {
67 template <typename FormatContext>
68 constexpr auto format(etl::inplace_string<Capacity> const& str, FormatContext& fc) -> decltype(fc.out())
69 {
70 return formatter<etl::string_view>().format(str, fc);
71 }
72};
73
74namespace detail {
75template <typename Integer, typename FormatContext>
76constexpr auto integer_format(Integer v, FormatContext& fc) -> decltype(fc.out())
77{
78 char buf[32]{};
79 auto res = strings::from_integer(v, begin(buf), sizeof(buf), 10);
80 if (res.error == strings::from_integer_error::none) {
81 auto str = string_view{begin(buf)};
82 return formatter<string_view>().format(str, fc);
83 }
84 return formatter<string_view>().format("", fc);
85}
86} // namespace detail
87
88template <>
89struct formatter<short, char> {
90 template <typename FormatContext>
91 constexpr auto format(short v, FormatContext& fc) -> decltype(fc.out())
92 {
93 return detail::integer_format(v, fc);
94 }
95};
96
97template <>
98struct formatter<int, char> {
99 template <typename FormatContext>
100 constexpr auto format(int v, FormatContext& fc) -> decltype(fc.out())
101 {
102 return detail::integer_format(v, fc);
103 }
104};
105
106template <>
107struct formatter<long, char> {
108 template <typename FormatContext>
109 constexpr auto format(long v, FormatContext& fc) -> decltype(fc.out())
110 {
111 return detail::integer_format(v, fc);
112 }
113};
114
115template <>
116struct formatter<long long, char> {
117 template <typename FormatContext>
118 constexpr auto format(long long v, FormatContext& fc) -> decltype(fc.out())
119 {
120 return detail::integer_format(v, fc);
121 }
122};
123
124template <>
125struct formatter<unsigned short, char> {
126 template <typename FormatContext>
127 constexpr auto format(unsigned short v, FormatContext& fc) -> decltype(fc.out())
128 {
129 return detail::integer_format(v, fc);
130 }
131};
132
133template <>
134struct formatter<unsigned, char> {
135 template <typename FormatContext>
136 constexpr auto format(int v, FormatContext& fc) -> decltype(fc.out())
137 {
138 return detail::integer_format(v, fc);
139 }
140};
141
142template <>
143struct formatter<unsigned long, char> {
144 template <typename FormatContext>
145 constexpr auto format(unsigned long v, FormatContext& fc) -> decltype(fc.out())
146 {
147 return detail::integer_format(v, fc);
148 }
149};
150
151template <>
152struct formatter<unsigned long long, char> {
153 template <typename FormatContext>
154 constexpr auto format(unsigned long long v, FormatContext& fc) -> decltype(fc.out())
155 {
156 return detail::integer_format(v, fc);
157 }
158};
159
160} // namespace etl
161
162#endif // TETL_FORMAT_FORMATTER_HPP
constexpr auto strlen(char const *str) -> etl::size_t
Returns the length of the C string str.
Definition strlen.hpp:14
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:15
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:21
constexpr auto begin(T(&array)[N]) noexcept -> T *
Definition begin.hpp:35
Definition find.hpp:9
from_integer_error
Definition from_integer.hpp:20
Definition adjacent_find.hpp:9
constexpr basic_string_view(Char const *str)
Constructs a view of the null-terminated character string pointed to by s, not including the terminat...
Definition basic_string_view.hpp:75
constexpr auto format(char val, FormatContext &fc) -> decltype(fc.out())
Definition formatter.hpp:30
constexpr auto format(char const *val, FormatContext &fc) -> decltype(fc.out())
Definition formatter.hpp:41
constexpr auto format(char const *val, FormatContext &fc) -> decltype(fc.out())
Definition formatter.hpp:50
constexpr auto format(etl::inplace_string< Capacity > const &str, FormatContext &fc) -> decltype(fc.out())
Definition formatter.hpp:68
constexpr auto format(etl::string_view str, FormatContext &fc) -> decltype(fc.out())
Definition formatter.hpp:59
constexpr auto format(int v, FormatContext &fc) -> decltype(fc.out())
Definition formatter.hpp:100
constexpr auto format(long v, FormatContext &fc) -> decltype(fc.out())
Definition formatter.hpp:109
constexpr auto format(long long v, FormatContext &fc) -> decltype(fc.out())
Definition formatter.hpp:118
constexpr auto format(short v, FormatContext &fc) -> decltype(fc.out())
Definition formatter.hpp:91
constexpr auto format(int v, FormatContext &fc) -> decltype(fc.out())
Definition formatter.hpp:136
constexpr auto format(unsigned long v, FormatContext &fc) -> decltype(fc.out())
Definition formatter.hpp:145
constexpr auto format(unsigned long long v, FormatContext &fc) -> decltype(fc.out())
Definition formatter.hpp:154
constexpr auto format(unsigned short v, FormatContext &fc) -> decltype(fc.out())
Definition formatter.hpp:127
formatter()=delete
formatter(formatter const &other)=delete
auto operator=(formatter const &other) -> formatter=delete