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// SPDX-FileCopyrightText: Copyright (C) 2021 Tobias Hienzsch
3
4#ifndef TETL_FORMAT_ARGUMENT_HPP
5#define TETL_FORMAT_ARGUMENT_HPP
6
7#include <etl/_algorithm/find.hpp>
8#include <etl/_contracts/check.hpp>
9#include <etl/_format/formatter.hpp>
10#include <etl/_utility/pair.hpp>
11
12namespace etl::detail {
13// Escape tokens
14inline constexpr auto token_begin = '{';
15inline constexpr auto token_end = '}';
16
17template <typename ValueT, typename FormatContext>
18auto format_argument(ValueT const& val, FormatContext& fc) -> decltype(fc.out())
19{
20 auto f = formatter<ValueT, char>{};
21 return f.format(val, fc);
22}
23
24inline auto split_at_next_argument(etl::string_view str) -> etl::pair<etl::string_view, etl::string_view>
25{
26 using size_type = etl::string_view::size_type;
27
28 auto const* res = etl::find(begin(str), end(str), token_begin);
29 if (res != end(str) && *etl::next(res) == token_end) {
30 auto index = static_cast<size_type>(etl::distance(begin(str), res));
31 auto first = str.substr(0, index);
32 auto second = str.substr(index + 2);
33 return etl::make_pair(first, second);
34 }
35
36 return etl::make_pair(str, etl::string_view{});
37}
38
39template <typename FormatContext>
40auto format_escaped_sequences(etl::string_view str, FormatContext& ctx) -> void
41{
42 // Loop as long as escaped sequences are found.
43 auto const* first = begin(str);
44 while (true) {
45 // Find open sequence {{
46 auto const* const openFirst = etl::find(first, end(str), token_begin);
47 auto const* const openSec = etl::next(openFirst);
48 auto const escapeStart = openFirst != end(str) //
49 && openSec != end(str) //
50 && *openSec == token_begin;
51
52 if (escapeStart) {
53 // Copy upto {{
54 detail::format_argument(etl::string_view(first, openFirst), ctx);
55
56 // Find sequence }}
57 auto const* closeFirst = etl::find(etl::next(openSec), end(str), token_end);
58 auto const* closeSec = etl::next(closeFirst);
59 auto escapeClose = closeFirst != end(str) //
60 && closeSec != end(str) //
61 && *closeSec == token_end;
62
63 // Copy everything between {{ ... }}, but only one curly each.
64 if (escapeClose) {
65 detail::format_argument(etl::string_view(openSec, closeFirst + 1), ctx);
66 first = closeFirst + 2;
67 } else {
68 // No closing "}}" found
69 TETL_PRECONDITION(false);
70 return;
71 }
72 } else {
73 // No more escaped sequence found, copy rest.
74 detail::format_argument(etl::string_view(first, end(str)), ctx);
75 return;
76 }
77 }
78}
79
80} // namespace etl::detail
81
82#endif // TETL_FORMAT_ARGUMENT_HPP
constexpr auto find(InputIt first, InputIt last, T const &value) noexcept -> InputIt
Searches for an element equal to value.
Definition find.hpp:19
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 next(InputIt it, typename iterator_traits< InputIt >::difference_type n=1) -> InputIt
Return the nth successor of iterator it.
Definition next.hpp:15
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:17
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
Definition adjacent_find.hpp:9
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:167
constexpr basic_string_view() noexcept=default
Default constructor. Constructs an empty basic_string_view. After construction, data() is equal to nu...
constexpr auto substr(size_type pos=0, size_type count=npos) const -> basic_string_view
Returns a view of the substring [pos, pos + rcount), where rcount is the smaller of count and size() ...
Definition basic_string_view.hpp:257
etl::pair is a class template that provides a way to store two heterogeneous objects as a single unit...
Definition pair.hpp:37