7#include "../macros.hpp"
11#include "exception.hpp"
20hi_export_module(hikogui.utility.fixed_string);
22hi_export
namespace hi {
inline namespace v1 {
41hi_export
template<
size_t N>
43 using value_type = char;
53 template<std::
size_t O>
55 requires((O - 1) == N)
57 for (
auto i = 0_uz; i != (O - 1); ++i) {
64 template<std::invocable F>
69 hi_assert(str.size() == N);
70 for (
auto i = 0_uz; i != str.size(); ++i) {
71 _str[i] = char_cast<char>(str[i]);
75 constexpr operator std::string_view() const noexcept
77 return std::string_view{_str.
data(), size()};
85 [[nodiscard]]
constexpr std::size_t size() const noexcept
90 [[nodiscard]]
constexpr bool empty() const noexcept
96 [[nodiscard]]
constexpr friend char& get(fixed_string& a)
noexcept
98 return std::get<I>(a._str);
102 [[nodiscard]]
constexpr friend char const& get(fixed_string
const& a)
noexcept
104 return std::get<I>(a._str);
107 [[nodiscard]]
constexpr char& operator[](
size_t index)
noexcept
110 if (not(index < N)) {
117 [[nodiscard]]
constexpr char const& operator[](
size_t index)
const noexcept
120 if (not(index < N)) {
127 [[nodiscard]]
constexpr auto begin() noexcept
132 [[nodiscard]]
constexpr auto end() noexcept
134 return _str.
begin() + size();
137 [[nodiscard]]
constexpr bool operator==(fixed_string
const& rhs)
const noexcept =
default;
138 [[nodiscard]]
constexpr auto operator<=>(fixed_string
const& rhs)
const noexcept =
default;
141 [[nodiscard]]
constexpr bool operator==(fixed_string<O>
const& rhs)
const noexcept
148 [[nodiscard]]
constexpr auto operator<=>(fixed_string<O>
const& rhs)
const noexcept
151 return static_cast<std::string_view
>(*this) <=>
static_cast<std::string_view
>(rhs);
154 [[nodiscard]]
constexpr bool operator==(std::string_view rhs)
const noexcept
156 return static_cast<std::string_view
>(*this) == rhs;
159 [[nodiscard]]
constexpr auto operator<=>(std::string_view rhs)
const noexcept
161 return static_cast<std::string_view
>(*this) <=> rhs;
164 [[nodiscard]]
constexpr bool operator==(
std::string const& rhs)
const noexcept
166 return static_cast<std::string_view
>(*this) == rhs;
169 [[nodiscard]]
constexpr auto operator<=>(
std::string const& rhs)
const noexcept
171 return static_cast<std::string_view
>(*this) <=> rhs;
174 [[nodiscard]]
constexpr bool operator==(
char const *rhs)
const noexcept
176 return static_cast<std::string_view
>(*this) == rhs;
179 [[nodiscard]]
constexpr auto operator<=>(
char const *rhs)
const noexcept
181 return static_cast<std::string_view
>(*this) <=> rhs;
185 [[nodiscard]]
constexpr bool operator==(
char const (&rhs)[O])
const noexcept
187 return *
this == fixed_string<O - 1>(rhs);
191 [[nodiscard]]
constexpr auto operator<=>(
char const (&rhs)[O])
const noexcept
193 return *this <=> fixed_string<O - 1>(rhs);
203 for (
auto src_i = 0_uz; src_i != N; ++src_i, ++dst_i) {
204 r[dst_i] = (*this)[src_i];
206 for (
auto src_i = 0_uz; src_i != R; ++src_i, ++dst_i) {
207 r[dst_i] = rhs[src_i];
214 [[nodiscard]]
constexpr auto operator+(
char const (&rhs)[R])
const noexcept
226 constexpr auto has_dot = N != 0 and R != 0 ? 1_uz : 0_uz;
230 for (
auto src_i = 0_uz; src_i != N; ++src_i, ++dst_i) {
231 r[dst_i] = (*this)[src_i];
238 for (
auto src_i = 0_uz; src_i != R; ++src_i, ++dst_i) {
239 r[dst_i] = rhs[src_i];
246 [[nodiscard]]
constexpr auto operator/(
char const (&rhs)[R])
const noexcept
252hi_export
template<fixed_
string Tag>
253[[nodiscard]]
consteval uint32_t fourcc() noexcept
255 static_assert(Tag.size() == 4,
"fourcc must get a 4 character fixed_string");
257 return (
static_cast<uint32_t
>(get<0>(Tag)) << 24) | (
static_cast<uint32_t
>(get<1>(Tag)) << 16) |
258 (
static_cast<uint32_t
>(get<2>(Tag)) << 8) |
static_cast<uint32_t
>(get<3>(Tag));
261hi_export
template<fixed_
string Tag>
262consteval uint32_t
operator"" _fcc()
264 return fourcc<Tag>();
267hi_export
template<std::
size_t N>
268fixed_string(
char const (&str)[N]) -> fixed_string<N - 1>;
270hi_export
template<std::invocable F>
271fixed_string(F
const& f) -> fixed_string<F{}().size()>;
274#define hi_to_fixed_string(x) ::hi::fixed_string{[]{ return x; }}
281hi_export
template<std::
size_t N>
282struct formatter<
hi::fixed_string<N>, char> : formatter<std::string_view, char> {
285 return std::formatter<std::string_view, char>::format(
static_cast<std::string_view
>(t), fc);
Utilities to assert and bound check.
Functions for casting values between types savely.
Utilities for throwing exceptions and terminating the application.
The HikoGUI namespace.
Definition array_generic.hpp:20
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
A string which may be used as a none-type template parameter.
Definition fixed_string.hpp:42
constexpr auto operator/(fixed_string< R > const &rhs) const noexcept
Join two strings with a slash '/'.
Definition fixed_string.hpp:224
constexpr fixed_string(F const &f) noexcept
Create a fixed string from function returning a string-like.
Definition fixed_string.hpp:65
constexpr auto operator+(fixed_string< R > const &rhs) const noexcept
Append two strings.
Definition fixed_string.hpp:199