cMHN 1.2
C++ library for learning MHNs with pRC
Loading...
Searching...
No Matches
string.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-2-Clause
2
3#ifndef pRC_CORE_BASIC_STRING_H
4#define pRC_CORE_BASIC_STRING_H
5
6#include <prc/config.hpp>
10#include <prc/core/log/log.hpp>
11
12namespace pRC
13{
14 template<Size N>
15 class String
16 {
17 public:
18 static constexpr auto size()
19 {
20 return N;
21 }
22
23 static constexpr auto empty()
24 {
25 return !size();
26 }
27
28 private:
29 using Type = char;
30
31 static constexpr auto check([[maybe_unused]] Index const index)
32 {
33 if constexpr(cDebugLevel >= DebugLevel::Low)
34 {
35 if(!(index <= size()))
36 {
37 Logging::error("String index out of range.");
38 }
39 }
40 }
41
42 public:
43 ~String() = default;
44 constexpr String(String const &) = default;
45 constexpr String(String &&) = default;
46 constexpr String &operator=(String const &) & = default;
47 constexpr String &operator=(String &&) & = default;
48
49 constexpr String()
50 : mData{}
51 {
52 }
53
54 constexpr String(char const (&string)[N + 1])
55 : String(makeSeries<Index, N + 1>(), string)
56 {
57 }
58
59 template<IsSame<char>... Ts>
60 requires(sizeof...(Ts) == size())
61 constexpr String(Ts const &...chars)
62 : mData{chars..., '\0'}
63 {
64 }
65
66 constexpr decltype(auto) operator[](Index const index) &&
67 {
68 check(index);
69 return move(mData)[index];
70 }
71
72 constexpr decltype(auto) operator[](Index const index) const &&
73 {
74 check(index);
75 return move(mData)[index];
76 }
77
78 constexpr decltype(auto) operator[](Index const index) &
79 {
80 check(index);
81 return mData[index];
82 }
83
84 constexpr decltype(auto) operator[](Index const index) const &
85 {
86 check(index);
87 return mData[index];
88 }
89
90 constexpr auto find(char const character) const
91 {
92 for(Index i = 0; i < size(); ++i)
93 {
94 if(mData[i] == character)
95 {
96 return i;
97 }
98 }
99 return N;
100 }
101
102 template<Index P, Index L>
103 requires(P + L < N)
104 constexpr auto substring() const
105 {
106 String<L> sub;
107 range<L>(
108 [this, &sub](auto const i)
109 {
110 sub[i] = mData[P + i];
111 });
112
113 return sub;
114 }
115
116 constexpr auto cString() && = delete;
117 constexpr auto cString() const && = delete;
118
119 constexpr auto cString() &
120 {
121 return mData;
122 }
123
124 constexpr auto cString() const &
125 {
126 return mData;
127 }
128
129 private:
130 template<Index... Is>
131 requires(sizeof...(Is) == N + 1)
132 constexpr String(Sequence<Index, Is...> const,
133 char const (&string)[N + 1])
134 : mData{string[Is]...}
135 {
136 }
137
138 private:
139 alignas(cMaxDefaultAlignment) Type mData[size() + 1] = {};
140 };
141
142 String(char const &) -> String<1>;
143 template<Size N>
144 String(char const (&)[N]) -> String<N - 1>;
145
146 template<class T>
147 concept IsString = !IsReference<T> && requires {
148 {
149 []<Size N>(String<N> const &&)
150 {
151 }(std::declval<T>())
152 };
153 };
154
155 template<Size M, Size N>
156 static inline constexpr auto operator+(String<M> const &lhs,
157 String<N> const &rhs)
158 {
159 String<M + N> concat;
160 range<M>(
161 [&concat, &lhs](auto const i)
162 {
163 concat[i] = lhs[i];
164 });
165 range<N>(
166 [&concat, &rhs](auto const i)
167 {
168 concat[M + i] = rhs[i];
169 });
170
171 return concat;
172 }
173
174 template<Size M, Size N>
175 static inline constexpr auto operator+(String<M> const &lhs,
176 char const (&rhs)[N])
177 {
178 return lhs + String(rhs);
179 }
180
181 template<Size M, Size N>
182 static inline constexpr auto operator+(char const (&lhs)[M],
183 String<N> const &rhs)
184 {
185 return String(lhs) + rhs;
186 }
187}
188#endif // pRC_CORE_BASIC_STRING_H
Definition sequence.hpp:29
Definition string.hpp:16
static constexpr auto size()
Definition string.hpp:18
~String()=default
constexpr auto find(char const character) const
Definition string.hpp:90
constexpr auto cString() const &
Definition string.hpp:124
constexpr String(Ts const &...chars)
Definition string.hpp:61
constexpr String(char const (&string)[N+1])
Definition string.hpp:54
constexpr auto cString() &&=delete
constexpr String(String &&)=default
constexpr String & operator=(String const &) &=default
static constexpr auto empty()
Definition string.hpp:23
constexpr String()
Definition string.hpp:49
constexpr auto substring() const
Definition string.hpp:104
constexpr auto cString() const &&=delete
constexpr String(String const &)=default
constexpr String & operator=(String &&) &=default
Definition concepts.hpp:19
Definition string.hpp:147
int i
Definition gmock-matchers-comparisons_test.cc:603
static void error(Xs &&...args)
Definition log.hpp:14
Definition cholesky.hpp:10
Size Index
Definition basics.hpp:32
std::size_t Size
Definition basics.hpp:31
static constexpr auto makeSeries()
Definition sequence.hpp:390
constexpr auto cDebugLevel
Definition config.hpp:48
static constexpr auto range(F &&f, Xs &&...args)
Definition range.hpp:18
static constexpr auto operator+(Sequence< T, As... > const, Sequence< T, Bs... > const)
Definition sequence.hpp:107
String(char const &) -> String< 1 >
constexpr Size cMaxDefaultAlignment
Definition config.hpp:54