tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
argument.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2
3#ifndef TETL_FORMAT_ARGUMENT_HPP
4#define TETL_FORMAT_ARGUMENT_HPP
5
10
11namespace etl::detail {
12// Escape tokens
13inline constexpr auto token_begin = '{';
14inline constexpr auto token_end = '}';
15
16template <typename ValueT, typename FormatContext>
17auto format_argument(ValueT const& val, FormatContext& fc) -> decltype(fc.out())
18{
19 auto f = formatter<ValueT, char>{};
20 return f.format(val, fc);
21}
22
23inline auto split_at_next_argument(etl::string_view str) -> etl::pair<etl::string_view, etl::string_view>
24{
25 using size_type = etl::string_view::size_type;
26
27 auto const* res = etl::find(begin(str), end(str), token_begin);
28 if (res != end(str) && *etl::next(res) == token_end) {
29 auto index = static_cast<size_type>(etl::distance(begin(str), res));
30 auto first = str.substr(0, index);
31 auto second = str.substr(index + 2);
32 return etl::make_pair(first, second);
33 }
34
35 return etl::make_pair(str, etl::string_view{});
36}
37
38template <typename FormatContext>
39auto format_escaped_sequences(etl::string_view str, FormatContext& ctx) -> void
40{
41 // Loop as long as escaped sequences are found.
42 auto const* first = begin(str);
43 while (true) {
44 // Find open sequence {{
45 auto const* const openFirst = etl::find(first, end(str), token_begin);
46 auto const* const openSec = etl::next(openFirst);
47 auto const escapeStart = openFirst != end(str) //
48 && openSec != end(str) //
49 && *openSec == token_begin;
50
51 if (escapeStart) {
52 // Copy upto {{
53 detail::format_argument(etl::string_view(first, openFirst), ctx);
54
55 // Find sequence }}
56 auto const* closeFirst = etl::find(etl::next(openSec), end(str), token_end);
57 auto const* closeSec = etl::next(closeFirst);
58 auto escapeClose = closeFirst != end(str) //
59 && closeSec != end(str) //
60 && *closeSec == token_end;
61
62 // Copy everything between {{ ... }}, but only one curly each.
63 if (escapeClose) {
64 detail::format_argument(etl::string_view(openSec, closeFirst + 1), ctx);
65 first = closeFirst + 2;
66 } else {
67 // No closing "}}" found
68 TETL_PRECONDITION(false);
69 return;
70 }
71 } else {
72 // No more escaped sequence found, copy rest.
73 detail::format_argument(etl::string_view(first, end(str)), ctx);
74 return;
75 }
76 }
77}
78
79} // namespace etl::detail
80
81#endif // TETL_FORMAT_ARGUMENT_HPP
#define TETL_PRECONDITION(...)
Definition check.hpp:16
constexpr auto find(InputIt first, InputIt last, T const &value) noexcept -> InputIt
Searches for an element equal to value.
Definition find.hpp:18
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 next(InputIt it, typename iterator_traits< InputIt >::difference_type n=1) -> InputIt
Return the nth successor of iterator it.
Definition next.hpp:14
constexpr auto distance(It first, It last) -> typename iterator_traits< It >::difference_type
Returns the number of hops from first to last.
Definition distance.hpp:16
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
pair(T1, T2) -> pair< T1, T2 >
constexpr auto make_pair(T1 &&t, T2 &&u) -> pair< decay_t< T1 >, decay_t< T2 > >
Creates a etl::pair object, deducing the target type from the types of arguments.
Definition pair.hpp:164
etl::size_t size_type
Definition basic_string_view.hpp:43