forked from moonpotato/zstring_view
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzstring_view.hpp
549 lines (452 loc) · 17.5 KB
/
zstring_view.hpp
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
// This file is part of moonpotato/zstring_view
// zstring_view: A non-owning reference to a null-terminated part of a string
//
// Copyright 2018 moonpotato <[email protected]>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef ZSTRING_VIEW_INCLUDED
#define ZSTRING_VIEW_INCLUDED
#include <cstddef>
#include <ostream>
#include <string_view>
namespace mpt
{
template <typename CharT, typename Traits = std::char_traits<CharT>>
class basic_zstring_view;
using zstring_view = basic_zstring_view<char>;
using wzstring_view = basic_zstring_view<wchar_t>;
using u16zstring_view = basic_zstring_view<char16_t>;
using u32zstring_view = basic_zstring_view<char32_t>;
template <typename CharT, typename Traits>
class basic_zstring_view
{
public:
using underlying_type = std::basic_string_view<CharT, Traits>;
using traits_type = typename underlying_type::traits_type;
using value_type = typename underlying_type::value_type;
using pointer = typename underlying_type::pointer;
using const_pointer = typename underlying_type::const_pointer;
using reference = typename underlying_type::reference;
using const_reference = typename underlying_type::const_reference;
using const_iterator = typename underlying_type::const_iterator;
using iterator = typename underlying_type::iterator;
using const_reverse_iterator = typename underlying_type::const_reverse_iterator;
using reverse_iterator = typename underlying_type::reverse_iterator;
using size_type = typename underlying_type::size_type;
using difference_type = typename underlying_type::difference_type;
static constexpr size_type npos = static_cast<size_type>(-1);
constexpr basic_zstring_view() noexcept = default;
constexpr basic_zstring_view(const basic_zstring_view& other) noexcept = default;
constexpr basic_zstring_view(basic_zstring_view&& other) noexcept = default;
constexpr basic_zstring_view(const CharT* s) : m_view{s} {}
constexpr basic_zstring_view(const std::basic_string<CharT>& s) : m_view{ s } {}
~basic_zstring_view() = default;
// Needed as a workaround for gcc bug #61648
// Allows non-friend string literal operators the ability to indirectly call the private constructor
// Safe calling of this requires *certainty* that s[count] is a null character
// Has to be public thusly, but don't call this
static constexpr basic_zstring_view _INTERNAL_unsafe_make_from_string_range(const CharT* s, size_type count)
{
return {s, count};
}
constexpr basic_zstring_view& operator =(const basic_zstring_view& view) noexcept = default;
constexpr basic_zstring_view& operator =(basic_zstring_view&& view) noexcept = default;
constexpr const_iterator begin() const noexcept
{
return m_view.begin();
}
constexpr const_iterator cbegin() const noexcept
{
return m_view.cbegin();
}
constexpr const_iterator end() const noexcept
{
return m_view.end();
}
constexpr const_iterator cend() const noexcept
{
return m_view.cend();
}
constexpr const_reverse_iterator rbegin() const noexcept
{
return m_view.rbegin();
}
constexpr const_reverse_iterator crbegin() const noexcept
{
return m_view.crbegin();
}
constexpr const_reverse_iterator rend() const noexcept
{
return m_view.rend();
}
constexpr const_reverse_iterator crend() const noexcept
{
return m_view.crend();
}
constexpr const_reference operator [](size_type pos) const
{
return m_view[pos];
}
constexpr const_reference at(size_type pos) const
{
return m_view.at(pos);
}
constexpr const_reference front() const
{
return m_view.front();
}
constexpr const_reference back() const
{
return m_view.back();
}
constexpr const_pointer data() const noexcept
{
return m_view.data();
}
// Additional function
constexpr const_pointer c_str() const noexcept
{
return m_view.data();
}
constexpr size_type size() const noexcept
{
return m_view.size();
}
constexpr size_type length() const noexcept
{
return m_view.length();
}
constexpr size_type max_size() const noexcept
{
return m_view.max_size();
}
constexpr bool empty() const noexcept
{
return m_view.empty();
}
constexpr void remove_prefix(size_type n)
{
m_view.remove_prefix(n);
}
constexpr void swap(basic_zstring_view& v) noexcept
{
swap(m_view, v.m_view);
}
size_type copy(CharT* dest, size_type count, size_type pos = 0) const
{
return m_view.copy(dest, count, pos);
}
constexpr underlying_type substr(size_type pos = 0, size_type count = npos) const
{
return m_view.substr(pos, count);
}
// Additional function
constexpr basic_zstring_view suffix(size_type start = 0) const
{
return basic_zstring_view{m_view.substr(start, npos)};
}
constexpr int compare(underlying_type v) const noexcept
{
return m_view.compare(v);
}
constexpr int compare(size_type pos1, size_type count1, underlying_type v) const
{
return m_view.compare(pos1, count1, v);
}
constexpr int compare(size_type pos1, size_type count1, underlying_type v,
size_type pos2, size_type count2) const
{
return m_view.compare(pos1, count1, v, pos2, count2);
}
constexpr int compare(const CharT* s) const
{
return m_view.compare(s);
}
constexpr int compare(size_type pos1, size_type count1,
const CharT* s, size_type count2) const
{
return m_view.compare(pos1, count1, s, count2);
}
constexpr bool starts_with(underlying_type x) const noexcept
{
return m_view.starts_with(x);
}
constexpr bool starts_with(CharT x) const noexcept
{
return m_view.starts_with(x);
}
constexpr bool starts_with(const CharT* x) const
{
return m_view.starts_with(x);
}
constexpr bool ends_with(underlying_type x) const noexcept
{
return m_view.ends_with(x);
}
constexpr bool ends_with(CharT x) const noexcept
{
return m_view.ends_with(x);
}
constexpr bool ends_with(const CharT* x) const
{
return m_view.ends_with(x);
}
constexpr size_type find(underlying_type v, size_type pos = 0) const noexcept
{
return m_view.find(v, pos);
}
constexpr size_type find(CharT ch, size_type pos = 0) const noexcept
{
return m_view.find(ch, pos);
}
constexpr size_type find(const CharT* s, size_type pos, size_type count) const
{
return m_view.find(s, pos, count);
}
constexpr size_type find(const CharT* s, size_type pos = 0) const
{
return m_view.find(s, pos);
}
constexpr size_type rfind(underlying_type v, size_type pos = npos) const noexcept
{
return m_view.rfind(v, pos);
}
constexpr size_type rfind(CharT ch, size_type pos = npos) const noexcept
{
return m_view.rfind(ch, pos);
}
constexpr size_type rfind(const CharT* s, size_type pos, size_type count) const
{
return m_view.rfind(s, pos, count);
}
constexpr size_type rfind(const CharT* s, size_type pos = npos) const
{
return m_view.rfind(s, pos);
}
constexpr size_type find_first_of(underlying_type v, size_type pos = 0) const noexcept
{
return m_view.find_first_of(v, pos);
}
constexpr size_type find_first_of(CharT c, size_type pos = 0) const noexcept
{
return m_view.find_first_of(c, pos);
}
constexpr size_type find_first_of(const CharT* s, size_type pos, size_type count) const
{
return m_view.find_first_of(s, pos, count);
}
constexpr size_type find_first_of(const CharT* s, size_type pos = 0) const
{
return m_view.find_first_of(s, pos);
}
constexpr size_type find_last_of(underlying_type v, size_type pos = npos) const noexcept
{
return m_view.find_last_of(v, pos);
}
constexpr size_type find_last_of(CharT c, size_type pos = npos) const noexcept
{
return m_view.find_last_of(c, pos);
}
constexpr size_type find_last_of(const CharT* s, size_type pos, size_type count) const
{
return m_view.find_last_of(s, pos, count);
}
constexpr size_type find_last_of(const CharT* s, size_type pos = npos) const
{
return m_view.find_last_of(s, pos);
}
constexpr size_type find_first_not_of(underlying_type v, size_type pos = 0) const noexcept
{
return m_view.find_first_not_of(v, pos);
}
constexpr size_type find_first_not_of(CharT c, size_type pos = 0) const noexcept
{
return m_view.find_first_not_of(c, pos);
}
constexpr size_type find_first_not_of(const CharT* s, size_type pos, size_type count) const
{
return m_view.find_first_not_of(s, pos, count);
}
constexpr size_type find_first_not_of(const CharT* s, size_type pos = 0) const
{
return m_view.find_first_not_of(s, pos);
}
constexpr size_type find_last_not_of(underlying_type v, size_type pos = npos) const noexcept
{
return m_view.find_last_not_of(v, pos);
}
constexpr size_type find_last_not_of(CharT c, size_type pos = npos) const noexcept
{
return m_view.find_last_not_of(c, pos);
}
constexpr size_type find_last_not_of(const CharT* s, size_type pos, size_type count) const
{
return m_view.find_last_not_of(s, pos, count);
}
constexpr size_type find_last_not_of(const CharT* s, size_type pos = npos) const
{
return m_view.find_last_not_of(s, pos);
}
constexpr const underlying_type& view() const noexcept
{
return m_view;
}
constexpr operator underlying_type() const noexcept
{
return m_view;
}
private:
// Private constructor, called by the string literal operators
constexpr basic_zstring_view(const CharT* s, size_type count) : m_view{s, count} {}
// Private constructor, needed by suffix()
explicit constexpr basic_zstring_view(const underlying_type& v) noexcept : m_view{v} {}
underlying_type m_view;
};
template <typename CharT, typename Traits>
constexpr bool operator ==(basic_zstring_view<CharT, Traits> lhs,
basic_zstring_view<CharT, Traits> rhs) noexcept
{
return lhs.view() == rhs.view();
}
template <typename CharT, typename Traits>
constexpr bool operator ==(basic_zstring_view<CharT, Traits> lhs,
std::basic_string_view<CharT, Traits> rhs) noexcept
{
return lhs.view() == rhs;
}
template <typename CharT, typename Traits>
constexpr bool operator ==(std::basic_string_view<CharT, Traits> lhs,
basic_zstring_view<CharT, Traits> rhs) noexcept
{
return lhs == rhs.view();
}
template <typename CharT, typename Traits>
constexpr bool operator !=(basic_zstring_view<CharT, Traits> lhs,
basic_zstring_view<CharT, Traits> rhs) noexcept
{
return lhs.view() != rhs.view();
}
template <typename CharT, typename Traits>
constexpr bool operator !=(basic_zstring_view<CharT, Traits> lhs,
std::basic_string_view<CharT, Traits> rhs) noexcept
{
return lhs.view() != rhs;
}
template <typename CharT, typename Traits>
constexpr bool operator !=(std::basic_string_view<CharT, Traits> lhs,
basic_zstring_view<CharT, Traits> rhs) noexcept
{
return lhs != rhs.view();
}
template <typename CharT, typename Traits>
constexpr bool operator <(basic_zstring_view<CharT, Traits> lhs,
basic_zstring_view<CharT, Traits> rhs) noexcept
{
return lhs.view() < rhs.view();
}
template <typename CharT, typename Traits>
constexpr bool operator <(basic_zstring_view<CharT, Traits> lhs,
std::basic_string_view<CharT, Traits> rhs) noexcept
{
return lhs.view() < rhs;
}
template <typename CharT, typename Traits>
constexpr bool operator <(std::basic_string_view<CharT, Traits> lhs,
basic_zstring_view<CharT, Traits> rhs) noexcept
{
return lhs < rhs.view();
}
template <typename CharT, typename Traits>
constexpr bool operator <=(basic_zstring_view<CharT, Traits> lhs,
basic_zstring_view<CharT, Traits> rhs) noexcept
{
return lhs.view() <= rhs.view();
}
template <typename CharT, typename Traits>
constexpr bool operator <=(basic_zstring_view<CharT, Traits> lhs,
std::basic_string_view<CharT, Traits> rhs) noexcept
{
return lhs.view() <= rhs;
}
template <typename CharT, typename Traits>
constexpr bool operator <=(std::basic_string_view<CharT, Traits> lhs,
basic_zstring_view<CharT, Traits> rhs) noexcept
{
return lhs <= rhs.view();
}
template <typename CharT, typename Traits>
constexpr bool operator >(basic_zstring_view<CharT, Traits> lhs,
basic_zstring_view<CharT, Traits> rhs) noexcept
{
return lhs.view() > rhs.view();
}
template <typename CharT, typename Traits>
constexpr bool operator >(basic_zstring_view<CharT, Traits> lhs,
std::basic_string_view<CharT, Traits> rhs) noexcept
{
return lhs.view() > rhs;
}
template <typename CharT, typename Traits>
constexpr bool operator >(std::basic_string_view<CharT, Traits> lhs,
basic_zstring_view<CharT, Traits> rhs) noexcept
{
return lhs > rhs.view();
}
template <typename CharT, typename Traits>
constexpr bool operator >=(basic_zstring_view<CharT, Traits> lhs,
basic_zstring_view<CharT, Traits> rhs) noexcept
{
return lhs.view() >= rhs.view();
}
template <typename CharT, typename Traits>
constexpr bool operator >=(basic_zstring_view<CharT, Traits> lhs,
std::basic_string_view<CharT, Traits> rhs) noexcept
{
return lhs.view() >= rhs;
}
template <typename CharT, typename Traits>
constexpr bool operator >=(std::basic_string_view<CharT, Traits> lhs,
basic_zstring_view<CharT, Traits> rhs) noexcept
{
return lhs >= rhs.view();
}
template <typename CharT, typename Traits>
auto operator << (std::basic_ostream<CharT, Traits>& os,
basic_zstring_view<CharT, Traits> v) -> decltype(os)
{
return os << v.view();
}
inline namespace literals
{
inline namespace zstring_view_literals
{
constexpr zstring_view operator ""_zsv(const char* str, std::size_t len) noexcept
{
return zstring_view::_INTERNAL_unsafe_make_from_string_range(str, len);
}
constexpr u16zstring_view operator ""_zsv(const char16_t* str, std::size_t len) noexcept
{
return u16zstring_view::_INTERNAL_unsafe_make_from_string_range(str, len);
}
constexpr u32zstring_view operator ""_zsv(const char32_t* str, std::size_t len) noexcept
{
return u32zstring_view::_INTERNAL_unsafe_make_from_string_range(str, len);
}
constexpr wzstring_view operator ""_zsv(const wchar_t* str, std::size_t len) noexcept
{
return wzstring_view::_INTERNAL_unsafe_make_from_string_range(str, len);
}
}
}
}
#endif