GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
CStringView.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_CORE_DATA_C_STRING_VIEW_HPP
5#define GREM_CORE_DATA_C_STRING_VIEW_HPP
6
8
13
14#include <compare> // std::weak_ordering
15#include <cstddef> // std::size_t, std::ptrdiff_t, std::nullptr_t
16#include <iosfwd> // std::basic_ostream
17#include <iterator> // std::reverse_iterator
18#include <limits> // std::numeric_limits
19#include <string> // std::char_traits
20#include <utility> // std::swap
21
22namespace grem {
23
24namespace detail {
25
26template <typename CharT, typename Traits>
27struct CStringViewSentinel {
28 [[nodiscard]] GREM_ALWAYS_INLINE constexpr bool operator==(const CharT* it) const {
29 GREM_ASSERT(it);
30 return Traits::eq(*it, CharT());
31 }
32};
33
34} // namespace detail
35
36template <typename CharT, typename Traits = std::char_traits<CharT>>
38public:
39 using traits_type = Traits;
40 using value_type = CharT;
41 using pointer = CharT*;
42 using const_pointer = const CharT*;
43 using reference = CharT&;
44 using const_reference = const CharT&;
47 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
49 using size_type = std::size_t;
50 using difference_type = std::ptrdiff_t;
51 using sentinel = detail::CStringViewSentinel<CharT, Traits>;
52
53 static constexpr size_type npos = static_cast<size_type>(-1);
54
55 GREM_ALWAYS_INLINE constexpr CStringViewBase() noexcept = default;
56
57 GREM_ALWAYS_INLINE constexpr CStringViewBase(const CharT* s)
58 : string(s) {}
59
60 template <typename Allocator>
62 : string(s.c_str()) {
63 GREM_ASSERT(string);
64 }
65
66 GREM_ALWAYS_INLINE constexpr CStringViewBase(std::nullptr_t) = delete;
67
69 GREM_ASSERT(string);
70 return StringViewBase<CharT, Traits>{string};
71 }
72
73 [[nodiscard]] GREM_ALWAYS_INLINE constexpr const_iterator begin() const noexcept {
74 return const_iterator{string};
75 }
76
77 [[nodiscard]] GREM_ALWAYS_INLINE constexpr const_iterator cbegin() const noexcept {
78 return begin();
79 }
80
81 [[nodiscard]] GREM_ALWAYS_INLINE constexpr sentinel end() const noexcept {
82 return sentinel{};
83 }
84
85 [[nodiscard]] GREM_ALWAYS_INLINE constexpr sentinel cend() const noexcept {
86 return end();
87 }
88
89 [[nodiscard]] GREM_ALWAYS_INLINE constexpr const_reverse_iterator rbegin() const noexcept {
90 return const_reverse_iterator{const_iterator{string + size()}};
91 }
92
93 [[nodiscard]] GREM_ALWAYS_INLINE constexpr const_reverse_iterator crbegin() const noexcept {
94 return rbegin();
95 }
96
97 [[nodiscard]] GREM_ALWAYS_INLINE constexpr const_reverse_iterator rend() const noexcept {
99 }
100
101 [[nodiscard]] GREM_ALWAYS_INLINE constexpr const_reverse_iterator crend() const noexcept {
102 return rend();
103 }
104
105 [[nodiscard]] GREM_ALWAYS_INLINE constexpr const_reference operator[](size_type pos) const {
106 GREM_ASSERT(string);
107 return string[pos];
108 }
109
110 [[nodiscard]] GREM_ALWAYS_INLINE constexpr const_reference at(size_type pos) const {
111 return static_cast<StringViewBase<CharT, Traits>>(*this).at(pos);
112 }
113
114 [[nodiscard]] GREM_ALWAYS_INLINE constexpr const_reference front() const {
115 GREM_ASSERT(!empty());
116 return string[0];
117 }
118
119 [[nodiscard]] GREM_ALWAYS_INLINE constexpr const_reference back() const {
120 GREM_ASSERT(!empty());
121 return string[size() - 1];
122 }
123
124 [[nodiscard]] GREM_ALWAYS_INLINE constexpr const_pointer data() const noexcept {
125 return string;
126 }
127
128 [[nodiscard]] GREM_ALWAYS_INLINE constexpr const_pointer c_str() const noexcept {
129 return string;
130 }
131
132 [[nodiscard]] GREM_ALWAYS_INLINE constexpr size_type size() const noexcept {
133 GREM_ASSERT(string);
134 return Traits::length(string);
135 }
136
137 [[nodiscard]] GREM_ALWAYS_INLINE constexpr size_type length() const noexcept {
138 return size();
139 }
140
141 [[nodiscard]] GREM_ALWAYS_INLINE constexpr size_type max_size() const noexcept {
142 constexpr size_type MAX_DIFFERENCE = static_cast<size_type>(std::numeric_limits<difference_type>::max());
143 constexpr size_type MAX_COUNT = static_cast<size_type>(std::numeric_limits<size_type>::max() / sizeof(value_type));
144 return ((MAX_DIFFERENCE < MAX_COUNT) ? MAX_DIFFERENCE : MAX_COUNT) - 1;
145 }
146
147 [[nodiscard]] GREM_ALWAYS_INLINE constexpr bool empty() const noexcept {
148 GREM_ASSERT(string);
149 return Traits::eq(string[0], CharT());
150 }
151
153 GREM_ASSERT(string);
154 string += n;
155 }
156
157 GREM_ALWAYS_INLINE constexpr void swap(CStringViewBase& v) noexcept {
158 using std::swap;
159 swap(string, v.string);
160 }
161
162 GREM_ALWAYS_INLINE constexpr size_type copy(CharT* dest, size_type count, size_type pos = 0) const {
163 return static_cast<StringViewBase<CharT, Traits>>(*this).copy(dest, count, pos);
164 }
165
166 [[nodiscard]] GREM_ALWAYS_INLINE constexpr CStringViewBase substr(size_type pos = 0) const {
167 GREM_ASSERT(string);
168 return CStringViewBase{string + pos};
169 }
170
172 return static_cast<StringViewBase<CharT, Traits>>(*this).substr(pos, count);
173 }
174
175 [[nodiscard]] GREM_ALWAYS_INLINE constexpr int compare(StringViewBase<CharT, Traits> v) const {
176 return compareSubString(v.begin(), v.end());
177 }
178
179 template <typename Allocator>
180 [[nodiscard]] GREM_ALWAYS_INLINE constexpr int compare(const StringBase<CharT, Traits, Allocator>& v) const {
181 return compareSubString(v.begin(), v.end());
182 }
183
184 [[nodiscard]] GREM_ALWAYS_INLINE constexpr int compare(CStringViewBase v) const noexcept {
185 return compareSubString(v.begin(), v.end());
186 }
187
188 [[nodiscard]] GREM_ALWAYS_INLINE constexpr int compare(size_type pos1, size_type count1, CStringViewBase v) const {
189 return substr(pos1, count1).compare(static_cast<StringViewBase<CharT, Traits>>(v));
190 }
191
192 [[nodiscard]] GREM_ALWAYS_INLINE constexpr int compare(size_type pos1, size_type count1, CStringViewBase v, size_type pos2, size_type count2) const {
193 return substr(pos1, count1).compare(v.substr(pos2, count2));
194 }
195
196 [[nodiscard]] GREM_ALWAYS_INLINE constexpr int compare(const CharT* s) const {
197 return compare(CStringViewBase{s});
198 }
199
200 [[nodiscard]] GREM_ALWAYS_INLINE constexpr int compare(size_type pos1, size_type count1, const CharT* s) const {
201 return substr(pos1, count1).compare(StringViewBase<CharT, Traits>{s});
202 }
203
204 [[nodiscard]] GREM_ALWAYS_INLINE constexpr int compare(size_type pos1, size_type count1, const CharT* s, size_type count2) const {
205 return substr(pos1, count1).compare(StringViewBase<CharT, Traits>{s, count2});
206 }
207
208 [[nodiscard]] GREM_ALWAYS_INLINE constexpr bool starts_with(StringViewBase<CharT, Traits> sv) const noexcept {
209 return startsWithSubString(sv.begin(), sv.end());
210 }
211
212 [[nodiscard]] GREM_ALWAYS_INLINE constexpr bool starts_with(CStringViewBase sv) const noexcept {
213 return startsWithSubString(sv.begin(), sv.end());
214 }
215
216 [[nodiscard]] GREM_ALWAYS_INLINE constexpr bool starts_with(CharT ch) const noexcept {
217 return startsWithCharacter(ch);
218 }
219
220 [[nodiscard]] GREM_ALWAYS_INLINE constexpr bool starts_with(const CharT* s) const {
221 return starts_with(CStringViewBase{s});
222 }
223
224 [[nodiscard]] GREM_ALWAYS_INLINE constexpr bool ends_with(StringViewBase<CharT, Traits> sv) const noexcept {
225 return static_cast<StringViewBase<CharT, Traits>>(*this).ends_with(sv);
226 }
227
228 template <typename Allocator>
229 [[nodiscard]] GREM_ALWAYS_INLINE constexpr bool ends_with(const StringBase<CharT, Traits, Allocator>& s) const noexcept {
230 return static_cast<StringViewBase<CharT, Traits>>(*this).ends_with(s);
231 }
232
233 [[nodiscard]] GREM_ALWAYS_INLINE constexpr bool ends_with(CStringViewBase sv) const noexcept {
234 return static_cast<StringViewBase<CharT, Traits>>(*this).ends_with(static_cast<StringViewBase<CharT, Traits>>(sv));
235 }
236
237 [[nodiscard]] GREM_ALWAYS_INLINE constexpr bool ends_with(CharT ch) const noexcept {
238 return static_cast<StringViewBase<CharT, Traits>>(*this).ends_with(ch);
239 }
240
241 [[nodiscard]] GREM_ALWAYS_INLINE constexpr bool ends_with(const CharT* s) const {
242 return static_cast<StringViewBase<CharT, Traits>>(*this).ends_with(s);
243 }
244
245 [[nodiscard]] GREM_ALWAYS_INLINE constexpr bool contains(StringViewBase<CharT, Traits> sv) const noexcept {
246 return find(sv) != npos;
247 }
248
249 template <typename Allocator>
250 [[nodiscard]] GREM_ALWAYS_INLINE constexpr bool contains(const StringBase<CharT, Traits, Allocator>& s) const noexcept {
251 return find(s) != npos;
252 }
253
254 [[nodiscard]] GREM_ALWAYS_INLINE constexpr bool contains(CStringViewBase sv) const noexcept {
255 return find(sv) != npos;
256 }
257
258 [[nodiscard]] GREM_ALWAYS_INLINE constexpr bool contains(CharT c) const noexcept {
259 return find(c) != npos;
260 }
261
262 [[nodiscard]] GREM_ALWAYS_INLINE constexpr bool contains(const CharT* s) const noexcept {
263 return find(s) != npos;
264 }
265
266 [[nodiscard]] GREM_ALWAYS_INLINE constexpr size_type find(StringViewBase<CharT, Traits> sv, size_type pos = 0) const noexcept {
267 return findSubString(sv.begin(), sv.end(), pos);
268 }
269
270 template <typename Allocator>
271 [[nodiscard]] GREM_ALWAYS_INLINE constexpr size_type find(const StringBase<CharT, Traits, Allocator>& s, size_type pos = 0) const noexcept {
272 return findSubString(s.begin(), s.end(), pos);
273 }
274
275 [[nodiscard]] GREM_ALWAYS_INLINE constexpr size_type find(CStringViewBase sv, size_type pos = 0) const noexcept {
276 return findSubString(sv.begin(), sv.end(), pos);
277 }
278
279 [[nodiscard]] GREM_ALWAYS_INLINE constexpr size_type find(CharT ch, size_type pos = 0) const noexcept {
280 return findCharacter(ch, pos);
281 }
282
283 [[nodiscard]] GREM_ALWAYS_INLINE constexpr size_type find(const CharT* s, size_type pos, size_type count) const {
285 }
286
287 [[nodiscard]] GREM_ALWAYS_INLINE constexpr size_type find(const CharT* s, size_type pos = 0) const {
288 return find(CStringViewBase{s}, pos);
289 }
290
291 [[nodiscard]] GREM_ALWAYS_INLINE constexpr size_type rfind(StringViewBase<CharT, Traits> sv, size_type pos = npos) const noexcept {
292 return static_cast<StringViewBase<CharT, Traits>>(*this).rfind(sv, pos);
293 }
294
295 template <typename Allocator>
296 [[nodiscard]] GREM_ALWAYS_INLINE constexpr size_type rfind(const StringBase<CharT, Traits, Allocator>& s, size_type pos = npos) const noexcept {
297 return static_cast<StringViewBase<CharT, Traits>>(*this).rfind(s, pos);
298 }
299
300 [[nodiscard]] GREM_ALWAYS_INLINE constexpr size_type rfind(CStringViewBase sv, size_type pos = npos) const noexcept {
301 return static_cast<StringViewBase<CharT, Traits>>(*this).rfind(sv.c_str(), pos);
302 }
303
304 [[nodiscard]] GREM_ALWAYS_INLINE constexpr size_type rfind(CharT ch, size_type pos = npos) const noexcept {
305 return static_cast<StringViewBase<CharT, Traits>>(*this).rfind(ch, pos);
306 }
307
308 [[nodiscard]] GREM_ALWAYS_INLINE constexpr size_type rfind(const CharT* s, size_type pos, size_type count) const {
309 return static_cast<StringViewBase<CharT, Traits>>(*this).rfind(s, pos, count);
310 }
311
312 [[nodiscard]] GREM_ALWAYS_INLINE constexpr size_type rfind(const CharT* s, size_type pos = npos) const {
313 return static_cast<StringViewBase<CharT, Traits>>(*this).rfind(s, pos);
314 }
315
316 [[nodiscard]] GREM_ALWAYS_INLINE constexpr size_type find_first_of(StringViewBase<CharT, Traits> v, size_type pos = 0) const noexcept {
317 return findFirstOfSubString(v.begin(), v.end(), pos);
318 }
319
320 template <typename Allocator>
321 [[nodiscard]] GREM_ALWAYS_INLINE constexpr size_type find_first_of(const StringBase<CharT, Traits, Allocator>& s, size_type pos = 0) const noexcept {
322 return findFirstOfSubString(s.begin(), s.end(), pos);
323 }
324
325 [[nodiscard]] GREM_ALWAYS_INLINE constexpr size_type find_first_of(CStringViewBase v, size_type pos = 0) const noexcept {
326 return findFirstOfSubString(v.begin(), v.end(), pos);
327 }
328
329 [[nodiscard]] GREM_ALWAYS_INLINE constexpr size_type find_first_of(CharT ch, size_type pos = 0) const noexcept {
330 return find(ch, pos);
331 }
332
333 [[nodiscard]] GREM_ALWAYS_INLINE constexpr size_type find_first_of(const CharT* s, size_type pos, size_type count) const {
335 }
336
337 [[nodiscard]] GREM_ALWAYS_INLINE constexpr size_type find_first_of(const CharT* s, size_type pos = 0) const {
338 return find_first_of(CStringViewBase{s}, pos);
339 }
340
341 [[nodiscard]] GREM_ALWAYS_INLINE constexpr size_type find_last_of(StringViewBase<CharT, Traits> v, size_type pos = npos) const noexcept {
342 return static_cast<StringViewBase<CharT, Traits>>(*this).find_last_of(v, pos);
343 }
344
345 template <typename Allocator>
346 [[nodiscard]] GREM_ALWAYS_INLINE constexpr size_type find_last_of(const StringBase<CharT, Traits, Allocator>& s, size_type pos = npos) const noexcept {
347 return static_cast<StringViewBase<CharT, Traits>>(*this).find_last_of(s, pos);
348 }
349
350 [[nodiscard]] GREM_ALWAYS_INLINE constexpr size_type find_last_of(CStringViewBase v, size_type pos = npos) const noexcept {
351 return static_cast<StringViewBase<CharT, Traits>>(*this).find_last_of(v.c_str(), pos);
352 }
353
354 [[nodiscard]] GREM_ALWAYS_INLINE constexpr size_type find_last_of(CharT ch, size_type pos = npos) const noexcept {
355 return static_cast<StringViewBase<CharT, Traits>>(*this).find_last_of(ch, pos);
356 }
357
358 [[nodiscard]] GREM_ALWAYS_INLINE constexpr size_type find_last_of(const CharT* s, size_type pos, size_type count) const {
359 return static_cast<StringViewBase<CharT, Traits>>(*this).find_last_of(s, pos, count);
360 }
361
362 [[nodiscard]] GREM_ALWAYS_INLINE constexpr size_type find_last_of(const CharT* s, size_type pos = npos) const {
363 return static_cast<StringViewBase<CharT, Traits>>(*this).find_last_of(s, pos);
364 }
365
366 [[nodiscard]] GREM_ALWAYS_INLINE constexpr size_type find_first_not_of(StringViewBase<CharT, Traits> v, size_type pos = 0) const noexcept {
367 return findFirstNotOfSubString(v.begin(), v.end(), pos);
368 }
369
370 template <typename Allocator>
371 [[nodiscard]] GREM_ALWAYS_INLINE constexpr size_type find_first_not_of(const StringBase<CharT, Traits, Allocator>& s, size_type pos = 0) const noexcept {
372 return findFirstNotOfSubString(s.begin(), s.end(), pos);
373 }
374
375 [[nodiscard]] GREM_ALWAYS_INLINE constexpr size_type find_first_not_of(CStringViewBase v, size_type pos = 0) const noexcept {
376 return findFirstNotOfSubString(v.begin(), v.end(), pos);
377 }
378
379 [[nodiscard]] GREM_ALWAYS_INLINE constexpr size_type find_first_not_of(CharT ch, size_type pos = 0) const noexcept {
380 return findFirstNotOfCharacter(ch, pos);
381 }
382
383 [[nodiscard]] GREM_ALWAYS_INLINE constexpr size_type find_first_not_of(const CharT* s, size_type pos, size_type count) const {
385 }
386
387 [[nodiscard]] GREM_ALWAYS_INLINE constexpr size_type find_first_not_of(const CharT* s, size_type pos = 0) const {
388 return find_first_not_of(CStringViewBase{s}, pos);
389 }
390
392 return static_cast<StringViewBase<CharT, Traits>>(*this).find_last_not_of(v, pos);
393 }
394
395 template <typename Allocator>
396 [[nodiscard]] GREM_ALWAYS_INLINE constexpr size_type find_last_not_of(const StringBase<CharT, Traits, Allocator>& s, size_type pos = npos) const noexcept {
397 return static_cast<StringViewBase<CharT, Traits>>(*this).find_last_not_of(s, pos);
398 }
399
400 [[nodiscard]] GREM_ALWAYS_INLINE constexpr size_type find_last_not_of(CStringViewBase v, size_type pos = npos) const noexcept {
401 return static_cast<StringViewBase<CharT, Traits>>(*this).find_last_not_of(v.c_str(), pos);
402 }
403
404 [[nodiscard]] GREM_ALWAYS_INLINE constexpr size_type find_last_not_of(CharT ch, size_type pos = npos) const noexcept {
405 return static_cast<StringViewBase<CharT, Traits>>(*this).find_last_not_of(ch, pos);
406 }
407
408 [[nodiscard]] GREM_ALWAYS_INLINE constexpr size_type find_last_not_of(const CharT* s, size_type pos, size_type count) const {
409 return static_cast<StringViewBase<CharT, Traits>>(*this).find_last_not_of(s, pos, count);
410 }
411
412 [[nodiscard]] GREM_ALWAYS_INLINE constexpr size_type find_last_not_of(const CharT* s, size_type pos = npos) const {
413 return static_cast<StringViewBase<CharT, Traits>>(*this).find_last_not_of(s, pos);
414 }
415
416 GREM_ALWAYS_INLINE friend constexpr void swap(CStringViewBase& a, CStringViewBase& b) noexcept {
417 a.swap(b);
418 }
419
420 GREM_ALWAYS_INLINE friend std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& stream, CStringViewBase v) {
421 return stream << v.c_str();
422 }
423
424 [[nodiscard]] GREM_ALWAYS_INLINE friend constexpr bool operator==(CStringViewBase a, CStringViewBase b) noexcept {
425 return a.compare(b) == 0;
426 }
427
428 [[nodiscard]] GREM_ALWAYS_INLINE friend constexpr bool operator==(CStringViewBase a, StringViewBase<CharT, Traits> b) noexcept {
429 return a.compare(b) == 0;
430 }
431
432 [[nodiscard]] GREM_ALWAYS_INLINE friend constexpr bool operator==(StringViewBase<CharT, Traits> a, CStringViewBase b) noexcept {
433 return a.compare(b) == 0;
434 }
435
436 template <typename Allocator>
437 [[nodiscard]] GREM_ALWAYS_INLINE friend constexpr bool operator==(CStringViewBase a, const StringBase<CharT, Traits, Allocator>& b) noexcept {
438 return a.compare(b) == 0;
439 }
440
441 template <typename Allocator>
442 [[nodiscard]] GREM_ALWAYS_INLINE friend constexpr bool operator==(const StringBase<CharT, Traits, Allocator>& a, CStringViewBase b) noexcept {
443 return a.compare(b) == 0;
444 }
445
446 [[nodiscard]] GREM_ALWAYS_INLINE friend constexpr bool operator==(CStringViewBase a, const CharT* b) noexcept {
447 return a.compare(b) == 0;
448 }
449
450 [[nodiscard]] GREM_ALWAYS_INLINE friend constexpr bool operator==(const CharT* a, CStringViewBase b) noexcept {
451 return CStringViewBase{a}.compare(b) == 0;
452 }
453
454 [[nodiscard]] GREM_ALWAYS_INLINE friend constexpr auto operator<=>(CStringViewBase a, CStringViewBase b) noexcept {
455 if constexpr (requires { typename Traits::comparison_category; }) {
456 return static_cast<typename Traits::comparison_category>(a.compare(b) <=> 0);
457 } else {
458 return static_cast<std::weak_ordering>(a.compare(b) <=> 0);
459 }
460 }
461
462 [[nodiscard]] GREM_ALWAYS_INLINE friend constexpr auto operator<=>(CStringViewBase a, StringViewBase<CharT, Traits> b) noexcept {
463 if constexpr (requires { typename Traits::comparison_category; }) {
464 return static_cast<typename Traits::comparison_category>(a.compare(b) <=> 0);
465 } else {
466 return static_cast<std::weak_ordering>(a.compare(b) <=> 0);
467 }
468 }
469
470 [[nodiscard]] GREM_ALWAYS_INLINE friend constexpr auto operator<=>(StringViewBase<CharT, Traits> a, CStringViewBase b) noexcept {
471 if constexpr (requires { typename Traits::comparison_category; }) {
472 return static_cast<typename Traits::comparison_category>(a.compare(b) <=> 0);
473 } else {
474 return static_cast<std::weak_ordering>(a.compare(b) <=> 0);
475 }
476 }
477
478 template <typename Allocator>
479 [[nodiscard]] GREM_ALWAYS_INLINE friend constexpr auto operator<=>(CStringViewBase a, const StringBase<CharT, Traits, Allocator>& b) noexcept {
480 if constexpr (requires { typename Traits::comparison_category; }) {
481 return static_cast<typename Traits::comparison_category>(a.compare(b) <=> 0);
482 } else {
483 return static_cast<std::weak_ordering>(a.compare(b) <=> 0);
484 }
485 }
486
487 template <typename Allocator>
488 [[nodiscard]] GREM_ALWAYS_INLINE friend constexpr auto operator<=>(const StringBase<CharT, Traits, Allocator>& a, CStringViewBase b) noexcept {
489 if constexpr (requires { typename Traits::comparison_category; }) {
490 return static_cast<typename Traits::comparison_category>(a.compare(b) <=> 0);
491 } else {
492 return static_cast<std::weak_ordering>(a.compare(b) <=> 0);
493 }
494 }
495
496 [[nodiscard]] GREM_ALWAYS_INLINE friend constexpr auto operator<=>(CStringViewBase a, const CharT* b) noexcept {
497 if constexpr (requires { typename Traits::comparison_category; }) {
498 return static_cast<typename Traits::comparison_category>(a.compare(b) <=> 0);
499 } else {
500 return static_cast<std::weak_ordering>(a.compare(b) <=> 0);
501 }
502 }
503
504 [[nodiscard]] GREM_ALWAYS_INLINE friend constexpr auto operator<=>(const CharT* a, CStringViewBase b) noexcept {
505 if constexpr (requires { typename Traits::comparison_category; }) {
506 return static_cast<typename Traits::comparison_category>(CStringViewBase{a}.compare(b) <=> 0);
507 } else {
508 return static_cast<std::weak_ordering>(CStringViewBase{a}.compare(b) <=> 0);
509 }
510 }
511
512private:
513 [[nodiscard]] constexpr int compareSubString(auto first, auto last) const {
514 GREM_ASSERT(string);
515 const CharT* p = string;
516 while (true) {
517 if (Traits::eq(*p, CharT())) {
518 return (first == last) ? 0 : -1;
519 }
520 if (first == last) {
521 return (Traits::eq(*p, CharT())) ? 0 : 1;
522 }
523 if (!Traits::eq(*p, *first)) {
524 return (Traits::lt(*p, *first)) ? -1 : 1;
525 }
526 ++p;
527 ++first;
528 }
529 return 0;
530 }
531
532 [[nodiscard]] GREM_ALWAYS_INLINE constexpr bool startsWithCharacter(CharT ch) const {
533 GREM_ASSERT(string);
534 return !Traits::eq(string[0], CharT()) && Traits::eq(string[0], ch);
535 }
536
537 [[nodiscard]] constexpr bool startsWithSubString(auto first, auto last) const {
538 GREM_ASSERT(string);
539 const CharT* p = string;
540 while (true) {
541 if (first == last) {
542 return true;
543 }
544 if (Traits::eq(*p, CharT()) || !Traits::eq(*p, *first)) {
545 return false;
546 }
547 ++p;
548 ++first;
549 }
550 return false;
551 }
552
553 [[nodiscard]] constexpr size_type findCharacter(CharT ch, size_type pos) const {
554 GREM_ASSERT(string);
555 while (!Traits::eq(string[pos], CharT())) {
556 if (Traits::eq(string[pos], ch)) {
557 return pos;
558 }
559 ++pos;
560 }
561 return npos;
562 }
563
564 [[nodiscard]] constexpr size_type findSubString(auto first, auto last, size_type pos) const {
565 GREM_ASSERT(string);
566 while (true) {
567 size_type subPos = pos;
568 auto it = first;
569 while (true) {
570 if (Traits::eq(string[subPos], CharT())) {
571 return (it == last) ? pos : npos;
572 }
573 if (it == last) {
574 return pos;
575 }
576 if (!Traits::eq(string[subPos], *it)) {
577 break;
578 }
579 ++subPos;
580 ++it;
581 }
582 ++pos;
583 }
584 return npos;
585 }
586
587 [[nodiscard]] constexpr size_type findFirstOfSubString(auto first, auto last, size_type pos) const {
588 GREM_ASSERT(string);
589 while (!Traits::eq(string[pos], CharT())) {
590 const CharT ch = string[pos];
591 for (auto it = first; it != last; ++it) {
592 if (Traits::eq(ch, *it)) {
593 return pos;
594 }
595 }
596 ++pos;
597 }
598 return npos;
599 }
600
601 [[nodiscard]] constexpr size_type findFirstNotOfCharacter(CharT ch, size_type pos) const {
602 GREM_ASSERT(string);
603 while (!Traits::eq(string[pos], CharT())) {
604 if (!Traits::eq(string[pos], ch)) {
605 return pos;
606 }
607 ++pos;
608 }
609 return npos;
610 }
611
612 [[nodiscard]] constexpr size_type findFirstNotOfSubString(auto first, auto last, size_type pos) const {
613 GREM_ASSERT(string);
614 while (!Traits::eq(string[pos], CharT())) {
615 const CharT ch = string[pos];
616 auto it = first;
617 for (; it != last; ++it) {
618 if (Traits::eq(ch, *it)) {
619 break;
620 }
621 }
622 if (it == last) {
623 return pos;
624 }
625 ++pos;
626 }
627 return npos;
628 }
629
630 static constexpr CharT EMPTY_STRING_DATA = CharT();
631
632 const_pointer string = &EMPTY_STRING_DATA;
633};
634
640
641} // namespace grem
642
643template <typename CharT, typename Traits>
644struct std::hash<grem::CStringViewBase<CharT, Traits>> {
645 [[nodiscard]] std::size_t operator()(const grem::CStringViewBase<CharT, Traits>& sv) const {
646 return hasher(static_cast<grem::StringViewBase<CharT, Traits>>(sv));
647 }
648
649private:
650 [[no_unique_address]] std::hash<grem::StringViewBase<CharT, Traits>> hasher;
651};
652
653#endif
#define GREM_ASSERT(expression)
Definition assertions.hpp:77
#define GREM_ALWAYS_INLINE
Definition attributes.hpp:10
Definition CStringView.hpp:37
GREM_ALWAYS_INLINE constexpr size_type rfind(const CharT *s, size_type pos=npos) const
Definition CStringView.hpp:312
GREM_ALWAYS_INLINE friend constexpr bool operator==(const StringBase< CharT, Traits, Allocator > &a, CStringViewBase b) noexcept
Definition CStringView.hpp:442
GREM_ALWAYS_INLINE constexpr size_type find_first_not_of(const CharT *s, size_type pos, size_type count) const
Definition CStringView.hpp:383
GREM_ALWAYS_INLINE constexpr int compare(size_type pos1, size_type count1, const CharT *s, size_type count2) const
Definition CStringView.hpp:204
GREM_ALWAYS_INLINE constexpr const_iterator begin() const noexcept
Definition CStringView.hpp:73
const CharT & const_reference
Definition CStringView.hpp:44
GREM_ALWAYS_INLINE constexpr size_type find_first_of(const CharT *s, size_type pos=0) const
Definition CStringView.hpp:337
GREM_ALWAYS_INLINE friend constexpr auto operator<=>(CStringViewBase a, CStringViewBase b) noexcept
Definition CStringView.hpp:454
GREM_ALWAYS_INLINE constexpr const_reverse_iterator crend() const noexcept
Definition CStringView.hpp:101
GREM_ALWAYS_INLINE friend constexpr auto operator<=>(const CharT *a, CStringViewBase b) noexcept
Definition CStringView.hpp:504
GREM_ALWAYS_INLINE friend constexpr bool operator==(CStringViewBase a, StringViewBase< CharT, Traits > b) noexcept
Definition CStringView.hpp:428
GREM_ALWAYS_INLINE constexpr const_reference front() const
Definition CStringView.hpp:114
GREM_ALWAYS_INLINE constexpr bool starts_with(CStringViewBase sv) const noexcept
Definition CStringView.hpp:212
GREM_ALWAYS_INLINE constexpr size_type find_last_not_of(const CharT *s, size_type pos, size_type count) const
Definition CStringView.hpp:408
GREM_ALWAYS_INLINE constexpr bool ends_with(StringViewBase< CharT, Traits > sv) const noexcept
Definition CStringView.hpp:224
GREM_ALWAYS_INLINE constexpr size_type find_last_not_of(const CharT *s, size_type pos=npos) const
Definition CStringView.hpp:412
GREM_ALWAYS_INLINE constexpr size_type find_last_of(const CharT *s, size_type pos, size_type count) const
Definition CStringView.hpp:358
GREM_ALWAYS_INLINE friend constexpr auto operator<=>(const StringBase< CharT, Traits, Allocator > &a, CStringViewBase b) noexcept
Definition CStringView.hpp:488
GREM_ALWAYS_INLINE constexpr size_type find_first_not_of(CharT ch, size_type pos=0) const noexcept
Definition CStringView.hpp:379
const_pointer const_iterator
Definition CStringView.hpp:45
GREM_ALWAYS_INLINE friend constexpr bool operator==(CStringViewBase a, const CharT *b) noexcept
Definition CStringView.hpp:446
GREM_ALWAYS_INLINE friend constexpr bool operator==(CStringViewBase a, CStringViewBase b) noexcept
Definition CStringView.hpp:424
GREM_ALWAYS_INLINE constexpr size_type find(CStringViewBase sv, size_type pos=0) const noexcept
Definition CStringView.hpp:275
GREM_ALWAYS_INLINE constexpr bool contains(StringViewBase< CharT, Traits > sv) const noexcept
Definition CStringView.hpp:245
GREM_ALWAYS_INLINE constexpr size_type find(const CharT *s, size_type pos=0) const
Definition CStringView.hpp:287
GREM_ALWAYS_INLINE constexpr const_reverse_iterator rend() const noexcept
Definition CStringView.hpp:97
std::size_t size_type
Definition CStringView.hpp:49
CharT value_type
Definition CStringView.hpp:40
GREM_ALWAYS_INLINE constexpr const_reverse_iterator crbegin() const noexcept
Definition CStringView.hpp:93
GREM_ALWAYS_INLINE constexpr size_type max_size() const noexcept
Definition CStringView.hpp:141
GREM_ALWAYS_INLINE constexpr size_type find_last_not_of(CharT ch, size_type pos=npos) const noexcept
Definition CStringView.hpp:404
GREM_ALWAYS_INLINE constexpr size_type find_first_of(StringViewBase< CharT, Traits > v, size_type pos=0) const noexcept
Definition CStringView.hpp:316
GREM_ALWAYS_INLINE constexpr size_type find_last_of(StringViewBase< CharT, Traits > v, size_type pos=npos) const noexcept
Definition CStringView.hpp:341
GREM_ALWAYS_INLINE constexpr size_type find_first_not_of(const StringBase< CharT, Traits, Allocator > &s, size_type pos=0) const noexcept
Definition CStringView.hpp:371
GREM_ALWAYS_INLINE constexpr size_type rfind(const CharT *s, size_type pos, size_type count) const
Definition CStringView.hpp:308
GREM_ALWAYS_INLINE constexpr size_type find_last_not_of(CStringViewBase v, size_type pos=npos) const noexcept
Definition CStringView.hpp:400
GREM_ALWAYS_INLINE constexpr bool contains(const CharT *s) const noexcept
Definition CStringView.hpp:262
GREM_ALWAYS_INLINE friend std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &stream, CStringViewBase v)
Definition CStringView.hpp:420
GREM_ALWAYS_INLINE constexpr bool contains(const StringBase< CharT, Traits, Allocator > &s) const noexcept
Definition CStringView.hpp:250
GREM_ALWAYS_INLINE constexpr const_reference back() const
Definition CStringView.hpp:119
GREM_ALWAYS_INLINE constexpr size_type find_last_not_of(const StringBase< CharT, Traits, Allocator > &s, size_type pos=npos) const noexcept
Definition CStringView.hpp:396
CharT & reference
Definition CStringView.hpp:43
GREM_ALWAYS_INLINE constexpr bool starts_with(const CharT *s) const
Definition CStringView.hpp:220
Traits traits_type
Definition CStringView.hpp:39
GREM_ALWAYS_INLINE constexpr int compare(CStringViewBase v) const noexcept
Definition CStringView.hpp:184
GREM_ALWAYS_INLINE friend constexpr auto operator<=>(CStringViewBase a, StringViewBase< CharT, Traits > b) noexcept
Definition CStringView.hpp:462
GREM_ALWAYS_INLINE constexpr bool contains(CStringViewBase sv) const noexcept
Definition CStringView.hpp:254
GREM_ALWAYS_INLINE constexpr const_iterator cbegin() const noexcept
Definition CStringView.hpp:77
GREM_ALWAYS_INLINE constexpr size_type find_first_not_of(CStringViewBase v, size_type pos=0) const noexcept
Definition CStringView.hpp:375
GREM_ALWAYS_INLINE constexpr const_pointer data() const noexcept
Definition CStringView.hpp:124
GREM_ALWAYS_INLINE constexpr size_type find_first_of(CharT ch, size_type pos=0) const noexcept
Definition CStringView.hpp:329
GREM_ALWAYS_INLINE friend constexpr void swap(CStringViewBase &a, CStringViewBase &b) noexcept
Definition CStringView.hpp:416
GREM_ALWAYS_INLINE constexpr size_type find_last_not_of(StringViewBase< CharT, Traits > v, size_type pos=npos) const noexcept
Definition CStringView.hpp:391
GREM_ALWAYS_INLINE constexpr size_type find(const StringBase< CharT, Traits, Allocator > &s, size_type pos=0) const noexcept
Definition CStringView.hpp:271
GREM_ALWAYS_INLINE constexpr bool ends_with(CharT ch) const noexcept
Definition CStringView.hpp:237
GREM_ALWAYS_INLINE constexpr CStringViewBase substr(size_type pos=0) const
Definition CStringView.hpp:166
GREM_ALWAYS_INLINE constexpr CStringViewBase(const StringBase< CharT, Traits, Allocator > &s)
Definition CStringView.hpp:61
GREM_ALWAYS_INLINE constexpr size_type find(StringViewBase< CharT, Traits > sv, size_type pos=0) const noexcept
Definition CStringView.hpp:266
GREM_ALWAYS_INLINE constexpr int compare(const StringBase< CharT, Traits, Allocator > &v) const
Definition CStringView.hpp:180
GREM_ALWAYS_INLINE constexpr void swap(CStringViewBase &v) noexcept
Definition CStringView.hpp:157
const_reverse_iterator reverse_iterator
Definition CStringView.hpp:48
GREM_ALWAYS_INLINE constexpr size_type rfind(CharT ch, size_type pos=npos) const noexcept
Definition CStringView.hpp:304
GREM_ALWAYS_INLINE friend constexpr auto operator<=>(CStringViewBase a, const StringBase< CharT, Traits, Allocator > &b) noexcept
Definition CStringView.hpp:479
GREM_ALWAYS_INLINE friend constexpr bool operator==(CStringViewBase a, const StringBase< CharT, Traits, Allocator > &b) noexcept
Definition CStringView.hpp:437
GREM_ALWAYS_INLINE constexpr sentinel end() const noexcept
Definition CStringView.hpp:81
GREM_ALWAYS_INLINE constexpr size_type find_last_of(const StringBase< CharT, Traits, Allocator > &s, size_type pos=npos) const noexcept
Definition CStringView.hpp:346
GREM_ALWAYS_INLINE constexpr int compare(size_type pos1, size_type count1, const CharT *s) const
Definition CStringView.hpp:200
GREM_ALWAYS_INLINE constexpr size_type length() const noexcept
Definition CStringView.hpp:137
GREM_ALWAYS_INLINE friend constexpr bool operator==(const CharT *a, CStringViewBase b) noexcept
Definition CStringView.hpp:450
GREM_ALWAYS_INLINE constexpr bool ends_with(const StringBase< CharT, Traits, Allocator > &s) const noexcept
Definition CStringView.hpp:229
std::ptrdiff_t difference_type
Definition CStringView.hpp:50
GREM_ALWAYS_INLINE constexpr size_type rfind(const StringBase< CharT, Traits, Allocator > &s, size_type pos=npos) const noexcept
Definition CStringView.hpp:296
GREM_ALWAYS_INLINE constexpr const_reference operator[](size_type pos) const
Definition CStringView.hpp:105
GREM_ALWAYS_INLINE constexpr int compare(size_type pos1, size_type count1, CStringViewBase v) const
Definition CStringView.hpp:188
GREM_ALWAYS_INLINE constexpr CStringViewBase(std::nullptr_t)=delete
GREM_ALWAYS_INLINE friend constexpr auto operator<=>(CStringViewBase a, const CharT *b) noexcept
Definition CStringView.hpp:496
GREM_ALWAYS_INLINE constexpr size_type rfind(CStringViewBase sv, size_type pos=npos) const noexcept
Definition CStringView.hpp:300
GREM_ALWAYS_INLINE constexpr bool contains(CharT c) const noexcept
Definition CStringView.hpp:258
GREM_ALWAYS_INLINE constexpr bool empty() const noexcept
Definition CStringView.hpp:147
GREM_ALWAYS_INLINE constexpr StringViewBase< CharT, Traits > substr(size_type pos, size_type count) const
Definition CStringView.hpp:171
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition CStringView.hpp:47
GREM_ALWAYS_INLINE constexpr bool starts_with(CharT ch) const noexcept
Definition CStringView.hpp:216
GREM_ALWAYS_INLINE constexpr size_type size() const noexcept
Definition CStringView.hpp:132
GREM_ALWAYS_INLINE constexpr size_type find(const CharT *s, size_type pos, size_type count) const
Definition CStringView.hpp:283
GREM_ALWAYS_INLINE constexpr CStringViewBase() noexcept=default
GREM_ALWAYS_INLINE constexpr size_type find(CharT ch, size_type pos=0) const noexcept
Definition CStringView.hpp:279
GREM_ALWAYS_INLINE constexpr size_type find_last_of(CharT ch, size_type pos=npos) const noexcept
Definition CStringView.hpp:354
GREM_ALWAYS_INLINE constexpr sentinel cend() const noexcept
Definition CStringView.hpp:85
GREM_ALWAYS_INLINE constexpr bool starts_with(StringViewBase< CharT, Traits > sv) const noexcept
Definition CStringView.hpp:208
GREM_ALWAYS_INLINE constexpr size_type find_first_of(const StringBase< CharT, Traits, Allocator > &s, size_type pos=0) const noexcept
Definition CStringView.hpp:321
GREM_ALWAYS_INLINE constexpr int compare(size_type pos1, size_type count1, CStringViewBase v, size_type pos2, size_type count2) const
Definition CStringView.hpp:192
GREM_ALWAYS_INLINE constexpr size_type find_last_of(CStringViewBase v, size_type pos=npos) const noexcept
Definition CStringView.hpp:350
GREM_ALWAYS_INLINE constexpr bool ends_with(const CharT *s) const
Definition CStringView.hpp:241
GREM_ALWAYS_INLINE friend constexpr auto operator<=>(StringViewBase< CharT, Traits > a, CStringViewBase b) noexcept
Definition CStringView.hpp:470
const CharT * const_pointer
Definition CStringView.hpp:42
GREM_ALWAYS_INLINE constexpr int compare(StringViewBase< CharT, Traits > v) const
Definition CStringView.hpp:175
GREM_ALWAYS_INLINE constexpr size_type find_last_of(const CharT *s, size_type pos=npos) const
Definition CStringView.hpp:362
GREM_ALWAYS_INLINE constexpr void remove_prefix(size_type n)
Definition CStringView.hpp:152
GREM_ALWAYS_INLINE constexpr int compare(const CharT *s) const
Definition CStringView.hpp:196
GREM_ALWAYS_INLINE constexpr size_type rfind(StringViewBase< CharT, Traits > sv, size_type pos=npos) const noexcept
Definition CStringView.hpp:291
GREM_ALWAYS_INLINE constexpr size_type find_first_not_of(const CharT *s, size_type pos=0) const
Definition CStringView.hpp:387
GREM_ALWAYS_INLINE constexpr const_reference at(size_type pos) const
Definition CStringView.hpp:110
GREM_ALWAYS_INLINE constexpr size_type find_first_not_of(StringViewBase< CharT, Traits > v, size_type pos=0) const noexcept
Definition CStringView.hpp:366
static constexpr size_type npos
Definition CStringView.hpp:53
GREM_ALWAYS_INLINE constexpr size_type copy(CharT *dest, size_type count, size_type pos=0) const
Definition CStringView.hpp:162
GREM_ALWAYS_INLINE constexpr bool ends_with(CStringViewBase sv) const noexcept
Definition CStringView.hpp:233
GREM_ALWAYS_INLINE constexpr const_pointer c_str() const noexcept
Definition CStringView.hpp:128
GREM_ALWAYS_INLINE constexpr const_reverse_iterator rbegin() const noexcept
Definition CStringView.hpp:89
GREM_ALWAYS_INLINE constexpr size_type find_first_of(const CharT *s, size_type pos, size_type count) const
Definition CStringView.hpp:333
const_iterator iterator
Definition CStringView.hpp:46
detail::CStringViewSentinel< CharT, Traits > sentinel
Definition CStringView.hpp:51
GREM_ALWAYS_INLINE friend constexpr bool operator==(StringViewBase< CharT, Traits > a, CStringViewBase b) noexcept
Definition CStringView.hpp:432
GREM_ALWAYS_INLINE constexpr size_type find_first_of(CStringViewBase v, size_type pos=0) const noexcept
Definition CStringView.hpp:325
Definition concepts.hpp:106
Definition aliases.hpp:9
CStringViewBase< char8_t, std::char_traits< char8_t > > UTF8CStringView
Definition CStringView.hpp:636
std::basic_string_view< CharT, Traits > StringViewBase
Definition StringView.hpp:14
constexpr auto count(input_range auto &&range, const auto &value)
Definition algorithms.hpp:234
CStringViewBase< char16_t, std::char_traits< char16_t > > UTF16CStringView
Definition CStringView.hpp:637
CStringViewBase< wchar_t, std::char_traits< wchar_t > > WCStringView
Definition CStringView.hpp:639
CStringViewBase< char32_t, std::char_traits< char32_t > > UTF32CStringView
Definition CStringView.hpp:638
std::basic_string< CharT, Traits, Allocator > StringBase
Definition String.hpp:15
CStringViewBase< char, std::char_traits< char > > CStringView
Definition CStringView.hpp:635
std::size_t operator()(const grem::CStringViewBase< CharT, Traits > &sv) const
Definition CStringView.hpp:645