di 0.1.0
Loading...
Searching...
No Matches
fixed_string.h
Go to the documentation of this file.
1#pragma once
2
3#include "di/types/size_t.h"
5
6namespace di::container {
7namespace detail {
8 struct FixedStringConcat;
9}
10
11template<types::size_t count>
13public:
14 char m_data[count + 1];
15
16 constexpr FixedString(char const (&data)[count + 1]) {
17 for (types::size_t i = 0; i < count; i++) {
18 m_data[i] = data[i];
19 }
20 m_data[count] = '\0';
21 }
22
23 constexpr auto data() const -> char const* { return m_data; }
24 constexpr static auto size() { return count; }
25
26 constexpr auto begin() const -> char const* { return m_data; }
27 constexpr auto end() const -> char const* { return m_data + count; }
28
29 template<types::size_t other_size>
30 requires(count != other_size)
31 constexpr friend auto operator==(FixedString const&, FixedString<other_size> const&) -> bool {
32 return false;
33 }
34
35 auto operator==(FixedString const&) const -> bool = default;
36 auto operator<=>(FixedString const&) const = default;
37
38 friend struct detail::FixedStringConcat;
39};
40
41template<types::size_t size>
42FixedString(char const (&)[size]) -> FixedString<size - 1>;
43
44namespace detail {
45 struct FixedStringConcat {
46 private:
47 template<size_t s0, size_t s1>
48 constexpr static auto h(FixedString<s0> a, FixedString<s1> b) -> FixedString<s0 + s1> {
49 auto result = FixedString<s0 + s1> { {} };
50 for (size_t i = 0; i < s0; i++) {
51 result.m_data[i] = a.m_data[i];
52 }
53 for (size_t i = 0; i < s1; i++) {
54 result.m_data[s0 + i] = b.m_data[i];
55 }
56 return result;
57 }
58
59 public:
60 template<size_t sz0, size_t... sz>
61 constexpr static auto operator()(FixedString<sz0> s0, FixedString<sz>... strs)
62 -> FixedString<sz0 + (sz + ... + 0zu)> {
63 return h(s0, FixedStringConcat::operator()(strs...));
64 }
65
66 constexpr static auto operator()() -> FixedString<0> { return { {} }; }
67 };
68}
69
70constexpr inline auto fixed_string_concat = detail::FixedStringConcat {};
71}
72
73namespace di {
75}
Definition fixed_string.h:12
constexpr auto data() const -> char const *
Definition fixed_string.h:23
constexpr FixedString(char const (&data)[count+1])
Definition fixed_string.h:16
auto operator<=>(FixedString const &) const =default
auto operator==(FixedString const &) const -> bool=default
constexpr auto end() const -> char const *
Definition fixed_string.h:27
static constexpr auto size()
Definition fixed_string.h:24
char m_data[count+1]
Definition fixed_string.h:14
constexpr auto begin() const -> char const *
Definition fixed_string.h:26
Definition sequence.h:12
constexpr auto fixed_string_concat
Definition fixed_string.h:70
FixedString(char const (&)[size]) -> FixedString< size - 1 >
constexpr auto size
Definition size.h:62
constexpr auto count
Definition count.h:37
std::size_t size_t
Definition size_t.h:12
Definition any_storable.h:9