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

fixed_size_str.hpp
Go to the documentation of this file.
1 
9 #ifndef EAGINE_FIXED_SIZE_STR_HPP
10 #define EAGINE_FIXED_SIZE_STR_HPP
11 
12 #include "assert.hpp"
13 #include "int_constant.hpp"
14 #include "string_span.hpp"
15 #include "types.hpp"
16 #include <cstring>
17 
18 namespace eagine {
19 
22 template <span_size_t N>
24 public:
27  fixed_size_string() noexcept {
28  std::memset(_str, '\0', N);
29  }
30 
32  template <typename... C, typename = std::enable_if_t<sizeof...(C) == N>>
33  constexpr fixed_size_string(C... c) noexcept
34  : _str{c...} {}
35 
37  fixed_size_string(const char (&s)[N]) noexcept {
38  std::strncpy(_str, s, N);
39  EAGINE_ASSERT(_str[N - 1] == '\0');
40  }
41 
42  template <
43  span_size_t N1,
44  span_size_t N2,
45  typename = std::enable_if_t<N1 + N2 == N + 1>>
47  const fixed_size_string<N1>& s1,
48  const fixed_size_string<N2>& s2) noexcept {
49  std::strncpy(_str, s1._str, N1);
50  std::strncpy(_str + N1 - 1, s2._str, N2);
51  }
52 
55 
57  using iterator = char*;
58 
60  using const_iterator = const char*;
61 
63  auto empty() const noexcept {
64  return _str[0] == '\0';
65  }
66 
68  auto size() const noexcept -> span_size_t {
69  return N - 1;
70  }
71 
73  auto data() const noexcept {
74  return static_cast<const char*>(_str);
75  }
76 
80  auto c_str() const noexcept {
81  EAGINE_ASSERT(_str[N - 1] == '\0');
82  return data();
83  }
84 
86  auto begin() noexcept -> iterator {
87  return _str;
88  }
89 
91  auto begin() const noexcept -> const_iterator {
92  return _str;
93  }
94 
96  auto end() noexcept -> iterator {
97  return _str + N - 1;
98  }
99 
101  auto end() const noexcept -> const_iterator {
102  return _str + N - 1;
103  }
104 
106  auto view() const noexcept -> string_view {
107  return {_str, N - 1};
108  }
109 
112  operator string_view() const noexcept {
113  return view();
114  }
115 
116 private:
117  static_assert(N > 0, "Zero-length fixed size strings are not supported");
118 
119  char _str[N] = {};
120 
121  template <span_size_t>
122  friend class fixed_size_string;
123 };
124 
127 template <span_size_t N>
128 static inline auto make_fixed_size_string(const char (&str)[N]) noexcept {
129  return fixed_size_string<N>(str);
130 }
131 
134 template <span_size_t N1, span_size_t N2>
135 static inline auto operator+(
136  const fixed_size_string<N1>& s1,
137  const fixed_size_string<N2>& s2) noexcept {
138  return fixed_size_string<N1 + N2 - 1>(s1, s2);
139 }
140 
143 template <int I>
144 static inline auto to_fixed_size_string(
146  std::enable_if_t<(I >= 0) && (I < 10)>* = nullptr) noexcept {
147  return fixed_size_string<2>(char('0' + I), '\0');
148 }
149 
152 template <int I>
153 static inline auto to_fixed_size_string(
155  std::enable_if_t<(I > 9)>* = nullptr) noexcept {
157  fixed_size_string<2>(char('0' + I % 10), '\0');
158 }
159 
162 template <int I>
163 static inline auto to_fixed_size_string(
165  std::enable_if_t<(I < 0)>* = nullptr) noexcept {
167 }
168 
169 } // namespace eagine
170 
171 #endif // EAGINE_FIXED_SIZE_STR_HPP
auto size() const noexcept -> span_size_t
Returns the size of the string.
Definition: fixed_size_str.hpp:68
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
static auto to_fixed_size_string(int_constant< I >, std::enable_if_t<(I >=0) &&(I< 10)> *=nullptr) noexcept
Converts a single-digit decimal number into fixed_size_string.
Definition: fixed_size_str.hpp:144
Common code is placed in this namespace.
Definition: eagine.hpp:21
static auto make_fixed_size_string(const char(&str)[N]) noexcept
Creates a fixed_size_string from a C-string literal.
Definition: fixed_size_str.hpp:128
fixed_size_string(const char(&s)[N]) noexcept
Construction from a C-string literal.
Definition: fixed_size_str.hpp:37
char * iterator
Alias for iterator type.
Definition: fixed_size_str.hpp:57
span_size_t size_type
Alias for length type.
Definition: fixed_size_str.hpp:54
auto begin() noexcept -> iterator
Returns iterator to the start of the string.
Definition: fixed_size_str.hpp:86
auto view() const noexcept -> string_view
Returns a view over the internal character data storage.
Definition: fixed_size_str.hpp:106
static auto operator+(const fixed_size_string< N1 > &s1, const fixed_size_string< N2 > &s2) noexcept
Concatenation operator for fixed_size_string.
Definition: fixed_size_str.hpp:135
auto end() const noexcept -> const_iterator
Returns const iterator past the end of the string.
Definition: fixed_size_str.hpp:101
auto end() noexcept -> iterator
Returns iterator past the end of the string.
Definition: fixed_size_str.hpp:96
fixed_size_string() noexcept
Default construction.
Definition: fixed_size_str.hpp:27
const char * const_iterator
Alias for const iterator type.
Definition: fixed_size_str.hpp:60
auto empty() const noexcept
Indicates if the string is empty.
Definition: fixed_size_str.hpp:63
auto data() const noexcept
Returns pointer to the internally-stored character data.
Definition: fixed_size_str.hpp:73
std::integral_constant< int, I > int_constant
Alias for signed int constant type.
Definition: int_constant.hpp:25
constexpr fixed_size_string(C... c) noexcept
Construction from a pack of characters.
Definition: fixed_size_str.hpp:33
String with maximum fixed-size internal storage.
Definition: fixed_size_str.hpp:23
auto begin() const noexcept -> const_iterator
Returns const iterator to the start of the string.
Definition: fixed_size_str.hpp:91
auto c_str() const noexcept
Returns pointer to the internally-stored character data.
Definition: fixed_size_str.hpp:80

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