rsl 1.1.0
ROS Support Library
Loading...
Searching...
No Matches
static_string.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <cassert>
5#include <string>
6#include <string_view>
7
8namespace rsl {
9
16template <size_t capacity>
18 std::array<std::string::value_type, capacity> data_{};
19 size_t size_{};
20
21 public:
25 StaticString() = default;
26
30 StaticString(std::string const& string) : size_(std::min(string.size(), capacity)) {
31 assert(string.size() <= capacity &&
32 "rsl::StaticString::StaticString: Input exceeds capacity");
33 std::copy(string.cbegin(), string.cbegin() + std::string::difference_type(size_),
34 data_.begin());
35 }
39 [[nodiscard]] auto begin() const { return data_.cbegin(); }
40
44 [[nodiscard]] auto end() const { return data_.cbegin() + size_; }
45
49 operator std::string_view() const { return std::string_view(data_.data(), size_); }
50};
51
55template <size_t capacity>
56[[nodiscard]] auto to_string(StaticString<capacity> const& static_string) {
57 return std::string(static_string);
58}
59
60} // namespace rsl
Fixed capacity string with an implicit conversion to std::string_view. Capacity is specified as a tem...
Definition static_string.hpp:17
auto end() const
Get a const end iterator.
Definition static_string.hpp:44
StaticString()=default
Construct an empty string.
auto begin() const
Get a const begin iterator.
Definition static_string.hpp:39
StaticString(std::string const &string)
Construct from a std::string.
Definition static_string.hpp:30
auto to_string(StaticString< capacity > const &static_string)
Explicit conversion to std::string.
Definition static_string.hpp:56