-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcstring.hpp
More file actions
687 lines (591 loc) · 26.3 KB
/
cstring.hpp
File metadata and controls
687 lines (591 loc) · 26.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
#pragma once
#include <memory>
#include <string>
#include <limits>
#include <atomic>
#include <utility>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <ostream>
#include <iterator>
#include <stdexcept>
#include <string_view>
namespace gto {
/**
* @brief Immutable string based on a plain C-string (char *) with ref-counting.
*
* @details
* - Shared content between multiple instances (using ref counting).
* - Automatic mem dealloc (when no refs point to content).
* - Same sizeof as a 'char *'.
* - Null not allowed (equals to an empty string).
* - Empty string do not require memory allocation.
* - String content is available in debug.
* - Mimics the STL basic_string class.
*
* @details Memory layout:
*
* ----|----|-----------NUL
* ^ ^ ^
* | | |-- string content (NUL-terminated)
* | |-- string length (4-bytes)
* |-- reference counter (4-bytes)
*
* m_str (cstring pointer) points to the string content (to allow viewing content in debug).
* Allocated memory is aligned to ref-counter type size.
* Allocated memory is a multiple of ref-counter type size.
*
* @see https://en.cppreference.com/w/cpp/string/basic_string
* @see https://github.com/torrentg/cstring
*
* @note This class is immutable.
* @version 1.0.6
*/
template<typename Char,
typename Traits = std::char_traits<Char>,
typename Allocator = std::allocator<Char>>
class basic_cstring
{
private: // declarations
using prefix_type = std::uint32_t;
using atomic_prefix_type = std::atomic<prefix_type>;
using pointer = typename std::allocator_traits<Allocator>::pointer;
public: // declarations
using allocator_type = typename std::allocator_traits<Allocator>::template rebind_alloc<prefix_type>;
using allocator_traits = std::allocator_traits<allocator_type>;
using traits_type = Traits;
using size_type = typename std::allocator_traits<Allocator>::size_type;
using difference_type = typename std::allocator_traits<Allocator>::difference_type;
using value_type = Char;
using const_reference = const value_type &;
using const_pointer = typename std::allocator_traits<Allocator>::const_pointer;
using const_iterator = const_pointer;
using const_reverse_iterator = typename std::reverse_iterator<const_iterator>;
using basic_cstring_view = std::basic_string_view<value_type, traits_type>;
public: // static members
//! Special value indicating the maximum achievable index + 1.
static constexpr size_type npos = std::numeric_limits<size_type>::max();
//! Returns the maximum number of elements the string is able to hold.
static constexpr size_type max_size() noexcept {
return std::numeric_limits<prefix_type>::max() - 1;
}
private: // static members
struct empty_cstring_t {
atomic_prefix_type counter{0};
prefix_type len{0};
value_type str[2] = {value_type(), value_type()};
};
static allocator_type m_allocator;
static constexpr empty_cstring_t m_empty{};
private: // members
//! String content (NUL-terminated, prefix_type alignment).
const_pointer m_str = nullptr;
private: // static methods
//! Sanitize a char array pointer avoiding nulls.
static constexpr const_pointer sanitize(const_pointer str) noexcept {
return ((str == nullptr || str[0] == value_type()) ? m_empty.str : str);
}
//! Return pointer to counter from pointer to string.
static constexpr atomic_prefix_type * get_ptr_to_counter(const_pointer str) noexcept
{
static_assert(sizeof(atomic_prefix_type) == sizeof(prefix_type));
static_assert(sizeof(value_type) <= sizeof(prefix_type));
assert(str != nullptr);
if (str == m_empty.str)
return const_cast<atomic_prefix_type *>(&m_empty.counter);
pointer ptr = const_cast<pointer>(str) - (2 * sizeof(prefix_type)) / sizeof(value_type);
return reinterpret_cast<atomic_prefix_type *>(ptr);
}
//! Return pointer to string length from pointer to string.
static constexpr prefix_type * get_ptr_to_length(const_pointer str) noexcept
{
static_assert(sizeof(value_type) <= sizeof(prefix_type));
assert(str != nullptr);
if (str == m_empty.str)
return const_cast<prefix_type *>(&m_empty.len);
pointer ptr = const_cast<pointer>(str) - (sizeof(prefix_type) / sizeof(value_type));
return reinterpret_cast<prefix_type *>(ptr);
}
/**
* Returns the length of the prefix_type array to allocate (in prefix_type sizeof units).
*
* @details Allocator allocates an array of prefix_type (not Char) to grant the memory alignment.
* @details The returned length considere the place for the ending NUL.
*
* @example Let len=6, sizeof(Char)=2, sizeof(prefix_type)=4
* We need to reserve:
* - 4-bytes for the counter (uint32_t)
* - 4-bytes for the length (uint32_t)
* - 6 x 2-bytes = 12-bytes for the string content
* - 1 x 2-bytes = 2-bytes for the terminating NUL
* Total to reserve = 22-bytes
* Expresed in 4-bytes (uint32_t) units = 6 (upper rounding)
* In this case there are 2 ending bytes acting as padding.
*
* @param[in] len Length of the string (without ending NUL).
*
* @return Length of the prefix_type array (in prefix_type sizeof units).
*/
static size_type get_allocated_length(size_type len) noexcept
{
static_assert(sizeof(value_type) <= sizeof(prefix_type));
return 3 + (len * sizeof(value_type)) / sizeof(prefix_type);
}
/**
* Allocate memory for the ref-counter + length + string + NUL.
*
* @details We allocate prefix_types to grant memory alignment.
*
* @param[in] len Length to reserve (of prefix_types).
*
* @return A pointer to the allocated memory.
*/
[[nodiscard]]
static prefix_type * allocate(size_type len)
{
static_assert(sizeof(atomic_prefix_type) == sizeof(prefix_type));
static_assert(alignof(value_type) <= alignof(prefix_type));
static_assert(sizeof(value_type) <= sizeof(prefix_type));
assert(len > 0);
prefix_type *ptr = allocator_traits::allocate(m_allocator, len);
assert(reinterpret_cast<std::size_t>(ptr) % alignof(prefix_type) == 0);
return ptr;
}
//! Deallocates the memory allocated by the object.
static void deallocate(const_pointer str) noexcept
{
assert(str != nullptr);
assert(str != m_empty.str);
atomic_prefix_type *ptr = get_ptr_to_counter(str);
prefix_type len = *get_ptr_to_length(str);
size_type n = get_allocated_length(len);
ptr->~atomic_prefix_type();
allocator_traits::deallocate(m_allocator, reinterpret_cast<prefix_type *>(ptr), n);
}
/**
* Decrements the ref-counter.
* If no more references then deallocate the memory.
* The empty string is never deallocated.
*
* @param[in] str Memory to release.
*/
static void release(const_pointer str) noexcept
{
if (!str || str == m_empty.str)
return;
atomic_prefix_type *ptr = get_ptr_to_counter(str);
prefix_type counts = ptr[0].load(std::memory_order_relaxed);
if (counts == 0) // constant (eg. m_empty)
return;
if (counts > 1)
counts = ptr[0].fetch_sub(1, std::memory_order_relaxed);
if (counts == 1)
deallocate(str);
}
//! Increment the reference counter (except for constants).
static void increment_ref_counter(const_pointer str) noexcept
{
atomic_prefix_type *ptr = get_ptr_to_counter(str);
if (ptr[0].load(std::memory_order_relaxed) > 0)
ptr[0].fetch_add(1, std::memory_order_relaxed);
}
public: // methods
/**
* Constructs a default cstring (empty string).
*/
basic_cstring() noexcept : basic_cstring(nullptr) {}
/**
* Constructs a cstring with a copy of the NULL-terminated str.
* The length is determined by the first NULL character.
*
* @param[in] str The NULL-terminated string to copy (NULL means the empty string).
*/
basic_cstring(const_pointer str) : basic_cstring(str, (str == nullptr ? 0 : traits_type::length(str))) {}
/**
* Constructs a cstring with the first len characters pointed by str.
*
* A terminating NUL character will be added.
* str can contain null characters.
*
* @param[in] str The string to copy (NULL means the empty string, not necessarily NUL-terminated).
* @param[in] len The number of characters to copy (0 means the empty string).
*/
basic_cstring(const_pointer str, size_type len)
{
if (len > max_size())
throw std::length_error("invalid cstring length");
if (str == nullptr || len == 0) {
m_str = m_empty.str;
} else {
size_type n = get_allocated_length(len);
prefix_type *ptr = allocate(n);
std::atomic_init(reinterpret_cast<atomic_prefix_type*>(ptr), 1); // ref-counter = 1
ptr[1] = static_cast<prefix_type>(len); // length
pointer content = reinterpret_cast<pointer>(ptr + 2);
traits_type::copy(content, str, len);
content[len] = value_type();
m_str = content;
}
}
/**
* Destructor.
* Decrements the ref-counter if other instances exists.
* Otherwise deallocates memory.
*/
~basic_cstring() { release(m_str); }
//! Copy constructor.
basic_cstring(const basic_cstring &other) noexcept : m_str(other.m_str) { increment_ref_counter(m_str); }
//! Move constructor.
basic_cstring(basic_cstring &&other) noexcept : m_str(std::exchange(other.m_str, m_empty.str)) {}
//! Copy assignment.
basic_cstring & operator=(const basic_cstring &other) noexcept
{
if (m_str == other.m_str)
return *this;
release(m_str);
m_str = other.m_str;
increment_ref_counter(m_str);
return *this;
}
//! Move assignment.
basic_cstring & operator=(basic_cstring &&other) noexcept { std::swap(m_str, other.m_str); return *this; }
//! Conversion to 'const char *'
operator const_pointer() const { return m_str; }
//! Returns the reference-counter value (0 if empty).
prefix_type use_count() const noexcept { return get_ptr_to_counter(m_str)[0].load(std::memory_order_relaxed); }
//! Return length of string.
size_type size() const noexcept { return *(get_ptr_to_length(m_str)); }
//! Return length of string.
size_type length() const noexcept { return *(get_ptr_to_length(m_str)); }
//! Test if string is empty.
bool empty() const noexcept { return (length() == 0); }
//! Returns a reference to the character at specified location pos in range [0, length()].
const_reference operator[](size_type pos) const noexcept { return m_str[pos]; }
//! Returns a reference to the character at specified location pos in range [0, length()].
const_reference at(size_type pos) const { return (pos > length() ? throw std::out_of_range("cstring::at") : m_str[pos]); }
//! Get last character of the string.
const_reference back() const { return (empty() ? throw std::out_of_range("cstring::back") : m_str[length()-1]); }
//! Get first character of the string.
const_reference front() const { return (empty() ? throw std::out_of_range("cstring::front") : m_str[0]); }
//! Returns a non-null pointer to a null-terminated character array.
const_pointer data() const noexcept { assert(m_str != nullptr); return m_str; }
//! Returns a non-null pointer to a null-terminated character array.
const_pointer c_str() const noexcept { return data(); }
//! Returns a string_view of content.
basic_cstring_view view() const noexcept { return basic_cstring_view(m_str, length()); }
// Const iterator to the begin.
const_iterator cbegin() const noexcept { return view().cbegin(); }
// Const iterator to the end.
const_iterator cend() const noexcept { return view().cend(); }
// Const reverse iterator to the begin.
const_reverse_iterator crbegin() const noexcept { return view().crbegin(); }
// Const reverse iterator to the end.
const_reverse_iterator crend() const noexcept { return view().crend(); }
//! Exchanges the contents of the string with those of other.
void swap(basic_cstring &other) noexcept { std::swap(m_str, other.m_str); }
//! Returns the substring [pos, pos+len).
basic_cstring_view substr(size_type pos=0, size_type len=npos) const { return view().substr(pos, len); }
//! Compare contents.
int compare(const basic_cstring &other) const noexcept {
return view().compare(other.view());
}
int compare(size_type pos, size_type len, const basic_cstring &other) const noexcept {
return substr(pos, len).compare(other.view());
}
int compare(size_type pos1, size_type len1, const basic_cstring &other, size_type pos2, size_type len2=npos) const {
return substr(pos1, len1).compare(other.substr(pos2, len2));
}
int compare(const_pointer str) const {
return view().compare(sanitize(str));
}
int compare(size_type pos, size_type len, const_pointer str) const {
return substr(pos, len).compare(sanitize(str));
}
int compare(size_type pos, size_type len, const_pointer str, size_type len2) const {
return substr(pos, len).compare(basic_cstring_view(sanitize(str), len2));
}
int compare(const basic_cstring_view other) const noexcept {
return view().compare(other);
}
//! Checks if the string view begins with the given prefix.
bool starts_with(const basic_cstring &other) const noexcept {
size_type len = other.length();
return (compare(0, len, other) == 0);
}
bool starts_with(const basic_cstring_view sv) const noexcept {
auto len = sv.length();
return (compare(0, len, sv.data(), len) == 0);
}
bool starts_with(const_pointer str) const noexcept {
return starts_with(basic_cstring_view(sanitize(str)));
}
//! Checks if the string ends with the given suffix.
bool ends_with(const basic_cstring &other) const noexcept {
auto len1 = length();
auto len2 = other.length();
return (len1 >= len2 && compare(len1-len2, len2, other) == 0);
}
bool ends_with(const basic_cstring_view sv) const noexcept {
size_type len1 = length();
size_type len2 = sv.length();
return (len1 >= len2 && compare(len1-len2, len2, sv.data(), len2) == 0);
}
bool ends_with(const_pointer str) const noexcept {
return ends_with(basic_cstring_view(sanitize(str)));
}
//! Find the first ocurrence of a substring.
auto find(const basic_cstring &other, size_type pos=0) const noexcept{
return view().find(other.view(), pos);
}
auto find(const_pointer str, size_type pos, size_type len) const {
return view().find(sanitize(str), pos, len);
}
auto find(const_pointer str, size_type pos=0) const {
return view().find(sanitize(str), pos);
}
auto find(value_type c, size_type pos=0) const noexcept {
return view().find(c, pos);
}
//! Find the last occurrence of a substring.
auto rfind(const basic_cstring &other, size_type pos=npos) const noexcept{
return view().rfind(other.view(), pos);
}
auto rfind(const_pointer str, size_type pos, size_type len) const {
return view().rfind(sanitize(str), pos, len);
}
auto rfind(const_pointer str, size_type pos=npos) const {
return view().rfind(sanitize(str), pos);
}
auto rfind(value_type c, size_type pos=npos) const noexcept {
return view().rfind(c, pos);
}
//! Finds the first character equal to one of the given characters.
auto find_first_of(const basic_cstring &other, size_type pos=0) const noexcept {
return view().find_first_of(other.view(), pos);
}
auto find_first_of(const_pointer str, size_type pos, size_type len) const {
return view().find_first_of(sanitize(str), pos, len);
}
auto find_first_of(const_pointer str, size_type pos=0) const {
return view().find_first_of(sanitize(str), pos);
}
auto find_first_of(value_type c, size_type pos=0) const noexcept {
return view().find_first_of(c, pos);
}
//! Finds the first character equal to none of the given characters.
auto find_first_not_of(const basic_cstring &other, size_type pos=0) const noexcept {
return view().find_first_not_of(other.view(), pos);
}
auto find_first_not_of(const_pointer str, size_type pos, size_type len) const {
return view().find_first_not_of(sanitize(str), pos, len);
}
auto find_first_not_of(const_pointer str, size_type pos=0) const {
return view().find_first_not_of(sanitize(str), pos);
}
auto find_first_not_of(value_type c, size_type pos=0) const noexcept {
return view().find_first_not_of(c, pos);
}
//! Finds the last character equal to one of given characters.
auto find_last_of(const basic_cstring &other, size_type pos=npos) const noexcept {
return view().find_last_of(other.view(), pos);
}
auto find_last_of(const_pointer str, size_type pos, size_type len) const {
return view().find_last_of(sanitize(str), pos, len);
}
auto find_last_of(const_pointer str, size_type pos=npos) const {
return view().find_last_of(sanitize(str), pos);
}
auto find_last_of(value_type c, size_type pos=npos) const noexcept {
return view().find_last_of(c, pos);
}
//! Finds the last character equal to none of the given characters.
auto find_last_not_of(const basic_cstring &other, size_type pos=npos) const noexcept {
return view().find_last_not_of(other.view(), pos);
}
auto find_last_not_of(const_pointer str, size_type pos, size_type len) const {
return view().find_last_not_of(sanitize(str), pos, len);
}
auto find_last_not_of(const_pointer str, size_type pos=npos) const {
return view().find_last_not_of(sanitize(str), pos);
}
auto find_last_not_of(value_type c, size_type pos=npos) const noexcept {
return view().find_last_not_of(c, pos);
}
//! Checks if the string contains the given substring.
bool contains(basic_cstring_view sv) const noexcept {
return (view().find(sv) != npos);
}
bool contains(value_type c) const noexcept {
return (find(c) != npos);
}
bool contains(const_pointer str) const noexcept {
return (find(str) != npos);
}
//! Left trim spaces.
basic_cstring_view ltrim() const
{
const_pointer ptr = m_str;
while (std::isspace(*ptr)) ptr++;
return basic_cstring_view(ptr);
}
//! Right trim spaces.
basic_cstring_view rtrim() const
{
const_pointer ptr = m_str + length() - 1;
while (ptr >= m_str && std::isspace(*ptr)) ptr--;
ptr++;
return basic_cstring_view(m_str, static_cast<size_type>(ptr - m_str));
}
//! Trim spaces.
basic_cstring_view trim() const
{
const_pointer ptr1 = m_str;
const_pointer ptr2 = m_str + length() - 1;
while (std::isspace(*ptr1)) ptr1++;
while (ptr2 >= ptr1 && std::isspace(*ptr2)) ptr2--;
ptr2++;
return basic_cstring_view(ptr1, static_cast<size_type>(ptr2 - ptr1));
}
}; // class basic_cstring
//! Static variable declaration
template<typename Char, typename Traits, typename Allocator>
typename gto::basic_cstring<Char, Traits, Allocator>::allocator_type gto::basic_cstring<Char, Traits, Allocator>::m_allocator{};
//! Comparison operators (between basic_cstring)
template<typename Char, typename Traits, typename Allocator>
inline bool operator==(const basic_cstring<Char,Traits,Allocator> &lhs, const basic_cstring<Char,Traits,Allocator> &rhs) noexcept {
return (lhs.compare(rhs) == 0);
}
template<typename Char, typename Traits, typename Allocator>
inline bool operator!=(const basic_cstring<Char,Traits,Allocator> &lhs, const basic_cstring<Char,Traits,Allocator> &rhs) noexcept {
return (lhs.compare(rhs) != 0);
}
template<typename Char, typename Traits, typename Allocator>
inline bool operator<(const basic_cstring<Char,Traits,Allocator> &lhs, const basic_cstring<Char,Traits,Allocator> &rhs) noexcept {
return (lhs.compare(rhs) < 0);
}
template<typename Char, typename Traits, typename Allocator>
inline bool operator<=(const basic_cstring<Char,Traits,Allocator> &lhs, const basic_cstring<Char,Traits,Allocator> &rhs) noexcept {
return (lhs.compare(rhs) <= 0);
}
template<typename Char, typename Traits, typename Allocator>
inline bool operator>(const basic_cstring<Char,Traits,Allocator> &lhs, const basic_cstring<Char,Traits,Allocator> &rhs) noexcept {
return (lhs.compare(rhs) > 0);
}
template<typename Char, typename Traits, typename Allocator>
inline bool operator>=(const basic_cstring<Char,Traits,Allocator> &lhs, const basic_cstring<Char,Traits,Allocator> &rhs) noexcept {
return (lhs.compare(rhs) >= 0);
}
//! Comparison operators (between basic_cstring and Char*)
template<typename Char, typename Traits, typename Allocator>
inline bool operator==(const basic_cstring<Char,Traits,Allocator> &lhs, const Char *rhs) noexcept {
return (lhs.compare(rhs) == 0);
}
template<typename Char, typename Traits, typename Allocator>
inline bool operator!=(const basic_cstring<Char,Traits,Allocator> &lhs, const Char *rhs) noexcept {
return (lhs.compare(rhs) != 0);
}
template<typename Char, typename Traits, typename Allocator>
inline bool operator<(const basic_cstring<Char,Traits,Allocator> &lhs, const Char *rhs) noexcept {
return (lhs.compare(rhs) < 0);
}
template<typename Char, typename Traits, typename Allocator>
inline bool operator<=(const basic_cstring<Char,Traits,Allocator> &lhs, const Char *rhs) noexcept {
return (lhs.compare(rhs) <= 0);
}
template<typename Char, typename Traits, typename Allocator>
inline bool operator>(const basic_cstring<Char,Traits,Allocator> &lhs, const Char *rhs) noexcept {
return (lhs.compare(rhs) > 0);
}
template<typename Char, typename Traits, typename Allocator>
inline bool operator>=(const basic_cstring<Char,Traits,Allocator> &lhs, const Char *rhs) noexcept {
return (lhs.compare(rhs) >= 0);
}
//! Comparison operators (between Char * and basic_cstring)
template<typename Char, typename Traits, typename Allocator>
inline bool operator==(const Char *lhs, const basic_cstring<Char,Traits,Allocator> &rhs) noexcept {
return (rhs.compare(lhs) == 0);
}
template<typename Char, typename Traits, typename Allocator>
inline bool operator!=(const Char *lhs, const basic_cstring<Char,Traits,Allocator> &rhs) noexcept {
return (rhs.compare(lhs) != 0);
}
template<typename Char, typename Traits, typename Allocator>
inline bool operator<(const Char *lhs, const basic_cstring<Char,Traits,Allocator> &rhs) noexcept {
return (rhs.compare(lhs) > 0);
}
template<typename Char, typename Traits, typename Allocator>
inline bool operator<=(const Char *lhs, const basic_cstring<Char,Traits,Allocator> &rhs) noexcept {
return (rhs.compare(lhs) >= 0);
}
template<typename Char, typename Traits, typename Allocator>
inline bool operator>(const Char *lhs, const basic_cstring<Char,Traits,Allocator> &rhs) noexcept {
return (rhs.compare(lhs) < 0);
}
template<typename Char, typename Traits, typename Allocator>
inline bool operator>=(const Char *lhs, const basic_cstring<Char,Traits,Allocator> &rhs) noexcept {
return (rhs.compare(lhs) <= 0);
}
//! Overloading the std::swap algorithm for std::basic_cstring.
template<typename Char, typename Traits, typename Allocator>
inline void swap(gto::basic_cstring<Char,Traits,Allocator> &lhs, gto::basic_cstring<Char,Traits,Allocator> &rhs) noexcept {
lhs.swap(rhs);
}
//! Performs stream output on basic_cstring.
template<typename Char, typename Traits, typename Allocator>
inline std::basic_ostream<Char,Traits> & operator<<(std::basic_ostream<Char,Traits> &os, const gto::basic_cstring<Char,Traits,Allocator> &str) {
return operator<<(os, str.view());
}
//! Utility class to compare basic_cstring to other types.
template<typename Char,
typename Traits = std::char_traits<Char>,
typename Allocator = std::allocator<Char>>
struct basic_cstring_compare
{
using is_transparent = std::true_type;
// basic_cstring vs basic_cstring
bool operator()(const basic_cstring<Char, Traits, Allocator>& lhs, const basic_cstring<Char, Traits, Allocator>& rhs) const noexcept {
return lhs.compare(rhs) < 0;
}
// basic_cstring vs const Char *
bool operator()(const basic_cstring<Char, Traits, Allocator>& lhs, const Char* rhs) const noexcept {
return lhs.compare(rhs) < 0;
}
bool operator()(const Char* lhs, const basic_cstring<Char, Traits, Allocator>& rhs) const noexcept {
return rhs.compare(lhs) > 0;
}
// basic_cstring vs std::basic:string
bool operator()(const basic_cstring<Char, Traits, Allocator>& lhs, const std::basic_string<Char, Traits, Allocator>& rhs) const noexcept {
return lhs.compare(rhs.c_str()) < 0;
}
bool operator()(const std::basic_string<Char, Traits, Allocator>& lhs, const basic_cstring<Char, Traits, Allocator>& rhs) const noexcept {
return rhs.compare(lhs.c_str()) > 0;
}
// basic_cstring vs std::basic:string_view
bool operator()(const basic_cstring<Char, Traits, Allocator>& lhs, const std::basic_string_view<Char, Traits>& rhs) const noexcept {
return lhs.compare(rhs) < 0;
}
bool operator()(const std::basic_string_view<Char, Traits>& lhs, const basic_cstring<Char, Traits, Allocator>& rhs) const noexcept {
return rhs.compare(lhs) > 0;
}
};
// templates incarnations
using cstring = basic_cstring<char>;
using wcstring = basic_cstring<wchar_t> ;
using cstring_compare = basic_cstring_compare<char>;
using wcstring_compare = basic_cstring_compare<wchar_t>;
} // namespace gto
//! The template specializations of std::hash for gto::cstring.
template<>
struct std::hash<gto::cstring> {
std::size_t operator()(const gto::cstring &str) const {
return std::hash<std::string_view>()(str.view());
}
};
//! The template specializations of std::hash for gto::wcstring.
template<>
struct std::hash<gto::wcstring> {
std::size_t operator()(const gto::wcstring &str) const {
return std::hash<std::wstring_view>()(str.view());
}
};