emlabcpp
modern opinionated embedded C++ library
string_buffer.h
Go to the documentation of this file.
1 #pragma once
23 
24 #include <algorithm>
25 #include <array>
26 #include <cstring>
27 #include <string_view>
28 
29 #ifdef EMLABCPP_USE_NLOHMANN_JSON
30 #include <nlohmann/json.hpp>
31 #endif
32 
33 namespace emlabcpp
34 {
35 
36 template < std::size_t N >
38 {
39  static constexpr std::size_t capacity = N;
40  using base_type = std::array< char, N >;
41 
42  constexpr string_buffer()
43  {
44  std::fill_n( this->data(), N, '\0' );
45  }
46 
47  constexpr string_buffer( std::string_view sv )
48  : string_buffer()
49  {
50  std::copy_n( sv.begin(), std::min( sv.size(), N - 1 ), this->begin() );
51  }
52 
53  template < std::size_t M >
54  constexpr string_buffer( char const ( &msg )[M] )
55  : string_buffer()
56  {
57  static_assert( M < N );
58  std::copy_n( msg, M, this->begin() );
59  }
60 
61  template < std::size_t M >
62  constexpr string_buffer( string_buffer< M > const& msg )
63  : string_buffer()
64  {
65  static_assert( M < N );
66  std::copy_n( msg.data(), M, this->begin() );
67  }
68 
69  operator std::string_view() const
70  {
71  return this->sv();
72  }
73 
74  std::string_view sv() const
75  {
76  return std::string_view( this->data(), size() );
77  }
78 
79  constexpr auto begin()
80  {
81  return data_.begin();
82  }
83 
84  constexpr auto begin() const
85  {
86  return data_.begin();
87  }
88 
89  constexpr auto end()
90  {
91  return data_.end();
92  }
93 
94  constexpr auto end() const
95  {
96  return data_.end();
97  }
98 
99  constexpr char* data()
100  {
101  return data_.data();
102  }
103 
104  constexpr char const* data() const
105  {
106  return data_.data();
107  }
108 
109  [[nodiscard]] constexpr std::size_t size() const
110  {
111  for ( std::size_t i = 0; i < N; i++ )
112  if ( data_[i] == '\0' )
113  return i;
114  return N;
115  }
116 
117  template < std::size_t M >
118  constexpr auto operator<=>( string_buffer< M > const& other ) const noexcept
119  {
120  return std::string_view{ *this } <=> std::string_view{ other };
121  }
122 
123  template < std::size_t M >
124  constexpr bool operator==( string_buffer< M > const& other ) const noexcept
125  {
126  return std::string_view{ *this } == std::string_view{ other };
127  }
128 
129  constexpr bool operator==( std::string_view other ) const noexcept
130  {
131  return std::string_view{ *this } == other;
132  }
133 
134  string_buffer& extend( std::string_view sv )
135  {
136  std::size_t curr_size = size();
137  std::copy_n(
138  sv.begin(),
139  std::min( sv.size(), N - 1 - curr_size ),
140  this->begin() + curr_size );
141  return *this;
142  }
143 
144 private:
145  std::array< char, N > data_;
146 };
147 
148 namespace bits
149 {
150  template < typename T >
152  {
153  static constexpr bool value = false;
154  };
155 
156  template < std::size_t N >
158  {
159  static constexpr bool value = true;
160  };
161 
162 } // namespace bits
163 
164 template < typename T >
166 
167 template < std::size_t N >
168 std::ostream& operator<<( std::ostream& os, string_buffer< N > const& sb )
169 {
170  return os << std::string_view( sb );
171 }
172 
173 #ifdef EMLABCPP_USE_NLOHMANN_JSON
174 
175 template < std::size_t N >
176 void to_json( nlohmann::json& j, string_buffer< N > const& buffer )
177 {
178  j = std::string_view{ buffer };
179 }
180 
181 template < std::size_t N >
182 void from_json( nlohmann::json const& j, string_buffer< N >& buffer )
183 {
184  std::string const s = j;
185  buffer = string_buffer< N >( std::string_view{ s } );
186 }
187 
188 #endif
189 
190 } // namespace emlabcpp
MIT License.
Definition: impl.h:31
concept is_string_buffer_v
Definition: string_buffer.h:165
constexpr Derived const & min(vec_point_base< Derived, N > const &a, vec_point_base< Derived, N > const &b)
Definition: vec_point_base.h:236
std::ostream & operator<<(std::ostream &os, string_buffer< N > const &sb)
Definition: string_buffer.h:168
N
Definition: static_storage.h:97
Definition: string_buffer.h:152
static constexpr bool value
Definition: string_buffer.h:153
Definition: string_buffer.h:38
constexpr string_buffer(std::string_view sv)
Definition: string_buffer.h:47
constexpr bool operator==(string_buffer< M > const &other) const noexcept
Definition: string_buffer.h:124
std::string_view sv() const
Definition: string_buffer.h:74
constexpr auto end() const
Definition: string_buffer.h:94
constexpr auto begin() const
Definition: string_buffer.h:84
std::array< char, N > base_type
Definition: string_buffer.h:40
constexpr bool operator==(std::string_view other) const noexcept
Definition: string_buffer.h:129
constexpr char * data()
Definition: string_buffer.h:99
constexpr auto end()
Definition: string_buffer.h:89
constexpr string_buffer(char const (&msg)[M])
Definition: string_buffer.h:54
string_buffer & extend(std::string_view sv)
Definition: string_buffer.h:134
constexpr char const * data() const
Definition: string_buffer.h:104
constexpr auto begin()
Definition: string_buffer.h:79
constexpr std::size_t size() const
Definition: string_buffer.h:109
constexpr string_buffer()
Definition: string_buffer.h:42
constexpr auto operator<=>(string_buffer< M > const &other) const noexcept
Definition: string_buffer.h:118
constexpr string_buffer(string_buffer< M > const &msg)
Definition: string_buffer.h:62
static constexpr std::size_t capacity
Definition: string_buffer.h:39