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
|
// Copyright 2019 Daniel Parker
// Distributed under the Boost license, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
// See https://github.com/danielaparker/jsoncons for latest version
#ifndef JSONCONS_STRING_VIEW_HPP
#define JSONCONS_STRING_VIEW_HPP
#include <stdexcept>
#include <string>
#include <vector>
#include <ostream>
#include <cmath>
#include <algorithm> // std::find, std::min, std::reverse
#include <memory>
#include <iterator>
#include <exception>
#include <stdexcept>
#include <istream> // std::basic_istream
#include <jsoncons/config/compiler_support.hpp>
namespace jsoncons {
namespace detail {
template <class CharT, class Traits = std::char_traits<CharT>>
class basic_string_view
{
private:
const CharT* data_;
std::size_t length_;
public:
using value_type = CharT;
using const_reference = const CharT&;
using traits_type = Traits;
using size_type = std::size_t;
static constexpr size_type npos = size_type(-1);
using const_iterator = const CharT*;
using iterator = const CharT*;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
constexpr basic_string_view() noexcept
: data_(nullptr), length_(0)
{
}
constexpr basic_string_view(const CharT* data, std::size_t length)
: data_(data), length_(length)
{
}
basic_string_view(const CharT* data)
: data_(data), length_(Traits::length(data))
{
}
constexpr basic_string_view(const basic_string_view& other) noexcept = default;
template <class Tr, class Allocator>
JSONCONS_CPP14_CONSTEXPR basic_string_view(const std::basic_string<CharT,Tr,Allocator>& s) noexcept
: data_(s.data()), length_(s.length())
{
}
JSONCONS_CPP14_CONSTEXPR basic_string_view& operator=( const basic_string_view& view ) noexcept
{
data_ = view.data();
length_ = view.length();
return *this;
}
template <class Allocator>
explicit operator std::basic_string<CharT,Traits,Allocator>() const
{
return std::basic_string<CharT,Traits,Allocator>(data_,length_);
}
// iterator support
const_iterator begin() const noexcept
{
return data_;
}
const_iterator end() const noexcept
{
return data_ + length_;
}
const_iterator cbegin() const noexcept
{
return data_;
}
const_iterator cend() const noexcept
{
return data_ + length_;
}
const_reverse_iterator rbegin() const noexcept
{
return const_reverse_iterator(end());
}
const_reverse_iterator rend() const noexcept
{
return const_reverse_iterator(begin());
}
const_reverse_iterator crbegin() const noexcept
{
return const_reverse_iterator(end());
}
const_reverse_iterator crend() const noexcept
{
return const_reverse_iterator(begin());
}
// capacity
std::size_t size() const
{
return length_;
}
std::size_t length() const
{
return length_;
}
size_type max_size() const noexcept
{
return length_;
}
bool empty() const noexcept
{
return length_ == 0;
}
// element access
const_reference operator[](size_type pos) const
{
return data_[pos];
}
const_reference at(std::size_t pos) const
{
if (pos >= length_)
{
JSONCONS_THROW(std::out_of_range("pos exceeds length"));
}
return data_[pos];
}
const_reference front() const
{
return data_[0];
}
const_reference back() const
{
return data_[length_-1];
}
const CharT* data() const
{
return data_;
}
// string operations
basic_string_view substr(size_type pos, size_type n=npos) const
{
if (pos > length_)
{
JSONCONS_THROW(std::out_of_range("pos exceeds size"));
}
if (n == npos || pos + n > length_)
{
n = length_ - pos;
}
return basic_string_view(data_ + pos, n);
}
int compare(const basic_string_view& s) const noexcept
{
const int rc = Traits::compare(data_, s.data_, (std::min)(length_, s.length_));
return rc != 0 ? rc : (length_ == s.length_ ? 0 : length_ < s.length_ ? -1 : 1);
}
int compare(const CharT* data) const noexcept
{
const size_t length = Traits::length(data);
const int rc = Traits::compare(data_, data, (std::min)(length_, length));
return rc != 0 ? rc : (length_ == length? 0 : length_ < length? -1 : 1);
}
template <class Allocator>
int compare(const std::basic_string<CharT,Traits,Allocator>& s) const noexcept
{
const int rc = Traits::compare(data_, s.data(), (std::min)(length_, s.length()));
return rc != 0 ? rc : (length_ == s.length() ? 0 : length_ < s.length() ? -1 : 1);
}
size_type find(basic_string_view s, size_type pos = 0) const noexcept
{
if (pos > length_)
{
return npos;
}
if (s.length_ == 0)
{
return pos;
}
const_iterator it = std::search(cbegin() + pos, cend(),
s.cbegin(), s.cend(), Traits::eq);
return it == cend() ? npos : std::distance(cbegin(), it);
}
size_type find(CharT ch, size_type pos = 0) const noexcept
{
return find(basic_string_view(&ch, 1), pos);
}
size_type find(const CharT* s, size_type pos, size_type n) const noexcept
{
return find(basic_string_view(s, n), pos);
}
size_type find(const CharT* s, size_type pos = 0) const noexcept
{
return find(basic_string_view(s), pos);
}
size_type rfind(basic_string_view s, size_type pos = npos) const noexcept
{
if (length_ < s.length_)
{
return npos;
}
if (pos > length_ - s.length_)
{
pos = length_ - s.length_;
}
if (s.length_ == 0)
{
return pos;
}
for (const CharT* p = data_ + pos; true; --p)
{
if (Traits::compare(p, s.data_, s.length_) == 0)
{
return p - data_;
}
if (p == data_)
{
return npos;
}
};
}
size_type rfind(CharT ch, size_type pos = npos) const noexcept
{
return rfind(basic_string_view(&ch, 1), pos);
}
size_type rfind(const CharT* s, size_type pos, size_type n) const noexcept
{
return rfind(basic_string_view(s, n), pos);
}
size_type rfind(const CharT* s, size_type pos = npos) const noexcept
{
return rfind(basic_string_view(s), pos);
}
size_type find_first_of(basic_string_view s, size_type pos = 0) const noexcept
{
if (pos >= length_ || s.length_ == 0)
{
return npos;
}
const_iterator it = std::find_first_of
(cbegin() + pos, cend(), s.cbegin(), s.cend(), Traits::eq);
return it == cend() ? npos : std::distance (cbegin(), it);
}
size_type find_first_of(CharT ch, size_type pos = 0) const noexcept
{
return find_first_of(basic_string_view(&ch, 1), pos);
}
size_type find_first_of(const CharT* s, size_type pos, size_type n) const noexcept
{
return find_first_of(basic_string_view(s, n), pos);
}
size_type find_first_of(const CharT* s, size_type pos = 0) const noexcept
{
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 (s.length_ == 0)
{
return npos;
}
if (pos >= length_)
{
pos = 0;
}
else
{
pos = length_ - (pos+1);
}
const_reverse_iterator it = std::find_first_of
(crbegin() + pos, crend(), s.cbegin(), s.cend(), Traits::eq);
return it == crend() ? npos : (length_ - 1 - std::distance(crbegin(), it));
}
size_type find_last_of(CharT ch, size_type pos = npos) const noexcept
{
return find_last_of(basic_string_view(&ch, 1), pos);
}
size_type find_last_of(const CharT* s, size_type pos, size_type n) const noexcept
{
return find_last_of(basic_string_view(s, n), pos);
}
size_type find_last_of(const CharT* s, size_type pos = npos) const noexcept
{
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 >= length_)
return npos;
if (s.length_ == 0)
return pos;
const_iterator it = cend();
for (auto p = cbegin()+pos; p != cend(); ++p)
{
if (Traits::find(s.data_, s.length_, *p) == 0)
{
it = p;
break;
}
}
return it == cend() ? npos : std::distance (cbegin(), it);
}
size_type find_first_not_of(CharT ch, size_type pos = 0) const noexcept
{
return find_first_not_of(basic_string_view(&ch, 1), pos);
}
size_type find_first_not_of(const CharT* s, size_type pos, size_type n) const noexcept
{
return find_first_not_of(basic_string_view(s, n), pos);
}
size_type find_first_not_of(const CharT* s, size_type pos = 0) const noexcept
{
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 >= length_)
{
pos = length_ - 1;
}
if (s.length_ == 0)
{
return pos;
}
pos = length_ - (pos+1);
const_iterator it = crend();
for (auto p = crbegin()+pos; p != crend(); ++p)
{
if (Traits::find(s.data_, s.length_, *p) == 0)
{
it = p;
break;
}
}
return it == crend() ? npos : (length_ - 1 - std::distance(crbegin(), it));
}
size_type find_last_not_of(CharT ch, size_type pos = npos) const noexcept
{
return find_last_not_of(basic_string_view(&ch, 1), pos);
}
size_type find_last_not_of(const CharT* s, size_type pos, size_type n) const noexcept
{
return find_last_not_of(basic_string_view(s, n), pos);
}
size_type find_last_not_of(const CharT* s, size_type pos = npos) const noexcept
{
return find_last_not_of(basic_string_view(s), pos);
}
friend std::basic_ostream<CharT>& operator<<(std::basic_ostream<CharT>& os, const basic_string_view& sv)
{
os.write(sv.data_,sv.length_);
return os;
}
};
// ==
template<class CharT,class Traits>
bool operator==(const basic_string_view<CharT,Traits>& lhs,
const basic_string_view<CharT,Traits>& rhs) noexcept
{
return lhs.compare(rhs) == 0;
}
template<class CharT,class Traits,class Allocator>
bool operator==(const basic_string_view<CharT,Traits>& lhs,
const std::basic_string<CharT,Traits,Allocator>& rhs) noexcept
{
return lhs.compare(rhs) == 0;
}
template<class CharT,class Traits,class Allocator>
bool operator==(const std::basic_string<CharT,Traits,Allocator>& lhs,
const basic_string_view<CharT,Traits>& rhs) noexcept
{
return rhs.compare(lhs) == 0;
}
template<class CharT,class Traits>
bool operator==(const basic_string_view<CharT,Traits>& lhs,
const CharT* rhs) noexcept
{
return lhs.compare(rhs) == 0;
}
template<class CharT,class Traits>
bool operator==(const CharT* lhs,
const basic_string_view<CharT,Traits>& rhs) noexcept
{
return rhs.compare(lhs) == 0;
}
// !=
template<class CharT,class Traits>
bool operator!=(const basic_string_view<CharT,Traits>& lhs,
const basic_string_view<CharT,Traits>& rhs) noexcept
{
return lhs.compare(rhs) != 0;
}
template<class CharT,class Traits,class Allocator>
bool operator!=(const basic_string_view<CharT,Traits>& lhs,
const std::basic_string<CharT,Traits,Allocator>& rhs) noexcept
{
return lhs.compare(rhs) != 0;
}
template<class CharT,class Traits,class Allocator>
bool operator!=(const std::basic_string<CharT,Traits,Allocator>& lhs,
const basic_string_view<CharT,Traits>& rhs) noexcept
{
return rhs.compare(lhs) != 0;
}
template<class CharT,class Traits>
bool operator!=(const basic_string_view<CharT,Traits>& lhs,
const CharT* rhs) noexcept
{
return lhs.compare(rhs) != 0;
}
template<class CharT,class Traits>
bool operator!=(const CharT* lhs,
const basic_string_view<CharT,Traits>& rhs) noexcept
{
return rhs.compare(lhs) != 0;
}
// <=
template<class CharT,class Traits>
bool operator<=(const basic_string_view<CharT,Traits>& lhs,
const basic_string_view<CharT,Traits>& rhs) noexcept
{
return lhs.compare(rhs) <= 0;
}
template<class CharT,class Traits,class Allocator>
bool operator<=(const basic_string_view<CharT,Traits>& lhs,
const std::basic_string<CharT,Traits,Allocator>& rhs) noexcept
{
return lhs.compare(rhs) <= 0;
}
template<class CharT,class Traits,class Allocator>
bool operator<=(const std::basic_string<CharT,Traits,Allocator>& lhs,
const basic_string_view<CharT,Traits>& rhs) noexcept
{
return rhs.compare(lhs) >= 0;
}
// <
template<class CharT,class Traits>
bool operator<(const basic_string_view<CharT,Traits>& lhs,
const basic_string_view<CharT,Traits>& rhs) noexcept
{
return lhs.compare(rhs) < 0;
}
template<class CharT,class Traits,class Allocator>
bool operator<(const basic_string_view<CharT,Traits>& lhs,
const std::basic_string<CharT,Traits,Allocator>& rhs) noexcept
{
return lhs.compare(rhs) < 0;
}
template<class CharT,class Traits,class Allocator>
bool operator<(const std::basic_string<CharT,Traits,Allocator>& lhs,
const basic_string_view<CharT,Traits>& rhs) noexcept
{
return rhs.compare(lhs) > 0;
}
// >=
template<class CharT,class Traits>
bool operator>=(const basic_string_view<CharT,Traits>& lhs,
const basic_string_view<CharT,Traits>& rhs) noexcept
{
return lhs.compare(rhs) >= 0;
}
template<class CharT,class Traits,class Allocator>
bool operator>=(const basic_string_view<CharT,Traits>& lhs,
const std::basic_string<CharT,Traits,Allocator>& rhs) noexcept
{
return lhs.compare(rhs) >= 0;
}
template<class CharT,class Traits,class Allocator>
bool operator>=(const std::basic_string<CharT,Traits,Allocator>& lhs,
const basic_string_view<CharT,Traits>& rhs) noexcept
{
return rhs.compare(lhs) <= 0;
}
// >
template<class CharT,class Traits>
bool operator>(const basic_string_view<CharT,Traits>& lhs,
const basic_string_view<CharT,Traits>& rhs) noexcept
{
return lhs.compare(rhs) > 0;
}
template<class CharT,class Traits,class Allocator>
bool operator>(const basic_string_view<CharT,Traits>& lhs,
const std::basic_string<CharT,Traits,Allocator>& rhs) noexcept
{
return lhs.compare(rhs) > 0;
}
template<class CharT,class Traits,class Allocator>
bool operator>(const std::basic_string<CharT,Traits,Allocator>& lhs,
const basic_string_view<CharT,Traits>& rhs) noexcept
{
return rhs.compare(lhs) < 0;
}
} // namespace detail
} // namespace jsoncons
#endif
|