string_view.hpp
Go to the documentation of this file.
1 // Copyright (C) 2020-2024 Jonathan Müller and lexy contributors
2 // SPDX-License-Identifier: BSL-1.0
3 
4 #ifndef LEXY_DETAIL_STRING_VIEW_HPP_INCLUDED
5 #define LEXY_DETAIL_STRING_VIEW_HPP_INCLUDED
6 
10 
11 namespace lexy::_detail
12 {
14 {};
15 
16 template <typename CharT>
18 {
19  static constexpr CharT empty_string[] = {CharT()};
20 
21 public:
22  using char_type = CharT;
23 
24  //=== constructor ===//
25  constexpr basic_string_view() noexcept : _ptr(empty_string), _size(0u), _null_terminated(true)
26  {}
27 
28  constexpr basic_string_view(const char_type* str) noexcept
29  : _ptr(str), _size(0u), _null_terminated(true)
30  {
31  while (*str++)
32  ++_size;
33  }
34 
35  constexpr basic_string_view(const char_type* ptr, std::size_t size) noexcept
36  : _ptr(ptr), _size(size), _null_terminated(false)
37  {}
38  constexpr basic_string_view(null_terminated, const char_type* ptr, std::size_t size) noexcept
39  : _ptr(ptr), _size(size), _null_terminated(true)
40  {
41  LEXY_PRECONDITION(_ptr[_size] == CharT());
42  }
43 
44  constexpr basic_string_view(const char_type* begin, const char_type* end) noexcept
45  : _ptr(begin), _size(std::size_t(end - begin)), _null_terminated(false)
46  {
48  }
49 
50  //=== access ===//
51  using iterator = const char_type*;
52 
53  constexpr iterator begin() const noexcept
54  {
55  return _ptr;
56  }
57  constexpr iterator end() const noexcept
58  {
59  return _ptr + _size;
60  }
61 
62  constexpr bool empty() const noexcept
63  {
64  return _size == 0u;
65  }
66  constexpr std::size_t size() const noexcept
67  {
68  return _size;
69  }
70  constexpr std::size_t length() const noexcept
71  {
72  return _size;
73  }
74 
75  constexpr char_type operator[](std::size_t i) const noexcept
76  {
78  return _ptr[i];
79  }
80  constexpr char_type front() const noexcept
81  {
83  return *_ptr;
84  }
85  constexpr char_type back() const noexcept
86  {
88  return _ptr[_size - 1];
89  }
90 
91  constexpr const char_type* data() const noexcept
92  {
93  return _ptr;
94  }
95 
96  constexpr bool is_null_terminated() const noexcept
97  {
98  return _null_terminated;
99  }
100 
101  constexpr const char_type* c_str() const noexcept
102  {
104  return _ptr;
105  }
106 
107  //=== operations ===//
108  static constexpr std::size_t npos = std::size_t(-1);
109 
110  constexpr void remove_prefix(std::size_t n) noexcept
111  {
113  _ptr += n;
114  _size -= n;
115  }
116  constexpr void remove_suffix(std::size_t n) noexcept
117  {
119  _size -= n;
120  _null_terminated = false;
121  }
122 
123  constexpr basic_string_view substr(std::size_t pos, std::size_t length = npos) const noexcept
124  {
125  LEXY_PRECONDITION(pos < _size);
126  if (length >= _size - pos)
127  {
128  auto result = basic_string_view(_ptr + pos, end());
129  result._null_terminated = _null_terminated;
130  return result;
131  }
132  else
133  {
134  // Note that we're loosing null-terminated-ness.
135  return basic_string_view(_ptr + pos, length);
136  }
137  }
138 
139  constexpr bool starts_with(basic_string_view prefix) const noexcept
140  {
141  return substr(0, prefix.size()) == prefix;
142  }
143  constexpr bool try_remove_prefix(basic_string_view prefix) noexcept
144  {
145  if (!starts_with(prefix))
146  return false;
147 
148  remove_prefix(prefix.length());
149  return true;
150  }
151 
152  constexpr std::size_t find(basic_string_view str, std::size_t pos = 0) const noexcept
153  {
154  for (auto i = pos; i < length(); ++i)
155  {
156  if (substr(i, str.length()) == str)
157  return i;
158  }
159 
160  return npos;
161  }
162  constexpr std::size_t find(CharT c, std::size_t pos = 0) const noexcept
163  {
164  return find(basic_string_view(&c, 1), pos);
165  }
166 
167  //=== comparison ===//
168  friend constexpr bool operator==(basic_string_view<CharT> lhs,
169  basic_string_view<CharT> rhs) noexcept
170  {
171  if (lhs.size() != rhs.size())
172  return false;
173 
174  for (auto a = lhs.begin(), b = rhs.begin(); a != lhs.end(); ++a, ++b)
175  if (*a != *b)
176  return false;
177 
178  return true;
179  }
180 
181  friend constexpr bool operator!=(basic_string_view<CharT> lhs,
182  basic_string_view<CharT> rhs) noexcept
183  {
184  return !(lhs == rhs);
185  }
186 
187 private:
188  const CharT* _ptr;
189  std::size_t _size;
191 };
193 } // namespace lexy::_detail
194 
195 namespace lexy::_detail
196 {
197 template <auto FnPtr, typename Indices = make_index_sequence<FnPtr().size()>>
199 template <auto FnPtr, std::size_t... Indices>
200 struct _string_view_holder<FnPtr, index_sequence<Indices...>>
201 {
202  static constexpr auto view = FnPtr();
203 
204  static constexpr typename decltype(view)::char_type value[] = {view[Indices]..., {}};
205 };
206 
207 template <auto FnPtr>
208 inline constexpr const auto* make_cstr = _string_view_holder<FnPtr>::value;
209 } // namespace lexy::_detail
210 
211 #endif // LEXY_DETAIL_STRING_VIEW_HPP_INCLUDED
212 
lexy::_detail::basic_string_view::basic_string_view
constexpr basic_string_view(null_terminated, const char_type *ptr, std::size_t size) noexcept
Definition: string_view.hpp:38
lexy::_detail::basic_string_view::empty
constexpr bool empty() const noexcept
Definition: string_view.hpp:62
lexy::_detail::basic_string_view::operator!=
constexpr friend bool operator!=(basic_string_view< CharT > lhs, basic_string_view< CharT > rhs) noexcept
Definition: string_view.hpp:181
magic_enum::char_type
string_view::value_type char_type
Definition: magic_enum.hpp:145
config.hpp
lexy::_detail::basic_string_view::try_remove_prefix
constexpr bool try_remove_prefix(basic_string_view prefix) noexcept
Definition: string_view.hpp:143
lexy::_detail::basic_string_view::char_type
CharT char_type
Definition: string_view.hpp:22
lexy::_detail::basic_string_view::iterator
const char_type * iterator
Definition: string_view.hpp:51
lexy::_detail::basic_string_view::starts_with
constexpr bool starts_with(basic_string_view prefix) const noexcept
Definition: string_view.hpp:139
lexy::_detail::basic_string_view::begin
constexpr iterator begin() const noexcept
Definition: string_view.hpp:53
lexy::_detail::basic_string_view::empty_string
static constexpr CharT empty_string[]
Definition: string_view.hpp:19
lexy::_detail::basic_string_view::_ptr
const CharT * _ptr
Definition: string_view.hpp:188
lexy::_detail::basic_string_view::end
constexpr iterator end() const noexcept
Definition: string_view.hpp:57
lexy::_detail::integer_sequence
Definition: integer_sequence.hpp:12
lexy::_detail::basic_string_view
Definition: string_view.hpp:17
LEXY_PRECONDITION
#define LEXY_PRECONDITION(Expr)
Definition: assert.hpp:36
lexy::_detail::basic_string_view::basic_string_view
constexpr basic_string_view(const char_type *begin, const char_type *end) noexcept
Definition: string_view.hpp:44
lexy::_detail::make_index_sequence
typename _make_index_sequence< Size >::type make_index_sequence
Definition: integer_sequence.hpp:55
lexy::_detail::null_terminated
Definition: string_view.hpp:13
lexy::_detail::basic_string_view::find
constexpr std::size_t find(basic_string_view str, std::size_t pos=0) const noexcept
Definition: string_view.hpp:152
lexy::_detail::basic_string_view::is_null_terminated
constexpr bool is_null_terminated() const noexcept
Definition: string_view.hpp:96
lexy::_detail::make_cstr
constexpr const auto * make_cstr
Definition: string_view.hpp:208
lexy::_detail::basic_string_view::basic_string_view
constexpr basic_string_view(const char_type *str) noexcept
Definition: string_view.hpp:28
lexy::_detail::basic_string_view::operator[]
constexpr char_type operator[](std::size_t i) const noexcept
Definition: string_view.hpp:75
assert.hpp
lexy::_detail::basic_string_view::_null_terminated
bool _null_terminated
Definition: string_view.hpp:190
lexy::_detail::basic_string_view::c_str
constexpr const char_type * c_str() const noexcept
Definition: string_view.hpp:101
lexy::_detail::_string_view_holder
Definition: string_view.hpp:198
magic_enum::detail::n
constexpr auto n() noexcept
Definition: magic_enum.hpp:417
lexy::_detail::basic_string_view::substr
constexpr basic_string_view substr(std::size_t pos, std::size_t length=npos) const noexcept
Definition: string_view.hpp:123
lexy::_detail::basic_string_view::remove_suffix
constexpr void remove_suffix(std::size_t n) noexcept
Definition: string_view.hpp:116
integer_sequence.hpp
lexy::_detail::basic_string_view::back
constexpr char_type back() const noexcept
Definition: string_view.hpp:85
lexy::_detail::basic_string_view::size
constexpr std::size_t size() const noexcept
Definition: string_view.hpp:66
lexy::_detail
Definition: any_ref.hpp:12
lexy::_detail::basic_string_view::basic_string_view
constexpr basic_string_view() noexcept
Definition: string_view.hpp:25
lexy::_detail::basic_string_view::operator==
constexpr friend bool operator==(basic_string_view< CharT > lhs, basic_string_view< CharT > rhs) noexcept
Definition: string_view.hpp:168
lexy::_detail::basic_string_view::front
constexpr char_type front() const noexcept
Definition: string_view.hpp:80
lexy::_detail::basic_string_view::npos
static constexpr std::size_t npos
Definition: string_view.hpp:108
lexy::_detail::basic_string_view::_size
std::size_t _size
Definition: string_view.hpp:189
lexy::_detail::basic_string_view::length
constexpr std::size_t length() const noexcept
Definition: string_view.hpp:70
lexy::_ptr
Definition: parse_tree_input.hpp:71
lexy::_detail::basic_string_view::data
constexpr const char_type * data() const noexcept
Definition: string_view.hpp:91
lexy::_detail::basic_string_view::basic_string_view
constexpr basic_string_view(const char_type *ptr, std::size_t size) noexcept
Definition: string_view.hpp:35
lexy::_detail::basic_string_view::find
constexpr std::size_t find(CharT c, std::size_t pos=0) const noexcept
Definition: string_view.hpp:162
lexy::_detail::basic_string_view::remove_prefix
constexpr void remove_prefix(std::size_t n) noexcept
Definition: string_view.hpp:110


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Dec 13 2024 03:19:17