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 empty() const { return size() == 0; }
27
28 constexpr auto begin() const -> char const* { return m_data; }
29 constexpr auto end() const -> char const* { return m_data + count; }
30
31 template<types::size_t other_size>
32 requires(count != other_size)
33 constexpr friend auto operator==(FixedString const&, FixedString<other_size> const&) -> bool {
34 return false;
35 }
36
37 auto operator==(FixedString const&) const -> bool = default;
38 auto operator<=>(FixedString const&) const = default;
39
40 friend struct detail::FixedStringConcat;
41};
42
43template<types::size_t size>
44FixedString(char const (&)[size]) -> FixedString<size - 1>;
45
46namespace detail {
47 struct FixedStringConcat {
48 private:
49 template<size_t s0, size_t s1>
50 constexpr static auto h(FixedString<s0> a, FixedString<s1> b) -> FixedString<s0 + s1> {
51 auto result = FixedString<s0 + s1> { {} };
52 for (size_t i = 0; i < s0; i++) {
53 result.m_data[i] = a.m_data[i];
54 }
55 for (size_t i = 0; i < s1; i++) {
56 result.m_data[s0 + i] = b.m_data[i];
57 }
58 return result;
59 }
60
61 public:
62 template<size_t sz0, size_t... sz>
63 constexpr static auto operator()(FixedString<sz0> s0, FixedString<sz>... strs)
64 -> FixedString<sz0 + (sz + ... + 0zu)> {
65 return h(s0, FixedStringConcat::operator()(strs...));
66 }
67
68 constexpr static auto operator()() -> FixedString<0> { return { {} }; }
69 };
70}
71
72constexpr inline auto fixed_string_concat = detail::FixedStringConcat {};
73}
74
75namespace di {
77}
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:29
static constexpr auto size()
Definition fixed_string.h:24
constexpr auto empty() const
Definition fixed_string.h:26
char m_data[count+1]
Definition fixed_string.h:14
constexpr auto begin() const -> char const *
Definition fixed_string.h:28
Definition sequence.h:12
constexpr auto fixed_string_concat
Definition fixed_string.h:72
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