IceShard 1
A personal game engine project, with development focused on 2D/2.5D games.
Loading...
Searching...
No Matches
readonly_operations.hxx
Go to the documentation of this file.
1
3
4#pragma once
5#include <utility>
7
8namespace ice::string
9{
10
11 using ice::concepts::StringType;
12
14 {
15 template<StringType Self>
16 constexpr bool is_empty(this Self const& self) noexcept
17 {
18 return self.size() == 0;
19 }
20
21 template<StringType Self>
22 constexpr bool not_empty(this Self const& self) noexcept
23 {
24 return self.size() > 0;
25 }
26
27 template<StringType Self>
28 constexpr auto front(this Self const& self) noexcept -> typename Self::CharType
29 {
30 return self.data()[0];
31 }
32
33 template<StringType Self>
34 constexpr auto back(this Self const& self) noexcept -> typename Self::CharType
35 {
36 return self.data()[self.size().native() - 1];
37 }
38
39 template<StringType Self>
40 constexpr auto substr(
41 this Self const& self, ice::nindex pos, ice::ncount len = {}
42 ) noexcept -> typename Self::StringType
43 {
44 ice::ncount const size = self.size();
45 if (pos >= size)
46 {
47 return { };
48 }
49
50 if (len == ice::ncount_none)
51 {
52 return { self.data() + pos, size - pos };
53 }
54 else
55 {
56 return { self.data() + pos, std::min(len, size - pos) };
57 }
58 }
59
60 template<StringType Self>
61 constexpr auto substr(
62 this Self const& self, ice::ref32 ref
63 ) noexcept -> typename Self::StringType
64 {
65 return self.substr(ref.offset, ref.size);
66 }
67
68 template<StringType Self>
69 constexpr auto starts_with(this Self const& self, StringType auto prefix) noexcept
70 {
71 return self.substr(0, prefix.size()) == typename Self::StringType{ prefix };
72 }
73
74 template<StringType Self>
75 constexpr auto find_first_of(
76 this Self const& self,
77 typename Self::CharType character_value,
78 ice::nindex start = 0
79 ) noexcept -> ice::nindex
80 {
81 auto const* it = self.cbegin() + start;
82 auto const* const beg = it;
83 auto const* const end = self.cend();
84
85 while (it < end && *it != character_value)
86 {
87 it += 1;
88 }
89
90 return it >= end ? ice::nindex{} : ice::nindex{ start + (it - beg) };
91 }
92
93 template<StringType Self>
94 constexpr auto find_first_of(
95 this Self const& self,
96 typename Self::StringType character_values,
97 ice::nindex start = 0
98 ) noexcept -> ice::nindex
99 {
100 auto const* it = self.cbegin() + start;
101 auto const* const beg = it;
102 auto const* const it_end = self.cend();
103
104 while (it < it_end && character_values.find_first_of(*it) == nindex_none)
105 {
106 it += 1;
107 }
108
109 return it >= it_end ? ice::nindex{} : ice::nindex{ start + (it - beg) };
110 }
111
112 template<StringType Self>
113 constexpr auto find_last_of(
114 this Self const& self,
115 typename Self::CharType character_value,
116 ice::nindex start = 0
117 ) noexcept -> ice::nindex
118 {
119 auto it = self.crbegin();
120 auto const it_end = self.crend();
121
122 while (it != it_end && start > 0)
123 {
124 it += 1;
125 start -= 1;
126 }
127
128 while (it != it_end && *it != character_value)
129 {
130 it += 1;
131 }
132
133 return it == it_end ? ice::nindex{} : ice::nindex{ u64((it_end - it) - 1) };
134 }
135
136 template<StringType Self>
137 constexpr auto find_last_of(
138 this Self const& self,
139 typename Self::StringType character_values,
140 ice::nindex start = 0
141 ) noexcept -> ice::nindex
142 {
143 auto it = self.crbegin();
144 auto const it_end = self.crend();
145
146 while (it != it_end && start > 0)
147 {
148 it += 1;
149 start -= 1;
150 }
151
152 while (it != it_end && character_values.find_first_of(*it) == nindex_none)
153 {
154 it += 1;
155 }
156
157 return it == it_end ? ice::nindex{} : ice::nindex{ u64((it_end - it) - 1) };
158 }
159
160 template<StringType Self>
161 constexpr auto find_first_not_of(
162 this Self const& self,
163 typename Self::CharType character_value,
164 ice::nindex start_idx = 0
165 ) noexcept -> ice::nindex
166 {
167 auto const* it = self.cbegin() + start_idx;
168 auto const* const beg = it;
169 auto const* const end = self.cend();
170
171 while (it < end && *it == character_value)
172 {
173 it += 1;
174 }
175
176 return it >= end ? ice::nindex{} : start_idx + ice::nindex{ u64(it - beg) };
177 }
178
179 template<StringType Self>
180 constexpr auto find_first_not_of(
181 this Self const& self,
182 typename Self::StringType character_values,
183 ice::nindex start_idx = 0
184 ) noexcept -> ice::nindex
185 {
186 auto const* it = self.cbegin() + start_idx;
187 auto const* const beg = it;
188 auto const* const it_end = self.cend();
189
190 while (it < it_end && character_values.find_first_of(*it) != ice::nindex_none)
191 {
192 it += 1;
193 }
194
195 return it >= it_end ? ice::nindex{} : start_idx + ice::nindex{ u64(it - beg) };
196 }
197
198 template<StringType Self>
199 constexpr auto find_last_not_of(
200 this Self const& self,
201 typename Self::CharType character_value,
202 ice::nindex start_idx = 0
203 ) noexcept -> ice::nindex
204 {
205 auto it = self.crbegin();
206 auto const end = self.crend();
207
208 while (it != end && start_idx > 0)
209 {
210 it += 1;
211 start_idx -= 1;
212 }
213
214 while (it != end && *it == character_value)
215 {
216 it += 1;
217 }
218
219 return it == end ? ice::nindex{} : ice::nindex{ u64((end - it) - 1) };
220 }
221
222 template<StringType Self>
223 constexpr auto find_last_not_of(
224 this Self const& self,
225 typename Self::StringType character_values,
226 ice::nindex start_idx = 0
227 ) noexcept -> ice::nindex
228 {
229 auto it = self.crbegin();
230 auto const it_end = self.crend();
231
232 while (it != it_end && start_idx > 0)
233 {
234 it += 1;
235 start_idx -= 1;
236 }
237
238 while (it != it_end && character_values.find_first_of(*it) != ice::nindex_none)
239 {
240 it += 1;
241 }
242
243 return it == it_end ? ice::nindex{} : ice::nindex{ (it_end - it) - 1 };
244 }
245
246 // Iterators
247
248 template<StringType Self>
249 constexpr auto cbegin(this Self const& self) noexcept -> typename Self::ConstIterator
250 {
251 return self.data();
252 }
253
254 template<StringType Self>
255 constexpr auto cend(this Self const& self) noexcept -> typename Self::ConstIterator
256 {
257 return self.data() + self.size();
258 }
259
260 template<StringType Self>
261 constexpr auto crbegin(this Self const& self) noexcept -> typename Self::ConstReverseIterator
262 {
263 return typename Self::ConstReverseIterator{ self.data() + self.size() };
264 }
265
266 template<StringType Self>
267 constexpr auto crend(this Self const& self) noexcept -> typename Self::ConstReverseIterator
268 {
269 return typename Self::ConstReverseIterator{ self.data() };
270 }
271
272 template<StringType Self>
273 constexpr auto begin(this Self const& self) noexcept -> typename Self::ConstIterator
274 {
275 return self.cbegin();
276 }
277
278 template<StringType Self>
279 constexpr auto end(this Self const& self) noexcept -> typename Self::ConstIterator
280 {
281 return self.cend();
282 }
283
284 template<StringType Self>
285 constexpr auto rbegin(this Self const& self) noexcept -> typename Self::ConstReverseIterator
286 {
287 return self.crbegin();
288 }
289
290 template<StringType Self>
291 constexpr auto rend(this Self const& self) noexcept -> typename Self::ConstReverseIterator
292 {
293 return self.crend();
294 }
295
296 // Operators
297
298 template<StringType Self>
299 constexpr auto operator[](this Self const& self, ice::nindex index) noexcept -> typename Self::ValueType
300 {
301 return self.data()[index.native()];
302 }
303
304 template<StringType Self>
305 constexpr bool operator==(this Self const& self, typename Self::StringType const& other) noexcept
306 {
307 ice::ncount const size = self.size();
308 if (size == other.size())
309 {
310 typename Self::ValueType const* self_data = self.data();
311 typename Self::ValueType const* other_data = other.data();
312
314 while (idx < size && self_data[idx] == other_data[idx])
315 {
316 idx += 1;
317 }
318 return idx == size;
319 }
320 return false;
321 }
322
323 // Data Helpers
324
325 template<StringType Self>
326 constexpr auto meminfo(this Self const& self) noexcept -> ice::meminfo
327 {
328 return ice::meminfo{
329 ice::meminfo_of<typename Self::CharType> * self.size().native()
330 };
331 }
332 };
333
334} // namespace ice::string
Definition editable_operations.hxx:9
SPDX-License-Identifier: MIT.
Definition array.hxx:12
std::uint64_t u64
Definition types.hxx:27
constexpr ice::meminfo meminfo_of
Definition mem_info.hxx:18
static constexpr ice::ncount_invalid_t ncount_none
Definition ncount.hxx:60
static constexpr ice::nindex_invalid_t nindex_none
Definition nindex.hxx:56
Definition mem_size_types.hxx:59
Definition ncount.hxx:14
Definition nindex.hxx:13
ice::detail::nvalue_base_utype base_type
Definition nvalue.hxx:75
Holds 'offset' and 'size' fields (u32) to access data stored in a buffer-like object.
Definition ref.hxx:12
Definition readonly_operations.hxx:14
constexpr bool is_empty(this Self const &self) noexcept
Definition readonly_operations.hxx:16
constexpr auto front(this Self const &self) noexcept -> typename Self::CharType
Definition readonly_operations.hxx:28
constexpr auto meminfo(this Self const &self) noexcept -> ice::meminfo
Definition readonly_operations.hxx:326
constexpr auto crend(this Self const &self) noexcept -> typename Self::ConstReverseIterator
Definition readonly_operations.hxx:267
constexpr auto find_first_of(this Self const &self, typename Self::StringType character_values, ice::nindex start=0) noexcept -> ice::nindex
Definition readonly_operations.hxx:94
constexpr auto find_last_of(this Self const &self, typename Self::StringType character_values, ice::nindex start=0) noexcept -> ice::nindex
Definition readonly_operations.hxx:137
constexpr auto end(this Self const &self) noexcept -> typename Self::ConstIterator
Definition readonly_operations.hxx:279
constexpr auto operator[](this Self const &self, ice::nindex index) noexcept -> typename Self::ValueType
Definition readonly_operations.hxx:299
constexpr auto find_first_not_of(this Self const &self, typename Self::CharType character_value, ice::nindex start_idx=0) noexcept -> ice::nindex
Definition readonly_operations.hxx:161
constexpr bool not_empty(this Self const &self) noexcept
Definition readonly_operations.hxx:22
constexpr auto find_first_of(this Self const &self, typename Self::CharType character_value, ice::nindex start=0) noexcept -> ice::nindex
Definition readonly_operations.hxx:75
constexpr auto find_last_of(this Self const &self, typename Self::CharType character_value, ice::nindex start=0) noexcept -> ice::nindex
Definition readonly_operations.hxx:113
constexpr auto begin(this Self const &self) noexcept -> typename Self::ConstIterator
Definition readonly_operations.hxx:273
constexpr auto substr(this Self const &self, ice::ref32 ref) noexcept -> typename Self::StringType
Definition readonly_operations.hxx:61
constexpr auto find_first_not_of(this Self const &self, typename Self::StringType character_values, ice::nindex start_idx=0) noexcept -> ice::nindex
Definition readonly_operations.hxx:180
constexpr auto rbegin(this Self const &self) noexcept -> typename Self::ConstReverseIterator
Definition readonly_operations.hxx:285
constexpr auto crbegin(this Self const &self) noexcept -> typename Self::ConstReverseIterator
Definition readonly_operations.hxx:261
constexpr auto find_last_not_of(this Self const &self, typename Self::StringType character_values, ice::nindex start_idx=0) noexcept -> ice::nindex
Definition readonly_operations.hxx:223
constexpr auto find_last_not_of(this Self const &self, typename Self::CharType character_value, ice::nindex start_idx=0) noexcept -> ice::nindex
Definition readonly_operations.hxx:199
constexpr auto starts_with(this Self const &self, StringType auto prefix) noexcept
Definition readonly_operations.hxx:69
constexpr auto back(this Self const &self) noexcept -> typename Self::CharType
Definition readonly_operations.hxx:34
constexpr bool operator==(this Self const &self, typename Self::StringType const &other) noexcept
Definition readonly_operations.hxx:305
constexpr auto cend(this Self const &self) noexcept -> typename Self::ConstIterator
Definition readonly_operations.hxx:255
constexpr auto substr(this Self const &self, ice::nindex pos, ice::ncount len={}) noexcept -> typename Self::StringType
Definition readonly_operations.hxx:40
constexpr auto cbegin(this Self const &self) noexcept -> typename Self::ConstIterator
Definition readonly_operations.hxx:249
constexpr auto rend(this Self const &self) noexcept -> typename Self::ConstReverseIterator
Definition readonly_operations.hxx:291