pRC
multi-purpose Tensor Train library for C++
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
10
11namespace pRC
12{
13 String(char const &)->String<1>;
14 template<Size N>
15 String(char const (&)[N]) -> String<N - 1>;
16
17 template<Size N>
18 class String
19 {
20 public:
21 static constexpr auto size()
22 {
23 return N;
24 }
25
26 static constexpr auto empty()
27 {
28 return !size();
29 }
30
31 private:
32 using Type = char;
33
34 static constexpr auto check([[maybe_unused]] Index const index)
35 {
36 if constexpr(cDebugLevel >= DebugLevel::Low)
37 {
38 if(!(index <= size()))
39 {
40 Logging::error("String index out of range.");
41 }
42 }
43 }
44
45 public:
46 ~String() = default;
47 constexpr String(String const &) = default;
48 constexpr String(String &&) = default;
49 constexpr String &operator=(String const &) & = default;
50 constexpr String &operator=(String &&) & = default;
51 constexpr String()
52 : mData{}
53 {
54 }
55
56 constexpr String(char const (&string)[N + 1])
57 : String(makeSeries<Index, N + 1>(), string)
58 {
59 }
60
61 template<class... Ts,
62 If<All<IsSame<char, Ts...>,
63 IsSatisfied<(sizeof...(Ts) == size())>>> = 0>
64 constexpr String(Ts const &...chars)
65 : mData{chars..., '\0'}
66 {
67 }
68
69 constexpr decltype(auto) operator[](Index const index) &&
70 {
71 check(index);
72 return move(mData)[index];
73 }
74
75 constexpr decltype(auto) operator[](Index const index) const &&
76 {
77 check(index);
78 return move(mData)[index];
79 }
80
81 constexpr decltype(auto) operator[](Index const index) &
82 {
83 check(index);
84 return mData[index];
85 }
86
87 constexpr decltype(auto) operator[](Index const index) const &
88 {
89 check(index);
90 return mData[index];
91 }
92
93 constexpr auto find(char const character) const
94 {
95 for(Index i = 0; i < size(); ++i)
96 {
97 if(mData[i] == character)
98 {
99 return i;
100 }
101 }
102 return N;
103 }
104
105 template<Index P, Index L,
106 If<IsSatisfied<P + L<N>> = 0> constexpr auto substring() const
107 {
109 range<L>(
110 [this, &sub](auto const i)
111 {
112 sub[i] = mData[P + i];
113 });
114
115 return sub;
116 }
117
118 constexpr auto cString() && = delete;
119 constexpr auto cString() const && = delete;
120
122 {
123 return mData.data();
124 }
125
126 constexpr auto cString() const &
127 {
128 return mData.data();
129 }
130
131 private:
132 template<Index... Is, If<IsSatisfied<(sizeof...(Is) == N + 1)>> = 0>
133 constexpr String(Sequence<Index, Is...> const,
134 char const (&string)[N + 1])
135 : mData{string[Is]...}
136 {
137 }
138
139 private:
140 StackArray<Type, size() + 1> mData;
141 };
142
143 template<Size M, Size N>
144 static inline constexpr auto operator+(String<M> const &lhs,
145 String<N> const &rhs)
146 {
148 range<M>(
149 [&concat, &lhs](auto const i)
150 {
151 concat[i] = lhs[i];
152 });
153 range<N>(
154 [&concat, &rhs](auto const i)
155 {
156 concat[M + i] = rhs[i];
157 });
158
159 return concat;
160 }
161
162 template<Size M, Size N>
163 static inline constexpr auto operator+(String<M> const &lhs,
164 char const (&rhs)[N])
165 {
166 return lhs + String(rhs);
167 }
168
169 template<Size M, Size N>
170 static inline constexpr auto operator+(char const (&lhs)[M],
171 String<N> const &rhs)
172 {
173 return String(lhs) + rhs;
174 }
175}
176#endif // pRC_CORE_BASIC_STRING_H
Definition string.hpp:19
static constexpr auto size()
Definition string.hpp:21
~String()=default
constexpr auto find(char const character) const
Definition string.hpp:93
constexpr String(char const (&string)[N+1])
Definition string.hpp:56
constexpr String(String &&)=default
constexpr String & operator=(String const &) &=default
static constexpr auto empty()
Definition string.hpp:26
constexpr String()
Definition string.hpp:51
constexpr String(String const &)=default
constexpr String & operator=(String &&) &=default
constexpr String(Ts const &...chars)
Definition string.hpp:64
static void error(Xs &&...args)
Definition log.hpp:14
Definition cholesky.hpp:18
CommonArray< Allocation::Stack, T, Ns... > StackArray
Definition type_traits.hpp:52
std::conjunction< Bs... > All
Definition type_traits.hpp:77
std::enable_if_t< B{}, int > If
Definition type_traits.hpp:68
static constexpr auto makeSeries()
Definition sequence.hpp:361
Constant< Bool, B > IsSatisfied
Definition type_traits.hpp:71
static constexpr Conditional< IsSatisfied< C >, RemoveConstReference< X >, X > copy(X &&a)
Definition copy.hpp:13
constexpr auto cDebugLevel
Definition config.hpp:46
static constexpr auto operator+(Sequence< T, As... > const, Sequence< T, Bs... > const)
Definition sequence.hpp:110
Size Index
Definition type_traits.hpp:21
Definition type_traits.hpp:99