string_input.hpp
Go to the documentation of this file.
1 // Copyright (C) 2020-2023 Jonathan Müller and lexy contributors
2 // SPDX-License-Identifier: BSL-1.0
3 
4 #ifndef LEXY_INPUT_STRING_INPUT_HPP_INCLUDED
5 #define LEXY_INPUT_STRING_INPUT_HPP_INCLUDED
6 
7 #include <lexy/error.hpp>
8 #include <lexy/input/base.hpp>
9 #include <lexy/lexeme.hpp>
10 
11 namespace lexy
12 {
13 template <typename View>
15 
17 template <typename Encoding = default_encoding>
19 {
20 public:
21  using encoding = Encoding;
22  using char_type = typename encoding::char_type;
23 
24  //=== constructors ===//
25  constexpr string_input() noexcept : _data(nullptr), _size(0u) {}
26 
27  constexpr string_input(const char_type* data, std::size_t size) noexcept
28  : _data(data), _size(size)
29  {}
30  constexpr string_input(const char_type* begin, const char_type* end) noexcept
31  : string_input(begin, std::size_t(end - begin))
32  {}
33 
34  template <typename CharT, typename = _detail::require_secondary_char_type<Encoding, CharT>>
35  string_input(const CharT* data, std::size_t size) noexcept
36  : _data(reinterpret_cast<const char_type*>(data)), _size(size)
37  {}
38  template <typename CharT, typename = _detail::require_secondary_char_type<Encoding, CharT>>
39  string_input(const CharT* begin, const CharT* end) noexcept
40  : string_input(begin, std::size_t(end - begin))
41  {}
42 
43  template <typename View, typename CharT = _string_view_char_type<View>>
44  constexpr explicit string_input(const View& view) noexcept : _size(view.size())
45  {
46  if constexpr (std::is_same_v<CharT, char_type>)
47  {
48  _data = view.data();
49  }
50  else
51  {
52  static_assert(Encoding::template is_secondary_char_type<CharT>());
53  _data = reinterpret_cast<const char_type*>(view.data());
54  }
55  }
56 
57  //=== access ===//
58  constexpr const char_type* data() const noexcept
59  {
60  return _data;
61  }
62 
63  constexpr std::size_t size() const noexcept
64  {
65  return _size;
66  }
67 
68  //=== reader ===//
69  constexpr auto reader() const& noexcept
70  {
71  return _range_reader<encoding>(_data, _data + _size);
72  }
73 
74 private:
75  const char_type* _data;
76  std::size_t _size;
77 };
78 
79 template <typename CharT>
80 string_input(const CharT* begin, const CharT* end) -> string_input<deduce_encoding<CharT>>;
81 template <typename CharT>
82 string_input(const CharT* data, std::size_t size) -> string_input<deduce_encoding<CharT>>;
83 template <typename View>
85 
86 template <typename Encoding, typename CharT>
87 constexpr string_input<Encoding> zstring_input(const CharT* str) noexcept
88 {
89  auto end = str;
90  while (*end != CharT())
91  ++end;
92 
93  return string_input<Encoding>(str, end);
94 }
95 template <typename CharT>
96 constexpr auto zstring_input(const CharT* str) noexcept
97 {
98  return zstring_input<deduce_encoding<CharT>>(str);
99 }
100 
101 //=== convenience typedefs ===//
102 template <typename Encoding = default_encoding>
104 
105 template <typename Tag, typename Encoding = default_encoding>
107 
108 template <typename Encoding = default_encoding>
110 } // namespace lexy
111 
112 #endif // LEXY_INPUT_STRING_INPUT_HPP_INCLUDED
113 
cx::size
constexpr auto size(const C &c) -> decltype(c.size())
Definition: wildcards.hpp:636
lexy::string_input::string_input
constexpr string_input() noexcept
Definition: string_input.hpp:25
lexy::string_input::_size
std::size_t _size
Definition: string_input.hpp:76
magic_enum::char_type
string_view::value_type char_type
Definition: magic_enum.hpp:145
lexy::string_input::string_input
constexpr string_input(const View &view) noexcept
Definition: string_input.hpp:44
lexy::string_input::reader
constexpr auto reader() const &noexcept
Definition: string_input.hpp:69
lexy::string_input::string_input
constexpr string_input(const char_type *data, std::size_t size) noexcept
Definition: string_input.hpp:27
lexy::error_context
Contains information about the context of an error, production is type-erased.
Definition: error.hpp:198
lexy
Definition: any_ref.hpp:12
lexy::string_input::_data
const char_type * _data
Definition: string_input.hpp:75
cx::end
constexpr auto end(const C &c) -> decltype(c.end())
Definition: wildcards.hpp:686
lexy::error
Generic failure.
Definition: error.hpp:14
lexy::string_input::char_type
typename encoding::char_type char_type
Definition: string_input.hpp:22
lexeme.hpp
lexy::string_input::string_input
constexpr string_input(const char_type *begin, const char_type *end) noexcept
Definition: string_input.hpp:30
lexy::string_input::data
constexpr const char_type * data() const noexcept
Definition: string_input.hpp:58
LEXY_DECAY_DECLTYPE
#define LEXY_DECAY_DECLTYPE(...)
Definition: config.hpp:26
lexy::string_input::size
constexpr std::size_t size() const noexcept
Definition: string_input.hpp:63
cx::begin
constexpr auto begin(const C &c) -> decltype(c.begin())
Definition: wildcards.hpp:661
lexy::lexeme
Definition: lexeme.hpp:16
lexy::string_input
string_input(const CharT *begin, const CharT *end) -> string_input< deduce_encoding< CharT >>
base.hpp
lexy::_string_view_char_type
LEXY_DECAY_DECLTYPE(*LEXY_DECLVAL(View).data()) _string_view_char_type
Definition: string_input.hpp:14
LEXY_DECLVAL
#define LEXY_DECLVAL(...)
Definition: config.hpp:24
lexy::string_input::string_input
string_input(const CharT *begin, const CharT *end) noexcept
Definition: string_input.hpp:39
lexy::zstring_input
constexpr string_input< Encoding > zstring_input(const CharT *str) noexcept
Definition: string_input.hpp:87
lexy::string_input
An input that refers to a string.
Definition: string_input.hpp:18
lexy::string_input::string_input
string_input(const CharT *data, std::size_t size) noexcept
Definition: string_input.hpp:35
error.hpp
lexy::string_input::encoding
Encoding encoding
Definition: string_input.hpp:21


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Jun 28 2024 02:20:08