|
| 1 | +#ifndef IRIS_FIXED_STRING_HPP |
| 2 | +#define IRIS_FIXED_STRING_HPP |
| 3 | + |
| 4 | +// SPDX-License-Identifier: MIT |
| 5 | + |
| 6 | +#include <algorithm> |
| 7 | +#include <array> |
| 8 | +#include <string> |
| 9 | +#include <string_view> |
| 10 | + |
| 11 | +namespace iris { |
| 12 | + |
| 13 | +template<class CharT, std::size_t N, class Traits = std::char_traits<CharT>> |
| 14 | +struct basic_fixed_string |
| 15 | +{ |
| 16 | + using storage_type = std::array<CharT, N + 1>; |
| 17 | + |
| 18 | + storage_type data{}; |
| 19 | + |
| 20 | + using traits_type = Traits; |
| 21 | + using value_type = CharT; |
| 22 | + using iterator = storage_type::iterator; |
| 23 | + using const_iterator = storage_type::const_iterator; |
| 24 | + using string_view_type = std::basic_string_view<CharT, Traits>; |
| 25 | + |
| 26 | + constexpr basic_fixed_string() noexcept = default; |
| 27 | + constexpr basic_fixed_string(CharT const (&str)[N + 1]) { std::ranges::copy(str, data.begin()); } |
| 28 | + |
| 29 | + [[nodiscard]] constexpr iterator begin() noexcept { return data.begin(); } |
| 30 | + [[nodiscard]] constexpr const_iterator begin() const noexcept { return data.begin(); } |
| 31 | + [[nodiscard]] constexpr iterator end() noexcept { return data.end() - 1; } |
| 32 | + [[nodiscard]] constexpr const_iterator end() const noexcept { return data.end() - 1; } |
| 33 | + |
| 34 | + [[nodiscard]] constexpr operator string_view_type() const { return {this->begin(), this->end()}; } |
| 35 | +}; |
| 36 | + |
| 37 | +template<class CharT, std::size_t N> |
| 38 | +basic_fixed_string(CharT const (&)[N]) -> basic_fixed_string<CharT, N - 1>; |
| 39 | + |
| 40 | +template<std::size_t N> |
| 41 | +using fixed_string = basic_fixed_string<char, N>; |
| 42 | + |
| 43 | +} // iris |
| 44 | + |
| 45 | +#endif |
0 commit comments