-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_view.h
660 lines (556 loc) · 17.4 KB
/
string_view.h
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
/**
* @file string_view.h
* @author Chase Geigle
*
* All files in META are dual-licensed under the MIT and NCSA licenses. For more
* details, consult the file LICENSE.mit and LICENSE.ncsa in the root of the
* project.
*/
#ifndef META_UTIL_STRING_VIEW_H_
#define META_UTIL_STRING_VIEW_H_
#include "hash.h"
#if META_HAS_EXPERIMENTAL_STRING_VIEW
#include <experimental/string_view>
namespace meta
{
namespace util
{
template <class Char, class Traits = std::char_traits<Char>>
using basic_string_view = std::experimental::basic_string_view<Char, Traits>;
using string_view = basic_string_view<char>;
using u16string_view = basic_string_view<char16_t>;
using u32string_view = basic_string_view<char32_t>;
using wstring_view = basic_string_view<wchar_t>;
}
}
#else
#include <algorithm>
#include <stdexcept>
#include <string>
namespace meta
{
namespace util
{
/**
* A non-owning reference to a string. I make no claims that this is
* completely standards-compliant---this is just a best-effort attempt at
* implementing what we need for MeTA. I have built this using its paper's
* wording for the Fundamentals TS.
*
* @see http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3921.html
*/
template <class Char, class Traits = std::char_traits<Char>>
class basic_string_view
{
public:
using traits_type = Traits;
using value_type = Char;
using pointer = Char*;
using const_pointer = const Char*;
using reference = Char&;
using const_reference = const Char&;
using const_iterator = const_pointer;
using iterator = const_iterator;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
using reverse_iterator = const_reverse_iterator;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
static constexpr size_type npos = size_type(-1);
constexpr basic_string_view() noexcept : data_{nullptr}, size_{0}
{
// nothing
}
constexpr basic_string_view(const basic_string_view&) noexcept = default;
basic_string_view& operator=(const basic_string_view&) noexcept = default;
template <class Allocator>
basic_string_view(
const std::basic_string<Char, Traits, Allocator>& str) noexcept
: data_{str.data()},
size_{str.size()}
{
// nothing
}
constexpr basic_string_view(const Char* str)
: data_{str}, size_{Traits::length(str)}
{
// nothing
}
constexpr basic_string_view(const Char* str, size_type len)
: data_{str}, size_{len}
{
// nothing
}
constexpr const_iterator begin() const noexcept
{
return data_;
}
constexpr const_iterator end() const noexcept
{
return data_ + size_;
}
constexpr const_iterator cbegin() const noexcept
{
return begin();
}
constexpr const_iterator cend() const noexcept
{
return end();
}
const_reverse_iterator rbegin() const noexcept
{
return {end()};
}
const_reverse_iterator rend() const noexcept
{
return {begin()};
}
const_reverse_iterator crbegin() const noexcept
{
return rbegin();
}
const_reverse_iterator crend() const noexcept
{
return rend();
}
constexpr size_type size() const noexcept
{
return size_;
}
constexpr size_type length() const noexcept
{
return size();
}
constexpr size_type max_size() const noexcept
{
return size();
}
constexpr bool empty() const noexcept
{
return size() == 0;
}
constexpr const_reference operator[](size_type pos) const
{
return data_[pos];
}
const_reference at(size_type pos) const
{
if (pos >= size())
throw std::out_of_range{"index out of bounds"};
return data_[pos];
}
constexpr const_reference front() const
{
return data_[0];
}
constexpr const_reference back() const
{
return data_[size_ - 1];
}
constexpr const_pointer data() const noexcept
{
return data_;
}
void clear() noexcept
{
data_ = nullptr;
size_ = 0;
}
void remove_prefix(size_type n)
{
data_ += n;
size_ -= n;
}
void remove_suffix(size_type n)
{
size_ -= n;
}
void swap(basic_string_view& s) noexcept
{
using ::std::swap;
swap(data_, s.data_);
swap(size_, s.size_);
}
template <class Allocator>
explicit operator std::basic_string<Char, Traits, Allocator>() const
{
return {begin(), end()};
}
template <class Allocator = std::allocator<Char>>
std::basic_string<Char, Traits, Allocator> to_string(const Allocator& a
= Allocator{}) const
{
return {begin(), end(), a};
}
size_type copy(Char* s, size_type n, size_type pos = 0) const
{
if (pos > size())
throw std::out_of_range{"index out of bounds"};
auto rlen = std::min(n, size() - pos);
std::copy_n(begin() + pos, rlen, s);
return rlen;
}
constexpr basic_string_view substr(size_type pos = 0,
size_type n = npos) const
{
return pos > size()
? throw std::out_of_range{"index out of bounds"}
: basic_string_view{data() + pos, std::min(n, size() - pos)};
}
int compare(basic_string_view s) const noexcept
{
auto cmp
= Traits::compare(data(), s.data(), std::min(size(), s.size()));
if (cmp != 0)
return cmp;
if (size() < s.size())
return -1;
if (size() == s.size())
return 0;
return 1;
}
constexpr int compare(size_type pos1, size_type n1,
basic_string_view s) const
{
return substr(pos1, n1).compare(s);
}
constexpr int compare(size_type pos1, size_type n1, basic_string_view s,
size_type pos2, size_type n2) const
{
return substr(pos1, n1).compare(s.substr(pos2, n2));
}
constexpr int compare(const Char* s) const
{
return compare(basic_string_view{s});
}
constexpr int compare(size_type pos1, size_type n1, const Char* s) const
{
return substr(pos1, n1).compare(basic_string_view{s});
}
constexpr int compare(size_type pos1, size_type n1, const Char* s,
size_type n2) const
{
return substr(pos1, n1).compare(basic_string_view{s, n2});
}
size_type find(basic_string_view s, size_type pos = 0) const noexcept
{
if (pos >= size())
return npos;
auto it
= std::search(begin() + pos, end(), s.begin(), s.end(), Traits::eq);
if (it == end())
return npos;
return std::distance(begin(), it);
}
constexpr size_type find(Char c, size_type pos = 0) const noexcept
{
return find(basic_string_view{&c, 1}, pos);
}
constexpr size_type find(const Char* s, size_type pos, size_type n) const
{
return find(basic_string_view{s, n}, pos);
}
constexpr size_type find(const Char* s, size_type pos = 0) const
{
return find(basic_string_view{s}, pos);
}
size_type rfind(basic_string_view s, size_type pos = npos) const noexcept
{
if (size() < s.size())
return npos;
pos = std::min(pos, size());
if (s.size() < size() - pos)
pos += s.size();
else
pos = size();
auto it = std::find_end(begin(), begin() + pos, s.begin(), s.end(),
Traits::eq);
if (it == begin() + pos)
return npos;
return std::distance(begin(), it);
}
constexpr size_type rfind(Char c, size_type pos = npos) const noexcept
{
return rfind(basic_string_view{&c, 1}, pos);
}
constexpr size_type rfind(const Char* s, size_type pos, size_type n) const
{
return rfind(basic_string_view{s, n}, pos);
}
constexpr size_type rfind(const Char* s, size_type pos = npos) const
{
return rfind(basic_string_view{s}, pos);
}
size_type find_first_of(basic_string_view s, size_type pos = 0) const
noexcept
{
if (pos >= size())
return npos;
auto it = std::find_first_of(begin() + pos, end(), s.begin(), s.end(),
Traits::eq);
if (it == end())
return npos;
return std::distance(begin(), it);
}
constexpr size_type find_first_of(Char c, size_type pos = 0) const noexcept
{
return find_first_of(basic_string_view{&c, 1}, pos);
}
constexpr size_type find_first_of(const Char* s, size_type pos,
size_type n) const
{
return find_first_of(basic_string_view{s, n}, pos);
}
constexpr size_type find_first_of(const Char* s, size_type pos = 0) const
{
return find_first_of(basic_string_view{s}, pos);
}
size_type find_last_of(basic_string_view s, size_type pos = npos) const
noexcept
{
if (pos >= size())
return npos;
auto diff = size() - std::min(size(), pos);
auto it = std::find_first_of(rbegin() + diff, rend(), s.begin(),
s.end(), Traits::eq);
if (it == rend())
return npos;
return size() - 1 - std::distance(rbegin(), it);
}
constexpr size_type find_last_of(Char c, size_type pos = npos) const
noexcept
{
return find_last_of(basic_string_view{&c, 1}, pos);
}
constexpr size_type find_last_of(const Char* s, size_type pos,
size_type n) const
{
return find_last_of(basic_string_view{s, n}, pos);
}
constexpr size_type find_last_of(const Char* s, size_type pos = npos) const
{
return find_last_of(basic_string_view{s}, pos);
}
size_type find_first_not_of(basic_string_view s, size_type pos = 0) const
noexcept
{
if (pos >= size())
return npos;
auto it = std::find_if(begin(), end(), [&](const_reference c)
{
return std::find(s.begin(), s.end(), c,
Traits::eq) == s.end();
});
if (it == end())
return npos;
return std::distance(begin(), it);
}
constexpr size_type find_first_not_of(Char c, size_type pos = 0) const
noexcept
{
return find_first_not_of(basic_string_view{&c, 1}, pos);
}
constexpr size_type find_first_not_of(const Char* s, size_type pos,
size_type n) const
{
return find_first_not_of(basic_string_view{s, n}, pos);
}
constexpr size_type find_first_not_of(const Char* s,
size_type pos = 0) const
{
return find_first_not_of(basic_string_view{s}, pos);
}
size_type find_last_not_of(basic_string_view s, size_type pos = npos) const
noexcept
{
if (pos >= size())
return npos;
auto diff = size() - std::min(size(), pos);
auto it = std::find_if(rbegin() + diff, rend(), [&](const_reference c)
{
return std::find(s.begin(), s.end(), c,
Traits::eq) == s.end();
});
if (it == rend())
return npos;
return size() - 1 - std::distance(rbegin(), it);
}
constexpr size_type find_last_not_of(Char c, size_type pos = npos) const
noexcept
{
return find_last_not_of(basic_string_view{&c, 1}, pos);
}
constexpr size_type find_last_not_of(const Char* s, size_type pos,
size_type n) const
{
return find_last_not_of(basic_string_view{s, n}, pos);
}
constexpr size_type find_last_not_of(const Char* s,
size_type pos = npos) const
{
return find_last_not_of(basic_string_view{s}, pos);
}
private:
const_pointer data_;
size_type size_;
};
using string_view = basic_string_view<char>;
using u16string_view = basic_string_view<char16_t>;
using u32string_view = basic_string_view<char32_t>;
using wstring_view = basic_string_view<wchar_t>;
namespace
{
template <class T>
using identity = typename std::decay<T>::type;
}
template <class Char, class Traits>
constexpr bool operator==(basic_string_view<Char, Traits> lhs,
basic_string_view<Char, Traits> rhs) noexcept
{
return lhs.compare(rhs) == 0;
}
template <class Char, class Traits>
constexpr bool
operator==(basic_string_view<Char, Traits> lhs,
identity<basic_string_view<Char, Traits>> rhs) noexcept
{
return lhs.compare(rhs) == 0;
}
template <class Char, class Traits>
constexpr bool operator==(identity<basic_string_view<Char, Traits>> lhs,
basic_string_view<Char, Traits> rhs) noexcept
{
return lhs.compare(rhs) == 0;
}
template <class Char, class Traits>
constexpr bool operator!=(basic_string_view<Char, Traits> lhs,
basic_string_view<Char, Traits> rhs) noexcept
{
return lhs.compare(rhs) != 0;
}
template <class Char, class Traits>
constexpr bool
operator!=(basic_string_view<Char, Traits> lhs,
identity<basic_string_view<Char, Traits>> rhs) noexcept
{
return lhs.compare(rhs) != 0;
}
template <class Char, class Traits>
constexpr bool operator!=(identity<basic_string_view<Char, Traits>> lhs,
basic_string_view<Char, Traits> rhs) noexcept
{
return lhs.compare(rhs) != 0;
}
template <class Char, class Traits>
constexpr bool operator<(basic_string_view<Char, Traits> lhs,
basic_string_view<Char, Traits> rhs) noexcept
{
return lhs.compare(rhs) < 0;
}
template <class Char, class Traits>
constexpr bool operator<(basic_string_view<Char, Traits> lhs,
identity<basic_string_view<Char, Traits>> rhs) noexcept
{
return lhs.compare(rhs) < 0;
}
template <class Char, class Traits>
constexpr bool operator<(identity<basic_string_view<Char, Traits>> lhs,
basic_string_view<Char, Traits> rhs) noexcept
{
return lhs.compare(rhs) < 0;
}
template <class Char, class Traits>
constexpr bool operator>(basic_string_view<Char, Traits> lhs,
basic_string_view<Char, Traits> rhs) noexcept
{
return lhs.compare(rhs) > 0;
}
template <class Char, class Traits>
constexpr bool operator>(basic_string_view<Char, Traits> lhs,
identity<basic_string_view<Char, Traits>> rhs) noexcept
{
return lhs.compare(rhs) > 0;
}
template <class Char, class Traits>
constexpr bool operator>(identity<basic_string_view<Char, Traits>> lhs,
basic_string_view<Char, Traits> rhs) noexcept
{
return lhs.compare(rhs) > 0;
}
template <class Char, class Traits>
constexpr bool operator<=(basic_string_view<Char, Traits> lhs,
basic_string_view<Char, Traits> rhs) noexcept
{
return lhs.compare(rhs) <= 0;
}
template <class Char, class Traits>
constexpr bool
operator<=(basic_string_view<Char, Traits> lhs,
identity<basic_string_view<Char, Traits>> rhs) noexcept
{
return lhs.compare(rhs) <= 0;
}
template <class Char, class Traits>
constexpr bool operator<=(identity<basic_string_view<Char, Traits>> lhs,
basic_string_view<Char, Traits> rhs) noexcept
{
return lhs.compare(rhs) <= 0;
}
template <class Char, class Traits>
constexpr bool operator>=(basic_string_view<Char, Traits> lhs,
basic_string_view<Char, Traits> rhs) noexcept
{
return lhs.compare(rhs) >= 0;
}
template <class Char, class Traits>
constexpr bool
operator>=(basic_string_view<Char, Traits> lhs,
identity<basic_string_view<Char, Traits>> rhs) noexcept
{
return lhs.compare(rhs) >= 0;
}
template <class Char, class Traits>
constexpr bool operator>=(identity<basic_string_view<Char, Traits>> lhs,
basic_string_view<Char, Traits> rhs) noexcept
{
return lhs.compare(rhs) >= 0;
}
template <class Char, class Traits>
std::basic_ostream<Char, Traits>&
operator<<(std::basic_ostream<Char, Traits>& os,
basic_string_view<Char, Traits> str)
{
return os << str.to_string();
}
}
}
namespace std
{
template <class Char, class Traits>
struct hash<meta::util::basic_string_view<Char, Traits>>
: public meta::util::hash<>
{
};
}
#endif // !META_HAS_EXPERIMENTAL_STRING_VIEW
namespace meta
{
namespace util
{
template <class HashAlgorithm, class Char, class Traits>
typename std::enable_if<is_contiguously_hashable<Char>::value>::type
hash_append(HashAlgorithm& h, const basic_string_view<Char, Traits>& s)
{
h(s.data(), s.size() * sizeof(Char));
hash_append(h, s.size());
}
template <class HashAlgorithm, class Char, class Traits>
typename std::enable_if<!is_contiguously_hashable<Char>::value>::type
hash_append(HashAlgorithm& h, const basic_string_view<Char, Traits>& s)
{
for (const auto& c : s)
hash_append(h, c);
hash_append(h, s.size());
}
}
}
#endif // META_UTIL_STRING_VIEW_H_