libfly  6.2.2
C++20 utility library for Linux, macOS, and Windows
format_parse_context.hpp
1 #pragma once
2 
3 #include "fly/types/string/concepts.hpp"
4 #include "fly/types/string/detail/format_parameter_type.hpp"
5 #include "fly/types/string/detail/traits.hpp"
6 #include "fly/types/string/lexer.hpp"
7 
8 #include <cstddef>
9 #include <string_view>
10 
11 namespace fly::detail {
12 
20 template <fly::StandardCharacter CharType>
22 {
24  using view_type = typename traits::view_type;
25 
26 public:
34  template <std::size_t N>
35  constexpr explicit BasicFormatParseContext(
36  const CharType (&format)[N],
37  const ParameterType *parameters,
38  std::size_t parameters_size) noexcept;
39 
48  constexpr std::size_t next_position();
49 
57  constexpr std::optional<ParameterType> parameter_type(std::size_t position);
58 
62  constexpr fly::BasicLexer<CharType> &lexer();
63 
67  constexpr view_type view() const;
68 
81  void on_error(const char *error);
82 
89  constexpr bool has_error() const;
90 
97  std::string error() const;
98 
99 private:
101  BasicFormatParseContext &operator=(BasicFormatParseContext &&) = delete;
102 
104  BasicFormatParseContext &operator=(const BasicFormatParseContext &) = delete;
105 
107 
108  const ParameterType *m_parameters;
109  std::size_t m_parameters_size;
110 
111  std::size_t m_next_position {0};
112  bool m_expect_no_positions_specified {false};
113  bool m_expect_all_positions_specified {false};
114 
115  std::string_view m_error;
116 };
117 
118 //==================================================================================================
119 template <fly::StandardCharacter CharType>
120 template <std::size_t N>
122  const CharType (&format)[N],
123  const ParameterType *parameters,
124  std::size_t parameters_size) noexcept :
125  m_lexer(format),
126  m_parameters(parameters),
127  m_parameters_size(parameters_size)
128 {
129 }
130 
131 //==================================================================================================
132 template <fly::StandardCharacter CharType>
134 {
135  std::size_t position = 0;
136 
137  if (auto specified_position = m_lexer.consume_number(); specified_position)
138  {
139  m_expect_all_positions_specified = true;
140  position = static_cast<std::size_t>(*specified_position);
141  }
142  else
143  {
144  m_expect_no_positions_specified = true;
145  position = m_next_position++;
146  }
147 
148  if (m_expect_all_positions_specified && m_expect_no_positions_specified)
149  {
150  on_error("Argument position must be provided on all or not on any specifier");
151  }
152 
153  return position;
154 }
155 
156 //==================================================================================================
157 template <fly::StandardCharacter CharType>
158 constexpr std::optional<ParameterType>
160 {
161  if (position >= m_parameters_size)
162  {
163  on_error("Argument position exceeds number of provided arguments");
164  return std::nullopt;
165  }
166 
167  return m_parameters[position];
168 }
169 
170 //==================================================================================================
171 template <fly::StandardCharacter CharType>
173 {
174  return m_lexer;
175 }
176 
177 //==================================================================================================
178 template <fly::StandardCharacter CharType>
179 constexpr auto BasicFormatParseContext<CharType>::view() const -> view_type
180 {
181  return m_lexer.view();
182 }
183 
184 //==================================================================================================
185 template <fly::StandardCharacter CharType>
187 {
188  m_error = error;
189 }
190 
191 //==================================================================================================
192 template <fly::StandardCharacter CharType>
194 {
195  return !m_error.empty();
196 }
197 
198 //==================================================================================================
199 template <fly::StandardCharacter CharType>
201 {
202  return std::string(m_error.data(), m_error.size());
203 }
204 
205 } // namespace fly::detail
Definition: lexer.hpp:31
Definition: format_parse_context.hpp:22
constexpr std::optional< ParameterType > parameter_type(std::size_t position)
Definition: format_parse_context.hpp:159
std::string error() const
Definition: format_parse_context.hpp:200
constexpr bool has_error() const
Definition: format_parse_context.hpp:193
void on_error(const char *error)
Definition: format_parse_context.hpp:186
constexpr BasicFormatParseContext(const CharType(&format)[N], const ParameterType *parameters, std::size_t parameters_size) noexcept
Definition: format_parse_context.hpp:121
constexpr view_type view() const
Definition: format_parse_context.hpp:179
constexpr std::size_t next_position()
Definition: format_parse_context.hpp:133
constexpr fly::BasicLexer< CharType > & lexer()
Definition: format_parse_context.hpp:172
Definition: traits.hpp:18