OGLplus  (0.59.0) a C++ wrapper for rendering APIs

string_path.hpp
Go to the documentation of this file.
1 
9 #ifndef EAGINE_STRING_PATH_HPP
10 #define EAGINE_STRING_PATH_HPP
11 
12 #include "assert.hpp"
13 #include "identifier.hpp"
14 #include "memory/block.hpp"
15 #include "span.hpp"
16 #include "string_list.hpp"
17 #include <string>
18 #include <tuple>
19 #include <utility>
20 
21 namespace eagine {
22 //------------------------------------------------------------------------------
28 public:
31 
34 
36  using iterator = string_list::iterator<const char*>;
37 
39  using reverse_iterator = string_list::rev_iterator<const char*>;
40 
43  basic_string_path() noexcept = default;
44 
46  basic_string_path(basic_string_path&&) noexcept = default;
47 
49  basic_string_path(const basic_string_path&) = default;
50 
52  auto operator=(basic_string_path&&) noexcept
53  -> basic_string_path& = default;
54 
56  auto operator=(const basic_string_path&) -> basic_string_path& = default;
57 
58  ~basic_string_path() noexcept = default;
59 
61  : _size{size}
62  , _str(str.data(), std_size(str.size())) {}
63 
64  basic_string_path(std::string str, span_size_t size)
65  : _size{size}
66  , _str(std::move(str)) {}
67 
68  basic_string_path(std::tuple<std::string, span_size_t>&& init)
69  : basic_string_path(std::move(std::get<0>(init)), std::get<1>(init)) {}
70 
73  string_view path,
74  EAGINE_TAG_TYPE(split_by),
75  string_view sep)
76  : basic_string_path(string_list::split(path, sep)) {}
77 
78  explicit basic_string_path(
79  const basic_string_path& a,
80  EAGINE_TAG_TYPE(plus),
81  const basic_string_path& b)
82  : _size(a._size + b._size)
83  , _str(a._str + b._str) {}
84 
86  explicit basic_string_path(span<const string_view> names) {
87  _init(names);
88  }
89 
91  template <std::size_t N>
92  explicit basic_string_path(const std::array<string_view, N>& names)
93  : basic_string_path(view(names)) {}
94 
96  template <typename... Str>
98  EAGINE_TAG_TYPE(from_pack),
99  string_view name,
100  const Str&... names)
101  : basic_string_path(_pack_names(name, view(names)...)) {}
102 
104  friend auto operator==(
105  const basic_string_path& a,
106  const basic_string_path& b) noexcept {
107  return a._str == b._str;
108  }
109 
111  friend auto operator!=(
112  const basic_string_path& a,
113  const basic_string_path& b) noexcept {
114  return a._str != b._str;
115  }
116 
118  friend auto
119  operator<(const basic_string_path& a, const basic_string_path& b) noexcept {
120  return a._str < b._str;
121  }
122 
124  friend auto operator<=(
125  const basic_string_path& a,
126  const basic_string_path& b) noexcept {
127  return a._str <= b._str;
128  }
129 
131  friend auto
132  operator>(const basic_string_path& a, const basic_string_path& b) noexcept {
133  return a._str > b._str;
134  }
135 
137  friend auto operator>=(
138  const basic_string_path& a,
139  const basic_string_path& b) noexcept {
140  return a._str >= b._str;
141  }
142 
144  friend auto
145  operator+(const basic_string_path& a, const basic_string_path& b) noexcept {
146  return basic_string_path(a, EAGINE_TAG(plus), b);
147  }
148 
152  auto empty() const noexcept -> bool {
153  EAGINE_ASSERT((size() == 0) == _str.empty());
154  return _str.empty();
155  }
156 
159  void clear() noexcept {
160  _size = 0;
161  _str.clear();
162  }
163 
166  auto size() const noexcept -> size_type {
167  return _size;
168  }
169 
170  static auto required_bytes(size_type l) -> size_type {
171  using namespace mbs;
172  return l + 2 * required_sequence_length(code_point_t(l)).value();
173  }
174 
175  static auto required_bytes(string_view str) -> size_type {
176  return required_bytes(size_type(str.size()));
177  }
178 
181  _str.reserve(std_size(s));
182  }
183 
185  auto front() const noexcept -> string_view {
186  EAGINE_ASSERT(!empty());
187  return string_list::front_value(_str);
188  }
189 
191  auto back() const noexcept -> string_view {
192  EAGINE_ASSERT(!empty());
193  return string_list::back_value(_str);
194  }
195 
198  void push_back(string_view name) {
199  string_list::push_back(_str, _fix(name));
200  ++_size;
201  }
202 
203  void push_back_elem(const string_list::element& elem) {
204  append_to(_str, elem);
205  ++_size;
206  }
207 
210  void pop_back() {
211  EAGINE_ASSERT(!empty());
212  _str.resize(std_size(string_list::pop_back(_str).size()));
213  --_size;
214  }
215 
219  auto begin() const noexcept -> iterator {
220  return empty() ? iterator(nullptr) : iterator(_str.data());
221  }
222 
226  auto end() const noexcept -> iterator {
227  return empty() ? iterator(nullptr)
228  : iterator(_str.data() + _str.size());
229  }
230 
234  auto rbegin() const noexcept -> reverse_iterator {
235  return empty() ? reverse_iterator(nullptr)
236  : reverse_iterator(_str.data() + _str.size() - 1);
237  }
238 
242  auto rend() const noexcept -> reverse_iterator {
243  return empty() ? reverse_iterator(nullptr)
244  : reverse_iterator(_str.data() - 1);
245  }
246 
248  template <typename Func>
249  void for_each_elem(Func func) const {
250  string_list::for_each_elem(view(_str), func);
251  }
252 
253  template <typename Func>
254  void for_each(Func func) const {
255  string_list::for_each(view(_str), func);
256  }
257 
259  template <typename Func>
260  void rev_for_each_elem(Func func) const {
261  string_list::rev_for_each_elem(view(_str), func);
262  }
263 
264  template <typename Func>
265  void rev_for_each(Func func) const {
266  string_list::rev_for_each(view(_str), func);
267  }
268 
270  auto as_string(string_view sep, bool trail_sep) const -> std::string {
271  return string_list::join(view(_str), sep, trail_sep);
272  }
273 
275  auto block() noexcept -> memory::const_block {
276  return as_bytes(view(_str));
277  }
278 
279 private:
280  span_size_t _size{0};
281  std::string _str{};
282 
283  template <typename Str>
284  void _init(span<Str> names) {
285  span_size_t len = 2 * names.size();
286  for(const auto& name : names) {
287  len += span_size(name.size());
288  }
289  _str.reserve(std_size(len));
290 
291  for(const auto& name : names) {
292  push_back(name);
293  }
294  EAGINE_ASSERT(span_size(_str.size()) == len);
295  }
296 
297  static inline auto _fix(string_view str) noexcept -> string_view {
298  while(str.size() > 0) {
299  if(str[str.size() - 1] == '\0') {
300  str = string_view(str.data(), str.size() - 1);
301  } else {
302  break;
303  }
304  }
305  return str;
306  }
307 
308  template <typename... Str>
309  static inline auto _pack_names(const Str&... n) noexcept
310  -> std::array<string_view, sizeof...(Str)> {
311  return {{_fix(n)...}};
312  }
313 };
314 //------------------------------------------------------------------------------
315 } // namespace eagine
316 
317 #endif // EAGINE_STRING_PATH_HPP
std::ptrdiff_t span_size_t
Signed span size type used by eagine.
Definition: types.hpp:36
basic_string_span< const char > string_view
Alias for const string views.
Definition: string_span.hpp:116
friend auto operator!=(const basic_string_path &a, const basic_string_path &b) noexcept
Non-equality comparison.
Definition: string_path.hpp:111
static constexpr auto as_bytes(basic_span< T, P, S > spn) noexcept -> basic_block< std::is_const_v< T >>
Converts a span into a basic_block.
Definition: block.hpp:39
basic_string_path(const std::array< string_view, N > &names)
Construction from a list of path element names.
Definition: string_path.hpp:92
static constexpr auto span_size(T v) noexcept
Converts argument to span size type.
Definition: types.hpp:59
Common code is placed in this namespace.
Definition: eagine.hpp:21
void pop_back()
Removes a single element from the end of this path.
Definition: string_path.hpp:210
void rev_for_each_elem(Func func) const
Calls the specified function for each element in reverse order.
Definition: string_path.hpp:260
string_list::iterator< const char * > iterator
Iterator type.
Definition: string_path.hpp:36
friend auto operator+(const basic_string_path &a, const basic_string_path &b) noexcept
Concatenates two paths.
Definition: string_path.hpp:145
auto block() noexcept -> memory::const_block
Returns a block covering the internal representation of this path.
Definition: string_path.hpp:275
basic_block< true > const_block
Alias for const byte memory span.
Definition: block.hpp:32
span_size_t size_type
The element count type.
Definition: string_path.hpp:33
basic_string_path(EAGINE_TAG_TYPE(from_pack), string_view name, const Str &... names)
Construction from a pack of path element names.
Definition: string_path.hpp:97
static constexpr auto view(T *addr, S size) noexcept -> const_span< T >
Creates a view starting at the specified pointer and specified length.
Definition: span.hpp:458
basic_string_path() noexcept=default
Default constructor. Creates an empty path.
auto size() const noexcept -> size_type
Returns the number of elements in this path.
Definition: string_path.hpp:166
friend auto operator<=(const basic_string_path &a, const basic_string_path &b) noexcept
Less-equal comparison.
Definition: string_path.hpp:124
Basic class storing paths made of string elements.
Definition: string_path.hpp:27
void reserve_bytes(size_type s)
Reserves the specified number of bytes in the storage.
Definition: string_path.hpp:180
void clear() noexcept
Clears this path.
Definition: string_path.hpp:159
void for_each_elem(Func func) const
Calls the specified function for each element of this path.
Definition: string_path.hpp:249
basic_string_path(span< const string_view > names)
Construction from a list of path element names.
Definition: string_path.hpp:86
string_list::rev_iterator< const char * > reverse_iterator
Reverse iterator type.
Definition: string_path.hpp:39
static constexpr auto std_size(T v) noexcept
Converts argument to std size type.
Definition: types.hpp:52
#define EAGINE_TAG(NAME)
Macro for defining selector values corresponding to identifier NAME.
Definition: identifier.hpp:375
auto rend() const noexcept -> reverse_iterator
Returns an iterator pointing past the start of the path.
Definition: string_path.hpp:242
friend auto operator==(const basic_string_path &a, const basic_string_path &b) noexcept
Equality comparison.
Definition: string_path.hpp:104
basic_string_path(string_view path, EAGINE_TAG_TYPE(split_by), string_view sep)
Construction from a path string and a separator string.
Definition: string_path.hpp:72
void push_back(string_view name)
Appends a new element with the specified name to the end.
Definition: string_path.hpp:198
friend auto operator>(const basic_string_path &a, const basic_string_path &b) noexcept
Greater-than comparison.
Definition: string_path.hpp:132
auto rbegin() const noexcept -> reverse_iterator
Returns a reverse iterator pointing to the end of the path.
Definition: string_path.hpp:234
friend auto operator<(const basic_string_path &a, const basic_string_path &b) noexcept
Less-than comparison.
Definition: string_path.hpp:119
friend auto operator>=(const basic_string_path &a, const basic_string_path &b) noexcept
Greater-equal comparison.
Definition: string_path.hpp:137
auto back() const noexcept -> string_view
Returns the element at the back of the path.
Definition: string_path.hpp:191
auto begin() const noexcept -> iterator
Returns an iterator pointing to the start of the path.
Definition: string_path.hpp:219
auto as_string(string_view sep, bool trail_sep) const -> std::string
Returns this path as string with elements separated by sep.
Definition: string_path.hpp:270
auto front() const noexcept -> string_view
Returns the element at the front of the path.
Definition: string_path.hpp:185
#define EAGINE_TAG_TYPE(NAME)
Macro for defining selector types corresponding to identifier NAME.
Definition: identifier.hpp:367
auto empty() const noexcept -> bool
Indicates if this path is empty.
Definition: string_path.hpp:152
auto end() const noexcept -> iterator
Returns an iterator pointing past the end of the path.
Definition: string_path.hpp:226
static constexpr auto append_to(std::basic_string< C, T, A > &str, memory::basic_span< const C, P, S > spn) -> auto &
Appends the contents of a span of characters to a standard string.
Definition: string_span.hpp:147

Copyright © 2015-2021 Matúš Chochlík.
<chochlik -at -gmail.com>
Documentation generated on Tue Apr 13 2021 by Doxygen (version 1.8.17).